dwarf2out.c revision 260396
1132718Skan/* Output Dwarf2 format symbol table information from GCC.
2132718Skan   Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3169689Skan   2003, 2004, 2005, 2006 Free Software Foundation, Inc.
450397Sobrien   Contributed by Gary Funck (gary@intrepid.com).
550397Sobrien   Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
650397Sobrien   Extensively modified by Jason Merrill (jason@cygnus.com).
750397Sobrien
890075SobrienThis file is part of GCC.
950397Sobrien
1090075SobrienGCC is free software; you can redistribute it and/or modify it under
1190075Sobrienthe terms of the GNU General Public License as published by the Free
1290075SobrienSoftware Foundation; either version 2, or (at your option) any later
1390075Sobrienversion.
1450397Sobrien
1590075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1690075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1790075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1890075Sobrienfor more details.
1950397Sobrien
2050397SobrienYou should have received a copy of the GNU General Public License
2190075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
22169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23169689Skan02110-1301, USA.  */
2450397Sobrien
2590075Sobrien/* TODO: Emit .debug_line header even when there are no functions, since
2690075Sobrien	   the file numbers are used by .debug_info.  Alternately, leave
2790075Sobrien	   out locations for types and decls.
2890075Sobrien	 Avoid talking about ctors and op= for PODs.
2990075Sobrien	 Factor out common prologue sequences into multiple CIEs.  */
3090075Sobrien
3150397Sobrien/* The first part of this file deals with the DWARF 2 frame unwind
3250397Sobrien   information, which is also used by the GCC efficient exception handling
3350397Sobrien   mechanism.  The second part, controlled only by an #ifdef
3450397Sobrien   DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
3550397Sobrien   information.  */
3650397Sobrien
3750397Sobrien#include "config.h"
3850397Sobrien#include "system.h"
39132718Skan#include "coretypes.h"
40132718Skan#include "tm.h"
4150397Sobrien#include "tree.h"
42169689Skan#include "version.h"
4350397Sobrien#include "flags.h"
44117395Skan#include "real.h"
4550397Sobrien#include "rtl.h"
4650397Sobrien#include "hard-reg-set.h"
4750397Sobrien#include "regs.h"
4850397Sobrien#include "insn-config.h"
4950397Sobrien#include "reload.h"
5090075Sobrien#include "function.h"
5150397Sobrien#include "output.h"
5250397Sobrien#include "expr.h"
5390075Sobrien#include "libfuncs.h"
5450397Sobrien#include "except.h"
5550397Sobrien#include "dwarf2.h"
5650397Sobrien#include "dwarf2out.h"
5790075Sobrien#include "dwarf2asm.h"
5850397Sobrien#include "toplev.h"
5990075Sobrien#include "varray.h"
6090075Sobrien#include "ggc.h"
6190075Sobrien#include "md5.h"
6290075Sobrien#include "tm_p.h"
6390075Sobrien#include "diagnostic.h"
6490075Sobrien#include "debug.h"
6590075Sobrien#include "target.h"
6690075Sobrien#include "langhooks.h"
67117395Skan#include "hashtab.h"
68132718Skan#include "cgraph.h"
69169689Skan#include "input.h"
7050397Sobrien
7190075Sobrien#ifdef DWARF2_DEBUGGING_INFO
72132718Skanstatic void dwarf2out_source_line (unsigned int, const char *);
7350397Sobrien#endif
7450397Sobrien
7590075Sobrien/* DWARF2 Abbreviation Glossary:
7690075Sobrien   CFA = Canonical Frame Address
7790075Sobrien	   a fixed address on the stack which identifies a call frame.
7890075Sobrien	   We define it to be the value of SP just before the call insn.
7990075Sobrien	   The CFA register and offset, which may change during the course
8090075Sobrien	   of the function, are used to calculate its value at runtime.
8190075Sobrien   CFI = Call Frame Instruction
8290075Sobrien	   an instruction for the DWARF2 abstract machine
8390075Sobrien   CIE = Common Information Entry
8490075Sobrien	   information describing information common to one or more FDEs
8590075Sobrien   DIE = Debugging Information Entry
8690075Sobrien   FDE = Frame Description Entry
8790075Sobrien	   information describing the stack call frame, in particular,
8890075Sobrien	   how to restore registers
8990075Sobrien
9090075Sobrien   DW_CFA_... = DWARF2 CFA call frame instruction
9190075Sobrien   DW_TAG_... = DWARF2 DIE tag */
9290075Sobrien
93169689Skan#ifndef DWARF2_FRAME_INFO
94169689Skan# ifdef DWARF2_DEBUGGING_INFO
95169689Skan#  define DWARF2_FRAME_INFO \
96169689Skan  (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97169689Skan# else
98169689Skan#  define DWARF2_FRAME_INFO 0
99169689Skan# endif
100169689Skan#endif
101169689Skan
102169689Skan/* Map register numbers held in the call frame info that gcc has
103169689Skan   collected using DWARF_FRAME_REGNUM to those that should be output in
104169689Skan   .debug_frame and .eh_frame.  */
105169689Skan#ifndef DWARF2_FRAME_REG_OUT
106169689Skan#define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
107169689Skan#endif
108169689Skan
10950397Sobrien/* Decide whether we want to emit frame unwind information for the current
11050397Sobrien   translation unit.  */
11150397Sobrien
11250397Sobrienint
113132718Skandwarf2out_do_frame (void)
11450397Sobrien{
115169689Skan  /* We want to emit correct CFA location expressions or lists, so we
116169689Skan     have to return true if we're going to output debug info, even if
117169689Skan     we're not going to output frame or unwind info.  */
11850397Sobrien  return (write_symbols == DWARF2_DEBUG
11990075Sobrien	  || write_symbols == VMS_AND_DWARF2_DEBUG
12090075Sobrien	  || DWARF2_FRAME_INFO
12150397Sobrien#ifdef DWARF2_UNWIND_INFO
122169689Skan	  || (DWARF2_UNWIND_INFO
123169689Skan	      && (flag_unwind_tables
124169689Skan		  || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
12550397Sobrien#endif
12650397Sobrien	  );
12750397Sobrien}
12850397Sobrien
12990075Sobrien/* The size of the target's pointer type.  */
13090075Sobrien#ifndef PTR_SIZE
13190075Sobrien#define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
13290075Sobrien#endif
13390075Sobrien
134117395Skan/* Array of RTXes referenced by the debugging information, which therefore
135117395Skan   must be kept around forever.  */
136169689Skanstatic GTY(()) VEC(rtx,gc) *used_rtx_array;
137117395Skan
138117395Skan/* A pointer to the base of a list of incomplete types which might be
139169689Skan   completed at some later time.  incomplete_types_list needs to be a
140169689Skan   VEC(tree,gc) because we want to tell the garbage collector about
141169689Skan   it.  */
142169689Skanstatic GTY(()) VEC(tree,gc) *incomplete_types;
143117395Skan
144117395Skan/* A pointer to the base of a table of references to declaration
145117395Skan   scopes.  This table is a display which tracks the nesting
146117395Skan   of declaration scopes at the current scope and containing
147117395Skan   scopes.  This table is used to find the proper place to
148117395Skan   define type declaration DIE's.  */
149169689Skanstatic GTY(()) VEC(tree,gc) *decl_scope_table;
150117395Skan
151169689Skan/* Pointers to various DWARF2 sections.  */
152169689Skanstatic GTY(()) section *debug_info_section;
153169689Skanstatic GTY(()) section *debug_abbrev_section;
154169689Skanstatic GTY(()) section *debug_aranges_section;
155169689Skanstatic GTY(()) section *debug_macinfo_section;
156169689Skanstatic GTY(()) section *debug_line_section;
157169689Skanstatic GTY(()) section *debug_loc_section;
158169689Skanstatic GTY(()) section *debug_pubnames_section;
159260396Spfgstatic GTY(()) section *debug_pubtypes_section;
160169689Skanstatic GTY(()) section *debug_str_section;
161169689Skanstatic GTY(()) section *debug_ranges_section;
162169689Skanstatic GTY(()) section *debug_frame_section;
163169689Skan
16450397Sobrien/* How to start an assembler comment.  */
16550397Sobrien#ifndef ASM_COMMENT_START
16650397Sobrien#define ASM_COMMENT_START ";#"
16750397Sobrien#endif
16850397Sobrien
16950397Sobrientypedef struct dw_cfi_struct *dw_cfi_ref;
17050397Sobrientypedef struct dw_fde_struct *dw_fde_ref;
17150397Sobrientypedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
17250397Sobrien
17350397Sobrien/* Call frames are described using a sequence of Call Frame
17450397Sobrien   Information instructions.  The register number, offset
17550397Sobrien   and address fields are provided as possible operands;
17650397Sobrien   their use is selected by the opcode field.  */
17750397Sobrien
178132718Skanenum dw_cfi_oprnd_type {
179132718Skan  dw_cfi_oprnd_unused,
180132718Skan  dw_cfi_oprnd_reg_num,
181132718Skan  dw_cfi_oprnd_offset,
182132718Skan  dw_cfi_oprnd_addr,
183132718Skan  dw_cfi_oprnd_loc
184132718Skan};
185132718Skan
186132718Skantypedef union dw_cfi_oprnd_struct GTY(())
18750397Sobrien{
188169689Skan  unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
189132718Skan  HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
190132718Skan  const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
191132718Skan  struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
19250397Sobrien}
19350397Sobriendw_cfi_oprnd;
19450397Sobrien
195132718Skantypedef struct dw_cfi_struct GTY(())
19650397Sobrien{
19750397Sobrien  dw_cfi_ref dw_cfi_next;
19850397Sobrien  enum dwarf_call_frame_info dw_cfi_opc;
199132718Skan  dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
200132718Skan    dw_cfi_oprnd1;
201132718Skan  dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
202132718Skan    dw_cfi_oprnd2;
20350397Sobrien}
20450397Sobriendw_cfi_node;
20550397Sobrien
20690075Sobrien/* This is how we define the location of the CFA. We use to handle it
20790075Sobrien   as REG + OFFSET all the time,  but now it can be more complex.
20890075Sobrien   It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
20990075Sobrien   Instead of passing around REG and OFFSET, we pass a copy
21090075Sobrien   of this structure.  */
211132718Skantypedef struct cfa_loc GTY(())
21290075Sobrien{
213132718Skan  HOST_WIDE_INT offset;
214132718Skan  HOST_WIDE_INT base_offset;
215169689Skan  unsigned int reg;
21690075Sobrien  int indirect;            /* 1 if CFA is accessed via a dereference.  */
21790075Sobrien} dw_cfa_location;
21890075Sobrien
21950397Sobrien/* All call frame descriptions (FDE's) in the GCC generated DWARF
22050397Sobrien   refer to a single Common Information Entry (CIE), defined at
22190075Sobrien   the beginning of the .debug_frame section.  This use of a single
22250397Sobrien   CIE obviates the need to keep track of multiple CIE's
22350397Sobrien   in the DWARF generation routines below.  */
22450397Sobrien
225132718Skantypedef struct dw_fde_struct GTY(())
22650397Sobrien{
227169689Skan  tree decl;
22890075Sobrien  const char *dw_fde_begin;
22990075Sobrien  const char *dw_fde_current_label;
23090075Sobrien  const char *dw_fde_end;
231169689Skan  const char *dw_fde_hot_section_label;
232169689Skan  const char *dw_fde_hot_section_end_label;
233169689Skan  const char *dw_fde_unlikely_section_label;
234169689Skan  const char *dw_fde_unlikely_section_end_label;
235169689Skan  bool dw_fde_switched_sections;
23650397Sobrien  dw_cfi_ref dw_fde_cfi;
23790075Sobrien  unsigned funcdef_number;
238117395Skan  unsigned all_throwers_are_sibcalls : 1;
23990075Sobrien  unsigned nothrow : 1;
24090075Sobrien  unsigned uses_eh_lsda : 1;
24150397Sobrien}
24250397Sobriendw_fde_node;
24350397Sobrien
24490075Sobrien/* Maximum size (in bytes) of an artificially generated label.  */
24550397Sobrien#define MAX_ARTIFICIAL_LABEL_BYTES	30
24650397Sobrien
24790075Sobrien/* The size of addresses as they appear in the Dwarf 2 data.
24890075Sobrien   Some architectures use word addresses to refer to code locations,
24990075Sobrien   but Dwarf 2 info always uses byte addresses.  On such machines,
25090075Sobrien   Dwarf 2 addresses need to be larger than the architecture's
25190075Sobrien   pointers.  */
25290075Sobrien#ifndef DWARF2_ADDR_SIZE
25390075Sobrien#define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
25450397Sobrien#endif
25550397Sobrien
25650397Sobrien/* The size in bytes of a DWARF field indicating an offset or length
25790075Sobrien   relative to a debug info section, specified to be 4 bytes in the
25890075Sobrien   DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
25990075Sobrien   as PTR_SIZE.  */
26050397Sobrien
26150397Sobrien#ifndef DWARF_OFFSET_SIZE
26250397Sobrien#define DWARF_OFFSET_SIZE 4
26350397Sobrien#endif
26450397Sobrien
265132718Skan/* According to the (draft) DWARF 3 specification, the initial length
266132718Skan   should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
267132718Skan   bytes are 0xffffffff, followed by the length stored in the next 8
268132718Skan   bytes.
269132718Skan
270132718Skan   However, the SGI/MIPS ABI uses an initial length which is equal to
271132718Skan   DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
272132718Skan
273132718Skan#ifndef DWARF_INITIAL_LENGTH_SIZE
274132718Skan#define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
275132718Skan#endif
276132718Skan
27750397Sobrien#define DWARF_VERSION 2
27850397Sobrien
27950397Sobrien/* Round SIZE up to the nearest BOUNDARY.  */
28050397Sobrien#define DWARF_ROUND(SIZE,BOUNDARY) \
28190075Sobrien  ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
28250397Sobrien
28350397Sobrien/* Offsets recorded in opcodes are a multiple of this alignment factor.  */
28490075Sobrien#ifndef DWARF_CIE_DATA_ALIGNMENT
28550397Sobrien#ifdef STACK_GROWS_DOWNWARD
28690075Sobrien#define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
28750397Sobrien#else
28890075Sobrien#define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
28950397Sobrien#endif
29090075Sobrien#endif
29150397Sobrien
292169689Skan/* CIE identifier.  */
293169689Skan#if HOST_BITS_PER_WIDE_INT >= 64
294169689Skan#define DWARF_CIE_ID \
295169689Skan  (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
296169689Skan#else
297169689Skan#define DWARF_CIE_ID DW_CIE_ID
298169689Skan#endif
299169689Skan
30050397Sobrien/* A pointer to the base of a table that contains frame description
30150397Sobrien   information for each routine.  */
302132718Skanstatic GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
30350397Sobrien
30450397Sobrien/* Number of elements currently allocated for fde_table.  */
305132718Skanstatic GTY(()) unsigned fde_table_allocated;
30650397Sobrien
30750397Sobrien/* Number of elements in fde_table currently in use.  */
308132718Skanstatic GTY(()) unsigned fde_table_in_use;
30950397Sobrien
31050397Sobrien/* Size (in elements) of increments by which we may expand the
31150397Sobrien   fde_table.  */
31250397Sobrien#define FDE_TABLE_INCREMENT 256
31350397Sobrien
31450397Sobrien/* A list of call frame insns for the CIE.  */
315132718Skanstatic GTY(()) dw_cfi_ref cie_cfi_head;
31650397Sobrien
317132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
31850397Sobrien/* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
31950397Sobrien   attribute that accelerates the lookup of the FDE associated
32090075Sobrien   with the subprogram.  This variable holds the table index of the FDE
32150397Sobrien   associated with the current function (body) definition.  */
32250397Sobrienstatic unsigned current_funcdef_fde;
323132718Skan#endif
32450397Sobrien
325132718Skanstruct indirect_string_node GTY(())
32690075Sobrien{
327132718Skan  const char *str;
32890075Sobrien  unsigned int refcount;
32990075Sobrien  unsigned int form;
33090075Sobrien  char *label;
33190075Sobrien};
33290075Sobrien
333132718Skanstatic GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
334132718Skan
335132718Skanstatic GTY(()) int dw2_string_counter;
336132718Skanstatic GTY(()) unsigned long dwarf2out_cfi_label_num;
337132718Skan
338132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339132718Skan
34050397Sobrien/* Forward declarations for functions defined in this file.  */
34150397Sobrien
342132718Skanstatic char *stripattributes (const char *);
343132718Skanstatic const char *dwarf_cfi_name (unsigned);
344132718Skanstatic dw_cfi_ref new_cfi (void);
345132718Skanstatic void add_cfi (dw_cfi_ref *, dw_cfi_ref);
346132718Skanstatic void add_fde_cfi (const char *, dw_cfi_ref);
347132718Skanstatic void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
348132718Skanstatic void lookup_cfa (dw_cfa_location *);
349132718Skanstatic void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
350132718Skanstatic void initial_return_save (rtx);
351132718Skanstatic HOST_WIDE_INT stack_adjust_offset (rtx);
352132718Skanstatic void output_cfi (dw_cfi_ref, dw_fde_ref, int);
353132718Skanstatic void output_call_frame_info (int);
354169689Skanstatic void dwarf2out_stack_adjust (rtx, bool);
355132718Skanstatic void flush_queued_reg_saves (void);
356132718Skanstatic bool clobbers_queued_reg_save (rtx);
357132718Skanstatic void dwarf2out_frame_debug_expr (rtx, const char *);
35850397Sobrien
35990075Sobrien/* Support for complex CFA locations.  */
360132718Skanstatic void output_cfa_loc (dw_cfi_ref);
361132718Skanstatic void get_cfa_from_loc_descr (dw_cfa_location *,
362132718Skan				    struct dw_loc_descr_struct *);
36390075Sobrienstatic struct dw_loc_descr_struct *build_cfa_loc
364169689Skan  (dw_cfa_location *, HOST_WIDE_INT);
365132718Skanstatic void def_cfa_1 (const char *, dw_cfa_location *);
36650397Sobrien
36790075Sobrien/* How to start an assembler comment.  */
36890075Sobrien#ifndef ASM_COMMENT_START
36990075Sobrien#define ASM_COMMENT_START ";#"
37050397Sobrien#endif
37150397Sobrien
37250397Sobrien/* Data and reference forms for relocatable data.  */
37350397Sobrien#define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
37450397Sobrien#define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
37550397Sobrien
37690075Sobrien#ifndef DEBUG_FRAME_SECTION
37790075Sobrien#define DEBUG_FRAME_SECTION	".debug_frame"
37850397Sobrien#endif
37950397Sobrien
38050397Sobrien#ifndef FUNC_BEGIN_LABEL
38150397Sobrien#define FUNC_BEGIN_LABEL	"LFB"
38250397Sobrien#endif
38390075Sobrien
38450397Sobrien#ifndef FUNC_END_LABEL
38550397Sobrien#define FUNC_END_LABEL		"LFE"
38650397Sobrien#endif
38790075Sobrien
388169689Skan#ifndef FRAME_BEGIN_LABEL
38990075Sobrien#define FRAME_BEGIN_LABEL	"Lframe"
390169689Skan#endif
39150397Sobrien#define CIE_AFTER_SIZE_LABEL	"LSCIE"
39250397Sobrien#define CIE_END_LABEL		"LECIE"
39390075Sobrien#define FDE_LABEL		"LSFDE"
39490075Sobrien#define FDE_AFTER_SIZE_LABEL	"LASFDE"
39550397Sobrien#define FDE_END_LABEL		"LEFDE"
39690075Sobrien#define LINE_NUMBER_BEGIN_LABEL	"LSLT"
39790075Sobrien#define LINE_NUMBER_END_LABEL	"LELT"
39890075Sobrien#define LN_PROLOG_AS_LABEL	"LASLTP"
39990075Sobrien#define LN_PROLOG_END_LABEL	"LELTP"
40090075Sobrien#define DIE_LABEL_PREFIX	"DW"
40150397Sobrien
40250397Sobrien/* The DWARF 2 CFA column which tracks the return address.  Normally this
40350397Sobrien   is the column for PC, or the first column after all of the hard
40450397Sobrien   registers.  */
40550397Sobrien#ifndef DWARF_FRAME_RETURN_COLUMN
40650397Sobrien#ifdef PC_REGNUM
407132718Skan#define DWARF_FRAME_RETURN_COLUMN	DWARF_FRAME_REGNUM (PC_REGNUM)
40850397Sobrien#else
409132718Skan#define DWARF_FRAME_RETURN_COLUMN	DWARF_FRAME_REGISTERS
41050397Sobrien#endif
41150397Sobrien#endif
41250397Sobrien
41350397Sobrien/* The mapping from gcc register number to DWARF 2 CFA column number.  By
41450397Sobrien   default, we just provide columns for all registers.  */
41550397Sobrien#ifndef DWARF_FRAME_REGNUM
41650397Sobrien#define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
41750397Sobrien#endif
41890075Sobrien
41950397Sobrien/* Hook used by __throw.  */
42050397Sobrien
42150397Sobrienrtx
422132718Skanexpand_builtin_dwarf_sp_column (void)
42350397Sobrien{
424169689Skan  unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
425169689Skan  return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
42650397Sobrien}
42750397Sobrien
42850397Sobrien/* Return a pointer to a copy of the section string name S with all
42950397Sobrien   attributes stripped off, and an asterisk prepended (for assemble_name).  */
43050397Sobrien
43150397Sobrienstatic inline char *
432132718Skanstripattributes (const char *s)
43350397Sobrien{
434169689Skan  char *stripped = XNEWVEC (char, strlen (s) + 2);
43550397Sobrien  char *p = stripped;
43650397Sobrien
43750397Sobrien  *p++ = '*';
43850397Sobrien
43950397Sobrien  while (*s && *s != ',')
44050397Sobrien    *p++ = *s++;
44150397Sobrien
44250397Sobrien  *p = '\0';
44350397Sobrien  return stripped;
44450397Sobrien}
44550397Sobrien
44690075Sobrien/* Generate code to initialize the register size table.  */
44750397Sobrien
44890075Sobrienvoid
449132718Skanexpand_builtin_init_dwarf_reg_sizes (tree address)
45050397Sobrien{
451169689Skan  unsigned int i;
45290075Sobrien  enum machine_mode mode = TYPE_MODE (char_type_node);
453169689Skan  rtx addr = expand_normal (address);
45490075Sobrien  rtx mem = gen_rtx_MEM (BLKmode, addr);
455132718Skan  bool wrote_return_column = false;
45650397Sobrien
457102780Skan  for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
458169689Skan    {
459169689Skan      int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
460169689Skan
461169689Skan      if (rnum < DWARF_FRAME_REGISTERS)
462169689Skan	{
463169689Skan	  HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
464169689Skan	  enum machine_mode save_mode = reg_raw_mode[i];
465169689Skan	  HOST_WIDE_INT size;
466169689Skan
467169689Skan	  if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
468169689Skan	    save_mode = choose_hard_reg_mode (i, 1, true);
469169689Skan	  if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
470169689Skan	    {
471169689Skan	      if (save_mode == VOIDmode)
472169689Skan		continue;
473169689Skan	      wrote_return_column = true;
474169689Skan	    }
475169689Skan	  size = GET_MODE_SIZE (save_mode);
476169689Skan	  if (offset < 0)
477169689Skan	    continue;
478169689Skan
479169689Skan	  emit_move_insn (adjust_address (mem, mode, offset),
480169689Skan			  gen_int_mode (size, mode));
481169689Skan	}
482169689Skan    }
48350397Sobrien
484122180Skan#ifdef DWARF_ALT_FRAME_RETURN_COLUMN
485169689Skan  gcc_assert (wrote_return_column);
486132718Skan  i = DWARF_ALT_FRAME_RETURN_COLUMN;
487132718Skan  wrote_return_column = false;
488132718Skan#else
489132718Skan  i = DWARF_FRAME_RETURN_COLUMN;
490122180Skan#endif
491132718Skan
492132718Skan  if (! wrote_return_column)
493132718Skan    {
494132718Skan      enum machine_mode save_mode = Pmode;
495132718Skan      HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
496132718Skan      HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
497132718Skan      emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
498132718Skan    }
49950397Sobrien}
50050397Sobrien
50150397Sobrien/* Convert a DWARF call frame info. operation to its string name */
50250397Sobrien
50390075Sobrienstatic const char *
504132718Skandwarf_cfi_name (unsigned int cfi_opc)
50550397Sobrien{
50650397Sobrien  switch (cfi_opc)
50750397Sobrien    {
50850397Sobrien    case DW_CFA_advance_loc:
50950397Sobrien      return "DW_CFA_advance_loc";
51050397Sobrien    case DW_CFA_offset:
51150397Sobrien      return "DW_CFA_offset";
51250397Sobrien    case DW_CFA_restore:
51350397Sobrien      return "DW_CFA_restore";
51450397Sobrien    case DW_CFA_nop:
51550397Sobrien      return "DW_CFA_nop";
51650397Sobrien    case DW_CFA_set_loc:
51750397Sobrien      return "DW_CFA_set_loc";
51850397Sobrien    case DW_CFA_advance_loc1:
51950397Sobrien      return "DW_CFA_advance_loc1";
52050397Sobrien    case DW_CFA_advance_loc2:
52150397Sobrien      return "DW_CFA_advance_loc2";
52250397Sobrien    case DW_CFA_advance_loc4:
52350397Sobrien      return "DW_CFA_advance_loc4";
52450397Sobrien    case DW_CFA_offset_extended:
52550397Sobrien      return "DW_CFA_offset_extended";
52650397Sobrien    case DW_CFA_restore_extended:
52750397Sobrien      return "DW_CFA_restore_extended";
52850397Sobrien    case DW_CFA_undefined:
52950397Sobrien      return "DW_CFA_undefined";
53050397Sobrien    case DW_CFA_same_value:
53150397Sobrien      return "DW_CFA_same_value";
53250397Sobrien    case DW_CFA_register:
53350397Sobrien      return "DW_CFA_register";
53450397Sobrien    case DW_CFA_remember_state:
53550397Sobrien      return "DW_CFA_remember_state";
53650397Sobrien    case DW_CFA_restore_state:
53750397Sobrien      return "DW_CFA_restore_state";
53850397Sobrien    case DW_CFA_def_cfa:
53950397Sobrien      return "DW_CFA_def_cfa";
54050397Sobrien    case DW_CFA_def_cfa_register:
54150397Sobrien      return "DW_CFA_def_cfa_register";
54250397Sobrien    case DW_CFA_def_cfa_offset:
54350397Sobrien      return "DW_CFA_def_cfa_offset";
54450397Sobrien
54590075Sobrien    /* DWARF 3 */
54690075Sobrien    case DW_CFA_def_cfa_expression:
54790075Sobrien      return "DW_CFA_def_cfa_expression";
54890075Sobrien    case DW_CFA_expression:
54990075Sobrien      return "DW_CFA_expression";
55090075Sobrien    case DW_CFA_offset_extended_sf:
55190075Sobrien      return "DW_CFA_offset_extended_sf";
55290075Sobrien    case DW_CFA_def_cfa_sf:
55390075Sobrien      return "DW_CFA_def_cfa_sf";
55490075Sobrien    case DW_CFA_def_cfa_offset_sf:
55590075Sobrien      return "DW_CFA_def_cfa_offset_sf";
55690075Sobrien
55750397Sobrien    /* SGI/MIPS specific */
55850397Sobrien    case DW_CFA_MIPS_advance_loc8:
55950397Sobrien      return "DW_CFA_MIPS_advance_loc8";
56050397Sobrien
56150397Sobrien    /* GNU extensions */
56250397Sobrien    case DW_CFA_GNU_window_save:
56350397Sobrien      return "DW_CFA_GNU_window_save";
56450397Sobrien    case DW_CFA_GNU_args_size:
56550397Sobrien      return "DW_CFA_GNU_args_size";
56670635Sobrien    case DW_CFA_GNU_negative_offset_extended:
56770635Sobrien      return "DW_CFA_GNU_negative_offset_extended";
56850397Sobrien
56950397Sobrien    default:
57050397Sobrien      return "DW_CFA_<unknown>";
57150397Sobrien    }
57250397Sobrien}
57350397Sobrien
57450397Sobrien/* Return a pointer to a newly allocated Call Frame Instruction.  */
57550397Sobrien
57650397Sobrienstatic inline dw_cfi_ref
577132718Skannew_cfi (void)
57850397Sobrien{
579132718Skan  dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
58050397Sobrien
58150397Sobrien  cfi->dw_cfi_next = NULL;
58250397Sobrien  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
58350397Sobrien  cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
58450397Sobrien
58550397Sobrien  return cfi;
58650397Sobrien}
58750397Sobrien
58850397Sobrien/* Add a Call Frame Instruction to list of instructions.  */
58950397Sobrien
59050397Sobrienstatic inline void
591132718Skanadd_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
59250397Sobrien{
59390075Sobrien  dw_cfi_ref *p;
59450397Sobrien
59550397Sobrien  /* Find the end of the chain.  */
59650397Sobrien  for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
59750397Sobrien    ;
59850397Sobrien
59950397Sobrien  *p = cfi;
60050397Sobrien}
60150397Sobrien
60250397Sobrien/* Generate a new label for the CFI info to refer to.  */
60350397Sobrien
60450397Sobrienchar *
605132718Skandwarf2out_cfi_label (void)
60650397Sobrien{
60750397Sobrien  static char label[20];
60890075Sobrien
609132718Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
61050397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, label);
61150397Sobrien  return label;
61250397Sobrien}
61350397Sobrien
61450397Sobrien/* Add CFI to the current fde at the PC value indicated by LABEL if specified,
61550397Sobrien   or to the CIE if LABEL is NULL.  */
61650397Sobrien
61750397Sobrienstatic void
618132718Skanadd_fde_cfi (const char *label, dw_cfi_ref cfi)
61950397Sobrien{
62050397Sobrien  if (label)
62150397Sobrien    {
62290075Sobrien      dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
62350397Sobrien
62450397Sobrien      if (*label == 0)
62550397Sobrien	label = dwarf2out_cfi_label ();
62650397Sobrien
62750397Sobrien      if (fde->dw_fde_current_label == NULL
62850397Sobrien	  || strcmp (label, fde->dw_fde_current_label) != 0)
62950397Sobrien	{
63090075Sobrien	  dw_cfi_ref xcfi;
63150397Sobrien
632169689Skan	  label = xstrdup (label);
63350397Sobrien
63450397Sobrien	  /* Set the location counter to the new label.  */
63550397Sobrien	  xcfi = new_cfi ();
636169689Skan	  /* If we have a current label, advance from there, otherwise
637169689Skan	     set the location directly using set_loc.  */
638169689Skan	  xcfi->dw_cfi_opc = fde->dw_fde_current_label
639169689Skan			     ? DW_CFA_advance_loc4
640169689Skan			     : DW_CFA_set_loc;
64150397Sobrien	  xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
64250397Sobrien	  add_cfi (&fde->dw_fde_cfi, xcfi);
643169689Skan
644169689Skan	  fde->dw_fde_current_label = label;
64550397Sobrien	}
64650397Sobrien
64750397Sobrien      add_cfi (&fde->dw_fde_cfi, cfi);
64850397Sobrien    }
64950397Sobrien
65050397Sobrien  else
65150397Sobrien    add_cfi (&cie_cfi_head, cfi);
65250397Sobrien}
65350397Sobrien
65450397Sobrien/* Subroutine of lookup_cfa.  */
65550397Sobrien
656169689Skanstatic void
657132718Skanlookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
65850397Sobrien{
65950397Sobrien  switch (cfi->dw_cfi_opc)
66050397Sobrien    {
66150397Sobrien    case DW_CFA_def_cfa_offset:
66290075Sobrien      loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
66350397Sobrien      break;
664169689Skan    case DW_CFA_def_cfa_offset_sf:
665169689Skan      loc->offset
666169689Skan	= cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
667169689Skan      break;
66850397Sobrien    case DW_CFA_def_cfa_register:
66990075Sobrien      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
67050397Sobrien      break;
67150397Sobrien    case DW_CFA_def_cfa:
67290075Sobrien      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
67390075Sobrien      loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
67450397Sobrien      break;
675169689Skan    case DW_CFA_def_cfa_sf:
676169689Skan      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
677169689Skan      loc->offset
678169689Skan	= cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
679169689Skan      break;
68090075Sobrien    case DW_CFA_def_cfa_expression:
68190075Sobrien      get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
68290075Sobrien      break;
68350397Sobrien    default:
68450397Sobrien      break;
68550397Sobrien    }
68650397Sobrien}
68750397Sobrien
68850397Sobrien/* Find the previous value for the CFA.  */
68950397Sobrien
69050397Sobrienstatic void
691132718Skanlookup_cfa (dw_cfa_location *loc)
69250397Sobrien{
69390075Sobrien  dw_cfi_ref cfi;
69450397Sobrien
695169689Skan  loc->reg = INVALID_REGNUM;
69690075Sobrien  loc->offset = 0;
69790075Sobrien  loc->indirect = 0;
69890075Sobrien  loc->base_offset = 0;
69950397Sobrien
70050397Sobrien  for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
70190075Sobrien    lookup_cfa_1 (cfi, loc);
70250397Sobrien
70350397Sobrien  if (fde_table_in_use)
70450397Sobrien    {
70590075Sobrien      dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
70650397Sobrien      for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
70790075Sobrien	lookup_cfa_1 (cfi, loc);
70850397Sobrien    }
70950397Sobrien}
71050397Sobrien
71150397Sobrien/* The current rule for calculating the DWARF2 canonical frame address.  */
71290075Sobrienstatic dw_cfa_location cfa;
71350397Sobrien
71450397Sobrien/* The register used for saving registers to the stack, and its offset
71550397Sobrien   from the CFA.  */
71690075Sobrienstatic dw_cfa_location cfa_store;
71750397Sobrien
71850397Sobrien/* The running total of the size of arguments pushed onto the stack.  */
719132718Skanstatic HOST_WIDE_INT args_size;
72050397Sobrien
72150397Sobrien/* The last args_size we actually output.  */
722132718Skanstatic HOST_WIDE_INT old_args_size;
72350397Sobrien
72450397Sobrien/* Entry point to update the canonical frame address (CFA).
72550397Sobrien   LABEL is passed to add_fde_cfi.  The value of CFA is now to be
72650397Sobrien   calculated from REG+OFFSET.  */
72750397Sobrien
72850397Sobrienvoid
729132718Skandwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
73050397Sobrien{
73190075Sobrien  dw_cfa_location loc;
73290075Sobrien  loc.indirect = 0;
73390075Sobrien  loc.base_offset = 0;
73490075Sobrien  loc.reg = reg;
73590075Sobrien  loc.offset = offset;
73690075Sobrien  def_cfa_1 (label, &loc);
73790075Sobrien}
73850397Sobrien
739169689Skan/* Determine if two dw_cfa_location structures define the same data.  */
740169689Skan
741169689Skanstatic bool
742169689Skancfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
743169689Skan{
744169689Skan  return (loc1->reg == loc2->reg
745169689Skan	  && loc1->offset == loc2->offset
746169689Skan	  && loc1->indirect == loc2->indirect
747169689Skan	  && (loc1->indirect == 0
748169689Skan	      || loc1->base_offset == loc2->base_offset));
749169689Skan}
750169689Skan
75190075Sobrien/* This routine does the actual work.  The CFA is now calculated from
75290075Sobrien   the dw_cfa_location structure.  */
75350397Sobrien
75490075Sobrienstatic void
755132718Skandef_cfa_1 (const char *label, dw_cfa_location *loc_p)
75690075Sobrien{
75790075Sobrien  dw_cfi_ref cfi;
75890075Sobrien  dw_cfa_location old_cfa, loc;
75950397Sobrien
76090075Sobrien  cfa = *loc_p;
76190075Sobrien  loc = *loc_p;
76290075Sobrien
76390075Sobrien  if (cfa_store.reg == loc.reg && loc.indirect == 0)
76490075Sobrien    cfa_store.offset = loc.offset;
76590075Sobrien
76690075Sobrien  loc.reg = DWARF_FRAME_REGNUM (loc.reg);
76790075Sobrien  lookup_cfa (&old_cfa);
76890075Sobrien
76990075Sobrien  /* If nothing changed, no need to issue any call frame instructions.  */
770169689Skan  if (cfa_equal_p (&loc, &old_cfa))
77150397Sobrien    return;
77250397Sobrien
77350397Sobrien  cfi = new_cfi ();
77450397Sobrien
77590075Sobrien  if (loc.reg == old_cfa.reg && !loc.indirect)
77650397Sobrien    {
777169689Skan      /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
778169689Skan	 the CFA register did not change but the offset did.  */
779169689Skan      if (loc.offset < 0)
780169689Skan	{
781169689Skan	  HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
782169689Skan	  gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
783169689Skan
784169689Skan	  cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
785169689Skan	  cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
786169689Skan	}
787169689Skan      else
788169689Skan	{
789169689Skan	  cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
790169689Skan	  cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
791169689Skan	}
79250397Sobrien    }
79350397Sobrien
79450397Sobrien#ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
795169689Skan  else if (loc.offset == old_cfa.offset
796169689Skan	   && old_cfa.reg != INVALID_REGNUM
79790075Sobrien	   && !loc.indirect)
79850397Sobrien    {
79990075Sobrien      /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
80090075Sobrien	 indicating the CFA register has changed to <register> but the
80190075Sobrien	 offset has not changed.  */
80250397Sobrien      cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
80390075Sobrien      cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
80450397Sobrien    }
80550397Sobrien#endif
80650397Sobrien
80790075Sobrien  else if (loc.indirect == 0)
80850397Sobrien    {
80990075Sobrien      /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
81090075Sobrien	 indicating the CFA register has changed to <register> with
81190075Sobrien	 the specified offset.  */
812169689Skan      if (loc.offset < 0)
813169689Skan	{
814169689Skan	  HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
815169689Skan	  gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
816169689Skan
817169689Skan	  cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
818169689Skan	  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
819169689Skan	  cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
820169689Skan	}
821169689Skan      else
822169689Skan	{
823169689Skan	  cfi->dw_cfi_opc = DW_CFA_def_cfa;
824169689Skan	  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
825169689Skan	  cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
826169689Skan	}
82750397Sobrien    }
82890075Sobrien  else
82990075Sobrien    {
83090075Sobrien      /* Construct a DW_CFA_def_cfa_expression instruction to
83190075Sobrien	 calculate the CFA using a full location expression since no
83290075Sobrien	 register-offset pair is available.  */
83390075Sobrien      struct dw_loc_descr_struct *loc_list;
83450397Sobrien
83590075Sobrien      cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
836169689Skan      loc_list = build_cfa_loc (&loc, 0);
83790075Sobrien      cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
83890075Sobrien    }
83990075Sobrien
84050397Sobrien  add_fde_cfi (label, cfi);
84150397Sobrien}
84250397Sobrien
84350397Sobrien/* Add the CFI for saving a register.  REG is the CFA column number.
84450397Sobrien   LABEL is passed to add_fde_cfi.
84550397Sobrien   If SREG is -1, the register is saved at OFFSET from the CFA;
84650397Sobrien   otherwise it is saved in SREG.  */
84750397Sobrien
84850397Sobrienstatic void
849132718Skanreg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
85050397Sobrien{
85190075Sobrien  dw_cfi_ref cfi = new_cfi ();
85250397Sobrien
85350397Sobrien  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
85450397Sobrien
855169689Skan  if (sreg == INVALID_REGNUM)
85650397Sobrien    {
85750397Sobrien      if (reg & ~0x3f)
85850397Sobrien	/* The register number won't fit in 6 bits, so we have to use
85950397Sobrien	   the long form.  */
86050397Sobrien	cfi->dw_cfi_opc = DW_CFA_offset_extended;
86150397Sobrien      else
86250397Sobrien	cfi->dw_cfi_opc = DW_CFA_offset;
86350397Sobrien
86490075Sobrien#ifdef ENABLE_CHECKING
86590075Sobrien      {
86690075Sobrien	/* If we get an offset that is not a multiple of
86790075Sobrien	   DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
86890075Sobrien	   definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
86990075Sobrien	   description.  */
870132718Skan	HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
87190075Sobrien
872169689Skan	gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
87390075Sobrien      }
87490075Sobrien#endif
87550397Sobrien      offset /= DWARF_CIE_DATA_ALIGNMENT;
87650397Sobrien      if (offset < 0)
87790075Sobrien	cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
87890075Sobrien
87950397Sobrien      cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
88050397Sobrien    }
88190075Sobrien  else if (sreg == reg)
882169689Skan    cfi->dw_cfi_opc = DW_CFA_same_value;
88350397Sobrien  else
88450397Sobrien    {
88550397Sobrien      cfi->dw_cfi_opc = DW_CFA_register;
88650397Sobrien      cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
88750397Sobrien    }
88850397Sobrien
88950397Sobrien  add_fde_cfi (label, cfi);
89050397Sobrien}
89150397Sobrien
89250397Sobrien/* Add the CFI for saving a register window.  LABEL is passed to reg_save.
89350397Sobrien   This CFI tells the unwinder that it needs to restore the window registers
89450397Sobrien   from the previous frame's window save area.
89590075Sobrien
89650397Sobrien   ??? Perhaps we should note in the CIE where windows are saved (instead of
89750397Sobrien   assuming 0(cfa)) and what registers are in the window.  */
89850397Sobrien
89950397Sobrienvoid
900132718Skandwarf2out_window_save (const char *label)
90150397Sobrien{
90290075Sobrien  dw_cfi_ref cfi = new_cfi ();
90390075Sobrien
90450397Sobrien  cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
90550397Sobrien  add_fde_cfi (label, cfi);
90650397Sobrien}
90750397Sobrien
90850397Sobrien/* Add a CFI to update the running total of the size of arguments
90950397Sobrien   pushed onto the stack.  */
91050397Sobrien
91150397Sobrienvoid
912132718Skandwarf2out_args_size (const char *label, HOST_WIDE_INT size)
91350397Sobrien{
91490075Sobrien  dw_cfi_ref cfi;
91550397Sobrien
91650397Sobrien  if (size == old_args_size)
91750397Sobrien    return;
91890075Sobrien
91950397Sobrien  old_args_size = size;
92050397Sobrien
92150397Sobrien  cfi = new_cfi ();
92250397Sobrien  cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
92350397Sobrien  cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
92450397Sobrien  add_fde_cfi (label, cfi);
92550397Sobrien}
92650397Sobrien
92750397Sobrien/* Entry point for saving a register to the stack.  REG is the GCC register
92850397Sobrien   number.  LABEL and OFFSET are passed to reg_save.  */
92950397Sobrien
93050397Sobrienvoid
931132718Skandwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
93250397Sobrien{
933169689Skan  reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
93450397Sobrien}
93550397Sobrien
93650397Sobrien/* Entry point for saving the return address in the stack.
93750397Sobrien   LABEL and OFFSET are passed to reg_save.  */
93850397Sobrien
93950397Sobrienvoid
940132718Skandwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
94150397Sobrien{
942169689Skan  reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
94350397Sobrien}
94450397Sobrien
94550397Sobrien/* Entry point for saving the return address in a register.
94650397Sobrien   LABEL and SREG are passed to reg_save.  */
94750397Sobrien
94850397Sobrienvoid
949132718Skandwarf2out_return_reg (const char *label, unsigned int sreg)
95050397Sobrien{
951169689Skan  reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
95250397Sobrien}
95350397Sobrien
95450397Sobrien/* Record the initial position of the return address.  RTL is
95550397Sobrien   INCOMING_RETURN_ADDR_RTX.  */
95650397Sobrien
95750397Sobrienstatic void
958132718Skaninitial_return_save (rtx rtl)
95950397Sobrien{
960169689Skan  unsigned int reg = INVALID_REGNUM;
96190075Sobrien  HOST_WIDE_INT offset = 0;
96250397Sobrien
96350397Sobrien  switch (GET_CODE (rtl))
96450397Sobrien    {
96550397Sobrien    case REG:
96650397Sobrien      /* RA is in a register.  */
96790075Sobrien      reg = DWARF_FRAME_REGNUM (REGNO (rtl));
96850397Sobrien      break;
96990075Sobrien
97050397Sobrien    case MEM:
97150397Sobrien      /* RA is on the stack.  */
97250397Sobrien      rtl = XEXP (rtl, 0);
97350397Sobrien      switch (GET_CODE (rtl))
97450397Sobrien	{
97550397Sobrien	case REG:
976169689Skan	  gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
97750397Sobrien	  offset = 0;
97850397Sobrien	  break;
97990075Sobrien
98050397Sobrien	case PLUS:
981169689Skan	  gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
98250397Sobrien	  offset = INTVAL (XEXP (rtl, 1));
98350397Sobrien	  break;
98490075Sobrien
98550397Sobrien	case MINUS:
986169689Skan	  gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
98750397Sobrien	  offset = -INTVAL (XEXP (rtl, 1));
98850397Sobrien	  break;
98990075Sobrien
99050397Sobrien	default:
991169689Skan	  gcc_unreachable ();
99250397Sobrien	}
99390075Sobrien
99450397Sobrien      break;
99590075Sobrien
99650397Sobrien    case PLUS:
99750397Sobrien      /* The return address is at some offset from any value we can
99850397Sobrien	 actually load.  For instance, on the SPARC it is in %i7+8. Just
99950397Sobrien	 ignore the offset for now; it doesn't matter for unwinding frames.  */
1000169689Skan      gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
100150397Sobrien      initial_return_save (XEXP (rtl, 0));
100250397Sobrien      return;
100390075Sobrien
100450397Sobrien    default:
1005169689Skan      gcc_unreachable ();
100650397Sobrien    }
100750397Sobrien
1008169689Skan  if (reg != DWARF_FRAME_RETURN_COLUMN)
1009169689Skan    reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
101050397Sobrien}
101150397Sobrien
101290075Sobrien/* Given a SET, calculate the amount of stack adjustment it
101390075Sobrien   contains.  */
101490075Sobrien
1015132718Skanstatic HOST_WIDE_INT
1016132718Skanstack_adjust_offset (rtx pattern)
101790075Sobrien{
101890075Sobrien  rtx src = SET_SRC (pattern);
101990075Sobrien  rtx dest = SET_DEST (pattern);
102090075Sobrien  HOST_WIDE_INT offset = 0;
102190075Sobrien  enum rtx_code code;
102290075Sobrien
102390075Sobrien  if (dest == stack_pointer_rtx)
102490075Sobrien    {
102590075Sobrien      /* (set (reg sp) (plus (reg sp) (const_int))) */
102690075Sobrien      code = GET_CODE (src);
102790075Sobrien      if (! (code == PLUS || code == MINUS)
102890075Sobrien	  || XEXP (src, 0) != stack_pointer_rtx
102990075Sobrien	  || GET_CODE (XEXP (src, 1)) != CONST_INT)
103090075Sobrien	return 0;
103190075Sobrien
103290075Sobrien      offset = INTVAL (XEXP (src, 1));
1033117395Skan      if (code == PLUS)
1034117395Skan	offset = -offset;
103590075Sobrien    }
1036169689Skan  else if (MEM_P (dest))
103790075Sobrien    {
103890075Sobrien      /* (set (mem (pre_dec (reg sp))) (foo)) */
103990075Sobrien      src = XEXP (dest, 0);
104090075Sobrien      code = GET_CODE (src);
104190075Sobrien
1042117395Skan      switch (code)
104390075Sobrien	{
1044117395Skan	case PRE_MODIFY:
1045117395Skan	case POST_MODIFY:
1046117395Skan	  if (XEXP (src, 0) == stack_pointer_rtx)
1047117395Skan	    {
1048117395Skan	      rtx val = XEXP (XEXP (src, 1), 1);
1049117395Skan	      /* We handle only adjustments by constant amount.  */
1050169689Skan	      gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1051169689Skan			  && GET_CODE (val) == CONST_INT);
1052117395Skan	      offset = -INTVAL (val);
1053117395Skan	      break;
1054117395Skan	    }
1055117395Skan	  return 0;
105690075Sobrien
1057117395Skan	case PRE_DEC:
1058117395Skan	case POST_DEC:
1059117395Skan	  if (XEXP (src, 0) == stack_pointer_rtx)
1060117395Skan	    {
1061117395Skan	      offset = GET_MODE_SIZE (GET_MODE (dest));
1062117395Skan	      break;
1063117395Skan	    }
1064117395Skan	  return 0;
106590075Sobrien
1066117395Skan	case PRE_INC:
1067117395Skan	case POST_INC:
1068117395Skan	  if (XEXP (src, 0) == stack_pointer_rtx)
1069117395Skan	    {
1070117395Skan	      offset = -GET_MODE_SIZE (GET_MODE (dest));
1071117395Skan	      break;
1072117395Skan	    }
1073117395Skan	  return 0;
1074117395Skan
1075117395Skan	default:
1076117395Skan	  return 0;
107790075Sobrien	}
107890075Sobrien    }
107990075Sobrien  else
108090075Sobrien    return 0;
108190075Sobrien
108290075Sobrien  return offset;
108390075Sobrien}
108490075Sobrien
108550397Sobrien/* Check INSN to see if it looks like a push or a stack adjustment, and
108650397Sobrien   make a note of it if it does.  EH uses this information to find out how
108750397Sobrien   much extra space it needs to pop off the stack.  */
108850397Sobrien
108950397Sobrienstatic void
1090169689Skandwarf2out_stack_adjust (rtx insn, bool after_p)
109150397Sobrien{
109290075Sobrien  HOST_WIDE_INT offset;
109390075Sobrien  const char *label;
109490075Sobrien  int i;
109550397Sobrien
1096132718Skan  /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1097132718Skan     with this function.  Proper support would require all frame-related
1098132718Skan     insns to be marked, and to be able to handle saving state around
1099132718Skan     epilogues textually in the middle of the function.  */
1100132718Skan  if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1101132718Skan    return;
1102132718Skan
1103169689Skan  /* If only calls can throw, and we have a frame pointer,
1104169689Skan     save up adjustments until we see the CALL_INSN.  */
1105169689Skan  if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
110650397Sobrien    {
1107169689Skan      if (CALL_P (insn) && !after_p)
1108169689Skan	{
1109169689Skan	  /* Extract the size of the args from the CALL rtx itself.  */
1110169689Skan	  insn = PATTERN (insn);
1111169689Skan	  if (GET_CODE (insn) == PARALLEL)
1112169689Skan	    insn = XVECEXP (insn, 0, 0);
1113169689Skan	  if (GET_CODE (insn) == SET)
1114169689Skan	    insn = SET_SRC (insn);
1115169689Skan	  gcc_assert (GET_CODE (insn) == CALL);
1116169689Skan	  dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1117169689Skan	}
111850397Sobrien      return;
111950397Sobrien    }
112050397Sobrien
1121169689Skan  if (CALL_P (insn) && !after_p)
112250397Sobrien    {
1123169689Skan      if (!flag_asynchronous_unwind_tables)
1124169689Skan	dwarf2out_args_size ("", args_size);
1125169689Skan      return;
1126169689Skan    }
1127169689Skan  else if (BARRIER_P (insn))
1128169689Skan    {
112950397Sobrien      /* When we see a BARRIER, we know to reset args_size to 0.  Usually
113050397Sobrien	 the compiler will have already emitted a stack adjustment, but
113150397Sobrien	 doesn't bother for calls to noreturn functions.  */
113250397Sobrien#ifdef STACK_GROWS_DOWNWARD
113350397Sobrien      offset = -args_size;
113450397Sobrien#else
113550397Sobrien      offset = args_size;
113650397Sobrien#endif
113750397Sobrien    }
113850397Sobrien  else if (GET_CODE (PATTERN (insn)) == SET)
113990075Sobrien    offset = stack_adjust_offset (PATTERN (insn));
114090075Sobrien  else if (GET_CODE (PATTERN (insn)) == PARALLEL
114190075Sobrien	   || GET_CODE (PATTERN (insn)) == SEQUENCE)
114250397Sobrien    {
114390075Sobrien      /* There may be stack adjustments inside compound insns.  Search
114490075Sobrien	 for them.  */
114590075Sobrien      for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
114690075Sobrien	if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
114790075Sobrien	  offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
114850397Sobrien    }
114950397Sobrien  else
115050397Sobrien    return;
115150397Sobrien
115250397Sobrien  if (offset == 0)
115350397Sobrien    return;
115450397Sobrien
115590075Sobrien  if (cfa.reg == STACK_POINTER_REGNUM)
115690075Sobrien    cfa.offset += offset;
115750397Sobrien
115850397Sobrien#ifndef STACK_GROWS_DOWNWARD
115950397Sobrien  offset = -offset;
116050397Sobrien#endif
116190075Sobrien
116250397Sobrien  args_size += offset;
116350397Sobrien  if (args_size < 0)
116450397Sobrien    args_size = 0;
116550397Sobrien
116650397Sobrien  label = dwarf2out_cfi_label ();
116790075Sobrien  def_cfa_1 (label, &cfa);
1168169689Skan  if (flag_asynchronous_unwind_tables)
1169169689Skan    dwarf2out_args_size (label, args_size);
117050397Sobrien}
117150397Sobrien
1172132718Skan#endif
1173132718Skan
117490075Sobrien/* We delay emitting a register save until either (a) we reach the end
117590075Sobrien   of the prologue or (b) the register is clobbered.  This clusters
117690075Sobrien   register saves so that there are fewer pc advances.  */
117752284Sobrien
1178132718Skanstruct queued_reg_save GTY(())
117990075Sobrien{
118090075Sobrien  struct queued_reg_save *next;
118190075Sobrien  rtx reg;
1182132718Skan  HOST_WIDE_INT cfa_offset;
1183169689Skan  rtx saved_reg;
118490075Sobrien};
118552284Sobrien
1186132718Skanstatic GTY(()) struct queued_reg_save *queued_reg_saves;
1187132718Skan
1188169689Skan/* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1189169689Skanstruct reg_saved_in_data GTY(()) {
1190169689Skan  rtx orig_reg;
1191169689Skan  rtx saved_in_reg;
1192169689Skan};
1193169689Skan
1194169689Skan/* A list of registers saved in other registers.
1195169689Skan   The list intentionally has a small maximum capacity of 4; if your
1196169689Skan   port needs more than that, you might consider implementing a
1197169689Skan   more efficient data structure.  */
1198169689Skanstatic GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1199169689Skanstatic GTY(()) size_t num_regs_saved_in_regs;
1200169689Skan
1201132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
120290075Sobrienstatic const char *last_reg_save_label;
120350397Sobrien
1204169689Skan/* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1205169689Skan   SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1206169689Skan
120752284Sobrienstatic void
1208169689Skanqueue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
120990075Sobrien{
1210169689Skan  struct queued_reg_save *q;
121190075Sobrien
1212169689Skan  /* Duplicates waste space, but it's also necessary to remove them
1213169689Skan     for correctness, since the queue gets output in reverse
1214169689Skan     order.  */
1215169689Skan  for (q = queued_reg_saves; q != NULL; q = q->next)
1216169689Skan    if (REGNO (q->reg) == REGNO (reg))
1217169689Skan      break;
1218169689Skan
1219169689Skan  if (q == NULL)
1220169689Skan    {
1221169689Skan      q = ggc_alloc (sizeof (*q));
1222169689Skan      q->next = queued_reg_saves;
1223169689Skan      queued_reg_saves = q;
1224169689Skan    }
1225169689Skan
122690075Sobrien  q->reg = reg;
122790075Sobrien  q->cfa_offset = offset;
1228169689Skan  q->saved_reg = sreg;
122990075Sobrien
123090075Sobrien  last_reg_save_label = label;
123190075Sobrien}
123290075Sobrien
1233169689Skan/* Output all the entries in QUEUED_REG_SAVES.  */
1234169689Skan
123590075Sobrienstatic void
1236132718Skanflush_queued_reg_saves (void)
123790075Sobrien{
1238169689Skan  struct queued_reg_save *q;
123990075Sobrien
1240169689Skan  for (q = queued_reg_saves; q; q = q->next)
124190075Sobrien    {
1242169689Skan      size_t i;
1243169689Skan      unsigned int reg, sreg;
1244169689Skan
1245169689Skan      for (i = 0; i < num_regs_saved_in_regs; i++)
1246169689Skan	if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1247169689Skan	  break;
1248169689Skan      if (q->saved_reg && i == num_regs_saved_in_regs)
1249169689Skan	{
1250169689Skan	  gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1251169689Skan	  num_regs_saved_in_regs++;
1252169689Skan	}
1253169689Skan      if (i != num_regs_saved_in_regs)
1254169689Skan	{
1255169689Skan	  regs_saved_in_regs[i].orig_reg = q->reg;
1256169689Skan	  regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1257169689Skan	}
1258169689Skan
1259169689Skan      reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1260169689Skan      if (q->saved_reg)
1261169689Skan	sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1262169689Skan      else
1263169689Skan	sreg = INVALID_REGNUM;
1264169689Skan      reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
126590075Sobrien    }
126690075Sobrien
126790075Sobrien  queued_reg_saves = NULL;
126890075Sobrien  last_reg_save_label = NULL;
126990075Sobrien}
127090075Sobrien
1271169689Skan/* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1272169689Skan   location for?  Or, does it clobber a register which we've previously
1273169689Skan   said that some other register is saved in, and for which we now
1274169689Skan   have a new location for?  */
1275169689Skan
127690075Sobrienstatic bool
1277132718Skanclobbers_queued_reg_save (rtx insn)
127890075Sobrien{
127990075Sobrien  struct queued_reg_save *q;
128090075Sobrien
1281117395Skan  for (q = queued_reg_saves; q; q = q->next)
1282169689Skan    {
1283169689Skan      size_t i;
1284169689Skan      if (modified_in_p (q->reg, insn))
1285169689Skan	return true;
1286169689Skan      for (i = 0; i < num_regs_saved_in_regs; i++)
1287169689Skan	if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1288169689Skan	    && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1289169689Skan	  return true;
1290169689Skan    }
129190075Sobrien
129290075Sobrien  return false;
129390075Sobrien}
129490075Sobrien
1295169689Skan/* Entry point for saving the first register into the second.  */
1296117395Skan
1297169689Skanvoid
1298169689Skandwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1299169689Skan{
1300169689Skan  size_t i;
1301169689Skan  unsigned int regno, sregno;
1302169689Skan
1303169689Skan  for (i = 0; i < num_regs_saved_in_regs; i++)
1304169689Skan    if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1305169689Skan      break;
1306169689Skan  if (i == num_regs_saved_in_regs)
1307169689Skan    {
1308169689Skan      gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1309169689Skan      num_regs_saved_in_regs++;
1310169689Skan    }
1311169689Skan  regs_saved_in_regs[i].orig_reg = reg;
1312169689Skan  regs_saved_in_regs[i].saved_in_reg = sreg;
1313169689Skan
1314169689Skan  regno = DWARF_FRAME_REGNUM (REGNO (reg));
1315169689Skan  sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1316169689Skan  reg_save (label, regno, sregno, 0);
1317169689Skan}
1318169689Skan
1319169689Skan/* What register, if any, is currently saved in REG?  */
1320169689Skan
1321169689Skanstatic rtx
1322169689Skanreg_saved_in (rtx reg)
1323169689Skan{
1324169689Skan  unsigned int regn = REGNO (reg);
1325169689Skan  size_t i;
1326169689Skan  struct queued_reg_save *q;
1327169689Skan
1328169689Skan  for (q = queued_reg_saves; q; q = q->next)
1329169689Skan    if (q->saved_reg && regn == REGNO (q->saved_reg))
1330169689Skan      return q->reg;
1331169689Skan
1332169689Skan  for (i = 0; i < num_regs_saved_in_regs; i++)
1333169689Skan    if (regs_saved_in_regs[i].saved_in_reg
1334169689Skan	&& regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1335169689Skan      return regs_saved_in_regs[i].orig_reg;
1336169689Skan
1337169689Skan  return NULL_RTX;
1338169689Skan}
1339169689Skan
1340169689Skan
134190075Sobrien/* A temporary register holding an integral value used in adjusting SP
134290075Sobrien   or setting up the store_reg.  The "offset" field holds the integer
134390075Sobrien   value, not an offset.  */
134490075Sobrienstatic dw_cfa_location cfa_temp;
134590075Sobrien
134690075Sobrien/* Record call frame debugging information for an expression EXPR,
134790075Sobrien   which either sets SP or FP (adjusting how we calculate the frame
1348169689Skan   address) or saves a register to the stack or another register.
1349169689Skan   LABEL indicates the address of EXPR.
135090075Sobrien
135190075Sobrien   This function encodes a state machine mapping rtxes to actions on
135290075Sobrien   cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
135390075Sobrien   users need not read the source code.
135490075Sobrien
135590075Sobrien  The High-Level Picture
135690075Sobrien
135790075Sobrien  Changes in the register we use to calculate the CFA: Currently we
135890075Sobrien  assume that if you copy the CFA register into another register, we
135990075Sobrien  should take the other one as the new CFA register; this seems to
136090075Sobrien  work pretty well.  If it's wrong for some target, it's simple
136190075Sobrien  enough not to set RTX_FRAME_RELATED_P on the insn in question.
136290075Sobrien
136390075Sobrien  Changes in the register we use for saving registers to the stack:
136490075Sobrien  This is usually SP, but not always.  Again, we deduce that if you
136590075Sobrien  copy SP into another register (and SP is not the CFA register),
136690075Sobrien  then the new register is the one we will be using for register
136790075Sobrien  saves.  This also seems to work.
136890075Sobrien
136990075Sobrien  Register saves: There's not much guesswork about this one; if
137090075Sobrien  RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
137190075Sobrien  register save, and the register used to calculate the destination
137290075Sobrien  had better be the one we think we're using for this purpose.
1373169689Skan  It's also assumed that a copy from a call-saved register to another
1374169689Skan  register is saving that register if RTX_FRAME_RELATED_P is set on
1375169689Skan  that instruction.  If the copy is from a call-saved register to
1376169689Skan  the *same* register, that means that the register is now the same
1377169689Skan  value as in the caller.
137890075Sobrien
137990075Sobrien  Except: If the register being saved is the CFA register, and the
1380117395Skan  offset is nonzero, we are saving the CFA, so we assume we have to
138190075Sobrien  use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
138290075Sobrien  the intent is to save the value of SP from the previous frame.
138390075Sobrien
1384169689Skan  In addition, if a register has previously been saved to a different
1385169689Skan  register,
1386169689Skan
138790075Sobrien  Invariants / Summaries of Rules
138890075Sobrien
138990075Sobrien  cfa	       current rule for calculating the CFA.  It usually
139090075Sobrien	       consists of a register and an offset.
139190075Sobrien  cfa_store    register used by prologue code to save things to the stack
139290075Sobrien	       cfa_store.offset is the offset from the value of
139390075Sobrien	       cfa_store.reg to the actual CFA
139490075Sobrien  cfa_temp     register holding an integral value.  cfa_temp.offset
139590075Sobrien	       stores the value, which will be used to adjust the
139690075Sobrien	       stack pointer.  cfa_temp is also used like cfa_store,
139790075Sobrien	       to track stores to the stack via fp or a temp reg.
1398117395Skan
139990075Sobrien  Rules  1- 4: Setting a register's value to cfa.reg or an expression
1400132718Skan	       with cfa.reg as the first operand changes the cfa.reg and its
140190075Sobrien	       cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
140290075Sobrien	       cfa_temp.offset.
140390075Sobrien
140490075Sobrien  Rules  6- 9: Set a non-cfa.reg register value to a constant or an
140590075Sobrien	       expression yielding a constant.  This sets cfa_temp.reg
140690075Sobrien	       and cfa_temp.offset.
140790075Sobrien
140890075Sobrien  Rule 5:      Create a new register cfa_store used to save items to the
140990075Sobrien	       stack.
141090075Sobrien
141190075Sobrien  Rules 10-14: Save a register to the stack.  Define offset as the
141290075Sobrien	       difference of the original location and cfa_store's
141390075Sobrien	       location (or cfa_temp's location if cfa_temp is used).
141490075Sobrien
141590075Sobrien  The Rules
141690075Sobrien
141790075Sobrien  "{a,b}" indicates a choice of a xor b.
141890075Sobrien  "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
141990075Sobrien
142090075Sobrien  Rule 1:
142190075Sobrien  (set <reg1> <reg2>:cfa.reg)
142290075Sobrien  effects: cfa.reg = <reg1>
1423132718Skan	   cfa.offset unchanged
142490075Sobrien	   cfa_temp.reg = <reg1>
142590075Sobrien	   cfa_temp.offset = cfa.offset
142690075Sobrien
142790075Sobrien  Rule 2:
142890075Sobrien  (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
142990075Sobrien			      {<const_int>,<reg>:cfa_temp.reg}))
143090075Sobrien  effects: cfa.reg = sp if fp used
1431132718Skan	   cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
143290075Sobrien	   cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
143390075Sobrien	     if cfa_store.reg==sp
143490075Sobrien
143590075Sobrien  Rule 3:
143690075Sobrien  (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
143790075Sobrien  effects: cfa.reg = fp
1438132718Skan	   cfa_offset += +/- <const_int>
143990075Sobrien
144090075Sobrien  Rule 4:
144190075Sobrien  (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
144290075Sobrien  constraints: <reg1> != fp
1443132718Skan	       <reg1> != sp
144490075Sobrien  effects: cfa.reg = <reg1>
144590075Sobrien	   cfa_temp.reg = <reg1>
144690075Sobrien	   cfa_temp.offset = cfa.offset
144790075Sobrien
144890075Sobrien  Rule 5:
144990075Sobrien  (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
145090075Sobrien  constraints: <reg1> != fp
1451132718Skan	       <reg1> != sp
145290075Sobrien  effects: cfa_store.reg = <reg1>
1453132718Skan	   cfa_store.offset = cfa.offset - cfa_temp.offset
145490075Sobrien
145590075Sobrien  Rule 6:
145690075Sobrien  (set <reg> <const_int>)
145790075Sobrien  effects: cfa_temp.reg = <reg>
1458132718Skan	   cfa_temp.offset = <const_int>
145990075Sobrien
146090075Sobrien  Rule 7:
146190075Sobrien  (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
146290075Sobrien  effects: cfa_temp.reg = <reg1>
146390075Sobrien	   cfa_temp.offset |= <const_int>
146490075Sobrien
146590075Sobrien  Rule 8:
146690075Sobrien  (set <reg> (high <exp>))
146790075Sobrien  effects: none
146890075Sobrien
146990075Sobrien  Rule 9:
147090075Sobrien  (set <reg> (lo_sum <exp> <const_int>))
147190075Sobrien  effects: cfa_temp.reg = <reg>
1472132718Skan	   cfa_temp.offset = <const_int>
147390075Sobrien
147490075Sobrien  Rule 10:
147590075Sobrien  (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
147690075Sobrien  effects: cfa_store.offset -= <const_int>
147790075Sobrien	   cfa.offset = cfa_store.offset if cfa.reg == sp
147890075Sobrien	   cfa.reg = sp
147990075Sobrien	   cfa.base_offset = -cfa_store.offset
148090075Sobrien
148190075Sobrien  Rule 11:
148290075Sobrien  (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
148390075Sobrien  effects: cfa_store.offset += -/+ mode_size(mem)
148490075Sobrien	   cfa.offset = cfa_store.offset if cfa.reg == sp
148590075Sobrien	   cfa.reg = sp
148690075Sobrien	   cfa.base_offset = -cfa_store.offset
148790075Sobrien
148890075Sobrien  Rule 12:
148990075Sobrien  (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
149090075Sobrien
149190075Sobrien       <reg2>)
149290075Sobrien  effects: cfa.reg = <reg1>
149390075Sobrien	   cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
149490075Sobrien
149590075Sobrien  Rule 13:
149690075Sobrien  (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
149790075Sobrien  effects: cfa.reg = <reg1>
149890075Sobrien	   cfa.base_offset = -{cfa_store,cfa_temp}.offset
149990075Sobrien
150090075Sobrien  Rule 14:
150190075Sobrien  (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
150290075Sobrien  effects: cfa.reg = <reg1>
150390075Sobrien	   cfa.base_offset = -cfa_temp.offset
1504169689Skan	   cfa_temp.offset -= mode_size(mem)
150590075Sobrien
1506169689Skan  Rule 15:
1507169689Skan  (set <reg> {unspec, unspec_volatile})
1508169689Skan  effects: target-dependent  */
1509169689Skan
151090075Sobrienstatic void
1511132718Skandwarf2out_frame_debug_expr (rtx expr, const char *label)
151250397Sobrien{
151350397Sobrien  rtx src, dest;
151490075Sobrien  HOST_WIDE_INT offset;
151550397Sobrien
151690075Sobrien  /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
151790075Sobrien     the PARALLEL independently. The first element is always processed if
151890075Sobrien     it is a SET. This is for backward compatibility.   Other elements
151990075Sobrien     are processed only if they are SETs and the RTX_FRAME_RELATED_P
152090075Sobrien     flag is set in them.  */
152190075Sobrien  if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
152290075Sobrien    {
152352284Sobrien      int par_index;
152452284Sobrien      int limit = XVECLEN (expr, 0);
152550397Sobrien
152652284Sobrien      for (par_index = 0; par_index < limit; par_index++)
152790075Sobrien	if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
152890075Sobrien	    && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
152990075Sobrien		|| par_index == 0))
153090075Sobrien	  dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
153190075Sobrien
153250397Sobrien      return;
153350397Sobrien    }
153490075Sobrien
1535169689Skan  gcc_assert (GET_CODE (expr) == SET);
153650397Sobrien
153752284Sobrien  src = SET_SRC (expr);
153852284Sobrien  dest = SET_DEST (expr);
153950397Sobrien
1540169689Skan  if (REG_P (src))
1541169689Skan    {
1542169689Skan      rtx rsi = reg_saved_in (src);
1543169689Skan      if (rsi)
1544169689Skan	src = rsi;
1545169689Skan    }
1546169689Skan
154750397Sobrien  switch (GET_CODE (dest))
154850397Sobrien    {
154950397Sobrien    case REG:
155050397Sobrien      switch (GET_CODE (src))
155190075Sobrien	{
155290075Sobrien	  /* Setting FP from SP.  */
155390075Sobrien	case REG:
155490075Sobrien	  if (cfa.reg == (unsigned) REGNO (src))
1555169689Skan	    {
1556169689Skan	      /* Rule 1 */
1557169689Skan	      /* Update the CFA rule wrt SP or FP.  Make sure src is
1558169689Skan		 relative to the current CFA register.
1559169689Skan
1560169689Skan		 We used to require that dest be either SP or FP, but the
1561169689Skan		 ARM copies SP to a temporary register, and from there to
1562169689Skan		 FP.  So we just rely on the backends to only set
1563169689Skan		 RTX_FRAME_RELATED_P on appropriate insns.  */
1564169689Skan	      cfa.reg = REGNO (dest);
1565169689Skan	      cfa_temp.reg = cfa.reg;
1566169689Skan	      cfa_temp.offset = cfa.offset;
1567169689Skan	    }
156890075Sobrien	  else
1569169689Skan	    {
1570169689Skan	      /* Saving a register in a register.  */
1571169689Skan	      gcc_assert (!fixed_regs [REGNO (dest)]
1572169689Skan			  /* For the SPARC and its register window.  */
1573169689Skan			  || (DWARF_FRAME_REGNUM (REGNO (src))
1574169689Skan			      == DWARF_FRAME_RETURN_COLUMN));
1575169689Skan	      queue_reg_save (label, src, dest, 0);
1576169689Skan	    }
157790075Sobrien	  break;
157850397Sobrien
157990075Sobrien	case PLUS:
158090075Sobrien	case MINUS:
158190075Sobrien	case LO_SUM:
158290075Sobrien	  if (dest == stack_pointer_rtx)
158390075Sobrien	    {
158490075Sobrien	      /* Rule 2 */
158590075Sobrien	      /* Adjusting SP.  */
158690075Sobrien	      switch (GET_CODE (XEXP (src, 1)))
158790075Sobrien		{
158890075Sobrien		case CONST_INT:
158990075Sobrien		  offset = INTVAL (XEXP (src, 1));
159090075Sobrien		  break;
159190075Sobrien		case REG:
1592169689Skan		  gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1593169689Skan			      == cfa_temp.reg);
159490075Sobrien		  offset = cfa_temp.offset;
159590075Sobrien		  break;
159690075Sobrien		default:
1597169689Skan		  gcc_unreachable ();
159890075Sobrien		}
159950397Sobrien
160090075Sobrien	      if (XEXP (src, 0) == hard_frame_pointer_rtx)
160190075Sobrien		{
160290075Sobrien		  /* Restoring SP from FP in the epilogue.  */
1603169689Skan		  gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
160490075Sobrien		  cfa.reg = STACK_POINTER_REGNUM;
160590075Sobrien		}
160690075Sobrien	      else if (GET_CODE (src) == LO_SUM)
160790075Sobrien		/* Assume we've set the source reg of the LO_SUM from sp.  */
160890075Sobrien		;
1609169689Skan	      else
1610169689Skan		gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
161150397Sobrien
161290075Sobrien	      if (GET_CODE (src) != MINUS)
161390075Sobrien		offset = -offset;
161490075Sobrien	      if (cfa.reg == STACK_POINTER_REGNUM)
161590075Sobrien		cfa.offset += offset;
161690075Sobrien	      if (cfa_store.reg == STACK_POINTER_REGNUM)
161790075Sobrien		cfa_store.offset += offset;
161890075Sobrien	    }
161990075Sobrien	  else if (dest == hard_frame_pointer_rtx)
162090075Sobrien	    {
162190075Sobrien	      /* Rule 3 */
162290075Sobrien	      /* Either setting the FP from an offset of the SP,
162390075Sobrien		 or adjusting the FP */
1624169689Skan	      gcc_assert (frame_pointer_needed);
162550397Sobrien
1626169689Skan	      gcc_assert (REG_P (XEXP (src, 0))
1627169689Skan			  && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1628169689Skan			  && GET_CODE (XEXP (src, 1)) == CONST_INT);
1629169689Skan	      offset = INTVAL (XEXP (src, 1));
1630169689Skan	      if (GET_CODE (src) != MINUS)
1631169689Skan		offset = -offset;
1632169689Skan	      cfa.offset += offset;
1633169689Skan	      cfa.reg = HARD_FRAME_POINTER_REGNUM;
163490075Sobrien	    }
163590075Sobrien	  else
163690075Sobrien	    {
1637169689Skan	      gcc_assert (GET_CODE (src) != MINUS);
163850397Sobrien
163990075Sobrien	      /* Rule 4 */
1640169689Skan	      if (REG_P (XEXP (src, 0))
164190075Sobrien		  && REGNO (XEXP (src, 0)) == cfa.reg
164290075Sobrien		  && GET_CODE (XEXP (src, 1)) == CONST_INT)
164390075Sobrien		{
164490075Sobrien		  /* Setting a temporary CFA register that will be copied
164590075Sobrien		     into the FP later on.  */
164690075Sobrien		  offset = - INTVAL (XEXP (src, 1));
164790075Sobrien		  cfa.offset += offset;
164890075Sobrien		  cfa.reg = REGNO (dest);
164990075Sobrien		  /* Or used to save regs to the stack.  */
165090075Sobrien		  cfa_temp.reg = cfa.reg;
165190075Sobrien		  cfa_temp.offset = cfa.offset;
165290075Sobrien		}
165350397Sobrien
165490075Sobrien	      /* Rule 5 */
1655169689Skan	      else if (REG_P (XEXP (src, 0))
165690075Sobrien		       && REGNO (XEXP (src, 0)) == cfa_temp.reg
165790075Sobrien		       && XEXP (src, 1) == stack_pointer_rtx)
165890075Sobrien		{
165990075Sobrien		  /* Setting a scratch register that we will use instead
166090075Sobrien		     of SP for saving registers to the stack.  */
1661169689Skan		  gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
166290075Sobrien		  cfa_store.reg = REGNO (dest);
166390075Sobrien		  cfa_store.offset = cfa.offset - cfa_temp.offset;
166490075Sobrien		}
166550397Sobrien
166690075Sobrien	      /* Rule 9 */
166790075Sobrien	      else if (GET_CODE (src) == LO_SUM
166890075Sobrien		       && GET_CODE (XEXP (src, 1)) == CONST_INT)
166990075Sobrien		{
167090075Sobrien		  cfa_temp.reg = REGNO (dest);
167190075Sobrien		  cfa_temp.offset = INTVAL (XEXP (src, 1));
167290075Sobrien		}
167390075Sobrien	      else
1674169689Skan		gcc_unreachable ();
167590075Sobrien	    }
167690075Sobrien	  break;
167750397Sobrien
167890075Sobrien	  /* Rule 6 */
167990075Sobrien	case CONST_INT:
168090075Sobrien	  cfa_temp.reg = REGNO (dest);
168190075Sobrien	  cfa_temp.offset = INTVAL (src);
168290075Sobrien	  break;
168350397Sobrien
168490075Sobrien	  /* Rule 7 */
168590075Sobrien	case IOR:
1686169689Skan	  gcc_assert (REG_P (XEXP (src, 0))
1687169689Skan		      && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1688169689Skan		      && GET_CODE (XEXP (src, 1)) == CONST_INT);
168950397Sobrien
169090075Sobrien	  if ((unsigned) REGNO (dest) != cfa_temp.reg)
169190075Sobrien	    cfa_temp.reg = REGNO (dest);
169290075Sobrien	  cfa_temp.offset |= INTVAL (XEXP (src, 1));
169390075Sobrien	  break;
169450397Sobrien
169590075Sobrien	  /* Skip over HIGH, assuming it will be followed by a LO_SUM,
169690075Sobrien	     which will fill in all of the bits.  */
169790075Sobrien	  /* Rule 8 */
169890075Sobrien	case HIGH:
169990075Sobrien	  break;
170050397Sobrien
1701169689Skan	  /* Rule 15 */
1702169689Skan	case UNSPEC:
1703169689Skan	case UNSPEC_VOLATILE:
1704169689Skan	  gcc_assert (targetm.dwarf_handle_frame_unspec);
1705169689Skan	  targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1706169689Skan	  return;
1707169689Skan
170890075Sobrien	default:
1709169689Skan	  gcc_unreachable ();
171090075Sobrien	}
171150397Sobrien
171290075Sobrien      def_cfa_1 (label, &cfa);
171390075Sobrien      break;
171450397Sobrien
171590075Sobrien    case MEM:
1716169689Skan      gcc_assert (REG_P (src));
171752284Sobrien
171890075Sobrien      /* Saving a register to the stack.  Make sure dest is relative to the
171990075Sobrien	 CFA register.  */
172090075Sobrien      switch (GET_CODE (XEXP (dest, 0)))
172190075Sobrien	{
172290075Sobrien	  /* Rule 10 */
172390075Sobrien	  /* With a push.  */
172490075Sobrien	case PRE_MODIFY:
172590075Sobrien	  /* We can't handle variable size modifications.  */
1726169689Skan	  gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1727169689Skan		      == CONST_INT);
172890075Sobrien	  offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
172990075Sobrien
1730169689Skan	  gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1731169689Skan		      && cfa_store.reg == STACK_POINTER_REGNUM);
173290075Sobrien
173390075Sobrien	  cfa_store.offset += offset;
173490075Sobrien	  if (cfa.reg == STACK_POINTER_REGNUM)
173590075Sobrien	    cfa.offset = cfa_store.offset;
173690075Sobrien
173790075Sobrien	  offset = -cfa_store.offset;
173890075Sobrien	  break;
173990075Sobrien
174090075Sobrien	  /* Rule 11 */
174190075Sobrien	case PRE_INC:
174290075Sobrien	case PRE_DEC:
174390075Sobrien	  offset = GET_MODE_SIZE (GET_MODE (dest));
174490075Sobrien	  if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
174590075Sobrien	    offset = -offset;
174690075Sobrien
1747169689Skan	  gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1748169689Skan		      && cfa_store.reg == STACK_POINTER_REGNUM);
174990075Sobrien
175090075Sobrien	  cfa_store.offset += offset;
175190075Sobrien	  if (cfa.reg == STACK_POINTER_REGNUM)
175290075Sobrien	    cfa.offset = cfa_store.offset;
175390075Sobrien
175490075Sobrien	  offset = -cfa_store.offset;
175590075Sobrien	  break;
175690075Sobrien
175790075Sobrien	  /* Rule 12 */
175890075Sobrien	  /* With an offset.  */
175990075Sobrien	case PLUS:
176090075Sobrien	case MINUS:
176190075Sobrien	case LO_SUM:
1762169689Skan	  {
1763169689Skan	    int regno;
176490075Sobrien
1765169689Skan	    gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1766169689Skan			&& REG_P (XEXP (XEXP (dest, 0), 0)));
1767169689Skan	    offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1768169689Skan	    if (GET_CODE (XEXP (dest, 0)) == MINUS)
1769169689Skan	      offset = -offset;
1770169689Skan
1771169689Skan	    regno = REGNO (XEXP (XEXP (dest, 0), 0));
1772169689Skan
1773169689Skan	    if (cfa_store.reg == (unsigned) regno)
1774169689Skan	      offset -= cfa_store.offset;
1775169689Skan	    else
1776169689Skan	      {
1777169689Skan		gcc_assert (cfa_temp.reg == (unsigned) regno);
1778169689Skan		offset -= cfa_temp.offset;
1779169689Skan	      }
1780169689Skan	  }
178190075Sobrien	  break;
178290075Sobrien
178390075Sobrien	  /* Rule 13 */
178490075Sobrien	  /* Without an offset.  */
178590075Sobrien	case REG:
1786169689Skan	  {
1787169689Skan	    int regno = REGNO (XEXP (dest, 0));
1788169689Skan
1789169689Skan	    if (cfa_store.reg == (unsigned) regno)
1790169689Skan	      offset = -cfa_store.offset;
1791169689Skan	    else
1792169689Skan	      {
1793169689Skan		gcc_assert (cfa_temp.reg == (unsigned) regno);
1794169689Skan		offset = -cfa_temp.offset;
1795169689Skan	      }
1796169689Skan	  }
179790075Sobrien	  break;
179890075Sobrien
179990075Sobrien	  /* Rule 14 */
180090075Sobrien	case POST_INC:
1801169689Skan	  gcc_assert (cfa_temp.reg
1802169689Skan		      == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
180390075Sobrien	  offset = -cfa_temp.offset;
180490075Sobrien	  cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
180590075Sobrien	  break;
180690075Sobrien
180790075Sobrien	default:
1808169689Skan	  gcc_unreachable ();
180990075Sobrien	}
181090075Sobrien
181190075Sobrien      if (REGNO (src) != STACK_POINTER_REGNUM
181290075Sobrien	  && REGNO (src) != HARD_FRAME_POINTER_REGNUM
181390075Sobrien	  && (unsigned) REGNO (src) == cfa.reg)
181490075Sobrien	{
181590075Sobrien	  /* We're storing the current CFA reg into the stack.  */
181690075Sobrien
181790075Sobrien	  if (cfa.offset == 0)
181890075Sobrien	    {
181990075Sobrien	      /* If the source register is exactly the CFA, assume
182090075Sobrien		 we're saving SP like any other register; this happens
182190075Sobrien		 on the ARM.  */
182290075Sobrien	      def_cfa_1 (label, &cfa);
1823169689Skan	      queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
182490075Sobrien	      break;
182590075Sobrien	    }
182690075Sobrien	  else
182790075Sobrien	    {
182890075Sobrien	      /* Otherwise, we'll need to look in the stack to
1829132718Skan		 calculate the CFA.  */
183090075Sobrien	      rtx x = XEXP (dest, 0);
183190075Sobrien
1832169689Skan	      if (!REG_P (x))
183390075Sobrien		x = XEXP (x, 0);
1834169689Skan	      gcc_assert (REG_P (x));
183590075Sobrien
183690075Sobrien	      cfa.reg = REGNO (x);
183790075Sobrien	      cfa.base_offset = offset;
183890075Sobrien	      cfa.indirect = 1;
183990075Sobrien	      def_cfa_1 (label, &cfa);
184090075Sobrien	      break;
184190075Sobrien	    }
184290075Sobrien	}
184390075Sobrien
184490075Sobrien      def_cfa_1 (label, &cfa);
1845169689Skan      queue_reg_save (label, src, NULL_RTX, offset);
184690075Sobrien      break;
184790075Sobrien
184890075Sobrien    default:
1849169689Skan      gcc_unreachable ();
185090075Sobrien    }
185152284Sobrien}
185252284Sobrien
185352284Sobrien/* Record call frame debugging information for INSN, which either
185452284Sobrien   sets SP or FP (adjusting how we calculate the frame address) or saves a
1855169689Skan   register to the stack.  If INSN is NULL_RTX, initialize our state.
185652284Sobrien
1857169689Skan   If AFTER_P is false, we're being called before the insn is emitted,
1858169689Skan   otherwise after.  Call instructions get invoked twice.  */
1859169689Skan
186052284Sobrienvoid
1861169689Skandwarf2out_frame_debug (rtx insn, bool after_p)
186252284Sobrien{
186390075Sobrien  const char *label;
186452284Sobrien  rtx src;
186552284Sobrien
186652284Sobrien  if (insn == NULL_RTX)
186752284Sobrien    {
1868169689Skan      size_t i;
1869169689Skan
187090075Sobrien      /* Flush any queued register saves.  */
187190075Sobrien      flush_queued_reg_saves ();
187290075Sobrien
187352284Sobrien      /* Set up state for generating call frame debug info.  */
187490075Sobrien      lookup_cfa (&cfa);
1875169689Skan      gcc_assert (cfa.reg
1876169689Skan		  == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
187790075Sobrien
187890075Sobrien      cfa.reg = STACK_POINTER_REGNUM;
187990075Sobrien      cfa_store = cfa;
188090075Sobrien      cfa_temp.reg = -1;
188190075Sobrien      cfa_temp.offset = 0;
1882169689Skan
1883169689Skan      for (i = 0; i < num_regs_saved_in_regs; i++)
1884169689Skan	{
1885169689Skan	  regs_saved_in_regs[i].orig_reg = NULL_RTX;
1886169689Skan	  regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1887169689Skan	}
1888169689Skan      num_regs_saved_in_regs = 0;
188952284Sobrien      return;
189050397Sobrien    }
189152284Sobrien
1892169689Skan  if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
189390075Sobrien    flush_queued_reg_saves ();
189490075Sobrien
189552284Sobrien  if (! RTX_FRAME_RELATED_P (insn))
189652284Sobrien    {
189790075Sobrien      if (!ACCUMULATE_OUTGOING_ARGS)
1898169689Skan	dwarf2out_stack_adjust (insn, after_p);
189952284Sobrien      return;
190052284Sobrien    }
190152284Sobrien
190252284Sobrien  label = dwarf2out_cfi_label ();
190352284Sobrien  src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
190452284Sobrien  if (src)
190552284Sobrien    insn = XEXP (src, 0);
190690075Sobrien  else
190752284Sobrien    insn = PATTERN (insn);
190852284Sobrien
190952284Sobrien  dwarf2out_frame_debug_expr (insn, label);
191050397Sobrien}
191150397Sobrien
1912132718Skan#endif
1913132718Skan
1914132718Skan/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1915132718Skanstatic enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1916132718Skan (enum dwarf_call_frame_info cfi);
1917132718Skan
1918132718Skanstatic enum dw_cfi_oprnd_type
1919132718Skandw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1920132718Skan{
1921132718Skan  switch (cfi)
1922132718Skan    {
1923132718Skan    case DW_CFA_nop:
1924132718Skan    case DW_CFA_GNU_window_save:
1925132718Skan      return dw_cfi_oprnd_unused;
1926132718Skan
1927132718Skan    case DW_CFA_set_loc:
1928132718Skan    case DW_CFA_advance_loc1:
1929132718Skan    case DW_CFA_advance_loc2:
1930132718Skan    case DW_CFA_advance_loc4:
1931132718Skan    case DW_CFA_MIPS_advance_loc8:
1932132718Skan      return dw_cfi_oprnd_addr;
1933132718Skan
1934132718Skan    case DW_CFA_offset:
1935132718Skan    case DW_CFA_offset_extended:
1936132718Skan    case DW_CFA_def_cfa:
1937132718Skan    case DW_CFA_offset_extended_sf:
1938132718Skan    case DW_CFA_def_cfa_sf:
1939132718Skan    case DW_CFA_restore_extended:
1940132718Skan    case DW_CFA_undefined:
1941132718Skan    case DW_CFA_same_value:
1942132718Skan    case DW_CFA_def_cfa_register:
1943132718Skan    case DW_CFA_register:
1944132718Skan      return dw_cfi_oprnd_reg_num;
1945132718Skan
1946132718Skan    case DW_CFA_def_cfa_offset:
1947132718Skan    case DW_CFA_GNU_args_size:
1948132718Skan    case DW_CFA_def_cfa_offset_sf:
1949132718Skan      return dw_cfi_oprnd_offset;
1950132718Skan
1951132718Skan    case DW_CFA_def_cfa_expression:
1952132718Skan    case DW_CFA_expression:
1953132718Skan      return dw_cfi_oprnd_loc;
1954132718Skan
1955132718Skan    default:
1956169689Skan      gcc_unreachable ();
1957132718Skan    }
1958132718Skan}
1959132718Skan
1960132718Skan/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1961132718Skanstatic enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1962132718Skan (enum dwarf_call_frame_info cfi);
1963132718Skan
1964132718Skanstatic enum dw_cfi_oprnd_type
1965132718Skandw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1966132718Skan{
1967132718Skan  switch (cfi)
1968132718Skan    {
1969132718Skan    case DW_CFA_def_cfa:
1970132718Skan    case DW_CFA_def_cfa_sf:
1971132718Skan    case DW_CFA_offset:
1972132718Skan    case DW_CFA_offset_extended_sf:
1973132718Skan    case DW_CFA_offset_extended:
1974132718Skan      return dw_cfi_oprnd_offset;
1975132718Skan
1976132718Skan    case DW_CFA_register:
1977132718Skan      return dw_cfi_oprnd_reg_num;
1978132718Skan
1979132718Skan    default:
1980132718Skan      return dw_cfi_oprnd_unused;
1981132718Skan    }
1982132718Skan}
1983132718Skan
1984132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1985132718Skan
1986169689Skan/* Switch to eh_frame_section.  If we don't have an eh_frame_section,
1987169689Skan   switch to the data section instead, and write out a synthetic label
1988169689Skan   for collect2.  */
1989169689Skan
1990169689Skanstatic void
1991169689Skanswitch_to_eh_frame_section (void)
1992169689Skan{
1993169689Skan  tree label;
1994169689Skan
1995169689Skan#ifdef EH_FRAME_SECTION_NAME
1996169689Skan  if (eh_frame_section == 0)
1997169689Skan    {
1998169689Skan      int flags;
1999169689Skan
2000169689Skan      if (EH_TABLES_CAN_BE_READ_ONLY)
2001169689Skan	{
2002169689Skan	  int fde_encoding;
2003169689Skan	  int per_encoding;
2004169689Skan	  int lsda_encoding;
2005169689Skan
2006169689Skan	  fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2007169689Skan						       /*global=*/0);
2008169689Skan	  per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2009169689Skan						       /*global=*/1);
2010169689Skan	  lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2011169689Skan							/*global=*/0);
2012169689Skan	  flags = ((! flag_pic
2013169689Skan		    || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2014169689Skan			&& (fde_encoding & 0x70) != DW_EH_PE_aligned
2015169689Skan			&& (per_encoding & 0x70) != DW_EH_PE_absptr
2016169689Skan			&& (per_encoding & 0x70) != DW_EH_PE_aligned
2017169689Skan			&& (lsda_encoding & 0x70) != DW_EH_PE_absptr
2018169689Skan			&& (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2019169689Skan		   ? 0 : SECTION_WRITE);
2020169689Skan	}
2021169689Skan      else
2022169689Skan	flags = SECTION_WRITE;
2023169689Skan      eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2024169689Skan    }
2025132718Skan#endif
2026132718Skan
2027169689Skan  if (eh_frame_section)
2028169689Skan    switch_to_section (eh_frame_section);
2029169689Skan  else
2030169689Skan    {
2031169689Skan      /* We have no special eh_frame section.  Put the information in
2032169689Skan	 the data section and emit special labels to guide collect2.  */
2033169689Skan      switch_to_section (data_section);
2034169689Skan      label = get_file_function_name ('F');
2035169689Skan      ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2036169689Skan      targetm.asm_out.globalize_label (asm_out_file,
2037169689Skan				       IDENTIFIER_POINTER (label));
2038169689Skan      ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2039169689Skan    }
2040169689Skan}
2041169689Skan
204250397Sobrien/* Output a Call Frame Information opcode and its operand(s).  */
204350397Sobrien
204450397Sobrienstatic void
2045132718Skanoutput_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
204650397Sobrien{
2047132718Skan  unsigned long r;
204850397Sobrien  if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
204990075Sobrien    dw2_asm_output_data (1, (cfi->dw_cfi_opc
205090075Sobrien			     | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2051132718Skan			 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
205290075Sobrien			 cfi->dw_cfi_oprnd1.dw_cfi_offset);
205350397Sobrien  else if (cfi->dw_cfi_opc == DW_CFA_offset)
205450397Sobrien    {
2055132718Skan      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2056132718Skan      dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2057132718Skan			   "DW_CFA_offset, column 0x%lx", r);
205890075Sobrien      dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
205950397Sobrien    }
206050397Sobrien  else if (cfi->dw_cfi_opc == DW_CFA_restore)
2061132718Skan    {
2062132718Skan      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2063132718Skan      dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2064132718Skan			   "DW_CFA_restore, column 0x%lx", r);
2065132718Skan    }
206650397Sobrien  else
206750397Sobrien    {
206890075Sobrien      dw2_asm_output_data (1, cfi->dw_cfi_opc,
206990075Sobrien			   "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
207050397Sobrien
207150397Sobrien      switch (cfi->dw_cfi_opc)
207250397Sobrien	{
207350397Sobrien	case DW_CFA_set_loc:
207490075Sobrien	  if (for_eh)
207590075Sobrien	    dw2_asm_output_encoded_addr_rtx (
207690075Sobrien		ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
207790075Sobrien		gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2078169689Skan		false, NULL);
207990075Sobrien	  else
208090075Sobrien	    dw2_asm_output_addr (DWARF2_ADDR_SIZE,
208190075Sobrien				 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2082169689Skan	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
208350397Sobrien	  break;
208490075Sobrien
208550397Sobrien	case DW_CFA_advance_loc1:
208690075Sobrien	  dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
208790075Sobrien				fde->dw_fde_current_label, NULL);
208850397Sobrien	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
208950397Sobrien	  break;
209090075Sobrien
209150397Sobrien	case DW_CFA_advance_loc2:
209290075Sobrien	  dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
209390075Sobrien				fde->dw_fde_current_label, NULL);
209450397Sobrien	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
209550397Sobrien	  break;
209690075Sobrien
209750397Sobrien	case DW_CFA_advance_loc4:
209890075Sobrien	  dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
209990075Sobrien				fde->dw_fde_current_label, NULL);
210050397Sobrien	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
210150397Sobrien	  break;
210290075Sobrien
210350397Sobrien	case DW_CFA_MIPS_advance_loc8:
210490075Sobrien	  dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
210590075Sobrien				fde->dw_fde_current_label, NULL);
210690075Sobrien	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
210750397Sobrien	  break;
210890075Sobrien
210950397Sobrien	case DW_CFA_offset_extended:
211050397Sobrien	case DW_CFA_def_cfa:
2111132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2112132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
211390075Sobrien	  dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
211450397Sobrien	  break;
211590075Sobrien
211690075Sobrien	case DW_CFA_offset_extended_sf:
211790075Sobrien	case DW_CFA_def_cfa_sf:
2118132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2119132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
212090075Sobrien	  dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
212190075Sobrien	  break;
212290075Sobrien
212350397Sobrien	case DW_CFA_restore_extended:
212450397Sobrien	case DW_CFA_undefined:
212550397Sobrien	case DW_CFA_same_value:
212650397Sobrien	case DW_CFA_def_cfa_register:
2127132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2128132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
212950397Sobrien	  break;
213090075Sobrien
213150397Sobrien	case DW_CFA_register:
2132132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2133132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
2134132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2135132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
213650397Sobrien	  break;
213790075Sobrien
213850397Sobrien	case DW_CFA_def_cfa_offset:
213990075Sobrien	case DW_CFA_GNU_args_size:
214090075Sobrien	  dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
214150397Sobrien	  break;
214290075Sobrien
214390075Sobrien	case DW_CFA_def_cfa_offset_sf:
214490075Sobrien	  dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
214590075Sobrien	  break;
214690075Sobrien
214750397Sobrien	case DW_CFA_GNU_window_save:
214850397Sobrien	  break;
214990075Sobrien
215090075Sobrien	case DW_CFA_def_cfa_expression:
215190075Sobrien	case DW_CFA_expression:
215290075Sobrien	  output_cfa_loc (cfi);
215350397Sobrien	  break;
215490075Sobrien
215590075Sobrien	case DW_CFA_GNU_negative_offset_extended:
215690075Sobrien	  /* Obsoleted by DW_CFA_offset_extended_sf.  */
2157169689Skan	  gcc_unreachable ();
215890075Sobrien
215950397Sobrien	default:
216050397Sobrien	  break;
216150397Sobrien	}
216290075Sobrien    }
216350397Sobrien}
216450397Sobrien
2165132718Skan/* Output the call frame information used to record information
216650397Sobrien   that relates to calculating the frame pointer, and records the
216750397Sobrien   location of saved registers.  */
216850397Sobrien
216950397Sobrienstatic void
2170132718Skanoutput_call_frame_info (int for_eh)
217150397Sobrien{
217290075Sobrien  unsigned int i;
217390075Sobrien  dw_fde_ref fde;
217490075Sobrien  dw_cfi_ref cfi;
217590075Sobrien  char l1[20], l2[20], section_start_label[20];
2176117395Skan  bool any_lsda_needed = false;
217790075Sobrien  char augmentation[6];
217890075Sobrien  int augmentation_size;
217990075Sobrien  int fde_encoding = DW_EH_PE_absptr;
218090075Sobrien  int per_encoding = DW_EH_PE_absptr;
218190075Sobrien  int lsda_encoding = DW_EH_PE_absptr;
2182169689Skan  int return_reg;
218350397Sobrien
2184117395Skan  /* Don't emit a CIE if there won't be any FDEs.  */
2185117395Skan  if (fde_table_in_use == 0)
2186117395Skan    return;
2187117395Skan
2188169689Skan  /* If we make FDEs linkonce, we may have to emit an empty label for
2189169689Skan     an FDE that wouldn't otherwise be emitted.  We want to avoid
2190169689Skan     having an FDE kept around when the function it refers to is
2191169689Skan     discarded.  Example where this matters: a primary function
2192169689Skan     template in C++ requires EH information, but an explicit
2193169689Skan     specialization doesn't.  */
2194169689Skan  if (TARGET_USES_WEAK_UNWIND_INFO
2195169689Skan      && ! flag_asynchronous_unwind_tables
2196169689Skan      && for_eh)
2197169689Skan    for (i = 0; i < fde_table_in_use; i++)
2198169689Skan      if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2199169689Skan          && !fde_table[i].uses_eh_lsda
2200169689Skan	  && ! DECL_WEAK (fde_table[i].decl))
2201169689Skan	targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2202169689Skan				      for_eh, /* empty */ 1);
2203169689Skan
2204117395Skan  /* If we don't have any functions we'll want to unwind out of, don't
2205117395Skan     emit any EH unwind information.  Note that if exceptions aren't
2206117395Skan     enabled, we won't have collected nothrow information, and if we
2207117395Skan     asked for asynchronous tables, we always want this info.  */
220890075Sobrien  if (for_eh)
220990075Sobrien    {
2210117395Skan      bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
221150397Sobrien
221290075Sobrien      for (i = 0; i < fde_table_in_use; i++)
221390075Sobrien	if (fde_table[i].uses_eh_lsda)
2214117395Skan	  any_eh_needed = any_lsda_needed = true;
2215169689Skan        else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2216169689Skan	  any_eh_needed = true;
2217132718Skan	else if (! fde_table[i].nothrow
2218132718Skan		 && ! fde_table[i].all_throwers_are_sibcalls)
2219117395Skan	  any_eh_needed = true;
222050397Sobrien
222190075Sobrien      if (! any_eh_needed)
222290075Sobrien	return;
222390075Sobrien    }
222490075Sobrien
222550397Sobrien  /* We're going to be generating comments, so turn on app.  */
222650397Sobrien  if (flag_debug_asm)
222750397Sobrien    app_enable ();
222850397Sobrien
222950397Sobrien  if (for_eh)
2230169689Skan    switch_to_eh_frame_section ();
223150397Sobrien  else
2232169689Skan    {
2233169689Skan      if (!debug_frame_section)
2234169689Skan	debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2235169689Skan					   SECTION_DEBUG, NULL);
2236169689Skan      switch_to_section (debug_frame_section);
2237169689Skan    }
223850397Sobrien
223990075Sobrien  ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
224090075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
224190075Sobrien
224290075Sobrien  /* Output the CIE.  */
224350397Sobrien  ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
224450397Sobrien  ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2245169689Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2246169689Skan    dw2_asm_output_data (4, 0xffffffff,
2247169689Skan      "Initial length escape value indicating 64-bit DWARF extension");
224890075Sobrien  dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
224990075Sobrien			"Length of Common Information Entry");
225050397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l1);
225150397Sobrien
225290075Sobrien  /* Now that the CIE pointer is PC-relative for EH,
225390075Sobrien     use 0 to identify the CIE.  */
225490075Sobrien  dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2255169689Skan		       (for_eh ? 0 : DWARF_CIE_ID),
225690075Sobrien		       "CIE Identifier Tag");
225750397Sobrien
225890075Sobrien  dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
225950397Sobrien
226090075Sobrien  augmentation[0] = 0;
226190075Sobrien  augmentation_size = 0;
226290075Sobrien  if (for_eh)
226350397Sobrien    {
226490075Sobrien      char *p;
226550397Sobrien
226690075Sobrien      /* Augmentation:
226790075Sobrien	 z	Indicates that a uleb128 is present to size the
2268132718Skan		augmentation section.
226990075Sobrien	 L	Indicates the encoding (and thus presence) of
227090075Sobrien		an LSDA pointer in the FDE augmentation.
227190075Sobrien	 R	Indicates a non-default pointer encoding for
227290075Sobrien		FDE code pointers.
227390075Sobrien	 P	Indicates the presence of an encoding + language
227490075Sobrien		personality routine in the CIE augmentation.  */
227550397Sobrien
227690075Sobrien      fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
227790075Sobrien      per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
227890075Sobrien      lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
227990075Sobrien
228090075Sobrien      p = augmentation + 1;
228190075Sobrien      if (eh_personality_libfunc)
228250397Sobrien	{
228390075Sobrien	  *p++ = 'P';
228490075Sobrien	  augmentation_size += 1 + size_of_encoded_value (per_encoding);
228550397Sobrien	}
228690075Sobrien      if (any_lsda_needed)
228750397Sobrien	{
228890075Sobrien	  *p++ = 'L';
228990075Sobrien	  augmentation_size += 1;
229050397Sobrien	}
229190075Sobrien      if (fde_encoding != DW_EH_PE_absptr)
229290075Sobrien	{
229390075Sobrien	  *p++ = 'R';
229490075Sobrien	  augmentation_size += 1;
229590075Sobrien	}
229690075Sobrien      if (p > augmentation + 1)
229790075Sobrien	{
229890075Sobrien	  augmentation[0] = 'z';
2299117395Skan	  *p = '\0';
230090075Sobrien	}
230150397Sobrien
230290075Sobrien      /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
230390075Sobrien      if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
230490075Sobrien	{
230590075Sobrien	  int offset = (  4		/* Length */
230690075Sobrien			+ 4		/* CIE Id */
230790075Sobrien			+ 1		/* CIE version */
230890075Sobrien			+ strlen (augmentation) + 1	/* Augmentation */
230990075Sobrien			+ size_of_uleb128 (1)		/* Code alignment */
231090075Sobrien			+ size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
231190075Sobrien			+ 1		/* RA column */
231290075Sobrien			+ 1		/* Augmentation size */
231390075Sobrien			+ 1		/* Personality encoding */ );
231490075Sobrien	  int pad = -offset & (PTR_SIZE - 1);
231590075Sobrien
231690075Sobrien	  augmentation_size += pad;
231790075Sobrien
231890075Sobrien	  /* Augmentations should be small, so there's scarce need to
231990075Sobrien	     iterate for a solution.  Die if we exceed one uleb128 byte.  */
2320169689Skan	  gcc_assert (size_of_uleb128 (augmentation_size) == 1);
232190075Sobrien	}
232250397Sobrien    }
232350397Sobrien
232490075Sobrien  dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
232590075Sobrien  dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
232690075Sobrien  dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
232790075Sobrien			       "CIE Data Alignment Factor");
232850397Sobrien
2329169689Skan  return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2330169689Skan  if (DW_CIE_VERSION == 1)
2331169689Skan    dw2_asm_output_data (1, return_reg, "CIE RA Column");
2332169689Skan  else
2333169689Skan    dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2334169689Skan
233590075Sobrien  if (augmentation[0])
233690075Sobrien    {
233790075Sobrien      dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
233890075Sobrien      if (eh_personality_libfunc)
233990075Sobrien	{
234090075Sobrien	  dw2_asm_output_data (1, per_encoding, "Personality (%s)",
234190075Sobrien			       eh_data_format_name (per_encoding));
234290075Sobrien	  dw2_asm_output_encoded_addr_rtx (per_encoding,
2343169689Skan					   eh_personality_libfunc,
2344169689Skan					   true, NULL);
234590075Sobrien	}
234650397Sobrien
234790075Sobrien      if (any_lsda_needed)
234890075Sobrien	dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
234990075Sobrien			     eh_data_format_name (lsda_encoding));
235050397Sobrien
235190075Sobrien      if (fde_encoding != DW_EH_PE_absptr)
235290075Sobrien	dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
235390075Sobrien			     eh_data_format_name (fde_encoding));
235490075Sobrien    }
235550397Sobrien
235650397Sobrien  for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
235790075Sobrien    output_cfi (cfi, NULL, for_eh);
235850397Sobrien
235950397Sobrien  /* Pad the CIE out to an address sized boundary.  */
2360117395Skan  ASM_OUTPUT_ALIGN (asm_out_file,
236190075Sobrien		    floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
236250397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l2);
236350397Sobrien
236450397Sobrien  /* Loop through all of the FDE's.  */
236590075Sobrien  for (i = 0; i < fde_table_in_use; i++)
236650397Sobrien    {
236750397Sobrien      fde = &fde_table[i];
236850397Sobrien
236990075Sobrien      /* Don't emit EH unwind info for leaf functions that don't need it.  */
2370117395Skan      if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2371117395Skan	  && (fde->nothrow || fde->all_throwers_are_sibcalls)
2372169689Skan	  && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2373117395Skan	  && !fde->uses_eh_lsda)
237490075Sobrien	continue;
237590075Sobrien
2376169689Skan      targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2377169689Skan      targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
237890075Sobrien      ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
237990075Sobrien      ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2380169689Skan      if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2381169689Skan	dw2_asm_output_data (4, 0xffffffff,
2382169689Skan			     "Initial length escape value indicating 64-bit DWARF extension");
238390075Sobrien      dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
238490075Sobrien			    "FDE Length");
238590075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, l1);
238690075Sobrien
238750397Sobrien      if (for_eh)
238890075Sobrien	dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
238950397Sobrien      else
239090075Sobrien	dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2391169689Skan			       debug_frame_section, "FDE CIE offset");
239250397Sobrien
239350397Sobrien      if (for_eh)
239490075Sobrien	{
2395169689Skan	  rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2396169689Skan	  SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
239790075Sobrien	  dw2_asm_output_encoded_addr_rtx (fde_encoding,
2398169689Skan					   sym_ref,
2399169689Skan					   false,
2400169689Skan					   "FDE initial location");
2401169689Skan	  if (fde->dw_fde_switched_sections)
2402169689Skan	    {
2403169689Skan	      rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2404169689Skan				      fde->dw_fde_unlikely_section_label);
2405169689Skan	      rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2406169689Skan				      fde->dw_fde_hot_section_label);
2407169689Skan	      SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2408169689Skan	      SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2409169689Skan	      dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2410169689Skan					       "FDE initial location");
2411169689Skan	      dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2412169689Skan				    fde->dw_fde_hot_section_end_label,
2413169689Skan				    fde->dw_fde_hot_section_label,
2414169689Skan				    "FDE address range");
2415169689Skan	      dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2416169689Skan					       "FDE initial location");
2417169689Skan	      dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2418169689Skan				    fde->dw_fde_unlikely_section_end_label,
2419169689Skan				    fde->dw_fde_unlikely_section_label,
2420169689Skan				    "FDE address range");
2421169689Skan	    }
2422169689Skan	  else
2423169689Skan	    dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2424169689Skan				  fde->dw_fde_end, fde->dw_fde_begin,
2425169689Skan				  "FDE address range");
242690075Sobrien	}
242750397Sobrien      else
242890075Sobrien	{
242990075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
243090075Sobrien			       "FDE initial location");
2431169689Skan	  if (fde->dw_fde_switched_sections)
2432169689Skan	    {
2433169689Skan	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2434169689Skan				   fde->dw_fde_hot_section_label,
2435169689Skan				   "FDE initial location");
2436169689Skan	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2437169689Skan				    fde->dw_fde_hot_section_end_label,
2438169689Skan				    fde->dw_fde_hot_section_label,
2439169689Skan				    "FDE address range");
2440169689Skan	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2441169689Skan				   fde->dw_fde_unlikely_section_label,
2442169689Skan				   "FDE initial location");
2443169689Skan	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2444169689Skan				    fde->dw_fde_unlikely_section_end_label,
2445169689Skan				    fde->dw_fde_unlikely_section_label,
2446169689Skan				    "FDE address range");
2447169689Skan	    }
2448169689Skan	  else
2449169689Skan	    dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2450169689Skan				  fde->dw_fde_end, fde->dw_fde_begin,
2451169689Skan				  "FDE address range");
245290075Sobrien	}
245350397Sobrien
245490075Sobrien      if (augmentation[0])
245590075Sobrien	{
245690075Sobrien	  if (any_lsda_needed)
245790075Sobrien	    {
245890075Sobrien	      int size = size_of_encoded_value (lsda_encoding);
245950397Sobrien
246090075Sobrien	      if (lsda_encoding == DW_EH_PE_aligned)
246190075Sobrien		{
246290075Sobrien		  int offset = (  4		/* Length */
246390075Sobrien				+ 4		/* CIE offset */
246490075Sobrien				+ 2 * size_of_encoded_value (fde_encoding)
246590075Sobrien				+ 1		/* Augmentation size */ );
246690075Sobrien		  int pad = -offset & (PTR_SIZE - 1);
246750397Sobrien
246890075Sobrien		  size += pad;
2469169689Skan		  gcc_assert (size_of_uleb128 (size) == 1);
247090075Sobrien		}
247150397Sobrien
247290075Sobrien	      dw2_asm_output_data_uleb128 (size, "Augmentation size");
247390075Sobrien
247490075Sobrien	      if (fde->uses_eh_lsda)
2475132718Skan		{
2476132718Skan		  ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
247790075Sobrien					       fde->funcdef_number);
2478132718Skan		  dw2_asm_output_encoded_addr_rtx (
247990075Sobrien			lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2480169689Skan			false, "Language Specific Data Area");
2481132718Skan		}
248290075Sobrien	      else
248390075Sobrien		{
248490075Sobrien		  if (lsda_encoding == DW_EH_PE_aligned)
248590075Sobrien		    ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
248690075Sobrien		  dw2_asm_output_data
248790075Sobrien		    (size_of_encoded_value (lsda_encoding), 0,
248890075Sobrien		     "Language Specific Data Area (none)");
248990075Sobrien		}
249090075Sobrien	    }
249190075Sobrien	  else
249290075Sobrien	    dw2_asm_output_data_uleb128 (0, "Augmentation size");
249390075Sobrien	}
249490075Sobrien
249550397Sobrien      /* Loop through the Call Frame Instructions associated with
249650397Sobrien	 this FDE.  */
249750397Sobrien      fde->dw_fde_current_label = fde->dw_fde_begin;
249850397Sobrien      for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
249990075Sobrien	output_cfi (cfi, fde, for_eh);
250050397Sobrien
250150397Sobrien      /* Pad the FDE out to an address sized boundary.  */
2502117395Skan      ASM_OUTPUT_ALIGN (asm_out_file,
2503132718Skan			floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
250450397Sobrien      ASM_OUTPUT_LABEL (asm_out_file, l2);
250550397Sobrien    }
250690075Sobrien
2507117395Skan  if (for_eh && targetm.terminate_dw2_eh_frame_info)
250890075Sobrien    dw2_asm_output_data (4, 0, "End of Table");
250950397Sobrien#ifdef MIPS_DEBUGGING_INFO
251050397Sobrien  /* Work around Irix 6 assembler bug whereby labels at the end of a section
251150397Sobrien     get a value of 0.  Putting .align 0 after the label fixes it.  */
251250397Sobrien  ASM_OUTPUT_ALIGN (asm_out_file, 0);
251350397Sobrien#endif
251450397Sobrien
251550397Sobrien  /* Turn off app to make assembly quicker.  */
251650397Sobrien  if (flag_debug_asm)
251750397Sobrien    app_disable ();
251850397Sobrien}
251950397Sobrien
252050397Sobrien/* Output a marker (i.e. a label) for the beginning of a function, before
252150397Sobrien   the prologue.  */
252250397Sobrien
252350397Sobrienvoid
2524132718Skandwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2525132718Skan			  const char *file ATTRIBUTE_UNUSED)
252650397Sobrien{
252750397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
2528169689Skan  char * dup_label;
252990075Sobrien  dw_fde_ref fde;
253050397Sobrien
2531169689Skan  current_function_func_begin_label = NULL;
253250397Sobrien
2533169689Skan#ifdef TARGET_UNWIND_INFO
253490075Sobrien  /* ??? current_function_func_begin_label is also used by except.c
253590075Sobrien     for call-site information.  We must emit this label if it might
253690075Sobrien     be used.  */
253790075Sobrien  if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
253890075Sobrien      && ! dwarf2out_do_frame ())
253990075Sobrien    return;
254090075Sobrien#else
254190075Sobrien  if (! dwarf2out_do_frame ())
254290075Sobrien    return;
254390075Sobrien#endif
254490075Sobrien
2545169689Skan  switch_to_section (function_section (current_function_decl));
254650397Sobrien  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2547117395Skan			       current_function_funcdef_no);
254890075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2549117395Skan			  current_function_funcdef_no);
2550169689Skan  dup_label = xstrdup (label);
2551169689Skan  current_function_func_begin_label = dup_label;
255250397Sobrien
2553169689Skan#ifdef TARGET_UNWIND_INFO
255490075Sobrien  /* We can elide the fde allocation if we're not emitting debug info.  */
255590075Sobrien  if (! dwarf2out_do_frame ())
255690075Sobrien    return;
255790075Sobrien#endif
255890075Sobrien
255950397Sobrien  /* Expand the fde table if necessary.  */
256050397Sobrien  if (fde_table_in_use == fde_table_allocated)
256150397Sobrien    {
256250397Sobrien      fde_table_allocated += FDE_TABLE_INCREMENT;
2563132718Skan      fde_table = ggc_realloc (fde_table,
2564132718Skan			       fde_table_allocated * sizeof (dw_fde_node));
2565132718Skan      memset (fde_table + fde_table_in_use, 0,
2566132718Skan	      FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
256750397Sobrien    }
256850397Sobrien
256950397Sobrien  /* Record the FDE associated with this function.  */
257050397Sobrien  current_funcdef_fde = fde_table_in_use;
257150397Sobrien
257250397Sobrien  /* Add the new FDE at the end of the fde_table.  */
257350397Sobrien  fde = &fde_table[fde_table_in_use++];
2574169689Skan  fde->decl = current_function_decl;
2575169689Skan  fde->dw_fde_begin = dup_label;
2576169689Skan  fde->dw_fde_current_label = dup_label;
2577169689Skan  fde->dw_fde_hot_section_label = NULL;
2578169689Skan  fde->dw_fde_hot_section_end_label = NULL;
2579169689Skan  fde->dw_fde_unlikely_section_label = NULL;
2580169689Skan  fde->dw_fde_unlikely_section_end_label = NULL;
2581169689Skan  fde->dw_fde_switched_sections = false;
258250397Sobrien  fde->dw_fde_end = NULL;
258350397Sobrien  fde->dw_fde_cfi = NULL;
2584117395Skan  fde->funcdef_number = current_function_funcdef_no;
2585169689Skan  fde->nothrow = TREE_NOTHROW (current_function_decl);
258690075Sobrien  fde->uses_eh_lsda = cfun->uses_eh_lsda;
2587117395Skan  fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
258850397Sobrien
258950397Sobrien  args_size = old_args_size = 0;
259090075Sobrien
259190075Sobrien  /* We only want to output line number information for the genuine dwarf2
259290075Sobrien     prologue case, not the eh frame case.  */
259390075Sobrien#ifdef DWARF2_DEBUGGING_INFO
259490075Sobrien  if (file)
259590075Sobrien    dwarf2out_source_line (line, file);
259690075Sobrien#endif
259750397Sobrien}
259850397Sobrien
259950397Sobrien/* Output a marker (i.e. a label) for the absolute end of the generated code
260050397Sobrien   for a function definition.  This gets called *after* the epilogue code has
260150397Sobrien   been generated.  */
260250397Sobrien
260350397Sobrienvoid
2604132718Skandwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2605132718Skan			const char *file ATTRIBUTE_UNUSED)
260650397Sobrien{
260750397Sobrien  dw_fde_ref fde;
260850397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
260950397Sobrien
261050397Sobrien  /* Output a label to mark the endpoint of the code generated for this
261190075Sobrien     function.  */
2612117395Skan  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2613117395Skan			       current_function_funcdef_no);
261450397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, label);
261550397Sobrien  fde = &fde_table[fde_table_in_use - 1];
261650397Sobrien  fde->dw_fde_end = xstrdup (label);
261750397Sobrien}
261850397Sobrien
261950397Sobrienvoid
2620132718Skandwarf2out_frame_init (void)
262150397Sobrien{
262250397Sobrien  /* Allocate the initial hunk of the fde_table.  */
2623132718Skan  fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
262450397Sobrien  fde_table_allocated = FDE_TABLE_INCREMENT;
262550397Sobrien  fde_table_in_use = 0;
262650397Sobrien
262750397Sobrien  /* Generate the CFA instructions common to all FDE's.  Do it now for the
262850397Sobrien     sake of lookup_cfa.  */
262950397Sobrien
263050397Sobrien  /* On entry, the Canonical Frame Address is at SP.  */
263150397Sobrien  dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2632169689Skan
2633169689Skan#ifdef DWARF2_UNWIND_INFO
2634169689Skan  if (DWARF2_UNWIND_INFO)
2635169689Skan    initial_return_save (INCOMING_RETURN_ADDR_RTX);
263650397Sobrien#endif
263750397Sobrien}
263850397Sobrien
263950397Sobrienvoid
2640132718Skandwarf2out_frame_finish (void)
264150397Sobrien{
264250397Sobrien  /* Output call frame information.  */
2643169689Skan  if (DWARF2_FRAME_INFO)
264450397Sobrien    output_call_frame_info (0);
264590075Sobrien
2646169689Skan#ifndef TARGET_UNWIND_INFO
2647169689Skan  /* Output another copy for the unwinder.  */
264890075Sobrien  if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
264950397Sobrien    output_call_frame_info (1);
2650169689Skan#endif
265190075Sobrien}
2652132718Skan#endif
265390075Sobrien
265490075Sobrien/* And now, the subset of the debugging information support code necessary
265590075Sobrien   for emitting location expressions.  */
265650397Sobrien
2657169689Skan/* Data about a single source file.  */
2658169689Skanstruct dwarf_file_data GTY(())
2659169689Skan{
2660169689Skan  const char * filename;
2661169689Skan  int emitted_number;
2662169689Skan};
2663169689Skan
2664117395Skan/* We need some way to distinguish DW_OP_addr with a direct symbol
2665117395Skan   relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2666117395Skan#define INTERNAL_DW_OP_tls_addr		(0x100 + DW_OP_addr)
2667117395Skan
2668117395Skan
266990075Sobrientypedef struct dw_val_struct *dw_val_ref;
267090075Sobrientypedef struct die_struct *dw_die_ref;
267190075Sobrientypedef struct dw_loc_descr_struct *dw_loc_descr_ref;
267290075Sobrientypedef struct dw_loc_list_struct *dw_loc_list_ref;
267350397Sobrien
267450397Sobrien/* Each DIE may have a series of attribute/value pairs.  Values
267550397Sobrien   can take on several forms.  The forms that are used in this
267650397Sobrien   implementation are listed below.  */
267750397Sobrien
2678132718Skanenum dw_val_class
267950397Sobrien{
268050397Sobrien  dw_val_class_addr,
268190075Sobrien  dw_val_class_offset,
268250397Sobrien  dw_val_class_loc,
268390075Sobrien  dw_val_class_loc_list,
268490075Sobrien  dw_val_class_range_list,
268550397Sobrien  dw_val_class_const,
268650397Sobrien  dw_val_class_unsigned_const,
268750397Sobrien  dw_val_class_long_long,
2688132718Skan  dw_val_class_vec,
268950397Sobrien  dw_val_class_flag,
269050397Sobrien  dw_val_class_die_ref,
269150397Sobrien  dw_val_class_fde_ref,
269250397Sobrien  dw_val_class_lbl_id,
2693169689Skan  dw_val_class_lineptr,
2694169689Skan  dw_val_class_str,
2695169689Skan  dw_val_class_macptr,
2696169689Skan  dw_val_class_file
2697132718Skan};
269850397Sobrien
269950397Sobrien/* Describe a double word constant value.  */
270090075Sobrien/* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
270150397Sobrien
2702132718Skantypedef struct dw_long_long_struct GTY(())
270350397Sobrien{
270450397Sobrien  unsigned long hi;
270550397Sobrien  unsigned long low;
270650397Sobrien}
270750397Sobriendw_long_long_const;
270850397Sobrien
2709132718Skan/* Describe a floating point constant value, or a vector constant value.  */
271050397Sobrien
2711132718Skantypedef struct dw_vec_struct GTY(())
271250397Sobrien{
2713132718Skan  unsigned char * GTY((length ("%h.length"))) array;
271450397Sobrien  unsigned length;
2715132718Skan  unsigned elt_size;
271650397Sobrien}
2717132718Skandw_vec_const;
271850397Sobrien
271950397Sobrien/* The dw_val_node describes an attribute's value, as it is
272050397Sobrien   represented internally.  */
272150397Sobrien
2722132718Skantypedef struct dw_val_struct GTY(())
272350397Sobrien{
2724132718Skan  enum dw_val_class val_class;
2725132718Skan  union dw_val_struct_union
272650397Sobrien    {
2727132718Skan      rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2728132718Skan      unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2729132718Skan      dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2730132718Skan      dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2731169689Skan      HOST_WIDE_INT GTY ((default)) val_int;
2732132718Skan      unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2733132718Skan      dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2734132718Skan      dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2735132718Skan      struct dw_val_die_union
273690075Sobrien	{
273790075Sobrien	  dw_die_ref die;
273890075Sobrien	  int external;
2739132718Skan	} GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2740132718Skan      unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2741132718Skan      struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2742132718Skan      char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2743132718Skan      unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2744169689Skan      struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
274550397Sobrien    }
2746132718Skan  GTY ((desc ("%1.val_class"))) v;
274750397Sobrien}
274850397Sobriendw_val_node;
274950397Sobrien
275050397Sobrien/* Locations in memory are described using a sequence of stack machine
275150397Sobrien   operations.  */
275250397Sobrien
2753132718Skantypedef struct dw_loc_descr_struct GTY(())
275450397Sobrien{
275550397Sobrien  dw_loc_descr_ref dw_loc_next;
275650397Sobrien  enum dwarf_location_atom dw_loc_opc;
275750397Sobrien  dw_val_node dw_loc_oprnd1;
275850397Sobrien  dw_val_node dw_loc_oprnd2;
275990075Sobrien  int dw_loc_addr;
276050397Sobrien}
276150397Sobriendw_loc_descr_node;
276250397Sobrien
276390075Sobrien/* Location lists are ranges + location descriptions for that range,
276490075Sobrien   so you can track variables that are in different places over
276590075Sobrien   their entire life.  */
2766132718Skantypedef struct dw_loc_list_struct GTY(())
276790075Sobrien{
276890075Sobrien  dw_loc_list_ref dw_loc_next;
276990075Sobrien  const char *begin; /* Label for begin address of range */
277090075Sobrien  const char *end;  /* Label for end address of range */
277190075Sobrien  char *ll_symbol; /* Label for beginning of location list.
277290075Sobrien		      Only on head of list */
277390075Sobrien  const char *section; /* Section this loclist is relative to */
277490075Sobrien  dw_loc_descr_ref expr;
277590075Sobrien} dw_loc_list_node;
277690075Sobrien
2777132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
277890075Sobrien
2779132718Skanstatic const char *dwarf_stack_op_name (unsigned);
2780132718Skanstatic dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2781132718Skan				       unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2782132718Skanstatic void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2783132718Skanstatic unsigned long size_of_loc_descr (dw_loc_descr_ref);
2784132718Skanstatic unsigned long size_of_locs (dw_loc_descr_ref);
2785132718Skanstatic void output_loc_operands (dw_loc_descr_ref);
2786132718Skanstatic void output_loc_sequence (dw_loc_descr_ref);
2787132718Skan
278890075Sobrien/* Convert a DWARF stack opcode into its string name.  */
278990075Sobrien
279090075Sobrienstatic const char *
2791132718Skandwarf_stack_op_name (unsigned int op)
279290075Sobrien{
279390075Sobrien  switch (op)
279490075Sobrien    {
279590075Sobrien    case DW_OP_addr:
2796117395Skan    case INTERNAL_DW_OP_tls_addr:
279790075Sobrien      return "DW_OP_addr";
279890075Sobrien    case DW_OP_deref:
279990075Sobrien      return "DW_OP_deref";
280090075Sobrien    case DW_OP_const1u:
280190075Sobrien      return "DW_OP_const1u";
280290075Sobrien    case DW_OP_const1s:
280390075Sobrien      return "DW_OP_const1s";
280490075Sobrien    case DW_OP_const2u:
280590075Sobrien      return "DW_OP_const2u";
280690075Sobrien    case DW_OP_const2s:
280790075Sobrien      return "DW_OP_const2s";
280890075Sobrien    case DW_OP_const4u:
280990075Sobrien      return "DW_OP_const4u";
281090075Sobrien    case DW_OP_const4s:
281190075Sobrien      return "DW_OP_const4s";
281290075Sobrien    case DW_OP_const8u:
281390075Sobrien      return "DW_OP_const8u";
281490075Sobrien    case DW_OP_const8s:
281590075Sobrien      return "DW_OP_const8s";
281690075Sobrien    case DW_OP_constu:
281790075Sobrien      return "DW_OP_constu";
281890075Sobrien    case DW_OP_consts:
281990075Sobrien      return "DW_OP_consts";
282090075Sobrien    case DW_OP_dup:
282190075Sobrien      return "DW_OP_dup";
282290075Sobrien    case DW_OP_drop:
282390075Sobrien      return "DW_OP_drop";
282490075Sobrien    case DW_OP_over:
282590075Sobrien      return "DW_OP_over";
282690075Sobrien    case DW_OP_pick:
282790075Sobrien      return "DW_OP_pick";
282890075Sobrien    case DW_OP_swap:
282990075Sobrien      return "DW_OP_swap";
283090075Sobrien    case DW_OP_rot:
283190075Sobrien      return "DW_OP_rot";
283290075Sobrien    case DW_OP_xderef:
283390075Sobrien      return "DW_OP_xderef";
283490075Sobrien    case DW_OP_abs:
283590075Sobrien      return "DW_OP_abs";
283690075Sobrien    case DW_OP_and:
283790075Sobrien      return "DW_OP_and";
283890075Sobrien    case DW_OP_div:
283990075Sobrien      return "DW_OP_div";
284090075Sobrien    case DW_OP_minus:
284190075Sobrien      return "DW_OP_minus";
284290075Sobrien    case DW_OP_mod:
284390075Sobrien      return "DW_OP_mod";
284490075Sobrien    case DW_OP_mul:
284590075Sobrien      return "DW_OP_mul";
284690075Sobrien    case DW_OP_neg:
284790075Sobrien      return "DW_OP_neg";
284890075Sobrien    case DW_OP_not:
284990075Sobrien      return "DW_OP_not";
285090075Sobrien    case DW_OP_or:
285190075Sobrien      return "DW_OP_or";
285290075Sobrien    case DW_OP_plus:
285390075Sobrien      return "DW_OP_plus";
285490075Sobrien    case DW_OP_plus_uconst:
285590075Sobrien      return "DW_OP_plus_uconst";
285690075Sobrien    case DW_OP_shl:
285790075Sobrien      return "DW_OP_shl";
285890075Sobrien    case DW_OP_shr:
285990075Sobrien      return "DW_OP_shr";
286090075Sobrien    case DW_OP_shra:
286190075Sobrien      return "DW_OP_shra";
286290075Sobrien    case DW_OP_xor:
286390075Sobrien      return "DW_OP_xor";
286490075Sobrien    case DW_OP_bra:
286590075Sobrien      return "DW_OP_bra";
286690075Sobrien    case DW_OP_eq:
286790075Sobrien      return "DW_OP_eq";
286890075Sobrien    case DW_OP_ge:
286990075Sobrien      return "DW_OP_ge";
287090075Sobrien    case DW_OP_gt:
287190075Sobrien      return "DW_OP_gt";
287290075Sobrien    case DW_OP_le:
287390075Sobrien      return "DW_OP_le";
287490075Sobrien    case DW_OP_lt:
287590075Sobrien      return "DW_OP_lt";
287690075Sobrien    case DW_OP_ne:
287790075Sobrien      return "DW_OP_ne";
287890075Sobrien    case DW_OP_skip:
287990075Sobrien      return "DW_OP_skip";
288090075Sobrien    case DW_OP_lit0:
288190075Sobrien      return "DW_OP_lit0";
288290075Sobrien    case DW_OP_lit1:
288390075Sobrien      return "DW_OP_lit1";
288490075Sobrien    case DW_OP_lit2:
288590075Sobrien      return "DW_OP_lit2";
288690075Sobrien    case DW_OP_lit3:
288790075Sobrien      return "DW_OP_lit3";
288890075Sobrien    case DW_OP_lit4:
288990075Sobrien      return "DW_OP_lit4";
289090075Sobrien    case DW_OP_lit5:
289190075Sobrien      return "DW_OP_lit5";
289290075Sobrien    case DW_OP_lit6:
289390075Sobrien      return "DW_OP_lit6";
289490075Sobrien    case DW_OP_lit7:
289590075Sobrien      return "DW_OP_lit7";
289690075Sobrien    case DW_OP_lit8:
289790075Sobrien      return "DW_OP_lit8";
289890075Sobrien    case DW_OP_lit9:
289990075Sobrien      return "DW_OP_lit9";
290090075Sobrien    case DW_OP_lit10:
290190075Sobrien      return "DW_OP_lit10";
290290075Sobrien    case DW_OP_lit11:
290390075Sobrien      return "DW_OP_lit11";
290490075Sobrien    case DW_OP_lit12:
290590075Sobrien      return "DW_OP_lit12";
290690075Sobrien    case DW_OP_lit13:
290790075Sobrien      return "DW_OP_lit13";
290890075Sobrien    case DW_OP_lit14:
290990075Sobrien      return "DW_OP_lit14";
291090075Sobrien    case DW_OP_lit15:
291190075Sobrien      return "DW_OP_lit15";
291290075Sobrien    case DW_OP_lit16:
291390075Sobrien      return "DW_OP_lit16";
291490075Sobrien    case DW_OP_lit17:
291590075Sobrien      return "DW_OP_lit17";
291690075Sobrien    case DW_OP_lit18:
291790075Sobrien      return "DW_OP_lit18";
291890075Sobrien    case DW_OP_lit19:
291990075Sobrien      return "DW_OP_lit19";
292090075Sobrien    case DW_OP_lit20:
292190075Sobrien      return "DW_OP_lit20";
292290075Sobrien    case DW_OP_lit21:
292390075Sobrien      return "DW_OP_lit21";
292490075Sobrien    case DW_OP_lit22:
292590075Sobrien      return "DW_OP_lit22";
292690075Sobrien    case DW_OP_lit23:
292790075Sobrien      return "DW_OP_lit23";
292890075Sobrien    case DW_OP_lit24:
292990075Sobrien      return "DW_OP_lit24";
293090075Sobrien    case DW_OP_lit25:
293190075Sobrien      return "DW_OP_lit25";
293290075Sobrien    case DW_OP_lit26:
293390075Sobrien      return "DW_OP_lit26";
293490075Sobrien    case DW_OP_lit27:
293590075Sobrien      return "DW_OP_lit27";
293690075Sobrien    case DW_OP_lit28:
293790075Sobrien      return "DW_OP_lit28";
293890075Sobrien    case DW_OP_lit29:
293990075Sobrien      return "DW_OP_lit29";
294090075Sobrien    case DW_OP_lit30:
294190075Sobrien      return "DW_OP_lit30";
294290075Sobrien    case DW_OP_lit31:
294390075Sobrien      return "DW_OP_lit31";
294490075Sobrien    case DW_OP_reg0:
294590075Sobrien      return "DW_OP_reg0";
294690075Sobrien    case DW_OP_reg1:
294790075Sobrien      return "DW_OP_reg1";
294890075Sobrien    case DW_OP_reg2:
294990075Sobrien      return "DW_OP_reg2";
295090075Sobrien    case DW_OP_reg3:
295190075Sobrien      return "DW_OP_reg3";
295290075Sobrien    case DW_OP_reg4:
295390075Sobrien      return "DW_OP_reg4";
295490075Sobrien    case DW_OP_reg5:
295590075Sobrien      return "DW_OP_reg5";
295690075Sobrien    case DW_OP_reg6:
295790075Sobrien      return "DW_OP_reg6";
295890075Sobrien    case DW_OP_reg7:
295990075Sobrien      return "DW_OP_reg7";
296090075Sobrien    case DW_OP_reg8:
296190075Sobrien      return "DW_OP_reg8";
296290075Sobrien    case DW_OP_reg9:
296390075Sobrien      return "DW_OP_reg9";
296490075Sobrien    case DW_OP_reg10:
296590075Sobrien      return "DW_OP_reg10";
296690075Sobrien    case DW_OP_reg11:
296790075Sobrien      return "DW_OP_reg11";
296890075Sobrien    case DW_OP_reg12:
296990075Sobrien      return "DW_OP_reg12";
297090075Sobrien    case DW_OP_reg13:
297190075Sobrien      return "DW_OP_reg13";
297290075Sobrien    case DW_OP_reg14:
297390075Sobrien      return "DW_OP_reg14";
297490075Sobrien    case DW_OP_reg15:
297590075Sobrien      return "DW_OP_reg15";
297690075Sobrien    case DW_OP_reg16:
297790075Sobrien      return "DW_OP_reg16";
297890075Sobrien    case DW_OP_reg17:
297990075Sobrien      return "DW_OP_reg17";
298090075Sobrien    case DW_OP_reg18:
298190075Sobrien      return "DW_OP_reg18";
298290075Sobrien    case DW_OP_reg19:
298390075Sobrien      return "DW_OP_reg19";
298490075Sobrien    case DW_OP_reg20:
298590075Sobrien      return "DW_OP_reg20";
298690075Sobrien    case DW_OP_reg21:
298790075Sobrien      return "DW_OP_reg21";
298890075Sobrien    case DW_OP_reg22:
298990075Sobrien      return "DW_OP_reg22";
299090075Sobrien    case DW_OP_reg23:
299190075Sobrien      return "DW_OP_reg23";
299290075Sobrien    case DW_OP_reg24:
299390075Sobrien      return "DW_OP_reg24";
299490075Sobrien    case DW_OP_reg25:
299590075Sobrien      return "DW_OP_reg25";
299690075Sobrien    case DW_OP_reg26:
299790075Sobrien      return "DW_OP_reg26";
299890075Sobrien    case DW_OP_reg27:
299990075Sobrien      return "DW_OP_reg27";
300090075Sobrien    case DW_OP_reg28:
300190075Sobrien      return "DW_OP_reg28";
300290075Sobrien    case DW_OP_reg29:
300390075Sobrien      return "DW_OP_reg29";
300490075Sobrien    case DW_OP_reg30:
300590075Sobrien      return "DW_OP_reg30";
300690075Sobrien    case DW_OP_reg31:
300790075Sobrien      return "DW_OP_reg31";
300890075Sobrien    case DW_OP_breg0:
300990075Sobrien      return "DW_OP_breg0";
301090075Sobrien    case DW_OP_breg1:
301190075Sobrien      return "DW_OP_breg1";
301290075Sobrien    case DW_OP_breg2:
301390075Sobrien      return "DW_OP_breg2";
301490075Sobrien    case DW_OP_breg3:
301590075Sobrien      return "DW_OP_breg3";
301690075Sobrien    case DW_OP_breg4:
301790075Sobrien      return "DW_OP_breg4";
301890075Sobrien    case DW_OP_breg5:
301990075Sobrien      return "DW_OP_breg5";
302090075Sobrien    case DW_OP_breg6:
302190075Sobrien      return "DW_OP_breg6";
302290075Sobrien    case DW_OP_breg7:
302390075Sobrien      return "DW_OP_breg7";
302490075Sobrien    case DW_OP_breg8:
302590075Sobrien      return "DW_OP_breg8";
302690075Sobrien    case DW_OP_breg9:
302790075Sobrien      return "DW_OP_breg9";
302890075Sobrien    case DW_OP_breg10:
302990075Sobrien      return "DW_OP_breg10";
303090075Sobrien    case DW_OP_breg11:
303190075Sobrien      return "DW_OP_breg11";
303290075Sobrien    case DW_OP_breg12:
303390075Sobrien      return "DW_OP_breg12";
303490075Sobrien    case DW_OP_breg13:
303590075Sobrien      return "DW_OP_breg13";
303690075Sobrien    case DW_OP_breg14:
303790075Sobrien      return "DW_OP_breg14";
303890075Sobrien    case DW_OP_breg15:
303990075Sobrien      return "DW_OP_breg15";
304090075Sobrien    case DW_OP_breg16:
304190075Sobrien      return "DW_OP_breg16";
304290075Sobrien    case DW_OP_breg17:
304390075Sobrien      return "DW_OP_breg17";
304490075Sobrien    case DW_OP_breg18:
304590075Sobrien      return "DW_OP_breg18";
304690075Sobrien    case DW_OP_breg19:
304790075Sobrien      return "DW_OP_breg19";
304890075Sobrien    case DW_OP_breg20:
304990075Sobrien      return "DW_OP_breg20";
305090075Sobrien    case DW_OP_breg21:
305190075Sobrien      return "DW_OP_breg21";
305290075Sobrien    case DW_OP_breg22:
305390075Sobrien      return "DW_OP_breg22";
305490075Sobrien    case DW_OP_breg23:
305590075Sobrien      return "DW_OP_breg23";
305690075Sobrien    case DW_OP_breg24:
305790075Sobrien      return "DW_OP_breg24";
305890075Sobrien    case DW_OP_breg25:
305990075Sobrien      return "DW_OP_breg25";
306090075Sobrien    case DW_OP_breg26:
306190075Sobrien      return "DW_OP_breg26";
306290075Sobrien    case DW_OP_breg27:
306390075Sobrien      return "DW_OP_breg27";
306490075Sobrien    case DW_OP_breg28:
306590075Sobrien      return "DW_OP_breg28";
306690075Sobrien    case DW_OP_breg29:
306790075Sobrien      return "DW_OP_breg29";
306890075Sobrien    case DW_OP_breg30:
306990075Sobrien      return "DW_OP_breg30";
307090075Sobrien    case DW_OP_breg31:
307190075Sobrien      return "DW_OP_breg31";
307290075Sobrien    case DW_OP_regx:
307390075Sobrien      return "DW_OP_regx";
307490075Sobrien    case DW_OP_fbreg:
307590075Sobrien      return "DW_OP_fbreg";
307690075Sobrien    case DW_OP_bregx:
307790075Sobrien      return "DW_OP_bregx";
307890075Sobrien    case DW_OP_piece:
307990075Sobrien      return "DW_OP_piece";
308090075Sobrien    case DW_OP_deref_size:
308190075Sobrien      return "DW_OP_deref_size";
308290075Sobrien    case DW_OP_xderef_size:
308390075Sobrien      return "DW_OP_xderef_size";
308490075Sobrien    case DW_OP_nop:
308590075Sobrien      return "DW_OP_nop";
3086117395Skan    case DW_OP_push_object_address:
3087117395Skan      return "DW_OP_push_object_address";
3088117395Skan    case DW_OP_call2:
3089117395Skan      return "DW_OP_call2";
3090117395Skan    case DW_OP_call4:
3091117395Skan      return "DW_OP_call4";
3092117395Skan    case DW_OP_call_ref:
3093117395Skan      return "DW_OP_call_ref";
3094117395Skan    case DW_OP_GNU_push_tls_address:
3095117395Skan      return "DW_OP_GNU_push_tls_address";
309690075Sobrien    default:
309790075Sobrien      return "OP_<unknown>";
309890075Sobrien    }
309990075Sobrien}
310090075Sobrien
310190075Sobrien/* Return a pointer to a newly allocated location description.  Location
310290075Sobrien   descriptions are simple expression terms that can be strung
310390075Sobrien   together to form more complicated location (address) descriptions.  */
310490075Sobrien
310590075Sobrienstatic inline dw_loc_descr_ref
3106132718Skannew_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3107132718Skan	       unsigned HOST_WIDE_INT oprnd2)
310890075Sobrien{
3109132718Skan  dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
311090075Sobrien
311190075Sobrien  descr->dw_loc_opc = op;
311290075Sobrien  descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
311390075Sobrien  descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
311490075Sobrien  descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
311590075Sobrien  descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
311690075Sobrien
311790075Sobrien  return descr;
311890075Sobrien}
311990075Sobrien
312090075Sobrien/* Add a location description term to a location description expression.  */
312190075Sobrien
312290075Sobrienstatic inline void
3123132718Skanadd_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
312490075Sobrien{
312590075Sobrien  dw_loc_descr_ref *d;
312690075Sobrien
312790075Sobrien  /* Find the end of the chain.  */
312890075Sobrien  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
312990075Sobrien    ;
313090075Sobrien
313190075Sobrien  *d = descr;
313290075Sobrien}
313390075Sobrien
313490075Sobrien/* Return the size of a location descriptor.  */
313590075Sobrien
313690075Sobrienstatic unsigned long
3137132718Skansize_of_loc_descr (dw_loc_descr_ref loc)
313890075Sobrien{
313990075Sobrien  unsigned long size = 1;
314090075Sobrien
314190075Sobrien  switch (loc->dw_loc_opc)
314290075Sobrien    {
314390075Sobrien    case DW_OP_addr:
3144117395Skan    case INTERNAL_DW_OP_tls_addr:
314590075Sobrien      size += DWARF2_ADDR_SIZE;
314690075Sobrien      break;
314790075Sobrien    case DW_OP_const1u:
314890075Sobrien    case DW_OP_const1s:
314990075Sobrien      size += 1;
315090075Sobrien      break;
315190075Sobrien    case DW_OP_const2u:
315290075Sobrien    case DW_OP_const2s:
315390075Sobrien      size += 2;
315490075Sobrien      break;
315590075Sobrien    case DW_OP_const4u:
315690075Sobrien    case DW_OP_const4s:
315790075Sobrien      size += 4;
315890075Sobrien      break;
315990075Sobrien    case DW_OP_const8u:
316090075Sobrien    case DW_OP_const8s:
316190075Sobrien      size += 8;
316290075Sobrien      break;
316390075Sobrien    case DW_OP_constu:
316490075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
316590075Sobrien      break;
316690075Sobrien    case DW_OP_consts:
316790075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
316890075Sobrien      break;
316990075Sobrien    case DW_OP_pick:
317090075Sobrien      size += 1;
317190075Sobrien      break;
317290075Sobrien    case DW_OP_plus_uconst:
317390075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
317490075Sobrien      break;
317590075Sobrien    case DW_OP_skip:
317690075Sobrien    case DW_OP_bra:
317790075Sobrien      size += 2;
317890075Sobrien      break;
317990075Sobrien    case DW_OP_breg0:
318090075Sobrien    case DW_OP_breg1:
318190075Sobrien    case DW_OP_breg2:
318290075Sobrien    case DW_OP_breg3:
318390075Sobrien    case DW_OP_breg4:
318490075Sobrien    case DW_OP_breg5:
318590075Sobrien    case DW_OP_breg6:
318690075Sobrien    case DW_OP_breg7:
318790075Sobrien    case DW_OP_breg8:
318890075Sobrien    case DW_OP_breg9:
318990075Sobrien    case DW_OP_breg10:
319090075Sobrien    case DW_OP_breg11:
319190075Sobrien    case DW_OP_breg12:
319290075Sobrien    case DW_OP_breg13:
319390075Sobrien    case DW_OP_breg14:
319490075Sobrien    case DW_OP_breg15:
319590075Sobrien    case DW_OP_breg16:
319690075Sobrien    case DW_OP_breg17:
319790075Sobrien    case DW_OP_breg18:
319890075Sobrien    case DW_OP_breg19:
319990075Sobrien    case DW_OP_breg20:
320090075Sobrien    case DW_OP_breg21:
320190075Sobrien    case DW_OP_breg22:
320290075Sobrien    case DW_OP_breg23:
320390075Sobrien    case DW_OP_breg24:
320490075Sobrien    case DW_OP_breg25:
320590075Sobrien    case DW_OP_breg26:
320690075Sobrien    case DW_OP_breg27:
320790075Sobrien    case DW_OP_breg28:
320890075Sobrien    case DW_OP_breg29:
320990075Sobrien    case DW_OP_breg30:
321090075Sobrien    case DW_OP_breg31:
321190075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
321290075Sobrien      break;
321390075Sobrien    case DW_OP_regx:
321490075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
321590075Sobrien      break;
321690075Sobrien    case DW_OP_fbreg:
321790075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
321890075Sobrien      break;
321990075Sobrien    case DW_OP_bregx:
322090075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
322190075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
322290075Sobrien      break;
322390075Sobrien    case DW_OP_piece:
322490075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
322590075Sobrien      break;
322690075Sobrien    case DW_OP_deref_size:
322790075Sobrien    case DW_OP_xderef_size:
322890075Sobrien      size += 1;
322990075Sobrien      break;
3230117395Skan    case DW_OP_call2:
3231117395Skan      size += 2;
3232117395Skan      break;
3233117395Skan    case DW_OP_call4:
3234117395Skan      size += 4;
3235117395Skan      break;
3236117395Skan    case DW_OP_call_ref:
3237117395Skan      size += DWARF2_ADDR_SIZE;
3238117395Skan      break;
323990075Sobrien    default:
324090075Sobrien      break;
324190075Sobrien    }
324290075Sobrien
324390075Sobrien  return size;
324490075Sobrien}
324590075Sobrien
324690075Sobrien/* Return the size of a series of location descriptors.  */
324790075Sobrien
324890075Sobrienstatic unsigned long
3249132718Skansize_of_locs (dw_loc_descr_ref loc)
325090075Sobrien{
3251169689Skan  dw_loc_descr_ref l;
325290075Sobrien  unsigned long size;
325390075Sobrien
3254169689Skan  /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3255169689Skan     field, to avoid writing to a PCH file.  */
3256169689Skan  for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
325790075Sobrien    {
3258169689Skan      if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3259169689Skan	break;
3260169689Skan      size += size_of_loc_descr (l);
326190075Sobrien    }
3262169689Skan  if (! l)
3263169689Skan    return size;
326490075Sobrien
3265169689Skan  for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3266169689Skan    {
3267169689Skan      l->dw_loc_addr = size;
3268169689Skan      size += size_of_loc_descr (l);
3269169689Skan    }
3270169689Skan
327190075Sobrien  return size;
327290075Sobrien}
327390075Sobrien
327490075Sobrien/* Output location description stack opcode's operands (if any).  */
327590075Sobrien
327690075Sobrienstatic void
3277132718Skanoutput_loc_operands (dw_loc_descr_ref loc)
327890075Sobrien{
327990075Sobrien  dw_val_ref val1 = &loc->dw_loc_oprnd1;
328090075Sobrien  dw_val_ref val2 = &loc->dw_loc_oprnd2;
328190075Sobrien
328290075Sobrien  switch (loc->dw_loc_opc)
328390075Sobrien    {
328490075Sobrien#ifdef DWARF2_DEBUGGING_INFO
328590075Sobrien    case DW_OP_addr:
328690075Sobrien      dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
328790075Sobrien      break;
328890075Sobrien    case DW_OP_const2u:
328990075Sobrien    case DW_OP_const2s:
329090075Sobrien      dw2_asm_output_data (2, val1->v.val_int, NULL);
329190075Sobrien      break;
329290075Sobrien    case DW_OP_const4u:
329390075Sobrien    case DW_OP_const4s:
329490075Sobrien      dw2_asm_output_data (4, val1->v.val_int, NULL);
329590075Sobrien      break;
329690075Sobrien    case DW_OP_const8u:
329790075Sobrien    case DW_OP_const8s:
3298169689Skan      gcc_assert (HOST_BITS_PER_LONG >= 64);
329990075Sobrien      dw2_asm_output_data (8, val1->v.val_int, NULL);
330090075Sobrien      break;
330190075Sobrien    case DW_OP_skip:
330290075Sobrien    case DW_OP_bra:
330390075Sobrien      {
330490075Sobrien	int offset;
330590075Sobrien
3306169689Skan	gcc_assert (val1->val_class == dw_val_class_loc);
3307169689Skan	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
330890075Sobrien
330990075Sobrien	dw2_asm_output_data (2, offset, NULL);
331090075Sobrien      }
331190075Sobrien      break;
331290075Sobrien#else
331390075Sobrien    case DW_OP_addr:
331490075Sobrien    case DW_OP_const2u:
331590075Sobrien    case DW_OP_const2s:
331690075Sobrien    case DW_OP_const4u:
331790075Sobrien    case DW_OP_const4s:
331890075Sobrien    case DW_OP_const8u:
331990075Sobrien    case DW_OP_const8s:
332090075Sobrien    case DW_OP_skip:
332190075Sobrien    case DW_OP_bra:
332290075Sobrien      /* We currently don't make any attempt to make sure these are
3323132718Skan	 aligned properly like we do for the main unwind info, so
3324132718Skan	 don't support emitting things larger than a byte if we're
3325132718Skan	 only doing unwinding.  */
3326169689Skan      gcc_unreachable ();
332790075Sobrien#endif
332890075Sobrien    case DW_OP_const1u:
332990075Sobrien    case DW_OP_const1s:
333090075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
333190075Sobrien      break;
333290075Sobrien    case DW_OP_constu:
333390075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
333490075Sobrien      break;
333590075Sobrien    case DW_OP_consts:
333690075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
333790075Sobrien      break;
333890075Sobrien    case DW_OP_pick:
333990075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
334090075Sobrien      break;
334190075Sobrien    case DW_OP_plus_uconst:
334290075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
334390075Sobrien      break;
334490075Sobrien    case DW_OP_breg0:
334590075Sobrien    case DW_OP_breg1:
334690075Sobrien    case DW_OP_breg2:
334790075Sobrien    case DW_OP_breg3:
334890075Sobrien    case DW_OP_breg4:
334990075Sobrien    case DW_OP_breg5:
335090075Sobrien    case DW_OP_breg6:
335190075Sobrien    case DW_OP_breg7:
335290075Sobrien    case DW_OP_breg8:
335390075Sobrien    case DW_OP_breg9:
335490075Sobrien    case DW_OP_breg10:
335590075Sobrien    case DW_OP_breg11:
335690075Sobrien    case DW_OP_breg12:
335790075Sobrien    case DW_OP_breg13:
335890075Sobrien    case DW_OP_breg14:
335990075Sobrien    case DW_OP_breg15:
336090075Sobrien    case DW_OP_breg16:
336190075Sobrien    case DW_OP_breg17:
336290075Sobrien    case DW_OP_breg18:
336390075Sobrien    case DW_OP_breg19:
336490075Sobrien    case DW_OP_breg20:
336590075Sobrien    case DW_OP_breg21:
336690075Sobrien    case DW_OP_breg22:
336790075Sobrien    case DW_OP_breg23:
336890075Sobrien    case DW_OP_breg24:
336990075Sobrien    case DW_OP_breg25:
337090075Sobrien    case DW_OP_breg26:
337190075Sobrien    case DW_OP_breg27:
337290075Sobrien    case DW_OP_breg28:
337390075Sobrien    case DW_OP_breg29:
337490075Sobrien    case DW_OP_breg30:
337590075Sobrien    case DW_OP_breg31:
337690075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
337790075Sobrien      break;
337890075Sobrien    case DW_OP_regx:
337990075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
338090075Sobrien      break;
338190075Sobrien    case DW_OP_fbreg:
338290075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
338390075Sobrien      break;
338490075Sobrien    case DW_OP_bregx:
338590075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
338690075Sobrien      dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
338790075Sobrien      break;
338890075Sobrien    case DW_OP_piece:
338990075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
339090075Sobrien      break;
339190075Sobrien    case DW_OP_deref_size:
339290075Sobrien    case DW_OP_xderef_size:
339390075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
339490075Sobrien      break;
3395117395Skan
3396117395Skan    case INTERNAL_DW_OP_tls_addr:
3397169689Skan      if (targetm.asm_out.output_dwarf_dtprel)
3398169689Skan	{
3399169689Skan	  targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3400169689Skan					       DWARF2_ADDR_SIZE,
3401169689Skan					       val1->v.val_addr);
3402169689Skan	  fputc ('\n', asm_out_file);
3403169689Skan	}
3404169689Skan      else
3405169689Skan	gcc_unreachable ();
3406117395Skan      break;
3407117395Skan
340890075Sobrien    default:
340990075Sobrien      /* Other codes have no operands.  */
341090075Sobrien      break;
341190075Sobrien    }
341290075Sobrien}
341390075Sobrien
341490075Sobrien/* Output a sequence of location operations.  */
341590075Sobrien
341690075Sobrienstatic void
3417132718Skanoutput_loc_sequence (dw_loc_descr_ref loc)
341890075Sobrien{
341990075Sobrien  for (; loc != NULL; loc = loc->dw_loc_next)
342090075Sobrien    {
342190075Sobrien      /* Output the opcode.  */
342290075Sobrien      dw2_asm_output_data (1, loc->dw_loc_opc,
342390075Sobrien			   "%s", dwarf_stack_op_name (loc->dw_loc_opc));
342490075Sobrien
342590075Sobrien      /* Output the operand(s) (if any).  */
342690075Sobrien      output_loc_operands (loc);
342790075Sobrien    }
342890075Sobrien}
342990075Sobrien
343090075Sobrien/* This routine will generate the correct assembly data for a location
343190075Sobrien   description based on a cfi entry with a complex address.  */
343290075Sobrien
343390075Sobrienstatic void
3434132718Skanoutput_cfa_loc (dw_cfi_ref cfi)
343590075Sobrien{
343690075Sobrien  dw_loc_descr_ref loc;
343790075Sobrien  unsigned long size;
343890075Sobrien
343990075Sobrien  /* Output the size of the block.  */
344090075Sobrien  loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
344190075Sobrien  size = size_of_locs (loc);
344290075Sobrien  dw2_asm_output_data_uleb128 (size, NULL);
344390075Sobrien
344490075Sobrien  /* Now output the operations themselves.  */
344590075Sobrien  output_loc_sequence (loc);
344690075Sobrien}
344790075Sobrien
3448169689Skan/* This function builds a dwarf location descriptor sequence from a
3449169689Skan   dw_cfa_location, adding the given OFFSET to the result of the
3450169689Skan   expression.  */
345190075Sobrien
345290075Sobrienstatic struct dw_loc_descr_struct *
3453169689Skanbuild_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
345490075Sobrien{
345590075Sobrien  struct dw_loc_descr_struct *head, *tmp;
345690075Sobrien
3457169689Skan  offset += cfa->offset;
345890075Sobrien
3459169689Skan  if (cfa->indirect)
346090075Sobrien    {
3461169689Skan      if (cfa->base_offset)
3462169689Skan	{
3463169689Skan	  if (cfa->reg <= 31)
3464169689Skan	    head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3465169689Skan	  else
3466169689Skan	    head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3467169689Skan	}
3468169689Skan      else if (cfa->reg <= 31)
3469169689Skan	head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
347090075Sobrien      else
3471169689Skan	head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3472169689Skan
3473169689Skan      head->dw_loc_oprnd1.val_class = dw_val_class_const;
3474169689Skan      tmp = new_loc_descr (DW_OP_deref, 0, 0);
3475169689Skan      add_loc_descr (&head, tmp);
3476169689Skan      if (offset != 0)
3477169689Skan	{
3478169689Skan	  tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3479169689Skan	  add_loc_descr (&head, tmp);
3480169689Skan	}
348190075Sobrien    }
348290075Sobrien  else
348390075Sobrien    {
3484169689Skan      if (offset == 0)
3485169689Skan	if (cfa->reg <= 31)
3486169689Skan	  head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3487169689Skan	else
3488169689Skan	  head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3489169689Skan      else if (cfa->reg <= 31)
3490169689Skan	head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3491169689Skan      else
3492169689Skan	head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
349390075Sobrien    }
349490075Sobrien
349590075Sobrien  return head;
349690075Sobrien}
349790075Sobrien
349890075Sobrien/* This function fills in aa dw_cfa_location structure from a dwarf location
349990075Sobrien   descriptor sequence.  */
350090075Sobrien
350190075Sobrienstatic void
3502132718Skanget_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
350390075Sobrien{
350490075Sobrien  struct dw_loc_descr_struct *ptr;
350590075Sobrien  cfa->offset = 0;
350690075Sobrien  cfa->base_offset = 0;
350790075Sobrien  cfa->indirect = 0;
350890075Sobrien  cfa->reg = -1;
350990075Sobrien
351090075Sobrien  for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
351190075Sobrien    {
351290075Sobrien      enum dwarf_location_atom op = ptr->dw_loc_opc;
351390075Sobrien
351490075Sobrien      switch (op)
351590075Sobrien	{
351690075Sobrien	case DW_OP_reg0:
351790075Sobrien	case DW_OP_reg1:
351890075Sobrien	case DW_OP_reg2:
351990075Sobrien	case DW_OP_reg3:
352090075Sobrien	case DW_OP_reg4:
352190075Sobrien	case DW_OP_reg5:
352290075Sobrien	case DW_OP_reg6:
352390075Sobrien	case DW_OP_reg7:
352490075Sobrien	case DW_OP_reg8:
352590075Sobrien	case DW_OP_reg9:
352690075Sobrien	case DW_OP_reg10:
352790075Sobrien	case DW_OP_reg11:
352890075Sobrien	case DW_OP_reg12:
352990075Sobrien	case DW_OP_reg13:
353090075Sobrien	case DW_OP_reg14:
353190075Sobrien	case DW_OP_reg15:
353290075Sobrien	case DW_OP_reg16:
353390075Sobrien	case DW_OP_reg17:
353490075Sobrien	case DW_OP_reg18:
353590075Sobrien	case DW_OP_reg19:
353690075Sobrien	case DW_OP_reg20:
353790075Sobrien	case DW_OP_reg21:
353890075Sobrien	case DW_OP_reg22:
353990075Sobrien	case DW_OP_reg23:
354090075Sobrien	case DW_OP_reg24:
354190075Sobrien	case DW_OP_reg25:
354290075Sobrien	case DW_OP_reg26:
354390075Sobrien	case DW_OP_reg27:
354490075Sobrien	case DW_OP_reg28:
354590075Sobrien	case DW_OP_reg29:
354690075Sobrien	case DW_OP_reg30:
354790075Sobrien	case DW_OP_reg31:
354890075Sobrien	  cfa->reg = op - DW_OP_reg0;
354990075Sobrien	  break;
355090075Sobrien	case DW_OP_regx:
355190075Sobrien	  cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
355290075Sobrien	  break;
355390075Sobrien	case DW_OP_breg0:
355490075Sobrien	case DW_OP_breg1:
355590075Sobrien	case DW_OP_breg2:
355690075Sobrien	case DW_OP_breg3:
355790075Sobrien	case DW_OP_breg4:
355890075Sobrien	case DW_OP_breg5:
355990075Sobrien	case DW_OP_breg6:
356090075Sobrien	case DW_OP_breg7:
356190075Sobrien	case DW_OP_breg8:
356290075Sobrien	case DW_OP_breg9:
356390075Sobrien	case DW_OP_breg10:
356490075Sobrien	case DW_OP_breg11:
356590075Sobrien	case DW_OP_breg12:
356690075Sobrien	case DW_OP_breg13:
356790075Sobrien	case DW_OP_breg14:
356890075Sobrien	case DW_OP_breg15:
356990075Sobrien	case DW_OP_breg16:
357090075Sobrien	case DW_OP_breg17:
357190075Sobrien	case DW_OP_breg18:
357290075Sobrien	case DW_OP_breg19:
357390075Sobrien	case DW_OP_breg20:
357490075Sobrien	case DW_OP_breg21:
357590075Sobrien	case DW_OP_breg22:
357690075Sobrien	case DW_OP_breg23:
357790075Sobrien	case DW_OP_breg24:
357890075Sobrien	case DW_OP_breg25:
357990075Sobrien	case DW_OP_breg26:
358090075Sobrien	case DW_OP_breg27:
358190075Sobrien	case DW_OP_breg28:
358290075Sobrien	case DW_OP_breg29:
358390075Sobrien	case DW_OP_breg30:
358490075Sobrien	case DW_OP_breg31:
358590075Sobrien	  cfa->reg = op - DW_OP_breg0;
358690075Sobrien	  cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
358790075Sobrien	  break;
358890075Sobrien	case DW_OP_bregx:
358990075Sobrien	  cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
359090075Sobrien	  cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
359190075Sobrien	  break;
359290075Sobrien	case DW_OP_deref:
359390075Sobrien	  cfa->indirect = 1;
359490075Sobrien	  break;
359590075Sobrien	case DW_OP_plus_uconst:
359690075Sobrien	  cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
359790075Sobrien	  break;
359890075Sobrien	default:
3599169689Skan	  internal_error ("DW_LOC_OP %s not implemented",
360090075Sobrien			  dwarf_stack_op_name (ptr->dw_loc_opc));
360190075Sobrien	}
360290075Sobrien    }
360390075Sobrien}
360490075Sobrien#endif /* .debug_frame support */
360590075Sobrien
360690075Sobrien/* And now, the support for symbolic debugging information.  */
360790075Sobrien#ifdef DWARF2_DEBUGGING_INFO
360890075Sobrien
360990075Sobrien/* .debug_str support.  */
3610132718Skanstatic int output_indirect_string (void **, void *);
361190075Sobrien
3612132718Skanstatic void dwarf2out_init (const char *);
3613132718Skanstatic void dwarf2out_finish (const char *);
3614132718Skanstatic void dwarf2out_define (unsigned int, const char *);
3615132718Skanstatic void dwarf2out_undef (unsigned int, const char *);
3616132718Skanstatic void dwarf2out_start_source_file (unsigned, const char *);
3617132718Skanstatic void dwarf2out_end_source_file (unsigned);
3618132718Skanstatic void dwarf2out_begin_block (unsigned, unsigned);
3619132718Skanstatic void dwarf2out_end_block (unsigned, unsigned);
3620132718Skanstatic bool dwarf2out_ignore_block (tree);
3621132718Skanstatic void dwarf2out_global_decl (tree);
3622169689Skanstatic void dwarf2out_type_decl (tree, int);
3623169689Skanstatic void dwarf2out_imported_module_or_decl (tree, tree);
3624132718Skanstatic void dwarf2out_abstract_function (tree);
3625169689Skanstatic void dwarf2out_var_location (rtx);
3626169689Skanstatic void dwarf2out_begin_function (tree);
3627169689Skanstatic void dwarf2out_switch_text_section (void);
362890075Sobrien
362990075Sobrien/* The debug hooks structure.  */
363090075Sobrien
3631117395Skanconst struct gcc_debug_hooks dwarf2_debug_hooks =
363290075Sobrien{
363390075Sobrien  dwarf2out_init,
363490075Sobrien  dwarf2out_finish,
363590075Sobrien  dwarf2out_define,
363690075Sobrien  dwarf2out_undef,
363790075Sobrien  dwarf2out_start_source_file,
363890075Sobrien  dwarf2out_end_source_file,
363990075Sobrien  dwarf2out_begin_block,
364090075Sobrien  dwarf2out_end_block,
364190075Sobrien  dwarf2out_ignore_block,
364290075Sobrien  dwarf2out_source_line,
364390075Sobrien  dwarf2out_begin_prologue,
3644117395Skan  debug_nothing_int_charstar,	/* end_prologue */
364590075Sobrien  dwarf2out_end_epilogue,
3646169689Skan  dwarf2out_begin_function,
364790075Sobrien  debug_nothing_int,		/* end_function */
364890075Sobrien  dwarf2out_decl,		/* function_decl */
364990075Sobrien  dwarf2out_global_decl,
3650169689Skan  dwarf2out_type_decl,		/* type_decl */
3651169689Skan  dwarf2out_imported_module_or_decl,
365290075Sobrien  debug_nothing_tree,		/* deferred_inline_function */
365390075Sobrien  /* The DWARF 2 backend tries to reduce debugging bloat by not
365490075Sobrien     emitting the abstract description of inline functions until
365590075Sobrien     something tries to reference them.  */
365690075Sobrien  dwarf2out_abstract_function,	/* outlining_inline_function */
3657132718Skan  debug_nothing_rtx,		/* label */
3658169689Skan  debug_nothing_int,		/* handle_pch */
3659169689Skan  dwarf2out_var_location,
3660169689Skan  dwarf2out_switch_text_section,
3661169689Skan  1                             /* start_end_main_source_file */
366290075Sobrien};
3663132718Skan#endif
366490075Sobrien
366590075Sobrien/* NOTE: In the comments in this file, many references are made to
366690075Sobrien   "Debugging Information Entries".  This term is abbreviated as `DIE'
366790075Sobrien   throughout the remainder of this file.  */
366890075Sobrien
366990075Sobrien/* An internal representation of the DWARF output is built, and then
367090075Sobrien   walked to generate the DWARF debugging info.  The walk of the internal
367190075Sobrien   representation is done after the entire program has been compiled.
367290075Sobrien   The types below are used to describe the internal representation.  */
367390075Sobrien
367490075Sobrien/* Various DIE's use offsets relative to the beginning of the
367590075Sobrien   .debug_info section to refer to each other.  */
367690075Sobrien
367790075Sobrientypedef long int dw_offset;
367890075Sobrien
367990075Sobrien/* Define typedefs here to avoid circular dependencies.  */
368090075Sobrien
368190075Sobrientypedef struct dw_attr_struct *dw_attr_ref;
368290075Sobrientypedef struct dw_line_info_struct *dw_line_info_ref;
368390075Sobrientypedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
368490075Sobrientypedef struct pubname_struct *pubname_ref;
368590075Sobrientypedef struct dw_ranges_struct *dw_ranges_ref;
368690075Sobrien
368790075Sobrien/* Each entry in the line_info_table maintains the file and
368890075Sobrien   line number associated with the label generated for that
368990075Sobrien   entry.  The label gives the PC value associated with
369090075Sobrien   the line number entry.  */
369190075Sobrien
3692132718Skantypedef struct dw_line_info_struct GTY(())
369390075Sobrien{
369490075Sobrien  unsigned long dw_file_num;
369590075Sobrien  unsigned long dw_line_num;
369690075Sobrien}
369790075Sobriendw_line_info_entry;
369890075Sobrien
369990075Sobrien/* Line information for functions in separate sections; each one gets its
370090075Sobrien   own sequence.  */
3701132718Skantypedef struct dw_separate_line_info_struct GTY(())
370290075Sobrien{
370390075Sobrien  unsigned long dw_file_num;
370490075Sobrien  unsigned long dw_line_num;
370590075Sobrien  unsigned long function;
370690075Sobrien}
370790075Sobriendw_separate_line_info_entry;
370890075Sobrien
370950397Sobrien/* Each DIE attribute has a field specifying the attribute kind,
371050397Sobrien   a link to the next attribute in the chain, and an attribute value.
371150397Sobrien   Attributes are typically linked below the DIE they modify.  */
371250397Sobrien
3713132718Skantypedef struct dw_attr_struct GTY(())
371450397Sobrien{
371550397Sobrien  enum dwarf_attribute dw_attr;
371650397Sobrien  dw_val_node dw_attr_val;
371750397Sobrien}
371850397Sobriendw_attr_node;
371950397Sobrien
3720169689SkanDEF_VEC_O(dw_attr_node);
3721169689SkanDEF_VEC_ALLOC_O(dw_attr_node,gc);
372250397Sobrien
3723169689Skan/* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3724169689Skan   The children of each node form a circular list linked by
3725169689Skan   die_sib.  die_child points to the node *before* the "first" child node.  */
3726169689Skan
3727132718Skantypedef struct die_struct GTY(())
372850397Sobrien{
372950397Sobrien  enum dwarf_tag die_tag;
373090075Sobrien  char *die_symbol;
3731169689Skan  VEC(dw_attr_node,gc) * die_attr;
373250397Sobrien  dw_die_ref die_parent;
373350397Sobrien  dw_die_ref die_child;
373450397Sobrien  dw_die_ref die_sib;
3735132718Skan  dw_die_ref die_definition; /* ref from a specification to its definition */
373650397Sobrien  dw_offset die_offset;
373750397Sobrien  unsigned long die_abbrev;
373890075Sobrien  int die_mark;
3739169689Skan  /* Die is used and must not be pruned as unused.  */
3740169689Skan  int die_perennial_p;
3741169689Skan  unsigned int decl_id;
374250397Sobrien}
374350397Sobriendie_node;
374450397Sobrien
3745169689Skan/* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3746169689Skan#define FOR_EACH_CHILD(die, c, expr) do {	\
3747169689Skan  c = die->die_child;				\
3748169689Skan  if (c) do {					\
3749169689Skan    c = c->die_sib;				\
3750169689Skan    expr;					\
3751169689Skan  } while (c != die->die_child);		\
3752169689Skan} while (0)
3753169689Skan
375450397Sobrien/* The pubname structure */
375550397Sobrien
3756132718Skantypedef struct pubname_struct GTY(())
375750397Sobrien{
375850397Sobrien  dw_die_ref die;
375990075Sobrien  char *name;
376050397Sobrien}
376150397Sobrienpubname_entry;
376250397Sobrien
3763260396SpfgDEF_VEC_O(pubname_entry);
3764260396SpfgDEF_VEC_ALLOC_O(pubname_entry, gc);
3765260396Spfg
3766132718Skanstruct dw_ranges_struct GTY(())
376790075Sobrien{
376890075Sobrien  int block_num;
376990075Sobrien};
377090075Sobrien
377150397Sobrien/* The limbo die list structure.  */
3772132718Skantypedef struct limbo_die_struct GTY(())
377350397Sobrien{
377450397Sobrien  dw_die_ref die;
377590075Sobrien  tree created_for;
377650397Sobrien  struct limbo_die_struct *next;
377750397Sobrien}
377850397Sobrienlimbo_die_node;
377950397Sobrien
378050397Sobrien/* How to start an assembler comment.  */
378150397Sobrien#ifndef ASM_COMMENT_START
378250397Sobrien#define ASM_COMMENT_START ";#"
378350397Sobrien#endif
378450397Sobrien
3785117395Skan/* Define a macro which returns nonzero for a TYPE_DECL which was
378650397Sobrien   implicitly generated for a tagged type.
378750397Sobrien
378850397Sobrien   Note that unlike the gcc front end (which generates a NULL named
378950397Sobrien   TYPE_DECL node for each complete tagged type, each array type, and
379050397Sobrien   each function type node created) the g++ front end generates a
379150397Sobrien   _named_ TYPE_DECL node for each tagged type node created.
379250397Sobrien   These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
379350397Sobrien   generate a DW_TAG_typedef DIE for them.  */
379450397Sobrien
379550397Sobrien#define TYPE_DECL_IS_STUB(decl)				\
379650397Sobrien  (DECL_NAME (decl) == NULL_TREE			\
379750397Sobrien   || (DECL_ARTIFICIAL (decl)				\
379850397Sobrien       && is_tagged_type (TREE_TYPE (decl))		\
379950397Sobrien       && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))	\
380050397Sobrien	   /* This is necessary for stub decls that	\
380150397Sobrien	      appear in nested inline functions.  */	\
380250397Sobrien	   || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE	\
380350397Sobrien	       && (decl_ultimate_origin (decl)		\
380450397Sobrien		   == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
380550397Sobrien
380650397Sobrien/* Information concerning the compilation unit's programming
380750397Sobrien   language, and compiler version.  */
380850397Sobrien
380950397Sobrien/* Fixed size portion of the DWARF compilation unit header.  */
3810132718Skan#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3811132718Skan  (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
381250397Sobrien
381350397Sobrien/* Fixed size portion of public names info.  */
381450397Sobrien#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
381550397Sobrien
381650397Sobrien/* Fixed size portion of the address range info.  */
381790075Sobrien#define DWARF_ARANGES_HEADER_SIZE					\
3818132718Skan  (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,	\
3819132718Skan                DWARF2_ADDR_SIZE * 2)					\
3820132718Skan   - DWARF_INITIAL_LENGTH_SIZE)
382150397Sobrien
382290075Sobrien/* Size of padding portion in the address range info.  It must be
382390075Sobrien   aligned to twice the pointer size.  */
382490075Sobrien#define DWARF_ARANGES_PAD_SIZE \
3825132718Skan  (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3826132718Skan                DWARF2_ADDR_SIZE * 2) \
3827132718Skan   - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
382890075Sobrien
382990075Sobrien/* Use assembler line directives if available.  */
383090075Sobrien#ifndef DWARF2_ASM_LINE_DEBUG_INFO
383190075Sobrien#ifdef HAVE_AS_DWARF2_DEBUG_LINE
383290075Sobrien#define DWARF2_ASM_LINE_DEBUG_INFO 1
383390075Sobrien#else
383490075Sobrien#define DWARF2_ASM_LINE_DEBUG_INFO 0
383590075Sobrien#endif
383690075Sobrien#endif
383790075Sobrien
383850397Sobrien/* Minimum line offset in a special line info. opcode.
383950397Sobrien   This value was chosen to give a reasonable range of values.  */
384050397Sobrien#define DWARF_LINE_BASE  -10
384150397Sobrien
384290075Sobrien/* First special line opcode - leave room for the standard opcodes.  */
384350397Sobrien#define DWARF_LINE_OPCODE_BASE  10
384450397Sobrien
384550397Sobrien/* Range of line offsets in a special line info. opcode.  */
384650397Sobrien#define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
384750397Sobrien
384850397Sobrien/* Flag that indicates the initial value of the is_stmt_start flag.
384950397Sobrien   In the present implementation, we do not mark any lines as
385050397Sobrien   the beginning of a source statement, because that information
385150397Sobrien   is not made available by the GCC front-end.  */
385250397Sobrien#define	DWARF_LINE_DEFAULT_IS_STMT_START 1
385350397Sobrien
3854132718Skan#ifdef DWARF2_DEBUGGING_INFO
385550397Sobrien/* This location is used by calc_die_sizes() to keep track
385650397Sobrien   the offset of each DIE within the .debug_info section.  */
385750397Sobrienstatic unsigned long next_die_offset;
3858132718Skan#endif
385950397Sobrien
386050397Sobrien/* Record the root of the DIE's built for the current compilation unit.  */
3861132718Skanstatic GTY(()) dw_die_ref comp_unit_die;
386250397Sobrien
386350397Sobrien/* A list of DIEs with a NULL parent waiting to be relocated.  */
3864132718Skanstatic GTY(()) limbo_die_node *limbo_die_list;
386550397Sobrien
386690075Sobrien/* Filenames referenced by this compilation unit.  */
3867169689Skanstatic GTY((param_is (struct dwarf_file_data))) htab_t file_table;
386890075Sobrien
3869169689Skan/* A hash table of references to DIE's that describe declarations.
3870169689Skan   The key is a DECL_UID() which is a unique number identifying each decl.  */
3871169689Skanstatic GTY ((param_is (struct die_struct))) htab_t decl_die_table;
387250397Sobrien
3873169689Skan/* Node of the variable location list.  */
3874169689Skanstruct var_loc_node GTY ((chain_next ("%h.next")))
3875169689Skan{
3876169689Skan  rtx GTY (()) var_loc_note;
3877169689Skan  const char * GTY (()) label;
3878169689Skan  const char * GTY (()) section_label;
3879169689Skan  struct var_loc_node * GTY (()) next;
3880169689Skan};
388150397Sobrien
3882169689Skan/* Variable location list.  */
3883169689Skanstruct var_loc_list_def GTY (())
3884169689Skan{
3885169689Skan  struct var_loc_node * GTY (()) first;
388650397Sobrien
3887169689Skan  /* Do not mark the last element of the chained list because
3888169689Skan     it is marked through the chain.  */
3889169689Skan  struct var_loc_node * GTY ((skip ("%h"))) last;
389050397Sobrien
3891169689Skan  /* DECL_UID of the variable decl.  */
3892169689Skan  unsigned int decl_id;
3893169689Skan};
3894169689Skantypedef struct var_loc_list_def var_loc_list;
3895169689Skan
3896169689Skan
3897169689Skan/* Table of decl location linked lists.  */
3898169689Skanstatic GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3899169689Skan
390050397Sobrien/* A pointer to the base of a list of references to DIE's that
390150397Sobrien   are uniquely identified by their tag, presence/absence of
390250397Sobrien   children DIE's, and list of attribute/value pairs.  */
3903132718Skanstatic GTY((length ("abbrev_die_table_allocated")))
3904132718Skan  dw_die_ref *abbrev_die_table;
390550397Sobrien
390650397Sobrien/* Number of elements currently allocated for abbrev_die_table.  */
3907132718Skanstatic GTY(()) unsigned abbrev_die_table_allocated;
390850397Sobrien
390950397Sobrien/* Number of elements in type_die_table currently in use.  */
3910132718Skanstatic GTY(()) unsigned abbrev_die_table_in_use;
391150397Sobrien
391250397Sobrien/* Size (in elements) of increments by which we may expand the
391350397Sobrien   abbrev_die_table.  */
391450397Sobrien#define ABBREV_DIE_TABLE_INCREMENT 256
391550397Sobrien
391650397Sobrien/* A pointer to the base of a table that contains line information
391750397Sobrien   for each source code line in .text in the compilation unit.  */
3918132718Skanstatic GTY((length ("line_info_table_allocated")))
3919132718Skan     dw_line_info_ref line_info_table;
392050397Sobrien
392150397Sobrien/* Number of elements currently allocated for line_info_table.  */
3922132718Skanstatic GTY(()) unsigned line_info_table_allocated;
392350397Sobrien
3924132718Skan/* Number of elements in line_info_table currently in use.  */
3925132718Skanstatic GTY(()) unsigned line_info_table_in_use;
392650397Sobrien
3927169689Skan/* True if the compilation unit places functions in more than one section.  */
3928169689Skanstatic GTY(()) bool have_multiple_function_sections = false;
3929169689Skan
393050397Sobrien/* A pointer to the base of a table that contains line information
393150397Sobrien   for each source code line outside of .text in the compilation unit.  */
3932132718Skanstatic GTY ((length ("separate_line_info_table_allocated")))
3933132718Skan     dw_separate_line_info_ref separate_line_info_table;
393450397Sobrien
393550397Sobrien/* Number of elements currently allocated for separate_line_info_table.  */
3936132718Skanstatic GTY(()) unsigned separate_line_info_table_allocated;
393750397Sobrien
3938132718Skan/* Number of elements in separate_line_info_table currently in use.  */
3939132718Skanstatic GTY(()) unsigned separate_line_info_table_in_use;
394050397Sobrien
394150397Sobrien/* Size (in elements) of increments by which we may expand the
394250397Sobrien   line_info_table.  */
394350397Sobrien#define LINE_INFO_TABLE_INCREMENT 1024
394450397Sobrien
394550397Sobrien/* A pointer to the base of a table that contains a list of publicly
394650397Sobrien   accessible names.  */
3947260396Spfgstatic GTY (()) VEC (pubname_entry, gc) *  pubname_table;
394850397Sobrien
3949260396Spfg/* A pointer to the base of a table that contains a list of publicly
3950260396Spfg   accessible types.  */
3951260396Spfgstatic GTY (()) VEC (pubname_entry, gc) * pubtype_table;
395250397Sobrien
395390075Sobrien/* Array of dies for which we should generate .debug_arange info.  */
3954132718Skanstatic GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
395550397Sobrien
395650397Sobrien/* Number of elements currently allocated for arange_table.  */
3957132718Skanstatic GTY(()) unsigned arange_table_allocated;
395850397Sobrien
395950397Sobrien/* Number of elements in arange_table currently in use.  */
3960132718Skanstatic GTY(()) unsigned arange_table_in_use;
396150397Sobrien
396250397Sobrien/* Size (in elements) of increments by which we may expand the
396350397Sobrien   arange_table.  */
396450397Sobrien#define ARANGE_TABLE_INCREMENT 64
396550397Sobrien
396690075Sobrien/* Array of dies for which we should generate .debug_ranges info.  */
3967132718Skanstatic GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
396850397Sobrien
396990075Sobrien/* Number of elements currently allocated for ranges_table.  */
3970132718Skanstatic GTY(()) unsigned ranges_table_allocated;
397150397Sobrien
397290075Sobrien/* Number of elements in ranges_table currently in use.  */
3973132718Skanstatic GTY(()) unsigned ranges_table_in_use;
397450397Sobrien
397590075Sobrien/* Size (in elements) of increments by which we may expand the
397690075Sobrien   ranges_table.  */
397790075Sobrien#define RANGES_TABLE_INCREMENT 64
397850397Sobrien
397990075Sobrien/* Whether we have location lists that need outputting */
3980169689Skanstatic GTY(()) bool have_location_lists;
398150397Sobrien
3982169689Skan/* Unique label counter.  */
3983169689Skanstatic GTY(()) unsigned int loclabel_num;
3984169689Skan
3985132718Skan#ifdef DWARF2_DEBUGGING_INFO
398650397Sobrien/* Record whether the function being analyzed contains inlined functions.  */
398750397Sobrienstatic int current_function_has_inlines;
3988132718Skan#endif
398950397Sobrien#if 0 && defined (MIPS_DEBUGGING_INFO)
399050397Sobrienstatic int comp_unit_has_inlines;
399150397Sobrien#endif
399250397Sobrien
3993169689Skan/* The last file entry emitted by maybe_emit_file().  */
3994169689Skanstatic GTY(()) struct dwarf_file_data * last_emitted_file;
3995132718Skan
3996132718Skan/* Number of internal labels generated by gen_internal_sym().  */
3997132718Skanstatic GTY(()) int label_num;
3998132718Skan
3999169689Skan/* Cached result of previous call to lookup_filename.  */
4000169689Skanstatic GTY(()) struct dwarf_file_data * file_table_last_lookup;
4001169689Skan
4002132718Skan#ifdef DWARF2_DEBUGGING_INFO
4003132718Skan
4004169689Skan/* Offset from the "steady-state frame pointer" to the frame base,
4005169689Skan   within the current function.  */
4006169689Skanstatic HOST_WIDE_INT frame_pointer_fb_offset;
4007169689Skan
400850397Sobrien/* Forward declarations for functions defined in this file.  */
400950397Sobrien
4010132718Skanstatic int is_pseudo_reg (rtx);
4011132718Skanstatic tree type_main_variant (tree);
4012132718Skanstatic int is_tagged_type (tree);
4013132718Skanstatic const char *dwarf_tag_name (unsigned);
4014132718Skanstatic const char *dwarf_attr_name (unsigned);
4015132718Skanstatic const char *dwarf_form_name (unsigned);
4016132718Skanstatic tree decl_ultimate_origin (tree);
4017132718Skanstatic tree block_ultimate_origin (tree);
4018132718Skanstatic tree decl_class_context (tree);
4019132718Skanstatic void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4020132718Skanstatic inline enum dw_val_class AT_class (dw_attr_ref);
4021132718Skanstatic void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4022132718Skanstatic inline unsigned AT_flag (dw_attr_ref);
4023132718Skanstatic void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4024132718Skanstatic inline HOST_WIDE_INT AT_int (dw_attr_ref);
4025132718Skanstatic void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4026132718Skanstatic inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4027132718Skanstatic void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4028132718Skan			      unsigned long);
4029132718Skanstatic inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4030132718Skan			       unsigned int, unsigned char *);
4031132718Skanstatic hashval_t debug_str_do_hash (const void *);
4032132718Skanstatic int debug_str_eq (const void *, const void *);
4033132718Skanstatic void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4034132718Skanstatic inline const char *AT_string (dw_attr_ref);
4035132718Skanstatic int AT_string_form (dw_attr_ref);
4036132718Skanstatic void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4037132718Skanstatic void add_AT_specification (dw_die_ref, dw_die_ref);
4038132718Skanstatic inline dw_die_ref AT_ref (dw_attr_ref);
4039132718Skanstatic inline int AT_ref_external (dw_attr_ref);
4040132718Skanstatic inline void set_AT_ref_external (dw_attr_ref, int);
4041132718Skanstatic void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4042132718Skanstatic void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4043132718Skanstatic inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4044132718Skanstatic void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4045132718Skan			     dw_loc_list_ref);
4046132718Skanstatic inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4047132718Skanstatic void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4048132718Skanstatic inline rtx AT_addr (dw_attr_ref);
4049132718Skanstatic void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4050169689Skanstatic void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4051169689Skanstatic void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4052132718Skanstatic void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4053132718Skan			   unsigned HOST_WIDE_INT);
4054132718Skanstatic void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4055132718Skan			       unsigned long);
4056132718Skanstatic inline const char *AT_lbl (dw_attr_ref);
4057132718Skanstatic dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4058132718Skanstatic const char *get_AT_low_pc (dw_die_ref);
4059132718Skanstatic const char *get_AT_hi_pc (dw_die_ref);
4060132718Skanstatic const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4061132718Skanstatic int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4062132718Skanstatic unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4063132718Skanstatic inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4064132718Skanstatic bool is_c_family (void);
4065132718Skanstatic bool is_cxx (void);
4066132718Skanstatic bool is_java (void);
4067132718Skanstatic bool is_fortran (void);
4068132718Skanstatic bool is_ada (void);
4069132718Skanstatic void remove_AT (dw_die_ref, enum dwarf_attribute);
4070146895Skanstatic void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4071132718Skanstatic void add_child_die (dw_die_ref, dw_die_ref);
4072132718Skanstatic dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4073132718Skanstatic dw_die_ref lookup_type_die (tree);
4074132718Skanstatic void equate_type_number_to_die (tree, dw_die_ref);
4075169689Skanstatic hashval_t decl_die_table_hash (const void *);
4076169689Skanstatic int decl_die_table_eq (const void *, const void *);
4077132718Skanstatic dw_die_ref lookup_decl_die (tree);
4078169689Skanstatic hashval_t decl_loc_table_hash (const void *);
4079169689Skanstatic int decl_loc_table_eq (const void *, const void *);
4080169689Skanstatic var_loc_list *lookup_decl_loc (tree);
4081132718Skanstatic void equate_decl_number_to_die (tree, dw_die_ref);
4082169689Skanstatic void add_var_loc_to_decl (tree, struct var_loc_node *);
4083132718Skanstatic void print_spaces (FILE *);
4084132718Skanstatic void print_die (dw_die_ref, FILE *);
4085132718Skanstatic void print_dwarf_line_table (FILE *);
4086132718Skanstatic dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4087132718Skanstatic dw_die_ref pop_compile_unit (dw_die_ref);
4088132718Skanstatic void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4089132718Skanstatic void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4090132718Skanstatic void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4091132718Skanstatic int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4092132718Skanstatic int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4093132718Skanstatic int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4094132718Skanstatic int same_die_p (dw_die_ref, dw_die_ref, int *);
4095132718Skanstatic int same_die_p_wrap (dw_die_ref, dw_die_ref);
4096132718Skanstatic void compute_section_prefix (dw_die_ref);
4097132718Skanstatic int is_type_die (dw_die_ref);
4098132718Skanstatic int is_comdat_die (dw_die_ref);
4099132718Skanstatic int is_symbol_die (dw_die_ref);
4100132718Skanstatic void assign_symbol_names (dw_die_ref);
4101132718Skanstatic void break_out_includes (dw_die_ref);
4102132718Skanstatic hashval_t htab_cu_hash (const void *);
4103132718Skanstatic int htab_cu_eq (const void *, const void *);
4104132718Skanstatic void htab_cu_del (void *);
4105132718Skanstatic int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4106132718Skanstatic void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4107132718Skanstatic void add_sibling_attributes (dw_die_ref);
4108132718Skanstatic void build_abbrev_table (dw_die_ref);
4109132718Skanstatic void output_location_lists (dw_die_ref);
4110132718Skanstatic int constant_size (long unsigned);
4111132718Skanstatic unsigned long size_of_die (dw_die_ref);
4112132718Skanstatic void calc_die_sizes (dw_die_ref);
4113132718Skanstatic void mark_dies (dw_die_ref);
4114132718Skanstatic void unmark_dies (dw_die_ref);
4115132718Skanstatic void unmark_all_dies (dw_die_ref);
4116260396Spfgstatic unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4117132718Skanstatic unsigned long size_of_aranges (void);
4118132718Skanstatic enum dwarf_form value_format (dw_attr_ref);
4119132718Skanstatic void output_value_format (dw_attr_ref);
4120132718Skanstatic void output_abbrev_section (void);
4121132718Skanstatic void output_die_symbol (dw_die_ref);
4122132718Skanstatic void output_die (dw_die_ref);
4123132718Skanstatic void output_compilation_unit_header (void);
4124132718Skanstatic void output_comp_unit (dw_die_ref, int);
4125132718Skanstatic const char *dwarf2_name (tree, int);
4126132718Skanstatic void add_pubname (tree, dw_die_ref);
4127260396Spfgstatic void add_pubtype (tree, dw_die_ref);
4128260396Spfgstatic void output_pubnames (VEC (pubname_entry,gc) *);
4129132718Skanstatic void add_arange (tree, dw_die_ref);
4130132718Skanstatic void output_aranges (void);
4131132718Skanstatic unsigned int add_ranges (tree);
4132132718Skanstatic void output_ranges (void);
4133132718Skanstatic void output_line_info (void);
4134132718Skanstatic void output_file_names (void);
4135132718Skanstatic dw_die_ref base_type_die (tree);
4136132718Skanstatic tree root_type (tree);
4137132718Skanstatic int is_base_type (tree);
4138132718Skanstatic bool is_subrange_type (tree);
4139132718Skanstatic dw_die_ref subrange_type_die (tree, dw_die_ref);
4140132718Skanstatic dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4141132718Skanstatic int type_is_enum (tree);
4142132718Skanstatic unsigned int dbx_reg_number (rtx);
4143169689Skanstatic void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4144132718Skanstatic dw_loc_descr_ref reg_loc_descriptor (rtx);
4145132718Skanstatic dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4146132718Skanstatic dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4147132718Skanstatic dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4148169689Skanstatic dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4149132718Skanstatic int is_based_loc (rtx);
4150132718Skanstatic dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4151132718Skanstatic dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4152132718Skanstatic dw_loc_descr_ref loc_descriptor (rtx);
4153169689Skanstatic dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4154169689Skanstatic dw_loc_descr_ref loc_descriptor_from_tree (tree);
4155132718Skanstatic HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4156132718Skanstatic tree field_type (tree);
4157132718Skanstatic unsigned int simple_type_align_in_bits (tree);
4158132718Skanstatic unsigned int simple_decl_align_in_bits (tree);
4159132718Skanstatic unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4160132718Skanstatic HOST_WIDE_INT field_byte_offset (tree);
4161132718Skanstatic void add_AT_location_description	(dw_die_ref, enum dwarf_attribute,
4162132718Skan					 dw_loc_descr_ref);
4163132718Skanstatic void add_data_member_location_attribute (dw_die_ref, tree);
4164132718Skanstatic void add_const_value_attribute (dw_die_ref, rtx);
4165132718Skanstatic void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4166132718Skanstatic HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4167132718Skanstatic void insert_float (rtx, unsigned char *);
4168132718Skanstatic rtx rtl_for_decl_location (tree);
4169169689Skanstatic void add_location_or_const_value_attribute (dw_die_ref, tree,
4170169689Skan						   enum dwarf_attribute);
4171132718Skanstatic void tree_add_const_value_attribute (dw_die_ref, tree);
4172132718Skanstatic void add_name_attribute (dw_die_ref, const char *);
4173132718Skanstatic void add_comp_dir_attribute (dw_die_ref);
4174132718Skanstatic void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4175132718Skanstatic void add_subscript_info (dw_die_ref, tree);
4176132718Skanstatic void add_byte_size_attribute (dw_die_ref, tree);
4177132718Skanstatic void add_bit_offset_attribute (dw_die_ref, tree);
4178132718Skanstatic void add_bit_size_attribute (dw_die_ref, tree);
4179132718Skanstatic void add_prototyped_attribute (dw_die_ref, tree);
4180132718Skanstatic void add_abstract_origin_attribute (dw_die_ref, tree);
4181132718Skanstatic void add_pure_or_virtual_attribute (dw_die_ref, tree);
4182132718Skanstatic void add_src_coords_attributes (dw_die_ref, tree);
4183132718Skanstatic void add_name_and_src_coords_attributes (dw_die_ref, tree);
4184132718Skanstatic void push_decl_scope (tree);
4185132718Skanstatic void pop_decl_scope (void);
4186132718Skanstatic dw_die_ref scope_die_for (tree, dw_die_ref);
4187132718Skanstatic inline int local_scope_p (dw_die_ref);
4188132718Skanstatic inline int class_or_namespace_scope_p (dw_die_ref);
4189132718Skanstatic void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4190169689Skanstatic void add_calling_convention_attribute (dw_die_ref, tree);
4191132718Skanstatic const char *type_tag (tree);
4192132718Skanstatic tree member_declared_type (tree);
419350397Sobrien#if 0
4194132718Skanstatic const char *decl_start_label (tree);
419550397Sobrien#endif
4196132718Skanstatic void gen_array_type_die (tree, dw_die_ref);
419750397Sobrien#if 0
4198132718Skanstatic void gen_entry_point_die (tree, dw_die_ref);
419950397Sobrien#endif
4200132718Skanstatic void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4201132718Skanstatic void gen_inlined_structure_type_die (tree, dw_die_ref);
4202132718Skanstatic void gen_inlined_union_type_die (tree, dw_die_ref);
4203132718Skanstatic dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4204132718Skanstatic dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4205132718Skanstatic void gen_unspecified_parameters_die (tree, dw_die_ref);
4206132718Skanstatic void gen_formal_types_die (tree, dw_die_ref);
4207132718Skanstatic void gen_subprogram_die (tree, dw_die_ref);
4208132718Skanstatic void gen_variable_die (tree, dw_die_ref);
4209132718Skanstatic void gen_label_die (tree, dw_die_ref);
4210132718Skanstatic void gen_lexical_block_die (tree, dw_die_ref, int);
4211132718Skanstatic void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4212132718Skanstatic void gen_field_die (tree, dw_die_ref);
4213132718Skanstatic void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4214132718Skanstatic dw_die_ref gen_compile_unit_die (const char *);
4215132718Skanstatic void gen_inheritance_die (tree, tree, dw_die_ref);
4216132718Skanstatic void gen_member_die (tree, dw_die_ref);
4217259269Spfgstatic void gen_struct_or_union_type_die (tree, dw_die_ref,
4218259269Spfg						enum debug_info_usage);
4219132718Skanstatic void gen_subroutine_type_die (tree, dw_die_ref);
4220132718Skanstatic void gen_typedef_die (tree, dw_die_ref);
4221132718Skanstatic void gen_type_die (tree, dw_die_ref);
4222132718Skanstatic void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4223132718Skanstatic void gen_block_die (tree, dw_die_ref, int);
4224132718Skanstatic void decls_for_scope (tree, dw_die_ref, int);
4225132718Skanstatic int is_redundant_typedef (tree);
4226132718Skanstatic void gen_namespace_die (tree);
4227132718Skanstatic void gen_decl_die (tree, dw_die_ref);
4228169689Skanstatic dw_die_ref force_decl_die (tree);
4229169689Skanstatic dw_die_ref force_type_die (tree);
4230132718Skanstatic dw_die_ref setup_namespace_context (tree, dw_die_ref);
4231132718Skanstatic void declare_in_namespace (tree, dw_die_ref);
4232169689Skanstatic struct dwarf_file_data * lookup_filename (const char *);
4233132718Skanstatic void retry_incomplete_types (void);
4234132718Skanstatic void gen_type_die_for_member (tree, tree, dw_die_ref);
4235132718Skanstatic void splice_child_die (dw_die_ref, dw_die_ref);
4236132718Skanstatic int file_info_cmp (const void *, const void *);
4237132718Skanstatic dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4238132718Skan				     const char *, const char *, unsigned);
4239132718Skanstatic void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4240132718Skan				       const char *, const char *,
4241132718Skan				       const char *);
4242132718Skanstatic void output_loc_list (dw_loc_list_ref);
4243132718Skanstatic char *gen_internal_sym (const char *);
424450397Sobrien
4245132718Skanstatic void prune_unmark_dies (dw_die_ref);
4246132718Skanstatic void prune_unused_types_mark (dw_die_ref, int);
4247132718Skanstatic void prune_unused_types_walk (dw_die_ref);
4248132718Skanstatic void prune_unused_types_walk_attribs (dw_die_ref);
4249132718Skanstatic void prune_unused_types_prune (dw_die_ref);
4250132718Skanstatic void prune_unused_types (void);
4251169689Skanstatic int maybe_emit_file (struct dwarf_file_data *fd);
4252132718Skan
425350397Sobrien/* Section names used to hold DWARF debugging information.  */
425450397Sobrien#ifndef DEBUG_INFO_SECTION
425550397Sobrien#define DEBUG_INFO_SECTION	".debug_info"
425650397Sobrien#endif
425790075Sobrien#ifndef DEBUG_ABBREV_SECTION
425890075Sobrien#define DEBUG_ABBREV_SECTION	".debug_abbrev"
425950397Sobrien#endif
426090075Sobrien#ifndef DEBUG_ARANGES_SECTION
426190075Sobrien#define DEBUG_ARANGES_SECTION	".debug_aranges"
426250397Sobrien#endif
426390075Sobrien#ifndef DEBUG_MACINFO_SECTION
426490075Sobrien#define DEBUG_MACINFO_SECTION	".debug_macinfo"
426550397Sobrien#endif
426650397Sobrien#ifndef DEBUG_LINE_SECTION
426750397Sobrien#define DEBUG_LINE_SECTION	".debug_line"
426850397Sobrien#endif
426990075Sobrien#ifndef DEBUG_LOC_SECTION
427090075Sobrien#define DEBUG_LOC_SECTION	".debug_loc"
427150397Sobrien#endif
427290075Sobrien#ifndef DEBUG_PUBNAMES_SECTION
427390075Sobrien#define DEBUG_PUBNAMES_SECTION	".debug_pubnames"
427450397Sobrien#endif
427590075Sobrien#ifndef DEBUG_STR_SECTION
427690075Sobrien#define DEBUG_STR_SECTION	".debug_str"
427750397Sobrien#endif
427890075Sobrien#ifndef DEBUG_RANGES_SECTION
427990075Sobrien#define DEBUG_RANGES_SECTION	".debug_ranges"
428090075Sobrien#endif
428150397Sobrien
428250397Sobrien/* Standard ELF section names for compiled code and data.  */
428390075Sobrien#ifndef TEXT_SECTION_NAME
428490075Sobrien#define TEXT_SECTION_NAME	".text"
428550397Sobrien#endif
428690075Sobrien
428790075Sobrien/* Section flags for .debug_str section.  */
428890075Sobrien#define DEBUG_STR_SECTION_FLAGS \
4289132718Skan  (HAVE_GAS_SHF_MERGE && flag_merge_constants			\
4290132718Skan   ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1	\
4291132718Skan   : SECTION_DEBUG)
429250397Sobrien
429352284Sobrien/* Labels we insert at beginning sections we can reference instead of
429490075Sobrien   the section names themselves.  */
429550397Sobrien
429652284Sobrien#ifndef TEXT_SECTION_LABEL
429790075Sobrien#define TEXT_SECTION_LABEL		"Ltext"
429852284Sobrien#endif
4299169689Skan#ifndef COLD_TEXT_SECTION_LABEL
4300169689Skan#define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4301169689Skan#endif
430252284Sobrien#ifndef DEBUG_LINE_SECTION_LABEL
430390075Sobrien#define DEBUG_LINE_SECTION_LABEL	"Ldebug_line"
430452284Sobrien#endif
430552284Sobrien#ifndef DEBUG_INFO_SECTION_LABEL
430690075Sobrien#define DEBUG_INFO_SECTION_LABEL	"Ldebug_info"
430752284Sobrien#endif
430890075Sobrien#ifndef DEBUG_ABBREV_SECTION_LABEL
430990075Sobrien#define DEBUG_ABBREV_SECTION_LABEL	"Ldebug_abbrev"
431052284Sobrien#endif
431190075Sobrien#ifndef DEBUG_LOC_SECTION_LABEL
431290075Sobrien#define DEBUG_LOC_SECTION_LABEL		"Ldebug_loc"
431390075Sobrien#endif
431490075Sobrien#ifndef DEBUG_RANGES_SECTION_LABEL
431590075Sobrien#define DEBUG_RANGES_SECTION_LABEL	"Ldebug_ranges"
431690075Sobrien#endif
431790075Sobrien#ifndef DEBUG_MACINFO_SECTION_LABEL
431890075Sobrien#define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
431990075Sobrien#endif
432052284Sobrien
432150397Sobrien/* Definitions of defaults for formats and names of various special
432250397Sobrien   (artificial) labels which may be generated within this file (when the -g
4323132718Skan   options is used and DWARF2_DEBUGGING_INFO is in effect.
432450397Sobrien   If necessary, these may be overridden from within the tm.h file, but
432550397Sobrien   typically, overriding these defaults is unnecessary.  */
432650397Sobrien
432750397Sobrienstatic char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
432852284Sobrienstatic char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4329169689Skanstatic char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4330169689Skanstatic char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
433152284Sobrienstatic char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433252284Sobrienstatic char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433352284Sobrienstatic char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433490075Sobrienstatic char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433590075Sobrienstatic char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433690075Sobrienstatic char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
433750397Sobrien
433850397Sobrien#ifndef TEXT_END_LABEL
433950397Sobrien#define TEXT_END_LABEL		"Letext"
434050397Sobrien#endif
4341169689Skan#ifndef COLD_END_LABEL
4342169689Skan#define COLD_END_LABEL          "Letext_cold"
4343169689Skan#endif
434450397Sobrien#ifndef BLOCK_BEGIN_LABEL
434550397Sobrien#define BLOCK_BEGIN_LABEL	"LBB"
434650397Sobrien#endif
434750397Sobrien#ifndef BLOCK_END_LABEL
434850397Sobrien#define BLOCK_END_LABEL		"LBE"
434950397Sobrien#endif
435050397Sobrien#ifndef LINE_CODE_LABEL
435150397Sobrien#define LINE_CODE_LABEL		"LM"
435250397Sobrien#endif
435350397Sobrien#ifndef SEPARATE_LINE_CODE_LABEL
435450397Sobrien#define SEPARATE_LINE_CODE_LABEL	"LSM"
435550397Sobrien#endif
435650397Sobrien
435790075Sobrien/* We allow a language front-end to designate a function that is to be
4358169689Skan   called to "demangle" any name before it is put into a DIE.  */
435950397Sobrien
4360132718Skanstatic const char *(*demangle_name_func) (const char *);
436150397Sobrien
436290075Sobrienvoid
4363132718Skandwarf2out_set_demangle_name_func (const char *(*func) (const char *))
436450397Sobrien{
436590075Sobrien  demangle_name_func = func;
436650397Sobrien}
436750397Sobrien
436850397Sobrien/* Test if rtl node points to a pseudo register.  */
436950397Sobrien
437050397Sobrienstatic inline int
4371132718Skanis_pseudo_reg (rtx rtl)
437250397Sobrien{
4373169689Skan  return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
437490075Sobrien	  || (GET_CODE (rtl) == SUBREG
437590075Sobrien	      && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
437650397Sobrien}
437750397Sobrien
437850397Sobrien/* Return a reference to a type, with its const and volatile qualifiers
437950397Sobrien   removed.  */
438050397Sobrien
438150397Sobrienstatic inline tree
4382132718Skantype_main_variant (tree type)
438350397Sobrien{
438450397Sobrien  type = TYPE_MAIN_VARIANT (type);
438550397Sobrien
438690075Sobrien  /* ??? There really should be only one main variant among any group of
438790075Sobrien     variants of a given type (and all of the MAIN_VARIANT values for all
438890075Sobrien     members of the group should point to that one type) but sometimes the C
438990075Sobrien     front-end messes this up for array types, so we work around that bug
439090075Sobrien     here.  */
439150397Sobrien  if (TREE_CODE (type) == ARRAY_TYPE)
439250397Sobrien    while (type != TYPE_MAIN_VARIANT (type))
439350397Sobrien      type = TYPE_MAIN_VARIANT (type);
439450397Sobrien
439550397Sobrien  return type;
439650397Sobrien}
439750397Sobrien
4398117395Skan/* Return nonzero if the given type node represents a tagged type.  */
439950397Sobrien
440050397Sobrienstatic inline int
4401132718Skanis_tagged_type (tree type)
440250397Sobrien{
440390075Sobrien  enum tree_code code = TREE_CODE (type);
440450397Sobrien
440550397Sobrien  return (code == RECORD_TYPE || code == UNION_TYPE
440650397Sobrien	  || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
440750397Sobrien}
440850397Sobrien
440950397Sobrien/* Convert a DIE tag into its string name.  */
441050397Sobrien
441190075Sobrienstatic const char *
4412132718Skandwarf_tag_name (unsigned int tag)
441350397Sobrien{
441450397Sobrien  switch (tag)
441550397Sobrien    {
441650397Sobrien    case DW_TAG_padding:
441750397Sobrien      return "DW_TAG_padding";
441850397Sobrien    case DW_TAG_array_type:
441950397Sobrien      return "DW_TAG_array_type";
442050397Sobrien    case DW_TAG_class_type:
442150397Sobrien      return "DW_TAG_class_type";
442250397Sobrien    case DW_TAG_entry_point:
442350397Sobrien      return "DW_TAG_entry_point";
442450397Sobrien    case DW_TAG_enumeration_type:
442550397Sobrien      return "DW_TAG_enumeration_type";
442650397Sobrien    case DW_TAG_formal_parameter:
442750397Sobrien      return "DW_TAG_formal_parameter";
442850397Sobrien    case DW_TAG_imported_declaration:
442950397Sobrien      return "DW_TAG_imported_declaration";
443050397Sobrien    case DW_TAG_label:
443150397Sobrien      return "DW_TAG_label";
443250397Sobrien    case DW_TAG_lexical_block:
443350397Sobrien      return "DW_TAG_lexical_block";
443450397Sobrien    case DW_TAG_member:
443550397Sobrien      return "DW_TAG_member";
443650397Sobrien    case DW_TAG_pointer_type:
443750397Sobrien      return "DW_TAG_pointer_type";
443850397Sobrien    case DW_TAG_reference_type:
443950397Sobrien      return "DW_TAG_reference_type";
444050397Sobrien    case DW_TAG_compile_unit:
444150397Sobrien      return "DW_TAG_compile_unit";
444250397Sobrien    case DW_TAG_string_type:
444350397Sobrien      return "DW_TAG_string_type";
444450397Sobrien    case DW_TAG_structure_type:
444550397Sobrien      return "DW_TAG_structure_type";
444650397Sobrien    case DW_TAG_subroutine_type:
444750397Sobrien      return "DW_TAG_subroutine_type";
444850397Sobrien    case DW_TAG_typedef:
444950397Sobrien      return "DW_TAG_typedef";
445050397Sobrien    case DW_TAG_union_type:
445150397Sobrien      return "DW_TAG_union_type";
445250397Sobrien    case DW_TAG_unspecified_parameters:
445350397Sobrien      return "DW_TAG_unspecified_parameters";
445450397Sobrien    case DW_TAG_variant:
445550397Sobrien      return "DW_TAG_variant";
445650397Sobrien    case DW_TAG_common_block:
445750397Sobrien      return "DW_TAG_common_block";
445850397Sobrien    case DW_TAG_common_inclusion:
445950397Sobrien      return "DW_TAG_common_inclusion";
446050397Sobrien    case DW_TAG_inheritance:
446150397Sobrien      return "DW_TAG_inheritance";
446250397Sobrien    case DW_TAG_inlined_subroutine:
446350397Sobrien      return "DW_TAG_inlined_subroutine";
446450397Sobrien    case DW_TAG_module:
446550397Sobrien      return "DW_TAG_module";
446650397Sobrien    case DW_TAG_ptr_to_member_type:
446750397Sobrien      return "DW_TAG_ptr_to_member_type";
446850397Sobrien    case DW_TAG_set_type:
446950397Sobrien      return "DW_TAG_set_type";
447050397Sobrien    case DW_TAG_subrange_type:
447150397Sobrien      return "DW_TAG_subrange_type";
447250397Sobrien    case DW_TAG_with_stmt:
447350397Sobrien      return "DW_TAG_with_stmt";
447450397Sobrien    case DW_TAG_access_declaration:
447550397Sobrien      return "DW_TAG_access_declaration";
447650397Sobrien    case DW_TAG_base_type:
447750397Sobrien      return "DW_TAG_base_type";
447850397Sobrien    case DW_TAG_catch_block:
447950397Sobrien      return "DW_TAG_catch_block";
448050397Sobrien    case DW_TAG_const_type:
448150397Sobrien      return "DW_TAG_const_type";
448250397Sobrien    case DW_TAG_constant:
448350397Sobrien      return "DW_TAG_constant";
448450397Sobrien    case DW_TAG_enumerator:
448550397Sobrien      return "DW_TAG_enumerator";
448650397Sobrien    case DW_TAG_file_type:
448750397Sobrien      return "DW_TAG_file_type";
448850397Sobrien    case DW_TAG_friend:
448950397Sobrien      return "DW_TAG_friend";
449050397Sobrien    case DW_TAG_namelist:
449150397Sobrien      return "DW_TAG_namelist";
449250397Sobrien    case DW_TAG_namelist_item:
449350397Sobrien      return "DW_TAG_namelist_item";
4494132718Skan    case DW_TAG_namespace:
4495132718Skan      return "DW_TAG_namespace";
449650397Sobrien    case DW_TAG_packed_type:
449750397Sobrien      return "DW_TAG_packed_type";
449850397Sobrien    case DW_TAG_subprogram:
449950397Sobrien      return "DW_TAG_subprogram";
450050397Sobrien    case DW_TAG_template_type_param:
450150397Sobrien      return "DW_TAG_template_type_param";
450250397Sobrien    case DW_TAG_template_value_param:
450350397Sobrien      return "DW_TAG_template_value_param";
450450397Sobrien    case DW_TAG_thrown_type:
450550397Sobrien      return "DW_TAG_thrown_type";
450650397Sobrien    case DW_TAG_try_block:
450750397Sobrien      return "DW_TAG_try_block";
450850397Sobrien    case DW_TAG_variant_part:
450950397Sobrien      return "DW_TAG_variant_part";
451050397Sobrien    case DW_TAG_variable:
451150397Sobrien      return "DW_TAG_variable";
451250397Sobrien    case DW_TAG_volatile_type:
451350397Sobrien      return "DW_TAG_volatile_type";
4514169689Skan    case DW_TAG_imported_module:
4515169689Skan      return "DW_TAG_imported_module";
451650397Sobrien    case DW_TAG_MIPS_loop:
451750397Sobrien      return "DW_TAG_MIPS_loop";
451850397Sobrien    case DW_TAG_format_label:
451950397Sobrien      return "DW_TAG_format_label";
452050397Sobrien    case DW_TAG_function_template:
452150397Sobrien      return "DW_TAG_function_template";
452250397Sobrien    case DW_TAG_class_template:
452350397Sobrien      return "DW_TAG_class_template";
452490075Sobrien    case DW_TAG_GNU_BINCL:
452590075Sobrien      return "DW_TAG_GNU_BINCL";
452690075Sobrien    case DW_TAG_GNU_EINCL:
452790075Sobrien      return "DW_TAG_GNU_EINCL";
452850397Sobrien    default:
452950397Sobrien      return "DW_TAG_<unknown>";
453050397Sobrien    }
453150397Sobrien}
453250397Sobrien
453350397Sobrien/* Convert a DWARF attribute code into its string name.  */
453450397Sobrien
453590075Sobrienstatic const char *
4536132718Skandwarf_attr_name (unsigned int attr)
453750397Sobrien{
453850397Sobrien  switch (attr)
453950397Sobrien    {
454050397Sobrien    case DW_AT_sibling:
454150397Sobrien      return "DW_AT_sibling";
454250397Sobrien    case DW_AT_location:
454350397Sobrien      return "DW_AT_location";
454450397Sobrien    case DW_AT_name:
454550397Sobrien      return "DW_AT_name";
454650397Sobrien    case DW_AT_ordering:
454750397Sobrien      return "DW_AT_ordering";
454850397Sobrien    case DW_AT_subscr_data:
454950397Sobrien      return "DW_AT_subscr_data";
455050397Sobrien    case DW_AT_byte_size:
455150397Sobrien      return "DW_AT_byte_size";
455250397Sobrien    case DW_AT_bit_offset:
455350397Sobrien      return "DW_AT_bit_offset";
455450397Sobrien    case DW_AT_bit_size:
455550397Sobrien      return "DW_AT_bit_size";
455650397Sobrien    case DW_AT_element_list:
455750397Sobrien      return "DW_AT_element_list";
455850397Sobrien    case DW_AT_stmt_list:
455950397Sobrien      return "DW_AT_stmt_list";
456050397Sobrien    case DW_AT_low_pc:
456150397Sobrien      return "DW_AT_low_pc";
456250397Sobrien    case DW_AT_high_pc:
456350397Sobrien      return "DW_AT_high_pc";
456450397Sobrien    case DW_AT_language:
456550397Sobrien      return "DW_AT_language";
456650397Sobrien    case DW_AT_member:
456750397Sobrien      return "DW_AT_member";
456850397Sobrien    case DW_AT_discr:
456950397Sobrien      return "DW_AT_discr";
457050397Sobrien    case DW_AT_discr_value:
457150397Sobrien      return "DW_AT_discr_value";
457250397Sobrien    case DW_AT_visibility:
457350397Sobrien      return "DW_AT_visibility";
457450397Sobrien    case DW_AT_import:
457550397Sobrien      return "DW_AT_import";
457650397Sobrien    case DW_AT_string_length:
457750397Sobrien      return "DW_AT_string_length";
457850397Sobrien    case DW_AT_common_reference:
457950397Sobrien      return "DW_AT_common_reference";
458050397Sobrien    case DW_AT_comp_dir:
458150397Sobrien      return "DW_AT_comp_dir";
458250397Sobrien    case DW_AT_const_value:
458350397Sobrien      return "DW_AT_const_value";
458450397Sobrien    case DW_AT_containing_type:
458550397Sobrien      return "DW_AT_containing_type";
458650397Sobrien    case DW_AT_default_value:
458750397Sobrien      return "DW_AT_default_value";
458850397Sobrien    case DW_AT_inline:
458950397Sobrien      return "DW_AT_inline";
459050397Sobrien    case DW_AT_is_optional:
459150397Sobrien      return "DW_AT_is_optional";
459250397Sobrien    case DW_AT_lower_bound:
459350397Sobrien      return "DW_AT_lower_bound";
459450397Sobrien    case DW_AT_producer:
459550397Sobrien      return "DW_AT_producer";
459650397Sobrien    case DW_AT_prototyped:
459750397Sobrien      return "DW_AT_prototyped";
459850397Sobrien    case DW_AT_return_addr:
459950397Sobrien      return "DW_AT_return_addr";
460050397Sobrien    case DW_AT_start_scope:
460150397Sobrien      return "DW_AT_start_scope";
460250397Sobrien    case DW_AT_stride_size:
460350397Sobrien      return "DW_AT_stride_size";
460450397Sobrien    case DW_AT_upper_bound:
460550397Sobrien      return "DW_AT_upper_bound";
460650397Sobrien    case DW_AT_abstract_origin:
460750397Sobrien      return "DW_AT_abstract_origin";
460850397Sobrien    case DW_AT_accessibility:
460950397Sobrien      return "DW_AT_accessibility";
461050397Sobrien    case DW_AT_address_class:
461150397Sobrien      return "DW_AT_address_class";
461250397Sobrien    case DW_AT_artificial:
461350397Sobrien      return "DW_AT_artificial";
461450397Sobrien    case DW_AT_base_types:
461550397Sobrien      return "DW_AT_base_types";
461650397Sobrien    case DW_AT_calling_convention:
461750397Sobrien      return "DW_AT_calling_convention";
461850397Sobrien    case DW_AT_count:
461950397Sobrien      return "DW_AT_count";
462050397Sobrien    case DW_AT_data_member_location:
462150397Sobrien      return "DW_AT_data_member_location";
462250397Sobrien    case DW_AT_decl_column:
462350397Sobrien      return "DW_AT_decl_column";
462450397Sobrien    case DW_AT_decl_file:
462550397Sobrien      return "DW_AT_decl_file";
462650397Sobrien    case DW_AT_decl_line:
462750397Sobrien      return "DW_AT_decl_line";
462850397Sobrien    case DW_AT_declaration:
462950397Sobrien      return "DW_AT_declaration";
463050397Sobrien    case DW_AT_discr_list:
463150397Sobrien      return "DW_AT_discr_list";
463250397Sobrien    case DW_AT_encoding:
463350397Sobrien      return "DW_AT_encoding";
463450397Sobrien    case DW_AT_external:
463550397Sobrien      return "DW_AT_external";
463650397Sobrien    case DW_AT_frame_base:
463750397Sobrien      return "DW_AT_frame_base";
463850397Sobrien    case DW_AT_friend:
463950397Sobrien      return "DW_AT_friend";
464050397Sobrien    case DW_AT_identifier_case:
464150397Sobrien      return "DW_AT_identifier_case";
464250397Sobrien    case DW_AT_macro_info:
464350397Sobrien      return "DW_AT_macro_info";
464450397Sobrien    case DW_AT_namelist_items:
464550397Sobrien      return "DW_AT_namelist_items";
464650397Sobrien    case DW_AT_priority:
464750397Sobrien      return "DW_AT_priority";
464850397Sobrien    case DW_AT_segment:
464950397Sobrien      return "DW_AT_segment";
465050397Sobrien    case DW_AT_specification:
465150397Sobrien      return "DW_AT_specification";
465250397Sobrien    case DW_AT_static_link:
465350397Sobrien      return "DW_AT_static_link";
465450397Sobrien    case DW_AT_type:
465550397Sobrien      return "DW_AT_type";
465650397Sobrien    case DW_AT_use_location:
465750397Sobrien      return "DW_AT_use_location";
465850397Sobrien    case DW_AT_variable_parameter:
465950397Sobrien      return "DW_AT_variable_parameter";
466050397Sobrien    case DW_AT_virtuality:
466150397Sobrien      return "DW_AT_virtuality";
466250397Sobrien    case DW_AT_vtable_elem_location:
466350397Sobrien      return "DW_AT_vtable_elem_location";
466450397Sobrien
466590075Sobrien    case DW_AT_allocated:
466690075Sobrien      return "DW_AT_allocated";
466790075Sobrien    case DW_AT_associated:
466890075Sobrien      return "DW_AT_associated";
466990075Sobrien    case DW_AT_data_location:
467090075Sobrien      return "DW_AT_data_location";
467190075Sobrien    case DW_AT_stride:
467290075Sobrien      return "DW_AT_stride";
467390075Sobrien    case DW_AT_entry_pc:
467490075Sobrien      return "DW_AT_entry_pc";
467590075Sobrien    case DW_AT_use_UTF8:
467690075Sobrien      return "DW_AT_use_UTF8";
467790075Sobrien    case DW_AT_extension:
467890075Sobrien      return "DW_AT_extension";
467990075Sobrien    case DW_AT_ranges:
468090075Sobrien      return "DW_AT_ranges";
468190075Sobrien    case DW_AT_trampoline:
468290075Sobrien      return "DW_AT_trampoline";
468390075Sobrien    case DW_AT_call_column:
468490075Sobrien      return "DW_AT_call_column";
468590075Sobrien    case DW_AT_call_file:
468690075Sobrien      return "DW_AT_call_file";
468790075Sobrien    case DW_AT_call_line:
468890075Sobrien      return "DW_AT_call_line";
468990075Sobrien
469050397Sobrien    case DW_AT_MIPS_fde:
469150397Sobrien      return "DW_AT_MIPS_fde";
469250397Sobrien    case DW_AT_MIPS_loop_begin:
469350397Sobrien      return "DW_AT_MIPS_loop_begin";
469450397Sobrien    case DW_AT_MIPS_tail_loop_begin:
469550397Sobrien      return "DW_AT_MIPS_tail_loop_begin";
469650397Sobrien    case DW_AT_MIPS_epilog_begin:
469750397Sobrien      return "DW_AT_MIPS_epilog_begin";
469850397Sobrien    case DW_AT_MIPS_loop_unroll_factor:
469950397Sobrien      return "DW_AT_MIPS_loop_unroll_factor";
470050397Sobrien    case DW_AT_MIPS_software_pipeline_depth:
470150397Sobrien      return "DW_AT_MIPS_software_pipeline_depth";
470250397Sobrien    case DW_AT_MIPS_linkage_name:
470350397Sobrien      return "DW_AT_MIPS_linkage_name";
470450397Sobrien    case DW_AT_MIPS_stride:
470550397Sobrien      return "DW_AT_MIPS_stride";
470650397Sobrien    case DW_AT_MIPS_abstract_name:
470750397Sobrien      return "DW_AT_MIPS_abstract_name";
470850397Sobrien    case DW_AT_MIPS_clone_origin:
470950397Sobrien      return "DW_AT_MIPS_clone_origin";
471050397Sobrien    case DW_AT_MIPS_has_inlines:
471150397Sobrien      return "DW_AT_MIPS_has_inlines";
471250397Sobrien
471350397Sobrien    case DW_AT_sf_names:
471450397Sobrien      return "DW_AT_sf_names";
471550397Sobrien    case DW_AT_src_info:
471650397Sobrien      return "DW_AT_src_info";
471750397Sobrien    case DW_AT_mac_info:
471850397Sobrien      return "DW_AT_mac_info";
471950397Sobrien    case DW_AT_src_coords:
472050397Sobrien      return "DW_AT_src_coords";
472150397Sobrien    case DW_AT_body_begin:
472250397Sobrien      return "DW_AT_body_begin";
472350397Sobrien    case DW_AT_body_end:
472450397Sobrien      return "DW_AT_body_end";
472596263Sobrien    case DW_AT_GNU_vector:
472696263Sobrien      return "DW_AT_GNU_vector";
472796263Sobrien
472890075Sobrien    case DW_AT_VMS_rtnbeg_pd_address:
472990075Sobrien      return "DW_AT_VMS_rtnbeg_pd_address";
473090075Sobrien
473150397Sobrien    default:
473250397Sobrien      return "DW_AT_<unknown>";
473350397Sobrien    }
473450397Sobrien}
473550397Sobrien
473650397Sobrien/* Convert a DWARF value form code into its string name.  */
473750397Sobrien
473890075Sobrienstatic const char *
4739132718Skandwarf_form_name (unsigned int form)
474050397Sobrien{
474150397Sobrien  switch (form)
474250397Sobrien    {
474350397Sobrien    case DW_FORM_addr:
474450397Sobrien      return "DW_FORM_addr";
474550397Sobrien    case DW_FORM_block2:
474650397Sobrien      return "DW_FORM_block2";
474750397Sobrien    case DW_FORM_block4:
474850397Sobrien      return "DW_FORM_block4";
474950397Sobrien    case DW_FORM_data2:
475050397Sobrien      return "DW_FORM_data2";
475150397Sobrien    case DW_FORM_data4:
475250397Sobrien      return "DW_FORM_data4";
475350397Sobrien    case DW_FORM_data8:
475450397Sobrien      return "DW_FORM_data8";
475550397Sobrien    case DW_FORM_string:
475650397Sobrien      return "DW_FORM_string";
475750397Sobrien    case DW_FORM_block:
475850397Sobrien      return "DW_FORM_block";
475950397Sobrien    case DW_FORM_block1:
476050397Sobrien      return "DW_FORM_block1";
476150397Sobrien    case DW_FORM_data1:
476250397Sobrien      return "DW_FORM_data1";
476350397Sobrien    case DW_FORM_flag:
476450397Sobrien      return "DW_FORM_flag";
476550397Sobrien    case DW_FORM_sdata:
476650397Sobrien      return "DW_FORM_sdata";
476750397Sobrien    case DW_FORM_strp:
476850397Sobrien      return "DW_FORM_strp";
476950397Sobrien    case DW_FORM_udata:
477050397Sobrien      return "DW_FORM_udata";
477150397Sobrien    case DW_FORM_ref_addr:
477250397Sobrien      return "DW_FORM_ref_addr";
477350397Sobrien    case DW_FORM_ref1:
477450397Sobrien      return "DW_FORM_ref1";
477550397Sobrien    case DW_FORM_ref2:
477650397Sobrien      return "DW_FORM_ref2";
477750397Sobrien    case DW_FORM_ref4:
477850397Sobrien      return "DW_FORM_ref4";
477950397Sobrien    case DW_FORM_ref8:
478050397Sobrien      return "DW_FORM_ref8";
478150397Sobrien    case DW_FORM_ref_udata:
478250397Sobrien      return "DW_FORM_ref_udata";
478350397Sobrien    case DW_FORM_indirect:
478450397Sobrien      return "DW_FORM_indirect";
478550397Sobrien    default:
478650397Sobrien      return "DW_FORM_<unknown>";
478750397Sobrien    }
478850397Sobrien}
478950397Sobrien
479050397Sobrien/* Determine the "ultimate origin" of a decl.  The decl may be an inlined
479150397Sobrien   instance of an inlined instance of a decl which is local to an inline
479250397Sobrien   function, so we have to trace all of the way back through the origin chain
479350397Sobrien   to find out what sort of node actually served as the original seed for the
479450397Sobrien   given block.  */
479550397Sobrien
479650397Sobrienstatic tree
4797132718Skandecl_ultimate_origin (tree decl)
479850397Sobrien{
4799169689Skan  if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4800169689Skan    return NULL_TREE;
4801169689Skan
480290075Sobrien  /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
480390075Sobrien     nodes in the function to point to themselves; ignore that if
480490075Sobrien     we're trying to output the abstract instance of this function.  */
480590075Sobrien  if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
480690075Sobrien    return NULL_TREE;
480790075Sobrien
4808169689Skan  /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4809169689Skan     most distant ancestor, this should never happen.  */
4810169689Skan  gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
481150397Sobrien
481252284Sobrien  return DECL_ABSTRACT_ORIGIN (decl);
481350397Sobrien}
481450397Sobrien
481550397Sobrien/* Determine the "ultimate origin" of a block.  The block may be an inlined
481650397Sobrien   instance of an inlined instance of a block which is local to an inline
481750397Sobrien   function, so we have to trace all of the way back through the origin chain
481850397Sobrien   to find out what sort of node actually served as the original seed for the
481950397Sobrien   given block.  */
482050397Sobrien
482150397Sobrienstatic tree
4822132718Skanblock_ultimate_origin (tree block)
482350397Sobrien{
482490075Sobrien  tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
482550397Sobrien
482690075Sobrien  /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
482790075Sobrien     nodes in the function to point to themselves; ignore that if
482890075Sobrien     we're trying to output the abstract instance of this function.  */
482990075Sobrien  if (BLOCK_ABSTRACT (block) && immediate_origin == block)
483090075Sobrien    return NULL_TREE;
483190075Sobrien
483250397Sobrien  if (immediate_origin == NULL_TREE)
483350397Sobrien    return NULL_TREE;
483450397Sobrien  else
483550397Sobrien    {
483690075Sobrien      tree ret_val;
483790075Sobrien      tree lookahead = immediate_origin;
483850397Sobrien
483950397Sobrien      do
484050397Sobrien	{
484150397Sobrien	  ret_val = lookahead;
484290075Sobrien	  lookahead = (TREE_CODE (ret_val) == BLOCK
484390075Sobrien		       ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
484450397Sobrien	}
484550397Sobrien      while (lookahead != NULL && lookahead != ret_val);
4846169689Skan
4847169689Skan      /* The block's abstract origin chain may not be the *ultimate* origin of
4848169689Skan	 the block. It could lead to a DECL that has an abstract origin set.
4849169689Skan	 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4850169689Skan	 will give us if it has one).  Note that DECL's abstract origins are
4851169689Skan	 supposed to be the most distant ancestor (or so decl_ultimate_origin
4852169689Skan	 claims), so we don't need to loop following the DECL origins.  */
4853169689Skan      if (DECL_P (ret_val))
4854169689Skan	return DECL_ORIGIN (ret_val);
485550397Sobrien
485650397Sobrien      return ret_val;
485750397Sobrien    }
485850397Sobrien}
485950397Sobrien
486050397Sobrien/* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
486150397Sobrien   of a virtual function may refer to a base class, so we check the 'this'
486250397Sobrien   parameter.  */
486350397Sobrien
486450397Sobrienstatic tree
4865132718Skandecl_class_context (tree decl)
486650397Sobrien{
486750397Sobrien  tree context = NULL_TREE;
486850397Sobrien
486950397Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
487050397Sobrien    context = DECL_CONTEXT (decl);
487150397Sobrien  else
487250397Sobrien    context = TYPE_MAIN_VARIANT
487350397Sobrien      (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
487450397Sobrien
487590075Sobrien  if (context && !TYPE_P (context))
487650397Sobrien    context = NULL_TREE;
487750397Sobrien
487850397Sobrien  return context;
487950397Sobrien}
488050397Sobrien
4881169689Skan/* Add an attribute/value pair to a DIE.  */
488250397Sobrien
488350397Sobrienstatic inline void
4884132718Skanadd_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
488550397Sobrien{
4886169689Skan  /* Maybe this should be an assert?  */
4887169689Skan  if (die == NULL)
4888169689Skan    return;
4889169689Skan
4890169689Skan  if (die->die_attr == NULL)
4891169689Skan    die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4892169689Skan  VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
489350397Sobrien}
489450397Sobrien
4895132718Skanstatic inline enum dw_val_class
4896132718SkanAT_class (dw_attr_ref a)
489790075Sobrien{
489890075Sobrien  return a->dw_attr_val.val_class;
489990075Sobrien}
490090075Sobrien
490150397Sobrien/* Add a flag value attribute to a DIE.  */
490250397Sobrien
490350397Sobrienstatic inline void
4904132718Skanadd_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
490550397Sobrien{
4906169689Skan  dw_attr_node attr;
490750397Sobrien
4908169689Skan  attr.dw_attr = attr_kind;
4909169689Skan  attr.dw_attr_val.val_class = dw_val_class_flag;
4910169689Skan  attr.dw_attr_val.v.val_flag = flag;
4911169689Skan  add_dwarf_attr (die, &attr);
491250397Sobrien}
491350397Sobrien
491490075Sobrienstatic inline unsigned
4915132718SkanAT_flag (dw_attr_ref a)
491690075Sobrien{
4917169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_flag);
4918169689Skan  return a->dw_attr_val.v.val_flag;
491990075Sobrien}
492090075Sobrien
492150397Sobrien/* Add a signed integer attribute value to a DIE.  */
492250397Sobrien
492350397Sobrienstatic inline void
4924132718Skanadd_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
492550397Sobrien{
4926169689Skan  dw_attr_node attr;
492750397Sobrien
4928169689Skan  attr.dw_attr = attr_kind;
4929169689Skan  attr.dw_attr_val.val_class = dw_val_class_const;
4930169689Skan  attr.dw_attr_val.v.val_int = int_val;
4931169689Skan  add_dwarf_attr (die, &attr);
493250397Sobrien}
493350397Sobrien
4934132718Skanstatic inline HOST_WIDE_INT
4935132718SkanAT_int (dw_attr_ref a)
493690075Sobrien{
4937169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_const);
4938169689Skan  return a->dw_attr_val.v.val_int;
493990075Sobrien}
494090075Sobrien
494150397Sobrien/* Add an unsigned integer attribute value to a DIE.  */
494250397Sobrien
494350397Sobrienstatic inline void
4944132718Skanadd_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4945132718Skan		 unsigned HOST_WIDE_INT unsigned_val)
494650397Sobrien{
4947169689Skan  dw_attr_node attr;
494850397Sobrien
4949169689Skan  attr.dw_attr = attr_kind;
4950169689Skan  attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4951169689Skan  attr.dw_attr_val.v.val_unsigned = unsigned_val;
4952169689Skan  add_dwarf_attr (die, &attr);
495350397Sobrien}
495450397Sobrien
4955132718Skanstatic inline unsigned HOST_WIDE_INT
4956132718SkanAT_unsigned (dw_attr_ref a)
495790075Sobrien{
4958169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4959169689Skan  return a->dw_attr_val.v.val_unsigned;
496090075Sobrien}
496190075Sobrien
496250397Sobrien/* Add an unsigned double integer attribute value to a DIE.  */
496350397Sobrien
496450397Sobrienstatic inline void
4965132718Skanadd_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4966132718Skan		  long unsigned int val_hi, long unsigned int val_low)
496750397Sobrien{
4968169689Skan  dw_attr_node attr;
496950397Sobrien
4970169689Skan  attr.dw_attr = attr_kind;
4971169689Skan  attr.dw_attr_val.val_class = dw_val_class_long_long;
4972169689Skan  attr.dw_attr_val.v.val_long_long.hi = val_hi;
4973169689Skan  attr.dw_attr_val.v.val_long_long.low = val_low;
4974169689Skan  add_dwarf_attr (die, &attr);
497550397Sobrien}
497650397Sobrien
497750397Sobrien/* Add a floating point attribute value to a DIE and return it.  */
497850397Sobrien
497950397Sobrienstatic inline void
4980132718Skanadd_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4981132718Skan	    unsigned int length, unsigned int elt_size, unsigned char *array)
498250397Sobrien{
4983169689Skan  dw_attr_node attr;
498450397Sobrien
4985169689Skan  attr.dw_attr = attr_kind;
4986169689Skan  attr.dw_attr_val.val_class = dw_val_class_vec;
4987169689Skan  attr.dw_attr_val.v.val_vec.length = length;
4988169689Skan  attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4989169689Skan  attr.dw_attr_val.v.val_vec.array = array;
4990169689Skan  add_dwarf_attr (die, &attr);
499150397Sobrien}
499250397Sobrien
4993132718Skan/* Hash and equality functions for debug_str_hash.  */
4994132718Skan
4995132718Skanstatic hashval_t
4996132718Skandebug_str_do_hash (const void *x)
4997132718Skan{
4998132718Skan  return htab_hash_string (((const struct indirect_string_node *)x)->str);
4999132718Skan}
5000132718Skan
5001132718Skanstatic int
5002132718Skandebug_str_eq (const void *x1, const void *x2)
5003132718Skan{
5004132718Skan  return strcmp ((((const struct indirect_string_node *)x1)->str),
5005132718Skan		 (const char *)x2) == 0;
5006132718Skan}
5007132718Skan
500850397Sobrien/* Add a string attribute value to a DIE.  */
500950397Sobrien
501050397Sobrienstatic inline void
5011132718Skanadd_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
501250397Sobrien{
5013169689Skan  dw_attr_node attr;
501490075Sobrien  struct indirect_string_node *node;
5015132718Skan  void **slot;
5016117395Skan
501790075Sobrien  if (! debug_str_hash)
5018132718Skan    debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5019132718Skan				      debug_str_eq, NULL);
502050397Sobrien
5021132718Skan  slot = htab_find_slot_with_hash (debug_str_hash, str,
5022132718Skan				   htab_hash_string (str), INSERT);
5023132718Skan  if (*slot == NULL)
5024132718Skan    *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
5025132718Skan  node = (struct indirect_string_node *) *slot;
5026132718Skan  node->str = ggc_strdup (str);
502790075Sobrien  node->refcount++;
502890075Sobrien
5029169689Skan  attr.dw_attr = attr_kind;
5030169689Skan  attr.dw_attr_val.val_class = dw_val_class_str;
5031169689Skan  attr.dw_attr_val.v.val_str = node;
5032169689Skan  add_dwarf_attr (die, &attr);
503350397Sobrien}
503450397Sobrien
503590075Sobrienstatic inline const char *
5036132718SkanAT_string (dw_attr_ref a)
503790075Sobrien{
5038169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_str);
5039169689Skan  return a->dw_attr_val.v.val_str->str;
504090075Sobrien}
504190075Sobrien
504290075Sobrien/* Find out whether a string should be output inline in DIE
504390075Sobrien   or out-of-line in .debug_str section.  */
504490075Sobrien
504590075Sobrienstatic int
5046132718SkanAT_string_form (dw_attr_ref a)
504790075Sobrien{
5048169689Skan  struct indirect_string_node *node;
5049169689Skan  unsigned int len;
5050169689Skan  char label[32];
505190075Sobrien
5052169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_str);
505390075Sobrien
5054169689Skan  node = a->dw_attr_val.v.val_str;
5055169689Skan  if (node->form)
5056169689Skan    return node->form;
505790075Sobrien
5058169689Skan  len = strlen (node->str) + 1;
505990075Sobrien
5060169689Skan  /* If the string is shorter or equal to the size of the reference, it is
5061169689Skan     always better to put it inline.  */
5062169689Skan  if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5063169689Skan    return node->form = DW_FORM_string;
506490075Sobrien
5065169689Skan  /* If we cannot expect the linker to merge strings in .debug_str
5066169689Skan     section, only put it into .debug_str if it is worth even in this
5067169689Skan     single module.  */
5068169689Skan  if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5069169689Skan      && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5070169689Skan    return node->form = DW_FORM_string;
507190075Sobrien
5072169689Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5073169689Skan  ++dw2_string_counter;
5074169689Skan  node->label = xstrdup (label);
507590075Sobrien
5076169689Skan  return node->form = DW_FORM_strp;
507790075Sobrien}
507890075Sobrien
507950397Sobrien/* Add a DIE reference attribute value to a DIE.  */
508050397Sobrien
508150397Sobrienstatic inline void
5082132718Skanadd_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
508350397Sobrien{
5084169689Skan  dw_attr_node attr;
508550397Sobrien
5086169689Skan  attr.dw_attr = attr_kind;
5087169689Skan  attr.dw_attr_val.val_class = dw_val_class_die_ref;
5088169689Skan  attr.dw_attr_val.v.val_die_ref.die = targ_die;
5089169689Skan  attr.dw_attr_val.v.val_die_ref.external = 0;
5090169689Skan  add_dwarf_attr (die, &attr);
509150397Sobrien}
509250397Sobrien
5093132718Skan/* Add an AT_specification attribute to a DIE, and also make the back
5094132718Skan   pointer from the specification to the definition.  */
5095132718Skan
5096132718Skanstatic inline void
5097132718Skanadd_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5098132718Skan{
5099132718Skan  add_AT_die_ref (die, DW_AT_specification, targ_die);
5100169689Skan  gcc_assert (!targ_die->die_definition);
5101132718Skan  targ_die->die_definition = die;
5102132718Skan}
5103132718Skan
510490075Sobrienstatic inline dw_die_ref
5105132718SkanAT_ref (dw_attr_ref a)
510690075Sobrien{
5107169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5108169689Skan  return a->dw_attr_val.v.val_die_ref.die;
510990075Sobrien}
511090075Sobrien
511190075Sobrienstatic inline int
5112132718SkanAT_ref_external (dw_attr_ref a)
511390075Sobrien{
511490075Sobrien  if (a && AT_class (a) == dw_val_class_die_ref)
511590075Sobrien    return a->dw_attr_val.v.val_die_ref.external;
511690075Sobrien
511790075Sobrien  return 0;
511890075Sobrien}
511990075Sobrien
512090075Sobrienstatic inline void
5121132718Skanset_AT_ref_external (dw_attr_ref a, int i)
512290075Sobrien{
5123169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5124169689Skan  a->dw_attr_val.v.val_die_ref.external = i;
512590075Sobrien}
512690075Sobrien
512750397Sobrien/* Add an FDE reference attribute value to a DIE.  */
512850397Sobrien
512950397Sobrienstatic inline void
5130132718Skanadd_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
513150397Sobrien{
5132169689Skan  dw_attr_node attr;
513350397Sobrien
5134169689Skan  attr.dw_attr = attr_kind;
5135169689Skan  attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5136169689Skan  attr.dw_attr_val.v.val_fde_index = targ_fde;
5137169689Skan  add_dwarf_attr (die, &attr);
513850397Sobrien}
513950397Sobrien
514050397Sobrien/* Add a location description attribute value to a DIE.  */
514150397Sobrien
514250397Sobrienstatic inline void
5143132718Skanadd_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
514450397Sobrien{
5145169689Skan  dw_attr_node attr;
514650397Sobrien
5147169689Skan  attr.dw_attr = attr_kind;
5148169689Skan  attr.dw_attr_val.val_class = dw_val_class_loc;
5149169689Skan  attr.dw_attr_val.v.val_loc = loc;
5150169689Skan  add_dwarf_attr (die, &attr);
515150397Sobrien}
515250397Sobrien
515390075Sobrienstatic inline dw_loc_descr_ref
5154132718SkanAT_loc (dw_attr_ref a)
515590075Sobrien{
5156169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_loc);
5157169689Skan  return a->dw_attr_val.v.val_loc;
515890075Sobrien}
515990075Sobrien
516090075Sobrienstatic inline void
5161132718Skanadd_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
516290075Sobrien{
5163169689Skan  dw_attr_node attr;
516490075Sobrien
5165169689Skan  attr.dw_attr = attr_kind;
5166169689Skan  attr.dw_attr_val.val_class = dw_val_class_loc_list;
5167169689Skan  attr.dw_attr_val.v.val_loc_list = loc_list;
5168169689Skan  add_dwarf_attr (die, &attr);
5169169689Skan  have_location_lists = true;
517090075Sobrien}
517190075Sobrien
517290075Sobrienstatic inline dw_loc_list_ref
5173132718SkanAT_loc_list (dw_attr_ref a)
517490075Sobrien{
5175169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5176169689Skan  return a->dw_attr_val.v.val_loc_list;
517790075Sobrien}
517890075Sobrien
517950397Sobrien/* Add an address constant attribute value to a DIE.  */
518050397Sobrien
518150397Sobrienstatic inline void
5182132718Skanadd_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
518350397Sobrien{
5184169689Skan  dw_attr_node attr;
518550397Sobrien
5186169689Skan  attr.dw_attr = attr_kind;
5187169689Skan  attr.dw_attr_val.val_class = dw_val_class_addr;
5188169689Skan  attr.dw_attr_val.v.val_addr = addr;
5189169689Skan  add_dwarf_attr (die, &attr);
519050397Sobrien}
519150397Sobrien
5192169689Skan/* Get the RTX from to an address DIE attribute.  */
5193169689Skan
519490075Sobrienstatic inline rtx
5195132718SkanAT_addr (dw_attr_ref a)
519690075Sobrien{
5197169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_addr);
5198169689Skan  return a->dw_attr_val.v.val_addr;
5199169689Skan}
520090075Sobrien
5201169689Skan/* Add a file attribute value to a DIE.  */
5202169689Skan
5203169689Skanstatic inline void
5204169689Skanadd_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5205169689Skan	     struct dwarf_file_data *fd)
5206169689Skan{
5207169689Skan  dw_attr_node attr;
5208169689Skan
5209169689Skan  attr.dw_attr = attr_kind;
5210169689Skan  attr.dw_attr_val.val_class = dw_val_class_file;
5211169689Skan  attr.dw_attr_val.v.val_file = fd;
5212169689Skan  add_dwarf_attr (die, &attr);
521390075Sobrien}
521490075Sobrien
5215169689Skan/* Get the dwarf_file_data from a file DIE attribute.  */
5216169689Skan
5217169689Skanstatic inline struct dwarf_file_data *
5218169689SkanAT_file (dw_attr_ref a)
5219169689Skan{
5220169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_file);
5221169689Skan  return a->dw_attr_val.v.val_file;
5222169689Skan}
5223169689Skan
522450397Sobrien/* Add a label identifier attribute value to a DIE.  */
522550397Sobrien
522650397Sobrienstatic inline void
5227132718Skanadd_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
522850397Sobrien{
5229169689Skan  dw_attr_node attr;
523050397Sobrien
5231169689Skan  attr.dw_attr = attr_kind;
5232169689Skan  attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5233169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5234169689Skan  add_dwarf_attr (die, &attr);
523550397Sobrien}
523650397Sobrien
5237169689Skan/* Add a section offset attribute value to a DIE, an offset into the
5238169689Skan   debug_line section.  */
523950397Sobrien
524050397Sobrienstatic inline void
5241169689Skanadd_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5242169689Skan		const char *label)
524350397Sobrien{
5244169689Skan  dw_attr_node attr;
524550397Sobrien
5246169689Skan  attr.dw_attr = attr_kind;
5247169689Skan  attr.dw_attr_val.val_class = dw_val_class_lineptr;
5248169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5249169689Skan  add_dwarf_attr (die, &attr);
525050397Sobrien}
525150397Sobrien
5252169689Skan/* Add a section offset attribute value to a DIE, an offset into the
5253169689Skan   debug_macinfo section.  */
5254169689Skan
5255169689Skanstatic inline void
5256169689Skanadd_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5257169689Skan	       const char *label)
5258169689Skan{
5259169689Skan  dw_attr_node attr;
5260169689Skan
5261169689Skan  attr.dw_attr = attr_kind;
5262169689Skan  attr.dw_attr_val.val_class = dw_val_class_macptr;
5263169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5264169689Skan  add_dwarf_attr (die, &attr);
5265169689Skan}
5266169689Skan
526790075Sobrien/* Add an offset attribute value to a DIE.  */
526850397Sobrien
526990075Sobrienstatic inline void
5270132718Skanadd_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5271132718Skan	       unsigned HOST_WIDE_INT offset)
527250397Sobrien{
5273169689Skan  dw_attr_node attr;
527450397Sobrien
5275169689Skan  attr.dw_attr = attr_kind;
5276169689Skan  attr.dw_attr_val.val_class = dw_val_class_offset;
5277169689Skan  attr.dw_attr_val.v.val_offset = offset;
5278169689Skan  add_dwarf_attr (die, &attr);
527990075Sobrien}
528050397Sobrien
528190075Sobrien/* Add an range_list attribute value to a DIE.  */
528290075Sobrien
528390075Sobrienstatic void
5284132718Skanadd_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5285132718Skan		   long unsigned int offset)
528690075Sobrien{
5287169689Skan  dw_attr_node attr;
528890075Sobrien
5289169689Skan  attr.dw_attr = attr_kind;
5290169689Skan  attr.dw_attr_val.val_class = dw_val_class_range_list;
5291169689Skan  attr.dw_attr_val.v.val_offset = offset;
5292169689Skan  add_dwarf_attr (die, &attr);
529350397Sobrien}
529450397Sobrien
529590075Sobrienstatic inline const char *
5296132718SkanAT_lbl (dw_attr_ref a)
529790075Sobrien{
5298169689Skan  gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5299169689Skan		    || AT_class (a) == dw_val_class_lineptr
5300169689Skan		    || AT_class (a) == dw_val_class_macptr));
5301169689Skan  return a->dw_attr_val.v.val_lbl_id;
530290075Sobrien}
530390075Sobrien
530450397Sobrien/* Get the attribute of type attr_kind.  */
530550397Sobrien
5306132718Skanstatic dw_attr_ref
5307132718Skanget_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
530850397Sobrien{
530990075Sobrien  dw_attr_ref a;
5310169689Skan  unsigned ix;
531190075Sobrien  dw_die_ref spec = NULL;
531290075Sobrien
5313169689Skan  if (! die)
5314169689Skan    return NULL;
531550397Sobrien
5316169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5317169689Skan    if (a->dw_attr == attr_kind)
5318169689Skan      return a;
5319169689Skan    else if (a->dw_attr == DW_AT_specification
5320169689Skan	     || a->dw_attr == DW_AT_abstract_origin)
5321169689Skan      spec = AT_ref (a);
5322169689Skan
5323169689Skan  if (spec)
5324169689Skan    return get_AT (spec, attr_kind);
532550397Sobrien
532650397Sobrien  return NULL;
532750397Sobrien}
532850397Sobrien
532990075Sobrien/* Return the "low pc" attribute value, typically associated with a subprogram
533090075Sobrien   DIE.  Return null if the "low pc" attribute is either not present, or if it
533190075Sobrien   cannot be represented as an assembler label identifier.  */
533250397Sobrien
533390075Sobrienstatic inline const char *
5334132718Skanget_AT_low_pc (dw_die_ref die)
533550397Sobrien{
533690075Sobrien  dw_attr_ref a = get_AT (die, DW_AT_low_pc);
533750397Sobrien
533890075Sobrien  return a ? AT_lbl (a) : NULL;
533950397Sobrien}
534050397Sobrien
534190075Sobrien/* Return the "high pc" attribute value, typically associated with a subprogram
534290075Sobrien   DIE.  Return null if the "high pc" attribute is either not present, or if it
534390075Sobrien   cannot be represented as an assembler label identifier.  */
534450397Sobrien
534590075Sobrienstatic inline const char *
5346132718Skanget_AT_hi_pc (dw_die_ref die)
534750397Sobrien{
534890075Sobrien  dw_attr_ref a = get_AT (die, DW_AT_high_pc);
534950397Sobrien
535090075Sobrien  return a ? AT_lbl (a) : NULL;
535150397Sobrien}
535250397Sobrien
535350397Sobrien/* Return the value of the string attribute designated by ATTR_KIND, or
535450397Sobrien   NULL if it is not present.  */
535550397Sobrien
535690075Sobrienstatic inline const char *
5357132718Skanget_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
535850397Sobrien{
535990075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
536050397Sobrien
536190075Sobrien  return a ? AT_string (a) : NULL;
536250397Sobrien}
536350397Sobrien
536450397Sobrien/* Return the value of the flag attribute designated by ATTR_KIND, or -1
536550397Sobrien   if it is not present.  */
536650397Sobrien
536750397Sobrienstatic inline int
5368132718Skanget_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
536950397Sobrien{
537090075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
537150397Sobrien
537290075Sobrien  return a ? AT_flag (a) : 0;
537350397Sobrien}
537450397Sobrien
537550397Sobrien/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
537650397Sobrien   if it is not present.  */
537750397Sobrien
537850397Sobrienstatic inline unsigned
5379132718Skanget_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
538050397Sobrien{
538190075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
538250397Sobrien
538390075Sobrien  return a ? AT_unsigned (a) : 0;
538490075Sobrien}
538550397Sobrien
538690075Sobrienstatic inline dw_die_ref
5387132718Skanget_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
538890075Sobrien{
538990075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
539090075Sobrien
539190075Sobrien  return a ? AT_ref (a) : NULL;
539250397Sobrien}
539350397Sobrien
5394169689Skanstatic inline struct dwarf_file_data *
5395169689Skanget_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5396169689Skan{
5397169689Skan  dw_attr_ref a = get_AT (die, attr_kind);
5398169689Skan
5399169689Skan  return a ? AT_file (a) : NULL;
5400169689Skan}
5401169689Skan
5402132718Skan/* Return TRUE if the language is C or C++.  */
5403132718Skan
5404132718Skanstatic inline bool
5405132718Skanis_c_family (void)
540650397Sobrien{
5407132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
540850397Sobrien
5409169689Skan  return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5410169689Skan	  || lang == DW_LANG_C99
5411169689Skan	  || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
541290075Sobrien}
541350397Sobrien
5414132718Skan/* Return TRUE if the language is C++.  */
5415132718Skan
5416132718Skanstatic inline bool
5417132718Skanis_cxx (void)
541890075Sobrien{
5419169689Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5420169689Skan
5421169689Skan  return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5422117395Skan}
542390075Sobrien
5424132718Skan/* Return TRUE if the language is Fortran.  */
5425132718Skan
5426132718Skanstatic inline bool
5427132718Skanis_fortran (void)
542850397Sobrien{
5429132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
543050397Sobrien
5431169689Skan  return (lang == DW_LANG_Fortran77
5432169689Skan	  || lang == DW_LANG_Fortran90
5433169689Skan	  || lang == DW_LANG_Fortran95);
543490075Sobrien}
543550397Sobrien
5436132718Skan/* Return TRUE if the language is Java.  */
5437132718Skan
5438132718Skanstatic inline bool
5439132718Skanis_java (void)
544090075Sobrien{
5441132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
544290075Sobrien
5443132718Skan  return lang == DW_LANG_Java;
544490075Sobrien}
544590075Sobrien
5446132718Skan/* Return TRUE if the language is Ada.  */
544790075Sobrien
5448132718Skanstatic inline bool
5449132718Skanis_ada (void)
545090075Sobrien{
5451132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
545290075Sobrien
5453132718Skan  return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5454132718Skan}
545590075Sobrien
545650397Sobrien/* Remove the specified attribute if present.  */
545750397Sobrien
545890075Sobrienstatic void
5459132718Skanremove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
546050397Sobrien{
5461169689Skan  dw_attr_ref a;
5462169689Skan  unsigned ix;
546350397Sobrien
5464169689Skan  if (! die)
5465169689Skan    return;
546650397Sobrien
5467169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5468169689Skan    if (a->dw_attr == attr_kind)
5469169689Skan      {
5470169689Skan	if (AT_class (a) == dw_val_class_str)
5471169689Skan	  if (a->dw_attr_val.v.val_str->refcount)
5472169689Skan	    a->dw_attr_val.v.val_str->refcount--;
5473169689Skan
5474169689Skan	/* VEC_ordered_remove should help reduce the number of abbrevs
5475169689Skan	   that are needed.  */
5476169689Skan	VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5477169689Skan	return;
5478169689Skan      }
547950397Sobrien}
548050397Sobrien
5481169689Skan/* Remove CHILD from its parent.  PREV must have the property that
5482169689Skan   PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5483146895Skan
5484146895Skanstatic void
5485169689Skanremove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5486146895Skan{
5487169689Skan  gcc_assert (child->die_parent == prev->die_parent);
5488169689Skan  gcc_assert (prev->die_sib == child);
5489169689Skan  if (prev == child)
5490146895Skan    {
5491169689Skan      gcc_assert (child->die_parent->die_child == child);
5492169689Skan      prev = NULL;
5493146895Skan    }
5494169689Skan  else
5495169689Skan    prev->die_sib = child->die_sib;
5496169689Skan  if (child->die_parent->die_child == child)
5497169689Skan    child->die_parent->die_child = prev;
5498146895Skan}
5499146895Skan
5500169689Skan/* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5501169689Skan   matches TAG.  */
550290075Sobrien
5503169689Skanstatic void
5504169689Skanremove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
550590075Sobrien{
5506169689Skan  dw_die_ref c;
5507169689Skan
5508169689Skan  c = die->die_child;
5509169689Skan  if (c) do {
5510169689Skan    dw_die_ref prev = c;
5511169689Skan    c = c->die_sib;
5512169689Skan    while (c->die_tag == tag)
5513169689Skan      {
5514169689Skan	remove_child_with_prev (c, prev);
5515169689Skan	/* Might have removed every child.  */
5516169689Skan	if (c == c->die_sib)
5517169689Skan	  return;
5518169689Skan	c = c->die_sib;
5519169689Skan      }
5520169689Skan  } while (c != die->die_child);
552190075Sobrien}
552290075Sobrien
5523169689Skan/* Add a CHILD_DIE as the last child of DIE.  */
552450397Sobrien
552590075Sobrienstatic void
5526169689Skanadd_child_die (dw_die_ref die, dw_die_ref child_die)
552750397Sobrien{
5528169689Skan  /* FIXME this should probably be an assert.  */
5529169689Skan  if (! die || ! child_die)
5530169689Skan    return;
5531169689Skan  gcc_assert (die != child_die);
553250397Sobrien
5533169689Skan  child_die->die_parent = die;
5534169689Skan  if (die->die_child)
553550397Sobrien    {
5536169689Skan      child_die->die_sib = die->die_child->die_sib;
5537169689Skan      die->die_child->die_sib = child_die;
553850397Sobrien    }
5539169689Skan  else
5540169689Skan    child_die->die_sib = child_die;
5541169689Skan  die->die_child = child_die;
554250397Sobrien}
554350397Sobrien
554490075Sobrien/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5545169689Skan   is the specification, to the end of PARENT's list of children.
5546169689Skan   This is done by removing and re-adding it.  */
554790075Sobrien
554890075Sobrienstatic void
5549132718Skansplice_child_die (dw_die_ref parent, dw_die_ref child)
555090075Sobrien{
5551169689Skan  dw_die_ref p;
555290075Sobrien
555390075Sobrien  /* We want the declaration DIE from inside the class, not the
555490075Sobrien     specification DIE at toplevel.  */
555590075Sobrien  if (child->die_parent != parent)
555690075Sobrien    {
555790075Sobrien      dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
555890075Sobrien
555990075Sobrien      if (tmp)
556090075Sobrien	child = tmp;
556150397Sobrien    }
556290075Sobrien
5563169689Skan  gcc_assert (child->die_parent == parent
5564169689Skan	      || (child->die_parent
5565169689Skan		  == get_AT_ref (parent, DW_AT_specification)));
5566169689Skan
5567169689Skan  for (p = child->die_parent->die_child; ; p = p->die_sib)
5568169689Skan    if (p->die_sib == child)
556990075Sobrien      {
5570169689Skan	remove_child_with_prev (child, p);
557190075Sobrien	break;
557290075Sobrien      }
557390075Sobrien
5574169689Skan  add_child_die (parent, child);
557550397Sobrien}
557650397Sobrien
557750397Sobrien/* Return a pointer to a newly created DIE node.  */
557850397Sobrien
557950397Sobrienstatic inline dw_die_ref
5580132718Skannew_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
558150397Sobrien{
5582132718Skan  dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
558350397Sobrien
558450397Sobrien  die->die_tag = tag_value;
558550397Sobrien
558650397Sobrien  if (parent_die != NULL)
558750397Sobrien    add_child_die (parent_die, die);
558850397Sobrien  else
558950397Sobrien    {
559050397Sobrien      limbo_die_node *limbo_node;
559150397Sobrien
5592132718Skan      limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
559350397Sobrien      limbo_node->die = die;
559490075Sobrien      limbo_node->created_for = t;
559550397Sobrien      limbo_node->next = limbo_die_list;
559650397Sobrien      limbo_die_list = limbo_node;
559750397Sobrien    }
559850397Sobrien
559950397Sobrien  return die;
560050397Sobrien}
560150397Sobrien
560250397Sobrien/* Return the DIE associated with the given type specifier.  */
560350397Sobrien
560450397Sobrienstatic inline dw_die_ref
5605132718Skanlookup_type_die (tree type)
560650397Sobrien{
5607117395Skan  return TYPE_SYMTAB_DIE (type);
560850397Sobrien}
560950397Sobrien
561050397Sobrien/* Equate a DIE to a given type specifier.  */
561150397Sobrien
561290075Sobrienstatic inline void
5613132718Skanequate_type_number_to_die (tree type, dw_die_ref type_die)
561450397Sobrien{
5615117395Skan  TYPE_SYMTAB_DIE (type) = type_die;
561650397Sobrien}
561750397Sobrien
5618169689Skan/* Returns a hash value for X (which really is a die_struct).  */
5619169689Skan
5620169689Skanstatic hashval_t
5621169689Skandecl_die_table_hash (const void *x)
5622169689Skan{
5623169689Skan  return (hashval_t) ((const dw_die_ref) x)->decl_id;
5624169689Skan}
5625169689Skan
5626169689Skan/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5627169689Skan
5628169689Skanstatic int
5629169689Skandecl_die_table_eq (const void *x, const void *y)
5630169689Skan{
5631169689Skan  return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5632169689Skan}
5633169689Skan
563450397Sobrien/* Return the DIE associated with a given declaration.  */
563550397Sobrien
563650397Sobrienstatic inline dw_die_ref
5637132718Skanlookup_decl_die (tree decl)
563850397Sobrien{
5639169689Skan  return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5640169689Skan}
564150397Sobrien
5642169689Skan/* Returns a hash value for X (which really is a var_loc_list).  */
5643169689Skan
5644169689Skanstatic hashval_t
5645169689Skandecl_loc_table_hash (const void *x)
5646169689Skan{
5647169689Skan  return (hashval_t) ((const var_loc_list *) x)->decl_id;
564850397Sobrien}
564950397Sobrien
5650169689Skan/* Return nonzero if decl_id of var_loc_list X is the same as
5651169689Skan   UID of decl *Y.  */
5652169689Skan
5653169689Skanstatic int
5654169689Skandecl_loc_table_eq (const void *x, const void *y)
5655169689Skan{
5656169689Skan  return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5657169689Skan}
5658169689Skan
5659169689Skan/* Return the var_loc list associated with a given declaration.  */
5660169689Skan
5661169689Skanstatic inline var_loc_list *
5662169689Skanlookup_decl_loc (tree decl)
5663169689Skan{
5664169689Skan  return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5665169689Skan}
5666169689Skan
566750397Sobrien/* Equate a DIE to a particular declaration.  */
566850397Sobrien
566950397Sobrienstatic void
5670132718Skanequate_decl_number_to_die (tree decl, dw_die_ref decl_die)
567150397Sobrien{
567290075Sobrien  unsigned int decl_id = DECL_UID (decl);
5673169689Skan  void **slot;
567450397Sobrien
5675169689Skan  slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5676169689Skan  *slot = decl_die;
5677169689Skan  decl_die->decl_id = decl_id;
5678169689Skan}
567950397Sobrien
5680169689Skan/* Add a variable location node to the linked list for DECL.  */
568150397Sobrien
5682169689Skanstatic void
5683169689Skanadd_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5684169689Skan{
5685169689Skan  unsigned int decl_id = DECL_UID (decl);
5686169689Skan  var_loc_list *temp;
5687169689Skan  void **slot;
5688169689Skan
5689169689Skan  slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5690169689Skan  if (*slot == NULL)
5691169689Skan    {
5692169689Skan      temp = ggc_alloc_cleared (sizeof (var_loc_list));
5693169689Skan      temp->decl_id = decl_id;
5694169689Skan      *slot = temp;
569550397Sobrien    }
5696169689Skan  else
5697169689Skan    temp = *slot;
569850397Sobrien
5699169689Skan  if (temp->last)
5700169689Skan    {
5701169689Skan      /* If the current location is the same as the end of the list,
5702169689Skan	 we have nothing to do.  */
5703169689Skan      if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5704169689Skan			NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5705169689Skan	{
5706169689Skan	  /* Add LOC to the end of list and update LAST.  */
5707169689Skan	  temp->last->next = loc;
5708169689Skan	  temp->last = loc;
5709169689Skan	}
5710169689Skan    }
5711169689Skan  /* Do not add empty location to the beginning of the list.  */
5712169689Skan  else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5713169689Skan    {
5714169689Skan      temp->first = loc;
5715169689Skan      temp->last = loc;
5716169689Skan    }
571750397Sobrien}
571850397Sobrien
571950397Sobrien/* Keep track of the number of spaces used to indent the
572050397Sobrien   output of the debugging routines that print the structure of
572150397Sobrien   the DIE internal representation.  */
572250397Sobrienstatic int print_indent;
572350397Sobrien
572450397Sobrien/* Indent the line the number of spaces given by print_indent.  */
572550397Sobrien
572650397Sobrienstatic inline void
5727132718Skanprint_spaces (FILE *outfile)
572850397Sobrien{
572950397Sobrien  fprintf (outfile, "%*s", print_indent, "");
573050397Sobrien}
573150397Sobrien
573250397Sobrien/* Print the information associated with a given DIE, and its children.
573350397Sobrien   This routine is a debugging aid only.  */
573450397Sobrien
573550397Sobrienstatic void
5736132718Skanprint_die (dw_die_ref die, FILE *outfile)
573750397Sobrien{
573890075Sobrien  dw_attr_ref a;
573990075Sobrien  dw_die_ref c;
5740169689Skan  unsigned ix;
574150397Sobrien
574250397Sobrien  print_spaces (outfile);
5743259948Spfg  fprintf (outfile, "DIE %4ld: %s\n",
574450397Sobrien	   die->die_offset, dwarf_tag_name (die->die_tag));
574550397Sobrien  print_spaces (outfile);
574650397Sobrien  fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5747259948Spfg  fprintf (outfile, " offset: %ld\n", die->die_offset);
574850397Sobrien
5749169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
575050397Sobrien    {
575150397Sobrien      print_spaces (outfile);
575250397Sobrien      fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
575350397Sobrien
575490075Sobrien      switch (AT_class (a))
575550397Sobrien	{
575650397Sobrien	case dw_val_class_addr:
575750397Sobrien	  fprintf (outfile, "address");
575850397Sobrien	  break;
575990075Sobrien	case dw_val_class_offset:
576090075Sobrien	  fprintf (outfile, "offset");
576190075Sobrien	  break;
576250397Sobrien	case dw_val_class_loc:
576350397Sobrien	  fprintf (outfile, "location descriptor");
576450397Sobrien	  break;
576590075Sobrien	case dw_val_class_loc_list:
576690075Sobrien	  fprintf (outfile, "location list -> label:%s",
576790075Sobrien		   AT_loc_list (a)->ll_symbol);
576890075Sobrien	  break;
576990075Sobrien	case dw_val_class_range_list:
577090075Sobrien	  fprintf (outfile, "range list");
577190075Sobrien	  break;
577250397Sobrien	case dw_val_class_const:
5773132718Skan	  fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
577450397Sobrien	  break;
577550397Sobrien	case dw_val_class_unsigned_const:
5776132718Skan	  fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
577750397Sobrien	  break;
577850397Sobrien	case dw_val_class_long_long:
577950397Sobrien	  fprintf (outfile, "constant (%lu,%lu)",
578090075Sobrien		   a->dw_attr_val.v.val_long_long.hi,
578190075Sobrien		   a->dw_attr_val.v.val_long_long.low);
578250397Sobrien	  break;
5783132718Skan	case dw_val_class_vec:
5784132718Skan	  fprintf (outfile, "floating-point or vector constant");
578550397Sobrien	  break;
578650397Sobrien	case dw_val_class_flag:
578790075Sobrien	  fprintf (outfile, "%u", AT_flag (a));
578850397Sobrien	  break;
578950397Sobrien	case dw_val_class_die_ref:
579090075Sobrien	  if (AT_ref (a) != NULL)
579190075Sobrien	    {
579290075Sobrien	      if (AT_ref (a)->die_symbol)
579390075Sobrien		fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
579490075Sobrien	      else
5795259948Spfg		fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
579690075Sobrien	    }
579750397Sobrien	  else
579850397Sobrien	    fprintf (outfile, "die -> <null>");
579950397Sobrien	  break;
580050397Sobrien	case dw_val_class_lbl_id:
5801169689Skan	case dw_val_class_lineptr:
5802169689Skan	case dw_val_class_macptr:
580390075Sobrien	  fprintf (outfile, "label: %s", AT_lbl (a));
580450397Sobrien	  break;
580550397Sobrien	case dw_val_class_str:
580690075Sobrien	  if (AT_string (a) != NULL)
580790075Sobrien	    fprintf (outfile, "\"%s\"", AT_string (a));
580850397Sobrien	  else
580950397Sobrien	    fprintf (outfile, "<null>");
581050397Sobrien	  break;
5811169689Skan	case dw_val_class_file:
5812169689Skan	  fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5813169689Skan		   AT_file (a)->emitted_number);
5814169689Skan	  break;
581550397Sobrien	default:
581650397Sobrien	  break;
581750397Sobrien	}
581850397Sobrien
581950397Sobrien      fprintf (outfile, "\n");
582050397Sobrien    }
582150397Sobrien
582250397Sobrien  if (die->die_child != NULL)
582350397Sobrien    {
582450397Sobrien      print_indent += 4;
5825169689Skan      FOR_EACH_CHILD (die, c, print_die (c, outfile));
582650397Sobrien      print_indent -= 4;
582750397Sobrien    }
582890075Sobrien  if (print_indent == 0)
582990075Sobrien    fprintf (outfile, "\n");
583050397Sobrien}
583150397Sobrien
583250397Sobrien/* Print the contents of the source code line number correspondence table.
583350397Sobrien   This routine is a debugging aid only.  */
583450397Sobrien
583550397Sobrienstatic void
5836132718Skanprint_dwarf_line_table (FILE *outfile)
583750397Sobrien{
583890075Sobrien  unsigned i;
583990075Sobrien  dw_line_info_ref line_info;
584050397Sobrien
584150397Sobrien  fprintf (outfile, "\n\nDWARF source line information\n");
584290075Sobrien  for (i = 1; i < line_info_table_in_use; i++)
584350397Sobrien    {
584450397Sobrien      line_info = &line_info_table[i];
5845169689Skan      fprintf (outfile, "%5d: %4ld %6ld\n", i,
5846169689Skan	       line_info->dw_file_num,
5847169689Skan	       line_info->dw_line_num);
584850397Sobrien    }
584950397Sobrien
585050397Sobrien  fprintf (outfile, "\n\n");
585150397Sobrien}
585250397Sobrien
585350397Sobrien/* Print the information collected for a given DIE.  */
585450397Sobrien
585550397Sobrienvoid
5856132718Skandebug_dwarf_die (dw_die_ref die)
585750397Sobrien{
585850397Sobrien  print_die (die, stderr);
585950397Sobrien}
586050397Sobrien
586150397Sobrien/* Print all DWARF information collected for the compilation unit.
586250397Sobrien   This routine is a debugging aid only.  */
586350397Sobrien
586450397Sobrienvoid
5865132718Skandebug_dwarf (void)
586650397Sobrien{
586750397Sobrien  print_indent = 0;
586850397Sobrien  print_die (comp_unit_die, stderr);
586990075Sobrien  if (! DWARF2_ASM_LINE_DEBUG_INFO)
587090075Sobrien    print_dwarf_line_table (stderr);
587150397Sobrien}
587250397Sobrien
587390075Sobrien/* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
587490075Sobrien   for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
587590075Sobrien   DIE that marks the start of the DIEs for this include file.  */
587690075Sobrien
587790075Sobrienstatic dw_die_ref
5878132718Skanpush_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
587990075Sobrien{
588090075Sobrien  const char *filename = get_AT_string (bincl_die, DW_AT_name);
588190075Sobrien  dw_die_ref new_unit = gen_compile_unit_die (filename);
588290075Sobrien
588390075Sobrien  new_unit->die_sib = old_unit;
588490075Sobrien  return new_unit;
588590075Sobrien}
588690075Sobrien
588790075Sobrien/* Close an include-file CU and reopen the enclosing one.  */
588890075Sobrien
588990075Sobrienstatic dw_die_ref
5890132718Skanpop_compile_unit (dw_die_ref old_unit)
589190075Sobrien{
589290075Sobrien  dw_die_ref new_unit = old_unit->die_sib;
589390075Sobrien
589490075Sobrien  old_unit->die_sib = NULL;
589590075Sobrien  return new_unit;
589690075Sobrien}
589790075Sobrien
589890075Sobrien#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
589990075Sobrien#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
590090075Sobrien
590190075Sobrien/* Calculate the checksum of a location expression.  */
590290075Sobrien
590390075Sobrienstatic inline void
5904132718Skanloc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
590590075Sobrien{
590690075Sobrien  CHECKSUM (loc->dw_loc_opc);
590790075Sobrien  CHECKSUM (loc->dw_loc_oprnd1);
590890075Sobrien  CHECKSUM (loc->dw_loc_oprnd2);
590990075Sobrien}
591090075Sobrien
591190075Sobrien/* Calculate the checksum of an attribute.  */
591290075Sobrien
591390075Sobrienstatic void
5914132718Skanattr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
591590075Sobrien{
591690075Sobrien  dw_loc_descr_ref loc;
591790075Sobrien  rtx r;
591890075Sobrien
591990075Sobrien  CHECKSUM (at->dw_attr);
592090075Sobrien
5921169689Skan  /* We don't care that this was compiled with a different compiler
5922169689Skan     snapshot; if the output is the same, that's what matters.  */
5923169689Skan  if (at->dw_attr == DW_AT_producer)
592490075Sobrien    return;
592590075Sobrien
592690075Sobrien  switch (AT_class (at))
592790075Sobrien    {
592890075Sobrien    case dw_val_class_const:
592990075Sobrien      CHECKSUM (at->dw_attr_val.v.val_int);
593090075Sobrien      break;
593190075Sobrien    case dw_val_class_unsigned_const:
593290075Sobrien      CHECKSUM (at->dw_attr_val.v.val_unsigned);
593390075Sobrien      break;
593490075Sobrien    case dw_val_class_long_long:
593590075Sobrien      CHECKSUM (at->dw_attr_val.v.val_long_long);
593690075Sobrien      break;
5937132718Skan    case dw_val_class_vec:
5938132718Skan      CHECKSUM (at->dw_attr_val.v.val_vec);
593990075Sobrien      break;
594090075Sobrien    case dw_val_class_flag:
594190075Sobrien      CHECKSUM (at->dw_attr_val.v.val_flag);
594290075Sobrien      break;
594390075Sobrien    case dw_val_class_str:
594490075Sobrien      CHECKSUM_STRING (AT_string (at));
594590075Sobrien      break;
594690075Sobrien
594790075Sobrien    case dw_val_class_addr:
594890075Sobrien      r = AT_addr (at);
5949169689Skan      gcc_assert (GET_CODE (r) == SYMBOL_REF);
5950169689Skan      CHECKSUM_STRING (XSTR (r, 0));
595190075Sobrien      break;
595290075Sobrien
595390075Sobrien    case dw_val_class_offset:
595490075Sobrien      CHECKSUM (at->dw_attr_val.v.val_offset);
595590075Sobrien      break;
595690075Sobrien
595790075Sobrien    case dw_val_class_loc:
595890075Sobrien      for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
595990075Sobrien	loc_checksum (loc, ctx);
596090075Sobrien      break;
596190075Sobrien
596290075Sobrien    case dw_val_class_die_ref:
5963117395Skan      die_checksum (AT_ref (at), ctx, mark);
5964117395Skan      break;
596590075Sobrien
596690075Sobrien    case dw_val_class_fde_ref:
596790075Sobrien    case dw_val_class_lbl_id:
5968169689Skan    case dw_val_class_lineptr:
5969169689Skan    case dw_val_class_macptr:
597090075Sobrien      break;
597190075Sobrien
5972169689Skan    case dw_val_class_file:
5973169689Skan      CHECKSUM_STRING (AT_file (at)->filename);
5974169689Skan      break;
5975169689Skan
597690075Sobrien    default:
597790075Sobrien      break;
597890075Sobrien    }
597990075Sobrien}
598090075Sobrien
598190075Sobrien/* Calculate the checksum of a DIE.  */
598290075Sobrien
598390075Sobrienstatic void
5984132718Skandie_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
598590075Sobrien{
598690075Sobrien  dw_die_ref c;
598790075Sobrien  dw_attr_ref a;
5988169689Skan  unsigned ix;
598990075Sobrien
5990117395Skan  /* To avoid infinite recursion.  */
5991117395Skan  if (die->die_mark)
5992117395Skan    {
5993117395Skan      CHECKSUM (die->die_mark);
5994117395Skan      return;
5995117395Skan    }
5996117395Skan  die->die_mark = ++(*mark);
5997117395Skan
599890075Sobrien  CHECKSUM (die->die_tag);
599990075Sobrien
6000169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6001117395Skan    attr_checksum (a, ctx, mark);
600290075Sobrien
6003169689Skan  FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
600490075Sobrien}
600590075Sobrien
600690075Sobrien#undef CHECKSUM
600790075Sobrien#undef CHECKSUM_STRING
600890075Sobrien
6009117395Skan/* Do the location expressions look same?  */
6010117395Skanstatic inline int
6011132718Skansame_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6012117395Skan{
6013117395Skan  return loc1->dw_loc_opc == loc2->dw_loc_opc
6014117395Skan	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6015117395Skan	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6016117395Skan}
6017117395Skan
6018117395Skan/* Do the values look the same?  */
6019117395Skanstatic int
6020132718Skansame_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6021117395Skan{
6022117395Skan  dw_loc_descr_ref loc1, loc2;
6023117395Skan  rtx r1, r2;
6024117395Skan
6025117395Skan  if (v1->val_class != v2->val_class)
6026117395Skan    return 0;
6027117395Skan
6028117395Skan  switch (v1->val_class)
6029117395Skan    {
6030117395Skan    case dw_val_class_const:
6031117395Skan      return v1->v.val_int == v2->v.val_int;
6032117395Skan    case dw_val_class_unsigned_const:
6033117395Skan      return v1->v.val_unsigned == v2->v.val_unsigned;
6034117395Skan    case dw_val_class_long_long:
6035117395Skan      return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6036132718Skan	     && v1->v.val_long_long.low == v2->v.val_long_long.low;
6037132718Skan    case dw_val_class_vec:
6038132718Skan      if (v1->v.val_vec.length != v2->v.val_vec.length
6039132718Skan	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6040117395Skan	return 0;
6041132718Skan      if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6042132718Skan		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
6043132718Skan	return 0;
6044117395Skan      return 1;
6045117395Skan    case dw_val_class_flag:
6046117395Skan      return v1->v.val_flag == v2->v.val_flag;
6047117395Skan    case dw_val_class_str:
6048132718Skan      return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6049117395Skan
6050117395Skan    case dw_val_class_addr:
6051117395Skan      r1 = v1->v.val_addr;
6052117395Skan      r2 = v2->v.val_addr;
6053117395Skan      if (GET_CODE (r1) != GET_CODE (r2))
6054117395Skan	return 0;
6055169689Skan      gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6056169689Skan      return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6057117395Skan
6058117395Skan    case dw_val_class_offset:
6059117395Skan      return v1->v.val_offset == v2->v.val_offset;
6060117395Skan
6061117395Skan    case dw_val_class_loc:
6062117395Skan      for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6063117395Skan	   loc1 && loc2;
6064117395Skan	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6065117395Skan	if (!same_loc_p (loc1, loc2, mark))
6066117395Skan	  return 0;
6067117395Skan      return !loc1 && !loc2;
6068117395Skan
6069117395Skan    case dw_val_class_die_ref:
6070117395Skan      return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6071117395Skan
6072117395Skan    case dw_val_class_fde_ref:
6073117395Skan    case dw_val_class_lbl_id:
6074169689Skan    case dw_val_class_lineptr:
6075169689Skan    case dw_val_class_macptr:
6076117395Skan      return 1;
6077117395Skan
6078169689Skan    case dw_val_class_file:
6079169689Skan      return v1->v.val_file == v2->v.val_file;
6080169689Skan
6081117395Skan    default:
6082117395Skan      return 1;
6083117395Skan    }
6084117395Skan}
6085117395Skan
6086117395Skan/* Do the attributes look the same?  */
6087117395Skan
6088117395Skanstatic int
6089132718Skansame_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6090117395Skan{
6091117395Skan  if (at1->dw_attr != at2->dw_attr)
6092117395Skan    return 0;
6093117395Skan
6094169689Skan  /* We don't care that this was compiled with a different compiler
6095169689Skan     snapshot; if the output is the same, that's what matters. */
6096169689Skan  if (at1->dw_attr == DW_AT_producer)
6097117395Skan    return 1;
6098117395Skan
6099117395Skan  return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6100117395Skan}
6101117395Skan
6102117395Skan/* Do the dies look the same?  */
6103117395Skan
6104117395Skanstatic int
6105132718Skansame_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6106117395Skan{
6107117395Skan  dw_die_ref c1, c2;
6108169689Skan  dw_attr_ref a1;
6109169689Skan  unsigned ix;
6110117395Skan
6111117395Skan  /* To avoid infinite recursion.  */
6112117395Skan  if (die1->die_mark)
6113117395Skan    return die1->die_mark == die2->die_mark;
6114117395Skan  die1->die_mark = die2->die_mark = ++(*mark);
6115117395Skan
6116117395Skan  if (die1->die_tag != die2->die_tag)
6117117395Skan    return 0;
6118117395Skan
6119169689Skan  if (VEC_length (dw_attr_node, die1->die_attr)
6120169689Skan      != VEC_length (dw_attr_node, die2->die_attr))
6121117395Skan    return 0;
6122169689Skan
6123169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6124169689Skan    if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6125117395Skan      return 0;
6126117395Skan
6127169689Skan  c1 = die1->die_child;
6128169689Skan  c2 = die2->die_child;
6129169689Skan  if (! c1)
6130169689Skan    {
6131169689Skan      if (c2)
6132169689Skan	return 0;
6133169689Skan    }
6134169689Skan  else
6135169689Skan    for (;;)
6136169689Skan      {
6137169689Skan	if (!same_die_p (c1, c2, mark))
6138169689Skan	  return 0;
6139169689Skan	c1 = c1->die_sib;
6140169689Skan	c2 = c2->die_sib;
6141169689Skan	if (c1 == die1->die_child)
6142169689Skan	  {
6143169689Skan	    if (c2 == die2->die_child)
6144169689Skan	      break;
6145169689Skan	    else
6146169689Skan	      return 0;
6147169689Skan	  }
6148169689Skan    }
6149169689Skan
6150117395Skan  return 1;
6151117395Skan}
6152117395Skan
6153117395Skan/* Do the dies look the same?  Wrapper around same_die_p.  */
6154117395Skan
6155117395Skanstatic int
6156132718Skansame_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6157117395Skan{
6158117395Skan  int mark = 0;
6159117395Skan  int ret = same_die_p (die1, die2, &mark);
6160117395Skan
6161117395Skan  unmark_all_dies (die1);
6162117395Skan  unmark_all_dies (die2);
6163117395Skan
6164117395Skan  return ret;
6165117395Skan}
6166117395Skan
616790075Sobrien/* The prefix to attach to symbols on DIEs in the current comdat debug
616890075Sobrien   info section.  */
616990075Sobrienstatic char *comdat_symbol_id;
617090075Sobrien
617190075Sobrien/* The index of the current symbol within the current comdat CU.  */
617290075Sobrienstatic unsigned int comdat_symbol_number;
617390075Sobrien
617490075Sobrien/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
617590075Sobrien   children, and set comdat_symbol_id accordingly.  */
617690075Sobrien
617790075Sobrienstatic void
6178132718Skancompute_section_prefix (dw_die_ref unit_die)
617990075Sobrien{
6180117395Skan  const char *die_name = get_AT_string (unit_die, DW_AT_name);
6181117395Skan  const char *base = die_name ? lbasename (die_name) : "anonymous";
6182132718Skan  char *name = alloca (strlen (base) + 64);
618390075Sobrien  char *p;
6184117395Skan  int i, mark;
618590075Sobrien  unsigned char checksum[16];
618690075Sobrien  struct md5_ctx ctx;
618790075Sobrien
618890075Sobrien  /* Compute the checksum of the DIE, then append part of it as hex digits to
618990075Sobrien     the name filename of the unit.  */
619090075Sobrien
619190075Sobrien  md5_init_ctx (&ctx);
6192117395Skan  mark = 0;
6193117395Skan  die_checksum (unit_die, &ctx, &mark);
6194117395Skan  unmark_all_dies (unit_die);
619590075Sobrien  md5_finish_ctx (&ctx, checksum);
619690075Sobrien
619790075Sobrien  sprintf (name, "%s.", base);
619890075Sobrien  clean_symbol_name (name);
619990075Sobrien
620090075Sobrien  p = name + strlen (name);
620190075Sobrien  for (i = 0; i < 4; i++)
620290075Sobrien    {
620390075Sobrien      sprintf (p, "%.2x", checksum[i]);
620490075Sobrien      p += 2;
620590075Sobrien    }
620690075Sobrien
620790075Sobrien  comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
620890075Sobrien  comdat_symbol_number = 0;
620990075Sobrien}
621090075Sobrien
621190075Sobrien/* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
621290075Sobrien
621390075Sobrienstatic int
6214132718Skanis_type_die (dw_die_ref die)
621590075Sobrien{
621690075Sobrien  switch (die->die_tag)
621790075Sobrien    {
621890075Sobrien    case DW_TAG_array_type:
621990075Sobrien    case DW_TAG_class_type:
622090075Sobrien    case DW_TAG_enumeration_type:
622190075Sobrien    case DW_TAG_pointer_type:
622290075Sobrien    case DW_TAG_reference_type:
622390075Sobrien    case DW_TAG_string_type:
622490075Sobrien    case DW_TAG_structure_type:
622590075Sobrien    case DW_TAG_subroutine_type:
622690075Sobrien    case DW_TAG_union_type:
622790075Sobrien    case DW_TAG_ptr_to_member_type:
622890075Sobrien    case DW_TAG_set_type:
622990075Sobrien    case DW_TAG_subrange_type:
623090075Sobrien    case DW_TAG_base_type:
623190075Sobrien    case DW_TAG_const_type:
623290075Sobrien    case DW_TAG_file_type:
623390075Sobrien    case DW_TAG_packed_type:
623490075Sobrien    case DW_TAG_volatile_type:
6235117395Skan    case DW_TAG_typedef:
623690075Sobrien      return 1;
623790075Sobrien    default:
623890075Sobrien      return 0;
623990075Sobrien    }
624090075Sobrien}
624190075Sobrien
624290075Sobrien/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
624390075Sobrien   Basically, we want to choose the bits that are likely to be shared between
624490075Sobrien   compilations (types) and leave out the bits that are specific to individual
624590075Sobrien   compilations (functions).  */
624690075Sobrien
624790075Sobrienstatic int
6248132718Skanis_comdat_die (dw_die_ref c)
624990075Sobrien{
625090075Sobrien  /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
625190075Sobrien     we do for stabs.  The advantage is a greater likelihood of sharing between
625290075Sobrien     objects that don't include headers in the same order (and therefore would
625390075Sobrien     put the base types in a different comdat).  jason 8/28/00 */
625490075Sobrien
625590075Sobrien  if (c->die_tag == DW_TAG_base_type)
625690075Sobrien    return 0;
625790075Sobrien
625890075Sobrien  if (c->die_tag == DW_TAG_pointer_type
625990075Sobrien      || c->die_tag == DW_TAG_reference_type
626090075Sobrien      || c->die_tag == DW_TAG_const_type
626190075Sobrien      || c->die_tag == DW_TAG_volatile_type)
626290075Sobrien    {
626390075Sobrien      dw_die_ref t = get_AT_ref (c, DW_AT_type);
626490075Sobrien
626590075Sobrien      return t ? is_comdat_die (t) : 0;
626690075Sobrien    }
626790075Sobrien
626890075Sobrien  return is_type_die (c);
626990075Sobrien}
627090075Sobrien
627190075Sobrien/* Returns 1 iff C is the sort of DIE that might be referred to from another
627290075Sobrien   compilation unit.  */
627390075Sobrien
627490075Sobrienstatic int
6275132718Skanis_symbol_die (dw_die_ref c)
627690075Sobrien{
627790075Sobrien  return (is_type_die (c)
6278117395Skan	  || (get_AT (c, DW_AT_declaration)
6279169689Skan	      && !get_AT (c, DW_AT_specification))
6280169689Skan	  || c->die_tag == DW_TAG_namespace);
628190075Sobrien}
628290075Sobrien
628390075Sobrienstatic char *
6284132718Skangen_internal_sym (const char *prefix)
628590075Sobrien{
628690075Sobrien  char buf[256];
628790075Sobrien
628890075Sobrien  ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
628990075Sobrien  return xstrdup (buf);
629090075Sobrien}
629190075Sobrien
629290075Sobrien/* Assign symbols to all worthy DIEs under DIE.  */
629390075Sobrien
629490075Sobrienstatic void
6295132718Skanassign_symbol_names (dw_die_ref die)
629690075Sobrien{
629790075Sobrien  dw_die_ref c;
629890075Sobrien
629990075Sobrien  if (is_symbol_die (die))
630090075Sobrien    {
630190075Sobrien      if (comdat_symbol_id)
630290075Sobrien	{
630390075Sobrien	  char *p = alloca (strlen (comdat_symbol_id) + 64);
630490075Sobrien
630590075Sobrien	  sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
630690075Sobrien		   comdat_symbol_id, comdat_symbol_number++);
630790075Sobrien	  die->die_symbol = xstrdup (p);
630890075Sobrien	}
630990075Sobrien      else
631090075Sobrien	die->die_symbol = gen_internal_sym ("LDIE");
631190075Sobrien    }
631290075Sobrien
6313169689Skan  FOR_EACH_CHILD (die, c, assign_symbol_names (c));
631490075Sobrien}
631590075Sobrien
6316117395Skanstruct cu_hash_table_entry
6317117395Skan{
6318117395Skan  dw_die_ref cu;
6319117395Skan  unsigned min_comdat_num, max_comdat_num;
6320117395Skan  struct cu_hash_table_entry *next;
6321117395Skan};
6322117395Skan
6323117395Skan/* Routines to manipulate hash table of CUs.  */
6324117395Skanstatic hashval_t
6325132718Skanhtab_cu_hash (const void *of)
6326117395Skan{
6327117395Skan  const struct cu_hash_table_entry *entry = of;
6328117395Skan
6329117395Skan  return htab_hash_string (entry->cu->die_symbol);
6330117395Skan}
6331117395Skan
6332117395Skanstatic int
6333132718Skanhtab_cu_eq (const void *of1, const void *of2)
6334117395Skan{
6335117395Skan  const struct cu_hash_table_entry *entry1 = of1;
6336117395Skan  const struct die_struct *entry2 = of2;
6337117395Skan
6338117395Skan  return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6339117395Skan}
6340117395Skan
6341117395Skanstatic void
6342132718Skanhtab_cu_del (void *what)
6343117395Skan{
6344117395Skan  struct cu_hash_table_entry *next, *entry = what;
6345117395Skan
6346117395Skan  while (entry)
6347117395Skan    {
6348117395Skan      next = entry->next;
6349117395Skan      free (entry);
6350117395Skan      entry = next;
6351117395Skan    }
6352117395Skan}
6353117395Skan
6354117395Skan/* Check whether we have already seen this CU and set up SYM_NUM
6355117395Skan   accordingly.  */
6356117395Skanstatic int
6357132718Skancheck_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6358117395Skan{
6359117395Skan  struct cu_hash_table_entry dummy;
6360117395Skan  struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6361117395Skan
6362117395Skan  dummy.max_comdat_num = 0;
6363117395Skan
6364117395Skan  slot = (struct cu_hash_table_entry **)
6365117395Skan    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6366117395Skan	INSERT);
6367117395Skan  entry = *slot;
6368117395Skan
6369117395Skan  for (; entry; last = entry, entry = entry->next)
6370117395Skan    {
6371117395Skan      if (same_die_p_wrap (cu, entry->cu))
6372117395Skan	break;
6373117395Skan    }
6374117395Skan
6375117395Skan  if (entry)
6376117395Skan    {
6377117395Skan      *sym_num = entry->min_comdat_num;
6378117395Skan      return 1;
6379117395Skan    }
6380117395Skan
6381169689Skan  entry = XCNEW (struct cu_hash_table_entry);
6382117395Skan  entry->cu = cu;
6383117395Skan  entry->min_comdat_num = *sym_num = last->max_comdat_num;
6384117395Skan  entry->next = *slot;
6385117395Skan  *slot = entry;
6386117395Skan
6387117395Skan  return 0;
6388117395Skan}
6389117395Skan
6390117395Skan/* Record SYM_NUM to record of CU in HTABLE.  */
6391117395Skanstatic void
6392132718Skanrecord_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6393117395Skan{
6394117395Skan  struct cu_hash_table_entry **slot, *entry;
6395117395Skan
6396117395Skan  slot = (struct cu_hash_table_entry **)
6397117395Skan    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6398117395Skan	NO_INSERT);
6399117395Skan  entry = *slot;
6400117395Skan
6401117395Skan  entry->max_comdat_num = sym_num;
6402117395Skan}
6403117395Skan
640490075Sobrien/* Traverse the DIE (which is always comp_unit_die), and set up
640590075Sobrien   additional compilation units for each of the include files we see
640690075Sobrien   bracketed by BINCL/EINCL.  */
640790075Sobrien
640890075Sobrienstatic void
6409132718Skanbreak_out_includes (dw_die_ref die)
641090075Sobrien{
6411169689Skan  dw_die_ref c;
641290075Sobrien  dw_die_ref unit = NULL;
6413117395Skan  limbo_die_node *node, **pnode;
6414117395Skan  htab_t cu_hash_table;
641590075Sobrien
6416169689Skan  c = die->die_child;
6417169689Skan  if (c) do {
6418169689Skan    dw_die_ref prev = c;
6419169689Skan    c = c->die_sib;
6420169689Skan    while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6421169689Skan	   || (unit && is_comdat_die (c)))
6422169689Skan      {
6423169689Skan	dw_die_ref next = c->die_sib;
642490075Sobrien
6425169689Skan	/* This DIE is for a secondary CU; remove it from the main one.  */
6426169689Skan	remove_child_with_prev (c, prev);
6427169689Skan
6428169689Skan	if (c->die_tag == DW_TAG_GNU_BINCL)
6429169689Skan	  unit = push_new_compile_unit (unit, c);
6430169689Skan	else if (c->die_tag == DW_TAG_GNU_EINCL)
6431169689Skan	  unit = pop_compile_unit (unit);
6432169689Skan	else
6433169689Skan	  add_child_die (unit, c);
6434169689Skan	c = next;
6435169689Skan	if (c == die->die_child)
6436169689Skan	  break;
6437169689Skan      }
6438169689Skan  } while (c != die->die_child);
643990075Sobrien
644090075Sobrien#if 0
644190075Sobrien  /* We can only use this in debugging, since the frontend doesn't check
644290075Sobrien     to make sure that we leave every include file we enter.  */
6443169689Skan  gcc_assert (!unit);
644490075Sobrien#endif
644590075Sobrien
644690075Sobrien  assign_symbol_names (die);
6447117395Skan  cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6448117395Skan  for (node = limbo_die_list, pnode = &limbo_die_list;
6449117395Skan       node;
6450117395Skan       node = node->next)
645190075Sobrien    {
6452117395Skan      int is_dupl;
6453117395Skan
645490075Sobrien      compute_section_prefix (node->die);
6455117395Skan      is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6456117395Skan			&comdat_symbol_number);
645790075Sobrien      assign_symbol_names (node->die);
6458117395Skan      if (is_dupl)
6459117395Skan	*pnode = node->next;
6460117395Skan      else
6461132718Skan	{
6462117395Skan	  pnode = &node->next;
6463117395Skan	  record_comdat_symbol_number (node->die, cu_hash_table,
6464117395Skan		comdat_symbol_number);
6465117395Skan	}
646690075Sobrien    }
6467117395Skan  htab_delete (cu_hash_table);
646890075Sobrien}
646990075Sobrien
647090075Sobrien/* Traverse the DIE and add a sibling attribute if it may have the
647190075Sobrien   effect of speeding up access to siblings.  To save some space,
647290075Sobrien   avoid generating sibling attributes for DIE's without children.  */
647390075Sobrien
647490075Sobrienstatic void
6475132718Skanadd_sibling_attributes (dw_die_ref die)
647690075Sobrien{
647790075Sobrien  dw_die_ref c;
647890075Sobrien
6479169689Skan  if (! die->die_child)
6480169689Skan    return;
6481169689Skan
6482169689Skan  if (die->die_parent && die != die->die_parent->die_child)
648390075Sobrien    add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
648490075Sobrien
6485169689Skan  FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
648650397Sobrien}
648750397Sobrien
648890075Sobrien/* Output all location lists for the DIE and its children.  */
648950397Sobrien
649050397Sobrienstatic void
6491132718Skanoutput_location_lists (dw_die_ref die)
649290075Sobrien{
649390075Sobrien  dw_die_ref c;
6494169689Skan  dw_attr_ref a;
6495169689Skan  unsigned ix;
649690075Sobrien
6497169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6498169689Skan    if (AT_class (a) == dw_val_class_loc_list)
6499169689Skan      output_loc_list (AT_loc_list (a));
650090075Sobrien
6501169689Skan  FOR_EACH_CHILD (die, c, output_location_lists (c));
650290075Sobrien}
6503117395Skan
650490075Sobrien/* The format of each DIE (and its attribute value pairs) is encoded in an
650590075Sobrien   abbreviation table.  This routine builds the abbreviation table and assigns
650690075Sobrien   a unique abbreviation id for each abbreviation entry.  The children of each
650790075Sobrien   die are visited recursively.  */
650890075Sobrien
650990075Sobrienstatic void
6510132718Skanbuild_abbrev_table (dw_die_ref die)
651150397Sobrien{
651290075Sobrien  unsigned long abbrev_id;
651390075Sobrien  unsigned int n_alloc;
651490075Sobrien  dw_die_ref c;
6515169689Skan  dw_attr_ref a;
6516169689Skan  unsigned ix;
651790075Sobrien
651890075Sobrien  /* Scan the DIE references, and mark as external any that refer to
651990075Sobrien     DIEs from other CUs (i.e. those which are not marked).  */
6520169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6521169689Skan    if (AT_class (a) == dw_val_class_die_ref
6522169689Skan	&& AT_ref (a)->die_mark == 0)
652390075Sobrien      {
6524169689Skan	gcc_assert (AT_ref (a)->die_symbol);
652590075Sobrien
6526169689Skan	set_AT_ref_external (a, 1);
652790075Sobrien      }
652890075Sobrien
652950397Sobrien  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
653050397Sobrien    {
653190075Sobrien      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6532169689Skan      dw_attr_ref die_a, abbrev_a;
6533169689Skan      unsigned ix;
6534169689Skan      bool ok = true;
6535169689Skan
6536169689Skan      if (abbrev->die_tag != die->die_tag)
6537169689Skan	continue;
6538169689Skan      if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6539169689Skan	continue;
6540169689Skan
6541169689Skan      if (VEC_length (dw_attr_node, abbrev->die_attr)
6542169689Skan	  != VEC_length (dw_attr_node, die->die_attr))
6543169689Skan	continue;
6544169689Skan
6545169689Skan      for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
654650397Sobrien	{
6547169689Skan	  abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6548169689Skan	  if ((abbrev_a->dw_attr != die_a->dw_attr)
6549169689Skan	      || (value_format (abbrev_a) != value_format (die_a)))
655050397Sobrien	    {
6551169689Skan	      ok = false;
6552169689Skan	      break;
655350397Sobrien	    }
655450397Sobrien	}
6555169689Skan      if (ok)
6556169689Skan	break;
655750397Sobrien    }
655850397Sobrien
655950397Sobrien  if (abbrev_id >= abbrev_die_table_in_use)
656050397Sobrien    {
656150397Sobrien      if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
656250397Sobrien	{
656350397Sobrien	  n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6564132718Skan	  abbrev_die_table = ggc_realloc (abbrev_die_table,
6565132718Skan					  sizeof (dw_die_ref) * n_alloc);
656650397Sobrien
6567132718Skan	  memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
656850397Sobrien		 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
656950397Sobrien	  abbrev_die_table_allocated = n_alloc;
657050397Sobrien	}
657150397Sobrien
657250397Sobrien      ++abbrev_die_table_in_use;
657350397Sobrien      abbrev_die_table[abbrev_id] = die;
657450397Sobrien    }
657550397Sobrien
657650397Sobrien  die->die_abbrev = abbrev_id;
6577169689Skan  FOR_EACH_CHILD (die, c, build_abbrev_table (c));
657850397Sobrien}
657950397Sobrien
658050397Sobrien/* Return the power-of-two number of bytes necessary to represent VALUE.  */
658150397Sobrien
658250397Sobrienstatic int
6583132718Skanconstant_size (long unsigned int value)
658450397Sobrien{
658550397Sobrien  int log;
658650397Sobrien
658750397Sobrien  if (value == 0)
658850397Sobrien    log = 0;
658950397Sobrien  else
659050397Sobrien    log = floor_log2 (value);
659150397Sobrien
659250397Sobrien  log = log / 8;
659350397Sobrien  log = 1 << (floor_log2 (log) + 1);
659450397Sobrien
659550397Sobrien  return log;
659650397Sobrien}
659750397Sobrien
659890075Sobrien/* Return the size of a DIE as it is represented in the
659950397Sobrien   .debug_info section.  */
660050397Sobrien
660150397Sobrienstatic unsigned long
6602132718Skansize_of_die (dw_die_ref die)
660350397Sobrien{
660490075Sobrien  unsigned long size = 0;
660590075Sobrien  dw_attr_ref a;
6606169689Skan  unsigned ix;
660750397Sobrien
660850397Sobrien  size += size_of_uleb128 (die->die_abbrev);
6609169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
661050397Sobrien    {
661190075Sobrien      switch (AT_class (a))
661250397Sobrien	{
661350397Sobrien	case dw_val_class_addr:
661490075Sobrien	  size += DWARF2_ADDR_SIZE;
661550397Sobrien	  break;
661690075Sobrien	case dw_val_class_offset:
661790075Sobrien	  size += DWARF_OFFSET_SIZE;
661890075Sobrien	  break;
661950397Sobrien	case dw_val_class_loc:
662050397Sobrien	  {
662190075Sobrien	    unsigned long lsize = size_of_locs (AT_loc (a));
662250397Sobrien
662350397Sobrien	    /* Block length.  */
662450397Sobrien	    size += constant_size (lsize);
662550397Sobrien	    size += lsize;
662650397Sobrien	  }
662750397Sobrien	  break;
662890075Sobrien	case dw_val_class_loc_list:
662990075Sobrien	  size += DWARF_OFFSET_SIZE;
663090075Sobrien	  break;
663190075Sobrien	case dw_val_class_range_list:
663290075Sobrien	  size += DWARF_OFFSET_SIZE;
663390075Sobrien	  break;
663450397Sobrien	case dw_val_class_const:
663590075Sobrien	  size += size_of_sleb128 (AT_int (a));
663650397Sobrien	  break;
663750397Sobrien	case dw_val_class_unsigned_const:
663890075Sobrien	  size += constant_size (AT_unsigned (a));
663950397Sobrien	  break;
664050397Sobrien	case dw_val_class_long_long:
664190075Sobrien	  size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
664250397Sobrien	  break;
6643132718Skan	case dw_val_class_vec:
6644132718Skan	  size += 1 + (a->dw_attr_val.v.val_vec.length
6645132718Skan		       * a->dw_attr_val.v.val_vec.elt_size); /* block */
664650397Sobrien	  break;
664750397Sobrien	case dw_val_class_flag:
664850397Sobrien	  size += 1;
664950397Sobrien	  break;
665050397Sobrien	case dw_val_class_die_ref:
6651132718Skan	  if (AT_ref_external (a))
6652132718Skan	    size += DWARF2_ADDR_SIZE;
6653132718Skan	  else
6654132718Skan	    size += DWARF_OFFSET_SIZE;
665550397Sobrien	  break;
665650397Sobrien	case dw_val_class_fde_ref:
665750397Sobrien	  size += DWARF_OFFSET_SIZE;
665850397Sobrien	  break;
665950397Sobrien	case dw_val_class_lbl_id:
666090075Sobrien	  size += DWARF2_ADDR_SIZE;
666150397Sobrien	  break;
6662169689Skan	case dw_val_class_lineptr:
6663169689Skan	case dw_val_class_macptr:
666450397Sobrien	  size += DWARF_OFFSET_SIZE;
666550397Sobrien	  break;
666650397Sobrien	case dw_val_class_str:
666790075Sobrien	  if (AT_string_form (a) == DW_FORM_strp)
666890075Sobrien	    size += DWARF_OFFSET_SIZE;
666990075Sobrien	  else
6670132718Skan	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
667150397Sobrien	  break;
6672169689Skan	case dw_val_class_file:
6673169689Skan	  size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6674169689Skan	  break;
667550397Sobrien	default:
6676169689Skan	  gcc_unreachable ();
667750397Sobrien	}
667850397Sobrien    }
667950397Sobrien
668050397Sobrien  return size;
668150397Sobrien}
668250397Sobrien
668390075Sobrien/* Size the debugging information associated with a given DIE.  Visits the
668490075Sobrien   DIE's children recursively.  Updates the global variable next_die_offset, on
668590075Sobrien   each time through.  Uses the current value of next_die_offset to update the
668690075Sobrien   die_offset field in each DIE.  */
668750397Sobrien
668850397Sobrienstatic void
6689132718Skancalc_die_sizes (dw_die_ref die)
669050397Sobrien{
669190075Sobrien  dw_die_ref c;
669290075Sobrien
669350397Sobrien  die->die_offset = next_die_offset;
669450397Sobrien  next_die_offset += size_of_die (die);
669550397Sobrien
6696169689Skan  FOR_EACH_CHILD (die, c, calc_die_sizes (c));
669750397Sobrien
669850397Sobrien  if (die->die_child != NULL)
669950397Sobrien    /* Count the null byte used to terminate sibling lists.  */
670050397Sobrien    next_die_offset += 1;
670150397Sobrien}
670250397Sobrien
670390075Sobrien/* Set the marks for a die and its children.  We do this so
670490075Sobrien   that we know whether or not a reference needs to use FORM_ref_addr; only
670590075Sobrien   DIEs in the same CU will be marked.  We used to clear out the offset
670690075Sobrien   and use that as the flag, but ran into ordering problems.  */
670750397Sobrien
670890075Sobrienstatic void
6709132718Skanmark_dies (dw_die_ref die)
671050397Sobrien{
671190075Sobrien  dw_die_ref c;
671250397Sobrien
6713169689Skan  gcc_assert (!die->die_mark);
6714132718Skan
671590075Sobrien  die->die_mark = 1;
6716169689Skan  FOR_EACH_CHILD (die, c, mark_dies (c));
671750397Sobrien}
671850397Sobrien
671990075Sobrien/* Clear the marks for a die and its children.  */
672050397Sobrien
672190075Sobrienstatic void
6722132718Skanunmark_dies (dw_die_ref die)
672350397Sobrien{
672490075Sobrien  dw_die_ref c;
672550397Sobrien
6726169689Skan  gcc_assert (die->die_mark);
6727132718Skan
672890075Sobrien  die->die_mark = 0;
6729169689Skan  FOR_EACH_CHILD (die, c, unmark_dies (c));
673050397Sobrien}
673150397Sobrien
6732117395Skan/* Clear the marks for a die, its children and referred dies.  */
6733117395Skan
6734117395Skanstatic void
6735132718Skanunmark_all_dies (dw_die_ref die)
6736117395Skan{
6737117395Skan  dw_die_ref c;
6738117395Skan  dw_attr_ref a;
6739169689Skan  unsigned ix;
6740117395Skan
6741117395Skan  if (!die->die_mark)
6742117395Skan    return;
6743117395Skan  die->die_mark = 0;
6744117395Skan
6745169689Skan  FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6746117395Skan
6747169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6748117395Skan    if (AT_class (a) == dw_val_class_die_ref)
6749117395Skan      unmark_all_dies (AT_ref (a));
6750117395Skan}
6751117395Skan
6752260396Spfg/* Return the size of the .debug_pubnames or .debug_pubtypes table
6753260396Spfg   generated for the compilation unit.  */
675450397Sobrien
675550397Sobrienstatic unsigned long
6756260396Spfgsize_of_pubnames (VEC (pubname_entry, gc) * names)
675750397Sobrien{
675890075Sobrien  unsigned long size;
675990075Sobrien  unsigned i;
6760260396Spfg  pubname_ref p;
676150397Sobrien
676250397Sobrien  size = DWARF_PUBNAMES_HEADER_SIZE;
6763260396Spfg  for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6764260396Spfg    if (names != pubtype_table
6765260396Spfg	|| p->die->die_offset != 0
6766260396Spfg	|| !flag_eliminate_unused_debug_types)
6767260396Spfg      size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
676850397Sobrien
676950397Sobrien  size += DWARF_OFFSET_SIZE;
677050397Sobrien  return size;
677150397Sobrien}
677250397Sobrien
677350397Sobrien/* Return the size of the information in the .debug_aranges section.  */
677450397Sobrien
677550397Sobrienstatic unsigned long
6776132718Skansize_of_aranges (void)
677750397Sobrien{
677890075Sobrien  unsigned long size;
677950397Sobrien
678050397Sobrien  size = DWARF_ARANGES_HEADER_SIZE;
678150397Sobrien
678250397Sobrien  /* Count the address/length pair for this compilation unit.  */
678390075Sobrien  size += 2 * DWARF2_ADDR_SIZE;
678490075Sobrien  size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
678550397Sobrien
678650397Sobrien  /* Count the two zero words used to terminated the address range table.  */
678790075Sobrien  size += 2 * DWARF2_ADDR_SIZE;
678850397Sobrien  return size;
678950397Sobrien}
679050397Sobrien
679150397Sobrien/* Select the encoding of an attribute value.  */
679250397Sobrien
679350397Sobrienstatic enum dwarf_form
6794132718Skanvalue_format (dw_attr_ref a)
679550397Sobrien{
679690075Sobrien  switch (a->dw_attr_val.val_class)
679750397Sobrien    {
679850397Sobrien    case dw_val_class_addr:
679950397Sobrien      return DW_FORM_addr;
680090075Sobrien    case dw_val_class_range_list:
680190075Sobrien    case dw_val_class_offset:
680290075Sobrien    case dw_val_class_loc_list:
6803169689Skan      switch (DWARF_OFFSET_SIZE)
6804169689Skan	{
6805169689Skan	case 4:
6806169689Skan	  return DW_FORM_data4;
6807169689Skan	case 8:
6808169689Skan	  return DW_FORM_data8;
6809169689Skan	default:
6810169689Skan	  gcc_unreachable ();
6811169689Skan	}
681250397Sobrien    case dw_val_class_loc:
681390075Sobrien      switch (constant_size (size_of_locs (AT_loc (a))))
681450397Sobrien	{
681550397Sobrien	case 1:
681650397Sobrien	  return DW_FORM_block1;
681750397Sobrien	case 2:
681850397Sobrien	  return DW_FORM_block2;
681950397Sobrien	default:
6820169689Skan	  gcc_unreachable ();
682150397Sobrien	}
682250397Sobrien    case dw_val_class_const:
682390075Sobrien      return DW_FORM_sdata;
682450397Sobrien    case dw_val_class_unsigned_const:
682590075Sobrien      switch (constant_size (AT_unsigned (a)))
682650397Sobrien	{
682750397Sobrien	case 1:
682850397Sobrien	  return DW_FORM_data1;
682950397Sobrien	case 2:
683050397Sobrien	  return DW_FORM_data2;
683150397Sobrien	case 4:
683250397Sobrien	  return DW_FORM_data4;
683350397Sobrien	case 8:
683450397Sobrien	  return DW_FORM_data8;
683550397Sobrien	default:
6836169689Skan	  gcc_unreachable ();
683750397Sobrien	}
683850397Sobrien    case dw_val_class_long_long:
683950397Sobrien      return DW_FORM_block1;
6840132718Skan    case dw_val_class_vec:
684150397Sobrien      return DW_FORM_block1;
684250397Sobrien    case dw_val_class_flag:
684350397Sobrien      return DW_FORM_flag;
684450397Sobrien    case dw_val_class_die_ref:
684590075Sobrien      if (AT_ref_external (a))
684690075Sobrien	return DW_FORM_ref_addr;
684790075Sobrien      else
684890075Sobrien	return DW_FORM_ref;
684950397Sobrien    case dw_val_class_fde_ref:
685050397Sobrien      return DW_FORM_data;
685150397Sobrien    case dw_val_class_lbl_id:
685250397Sobrien      return DW_FORM_addr;
6853169689Skan    case dw_val_class_lineptr:
6854169689Skan    case dw_val_class_macptr:
685550397Sobrien      return DW_FORM_data;
685650397Sobrien    case dw_val_class_str:
685790075Sobrien      return AT_string_form (a);
6858169689Skan    case dw_val_class_file:
6859169689Skan      switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6860169689Skan	{
6861169689Skan	case 1:
6862169689Skan	  return DW_FORM_data1;
6863169689Skan	case 2:
6864169689Skan	  return DW_FORM_data2;
6865169689Skan	case 4:
6866169689Skan	  return DW_FORM_data4;
6867169689Skan	default:
6868169689Skan	  gcc_unreachable ();
6869169689Skan	}
687090075Sobrien
687150397Sobrien    default:
6872169689Skan      gcc_unreachable ();
687350397Sobrien    }
687450397Sobrien}
687550397Sobrien
687650397Sobrien/* Output the encoding of an attribute value.  */
687750397Sobrien
687850397Sobrienstatic void
6879132718Skanoutput_value_format (dw_attr_ref a)
688050397Sobrien{
688190075Sobrien  enum dwarf_form form = value_format (a);
688250397Sobrien
688390075Sobrien  dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
688450397Sobrien}
688550397Sobrien
688650397Sobrien/* Output the .debug_abbrev section which defines the DIE abbreviation
688750397Sobrien   table.  */
688850397Sobrien
688950397Sobrienstatic void
6890132718Skanoutput_abbrev_section (void)
689150397Sobrien{
689250397Sobrien  unsigned long abbrev_id;
689350397Sobrien
689450397Sobrien  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
689550397Sobrien    {
689690075Sobrien      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6897169689Skan      unsigned ix;
6898169689Skan      dw_attr_ref a_attr;
689950397Sobrien
690090075Sobrien      dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
690190075Sobrien      dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
690290075Sobrien				   dwarf_tag_name (abbrev->die_tag));
690350397Sobrien
690490075Sobrien      if (abbrev->die_child != NULL)
690590075Sobrien	dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
690690075Sobrien      else
690790075Sobrien	dw2_asm_output_data (1, DW_children_no, "DW_children_no");
690850397Sobrien
6909169689Skan      for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6910169689Skan	   ix++)
691150397Sobrien	{
691290075Sobrien	  dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
691390075Sobrien				       dwarf_attr_name (a_attr->dw_attr));
691490075Sobrien	  output_value_format (a_attr);
691550397Sobrien	}
691650397Sobrien
691790075Sobrien      dw2_asm_output_data (1, 0, NULL);
691890075Sobrien      dw2_asm_output_data (1, 0, NULL);
691950397Sobrien    }
692052284Sobrien
692190075Sobrien  /* Terminate the table.  */
692290075Sobrien  dw2_asm_output_data (1, 0, NULL);
692350397Sobrien}
692450397Sobrien
692590075Sobrien/* Output a symbol we can use to refer to this DIE from another CU.  */
692650397Sobrien
692790075Sobrienstatic inline void
6928132718Skanoutput_die_symbol (dw_die_ref die)
692990075Sobrien{
693090075Sobrien  char *sym = die->die_symbol;
693190075Sobrien
693290075Sobrien  if (sym == 0)
693390075Sobrien    return;
693490075Sobrien
693590075Sobrien  if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
693690075Sobrien    /* We make these global, not weak; if the target doesn't support
693790075Sobrien       .linkonce, it doesn't support combining the sections, so debugging
693890075Sobrien       will break.  */
6939169689Skan    targetm.asm_out.globalize_label (asm_out_file, sym);
694090075Sobrien
694190075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, sym);
694290075Sobrien}
694390075Sobrien
694490075Sobrien/* Return a new location list, given the begin and end range, and the
694590075Sobrien   expression. gensym tells us whether to generate a new internal symbol for
694690075Sobrien   this location list node, which is done for the head of the list only.  */
694790075Sobrien
694890075Sobrienstatic inline dw_loc_list_ref
6949132718Skannew_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6950132718Skan	      const char *section, unsigned int gensym)
695190075Sobrien{
6952132718Skan  dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
695390075Sobrien
695490075Sobrien  retlist->begin = begin;
695590075Sobrien  retlist->end = end;
695690075Sobrien  retlist->expr = expr;
695790075Sobrien  retlist->section = section;
6958117395Skan  if (gensym)
695990075Sobrien    retlist->ll_symbol = gen_internal_sym ("LLST");
696090075Sobrien
696190075Sobrien  return retlist;
696290075Sobrien}
696390075Sobrien
6964132718Skan/* Add a location description expression to a location list.  */
696590075Sobrien
696690075Sobrienstatic inline void
6967132718Skanadd_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6968132718Skan			   const char *begin, const char *end,
6969132718Skan			   const char *section)
697090075Sobrien{
697190075Sobrien  dw_loc_list_ref *d;
6972117395Skan
697390075Sobrien  /* Find the end of the chain.  */
697490075Sobrien  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
697590075Sobrien    ;
697690075Sobrien
6977132718Skan  /* Add a new location list node to the list.  */
697890075Sobrien  *d = new_loc_list (descr, begin, end, section, 0);
697990075Sobrien}
698090075Sobrien
6981169689Skanstatic void
6982169689Skandwarf2out_switch_text_section (void)
6983169689Skan{
6984169689Skan  dw_fde_ref fde;
6985169689Skan
6986169689Skan  gcc_assert (cfun);
6987169689Skan
6988169689Skan  fde = &fde_table[fde_table_in_use - 1];
6989169689Skan  fde->dw_fde_switched_sections = true;
6990169689Skan  fde->dw_fde_hot_section_label = cfun->hot_section_label;
6991169689Skan  fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6992169689Skan  fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6993169689Skan  fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6994169689Skan  have_multiple_function_sections = true;
6995169689Skan
6996169689Skan  /* Reset the current label on switching text sections, so that we
6997169689Skan     don't attempt to advance_loc4 between labels in different sections.  */
6998169689Skan  fde->dw_fde_current_label = NULL;
6999169689Skan}
7000169689Skan
7001132718Skan/* Output the location list given to us.  */
700290075Sobrien
700350397Sobrienstatic void
7004132718Skanoutput_loc_list (dw_loc_list_ref list_head)
700550397Sobrien{
700690075Sobrien  dw_loc_list_ref curr = list_head;
700750397Sobrien
700890075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
700990075Sobrien
7010169689Skan  /* Walk the location list, and output each range + expression.  */
7011117395Skan  for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
701290075Sobrien    {
701390075Sobrien      unsigned long size;
7014169689Skan      if (!have_multiple_function_sections)
7015169689Skan	{
7016169689Skan	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7017169689Skan				"Location list begin address (%s)",
7018169689Skan				list_head->ll_symbol);
7019169689Skan	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7020169689Skan				"Location list end address (%s)",
7021169689Skan				list_head->ll_symbol);
7022169689Skan	}
7023169689Skan      else
7024169689Skan	{
7025169689Skan	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7026169689Skan			       "Location list begin address (%s)",
7027169689Skan			       list_head->ll_symbol);
7028169689Skan	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7029169689Skan			       "Location list end address (%s)",
7030169689Skan			       list_head->ll_symbol);
7031169689Skan	}
703290075Sobrien      size = size_of_locs (curr->expr);
7033117395Skan
703490075Sobrien      /* Output the block length for this list of location operations.  */
7035169689Skan      gcc_assert (size <= 0xffff);
703690075Sobrien      dw2_asm_output_data (2, size, "%s", "Location expression size");
703750397Sobrien
703890075Sobrien      output_loc_sequence (curr->expr);
703990075Sobrien    }
704050397Sobrien
7041169689Skan  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
704290075Sobrien		       "Location list terminator begin (%s)",
704390075Sobrien		       list_head->ll_symbol);
7044169689Skan  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
704590075Sobrien		       "Location list terminator end (%s)",
704690075Sobrien		       list_head->ll_symbol);
704750397Sobrien}
704850397Sobrien
704950397Sobrien/* Output the DIE and its attributes.  Called recursively to generate
705050397Sobrien   the definitions of each child DIE.  */
705150397Sobrien
705250397Sobrienstatic void
7053132718Skanoutput_die (dw_die_ref die)
705450397Sobrien{
705590075Sobrien  dw_attr_ref a;
705690075Sobrien  dw_die_ref c;
705790075Sobrien  unsigned long size;
7058169689Skan  unsigned ix;
705950397Sobrien
706090075Sobrien  /* If someone in another CU might refer to us, set up a symbol for
706190075Sobrien     them to point to.  */
706290075Sobrien  if (die->die_symbol)
706390075Sobrien    output_die_symbol (die);
706450397Sobrien
706590075Sobrien  dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7066259948Spfg			       (unsigned long)die->die_offset,
7067259948Spfg			       dwarf_tag_name (die->die_tag));
706850397Sobrien
7069169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
707050397Sobrien    {
707190075Sobrien      const char *name = dwarf_attr_name (a->dw_attr);
707290075Sobrien
707390075Sobrien      switch (AT_class (a))
707450397Sobrien	{
707550397Sobrien	case dw_val_class_addr:
707690075Sobrien	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
707750397Sobrien	  break;
707850397Sobrien
707990075Sobrien	case dw_val_class_offset:
708090075Sobrien	  dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
708190075Sobrien			       "%s", name);
708290075Sobrien	  break;
708350397Sobrien
708490075Sobrien	case dw_val_class_range_list:
708590075Sobrien	  {
708690075Sobrien	    char *p = strchr (ranges_section_label, '\0');
708750397Sobrien
7088132718Skan	    sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7089132718Skan		     a->dw_attr_val.v.val_offset);
709090075Sobrien	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7091169689Skan				   debug_ranges_section, "%s", name);
709290075Sobrien	    *p = '\0';
709390075Sobrien	  }
709490075Sobrien	  break;
709550397Sobrien
709690075Sobrien	case dw_val_class_loc:
709790075Sobrien	  size = size_of_locs (AT_loc (a));
709850397Sobrien
709990075Sobrien	  /* Output the block length for this list of location operations.  */
710090075Sobrien	  dw2_asm_output_data (constant_size (size), size, "%s", name);
710150397Sobrien
710290075Sobrien	  output_loc_sequence (AT_loc (a));
710350397Sobrien	  break;
710450397Sobrien
710550397Sobrien	case dw_val_class_const:
710690075Sobrien	  /* ??? It would be slightly more efficient to use a scheme like is
710790075Sobrien	     used for unsigned constants below, but gdb 4.x does not sign
710890075Sobrien	     extend.  Gdb 5.x does sign extend.  */
710990075Sobrien	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
711050397Sobrien	  break;
711150397Sobrien
711250397Sobrien	case dw_val_class_unsigned_const:
711390075Sobrien	  dw2_asm_output_data (constant_size (AT_unsigned (a)),
711490075Sobrien			       AT_unsigned (a), "%s", name);
711550397Sobrien	  break;
711650397Sobrien
711750397Sobrien	case dw_val_class_long_long:
711890075Sobrien	  {
711990075Sobrien	    unsigned HOST_WIDE_INT first, second;
712050397Sobrien
712190075Sobrien	    dw2_asm_output_data (1,
712290075Sobrien				 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7123117395Skan				 "%s", name);
712450397Sobrien
712590075Sobrien	    if (WORDS_BIG_ENDIAN)
712690075Sobrien	      {
712790075Sobrien		first = a->dw_attr_val.v.val_long_long.hi;
712890075Sobrien		second = a->dw_attr_val.v.val_long_long.low;
712990075Sobrien	      }
713090075Sobrien	    else
713190075Sobrien	      {
713290075Sobrien		first = a->dw_attr_val.v.val_long_long.low;
713390075Sobrien		second = a->dw_attr_val.v.val_long_long.hi;
713490075Sobrien	      }
713590075Sobrien
713690075Sobrien	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
713790075Sobrien				 first, "long long constant");
713890075Sobrien	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
713990075Sobrien				 second, NULL);
714090075Sobrien	  }
714150397Sobrien	  break;
714250397Sobrien
7143132718Skan	case dw_val_class_vec:
714452284Sobrien	  {
7145132718Skan	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7146132718Skan	    unsigned int len = a->dw_attr_val.v.val_vec.length;
714790075Sobrien	    unsigned int i;
7148132718Skan	    unsigned char *p;
714950397Sobrien
7150132718Skan	    dw2_asm_output_data (1, len * elt_size, "%s", name);
7151132718Skan	    if (elt_size > sizeof (HOST_WIDE_INT))
7152132718Skan	      {
7153132718Skan		elt_size /= 2;
7154132718Skan		len *= 2;
7155132718Skan	      }
7156132718Skan	    for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7157132718Skan		 i < len;
7158132718Skan		 i++, p += elt_size)
7159132718Skan	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7160132718Skan				   "fp or vector constant word %u", i);
716190075Sobrien	    break;
716252284Sobrien	  }
716350397Sobrien
716450397Sobrien	case dw_val_class_flag:
716590075Sobrien	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
716650397Sobrien	  break;
716750397Sobrien
7168117395Skan	case dw_val_class_loc_list:
716990075Sobrien	  {
717090075Sobrien	    char *sym = AT_loc_list (a)->ll_symbol;
717190075Sobrien
7172169689Skan	    gcc_assert (sym);
7173169689Skan	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7174169689Skan				   "%s", name);
717590075Sobrien	  }
717690075Sobrien	  break;
717790075Sobrien
717850397Sobrien	case dw_val_class_die_ref:
717990075Sobrien	  if (AT_ref_external (a))
718090075Sobrien	    {
718190075Sobrien	      char *sym = AT_ref (a)->die_symbol;
718290075Sobrien
7183169689Skan	      gcc_assert (sym);
7184169689Skan	      dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7185169689Skan				     "%s", name);
718690075Sobrien	    }
718750397Sobrien	  else
7188169689Skan	    {
7189169689Skan	      gcc_assert (AT_ref (a)->die_offset);
7190169689Skan	      dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7191169689Skan				   "%s", name);
7192169689Skan	    }
719350397Sobrien	  break;
719450397Sobrien
719550397Sobrien	case dw_val_class_fde_ref:
719650397Sobrien	  {
719750397Sobrien	    char l1[20];
719890075Sobrien
719990075Sobrien	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
720090075Sobrien					 a->dw_attr_val.v.val_fde_index * 2);
7201169689Skan	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7202169689Skan				   "%s", name);
720350397Sobrien	  }
720450397Sobrien	  break;
720550397Sobrien
720650397Sobrien	case dw_val_class_lbl_id:
720790075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
720850397Sobrien	  break;
720950397Sobrien
7210169689Skan	case dw_val_class_lineptr:
7211169689Skan	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7212169689Skan				 debug_line_section, "%s", name);
721350397Sobrien	  break;
721450397Sobrien
7215169689Skan	case dw_val_class_macptr:
7216169689Skan	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7217169689Skan				 debug_macinfo_section, "%s", name);
7218169689Skan	  break;
7219169689Skan
722050397Sobrien	case dw_val_class_str:
722190075Sobrien	  if (AT_string_form (a) == DW_FORM_strp)
722290075Sobrien	    dw2_asm_output_offset (DWARF_OFFSET_SIZE,
722390075Sobrien				   a->dw_attr_val.v.val_str->label,
7224169689Skan				   debug_str_section,
722590075Sobrien				   "%s: \"%s\"", name, AT_string (a));
722650397Sobrien	  else
722790075Sobrien	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
722850397Sobrien	  break;
722950397Sobrien
7230169689Skan	case dw_val_class_file:
7231169689Skan	  {
7232169689Skan	    int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7233169689Skan
7234169689Skan	    dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7235169689Skan				 a->dw_attr_val.v.val_file->filename);
7236169689Skan	    break;
7237169689Skan	  }
7238169689Skan
723950397Sobrien	default:
7240169689Skan	  gcc_unreachable ();
724150397Sobrien	}
724250397Sobrien    }
724350397Sobrien
7244169689Skan  FOR_EACH_CHILD (die, c, output_die (c));
724550397Sobrien
724690075Sobrien  /* Add null byte to terminate sibling list.  */
724750397Sobrien  if (die->die_child != NULL)
724890075Sobrien    dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7249259948Spfg			 (unsigned long) die->die_offset);
725050397Sobrien}
725150397Sobrien
725250397Sobrien/* Output the compilation unit that appears at the beginning of the
725350397Sobrien   .debug_info section, and precedes the DIE descriptions.  */
725450397Sobrien
725550397Sobrienstatic void
7256132718Skanoutput_compilation_unit_header (void)
725750397Sobrien{
7258132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7259132718Skan    dw2_asm_output_data (4, 0xffffffff,
7260132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
7261132718Skan  dw2_asm_output_data (DWARF_OFFSET_SIZE,
7262132718Skan                       next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
726390075Sobrien		       "Length of Compilation Unit Info");
726490075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
726590075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7266169689Skan			 debug_abbrev_section,
726790075Sobrien			 "Offset Into Abbrev. Section");
726890075Sobrien  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
726990075Sobrien}
727050397Sobrien
727190075Sobrien/* Output the compilation unit DIE and its children.  */
727250397Sobrien
727390075Sobrienstatic void
7274132718Skanoutput_comp_unit (dw_die_ref die, int output_if_empty)
727590075Sobrien{
727690075Sobrien  const char *secname;
7277117395Skan  char *oldsym, *tmp;
727850397Sobrien
7279117395Skan  /* Unless we are outputting main CU, we may throw away empty ones.  */
7280117395Skan  if (!output_if_empty && die->die_child == NULL)
7281117395Skan    return;
7282117395Skan
728390075Sobrien  /* Even if there are no children of this DIE, we must output the information
728490075Sobrien     about the compilation unit.  Otherwise, on an empty translation unit, we
728590075Sobrien     will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
728690075Sobrien     will then complain when examining the file.  First mark all the DIEs in
728790075Sobrien     this CU so we know which get local refs.  */
728890075Sobrien  mark_dies (die);
728950397Sobrien
729090075Sobrien  build_abbrev_table (die);
729190075Sobrien
729290075Sobrien  /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
729390075Sobrien  next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
729490075Sobrien  calc_die_sizes (die);
729590075Sobrien
7296117395Skan  oldsym = die->die_symbol;
7297117395Skan  if (oldsym)
729890075Sobrien    {
7299132718Skan      tmp = alloca (strlen (oldsym) + 24);
730090075Sobrien
7301117395Skan      sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
730290075Sobrien      secname = tmp;
730390075Sobrien      die->die_symbol = NULL;
7304169689Skan      switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
730590075Sobrien    }
730690075Sobrien  else
7307169689Skan    switch_to_section (debug_info_section);
730890075Sobrien
730990075Sobrien  /* Output debugging information.  */
731090075Sobrien  output_compilation_unit_header ();
731190075Sobrien  output_die (die);
731290075Sobrien
731390075Sobrien  /* Leave the marks on the main CU, so we can check them in
731490075Sobrien     output_pubnames.  */
7315117395Skan  if (oldsym)
7316117395Skan    {
7317117395Skan      unmark_dies (die);
7318117395Skan      die->die_symbol = oldsym;
7319117395Skan    }
732050397Sobrien}
732150397Sobrien
7322169689Skan/* Return the DWARF2/3 pubname associated with a decl.  */
732350397Sobrien
732490075Sobrienstatic const char *
7325132718Skandwarf2_name (tree decl, int scope)
732650397Sobrien{
7327169689Skan  return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
732850397Sobrien}
732950397Sobrien
733050397Sobrien/* Add a new entry to .debug_pubnames if appropriate.  */
733150397Sobrien
733250397Sobrienstatic void
7333132718Skanadd_pubname (tree decl, dw_die_ref die)
733450397Sobrien{
7335260396Spfg  pubname_entry e;
733650397Sobrien
733750397Sobrien  if (! TREE_PUBLIC (decl))
733850397Sobrien    return;
733950397Sobrien
7340260396Spfg  e.die = die;
7341260396Spfg  e.name = xstrdup (dwarf2_name (decl, 1));
7342260396Spfg  VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7343260396Spfg}
7344260396Spfg
7345260396Spfg/* Add a new entry to .debug_pubtypes if appropriate.  */
7346260396Spfg
7347260396Spfgstatic void
7348260396Spfgadd_pubtype (tree decl, dw_die_ref die)
7349260396Spfg{
7350260396Spfg  pubname_entry e;
7351260396Spfg
7352260396Spfg  e.name = NULL;
7353260396Spfg  if ((TREE_PUBLIC (decl)
7354260396Spfg       || die->die_parent == comp_unit_die)
7355260396Spfg      && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
735650397Sobrien    {
7357260396Spfg      e.die = die;
7358260396Spfg      if (TYPE_P (decl))
7359260396Spfg	{
7360260396Spfg	  if (TYPE_NAME (decl))
7361260396Spfg	    {
7362260396Spfg	      if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7363260396Spfg		e.name = xstrdup ((const char *) IDENTIFIER_POINTER
7364260396Spfg				                              (TYPE_NAME (decl)));
7365260396Spfg	      else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7366260396Spfg		       && DECL_NAME (TYPE_NAME (decl)))
7367260396Spfg		e.name = xstrdup ((const char *) IDENTIFIER_POINTER
7368260396Spfg				                  (DECL_NAME (TYPE_NAME (decl))));
7369260396Spfg             else
7370260396Spfg	       e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7371260396Spfg	    }
7372260396Spfg	}
7373260396Spfg      else
7374260396Spfg	e.name = xstrdup (dwarf2_name (decl, 1));
7375260396Spfg
7376260396Spfg      /* If we don't have a name for the type, there's no point in adding
7377260396Spfg	 it to the table.  */
7378260396Spfg      if (e.name && e.name[0] != '\0')
7379260396Spfg	VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
738050397Sobrien    }
738150397Sobrien}
738250397Sobrien
738350397Sobrien/* Output the public names table used to speed up access to externally
7384260396Spfg   visible names; or the public types table used to find type definitions.  */
738550397Sobrien
738650397Sobrienstatic void
7387260396Spfgoutput_pubnames (VEC (pubname_entry, gc) * names)
738850397Sobrien{
738990075Sobrien  unsigned i;
7390260396Spfg  unsigned long pubnames_length = size_of_pubnames (names);
7391260396Spfg  pubname_ref pub;
739250397Sobrien
7393132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7394132718Skan    dw2_asm_output_data (4, 0xffffffff,
7395132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
7396260396Spfg  if (names == pubname_table)
7397260396Spfg    dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7398260396Spfg			 "Length of Public Names Info");
7399260396Spfg  else
7400260396Spfg    dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7401260396Spfg			 "Length of Public Type Names Info");
740290075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
740390075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7404169689Skan			 debug_info_section,
740590075Sobrien			 "Offset of Compilation Unit Info");
740690075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
740790075Sobrien		       "Compilation Unit Length");
740850397Sobrien
7409260396Spfg  for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
741050397Sobrien    {
7411260396Spfg      /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7412260396Spfg      if (names == pubname_table)
7413260396Spfg	gcc_assert (pub->die->die_mark);
741450397Sobrien
7415260396Spfg      if (names != pubtype_table
7416260396Spfg	  || pub->die->die_offset != 0
7417260396Spfg	  || !flag_eliminate_unused_debug_types)
7418260396Spfg	{
7419260396Spfg	  dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7420260396Spfg			       "DIE offset");
742150397Sobrien
7422260396Spfg	  dw2_asm_output_nstring (pub->name, -1, "external name");
7423260396Spfg	}
742450397Sobrien    }
742550397Sobrien
742690075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
742750397Sobrien}
742850397Sobrien
742950397Sobrien/* Add a new entry to .debug_aranges if appropriate.  */
743050397Sobrien
743150397Sobrienstatic void
7432132718Skanadd_arange (tree decl, dw_die_ref die)
743350397Sobrien{
743450397Sobrien  if (! DECL_SECTION_NAME (decl))
743550397Sobrien    return;
743650397Sobrien
743750397Sobrien  if (arange_table_in_use == arange_table_allocated)
743850397Sobrien    {
743950397Sobrien      arange_table_allocated += ARANGE_TABLE_INCREMENT;
7440132718Skan      arange_table = ggc_realloc (arange_table,
7441132718Skan				  (arange_table_allocated
7442132718Skan				   * sizeof (dw_die_ref)));
7443132718Skan      memset (arange_table + arange_table_in_use, 0,
7444132718Skan	      ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
744550397Sobrien    }
744650397Sobrien
744750397Sobrien  arange_table[arange_table_in_use++] = die;
744850397Sobrien}
744950397Sobrien
745050397Sobrien/* Output the information that goes into the .debug_aranges table.
745150397Sobrien   Namely, define the beginning and ending address range of the
745250397Sobrien   text section generated for this compilation unit.  */
745350397Sobrien
745450397Sobrienstatic void
7455132718Skanoutput_aranges (void)
745650397Sobrien{
745790075Sobrien  unsigned i;
745890075Sobrien  unsigned long aranges_length = size_of_aranges ();
745950397Sobrien
7460132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7461132718Skan    dw2_asm_output_data (4, 0xffffffff,
7462132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
746390075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
746490075Sobrien		       "Length of Address Ranges Info");
746590075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
746690075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7467169689Skan			 debug_info_section,
746890075Sobrien			 "Offset of Compilation Unit Info");
746990075Sobrien  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
747090075Sobrien  dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
747150397Sobrien
747290075Sobrien  /* We need to align to twice the pointer size here.  */
747390075Sobrien  if (DWARF_ARANGES_PAD_SIZE)
747490075Sobrien    {
747590075Sobrien      /* Pad using a 2 byte words so that padding is correct for any
7476132718Skan	 pointer size.  */
747790075Sobrien      dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
747890075Sobrien			   2 * DWARF2_ADDR_SIZE);
747990075Sobrien      for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
748090075Sobrien	dw2_asm_output_data (2, 0, NULL);
748190075Sobrien    }
748250397Sobrien
748390075Sobrien  dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
748490075Sobrien  dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
748590075Sobrien			text_section_label, "Length");
7486169689Skan  if (flag_reorder_blocks_and_partition)
7487169689Skan    {
7488169689Skan      dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7489169689Skan			   "Address");
7490169689Skan      dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7491169689Skan			    cold_text_section_label, "Length");
7492169689Skan    }
749350397Sobrien
749490075Sobrien  for (i = 0; i < arange_table_in_use; i++)
749590075Sobrien    {
749690075Sobrien      dw_die_ref die = arange_table[i];
749750397Sobrien
749890075Sobrien      /* We shouldn't see aranges for DIEs outside of the main CU.  */
7499169689Skan      gcc_assert (die->die_mark);
750050397Sobrien
750190075Sobrien      if (die->die_tag == DW_TAG_subprogram)
750290075Sobrien	{
750390075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
750490075Sobrien			       "Address");
750590075Sobrien	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
750690075Sobrien				get_AT_low_pc (die), "Length");
750790075Sobrien	}
750890075Sobrien      else
750990075Sobrien	{
751090075Sobrien	  /* A static variable; extract the symbol from DW_AT_location.
751190075Sobrien	     Note that this code isn't currently hit, as we only emit
751290075Sobrien	     aranges for functions (jason 9/23/99).  */
751390075Sobrien	  dw_attr_ref a = get_AT (die, DW_AT_location);
751490075Sobrien	  dw_loc_descr_ref loc;
751550397Sobrien
7516169689Skan	  gcc_assert (a && AT_class (a) == dw_val_class_loc);
751750397Sobrien
751890075Sobrien	  loc = AT_loc (a);
7519169689Skan	  gcc_assert (loc->dw_loc_opc == DW_OP_addr);
752050397Sobrien
752190075Sobrien	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
752290075Sobrien				   loc->dw_loc_oprnd1.v.val_addr, "Address");
752390075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE,
752490075Sobrien			       get_AT_unsigned (die, DW_AT_byte_size),
752590075Sobrien			       "Length");
752690075Sobrien	}
752790075Sobrien    }
752850397Sobrien
752990075Sobrien  /* Output the terminator words.  */
753090075Sobrien  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
753190075Sobrien  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
753290075Sobrien}
753390075Sobrien
753490075Sobrien/* Add a new entry to .debug_ranges.  Return the offset at which it
753590075Sobrien   was placed.  */
753690075Sobrien
753790075Sobrienstatic unsigned int
7538132718Skanadd_ranges (tree block)
753990075Sobrien{
754090075Sobrien  unsigned int in_use = ranges_table_in_use;
754190075Sobrien
754290075Sobrien  if (in_use == ranges_table_allocated)
754350397Sobrien    {
754490075Sobrien      ranges_table_allocated += RANGES_TABLE_INCREMENT;
7545132718Skan      ranges_table
7546132718Skan	= ggc_realloc (ranges_table, (ranges_table_allocated
7547132718Skan				      * sizeof (struct dw_ranges_struct)));
7548132718Skan      memset (ranges_table + ranges_table_in_use, 0,
7549132718Skan	      RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
755090075Sobrien    }
755150397Sobrien
755290075Sobrien  ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
755390075Sobrien  ranges_table_in_use = in_use + 1;
755490075Sobrien
755590075Sobrien  return in_use * 2 * DWARF2_ADDR_SIZE;
755690075Sobrien}
755790075Sobrien
755890075Sobrienstatic void
7559132718Skanoutput_ranges (void)
756090075Sobrien{
756190075Sobrien  unsigned i;
756290075Sobrien  static const char *const start_fmt = "Offset 0x%x";
756390075Sobrien  const char *fmt = start_fmt;
756490075Sobrien
756590075Sobrien  for (i = 0; i < ranges_table_in_use; i++)
756690075Sobrien    {
756790075Sobrien      int block_num = ranges_table[i].block_num;
756890075Sobrien
756990075Sobrien      if (block_num)
757090075Sobrien	{
757190075Sobrien	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
757290075Sobrien	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
757390075Sobrien
757490075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
757590075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
757690075Sobrien
757790075Sobrien	  /* If all code is in the text section, then the compilation
757890075Sobrien	     unit base address defaults to DW_AT_low_pc, which is the
757990075Sobrien	     base of the text section.  */
7580169689Skan	  if (!have_multiple_function_sections)
758190075Sobrien	    {
758290075Sobrien	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
758390075Sobrien				    text_section_label,
758490075Sobrien				    fmt, i * 2 * DWARF2_ADDR_SIZE);
758590075Sobrien	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
758690075Sobrien				    text_section_label, NULL);
758790075Sobrien	    }
758890075Sobrien
758990075Sobrien	  /* Otherwise, we add a DW_AT_entry_pc attribute to force the
759090075Sobrien	     compilation unit base address to zero, which allows us to
759190075Sobrien	     use absolute addresses, and not worry about whether the
759290075Sobrien	     target supports cross-section arithmetic.  */
759390075Sobrien	  else
759490075Sobrien	    {
759590075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
759690075Sobrien				   fmt, i * 2 * DWARF2_ADDR_SIZE);
759790075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
759890075Sobrien	    }
759990075Sobrien
760090075Sobrien	  fmt = NULL;
760190075Sobrien	}
760250397Sobrien      else
760350397Sobrien	{
760490075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
760590075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
760690075Sobrien	  fmt = start_fmt;
760790075Sobrien	}
760890075Sobrien    }
760990075Sobrien}
761050397Sobrien
761190075Sobrien/* Data structure containing information about input files.  */
761290075Sobrienstruct file_info
761390075Sobrien{
7614169689Skan  const char *path;	/* Complete file name.  */
7615169689Skan  const char *fname;	/* File name part.  */
761690075Sobrien  int length;		/* Length of entire string.  */
7617169689Skan  struct dwarf_file_data * file_idx;	/* Index in input file table.  */
761890075Sobrien  int dir_idx;		/* Index in directory table.  */
761990075Sobrien};
762090075Sobrien
762190075Sobrien/* Data structure containing information about directories with source
762290075Sobrien   files.  */
762390075Sobrienstruct dir_info
762490075Sobrien{
7625169689Skan  const char *path;	/* Path including directory name.  */
762690075Sobrien  int length;		/* Path length.  */
762790075Sobrien  int prefix;		/* Index of directory entry which is a prefix.  */
762890075Sobrien  int count;		/* Number of files in this directory.  */
762990075Sobrien  int dir_idx;		/* Index of directory used as base.  */
763090075Sobrien};
763190075Sobrien
763290075Sobrien/* Callback function for file_info comparison.  We sort by looking at
763390075Sobrien   the directories in the path.  */
763490075Sobrien
763590075Sobrienstatic int
7636132718Skanfile_info_cmp (const void *p1, const void *p2)
763790075Sobrien{
763890075Sobrien  const struct file_info *s1 = p1;
763990075Sobrien  const struct file_info *s2 = p2;
764090075Sobrien  unsigned char *cp1;
764190075Sobrien  unsigned char *cp2;
764290075Sobrien
764390075Sobrien  /* Take care of file names without directories.  We need to make sure that
764490075Sobrien     we return consistent values to qsort since some will get confused if
764590075Sobrien     we return the same value when identical operands are passed in opposite
764690075Sobrien     orders.  So if neither has a directory, return 0 and otherwise return
764790075Sobrien     1 or -1 depending on which one has the directory.  */
764890075Sobrien  if ((s1->path == s1->fname || s2->path == s2->fname))
764990075Sobrien    return (s2->path == s2->fname) - (s1->path == s1->fname);
765090075Sobrien
765190075Sobrien  cp1 = (unsigned char *) s1->path;
765290075Sobrien  cp2 = (unsigned char *) s2->path;
765390075Sobrien
765490075Sobrien  while (1)
765590075Sobrien    {
765690075Sobrien      ++cp1;
765790075Sobrien      ++cp2;
765890075Sobrien      /* Reached the end of the first path?  If so, handle like above.  */
765990075Sobrien      if ((cp1 == (unsigned char *) s1->fname)
766090075Sobrien	  || (cp2 == (unsigned char *) s2->fname))
766190075Sobrien	return ((cp2 == (unsigned char *) s2->fname)
766290075Sobrien		- (cp1 == (unsigned char *) s1->fname));
766390075Sobrien
766490075Sobrien      /* Character of current path component the same?  */
766590075Sobrien      else if (*cp1 != *cp2)
766690075Sobrien	return *cp1 - *cp2;
766790075Sobrien    }
766890075Sobrien}
766990075Sobrien
7670169689Skanstruct file_name_acquire_data
7671169689Skan{
7672169689Skan  struct file_info *files;
7673169689Skan  int used_files;
7674169689Skan  int max_files;
7675169689Skan};
7676169689Skan
7677169689Skan/* Traversal function for the hash table.  */
7678169689Skan
7679169689Skanstatic int
7680169689Skanfile_name_acquire (void ** slot, void *data)
7681169689Skan{
7682169689Skan  struct file_name_acquire_data *fnad = data;
7683169689Skan  struct dwarf_file_data *d = *slot;
7684169689Skan  struct file_info *fi;
7685169689Skan  const char *f;
7686169689Skan
7687169689Skan  gcc_assert (fnad->max_files >= d->emitted_number);
7688169689Skan
7689169689Skan  if (! d->emitted_number)
7690169689Skan    return 1;
7691169689Skan
7692169689Skan  gcc_assert (fnad->max_files != fnad->used_files);
7693169689Skan
7694169689Skan  fi = fnad->files + fnad->used_files++;
7695169689Skan
7696169689Skan  /* Skip all leading "./".  */
7697169689Skan  f = d->filename;
7698169689Skan  while (f[0] == '.' && f[1] == '/')
7699169689Skan    f += 2;
7700169689Skan
7701169689Skan  /* Create a new array entry.  */
7702169689Skan  fi->path = f;
7703169689Skan  fi->length = strlen (f);
7704169689Skan  fi->file_idx = d;
7705169689Skan
7706169689Skan  /* Search for the file name part.  */
7707169689Skan  f = strrchr (f, '/');
7708169689Skan  fi->fname = f == NULL ? fi->path : f + 1;
7709169689Skan  return 1;
7710169689Skan}
7711169689Skan
771290075Sobrien/* Output the directory table and the file name table.  We try to minimize
771390075Sobrien   the total amount of memory needed.  A heuristic is used to avoid large
771490075Sobrien   slowdowns with many input files.  */
771590075Sobrien
771690075Sobrienstatic void
7717132718Skanoutput_file_names (void)
771890075Sobrien{
7719169689Skan  struct file_name_acquire_data fnad;
7720169689Skan  int numfiles;
772190075Sobrien  struct file_info *files;
772290075Sobrien  struct dir_info *dirs;
772390075Sobrien  int *saved;
772490075Sobrien  int *savehere;
772590075Sobrien  int *backmap;
7726169689Skan  int ndirs;
772790075Sobrien  int idx_offset;
7728169689Skan  int i;
772990075Sobrien  int idx;
773090075Sobrien
7731169689Skan  if (!last_emitted_file)
7732132718Skan    {
7733132718Skan      dw2_asm_output_data (1, 0, "End directory table");
7734132718Skan      dw2_asm_output_data (1, 0, "End file name table");
7735132718Skan      return;
7736132718Skan    }
7737132718Skan
7738169689Skan  numfiles = last_emitted_file->emitted_number;
7739169689Skan
774090075Sobrien  /* Allocate the various arrays we need.  */
7741169689Skan  files = alloca (numfiles * sizeof (struct file_info));
7742169689Skan  dirs = alloca (numfiles * sizeof (struct dir_info));
774390075Sobrien
7744169689Skan  fnad.files = files;
7745169689Skan  fnad.used_files = 0;
7746169689Skan  fnad.max_files = numfiles;
7747169689Skan  htab_traverse (file_table, file_name_acquire, &fnad);
7748169689Skan  gcc_assert (fnad.used_files == fnad.max_files);
774990075Sobrien
7750169689Skan  qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
775190075Sobrien
775290075Sobrien  /* Find all the different directories used.  */
7753169689Skan  dirs[0].path = files[0].path;
7754169689Skan  dirs[0].length = files[0].fname - files[0].path;
775590075Sobrien  dirs[0].prefix = -1;
775690075Sobrien  dirs[0].count = 1;
775790075Sobrien  dirs[0].dir_idx = 0;
7758169689Skan  files[0].dir_idx = 0;
775990075Sobrien  ndirs = 1;
776090075Sobrien
7761169689Skan  for (i = 1; i < numfiles; i++)
776290075Sobrien    if (files[i].fname - files[i].path == dirs[ndirs - 1].length
776390075Sobrien	&& memcmp (dirs[ndirs - 1].path, files[i].path,
776490075Sobrien		   dirs[ndirs - 1].length) == 0)
776590075Sobrien      {
776690075Sobrien	/* Same directory as last entry.  */
776790075Sobrien	files[i].dir_idx = ndirs - 1;
776890075Sobrien	++dirs[ndirs - 1].count;
776990075Sobrien      }
777090075Sobrien    else
777190075Sobrien      {
7772169689Skan	int j;
777390075Sobrien
777490075Sobrien	/* This is a new directory.  */
777590075Sobrien	dirs[ndirs].path = files[i].path;
777690075Sobrien	dirs[ndirs].length = files[i].fname - files[i].path;
777790075Sobrien	dirs[ndirs].count = 1;
777890075Sobrien	dirs[ndirs].dir_idx = ndirs;
777990075Sobrien	files[i].dir_idx = ndirs;
778090075Sobrien
778190075Sobrien	/* Search for a prefix.  */
778290075Sobrien	dirs[ndirs].prefix = -1;
778390075Sobrien	for (j = 0; j < ndirs; j++)
778490075Sobrien	  if (dirs[j].length < dirs[ndirs].length
778590075Sobrien	      && dirs[j].length > 1
778690075Sobrien	      && (dirs[ndirs].prefix == -1
778790075Sobrien		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
778890075Sobrien	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
778990075Sobrien	    dirs[ndirs].prefix = j;
779090075Sobrien
779190075Sobrien	++ndirs;
779290075Sobrien      }
779390075Sobrien
779490075Sobrien  /* Now to the actual work.  We have to find a subset of the directories which
779590075Sobrien     allow expressing the file name using references to the directory table
779690075Sobrien     with the least amount of characters.  We do not do an exhaustive search
779790075Sobrien     where we would have to check out every combination of every single
779890075Sobrien     possible prefix.  Instead we use a heuristic which provides nearly optimal
779990075Sobrien     results in most cases and never is much off.  */
7800132718Skan  saved = alloca (ndirs * sizeof (int));
7801132718Skan  savehere = alloca (ndirs * sizeof (int));
780290075Sobrien
780390075Sobrien  memset (saved, '\0', ndirs * sizeof (saved[0]));
780490075Sobrien  for (i = 0; i < ndirs; i++)
780590075Sobrien    {
7806169689Skan      int j;
780790075Sobrien      int total;
780890075Sobrien
780990075Sobrien      /* We can always save some space for the current directory.  But this
781090075Sobrien	 does not mean it will be enough to justify adding the directory.  */
781190075Sobrien      savehere[i] = dirs[i].length;
781290075Sobrien      total = (savehere[i] - saved[i]) * dirs[i].count;
781390075Sobrien
781490075Sobrien      for (j = i + 1; j < ndirs; j++)
781590075Sobrien	{
781690075Sobrien	  savehere[j] = 0;
781790075Sobrien	  if (saved[j] < dirs[i].length)
781890075Sobrien	    {
781990075Sobrien	      /* Determine whether the dirs[i] path is a prefix of the
782090075Sobrien		 dirs[j] path.  */
782190075Sobrien	      int k;
782290075Sobrien
782390075Sobrien	      k = dirs[j].prefix;
7824132718Skan	      while (k != -1 && k != (int) i)
782590075Sobrien		k = dirs[k].prefix;
782690075Sobrien
7827132718Skan	      if (k == (int) i)
782890075Sobrien		{
7829169689Skan		  /* Yes it is.  We can possibly save some memory by
783090075Sobrien		     writing the filenames in dirs[j] relative to
783190075Sobrien		     dirs[i].  */
783290075Sobrien		  savehere[j] = dirs[i].length;
783390075Sobrien		  total += (savehere[j] - saved[j]) * dirs[j].count;
783490075Sobrien		}
783590075Sobrien	    }
783650397Sobrien	}
783750397Sobrien
7838169689Skan      /* Check whether we can save enough to justify adding the dirs[i]
783990075Sobrien	 directory.  */
784090075Sobrien      if (total > dirs[i].length + 1)
784190075Sobrien	{
784290075Sobrien	  /* It's worthwhile adding.  */
7843117395Skan	  for (j = i; j < ndirs; j++)
784490075Sobrien	    if (savehere[j] > 0)
784590075Sobrien	      {
784690075Sobrien		/* Remember how much we saved for this directory so far.  */
784790075Sobrien		saved[j] = savehere[j];
784850397Sobrien
784990075Sobrien		/* Remember the prefix directory.  */
785090075Sobrien		dirs[j].dir_idx = i;
785190075Sobrien	      }
785290075Sobrien	}
785390075Sobrien    }
785450397Sobrien
7855169689Skan  /* Emit the directory name table.  */
785690075Sobrien  idx = 1;
785790075Sobrien  idx_offset = dirs[0].length > 0 ? 1 : 0;
785890075Sobrien  for (i = 1 - idx_offset; i < ndirs; i++)
7859169689Skan    dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7860169689Skan			    "Directory Entry: 0x%x", i + idx_offset);
786190075Sobrien
786290075Sobrien  dw2_asm_output_data (1, 0, "End directory table");
786390075Sobrien
7864169689Skan  /* We have to emit them in the order of emitted_number since that's
7865169689Skan     used in the debug info generation.  To do this efficiently we
7866169689Skan     generate a back-mapping of the indices first.  */
7867169689Skan  backmap = alloca (numfiles * sizeof (int));
7868169689Skan  for (i = 0; i < numfiles; i++)
7869169689Skan    backmap[files[i].file_idx->emitted_number - 1] = i;
787090075Sobrien
787190075Sobrien  /* Now write all the file names.  */
7872169689Skan  for (i = 0; i < numfiles; i++)
787390075Sobrien    {
787490075Sobrien      int file_idx = backmap[i];
787590075Sobrien      int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
787690075Sobrien
787790075Sobrien      dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7878169689Skan			      "File Entry: 0x%x", (unsigned) i + 1);
787990075Sobrien
788090075Sobrien      /* Include directory index.  */
7881169689Skan      dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
788290075Sobrien
788390075Sobrien      /* Modification time.  */
788490075Sobrien      dw2_asm_output_data_uleb128 (0, NULL);
788590075Sobrien
788690075Sobrien      /* File length in bytes.  */
788790075Sobrien      dw2_asm_output_data_uleb128 (0, NULL);
788890075Sobrien    }
788990075Sobrien
789090075Sobrien  dw2_asm_output_data (1, 0, "End file name table");
789150397Sobrien}
789250397Sobrien
789390075Sobrien
789450397Sobrien/* Output the source line number correspondence information.  This
789590075Sobrien   information goes into the .debug_line section.  */
789650397Sobrien
789750397Sobrienstatic void
7898132718Skanoutput_line_info (void)
789950397Sobrien{
790090075Sobrien  char l1[20], l2[20], p1[20], p2[20];
790150397Sobrien  char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
790250397Sobrien  char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
790390075Sobrien  unsigned opc;
790490075Sobrien  unsigned n_op_args;
790590075Sobrien  unsigned long lt_index;
790690075Sobrien  unsigned long current_line;
790790075Sobrien  long line_offset;
790890075Sobrien  long line_delta;
790990075Sobrien  unsigned long current_file;
791090075Sobrien  unsigned long function;
791150397Sobrien
791290075Sobrien  ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
791390075Sobrien  ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
791490075Sobrien  ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
791590075Sobrien  ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
791650397Sobrien
7917132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7918132718Skan    dw2_asm_output_data (4, 0xffffffff,
7919132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
792090075Sobrien  dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
792190075Sobrien			"Length of Source Line Info");
792290075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l1);
792350397Sobrien
792490075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
792590075Sobrien  dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
792690075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, p1);
792750397Sobrien
792896263Sobrien  /* Define the architecture-dependent minimum instruction length (in
792996263Sobrien   bytes).  In this implementation of DWARF, this field is used for
793096263Sobrien   information purposes only.  Since GCC generates assembly language,
793196263Sobrien   we have no a priori knowledge of how many instruction bytes are
793296263Sobrien   generated for each source line, and therefore can use only the
793396263Sobrien   DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
793496263Sobrien   commands.  Accordingly, we fix this as `1', which is "correct
793596263Sobrien   enough" for all architectures, and don't let the target override.  */
793696263Sobrien  dw2_asm_output_data (1, 1,
793790075Sobrien		       "Minimum Instruction Length");
793896263Sobrien
793990075Sobrien  dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
794090075Sobrien		       "Default is_stmt_start flag");
794190075Sobrien  dw2_asm_output_data (1, DWARF_LINE_BASE,
794290075Sobrien		       "Line Base Value (Special Opcodes)");
794390075Sobrien  dw2_asm_output_data (1, DWARF_LINE_RANGE,
794490075Sobrien		       "Line Range Value (Special Opcodes)");
794590075Sobrien  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
794690075Sobrien		       "Special Opcode Base");
794750397Sobrien
794890075Sobrien  for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
794950397Sobrien    {
795050397Sobrien      switch (opc)
795150397Sobrien	{
795250397Sobrien	case DW_LNS_advance_pc:
795350397Sobrien	case DW_LNS_advance_line:
795450397Sobrien	case DW_LNS_set_file:
795550397Sobrien	case DW_LNS_set_column:
795650397Sobrien	case DW_LNS_fixed_advance_pc:
795750397Sobrien	  n_op_args = 1;
795850397Sobrien	  break;
795950397Sobrien	default:
796050397Sobrien	  n_op_args = 0;
796150397Sobrien	  break;
796250397Sobrien	}
796350397Sobrien
796490075Sobrien      dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
796590075Sobrien			   opc, n_op_args);
796650397Sobrien    }
796750397Sobrien
796890075Sobrien  /* Write out the information about the files we use.  */
796990075Sobrien  output_file_names ();
797090075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, p2);
797150397Sobrien
797252284Sobrien  /* We used to set the address register to the first location in the text
797352284Sobrien     section here, but that didn't accomplish anything since we already
797452284Sobrien     have a line note for the opening brace of the first function.  */
797550397Sobrien
797650397Sobrien  /* Generate the line number to PC correspondence table, encoded as
797750397Sobrien     a series of state machine operations.  */
797850397Sobrien  current_file = 1;
797950397Sobrien  current_line = 1;
7980169689Skan
7981169689Skan  if (cfun && in_cold_section_p)
7982169689Skan    strcpy (prev_line_label, cfun->cold_section_label);
7983169689Skan  else
7984169689Skan    strcpy (prev_line_label, text_section_label);
798550397Sobrien  for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
798650397Sobrien    {
798790075Sobrien      dw_line_info_ref line_info = &line_info_table[lt_index];
798850397Sobrien
798990075Sobrien#if 0
799090075Sobrien      /* Disable this optimization for now; GDB wants to see two line notes
799190075Sobrien	 at the beginning of a function so it can find the end of the
799290075Sobrien	 prologue.  */
799390075Sobrien
799452284Sobrien      /* Don't emit anything for redundant notes.  Just updating the
7995132718Skan	 address doesn't accomplish anything, because we already assume
7996132718Skan	 that anything after the last address is this line.  */
799752284Sobrien      if (line_info->dw_line_num == current_line
799852284Sobrien	  && line_info->dw_file_num == current_file)
799952284Sobrien	continue;
800090075Sobrien#endif
800152284Sobrien
800290075Sobrien      /* Emit debug info for the address of the current line.
800390075Sobrien
800490075Sobrien	 Unfortunately, we have little choice here currently, and must always
800590075Sobrien	 use the most general form.  GCC does not know the address delta
800690075Sobrien	 itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
800790075Sobrien	 attributes which will give an upper bound on the address range.  We
800890075Sobrien	 could perhaps use length attributes to determine when it is safe to
800990075Sobrien	 use DW_LNS_fixed_advance_pc.  */
801090075Sobrien
801150397Sobrien      ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
801250397Sobrien      if (0)
801350397Sobrien	{
801450397Sobrien	  /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
801590075Sobrien	  dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
801690075Sobrien			       "DW_LNS_fixed_advance_pc");
801790075Sobrien	  dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
801850397Sobrien	}
801950397Sobrien      else
802050397Sobrien	{
802190075Sobrien	  /* This can handle any delta.  This takes
8022132718Skan	     4+DWARF2_ADDR_SIZE bytes.  */
802390075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
802490075Sobrien	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
802590075Sobrien	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
802690075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
802750397Sobrien	}
802890075Sobrien
802950397Sobrien      strcpy (prev_line_label, line_label);
803050397Sobrien
803150397Sobrien      /* Emit debug info for the source file of the current line, if
803250397Sobrien	 different from the previous line.  */
803350397Sobrien      if (line_info->dw_file_num != current_file)
803450397Sobrien	{
803550397Sobrien	  current_file = line_info->dw_file_num;
803690075Sobrien	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8037169689Skan	  dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
803850397Sobrien	}
803950397Sobrien
804050397Sobrien      /* Emit debug info for the current line number, choosing the encoding
804150397Sobrien	 that uses the least amount of space.  */
804252284Sobrien      if (line_info->dw_line_num != current_line)
804350397Sobrien	{
804452284Sobrien	  line_offset = line_info->dw_line_num - current_line;
804552284Sobrien	  line_delta = line_offset - DWARF_LINE_BASE;
804652284Sobrien	  current_line = line_info->dw_line_num;
804752284Sobrien	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
804890075Sobrien	    /* This can handle deltas from -10 to 234, using the current
804990075Sobrien	       definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
805090075Sobrien	       takes 1 byte.  */
805190075Sobrien	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
805290075Sobrien				 "line %lu", current_line);
805352284Sobrien	  else
805452284Sobrien	    {
805552284Sobrien	      /* This can handle any delta.  This takes at least 4 bytes,
805652284Sobrien		 depending on the value being encoded.  */
805790075Sobrien	      dw2_asm_output_data (1, DW_LNS_advance_line,
805890075Sobrien				   "advance to line %lu", current_line);
805990075Sobrien	      dw2_asm_output_data_sleb128 (line_offset, NULL);
806090075Sobrien	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
806152284Sobrien	    }
806250397Sobrien	}
806350397Sobrien      else
806490075Sobrien	/* We still need to start a new row, so output a copy insn.  */
806590075Sobrien	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
806650397Sobrien    }
806750397Sobrien
806850397Sobrien  /* Emit debug info for the address of the end of the function.  */
806950397Sobrien  if (0)
807050397Sobrien    {
807190075Sobrien      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
807290075Sobrien			   "DW_LNS_fixed_advance_pc");
807390075Sobrien      dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
807450397Sobrien    }
807550397Sobrien  else
807650397Sobrien    {
807790075Sobrien      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
807890075Sobrien      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
807990075Sobrien      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
808090075Sobrien      dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
808150397Sobrien    }
808250397Sobrien
808390075Sobrien  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
808490075Sobrien  dw2_asm_output_data_uleb128 (1, NULL);
808590075Sobrien  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
808650397Sobrien
808750397Sobrien  function = 0;
808850397Sobrien  current_file = 1;
808950397Sobrien  current_line = 1;
809090075Sobrien  for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
809150397Sobrien    {
809290075Sobrien      dw_separate_line_info_ref line_info
809350397Sobrien	= &separate_line_info_table[lt_index];
809450397Sobrien
809590075Sobrien#if 0
809652284Sobrien      /* Don't emit anything for redundant notes.  */
809752284Sobrien      if (line_info->dw_line_num == current_line
809852284Sobrien	  && line_info->dw_file_num == current_file
809952284Sobrien	  && line_info->function == function)
810052284Sobrien	goto cont;
810190075Sobrien#endif
810252284Sobrien
810350397Sobrien      /* Emit debug info for the address of the current line.  If this is
810450397Sobrien	 a new function, or the first line of a function, then we need
810550397Sobrien	 to handle it differently.  */
810650397Sobrien      ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
810750397Sobrien				   lt_index);
810850397Sobrien      if (function != line_info->function)
810950397Sobrien	{
811050397Sobrien	  function = line_info->function;
811150397Sobrien
8112132718Skan	  /* Set the address register to the first line in the function.  */
811390075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
811490075Sobrien	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
811590075Sobrien	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
811690075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
811750397Sobrien	}
811850397Sobrien      else
811950397Sobrien	{
812050397Sobrien	  /* ??? See the DW_LNS_advance_pc comment above.  */
812150397Sobrien	  if (0)
812250397Sobrien	    {
812390075Sobrien	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
812490075Sobrien				   "DW_LNS_fixed_advance_pc");
812590075Sobrien	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
812650397Sobrien	    }
812750397Sobrien	  else
812850397Sobrien	    {
812990075Sobrien	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
813090075Sobrien	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
813190075Sobrien	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
813290075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
813350397Sobrien	    }
813450397Sobrien	}
813590075Sobrien
813650397Sobrien      strcpy (prev_line_label, line_label);
813750397Sobrien
813850397Sobrien      /* Emit debug info for the source file of the current line, if
813950397Sobrien	 different from the previous line.  */
814050397Sobrien      if (line_info->dw_file_num != current_file)
814150397Sobrien	{
814250397Sobrien	  current_file = line_info->dw_file_num;
814390075Sobrien	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8144169689Skan	  dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
814550397Sobrien	}
814650397Sobrien
814750397Sobrien      /* Emit debug info for the current line number, choosing the encoding
814850397Sobrien	 that uses the least amount of space.  */
814950397Sobrien      if (line_info->dw_line_num != current_line)
815050397Sobrien	{
815150397Sobrien	  line_offset = line_info->dw_line_num - current_line;
815250397Sobrien	  line_delta = line_offset - DWARF_LINE_BASE;
815350397Sobrien	  current_line = line_info->dw_line_num;
815450397Sobrien	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
815590075Sobrien	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
815690075Sobrien				 "line %lu", current_line);
815750397Sobrien	  else
815850397Sobrien	    {
815990075Sobrien	      dw2_asm_output_data (1, DW_LNS_advance_line,
816090075Sobrien				   "advance to line %lu", current_line);
816190075Sobrien	      dw2_asm_output_data_sleb128 (line_offset, NULL);
816290075Sobrien	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
816350397Sobrien	    }
816450397Sobrien	}
816552284Sobrien      else
816690075Sobrien	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
816750397Sobrien
816890075Sobrien#if 0
816952284Sobrien    cont:
817090075Sobrien#endif
817150397Sobrien
817290075Sobrien      lt_index++;
817390075Sobrien
817450397Sobrien      /* If we're done with a function, end its sequence.  */
817550397Sobrien      if (lt_index == separate_line_info_table_in_use
817650397Sobrien	  || separate_line_info_table[lt_index].function != function)
817750397Sobrien	{
817850397Sobrien	  current_file = 1;
817950397Sobrien	  current_line = 1;
818050397Sobrien
818150397Sobrien	  /* Emit debug info for the address of the end of the function.  */
818250397Sobrien	  ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
818350397Sobrien	  if (0)
818450397Sobrien	    {
818590075Sobrien	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
818690075Sobrien				   "DW_LNS_fixed_advance_pc");
818790075Sobrien	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
818850397Sobrien	    }
818950397Sobrien	  else
819050397Sobrien	    {
819190075Sobrien	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
819290075Sobrien	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
819390075Sobrien	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
819490075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
819550397Sobrien	    }
819650397Sobrien
819750397Sobrien	  /* Output the marker for the end of this sequence.  */
819890075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
819990075Sobrien	  dw2_asm_output_data_uleb128 (1, NULL);
820090075Sobrien	  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
820150397Sobrien	}
820250397Sobrien    }
820390075Sobrien
820490075Sobrien  /* Output the marker for the end of the line number info.  */
820590075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l2);
820650397Sobrien}
820750397Sobrien
820850397Sobrien/* Given a pointer to a tree node for some base type, return a pointer to
820950397Sobrien   a DIE that describes the given type.
821050397Sobrien
821150397Sobrien   This routine must only be called for GCC type nodes that correspond to
821250397Sobrien   Dwarf base (fundamental) types.  */
821350397Sobrien
821450397Sobrienstatic dw_die_ref
8215132718Skanbase_type_die (tree type)
821650397Sobrien{
821790075Sobrien  dw_die_ref base_type_result;
821890075Sobrien  enum dwarf_type encoding;
821950397Sobrien
822090075Sobrien  if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
822150397Sobrien    return 0;
822250397Sobrien
822350397Sobrien  switch (TREE_CODE (type))
822450397Sobrien    {
822550397Sobrien    case INTEGER_TYPE:
8226169689Skan      if (TYPE_STRING_FLAG (type))
822750397Sobrien	{
8228169689Skan	  if (TYPE_UNSIGNED (type))
8229169689Skan	    encoding = DW_ATE_unsigned_char;
823050397Sobrien	  else
8231169689Skan	    encoding = DW_ATE_signed_char;
823250397Sobrien	}
8233169689Skan      else if (TYPE_UNSIGNED (type))
8234169689Skan	encoding = DW_ATE_unsigned;
823550397Sobrien      else
8236169689Skan	encoding = DW_ATE_signed;
823750397Sobrien      break;
823850397Sobrien
823950397Sobrien    case REAL_TYPE:
8240169689Skan      if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8241169689Skan	encoding = DW_ATE_decimal_float;
8242169689Skan      else
8243169689Skan	encoding = DW_ATE_float;
824450397Sobrien      break;
824550397Sobrien
824690075Sobrien      /* Dwarf2 doesn't know anything about complex ints, so use
824790075Sobrien	 a user defined type for it.  */
824850397Sobrien    case COMPLEX_TYPE:
824990075Sobrien      if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
825090075Sobrien	encoding = DW_ATE_complex_float;
825190075Sobrien      else
825290075Sobrien	encoding = DW_ATE_lo_user;
825350397Sobrien      break;
825450397Sobrien
825550397Sobrien    case BOOLEAN_TYPE:
825650397Sobrien      /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
825750397Sobrien      encoding = DW_ATE_boolean;
825850397Sobrien      break;
825950397Sobrien
826050397Sobrien    default:
826190075Sobrien      /* No other TREE_CODEs are Dwarf fundamental types.  */
8262169689Skan      gcc_unreachable ();
826350397Sobrien    }
826450397Sobrien
826590075Sobrien  base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
826690075Sobrien
8267169689Skan  /* This probably indicates a bug.  */
8268169689Skan  if (! TYPE_NAME (type))
8269169689Skan    add_name_attribute (base_type_result, "__unknown__");
8270169689Skan
827150397Sobrien  add_AT_unsigned (base_type_result, DW_AT_byte_size,
827250397Sobrien		   int_size_in_bytes (type));
827350397Sobrien  add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
827450397Sobrien
827550397Sobrien  return base_type_result;
827650397Sobrien}
827750397Sobrien
827850397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
827950397Sobrien   the Dwarf "root" type for the given input type.  The Dwarf "root" type of
828050397Sobrien   a given type is generally the same as the given type, except that if the
828150397Sobrien   given type is a pointer or reference type, then the root type of the given
828250397Sobrien   type is the root type of the "basis" type for the pointer or reference
828350397Sobrien   type.  (This definition of the "root" type is recursive.) Also, the root
828450397Sobrien   type of a `const' qualified type or a `volatile' qualified type is the
828550397Sobrien   root type of the given type without the qualifiers.  */
828650397Sobrien
828750397Sobrienstatic tree
8288132718Skanroot_type (tree type)
828950397Sobrien{
829050397Sobrien  if (TREE_CODE (type) == ERROR_MARK)
829150397Sobrien    return error_mark_node;
829250397Sobrien
829350397Sobrien  switch (TREE_CODE (type))
829450397Sobrien    {
829550397Sobrien    case ERROR_MARK:
829650397Sobrien      return error_mark_node;
829750397Sobrien
829850397Sobrien    case POINTER_TYPE:
829950397Sobrien    case REFERENCE_TYPE:
830050397Sobrien      return type_main_variant (root_type (TREE_TYPE (type)));
830150397Sobrien
830250397Sobrien    default:
830350397Sobrien      return type_main_variant (type);
830450397Sobrien    }
830550397Sobrien}
830650397Sobrien
8307117395Skan/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
830850397Sobrien   given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
830950397Sobrien
831050397Sobrienstatic inline int
8311132718Skanis_base_type (tree type)
831250397Sobrien{
831350397Sobrien  switch (TREE_CODE (type))
831450397Sobrien    {
831550397Sobrien    case ERROR_MARK:
831650397Sobrien    case VOID_TYPE:
831750397Sobrien    case INTEGER_TYPE:
831850397Sobrien    case REAL_TYPE:
831950397Sobrien    case COMPLEX_TYPE:
832050397Sobrien    case BOOLEAN_TYPE:
832150397Sobrien      return 1;
832250397Sobrien
832350397Sobrien    case ARRAY_TYPE:
832450397Sobrien    case RECORD_TYPE:
832550397Sobrien    case UNION_TYPE:
832650397Sobrien    case QUAL_UNION_TYPE:
832750397Sobrien    case ENUMERAL_TYPE:
832850397Sobrien    case FUNCTION_TYPE:
832950397Sobrien    case METHOD_TYPE:
833050397Sobrien    case POINTER_TYPE:
833150397Sobrien    case REFERENCE_TYPE:
833250397Sobrien    case OFFSET_TYPE:
833350397Sobrien    case LANG_TYPE:
833490075Sobrien    case VECTOR_TYPE:
833550397Sobrien      return 0;
833650397Sobrien
833750397Sobrien    default:
8338169689Skan      gcc_unreachable ();
833950397Sobrien    }
834050397Sobrien
834150397Sobrien  return 0;
834250397Sobrien}
834350397Sobrien
8344132718Skan/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8345132718Skan   node, return the size in bits for the type if it is a constant, or else
8346132718Skan   return the alignment for the type if the type's size is not constant, or
8347132718Skan   else return BITS_PER_WORD if the type actually turns out to be an
8348132718Skan   ERROR_MARK node.  */
8349132718Skan
8350132718Skanstatic inline unsigned HOST_WIDE_INT
8351132718Skansimple_type_size_in_bits (tree type)
8352132718Skan{
8353132718Skan  if (TREE_CODE (type) == ERROR_MARK)
8354132718Skan    return BITS_PER_WORD;
8355132718Skan  else if (TYPE_SIZE (type) == NULL_TREE)
8356132718Skan    return 0;
8357132718Skan  else if (host_integerp (TYPE_SIZE (type), 1))
8358132718Skan    return tree_low_cst (TYPE_SIZE (type), 1);
8359132718Skan  else
8360132718Skan    return TYPE_ALIGN (type);
8361132718Skan}
8362132718Skan
8363132718Skan/* Return true if the debug information for the given type should be
8364132718Skan   emitted as a subrange type.  */
8365132718Skan
8366132718Skanstatic inline bool
8367132718Skanis_subrange_type (tree type)
8368132718Skan{
8369132718Skan  tree subtype = TREE_TYPE (type);
8370132718Skan
8371169689Skan  /* Subrange types are identified by the fact that they are integer
8372169689Skan     types, and that they have a subtype which is either an integer type
8373169689Skan     or an enumeral type.  */
8374169689Skan
8375169689Skan  if (TREE_CODE (type) != INTEGER_TYPE
8376169689Skan      || subtype == NULL_TREE)
8377169689Skan    return false;
8378169689Skan
8379169689Skan  if (TREE_CODE (subtype) != INTEGER_TYPE
8380169689Skan      && TREE_CODE (subtype) != ENUMERAL_TYPE)
8381169689Skan    return false;
8382169689Skan
8383169689Skan  if (TREE_CODE (type) == TREE_CODE (subtype)
8384169689Skan      && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8385169689Skan      && TYPE_MIN_VALUE (type) != NULL
8386169689Skan      && TYPE_MIN_VALUE (subtype) != NULL
8387169689Skan      && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8388169689Skan      && TYPE_MAX_VALUE (type) != NULL
8389169689Skan      && TYPE_MAX_VALUE (subtype) != NULL
8390169689Skan      && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8391132718Skan    {
8392169689Skan      /* The type and its subtype have the same representation.  If in
8393169689Skan         addition the two types also have the same name, then the given
8394169689Skan         type is not a subrange type, but rather a plain base type.  */
8395169689Skan      /* FIXME: brobecker/2004-03-22:
8396169689Skan         Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8397169689Skan         therefore be sufficient to check the TYPE_SIZE node pointers
8398169689Skan         rather than checking the actual size.  Unfortunately, we have
8399169689Skan         found some cases, such as in the Ada "integer" type, where
8400169689Skan         this is not the case.  Until this problem is solved, we need to
8401169689Skan         keep checking the actual size.  */
8402169689Skan      tree type_name = TYPE_NAME (type);
8403169689Skan      tree subtype_name = TYPE_NAME (subtype);
8404169689Skan
8405169689Skan      if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8406169689Skan        type_name = DECL_NAME (type_name);
8407169689Skan
8408169689Skan      if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8409169689Skan        subtype_name = DECL_NAME (subtype_name);
8410169689Skan
8411169689Skan      if (type_name == subtype_name)
8412169689Skan        return false;
8413132718Skan    }
8414169689Skan
8415169689Skan  return true;
8416132718Skan}
8417132718Skan
8418132718Skan/*  Given a pointer to a tree node for a subrange type, return a pointer
8419132718Skan    to a DIE that describes the given type.  */
8420132718Skan
8421132718Skanstatic dw_die_ref
8422132718Skansubrange_type_die (tree type, dw_die_ref context_die)
8423132718Skan{
8424132718Skan  dw_die_ref subrange_die;
8425132718Skan  const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8426132718Skan
8427132718Skan  if (context_die == NULL)
8428132718Skan    context_die = comp_unit_die;
8429132718Skan
8430132718Skan  subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8431132718Skan
8432132718Skan  if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8433132718Skan    {
8434132718Skan      /* The size of the subrange type and its base type do not match,
8435132718Skan         so we need to generate a size attribute for the subrange type.  */
8436132718Skan      add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8437132718Skan    }
8438132718Skan
8439132718Skan  if (TYPE_MIN_VALUE (type) != NULL)
8440132718Skan    add_bound_info (subrange_die, DW_AT_lower_bound,
8441132718Skan                    TYPE_MIN_VALUE (type));
8442132718Skan  if (TYPE_MAX_VALUE (type) != NULL)
8443132718Skan    add_bound_info (subrange_die, DW_AT_upper_bound,
8444132718Skan                    TYPE_MAX_VALUE (type));
8445132718Skan
8446132718Skan  return subrange_die;
8447132718Skan}
8448132718Skan
844950397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
845050397Sobrien   entry that chains various modifiers in front of the given type.  */
845150397Sobrien
845250397Sobrienstatic dw_die_ref
8453132718Skanmodified_type_die (tree type, int is_const_type, int is_volatile_type,
8454132718Skan		   dw_die_ref context_die)
845550397Sobrien{
845690075Sobrien  enum tree_code code = TREE_CODE (type);
8457169689Skan  dw_die_ref mod_type_die;
845890075Sobrien  dw_die_ref sub_die = NULL;
845990075Sobrien  tree item_type = NULL;
8460169689Skan  tree qualified_type;
8461169689Skan  tree name;
846250397Sobrien
8463169689Skan  if (code == ERROR_MARK)
8464169689Skan    return NULL;
8465169689Skan
8466169689Skan  /* See if we already have the appropriately qualified variant of
8467169689Skan     this type.  */
8468169689Skan  qualified_type
8469169689Skan    = get_qualified_type (type,
8470169689Skan			  ((is_const_type ? TYPE_QUAL_CONST : 0)
8471169689Skan			   | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8472169689Skan
8473169689Skan  /* If we do, then we can just use its DIE, if it exists.  */
8474169689Skan  if (qualified_type)
847550397Sobrien    {
8476169689Skan      mod_type_die = lookup_type_die (qualified_type);
847750397Sobrien      if (mod_type_die)
8478169689Skan	return mod_type_die;
8479169689Skan    }
8480169689Skan
8481169689Skan  name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8482169689Skan
8483169689Skan  /* Handle C typedef types.  */
8484169689Skan  if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8485169689Skan    {
8486169689Skan      tree dtype = TREE_TYPE (name);
8487169689Skan
8488169689Skan      if (qualified_type == dtype)
848950397Sobrien	{
8490169689Skan	  /* For a named type, use the typedef.  */
8491169689Skan	  gen_type_die (qualified_type, context_die);
8492169689Skan	  return lookup_type_die (qualified_type);
849350397Sobrien	}
8494169689Skan      else if (is_const_type < TYPE_READONLY (dtype)
8495169689Skan	       || is_volatile_type < TYPE_VOLATILE (dtype)
8496169689Skan	       || (is_const_type <= TYPE_READONLY (dtype)
8497169689Skan		   && is_volatile_type <= TYPE_VOLATILE (dtype)
8498169689Skan		   && DECL_ORIGINAL_TYPE (name) != type))
8499169689Skan	/* cv-unqualified version of named type.  Just use the unnamed
8500169689Skan	   type to which it refers.  */
8501169689Skan	return modified_type_die (DECL_ORIGINAL_TYPE (name),
8502169689Skan				  is_const_type, is_volatile_type,
8503169689Skan				  context_die);
8504169689Skan      /* Else cv-qualified version of named type; fall through.  */
8505169689Skan    }
8506169689Skan
8507169689Skan  if (is_const_type)
8508169689Skan    {
8509169689Skan      mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8510169689Skan      sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8511169689Skan    }
8512169689Skan  else if (is_volatile_type)
8513169689Skan    {
8514169689Skan      mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8515169689Skan      sub_die = modified_type_die (type, 0, 0, context_die);
8516169689Skan    }
8517169689Skan  else if (code == POINTER_TYPE)
8518169689Skan    {
8519169689Skan      mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8520169689Skan      add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8521169689Skan		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
8522169689Skan      item_type = TREE_TYPE (type);
8523169689Skan    }
8524169689Skan  else if (code == REFERENCE_TYPE)
8525169689Skan    {
8526169689Skan      mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8527169689Skan      add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8528169689Skan		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
8529169689Skan      item_type = TREE_TYPE (type);
8530169689Skan    }
8531169689Skan  else if (is_subrange_type (type))
8532169689Skan    {
8533169689Skan      mod_type_die = subrange_type_die (type, context_die);
8534169689Skan      item_type = TREE_TYPE (type);
8535169689Skan    }
8536169689Skan  else if (is_base_type (type))
8537169689Skan    mod_type_die = base_type_die (type);
8538169689Skan  else
8539169689Skan    {
8540169689Skan      gen_type_die (type, context_die);
8541169689Skan
8542169689Skan      /* We have to get the type_main_variant here (and pass that to the
8543169689Skan	 `lookup_type_die' routine) because the ..._TYPE node we have
8544169689Skan	 might simply be a *copy* of some original type node (where the
8545169689Skan	 copy was created to help us keep track of typedef names) and
8546169689Skan	 that copy might have a different TYPE_UID from the original
8547169689Skan	 ..._TYPE node.  */
8548169689Skan      if (TREE_CODE (type) != VECTOR_TYPE)
8549169689Skan	return lookup_type_die (type_main_variant (type));
855050397Sobrien      else
8551169689Skan	/* Vectors have the debugging information in the type,
8552169689Skan	   not the main variant.  */
8553169689Skan	return lookup_type_die (type);
855450397Sobrien    }
8555169689Skan
8556169689Skan  /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8557169689Skan     don't output a DW_TAG_typedef, since there isn't one in the
8558169689Skan     user's program; just attach a DW_AT_name to the type.  */
8559169689Skan  if (name
8560169689Skan      && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8561169689Skan    {
8562169689Skan      if (TREE_CODE (name) == TYPE_DECL)
8563169689Skan	/* Could just call add_name_and_src_coords_attributes here,
8564169689Skan	   but since this is a builtin type it doesn't have any
8565169689Skan	   useful source coordinates anyway.  */
8566169689Skan	name = DECL_NAME (name);
8567169689Skan      add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8568169689Skan    }
8569169689Skan
8570169689Skan  if (qualified_type)
8571169689Skan    equate_type_number_to_die (qualified_type, mod_type_die);
857250397Sobrien
857350397Sobrien  if (item_type)
857450397Sobrien    /* We must do this after the equate_type_number_to_die call, in case
857550397Sobrien       this is a recursive type.  This ensures that the modified_type_die
857650397Sobrien       recursion will terminate even if the type is recursive.  Recursive
857750397Sobrien       types are possible in Ada.  */
857850397Sobrien    sub_die = modified_type_die (item_type,
857950397Sobrien				 TYPE_READONLY (item_type),
858050397Sobrien				 TYPE_VOLATILE (item_type),
858150397Sobrien				 context_die);
858250397Sobrien
858350397Sobrien  if (sub_die != NULL)
858450397Sobrien    add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
858550397Sobrien
858650397Sobrien  return mod_type_die;
858750397Sobrien}
858850397Sobrien
858950397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
859090075Sobrien   an enumerated type.  */
859150397Sobrien
859250397Sobrienstatic inline int
8593132718Skantype_is_enum (tree type)
859450397Sobrien{
859550397Sobrien  return TREE_CODE (type) == ENUMERAL_TYPE;
859650397Sobrien}
859750397Sobrien
8598132718Skan/* Return the DBX register number described by a given RTL node.  */
859950397Sobrien
860090075Sobrienstatic unsigned int
8601132718Skandbx_reg_number (rtx rtl)
860290075Sobrien{
860390075Sobrien  unsigned regno = REGNO (rtl);
860490075Sobrien
8605169689Skan  gcc_assert (regno < FIRST_PSEUDO_REGISTER);
860690075Sobrien
8607169689Skan#ifdef LEAF_REG_REMAP
8608169689Skan  if (current_function_uses_only_leaf_regs)
8609169689Skan    {
8610169689Skan      int leaf_reg = LEAF_REG_REMAP (regno);
8611169689Skan      if (leaf_reg != -1)
8612169689Skan	regno = (unsigned) leaf_reg;
8613169689Skan    }
8614169689Skan#endif
8615169689Skan
861690075Sobrien  return DBX_REGISTER_NUMBER (regno);
861790075Sobrien}
861890075Sobrien
8619169689Skan/* Optionally add a DW_OP_piece term to a location description expression.
8620169689Skan   DW_OP_piece is only added if the location description expression already
8621169689Skan   doesn't end with DW_OP_piece.  */
8622169689Skan
8623169689Skanstatic void
8624169689Skanadd_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8625169689Skan{
8626169689Skan  dw_loc_descr_ref loc;
8627169689Skan
8628169689Skan  if (*list_head != NULL)
8629169689Skan    {
8630169689Skan      /* Find the end of the chain.  */
8631169689Skan      for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8632169689Skan	;
8633169689Skan
8634169689Skan      if (loc->dw_loc_opc != DW_OP_piece)
8635169689Skan	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8636169689Skan    }
8637169689Skan}
8638169689Skan
863990075Sobrien/* Return a location descriptor that designates a machine register or
8640132718Skan   zero if there is none.  */
864190075Sobrien
864250397Sobrienstatic dw_loc_descr_ref
8643132718Skanreg_loc_descriptor (rtx rtl)
864450397Sobrien{
8645132718Skan  rtx regs;
864650397Sobrien
864790075Sobrien  if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
864890075Sobrien    return 0;
864990075Sobrien
8650169689Skan  regs = targetm.dwarf_register_span (rtl);
8651132718Skan
8652169689Skan  if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8653132718Skan    return multiple_reg_loc_descriptor (rtl, regs);
865450397Sobrien  else
8655169689Skan    return one_reg_loc_descriptor (dbx_reg_number (rtl));
8656132718Skan}
865750397Sobrien
8658132718Skan/* Return a location descriptor that designates a machine register for
8659132718Skan   a given hard register number.  */
8660132718Skan
8661132718Skanstatic dw_loc_descr_ref
8662132718Skanone_reg_loc_descriptor (unsigned int regno)
8663132718Skan{
8664132718Skan  if (regno <= 31)
8665132718Skan    return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8666132718Skan  else
8667132718Skan    return new_loc_descr (DW_OP_regx, regno, 0);
8668132718Skan}
8669132718Skan
8670132718Skan/* Given an RTL of a register, return a location descriptor that
8671132718Skan   designates a value that spans more than one register.  */
8672132718Skan
8673132718Skanstatic dw_loc_descr_ref
8674132718Skanmultiple_reg_loc_descriptor (rtx rtl, rtx regs)
8675132718Skan{
8676132718Skan  int nregs, size, i;
8677132718Skan  unsigned reg;
8678132718Skan  dw_loc_descr_ref loc_result = NULL;
8679132718Skan
8680169689Skan  reg = REGNO (rtl);
8681169689Skan#ifdef LEAF_REG_REMAP
8682169689Skan  if (current_function_uses_only_leaf_regs)
8683169689Skan    {
8684169689Skan      int leaf_reg = LEAF_REG_REMAP (reg);
8685169689Skan      if (leaf_reg != -1)
8686169689Skan	reg = (unsigned) leaf_reg;
8687169689Skan    }
8688169689Skan#endif
8689169689Skan  gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8690169689Skan  nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8691132718Skan
8692132718Skan  /* Simple, contiguous registers.  */
8693132718Skan  if (regs == NULL_RTX)
8694132718Skan    {
8695132718Skan      size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8696132718Skan
8697132718Skan      loc_result = NULL;
8698132718Skan      while (nregs--)
8699132718Skan	{
8700132718Skan	  dw_loc_descr_ref t;
8701132718Skan
8702169689Skan	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8703132718Skan	  add_loc_descr (&loc_result, t);
8704169689Skan	  add_loc_descr_op_piece (&loc_result, size);
8705132718Skan	  ++reg;
8706132718Skan	}
8707132718Skan      return loc_result;
8708132718Skan    }
8709132718Skan
8710132718Skan  /* Now onto stupid register sets in non contiguous locations.  */
8711132718Skan
8712169689Skan  gcc_assert (GET_CODE (regs) == PARALLEL);
8713132718Skan
8714132718Skan  size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8715132718Skan  loc_result = NULL;
8716132718Skan
8717132718Skan  for (i = 0; i < XVECLEN (regs, 0); ++i)
8718132718Skan    {
8719132718Skan      dw_loc_descr_ref t;
8720132718Skan
8721132718Skan      t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8722132718Skan      add_loc_descr (&loc_result, t);
8723132718Skan      size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8724169689Skan      add_loc_descr_op_piece (&loc_result, size);
8725132718Skan    }
872650397Sobrien  return loc_result;
872750397Sobrien}
872850397Sobrien
872990075Sobrien/* Return a location descriptor that designates a constant.  */
873090075Sobrien
873190075Sobrienstatic dw_loc_descr_ref
8732132718Skanint_loc_descriptor (HOST_WIDE_INT i)
873390075Sobrien{
873490075Sobrien  enum dwarf_location_atom op;
873590075Sobrien
873690075Sobrien  /* Pick the smallest representation of a constant, rather than just
873790075Sobrien     defaulting to the LEB encoding.  */
873890075Sobrien  if (i >= 0)
873990075Sobrien    {
874090075Sobrien      if (i <= 31)
874190075Sobrien	op = DW_OP_lit0 + i;
874290075Sobrien      else if (i <= 0xff)
874390075Sobrien	op = DW_OP_const1u;
874490075Sobrien      else if (i <= 0xffff)
874590075Sobrien	op = DW_OP_const2u;
874690075Sobrien      else if (HOST_BITS_PER_WIDE_INT == 32
874790075Sobrien	       || i <= 0xffffffff)
874890075Sobrien	op = DW_OP_const4u;
874990075Sobrien      else
875090075Sobrien	op = DW_OP_constu;
875190075Sobrien    }
875290075Sobrien  else
875390075Sobrien    {
875490075Sobrien      if (i >= -0x80)
875590075Sobrien	op = DW_OP_const1s;
875690075Sobrien      else if (i >= -0x8000)
875790075Sobrien	op = DW_OP_const2s;
875890075Sobrien      else if (HOST_BITS_PER_WIDE_INT == 32
875990075Sobrien	       || i >= -0x80000000)
876090075Sobrien	op = DW_OP_const4s;
876190075Sobrien      else
876290075Sobrien	op = DW_OP_consts;
876390075Sobrien    }
876490075Sobrien
876590075Sobrien  return new_loc_descr (op, i, 0);
876690075Sobrien}
876790075Sobrien
876850397Sobrien/* Return a location descriptor that designates a base+offset location.  */
876950397Sobrien
877050397Sobrienstatic dw_loc_descr_ref
8771169689Skanbased_loc_descr (rtx reg, HOST_WIDE_INT offset)
877250397Sobrien{
8773169689Skan  unsigned int regno;
877450397Sobrien
8775169689Skan  /* We only use "frame base" when we're sure we're talking about the
8776169689Skan     post-prologue local stack frame.  We do this by *not* running
8777169689Skan     register elimination until this point, and recognizing the special
8778169689Skan     argument pointer and soft frame pointer rtx's.  */
8779169689Skan  if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8780169689Skan    {
8781169689Skan      rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8782169689Skan
8783169689Skan      if (elim != reg)
8784169689Skan	{
8785169689Skan	  if (GET_CODE (elim) == PLUS)
8786169689Skan	    {
8787169689Skan	      offset += INTVAL (XEXP (elim, 1));
8788169689Skan	      elim = XEXP (elim, 0);
8789169689Skan	    }
8790169689Skan	  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8791169689Skan		      : stack_pointer_rtx));
8792169689Skan          offset += frame_pointer_fb_offset;
8793169689Skan
8794169689Skan          return new_loc_descr (DW_OP_fbreg, offset, 0);
8795169689Skan	}
8796169689Skan    }
8797169689Skan
8798169689Skan  regno = dbx_reg_number (reg);
8799169689Skan  if (regno <= 31)
8800169689Skan    return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
880150397Sobrien  else
8802169689Skan    return new_loc_descr (DW_OP_bregx, regno, offset);
880350397Sobrien}
880450397Sobrien
880550397Sobrien/* Return true if this RTL expression describes a base+offset calculation.  */
880650397Sobrien
880750397Sobrienstatic inline int
8808132718Skanis_based_loc (rtx rtl)
880950397Sobrien{
881090075Sobrien  return (GET_CODE (rtl) == PLUS
8811169689Skan	  && ((REG_P (XEXP (rtl, 0))
881290075Sobrien	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
881390075Sobrien	       && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
881450397Sobrien}
881550397Sobrien
881650397Sobrien/* The following routine converts the RTL for a variable or parameter
881750397Sobrien   (resident in memory) into an equivalent Dwarf representation of a
881850397Sobrien   mechanism for getting the address of that same variable onto the top of a
881950397Sobrien   hypothetical "address evaluation" stack.
882050397Sobrien
882150397Sobrien   When creating memory location descriptors, we are effectively transforming
882250397Sobrien   the RTL for a memory-resident object into its Dwarf postfix expression
882350397Sobrien   equivalent.  This routine recursively descends an RTL tree, turning
882490075Sobrien   it into Dwarf postfix code as it goes.
882550397Sobrien
882690075Sobrien   MODE is the mode of the memory reference, needed to handle some
882790075Sobrien   autoincrement addressing modes.
882890075Sobrien
8829169689Skan   CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8830169689Skan   location list for RTL.
8831169689Skan
883290075Sobrien   Return 0 if we can't represent the location.  */
883390075Sobrien
883450397Sobrienstatic dw_loc_descr_ref
8835132718Skanmem_loc_descriptor (rtx rtl, enum machine_mode mode)
883650397Sobrien{
883750397Sobrien  dw_loc_descr_ref mem_loc_result = NULL;
8838169689Skan  enum dwarf_location_atom op;
883990075Sobrien
884090075Sobrien  /* Note that for a dynamically sized array, the location we will generate a
884150397Sobrien     description of here will be the lowest numbered location which is
884250397Sobrien     actually within the array.  That's *not* necessarily the same as the
884350397Sobrien     zeroth element of the array.  */
884450397Sobrien
8845169689Skan  rtl = targetm.delegitimize_address (rtl);
884690075Sobrien
884750397Sobrien  switch (GET_CODE (rtl))
884850397Sobrien    {
884990075Sobrien    case POST_INC:
885090075Sobrien    case POST_DEC:
885190075Sobrien    case POST_MODIFY:
885290075Sobrien      /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
885390075Sobrien	 just fall into the SUBREG code.  */
885490075Sobrien
885590075Sobrien      /* ... fall through ...  */
885690075Sobrien
885750397Sobrien    case SUBREG:
885850397Sobrien      /* The case of a subreg may arise when we have a local (register)
8859132718Skan	 variable or a formal (register) parameter which doesn't quite fill
8860132718Skan	 up an entire register.  For now, just assume that it is
8861132718Skan	 legitimate to make the Dwarf info refer to the whole register which
8862132718Skan	 contains the given subreg.  */
8863169689Skan      rtl = XEXP (rtl, 0);
886450397Sobrien
886590075Sobrien      /* ... fall through ...  */
886650397Sobrien
886750397Sobrien    case REG:
886850397Sobrien      /* Whenever a register number forms a part of the description of the
8869132718Skan	 method for calculating the (dynamic) address of a memory resident
8870132718Skan	 object, DWARF rules require the register number be referred to as
8871132718Skan	 a "base register".  This distinction is not based in any way upon
8872132718Skan	 what category of register the hardware believes the given register
8873132718Skan	 belongs to.  This is strictly DWARF terminology we're dealing with
8874132718Skan	 here. Note that in cases where the location of a memory-resident
8875132718Skan	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8876132718Skan	 OP_CONST (0)) the actual DWARF location descriptor that we generate
8877132718Skan	 may just be OP_BASEREG (basereg).  This may look deceptively like
8878132718Skan	 the object in question was allocated to a register (rather than in
8879132718Skan	 memory) so DWARF consumers need to be aware of the subtle
8880132718Skan	 distinction between OP_REG and OP_BASEREG.  */
888190075Sobrien      if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8882169689Skan	mem_loc_result = based_loc_descr (rtl, 0);
888350397Sobrien      break;
888450397Sobrien
888550397Sobrien    case MEM:
888690075Sobrien      mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
888790075Sobrien      if (mem_loc_result != 0)
888890075Sobrien	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
888950397Sobrien      break;
889050397Sobrien
8891117395Skan    case LO_SUM:
8892117395Skan	 rtl = XEXP (rtl, 1);
8893117395Skan
8894117395Skan      /* ... fall through ...  */
8895117395Skan
889690075Sobrien    case LABEL_REF:
889790075Sobrien      /* Some ports can transform a symbol ref into a label ref, because
8898132718Skan	 the symbol ref is too far away and has to be dumped into a constant
8899132718Skan	 pool.  */
890050397Sobrien    case CONST:
890150397Sobrien    case SYMBOL_REF:
890290075Sobrien      /* Alternatively, the symbol in the constant pool might be referenced
890390075Sobrien	 by a different symbol.  */
890490075Sobrien      if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
890590075Sobrien	{
890696263Sobrien	  bool marked;
890796263Sobrien	  rtx tmp = get_pool_constant_mark (rtl, &marked);
890890075Sobrien
890990075Sobrien	  if (GET_CODE (tmp) == SYMBOL_REF)
891096263Sobrien	    {
891196263Sobrien	      rtl = tmp;
891296263Sobrien	      if (CONSTANT_POOL_ADDRESS_P (tmp))
891396263Sobrien		get_pool_constant_mark (tmp, &marked);
891496263Sobrien	      else
891596263Sobrien		marked = true;
891696263Sobrien	    }
891796263Sobrien
891896263Sobrien	  /* If all references to this pool constant were optimized away,
891996263Sobrien	     it was not output and thus we can't represent it.
892096263Sobrien	     FIXME: might try to use DW_OP_const_value here, though
892196263Sobrien	     DW_OP_piece complicates it.  */
892296263Sobrien	  if (!marked)
892396263Sobrien	    return 0;
892490075Sobrien	}
892590075Sobrien
892650397Sobrien      mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
892750397Sobrien      mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
892890075Sobrien      mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8929169689Skan      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
893050397Sobrien      break;
893150397Sobrien
893290075Sobrien    case PRE_MODIFY:
893390075Sobrien      /* Extract the PLUS expression nested inside and fall into
8934132718Skan	 PLUS code below.  */
893590075Sobrien      rtl = XEXP (rtl, 1);
893690075Sobrien      goto plus;
893790075Sobrien
893890075Sobrien    case PRE_INC:
893990075Sobrien    case PRE_DEC:
894090075Sobrien      /* Turn these into a PLUS expression and fall into the PLUS code
894190075Sobrien	 below.  */
894290075Sobrien      rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
894390075Sobrien			  GEN_INT (GET_CODE (rtl) == PRE_INC
894490075Sobrien				   ? GET_MODE_UNIT_SIZE (mode)
894590075Sobrien				   : -GET_MODE_UNIT_SIZE (mode)));
894690075Sobrien
894790075Sobrien      /* ... fall through ...  */
894890075Sobrien
894950397Sobrien    case PLUS:
895090075Sobrien    plus:
895150397Sobrien      if (is_based_loc (rtl))
8952169689Skan	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
895350397Sobrien					  INTVAL (XEXP (rtl, 1)));
895450397Sobrien      else
895550397Sobrien	{
895690075Sobrien	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
895790075Sobrien	  if (mem_loc_result == 0)
895890075Sobrien	    break;
895990075Sobrien
896090075Sobrien	  if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
896190075Sobrien	      && INTVAL (XEXP (rtl, 1)) >= 0)
896290075Sobrien	    add_loc_descr (&mem_loc_result,
896390075Sobrien			   new_loc_descr (DW_OP_plus_uconst,
896490075Sobrien					  INTVAL (XEXP (rtl, 1)), 0));
896590075Sobrien	  else
896690075Sobrien	    {
896790075Sobrien	      add_loc_descr (&mem_loc_result,
896890075Sobrien			     mem_loc_descriptor (XEXP (rtl, 1), mode));
896990075Sobrien	      add_loc_descr (&mem_loc_result,
897090075Sobrien			     new_loc_descr (DW_OP_plus, 0, 0));
897190075Sobrien	    }
897250397Sobrien	}
897350397Sobrien      break;
897450397Sobrien
8975169689Skan    /* If a pseudo-reg is optimized away, it is possible for it to
8976169689Skan       be replaced with a MEM containing a multiply or shift.  */
897750397Sobrien    case MULT:
8978169689Skan      op = DW_OP_mul;
8979169689Skan      goto do_binop;
8980169689Skan
8981169689Skan    case ASHIFT:
8982169689Skan      op = DW_OP_shl;
8983169689Skan      goto do_binop;
8984169689Skan
8985169689Skan    case ASHIFTRT:
8986169689Skan      op = DW_OP_shra;
8987169689Skan      goto do_binop;
8988169689Skan
8989169689Skan    case LSHIFTRT:
8990169689Skan      op = DW_OP_shr;
8991169689Skan      goto do_binop;
8992169689Skan
8993169689Skan    do_binop:
899490075Sobrien      {
899590075Sobrien	dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
899690075Sobrien	dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
899750397Sobrien
899890075Sobrien	if (op0 == 0 || op1 == 0)
899990075Sobrien	  break;
900090075Sobrien
900190075Sobrien	mem_loc_result = op0;
900290075Sobrien	add_loc_descr (&mem_loc_result, op1);
9003169689Skan	add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
900490075Sobrien	break;
900590075Sobrien      }
900690075Sobrien
900750397Sobrien    case CONST_INT:
900890075Sobrien      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
900950397Sobrien      break;
901050397Sobrien
901150397Sobrien    default:
9012169689Skan      gcc_unreachable ();
901350397Sobrien    }
901450397Sobrien
901550397Sobrien  return mem_loc_result;
901650397Sobrien}
901750397Sobrien
901850397Sobrien/* Return a descriptor that describes the concatenation of two locations.
901950397Sobrien   This is typically a complex variable.  */
902050397Sobrien
902150397Sobrienstatic dw_loc_descr_ref
9022132718Skanconcat_loc_descriptor (rtx x0, rtx x1)
902350397Sobrien{
902450397Sobrien  dw_loc_descr_ref cc_loc_result = NULL;
902590075Sobrien  dw_loc_descr_ref x0_ref = loc_descriptor (x0);
902690075Sobrien  dw_loc_descr_ref x1_ref = loc_descriptor (x1);
902750397Sobrien
902890075Sobrien  if (x0_ref == 0 || x1_ref == 0)
902990075Sobrien    return 0;
903090075Sobrien
903190075Sobrien  cc_loc_result = x0_ref;
9032169689Skan  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
903350397Sobrien
903490075Sobrien  add_loc_descr (&cc_loc_result, x1_ref);
9035169689Skan  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
903650397Sobrien
903750397Sobrien  return cc_loc_result;
903850397Sobrien}
903950397Sobrien
904050397Sobrien/* Output a proper Dwarf location descriptor for a variable or parameter
904150397Sobrien   which is either allocated in a register or in a memory location.  For a
904250397Sobrien   register, we just generate an OP_REG and the register number.  For a
904350397Sobrien   memory location we provide a Dwarf postfix expression describing how to
904490075Sobrien   generate the (dynamic) address of the object onto the address stack.
904550397Sobrien
904690075Sobrien   If we don't know how to describe it, return 0.  */
904790075Sobrien
904850397Sobrienstatic dw_loc_descr_ref
9049132718Skanloc_descriptor (rtx rtl)
905050397Sobrien{
905150397Sobrien  dw_loc_descr_ref loc_result = NULL;
905290075Sobrien
905350397Sobrien  switch (GET_CODE (rtl))
905450397Sobrien    {
905550397Sobrien    case SUBREG:
905650397Sobrien      /* The case of a subreg may arise when we have a local (register)
9057132718Skan	 variable or a formal (register) parameter which doesn't quite fill
9058132718Skan	 up an entire register.  For now, just assume that it is
9059132718Skan	 legitimate to make the Dwarf info refer to the whole register which
9060132718Skan	 contains the given subreg.  */
906190075Sobrien      rtl = SUBREG_REG (rtl);
906250397Sobrien
906390075Sobrien      /* ... fall through ...  */
906450397Sobrien
906550397Sobrien    case REG:
906650397Sobrien      loc_result = reg_loc_descriptor (rtl);
906750397Sobrien      break;
906850397Sobrien
906950397Sobrien    case MEM:
907090075Sobrien      loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
907150397Sobrien      break;
907250397Sobrien
907350397Sobrien    case CONCAT:
907450397Sobrien      loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
907550397Sobrien      break;
907650397Sobrien
9077169689Skan    case VAR_LOCATION:
9078169689Skan      /* Single part.  */
9079169689Skan      if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9080169689Skan	{
9081169689Skan	  loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9082169689Skan	  break;
9083169689Skan	}
9084169689Skan
9085169689Skan      rtl = XEXP (rtl, 1);
9086169689Skan      /* FALLTHRU */
9087169689Skan
9088169689Skan    case PARALLEL:
9089169689Skan      {
9090169689Skan	rtvec par_elems = XVEC (rtl, 0);
9091169689Skan	int num_elem = GET_NUM_ELEM (par_elems);
9092169689Skan	enum machine_mode mode;
9093169689Skan	int i;
9094169689Skan
9095169689Skan	/* Create the first one, so we have something to add to.  */
9096169689Skan	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9097169689Skan	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9098169689Skan	add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9099169689Skan	for (i = 1; i < num_elem; i++)
9100169689Skan	  {
9101169689Skan	    dw_loc_descr_ref temp;
9102169689Skan
9103169689Skan	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9104169689Skan	    add_loc_descr (&loc_result, temp);
9105169689Skan	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9106169689Skan	    add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9107169689Skan	  }
9108169689Skan      }
9109169689Skan      break;
9110169689Skan
911150397Sobrien    default:
9112169689Skan      gcc_unreachable ();
911350397Sobrien    }
911450397Sobrien
911550397Sobrien  return loc_result;
911650397Sobrien}
911750397Sobrien
911890075Sobrien/* Similar, but generate the descriptor from trees instead of rtl.  This comes
9119169689Skan   up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9120169689Skan   a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9121169689Skan   top-level invocation, and we require the address of LOC; is 0 if we require
9122169689Skan   the value of LOC.  */
912390075Sobrien
912490075Sobrienstatic dw_loc_descr_ref
9125169689Skanloc_descriptor_from_tree_1 (tree loc, int want_address)
912690075Sobrien{
912790075Sobrien  dw_loc_descr_ref ret, ret1;
9128169689Skan  int have_address = 0;
912990075Sobrien  enum dwarf_location_atom op;
913090075Sobrien
913190075Sobrien  /* ??? Most of the time we do not take proper care for sign/zero
913290075Sobrien     extending the values properly.  Hopefully this won't be a real
913390075Sobrien     problem...  */
913490075Sobrien
913590075Sobrien  switch (TREE_CODE (loc))
913690075Sobrien    {
913790075Sobrien    case ERROR_MARK:
913890075Sobrien      return 0;
913990075Sobrien
914090075Sobrien    case PLACEHOLDER_EXPR:
914190075Sobrien      /* This case involves extracting fields from an object to determine the
914290075Sobrien	 position of other fields.  We don't try to encode this here.  The
914390075Sobrien	 only user of this is Ada, which encodes the needed information using
914490075Sobrien	 the names of types.  */
914590075Sobrien      return 0;
914690075Sobrien
914790075Sobrien    case CALL_EXPR:
914890075Sobrien      return 0;
914990075Sobrien
9150132718Skan    case PREINCREMENT_EXPR:
9151132718Skan    case PREDECREMENT_EXPR:
9152132718Skan    case POSTINCREMENT_EXPR:
9153132718Skan    case POSTDECREMENT_EXPR:
9154132718Skan      /* There are no opcodes for these operations.  */
9155132718Skan      return 0;
9156132718Skan
915790075Sobrien    case ADDR_EXPR:
9158169689Skan      /* If we already want an address, there's nothing we can do.  */
9159169689Skan      if (want_address)
9160169689Skan	return 0;
916190075Sobrien
9162169689Skan      /* Otherwise, process the argument and look for the address.  */
9163169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
916490075Sobrien
916590075Sobrien    case VAR_DECL:
9166169689Skan      if (DECL_THREAD_LOCAL_P (loc))
9167117395Skan	{
9168117395Skan	  rtx rtl;
9169117395Skan
9170117395Skan	  /* If this is not defined, we have no way to emit the data.  */
9171169689Skan	  if (!targetm.asm_out.output_dwarf_dtprel)
9172169689Skan	    return 0;
9173117395Skan
9174117395Skan	  /* The way DW_OP_GNU_push_tls_address is specified, we can only
9175117395Skan	     look up addresses of objects in the current module.  */
9176117395Skan	  if (DECL_EXTERNAL (loc))
9177117395Skan	    return 0;
9178117395Skan
9179117395Skan	  rtl = rtl_for_decl_location (loc);
9180117395Skan	  if (rtl == NULL_RTX)
9181117395Skan	    return 0;
9182117395Skan
9183169689Skan	  if (!MEM_P (rtl))
9184117395Skan	    return 0;
9185117395Skan	  rtl = XEXP (rtl, 0);
9186117395Skan	  if (! CONSTANT_P (rtl))
9187117395Skan	    return 0;
9188117395Skan
9189117395Skan	  ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9190117395Skan	  ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9191117395Skan	  ret->dw_loc_oprnd1.v.val_addr = rtl;
9192117395Skan
9193117395Skan	  ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9194117395Skan	  add_loc_descr (&ret, ret1);
9195117395Skan
9196169689Skan	  have_address = 1;
9197117395Skan	  break;
9198117395Skan	}
9199169689Skan      /* FALLTHRU */
9200117395Skan
920190075Sobrien    case PARM_DECL:
9202169689Skan      if (DECL_HAS_VALUE_EXPR_P (loc))
9203169689Skan	return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9204169689Skan					   want_address);
9205169689Skan      /* FALLTHRU */
9206169689Skan
9207169689Skan    case RESULT_DECL:
9208169689Skan    case FUNCTION_DECL:
920990075Sobrien      {
921090075Sobrien	rtx rtl = rtl_for_decl_location (loc);
921190075Sobrien
921290075Sobrien	if (rtl == NULL_RTX)
921390075Sobrien	  return 0;
9214169689Skan        else if (GET_CODE (rtl) == CONST_INT)
9215169689Skan	  {
9216169689Skan	    HOST_WIDE_INT val = INTVAL (rtl);
9217169689Skan	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9218169689Skan	      val &= GET_MODE_MASK (DECL_MODE (loc));
9219169689Skan	    ret = int_loc_descriptor (val);
9220169689Skan	  }
9221169689Skan	else if (GET_CODE (rtl) == CONST_STRING)
9222169689Skan	  return 0;
922390075Sobrien	else if (CONSTANT_P (rtl))
922490075Sobrien	  {
922590075Sobrien	    ret = new_loc_descr (DW_OP_addr, 0, 0);
922690075Sobrien	    ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
922790075Sobrien	    ret->dw_loc_oprnd1.v.val_addr = rtl;
922890075Sobrien	  }
922990075Sobrien	else
923090075Sobrien	  {
9231169689Skan	    enum machine_mode mode;
923290075Sobrien
9233169689Skan	    /* Certain constructs can only be represented at top-level.  */
9234169689Skan	    if (want_address == 2)
9235169689Skan	      return loc_descriptor (rtl);
9236169689Skan
9237169689Skan	    mode = GET_MODE (rtl);
9238169689Skan	    if (MEM_P (rtl))
923990075Sobrien	      {
924090075Sobrien		rtl = XEXP (rtl, 0);
9241169689Skan		have_address = 1;
924290075Sobrien	      }
924390075Sobrien	    ret = mem_loc_descriptor (rtl, mode);
924490075Sobrien	  }
924590075Sobrien      }
924690075Sobrien      break;
924790075Sobrien
924890075Sobrien    case INDIRECT_REF:
9249169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9250169689Skan      have_address = 1;
925190075Sobrien      break;
925290075Sobrien
925390075Sobrien    case COMPOUND_EXPR:
9254169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
925590075Sobrien
925690075Sobrien    case NOP_EXPR:
925790075Sobrien    case CONVERT_EXPR:
925890075Sobrien    case NON_LVALUE_EXPR:
925990075Sobrien    case VIEW_CONVERT_EXPR:
926090075Sobrien    case SAVE_EXPR:
9261132718Skan    case MODIFY_EXPR:
9262169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
926390075Sobrien
926490075Sobrien    case COMPONENT_REF:
926590075Sobrien    case BIT_FIELD_REF:
926690075Sobrien    case ARRAY_REF:
926790075Sobrien    case ARRAY_RANGE_REF:
926890075Sobrien      {
926990075Sobrien	tree obj, offset;
927090075Sobrien	HOST_WIDE_INT bitsize, bitpos, bytepos;
927190075Sobrien	enum machine_mode mode;
927290075Sobrien	int volatilep;
9273169689Skan	int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
927490075Sobrien
927590075Sobrien	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9276169689Skan				   &unsignedp, &volatilep, false);
927790075Sobrien
927890075Sobrien	if (obj == loc)
927990075Sobrien	  return 0;
928090075Sobrien
9281169689Skan	ret = loc_descriptor_from_tree_1 (obj, 1);
928290075Sobrien	if (ret == 0
928390075Sobrien	    || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
928490075Sobrien	  return 0;
928590075Sobrien
928690075Sobrien	if (offset != NULL_TREE)
928790075Sobrien	  {
928890075Sobrien	    /* Variable offset.  */
9289169689Skan	    add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
929090075Sobrien	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
929190075Sobrien	  }
929290075Sobrien
929390075Sobrien	bytepos = bitpos / BITS_PER_UNIT;
929490075Sobrien	if (bytepos > 0)
929590075Sobrien	  add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
929690075Sobrien	else if (bytepos < 0)
929790075Sobrien	  {
929890075Sobrien	    add_loc_descr (&ret, int_loc_descriptor (bytepos));
929990075Sobrien	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
930090075Sobrien	  }
9301169689Skan
9302169689Skan	have_address = 1;
930390075Sobrien	break;
930490075Sobrien      }
930590075Sobrien
930690075Sobrien    case INTEGER_CST:
930790075Sobrien      if (host_integerp (loc, 0))
930890075Sobrien	ret = int_loc_descriptor (tree_low_cst (loc, 0));
930990075Sobrien      else
931090075Sobrien	return 0;
931190075Sobrien      break;
931290075Sobrien
9313132718Skan    case CONSTRUCTOR:
9314132718Skan      {
9315132718Skan	/* Get an RTL for this, if something has been emitted.  */
9316132718Skan	rtx rtl = lookup_constant_def (loc);
9317132718Skan	enum machine_mode mode;
9318132718Skan
9319169689Skan	if (!rtl || !MEM_P (rtl))
9320132718Skan	  return 0;
9321132718Skan	mode = GET_MODE (rtl);
9322132718Skan	rtl = XEXP (rtl, 0);
9323132718Skan	ret = mem_loc_descriptor (rtl, mode);
9324169689Skan	have_address = 1;
9325132718Skan	break;
9326132718Skan      }
9327132718Skan
9328117395Skan    case TRUTH_AND_EXPR:
932990075Sobrien    case TRUTH_ANDIF_EXPR:
933090075Sobrien    case BIT_AND_EXPR:
933190075Sobrien      op = DW_OP_and;
933290075Sobrien      goto do_binop;
933390075Sobrien
933490075Sobrien    case TRUTH_XOR_EXPR:
933590075Sobrien    case BIT_XOR_EXPR:
933690075Sobrien      op = DW_OP_xor;
933790075Sobrien      goto do_binop;
933890075Sobrien
933990075Sobrien    case TRUTH_OR_EXPR:
934090075Sobrien    case TRUTH_ORIF_EXPR:
934190075Sobrien    case BIT_IOR_EXPR:
934290075Sobrien      op = DW_OP_or;
934390075Sobrien      goto do_binop;
934490075Sobrien
9345132718Skan    case FLOOR_DIV_EXPR:
9346132718Skan    case CEIL_DIV_EXPR:
9347132718Skan    case ROUND_DIV_EXPR:
934890075Sobrien    case TRUNC_DIV_EXPR:
934990075Sobrien      op = DW_OP_div;
935090075Sobrien      goto do_binop;
935190075Sobrien
935290075Sobrien    case MINUS_EXPR:
935390075Sobrien      op = DW_OP_minus;
935490075Sobrien      goto do_binop;
935590075Sobrien
9356132718Skan    case FLOOR_MOD_EXPR:
9357132718Skan    case CEIL_MOD_EXPR:
9358132718Skan    case ROUND_MOD_EXPR:
935990075Sobrien    case TRUNC_MOD_EXPR:
936090075Sobrien      op = DW_OP_mod;
936190075Sobrien      goto do_binop;
936290075Sobrien
936390075Sobrien    case MULT_EXPR:
936490075Sobrien      op = DW_OP_mul;
936590075Sobrien      goto do_binop;
936690075Sobrien
936790075Sobrien    case LSHIFT_EXPR:
936890075Sobrien      op = DW_OP_shl;
936990075Sobrien      goto do_binop;
937090075Sobrien
937190075Sobrien    case RSHIFT_EXPR:
9372169689Skan      op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
937390075Sobrien      goto do_binop;
937490075Sobrien
937590075Sobrien    case PLUS_EXPR:
937690075Sobrien      if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
937790075Sobrien	  && host_integerp (TREE_OPERAND (loc, 1), 0))
937890075Sobrien	{
9379169689Skan	  ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
938090075Sobrien	  if (ret == 0)
938190075Sobrien	    return 0;
938290075Sobrien
938390075Sobrien	  add_loc_descr (&ret,
938490075Sobrien			 new_loc_descr (DW_OP_plus_uconst,
938590075Sobrien					tree_low_cst (TREE_OPERAND (loc, 1),
938690075Sobrien						      0),
938790075Sobrien					0));
938890075Sobrien	  break;
938990075Sobrien	}
939090075Sobrien
939190075Sobrien      op = DW_OP_plus;
939290075Sobrien      goto do_binop;
939390075Sobrien
939490075Sobrien    case LE_EXPR:
9395169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
939690075Sobrien	return 0;
939790075Sobrien
939890075Sobrien      op = DW_OP_le;
939990075Sobrien      goto do_binop;
940090075Sobrien
940190075Sobrien    case GE_EXPR:
9402169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
940390075Sobrien	return 0;
940490075Sobrien
940590075Sobrien      op = DW_OP_ge;
940690075Sobrien      goto do_binop;
940790075Sobrien
940890075Sobrien    case LT_EXPR:
9409169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
941090075Sobrien	return 0;
941190075Sobrien
941290075Sobrien      op = DW_OP_lt;
941390075Sobrien      goto do_binop;
941490075Sobrien
941590075Sobrien    case GT_EXPR:
9416169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
941790075Sobrien	return 0;
941890075Sobrien
941990075Sobrien      op = DW_OP_gt;
942090075Sobrien      goto do_binop;
942190075Sobrien
942290075Sobrien    case EQ_EXPR:
942390075Sobrien      op = DW_OP_eq;
942490075Sobrien      goto do_binop;
942590075Sobrien
942690075Sobrien    case NE_EXPR:
942790075Sobrien      op = DW_OP_ne;
942890075Sobrien      goto do_binop;
942990075Sobrien
943090075Sobrien    do_binop:
9431169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9432169689Skan      ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
943390075Sobrien      if (ret == 0 || ret1 == 0)
943490075Sobrien	return 0;
943590075Sobrien
943690075Sobrien      add_loc_descr (&ret, ret1);
943790075Sobrien      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
943890075Sobrien      break;
943990075Sobrien
944090075Sobrien    case TRUTH_NOT_EXPR:
944190075Sobrien    case BIT_NOT_EXPR:
944290075Sobrien      op = DW_OP_not;
944390075Sobrien      goto do_unop;
944490075Sobrien
944590075Sobrien    case ABS_EXPR:
944690075Sobrien      op = DW_OP_abs;
944790075Sobrien      goto do_unop;
944890075Sobrien
944990075Sobrien    case NEGATE_EXPR:
945090075Sobrien      op = DW_OP_neg;
945190075Sobrien      goto do_unop;
945290075Sobrien
945390075Sobrien    do_unop:
9454169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
945590075Sobrien      if (ret == 0)
945690075Sobrien	return 0;
945790075Sobrien
945890075Sobrien      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
945990075Sobrien      break;
946090075Sobrien
9461169689Skan    case MIN_EXPR:
946290075Sobrien    case MAX_EXPR:
9463169689Skan      {
9464169689Skan        const enum tree_code code =
9465169689Skan          TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
946690075Sobrien
9467169689Skan        loc = build3 (COND_EXPR, TREE_TYPE (loc),
9468169689Skan		      build2 (code, integer_type_node,
9469169689Skan			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9470169689Skan                      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9471169689Skan      }
9472169689Skan
947390075Sobrien      /* ... fall through ...  */
947490075Sobrien
947590075Sobrien    case COND_EXPR:
947690075Sobrien      {
947790075Sobrien	dw_loc_descr_ref lhs
9478169689Skan	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
947990075Sobrien	dw_loc_descr_ref rhs
9480169689Skan	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
948190075Sobrien	dw_loc_descr_ref bra_node, jump_node, tmp;
948290075Sobrien
9483169689Skan	ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
948490075Sobrien	if (ret == 0 || lhs == 0 || rhs == 0)
948590075Sobrien	  return 0;
948690075Sobrien
948790075Sobrien	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
948890075Sobrien	add_loc_descr (&ret, bra_node);
948990075Sobrien
949090075Sobrien	add_loc_descr (&ret, rhs);
949190075Sobrien	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
949290075Sobrien	add_loc_descr (&ret, jump_node);
949390075Sobrien
949490075Sobrien	add_loc_descr (&ret, lhs);
949590075Sobrien	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
949690075Sobrien	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
949790075Sobrien
949890075Sobrien	/* ??? Need a node to point the skip at.  Use a nop.  */
949990075Sobrien	tmp = new_loc_descr (DW_OP_nop, 0, 0);
950090075Sobrien	add_loc_descr (&ret, tmp);
950190075Sobrien	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
950290075Sobrien	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
950390075Sobrien      }
950490075Sobrien      break;
950590075Sobrien
9506146895Skan    case FIX_TRUNC_EXPR:
9507146895Skan    case FIX_CEIL_EXPR:
9508146895Skan    case FIX_FLOOR_EXPR:
9509146895Skan    case FIX_ROUND_EXPR:
9510146895Skan      return 0;
9511146895Skan
951290075Sobrien    default:
9513132718Skan      /* Leave front-end specific codes as simply unknown.  This comes
9514132718Skan	 up, for instance, with the C STMT_EXPR.  */
9515132718Skan      if ((unsigned int) TREE_CODE (loc)
9516132718Skan          >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9517132718Skan	return 0;
9518132718Skan
9519146895Skan#ifdef ENABLE_CHECKING
9520132718Skan      /* Otherwise this is a generic code; we should just lists all of
9521169689Skan	 these explicitly.  We forgot one.  */
9522169689Skan      gcc_unreachable ();
9523146895Skan#else
9524146895Skan      /* In a release build, we want to degrade gracefully: better to
9525146895Skan	 generate incomplete debugging information than to crash.  */
9526146895Skan      return NULL;
9527146895Skan#endif
952890075Sobrien    }
952990075Sobrien
953090075Sobrien  /* Show if we can't fill the request for an address.  */
9531169689Skan  if (want_address && !have_address)
953290075Sobrien    return 0;
953390075Sobrien
953490075Sobrien  /* If we've got an address and don't want one, dereference.  */
9535169689Skan  if (!want_address && have_address && ret)
953690075Sobrien    {
953790075Sobrien      HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
953890075Sobrien
953990075Sobrien      if (size > DWARF2_ADDR_SIZE || size == -1)
954090075Sobrien	return 0;
954190075Sobrien      else if (size == DWARF2_ADDR_SIZE)
954290075Sobrien	op = DW_OP_deref;
954390075Sobrien      else
954490075Sobrien	op = DW_OP_deref_size;
954590075Sobrien
954690075Sobrien      add_loc_descr (&ret, new_loc_descr (op, size, 0));
954790075Sobrien    }
954890075Sobrien
954990075Sobrien  return ret;
955090075Sobrien}
955190075Sobrien
9552169689Skanstatic inline dw_loc_descr_ref
9553169689Skanloc_descriptor_from_tree (tree loc)
9554169689Skan{
9555169689Skan  return loc_descriptor_from_tree_1 (loc, 2);
9556169689Skan}
9557169689Skan
955890075Sobrien/* Given a value, round it up to the lowest multiple of `boundary'
955950397Sobrien   which is not less than the value itself.  */
956050397Sobrien
956190075Sobrienstatic inline HOST_WIDE_INT
9562132718Skanceiling (HOST_WIDE_INT value, unsigned int boundary)
956350397Sobrien{
956450397Sobrien  return (((value + boundary - 1) / boundary) * boundary);
956550397Sobrien}
956650397Sobrien
956750397Sobrien/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
956850397Sobrien   pointer to the declared type for the relevant field variable, or return
956950397Sobrien   `integer_type_node' if the given node turns out to be an
957050397Sobrien   ERROR_MARK node.  */
957150397Sobrien
957250397Sobrienstatic inline tree
9573132718Skanfield_type (tree decl)
957450397Sobrien{
957590075Sobrien  tree type;
957650397Sobrien
957750397Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
957850397Sobrien    return integer_type_node;
957950397Sobrien
958050397Sobrien  type = DECL_BIT_FIELD_TYPE (decl);
958150397Sobrien  if (type == NULL_TREE)
958250397Sobrien    type = TREE_TYPE (decl);
958350397Sobrien
958450397Sobrien  return type;
958550397Sobrien}
958650397Sobrien
958790075Sobrien/* Given a pointer to a tree node, return the alignment in bits for
958890075Sobrien   it, or else return BITS_PER_WORD if the node actually turns out to
958990075Sobrien   be an ERROR_MARK node.  */
959050397Sobrien
959150397Sobrienstatic inline unsigned
9592132718Skansimple_type_align_in_bits (tree type)
959350397Sobrien{
959450397Sobrien  return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
959550397Sobrien}
959650397Sobrien
959790075Sobrienstatic inline unsigned
9598132718Skansimple_decl_align_in_bits (tree decl)
959990075Sobrien{
9600117395Skan  return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
960190075Sobrien}
960290075Sobrien
960390075Sobrien/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
960490075Sobrien   lowest addressed byte of the "containing object" for the given FIELD_DECL,
960590075Sobrien   or return 0 if we are unable to determine what that offset is, either
960690075Sobrien   because the argument turns out to be a pointer to an ERROR_MARK node, or
960790075Sobrien   because the offset is actually variable.  (We can't handle the latter case
960890075Sobrien   just yet).  */
960950397Sobrien
961090075Sobrienstatic HOST_WIDE_INT
9611132718Skanfield_byte_offset (tree decl)
961250397Sobrien{
961390075Sobrien  unsigned int type_align_in_bits;
961490075Sobrien  unsigned int decl_align_in_bits;
961590075Sobrien  unsigned HOST_WIDE_INT type_size_in_bits;
961690075Sobrien  HOST_WIDE_INT object_offset_in_bits;
961790075Sobrien  tree type;
961890075Sobrien  tree field_size_tree;
961990075Sobrien  HOST_WIDE_INT bitpos_int;
962090075Sobrien  HOST_WIDE_INT deepest_bitpos;
962190075Sobrien  unsigned HOST_WIDE_INT field_size_in_bits;
962250397Sobrien
962350397Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
962450397Sobrien    return 0;
962550397Sobrien
9626169689Skan  gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9627169689Skan
962850397Sobrien  type = field_type (decl);
962950397Sobrien  field_size_tree = DECL_SIZE (decl);
963050397Sobrien
963190075Sobrien  /* The size could be unspecified if there was an error, or for
963290075Sobrien     a flexible array member.  */
963390075Sobrien  if (! field_size_tree)
963490075Sobrien    field_size_tree = bitsize_zero_node;
963590075Sobrien
963690075Sobrien  /* We cannot yet cope with fields whose positions are variable, so
963750397Sobrien     for now, when we see such things, we simply return 0.  Someday, we may
963850397Sobrien     be able to handle such cases, but it will be damn difficult.  */
963990075Sobrien  if (! host_integerp (bit_position (decl), 0))
964050397Sobrien    return 0;
964150397Sobrien
964290075Sobrien  bitpos_int = int_bit_position (decl);
964350397Sobrien
964490075Sobrien  /* If we don't know the size of the field, pretend it's a full word.  */
964590075Sobrien  if (host_integerp (field_size_tree, 1))
964690075Sobrien    field_size_in_bits = tree_low_cst (field_size_tree, 1);
964790075Sobrien  else
964890075Sobrien    field_size_in_bits = BITS_PER_WORD;
964990075Sobrien
965050397Sobrien  type_size_in_bits = simple_type_size_in_bits (type);
965150397Sobrien  type_align_in_bits = simple_type_align_in_bits (type);
9652117395Skan  decl_align_in_bits = simple_decl_align_in_bits (decl);
965350397Sobrien
965490075Sobrien  /* The GCC front-end doesn't make any attempt to keep track of the starting
965590075Sobrien     bit offset (relative to the start of the containing structure type) of the
965690075Sobrien     hypothetical "containing object" for a bit-field.  Thus, when computing
965790075Sobrien     the byte offset value for the start of the "containing object" of a
965890075Sobrien     bit-field, we must deduce this information on our own. This can be rather
965990075Sobrien     tricky to do in some cases.  For example, handling the following structure
966090075Sobrien     type definition when compiling for an i386/i486 target (which only aligns
966190075Sobrien     long long's to 32-bit boundaries) can be very tricky:
966250397Sobrien
966350397Sobrien	 struct S { int field1; long long field2:31; };
966450397Sobrien
966590075Sobrien     Fortunately, there is a simple rule-of-thumb which can be used in such
966690075Sobrien     cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
966790075Sobrien     structure shown above.  It decides to do this based upon one simple rule
966890075Sobrien     for bit-field allocation.  GCC allocates each "containing object" for each
966990075Sobrien     bit-field at the first (i.e. lowest addressed) legitimate alignment
967090075Sobrien     boundary (based upon the required minimum alignment for the declared type
967190075Sobrien     of the field) which it can possibly use, subject to the condition that
967290075Sobrien     there is still enough available space remaining in the containing object
967390075Sobrien     (when allocated at the selected point) to fully accommodate all of the
967490075Sobrien     bits of the bit-field itself.
967550397Sobrien
967690075Sobrien     This simple rule makes it obvious why GCC allocates 8 bytes for each
967790075Sobrien     object of the structure type shown above.  When looking for a place to
967890075Sobrien     allocate the "containing object" for `field2', the compiler simply tries
967990075Sobrien     to allocate a 64-bit "containing object" at each successive 32-bit
968090075Sobrien     boundary (starting at zero) until it finds a place to allocate that 64-
968190075Sobrien     bit field such that at least 31 contiguous (and previously unallocated)
968290075Sobrien     bits remain within that selected 64 bit field.  (As it turns out, for the
968390075Sobrien     example above, the compiler finds it is OK to allocate the "containing
968490075Sobrien     object" 64-bit field at bit-offset zero within the structure type.)
968590075Sobrien
968690075Sobrien     Here we attempt to work backwards from the limited set of facts we're
968790075Sobrien     given, and we try to deduce from those facts, where GCC must have believed
968890075Sobrien     that the containing object started (within the structure type). The value
968990075Sobrien     we deduce is then used (by the callers of this routine) to generate
969090075Sobrien     DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
969190075Sobrien     and, in the case of DW_AT_location, regular fields as well).  */
969290075Sobrien
969350397Sobrien  /* Figure out the bit-distance from the start of the structure to the
969450397Sobrien     "deepest" bit of the bit-field.  */
969550397Sobrien  deepest_bitpos = bitpos_int + field_size_in_bits;
969650397Sobrien
969750397Sobrien  /* This is the tricky part.  Use some fancy footwork to deduce where the
969850397Sobrien     lowest addressed bit of the containing object must be.  */
969990075Sobrien  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
970050397Sobrien
970190075Sobrien  /* Round up to type_align by default.  This works best for bitfields.  */
970290075Sobrien  object_offset_in_bits += type_align_in_bits - 1;
970390075Sobrien  object_offset_in_bits /= type_align_in_bits;
970490075Sobrien  object_offset_in_bits *= type_align_in_bits;
970550397Sobrien
970690075Sobrien  if (object_offset_in_bits > bitpos_int)
970790075Sobrien    {
970890075Sobrien      /* Sigh, the decl must be packed.  */
970990075Sobrien      object_offset_in_bits = deepest_bitpos - type_size_in_bits;
971050397Sobrien
971190075Sobrien      /* Round up to decl_align instead.  */
971290075Sobrien      object_offset_in_bits += decl_align_in_bits - 1;
971390075Sobrien      object_offset_in_bits /= decl_align_in_bits;
971490075Sobrien      object_offset_in_bits *= decl_align_in_bits;
971590075Sobrien    }
971690075Sobrien
971790075Sobrien  return object_offset_in_bits / BITS_PER_UNIT;
971850397Sobrien}
971950397Sobrien
972050397Sobrien/* The following routines define various Dwarf attributes and any data
972150397Sobrien   associated with them.  */
972250397Sobrien
972350397Sobrien/* Add a location description attribute value to a DIE.
972450397Sobrien
972550397Sobrien   This emits location attributes suitable for whole variables and
972650397Sobrien   whole parameters.  Note that the location attributes for struct fields are
972750397Sobrien   generated by the routine `data_member_location_attribute' below.  */
972850397Sobrien
9729117395Skanstatic inline void
9730132718Skanadd_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9731132718Skan			     dw_loc_descr_ref descr)
973250397Sobrien{
973390075Sobrien  if (descr != 0)
973490075Sobrien    add_AT_loc (die, attr_kind, descr);
973550397Sobrien}
973650397Sobrien
973790075Sobrien/* Attach the specialized form of location attribute used for data members of
973890075Sobrien   struct and union types.  In the special case of a FIELD_DECL node which
973990075Sobrien   represents a bit-field, the "offset" part of this special location
974090075Sobrien   descriptor must indicate the distance in bytes from the lowest-addressed
974190075Sobrien   byte of the containing struct or union type to the lowest-addressed byte of
974290075Sobrien   the "containing object" for the bit-field.  (See the `field_byte_offset'
974390075Sobrien   function above).
974450397Sobrien
974590075Sobrien   For any given bit-field, the "containing object" is a hypothetical object
974690075Sobrien   (of some integral or enum type) within which the given bit-field lives.  The
974790075Sobrien   type of this hypothetical "containing object" is always the same as the
974890075Sobrien   declared type of the individual bit-field itself (for GCC anyway... the
974990075Sobrien   DWARF spec doesn't actually mandate this).  Note that it is the size (in
975090075Sobrien   bytes) of the hypothetical "containing object" which will be given in the
975190075Sobrien   DW_AT_byte_size attribute for this bit-field.  (See the
975290075Sobrien   `byte_size_attribute' function below.)  It is also used when calculating the
975390075Sobrien   value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
975490075Sobrien   function below.)  */
975590075Sobrien
975650397Sobrienstatic void
9757132718Skanadd_data_member_location_attribute (dw_die_ref die, tree decl)
975850397Sobrien{
9759132718Skan  HOST_WIDE_INT offset;
976090075Sobrien  dw_loc_descr_ref loc_descr = 0;
976150397Sobrien
9762169689Skan  if (TREE_CODE (decl) == TREE_BINFO)
976390075Sobrien    {
976490075Sobrien      /* We're working on the TAG_inheritance for a base class.  */
9765169689Skan      if (BINFO_VIRTUAL_P (decl) && is_cxx ())
976690075Sobrien	{
976790075Sobrien	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
976890075Sobrien	     aren't at a fixed offset from all (sub)objects of the same
976990075Sobrien	     type.  We need to extract the appropriate offset from our
977090075Sobrien	     vtable.  The following dwarf expression means
977190075Sobrien
977290075Sobrien	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
977390075Sobrien
977490075Sobrien	     This is specific to the V3 ABI, of course.  */
977590075Sobrien
977690075Sobrien	  dw_loc_descr_ref tmp;
977790075Sobrien
977890075Sobrien	  /* Make a copy of the object address.  */
977990075Sobrien	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
978090075Sobrien	  add_loc_descr (&loc_descr, tmp);
978190075Sobrien
978290075Sobrien	  /* Extract the vtable address.  */
978390075Sobrien	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
978490075Sobrien	  add_loc_descr (&loc_descr, tmp);
978590075Sobrien
978690075Sobrien	  /* Calculate the address of the offset.  */
978790075Sobrien	  offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9788169689Skan	  gcc_assert (offset < 0);
978990075Sobrien
979090075Sobrien	  tmp = int_loc_descriptor (-offset);
979190075Sobrien	  add_loc_descr (&loc_descr, tmp);
979290075Sobrien	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
979390075Sobrien	  add_loc_descr (&loc_descr, tmp);
979490075Sobrien
979590075Sobrien	  /* Extract the offset.  */
979690075Sobrien	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
979790075Sobrien	  add_loc_descr (&loc_descr, tmp);
979890075Sobrien
979990075Sobrien	  /* Add it to the object address.  */
980090075Sobrien	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
980190075Sobrien	  add_loc_descr (&loc_descr, tmp);
980290075Sobrien	}
980390075Sobrien      else
980490075Sobrien	offset = tree_low_cst (BINFO_OFFSET (decl), 0);
980590075Sobrien    }
980650397Sobrien  else
980750397Sobrien    offset = field_byte_offset (decl);
980850397Sobrien
980990075Sobrien  if (! loc_descr)
981090075Sobrien    {
981190075Sobrien      enum dwarf_location_atom op;
981250397Sobrien
981390075Sobrien      /* The DWARF2 standard says that we should assume that the structure
981490075Sobrien	 address is already on the stack, so we can specify a structure field
981590075Sobrien	 address by using DW_OP_plus_uconst.  */
981690075Sobrien
981750397Sobrien#ifdef MIPS_DEBUGGING_INFO
981890075Sobrien      /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
981990075Sobrien	 operator correctly.  It works only if we leave the offset on the
982090075Sobrien	 stack.  */
982190075Sobrien      op = DW_OP_constu;
982250397Sobrien#else
982390075Sobrien      op = DW_OP_plus_uconst;
982450397Sobrien#endif
982550397Sobrien
982690075Sobrien      loc_descr = new_loc_descr (op, offset, 0);
982790075Sobrien    }
982890075Sobrien
982950397Sobrien  add_AT_loc (die, DW_AT_data_member_location, loc_descr);
983050397Sobrien}
983150397Sobrien
9832132718Skan/* Writes integer values to dw_vec_const array.  */
9833132718Skan
9834132718Skanstatic void
9835132718Skaninsert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9836132718Skan{
9837132718Skan  while (size != 0)
9838132718Skan    {
9839132718Skan      *dest++ = val & 0xff;
9840132718Skan      val >>= 8;
9841132718Skan      --size;
9842132718Skan    }
9843132718Skan}
9844132718Skan
9845132718Skan/* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9846132718Skan
9847132718Skanstatic HOST_WIDE_INT
9848132718Skanextract_int (const unsigned char *src, unsigned int size)
9849132718Skan{
9850132718Skan  HOST_WIDE_INT val = 0;
9851132718Skan
9852132718Skan  src += size;
9853132718Skan  while (size != 0)
9854132718Skan    {
9855132718Skan      val <<= 8;
9856132718Skan      val |= *--src & 0xff;
9857132718Skan      --size;
9858132718Skan    }
9859132718Skan  return val;
9860132718Skan}
9861132718Skan
9862132718Skan/* Writes floating point values to dw_vec_const array.  */
9863132718Skan
9864132718Skanstatic void
9865132718Skaninsert_float (rtx rtl, unsigned char *array)
9866132718Skan{
9867132718Skan  REAL_VALUE_TYPE rv;
9868132718Skan  long val[4];
9869132718Skan  int i;
9870132718Skan
9871132718Skan  REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9872132718Skan  real_to_target (val, &rv, GET_MODE (rtl));
9873132718Skan
9874132718Skan  /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9875132718Skan  for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9876132718Skan    {
9877132718Skan      insert_int (val[i], 4, array);
9878132718Skan      array += 4;
9879132718Skan    }
9880132718Skan}
9881132718Skan
9882132718Skan/* Attach a DW_AT_const_value attribute for a variable or a parameter which
988350397Sobrien   does not have a "location" either in memory or in a register.  These
988450397Sobrien   things can arise in GNU C when a constant is passed as an actual parameter
988550397Sobrien   to an inlined function.  They can also arise in C++ where declared
988650397Sobrien   constants do not necessarily get memory "homes".  */
988750397Sobrien
988850397Sobrienstatic void
9889132718Skanadd_const_value_attribute (dw_die_ref die, rtx rtl)
989050397Sobrien{
989150397Sobrien  switch (GET_CODE (rtl))
989250397Sobrien    {
989350397Sobrien    case CONST_INT:
989490075Sobrien      {
989590075Sobrien	HOST_WIDE_INT val = INTVAL (rtl);
9896117395Skan
9897132718Skan	if (val < 0)
9898132718Skan	  add_AT_int (die, DW_AT_const_value, val);
9899169689Skan	else
9900132718Skan	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
990190075Sobrien      }
990250397Sobrien      break;
990350397Sobrien
990450397Sobrien    case CONST_DOUBLE:
990550397Sobrien      /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9906132718Skan	 floating-point constant.  A CONST_DOUBLE is used whenever the
9907132718Skan	 constant requires more than one word in order to be adequately
9908132718Skan	 represented.  We output CONST_DOUBLEs as blocks.  */
990950397Sobrien      {
991090075Sobrien	enum machine_mode mode = GET_MODE (rtl);
991150397Sobrien
9912169689Skan	if (SCALAR_FLOAT_MODE_P (mode))
991350397Sobrien	  {
9914132718Skan	    unsigned int length = GET_MODE_SIZE (mode);
9915132718Skan	    unsigned char *array = ggc_alloc (length);
991650397Sobrien
9917132718Skan	    insert_float (rtl, array);
9918132718Skan	    add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
991950397Sobrien	  }
992050397Sobrien	else
992190075Sobrien	  {
992290075Sobrien	    /* ??? We really should be using HOST_WIDE_INT throughout.  */
9923169689Skan	    gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
992490075Sobrien
992590075Sobrien	    add_AT_long_long (die, DW_AT_const_value,
992690075Sobrien			      CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
992790075Sobrien	  }
992850397Sobrien      }
992950397Sobrien      break;
993050397Sobrien
9931132718Skan    case CONST_VECTOR:
9932132718Skan      {
9933132718Skan	enum machine_mode mode = GET_MODE (rtl);
9934132718Skan	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9935132718Skan	unsigned int length = CONST_VECTOR_NUNITS (rtl);
9936132718Skan	unsigned char *array = ggc_alloc (length * elt_size);
9937132718Skan	unsigned int i;
9938132718Skan	unsigned char *p;
9939132718Skan
9940169689Skan	switch (GET_MODE_CLASS (mode))
9941132718Skan	  {
9942169689Skan	  case MODE_VECTOR_INT:
9943132718Skan	    for (i = 0, p = array; i < length; i++, p += elt_size)
9944132718Skan	      {
9945132718Skan		rtx elt = CONST_VECTOR_ELT (rtl, i);
9946132718Skan		HOST_WIDE_INT lo, hi;
9947169689Skan
9948169689Skan		switch (GET_CODE (elt))
9949132718Skan		  {
9950169689Skan		  case CONST_INT:
9951132718Skan		    lo = INTVAL (elt);
9952132718Skan		    hi = -(lo < 0);
9953169689Skan		    break;
9954169689Skan
9955169689Skan		  case CONST_DOUBLE:
9956132718Skan		    lo = CONST_DOUBLE_LOW (elt);
9957132718Skan		    hi = CONST_DOUBLE_HIGH (elt);
9958169689Skan		    break;
9959169689Skan
9960169689Skan		  default:
9961169689Skan		    gcc_unreachable ();
9962132718Skan		  }
9963132718Skan
9964132718Skan		if (elt_size <= sizeof (HOST_WIDE_INT))
9965132718Skan		  insert_int (lo, elt_size, p);
9966169689Skan		else
9967132718Skan		  {
9968132718Skan		    unsigned char *p0 = p;
9969132718Skan		    unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9970132718Skan
9971169689Skan		    gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9972132718Skan		    if (WORDS_BIG_ENDIAN)
9973132718Skan		      {
9974132718Skan			p0 = p1;
9975132718Skan			p1 = p;
9976132718Skan		      }
9977132718Skan		    insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9978132718Skan		    insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9979132718Skan		  }
9980132718Skan	      }
9981169689Skan	    break;
9982169689Skan
9983169689Skan	  case MODE_VECTOR_FLOAT:
9984132718Skan	    for (i = 0, p = array; i < length; i++, p += elt_size)
9985132718Skan	      {
9986132718Skan		rtx elt = CONST_VECTOR_ELT (rtl, i);
9987132718Skan		insert_float (elt, p);
9988132718Skan	      }
9989169689Skan	    break;
9990169689Skan
9991169689Skan	  default:
9992169689Skan	    gcc_unreachable ();
9993132718Skan	  }
9994132718Skan
9995132718Skan	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9996132718Skan      }
9997132718Skan      break;
9998132718Skan
999950397Sobrien    case CONST_STRING:
1000050397Sobrien      add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
1000150397Sobrien      break;
1000250397Sobrien
1000350397Sobrien    case SYMBOL_REF:
1000450397Sobrien    case LABEL_REF:
1000550397Sobrien    case CONST:
1000690075Sobrien      add_AT_addr (die, DW_AT_const_value, rtl);
10007169689Skan      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
1000850397Sobrien      break;
1000950397Sobrien
1001050397Sobrien    case PLUS:
1001150397Sobrien      /* In cases where an inlined instance of an inline function is passed
10012132718Skan	 the address of an `auto' variable (which is local to the caller) we
10013132718Skan	 can get a situation where the DECL_RTL of the artificial local
10014132718Skan	 variable (for the inlining) which acts as a stand-in for the
10015132718Skan	 corresponding formal parameter (of the inline function) will look
10016132718Skan	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10017132718Skan	 exactly a compile-time constant expression, but it isn't the address
10018132718Skan	 of the (artificial) local variable either.  Rather, it represents the
10019132718Skan	 *value* which the artificial local variable always has during its
10020132718Skan	 lifetime.  We currently have no way to represent such quasi-constant
10021132718Skan	 values in Dwarf, so for now we just punt and generate nothing.  */
1002250397Sobrien      break;
1002350397Sobrien
1002450397Sobrien    default:
1002550397Sobrien      /* No other kinds of rtx should be possible here.  */
10026169689Skan      gcc_unreachable ();
1002750397Sobrien    }
1002850397Sobrien
1002950397Sobrien}
1003050397Sobrien
10031169689Skan/* Determine whether the evaluation of EXPR references any variables
10032169689Skan   or functions which aren't otherwise used (and therefore may not be
10033169689Skan   output).  */
10034169689Skanstatic tree
10035169689Skanreference_to_unused (tree * tp, int * walk_subtrees,
10036169689Skan		     void * data ATTRIBUTE_UNUSED)
10037169689Skan{
10038169689Skan  if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10039169689Skan    *walk_subtrees = 0;
10040169689Skan
10041169689Skan  if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10042169689Skan      && ! TREE_ASM_WRITTEN (*tp))
10043169689Skan    return *tp;
10044169689Skan  else if (!flag_unit_at_a_time)
10045169689Skan    return NULL_TREE;
10046169689Skan  else if (!cgraph_global_info_ready
10047169689Skan	   && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10048195815Sbms    return *tp;
10049169689Skan  else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10050169689Skan    {
10051169689Skan      struct cgraph_varpool_node *node = cgraph_varpool_node (*tp);
10052169689Skan      if (!node->needed)
10053169689Skan	return *tp;
10054169689Skan    }
10055169689Skan   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10056169689Skan	    && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10057169689Skan    {
10058169689Skan      struct cgraph_node *node = cgraph_node (*tp);
10059169689Skan      if (!node->output)
10060169689Skan        return *tp;
10061169689Skan    }
10062169689Skan
10063169689Skan  return NULL_TREE;
10064169689Skan}
10065169689Skan
10066169689Skan/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10067169689Skan   for use in a later add_const_value_attribute call.  */
10068169689Skan
1006990075Sobrienstatic rtx
10070169689Skanrtl_for_decl_init (tree init, tree type)
10071169689Skan{
10072169689Skan  rtx rtl = NULL_RTX;
10073169689Skan
10074169689Skan  /* If a variable is initialized with a string constant without embedded
10075169689Skan     zeros, build CONST_STRING.  */
10076169689Skan  if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10077169689Skan    {
10078169689Skan      tree enttype = TREE_TYPE (type);
10079169689Skan      tree domain = TYPE_DOMAIN (type);
10080169689Skan      enum machine_mode mode = TYPE_MODE (enttype);
10081169689Skan
10082169689Skan      if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10083169689Skan	  && domain
10084169689Skan	  && integer_zerop (TYPE_MIN_VALUE (domain))
10085169689Skan	  && compare_tree_int (TYPE_MAX_VALUE (domain),
10086169689Skan			       TREE_STRING_LENGTH (init) - 1) == 0
10087169689Skan	  && ((size_t) TREE_STRING_LENGTH (init)
10088169689Skan	      == strlen (TREE_STRING_POINTER (init)) + 1))
10089169689Skan	rtl = gen_rtx_CONST_STRING (VOIDmode,
10090169689Skan				    ggc_strdup (TREE_STRING_POINTER (init)));
10091169689Skan    }
10092169689Skan  /* Other aggregates, and complex values, could be represented using
10093169689Skan     CONCAT: FIXME!  */
10094169689Skan  else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10095169689Skan    ;
10096169689Skan  /* Vectors only work if their mode is supported by the target.
10097169689Skan     FIXME: generic vectors ought to work too.  */
10098169689Skan  else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10099169689Skan    ;
10100169689Skan  /* If the initializer is something that we know will expand into an
10101169689Skan     immediate RTL constant, expand it now.  We must be careful not to
10102169689Skan     reference variables which won't be output.  */
10103169689Skan  else if (initializer_constant_valid_p (init, type)
10104169689Skan	   && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10105169689Skan    {
10106220150Smm      /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10107220150Smm	 possible.  */
10108220150Smm      if (TREE_CODE (type) == VECTOR_TYPE)
10109220150Smm	switch (TREE_CODE (init))
10110220150Smm	  {
10111220150Smm	  case VECTOR_CST:
10112220150Smm	    break;
10113220150Smm	  case CONSTRUCTOR:
10114220150Smm	    if (TREE_CONSTANT (init))
10115220150Smm	      {
10116220150Smm		VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10117220150Smm		bool constant_p = true;
10118220150Smm		tree value;
10119220150Smm		unsigned HOST_WIDE_INT ix;
10120220150Smm
10121220150Smm		/* Even when ctor is constant, it might contain non-*_CST
10122220150Smm		   elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10123220150Smm		   belong into VECTOR_CST nodes.  */
10124220150Smm		FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10125220150Smm		  if (!CONSTANT_CLASS_P (value))
10126220150Smm		    {
10127220150Smm		      constant_p = false;
10128220150Smm		      break;
10129220150Smm		    }
10130220150Smm
10131220150Smm		if (constant_p)
10132220150Smm		  {
10133220150Smm		    init = build_vector_from_ctor (type, elts);
10134220150Smm		    break;
10135220150Smm		  }
10136220150Smm	      }
10137220150Smm	    /* FALLTHRU */
10138220150Smm
10139220150Smm	  default:
10140220150Smm	    return NULL;
10141220150Smm	  }
10142220150Smm
10143169689Skan      rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10144169689Skan
10145169689Skan      /* If expand_expr returns a MEM, it wasn't immediate.  */
10146169689Skan      gcc_assert (!rtl || !MEM_P (rtl));
10147169689Skan    }
10148169689Skan
10149169689Skan  return rtl;
10150169689Skan}
10151169689Skan
10152169689Skan/* Generate RTL for the variable DECL to represent its location.  */
10153169689Skan
10154169689Skanstatic rtx
10155132718Skanrtl_for_decl_location (tree decl)
1015650397Sobrien{
1015790075Sobrien  rtx rtl;
1015850397Sobrien
1015950397Sobrien  /* Here we have to decide where we are going to say the parameter "lives"
1016050397Sobrien     (as far as the debugger is concerned).  We only have a couple of
1016150397Sobrien     choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
1016250397Sobrien
1016390075Sobrien     DECL_RTL normally indicates where the parameter lives during most of the
1016450397Sobrien     activation of the function.  If optimization is enabled however, this
1016590075Sobrien     could be either NULL or else a pseudo-reg.  Both of those cases indicate
1016650397Sobrien     that the parameter doesn't really live anywhere (as far as the code
1016750397Sobrien     generation parts of GCC are concerned) during most of the function's
1016850397Sobrien     activation.  That will happen (for example) if the parameter is never
1016950397Sobrien     referenced within the function.
1017050397Sobrien
1017150397Sobrien     We could just generate a location descriptor here for all non-NULL
1017250397Sobrien     non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
1017350397Sobrien     a little nicer than that if we also consider DECL_INCOMING_RTL in cases
1017450397Sobrien     where DECL_RTL is NULL or is a pseudo-reg.
1017550397Sobrien
1017650397Sobrien     Note however that we can only get away with using DECL_INCOMING_RTL as
1017750397Sobrien     a backup substitute for DECL_RTL in certain limited cases.  In cases
1017850397Sobrien     where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
1017950397Sobrien     we can be sure that the parameter was passed using the same type as it is
1018050397Sobrien     declared to have within the function, and that its DECL_INCOMING_RTL
1018150397Sobrien     points us to a place where a value of that type is passed.
1018250397Sobrien
1018350397Sobrien     In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
1018450397Sobrien     we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
1018550397Sobrien     because in these cases DECL_INCOMING_RTL points us to a value of some
1018650397Sobrien     type which is *different* from the type of the parameter itself.  Thus,
1018750397Sobrien     if we tried to use DECL_INCOMING_RTL to generate a location attribute in
1018850397Sobrien     such cases, the debugger would end up (for example) trying to fetch a
1018950397Sobrien     `float' from a place which actually contains the first part of a
1019050397Sobrien     `double'.  That would lead to really incorrect and confusing
1019150397Sobrien     output at debug-time.
1019250397Sobrien
1019350397Sobrien     So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
1019450397Sobrien     in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
1019550397Sobrien     are a couple of exceptions however.  On little-endian machines we can
1019650397Sobrien     get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
1019750397Sobrien     not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
1019850397Sobrien     an integral type that is smaller than TREE_TYPE (decl). These cases arise
1019950397Sobrien     when (on a little-endian machine) a non-prototyped function has a
1020050397Sobrien     parameter declared to be of type `short' or `char'.  In such cases,
1020150397Sobrien     TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
1020250397Sobrien     be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
1020350397Sobrien     passed `int' value.  If the debugger then uses that address to fetch
1020450397Sobrien     a `short' or a `char' (on a little-endian machine) the result will be
1020550397Sobrien     the correct data, so we allow for such exceptional cases below.
1020650397Sobrien
1020750397Sobrien     Note that our goal here is to describe the place where the given formal
1020890075Sobrien     parameter lives during most of the function's activation (i.e. between the
1020990075Sobrien     end of the prologue and the start of the epilogue).  We'll do that as best
1021090075Sobrien     as we can. Note however that if the given formal parameter is modified
1021190075Sobrien     sometime during the execution of the function, then a stack backtrace (at
1021290075Sobrien     debug-time) will show the function as having been called with the *new*
1021390075Sobrien     value rather than the value which was originally passed in.  This happens
1021490075Sobrien     rarely enough that it is not a major problem, but it *is* a problem, and
1021590075Sobrien     I'd like to fix it.
1021650397Sobrien
1021790075Sobrien     A future version of dwarf2out.c may generate two additional attributes for
1021890075Sobrien     any given DW_TAG_formal_parameter DIE which will describe the "passed
1021990075Sobrien     type" and the "passed location" for the given formal parameter in addition
1022090075Sobrien     to the attributes we now generate to indicate the "declared type" and the
1022190075Sobrien     "active location" for each parameter.  This additional set of attributes
1022290075Sobrien     could be used by debuggers for stack backtraces. Separately, note that
1022390075Sobrien     sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
1022490075Sobrien     This happens (for example) for inlined-instances of inline function formal
1022590075Sobrien     parameters which are never referenced.  This really shouldn't be
1022690075Sobrien     happening.  All PARM_DECL nodes should get valid non-NULL
10227169689Skan     DECL_INCOMING_RTL values.  FIXME.  */
1022850397Sobrien
1022950397Sobrien  /* Use DECL_RTL as the "location" unless we find something better.  */
1023090075Sobrien  rtl = DECL_RTL_IF_SET (decl);
1023150397Sobrien
1023290075Sobrien  /* When generating abstract instances, ignore everything except
10233117395Skan     constants, symbols living in memory, and symbols living in
10234117395Skan     fixed registers.  */
1023590075Sobrien  if (! reload_completed)
1023650397Sobrien    {
1023790075Sobrien      if (rtl
1023890075Sobrien	  && (CONSTANT_P (rtl)
10239169689Skan	      || (MEM_P (rtl)
10240117395Skan	          && CONSTANT_P (XEXP (rtl, 0)))
10241169689Skan	      || (REG_P (rtl)
10242117395Skan	          && TREE_CODE (decl) == VAR_DECL
10243117395Skan		  && TREE_STATIC (decl))))
1024496263Sobrien	{
10245169689Skan	  rtl = targetm.delegitimize_address (rtl);
1024696263Sobrien	  return rtl;
1024796263Sobrien	}
1024890075Sobrien      rtl = NULL_RTX;
1024990075Sobrien    }
1025090075Sobrien  else if (TREE_CODE (decl) == PARM_DECL)
1025190075Sobrien    {
1025250397Sobrien      if (rtl == NULL_RTX || is_pseudo_reg (rtl))
1025350397Sobrien	{
10254146895Skan	  tree declared_type = TREE_TYPE (decl);
10255146895Skan	  tree passed_type = DECL_ARG_TYPE (decl);
10256146895Skan	  enum machine_mode dmode = TYPE_MODE (declared_type);
10257146895Skan	  enum machine_mode pmode = TYPE_MODE (passed_type);
1025850397Sobrien
1025950397Sobrien	  /* This decl represents a formal parameter which was optimized out.
1026050397Sobrien	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
1026190075Sobrien	     all cases where (rtl == NULL_RTX) just below.  */
10262146895Skan	  if (dmode == pmode)
1026350397Sobrien	    rtl = DECL_INCOMING_RTL (decl);
10264146895Skan	  else if (SCALAR_INT_MODE_P (dmode)
10265146895Skan		   && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10266146895Skan		   && DECL_INCOMING_RTL (decl))
10267146895Skan	    {
10268146895Skan	      rtx inc = DECL_INCOMING_RTL (decl);
10269146895Skan	      if (REG_P (inc))
10270146895Skan		rtl = inc;
10271169689Skan	      else if (MEM_P (inc))
10272146895Skan		{
10273146895Skan		  if (BYTES_BIG_ENDIAN)
10274146895Skan		    rtl = adjust_address_nv (inc, dmode,
10275146895Skan					     GET_MODE_SIZE (pmode)
10276146895Skan					     - GET_MODE_SIZE (dmode));
10277146895Skan		  else
10278146895Skan		    rtl = inc;
10279146895Skan		}
10280146895Skan	    }
1028150397Sobrien	}
1028252284Sobrien
1028352284Sobrien      /* If the parm was passed in registers, but lives on the stack, then
1028452284Sobrien	 make a big endian correction if the mode of the type of the
1028552284Sobrien	 parameter is not the same as the mode of the rtl.  */
1028652284Sobrien      /* ??? This is the same series of checks that are made in dbxout.c before
1028752284Sobrien	 we reach the big endian correction code there.  It isn't clear if all
1028852284Sobrien	 of these checks are necessary here, but keeping them all is the safe
1028952284Sobrien	 thing to do.  */
10290169689Skan      else if (MEM_P (rtl)
1029152284Sobrien	       && XEXP (rtl, 0) != const0_rtx
1029252284Sobrien	       && ! CONSTANT_P (XEXP (rtl, 0))
1029352284Sobrien	       /* Not passed in memory.  */
10294169689Skan	       && !MEM_P (DECL_INCOMING_RTL (decl))
1029552284Sobrien	       /* Not passed by invisible reference.  */
10296169689Skan	       && (!REG_P (XEXP (rtl, 0))
1029752284Sobrien		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
1029852284Sobrien		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
1029952284Sobrien#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1030052284Sobrien		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
1030152284Sobrien#endif
1030252284Sobrien		     )
1030352284Sobrien	       /* Big endian correction check.  */
1030452284Sobrien	       && BYTES_BIG_ENDIAN
1030552284Sobrien	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
1030652284Sobrien	       && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
1030752284Sobrien		   < UNITS_PER_WORD))
1030852284Sobrien	{
1030952284Sobrien	  int offset = (UNITS_PER_WORD
1031052284Sobrien			- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
1031190075Sobrien
1031252284Sobrien	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
1031352284Sobrien			     plus_constant (XEXP (rtl, 0), offset));
1031452284Sobrien	}
1031550397Sobrien    }
10316169689Skan  else if (TREE_CODE (decl) == VAR_DECL
10317169689Skan	   && rtl
10318169689Skan	   && MEM_P (rtl)
10319169689Skan	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10320169689Skan	   && BYTES_BIG_ENDIAN)
10321169689Skan    {
10322169689Skan      int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10323169689Skan      int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
1032450397Sobrien
10325169689Skan      /* If a variable is declared "register" yet is smaller than
10326169689Skan	 a register, then if we store the variable to memory, it
10327169689Skan	 looks like we're storing a register-sized value, when in
10328169689Skan	 fact we are not.  We need to adjust the offset of the
10329169689Skan	 storage location to reflect the actual value's bytes,
10330169689Skan	 else gdb will not be able to display it.  */
10331169689Skan      if (rsize > dsize)
10332169689Skan	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10333169689Skan			   plus_constant (XEXP (rtl, 0), rsize-dsize));
1033490075Sobrien    }
1033590075Sobrien
1033690075Sobrien  /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
1033790075Sobrien     and will have been substituted directly into all expressions that use it.
1033890075Sobrien     C does not have such a concept, but C++ and other languages do.  */
10339169689Skan  if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10340169689Skan    rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
1034190075Sobrien
1034296263Sobrien  if (rtl)
10343169689Skan    rtl = targetm.delegitimize_address (rtl);
10344117395Skan
10345117395Skan  /* If we don't look past the constant pool, we risk emitting a
10346117395Skan     reference to a constant pool entry that isn't referenced from
10347117395Skan     code, and thus is not emitted.  */
10348117395Skan  if (rtl)
10349117395Skan    rtl = avoid_constant_pool_reference (rtl);
10350117395Skan
1035190075Sobrien  return rtl;
1035290075Sobrien}
1035390075Sobrien
10354169689Skan/* We need to figure out what section we should use as the base for the
10355169689Skan   address ranges where a given location is valid.
10356169689Skan   1. If this particular DECL has a section associated with it, use that.
10357169689Skan   2. If this function has a section associated with it, use that.
10358169689Skan   3. Otherwise, use the text section.
10359169689Skan   XXX: If you split a variable across multiple sections, we won't notice.  */
10360169689Skan
10361169689Skanstatic const char *
10362169689Skansecname_for_decl (tree decl)
10363169689Skan{
10364169689Skan  const char *secname;
10365169689Skan
10366169689Skan  if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10367169689Skan    {
10368169689Skan      tree sectree = DECL_SECTION_NAME (decl);
10369169689Skan      secname = TREE_STRING_POINTER (sectree);
10370169689Skan    }
10371169689Skan  else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10372169689Skan    {
10373169689Skan      tree sectree = DECL_SECTION_NAME (current_function_decl);
10374169689Skan      secname = TREE_STRING_POINTER (sectree);
10375169689Skan    }
10376169689Skan  else if (cfun && in_cold_section_p)
10377169689Skan    secname = cfun->cold_section_label;
10378169689Skan  else
10379169689Skan    secname = text_section_label;
10380169689Skan
10381169689Skan  return secname;
10382169689Skan}
10383169689Skan
10384132718Skan/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
1038590075Sobrien   data attribute for a variable or a parameter.  We generate the
1038690075Sobrien   DW_AT_const_value attribute only in those cases where the given variable
1038790075Sobrien   or parameter does not have a true "location" either in memory or in a
1038890075Sobrien   register.  This can happen (for example) when a constant is passed as an
1038990075Sobrien   actual argument in a call to an inline function.  (It's possible that
1039090075Sobrien   these things can crop up in other ways also.)  Note that one type of
1039190075Sobrien   constant value which can be passed into an inlined function is a constant
1039290075Sobrien   pointer.  This can happen for example if an actual argument in an inlined
1039390075Sobrien   function call evaluates to a compile-time constant address.  */
1039490075Sobrien
1039590075Sobrienstatic void
10396169689Skanadd_location_or_const_value_attribute (dw_die_ref die, tree decl,
10397169689Skan				       enum dwarf_attribute attr)
1039890075Sobrien{
1039990075Sobrien  rtx rtl;
10400117395Skan  dw_loc_descr_ref descr;
10401169689Skan  var_loc_list *loc_list;
10402169689Skan  struct var_loc_node *node;
1040390075Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
1040490075Sobrien    return;
1040590075Sobrien
10406169689Skan  gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10407169689Skan	      || TREE_CODE (decl) == RESULT_DECL);
10408169689Skan
10409169689Skan  /* See if we possibly have multiple locations for this variable.  */
10410169689Skan  loc_list = lookup_decl_loc (decl);
1041150397Sobrien
10412169689Skan  /* If it truly has multiple locations, the first and last node will
10413169689Skan     differ.  */
10414169689Skan  if (loc_list && loc_list->first != loc_list->last)
1041550397Sobrien    {
10416169689Skan      const char *endname, *secname;
10417169689Skan      dw_loc_list_ref list;
10418169689Skan      rtx varloc;
1041950397Sobrien
10420169689Skan      /* Now that we know what section we are using for a base,
10421169689Skan         actually construct the list of locations.
10422169689Skan	 The first location information is what is passed to the
10423169689Skan	 function that creates the location list, and the remaining
10424169689Skan	 locations just get added on to that list.
10425169689Skan	 Note that we only know the start address for a location
10426169689Skan	 (IE location changes), so to build the range, we use
10427169689Skan	 the range [current location start, next location start].
10428169689Skan	 This means we have to special case the last node, and generate
10429169689Skan	 a range of [last location start, end of function label].  */
1043050397Sobrien
10431169689Skan      node = loc_list->first;
10432169689Skan      varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10433169689Skan      secname = secname_for_decl (decl);
10434132718Skan
10435169689Skan      list = new_loc_list (loc_descriptor (varloc),
10436169689Skan			   node->label, node->next->label, secname, 1);
10437169689Skan      node = node->next;
10438132718Skan
10439169689Skan      for (; node->next; node = node->next)
10440169689Skan	if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10441132718Skan	  {
10442169689Skan	    /* The variable has a location between NODE->LABEL and
10443169689Skan	       NODE->NEXT->LABEL.  */
10444169689Skan	    varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10445169689Skan	    add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10446169689Skan				       node->label, node->next->label, secname);
10447132718Skan	  }
10448132718Skan
10449169689Skan      /* If the variable has a location at the last label
10450169689Skan	 it keeps its location until the end of function.  */
10451169689Skan      if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10452169689Skan	{
10453169689Skan	  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10454169689Skan
10455169689Skan	  varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10456169689Skan	  if (!current_function_decl)
10457169689Skan	    endname = text_end_label;
10458169689Skan	  else
10459169689Skan	    {
10460169689Skan	      ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10461169689Skan					   current_function_funcdef_no);
10462169689Skan	      endname = ggc_strdup (label_id);
10463169689Skan	    }
10464169689Skan	  add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10465169689Skan				     node->label, endname, secname);
10466169689Skan	}
10467169689Skan
10468169689Skan      /* Finally, add the location list to the DIE, and we are done.  */
10469169689Skan      add_AT_loc_list (die, attr, list);
10470169689Skan      return;
1047150397Sobrien    }
10472169689Skan
10473169689Skan  /* Try to get some constant RTL for this decl, and use that as the value of
10474169689Skan     the location.  */
10475169689Skan
10476169689Skan  rtl = rtl_for_decl_location (decl);
10477169689Skan  if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10478169689Skan    {
10479169689Skan      add_const_value_attribute (die, rtl);
10480169689Skan      return;
10481169689Skan    }
10482169689Skan
10483169689Skan  /* If we have tried to generate the location otherwise, and it
10484169689Skan     didn't work out (we wouldn't be here if we did), and we have a one entry
10485169689Skan     location list, try generating a location from that.  */
10486169689Skan  if (loc_list && loc_list->first)
10487169689Skan    {
10488169689Skan      node = loc_list->first;
10489169689Skan      descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10490169689Skan      if (descr)
10491169689Skan	{
10492169689Skan	  add_AT_location_description (die, attr, descr);
10493169689Skan	  return;
10494169689Skan	}
10495169689Skan    }
10496169689Skan
10497169689Skan  /* We couldn't get any rtl, so try directly generating the location
10498169689Skan     description from the tree.  */
10499169689Skan  descr = loc_descriptor_from_tree (decl);
10500169689Skan  if (descr)
10501169689Skan    {
10502169689Skan      add_AT_location_description (die, attr, descr);
10503169689Skan      return;
10504169689Skan    }
10505169689Skan  /* None of that worked, so it must not really have a location;
10506169689Skan     try adding a constant value attribute from the DECL_INITIAL.  */
10507169689Skan  tree_add_const_value_attribute (die, decl);
1050850397Sobrien}
1050950397Sobrien
1051090075Sobrien/* If we don't have a copy of this variable in memory for some reason (such
1051190075Sobrien   as a C++ member constant that doesn't have an out-of-line definition),
1051290075Sobrien   we should tell the debugger about the constant value.  */
1051390075Sobrien
1051490075Sobrienstatic void
10515132718Skantree_add_const_value_attribute (dw_die_ref var_die, tree decl)
1051690075Sobrien{
1051790075Sobrien  tree init = DECL_INITIAL (decl);
1051890075Sobrien  tree type = TREE_TYPE (decl);
10519169689Skan  rtx rtl;
1052090075Sobrien
10521169689Skan  if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
1052290075Sobrien    /* OK */;
1052390075Sobrien  else
1052490075Sobrien    return;
1052590075Sobrien
10526169689Skan  rtl = rtl_for_decl_init (init, type);
10527169689Skan  if (rtl)
10528169689Skan    add_const_value_attribute (var_die, rtl);
10529169689Skan}
10530169689Skan
10531169689Skan/* Convert the CFI instructions for the current function into a
10532169689Skan   location list.  This is used for DW_AT_frame_base when we targeting
10533169689Skan   a dwarf2 consumer that does not support the dwarf3
10534169689Skan   DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10535169689Skan   expressions.  */
10536169689Skan
10537169689Skanstatic dw_loc_list_ref
10538169689Skanconvert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10539169689Skan{
10540169689Skan  dw_fde_ref fde;
10541169689Skan  dw_loc_list_ref list, *list_tail;
10542169689Skan  dw_cfi_ref cfi;
10543169689Skan  dw_cfa_location last_cfa, next_cfa;
10544169689Skan  const char *start_label, *last_label, *section;
10545169689Skan
10546169689Skan  fde = &fde_table[fde_table_in_use - 1];
10547169689Skan
10548169689Skan  section = secname_for_decl (current_function_decl);
10549169689Skan  list_tail = &list;
10550169689Skan  list = NULL;
10551169689Skan
10552169689Skan  next_cfa.reg = INVALID_REGNUM;
10553169689Skan  next_cfa.offset = 0;
10554169689Skan  next_cfa.indirect = 0;
10555169689Skan  next_cfa.base_offset = 0;
10556169689Skan
10557169689Skan  start_label = fde->dw_fde_begin;
10558169689Skan
10559169689Skan  /* ??? Bald assumption that the CIE opcode list does not contain
10560169689Skan     advance opcodes.  */
10561169689Skan  for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10562169689Skan    lookup_cfa_1 (cfi, &next_cfa);
10563169689Skan
10564169689Skan  last_cfa = next_cfa;
10565169689Skan  last_label = start_label;
10566169689Skan
10567169689Skan  for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10568169689Skan    switch (cfi->dw_cfi_opc)
10569169689Skan      {
10570169689Skan      case DW_CFA_set_loc:
10571169689Skan      case DW_CFA_advance_loc1:
10572169689Skan      case DW_CFA_advance_loc2:
10573169689Skan      case DW_CFA_advance_loc4:
10574169689Skan	if (!cfa_equal_p (&last_cfa, &next_cfa))
10575169689Skan	  {
10576169689Skan	    *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10577169689Skan				       start_label, last_label, section,
10578169689Skan				       list == NULL);
10579169689Skan
10580169689Skan	    list_tail = &(*list_tail)->dw_loc_next;
10581169689Skan	    last_cfa = next_cfa;
10582169689Skan	    start_label = last_label;
10583169689Skan	  }
10584169689Skan	last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10585169689Skan	break;
10586169689Skan
10587169689Skan      case DW_CFA_advance_loc:
10588169689Skan	/* The encoding is complex enough that we should never emit this.  */
10589169689Skan      case DW_CFA_remember_state:
10590169689Skan      case DW_CFA_restore_state:
10591169689Skan	/* We don't handle these two in this function.  It would be possible
10592169689Skan	   if it were to be required.  */
10593169689Skan	gcc_unreachable ();
10594169689Skan
10595169689Skan      default:
10596169689Skan	lookup_cfa_1 (cfi, &next_cfa);
10597169689Skan	break;
10598169689Skan      }
10599169689Skan
10600169689Skan  if (!cfa_equal_p (&last_cfa, &next_cfa))
1060190075Sobrien    {
10602169689Skan      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10603169689Skan				 start_label, last_label, section,
10604169689Skan				 list == NULL);
10605169689Skan      list_tail = &(*list_tail)->dw_loc_next;
10606169689Skan      start_label = last_label;
10607169689Skan    }
10608169689Skan  *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10609169689Skan			     start_label, fde->dw_fde_end, section,
10610169689Skan			     list == NULL);
1061190075Sobrien
10612169689Skan  return list;
10613169689Skan}
10614169689Skan
10615169689Skan/* Compute a displacement from the "steady-state frame pointer" to the
10616169689Skan   frame base (often the same as the CFA), and store it in
10617169689Skan   frame_pointer_fb_offset.  OFFSET is added to the displacement
10618169689Skan   before the latter is negated.  */
10619169689Skan
10620169689Skanstatic void
10621169689Skancompute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10622169689Skan{
10623169689Skan  rtx reg, elim;
10624169689Skan
10625169689Skan#ifdef FRAME_POINTER_CFA_OFFSET
10626169689Skan  reg = frame_pointer_rtx;
10627169689Skan  offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10628169689Skan#else
10629169689Skan  reg = arg_pointer_rtx;
10630169689Skan  offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10631169689Skan#endif
10632169689Skan
10633169689Skan  elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10634169689Skan  if (GET_CODE (elim) == PLUS)
10635169689Skan    {
10636169689Skan      offset += INTVAL (XEXP (elim, 1));
10637169689Skan      elim = XEXP (elim, 0);
1063890075Sobrien    }
10639169689Skan  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10640169689Skan		       : stack_pointer_rtx));
10641169689Skan
10642169689Skan  frame_pointer_fb_offset = -offset;
1064390075Sobrien}
1064490075Sobrien
10645132718Skan/* Generate a DW_AT_name attribute given some string value to be included as
1064650397Sobrien   the value of the attribute.  */
1064750397Sobrien
10648132718Skanstatic void
10649132718Skanadd_name_attribute (dw_die_ref die, const char *name_string)
1065050397Sobrien{
1065150397Sobrien  if (name_string != NULL && *name_string != 0)
1065290075Sobrien    {
1065390075Sobrien      if (demangle_name_func)
1065490075Sobrien	name_string = (*demangle_name_func) (name_string);
1065590075Sobrien
1065690075Sobrien      add_AT_string (die, DW_AT_name, name_string);
1065790075Sobrien    }
1065850397Sobrien}
1065950397Sobrien
10660132718Skan/* Generate a DW_AT_comp_dir attribute for DIE.  */
10661132718Skan
10662132718Skanstatic void
10663132718Skanadd_comp_dir_attribute (dw_die_ref die)
10664132718Skan{
10665132718Skan  const char *wd = get_src_pwd ();
10666132718Skan  if (wd != NULL)
10667132718Skan    add_AT_string (die, DW_AT_comp_dir, wd);
10668132718Skan}
10669132718Skan
1067050397Sobrien/* Given a tree node describing an array bound (either lower or upper) output
1067150397Sobrien   a representation for that bound.  */
1067250397Sobrien
1067350397Sobrienstatic void
10674132718Skanadd_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
1067550397Sobrien{
1067650397Sobrien  switch (TREE_CODE (bound))
1067750397Sobrien    {
1067850397Sobrien    case ERROR_MARK:
1067950397Sobrien      return;
1068050397Sobrien
1068190075Sobrien    /* All fixed-bounds are represented by INTEGER_CST nodes.  */
1068250397Sobrien    case INTEGER_CST:
1068390075Sobrien      if (! host_integerp (bound, 0)
1068490075Sobrien	  || (bound_attr == DW_AT_lower_bound
1068590075Sobrien	      && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
1068690075Sobrien		  || (is_fortran () && integer_onep (bound)))))
10687169689Skan	/* Use the default.  */
1068890075Sobrien	;
1068950397Sobrien      else
1069090075Sobrien	add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
1069150397Sobrien      break;
1069250397Sobrien
1069350397Sobrien    case CONVERT_EXPR:
1069450397Sobrien    case NOP_EXPR:
1069550397Sobrien    case NON_LVALUE_EXPR:
1069690075Sobrien    case VIEW_CONVERT_EXPR:
1069750397Sobrien      add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
1069850397Sobrien      break;
1069990075Sobrien
1070050397Sobrien    case SAVE_EXPR:
1070150397Sobrien      break;
1070250397Sobrien
1070350397Sobrien    case VAR_DECL:
1070490075Sobrien    case PARM_DECL:
10705169689Skan    case RESULT_DECL:
1070690075Sobrien      {
1070790075Sobrien	dw_die_ref decl_die = lookup_decl_die (bound);
1070850397Sobrien
1070990075Sobrien	/* ??? Can this happen, or should the variable have been bound
1071090075Sobrien	   first?  Probably it can, since I imagine that we try to create
1071190075Sobrien	   the types of parameters in the order in which they exist in
1071290075Sobrien	   the list, and won't have created a forward reference to a
1071390075Sobrien	   later parameter.  */
1071490075Sobrien	if (decl_die != NULL)
1071590075Sobrien	  add_AT_die_ref (subrange_die, bound_attr, decl_die);
1071690075Sobrien	break;
1071790075Sobrien      }
1071890075Sobrien
1071950397Sobrien    default:
1072090075Sobrien      {
1072190075Sobrien	/* Otherwise try to create a stack operation procedure to
1072290075Sobrien	   evaluate the value of the array bound.  */
1072390075Sobrien
1072490075Sobrien	dw_die_ref ctx, decl_die;
1072590075Sobrien	dw_loc_descr_ref loc;
1072690075Sobrien
10727169689Skan	loc = loc_descriptor_from_tree (bound);
1072890075Sobrien	if (loc == NULL)
1072990075Sobrien	  break;
1073090075Sobrien
1073190075Sobrien	if (current_function_decl == 0)
1073290075Sobrien	  ctx = comp_unit_die;
1073390075Sobrien	else
1073490075Sobrien	  ctx = lookup_decl_die (current_function_decl);
1073590075Sobrien
1073690075Sobrien	decl_die = new_die (DW_TAG_variable, ctx, bound);
1073790075Sobrien	add_AT_flag (decl_die, DW_AT_artificial, 1);
1073890075Sobrien	add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
1073990075Sobrien	add_AT_loc (decl_die, DW_AT_location, loc);
1074090075Sobrien
1074190075Sobrien	add_AT_die_ref (subrange_die, bound_attr, decl_die);
1074290075Sobrien	break;
1074390075Sobrien      }
1074450397Sobrien    }
1074550397Sobrien}
1074650397Sobrien
1074750397Sobrien/* Note that the block of subscript information for an array type also
1074850397Sobrien   includes information about the element type of type given array type.  */
1074950397Sobrien
1075050397Sobrienstatic void
10751132718Skanadd_subscript_info (dw_die_ref type_die, tree type)
1075250397Sobrien{
1075350397Sobrien#ifndef MIPS_DEBUGGING_INFO
1075490075Sobrien  unsigned dimension_number;
1075550397Sobrien#endif
1075690075Sobrien  tree lower, upper;
1075790075Sobrien  dw_die_ref subrange_die;
1075850397Sobrien
1075990075Sobrien  /* The GNU compilers represent multidimensional array types as sequences of
1076050397Sobrien     one dimensional array types whose element types are themselves array
1076150397Sobrien     types.  Here we squish that down, so that each multidimensional array
1076290075Sobrien     type gets only one array_type DIE in the Dwarf debugging info. The draft
1076350397Sobrien     Dwarf specification say that we are allowed to do this kind of
1076450397Sobrien     compression in C (because there is no difference between an array or
1076590075Sobrien     arrays and a multidimensional array in C) but for other source languages
1076650397Sobrien     (e.g. Ada) we probably shouldn't do this.  */
1076750397Sobrien
1076850397Sobrien  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
1076950397Sobrien     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
1077050397Sobrien     We work around this by disabling this feature.  See also
1077150397Sobrien     gen_array_type_die.  */
1077250397Sobrien#ifndef MIPS_DEBUGGING_INFO
1077350397Sobrien  for (dimension_number = 0;
1077450397Sobrien       TREE_CODE (type) == ARRAY_TYPE;
1077550397Sobrien       type = TREE_TYPE (type), dimension_number++)
1077690075Sobrien#endif
1077750397Sobrien    {
1077890075Sobrien      tree domain = TYPE_DOMAIN (type);
1077950397Sobrien
1078050397Sobrien      /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
1078190075Sobrien	 and (in GNU C only) variable bounds.  Handle all three forms
10782132718Skan	 here.  */
1078390075Sobrien      subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
1078450397Sobrien      if (domain)
1078550397Sobrien	{
1078650397Sobrien	  /* We have an array type with specified bounds.  */
1078750397Sobrien	  lower = TYPE_MIN_VALUE (domain);
1078850397Sobrien	  upper = TYPE_MAX_VALUE (domain);
1078950397Sobrien
10790132718Skan	  /* Define the index type.  */
1079150397Sobrien	  if (TREE_TYPE (domain))
1079250397Sobrien	    {
1079350397Sobrien	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
1079450397Sobrien		 TREE_TYPE field.  We can't emit debug info for this
1079550397Sobrien		 because it is an unnamed integral type.  */
1079650397Sobrien	      if (TREE_CODE (domain) == INTEGER_TYPE
1079750397Sobrien		  && TYPE_NAME (domain) == NULL_TREE
1079850397Sobrien		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
1079950397Sobrien		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
1080090075Sobrien		;
1080150397Sobrien	      else
1080250397Sobrien		add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
1080350397Sobrien				    type_die);
1080450397Sobrien	    }
1080550397Sobrien
1080650397Sobrien	  /* ??? If upper is NULL, the array has unspecified length,
1080750397Sobrien	     but it does have a lower bound.  This happens with Fortran
1080850397Sobrien	       dimension arr(N:*)
10809132718Skan	     Since the debugger is definitely going to need to know N
1081050397Sobrien	     to produce useful results, go ahead and output the lower
1081150397Sobrien	     bound solo, and hope the debugger can cope.  */
1081250397Sobrien
1081350397Sobrien	  add_bound_info (subrange_die, DW_AT_lower_bound, lower);
1081450397Sobrien	  if (upper)
1081550397Sobrien	    add_bound_info (subrange_die, DW_AT_upper_bound, upper);
1081650397Sobrien	}
1081750397Sobrien
1081890075Sobrien      /* Otherwise we have an array type with an unspecified length.  The
1081990075Sobrien	 DWARF-2 spec does not say how to handle this; let's just leave out the
1082090075Sobrien	 bounds.  */
1082150397Sobrien    }
1082250397Sobrien}
1082350397Sobrien
1082450397Sobrienstatic void
10825132718Skanadd_byte_size_attribute (dw_die_ref die, tree tree_node)
1082650397Sobrien{
1082790075Sobrien  unsigned size;
1082850397Sobrien
1082950397Sobrien  switch (TREE_CODE (tree_node))
1083050397Sobrien    {
1083150397Sobrien    case ERROR_MARK:
1083250397Sobrien      size = 0;
1083350397Sobrien      break;
1083450397Sobrien    case ENUMERAL_TYPE:
1083550397Sobrien    case RECORD_TYPE:
1083650397Sobrien    case UNION_TYPE:
1083750397Sobrien    case QUAL_UNION_TYPE:
1083850397Sobrien      size = int_size_in_bytes (tree_node);
1083950397Sobrien      break;
1084050397Sobrien    case FIELD_DECL:
1084150397Sobrien      /* For a data member of a struct or union, the DW_AT_byte_size is
10842132718Skan	 generally given as the number of bytes normally allocated for an
10843132718Skan	 object of the *declared* type of the member itself.  This is true
10844132718Skan	 even for bit-fields.  */
1084550397Sobrien      size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
1084650397Sobrien      break;
1084750397Sobrien    default:
10848169689Skan      gcc_unreachable ();
1084950397Sobrien    }
1085050397Sobrien
1085150397Sobrien  /* Note that `size' might be -1 when we get to this point.  If it is, that
1085250397Sobrien     indicates that the byte size of the entity in question is variable.  We
10853245023Spfg     have no good way of expressing this fact in Dwarf at the present time.
10854245023Spfg     GCC/35998: Avoid passing negative sizes to Dtrace and gdb.  */
10855245023Spfg  add_AT_unsigned (die, DW_AT_byte_size, (size != (unsigned)-1 ? size : 0));
1085650397Sobrien}
1085750397Sobrien
1085850397Sobrien/* For a FIELD_DECL node which represents a bit-field, output an attribute
1085950397Sobrien   which specifies the distance in bits from the highest order bit of the
1086050397Sobrien   "containing object" for the bit-field to the highest order bit of the
1086150397Sobrien   bit-field itself.
1086250397Sobrien
1086390075Sobrien   For any given bit-field, the "containing object" is a hypothetical object
1086490075Sobrien   (of some integral or enum type) within which the given bit-field lives.  The
1086590075Sobrien   type of this hypothetical "containing object" is always the same as the
1086690075Sobrien   declared type of the individual bit-field itself.  The determination of the
1086790075Sobrien   exact location of the "containing object" for a bit-field is rather
1086890075Sobrien   complicated.  It's handled by the `field_byte_offset' function (above).
1086950397Sobrien
1087050397Sobrien   Note that it is the size (in bytes) of the hypothetical "containing object"
1087150397Sobrien   which will be given in the DW_AT_byte_size attribute for this bit-field.
1087250397Sobrien   (See `byte_size_attribute' above).  */
1087350397Sobrien
1087450397Sobrienstatic inline void
10875132718Skanadd_bit_offset_attribute (dw_die_ref die, tree decl)
1087650397Sobrien{
1087790075Sobrien  HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
1087890075Sobrien  tree type = DECL_BIT_FIELD_TYPE (decl);
1087990075Sobrien  HOST_WIDE_INT bitpos_int;
1088090075Sobrien  HOST_WIDE_INT highest_order_object_bit_offset;
1088190075Sobrien  HOST_WIDE_INT highest_order_field_bit_offset;
1088290075Sobrien  HOST_WIDE_INT unsigned bit_offset;
1088350397Sobrien
1088450397Sobrien  /* Must be a field and a bit field.  */
10885169689Skan  gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
1088650397Sobrien
1088750397Sobrien  /* We can't yet handle bit-fields whose offsets are variable, so if we
1088850397Sobrien     encounter such things, just return without generating any attribute
1088990075Sobrien     whatsoever.  Likewise for variable or too large size.  */
1089090075Sobrien  if (! host_integerp (bit_position (decl), 0)
1089190075Sobrien      || ! host_integerp (DECL_SIZE (decl), 1))
1089250397Sobrien    return;
1089350397Sobrien
1089490075Sobrien  bitpos_int = int_bit_position (decl);
1089550397Sobrien
1089650397Sobrien  /* Note that the bit offset is always the distance (in bits) from the
1089790075Sobrien     highest-order bit of the "containing object" to the highest-order bit of
1089890075Sobrien     the bit-field itself.  Since the "high-order end" of any object or field
1089950397Sobrien     is different on big-endian and little-endian machines, the computation
1090050397Sobrien     below must take account of these differences.  */
1090150397Sobrien  highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
1090250397Sobrien  highest_order_field_bit_offset = bitpos_int;
1090350397Sobrien
1090450397Sobrien  if (! BYTES_BIG_ENDIAN)
1090550397Sobrien    {
1090690075Sobrien      highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
1090750397Sobrien      highest_order_object_bit_offset += simple_type_size_in_bits (type);
1090850397Sobrien    }
1090950397Sobrien
1091050397Sobrien  bit_offset
1091150397Sobrien    = (! BYTES_BIG_ENDIAN
1091250397Sobrien       ? highest_order_object_bit_offset - highest_order_field_bit_offset
1091350397Sobrien       : highest_order_field_bit_offset - highest_order_object_bit_offset);
1091450397Sobrien
1091550397Sobrien  add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
1091650397Sobrien}
1091750397Sobrien
1091850397Sobrien/* For a FIELD_DECL node which represents a bit field, output an attribute
1091950397Sobrien   which specifies the length in bits of the given field.  */
1092050397Sobrien
1092150397Sobrienstatic inline void
10922132718Skanadd_bit_size_attribute (dw_die_ref die, tree decl)
1092350397Sobrien{
1092450397Sobrien  /* Must be a field and a bit field.  */
10925169689Skan  gcc_assert (TREE_CODE (decl) == FIELD_DECL
10926169689Skan	      && DECL_BIT_FIELD_TYPE (decl));
1092790075Sobrien
1092890075Sobrien  if (host_integerp (DECL_SIZE (decl), 1))
1092990075Sobrien    add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
1093050397Sobrien}
1093150397Sobrien
1093250397Sobrien/* If the compiled language is ANSI C, then add a 'prototyped'
1093350397Sobrien   attribute, if arg types are given for the parameters of a function.  */
1093450397Sobrien
1093550397Sobrienstatic inline void
10936132718Skanadd_prototyped_attribute (dw_die_ref die, tree func_type)
1093750397Sobrien{
1093850397Sobrien  if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
1093950397Sobrien      && TYPE_ARG_TYPES (func_type) != NULL)
1094050397Sobrien    add_AT_flag (die, DW_AT_prototyped, 1);
1094150397Sobrien}
1094250397Sobrien
1094350397Sobrien/* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
1094450397Sobrien   by looking in either the type declaration or object declaration
1094550397Sobrien   equate table.  */
1094650397Sobrien
1094750397Sobrienstatic inline void
10948132718Skanadd_abstract_origin_attribute (dw_die_ref die, tree origin)
1094950397Sobrien{
1095050397Sobrien  dw_die_ref origin_die = NULL;
1095190075Sobrien
1095290075Sobrien  if (TREE_CODE (origin) != FUNCTION_DECL)
1095390075Sobrien    {
1095490075Sobrien      /* We may have gotten separated from the block for the inlined
1095590075Sobrien	 function, if we're in an exception handler or some such; make
1095690075Sobrien	 sure that the abstract function has been written out.
1095790075Sobrien
10958132718Skan	 Doing this for nested functions is wrong, however; functions are
1095990075Sobrien	 distinct units, and our context might not even be inline.  */
1096090075Sobrien      tree fn = origin;
1096190075Sobrien
1096290075Sobrien      if (TYPE_P (fn))
1096390075Sobrien	fn = TYPE_STUB_DECL (fn);
10964169689Skan
1096590075Sobrien      fn = decl_function_context (fn);
1096690075Sobrien      if (fn)
1096790075Sobrien	dwarf2out_abstract_function (fn);
1096890075Sobrien    }
1096990075Sobrien
1097090075Sobrien  if (DECL_P (origin))
1097150397Sobrien    origin_die = lookup_decl_die (origin);
1097290075Sobrien  else if (TYPE_P (origin))
1097350397Sobrien    origin_die = lookup_type_die (origin);
1097450397Sobrien
10975169689Skan  /* XXX: Functions that are never lowered don't always have correct block
10976169689Skan     trees (in the case of java, they simply have no block tree, in some other
10977169689Skan     languages).  For these functions, there is nothing we can really do to
10978169689Skan     output correct debug info for inlined functions in all cases.  Rather
10979169689Skan     than die, we'll just produce deficient debug info now, in that we will
10980169689Skan     have variables without a proper abstract origin.  In the future, when all
10981169689Skan     functions are lowered, we should re-add a gcc_assert (origin_die)
10982169689Skan     here.  */
1098390075Sobrien
10984169689Skan  if (origin_die)
10985169689Skan      add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
1098650397Sobrien}
1098750397Sobrien
1098850397Sobrien/* We do not currently support the pure_virtual attribute.  */
1098950397Sobrien
1099050397Sobrienstatic inline void
10991132718Skanadd_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
1099250397Sobrien{
1099350397Sobrien  if (DECL_VINDEX (func_decl))
1099450397Sobrien    {
1099550397Sobrien      add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
1099650397Sobrien
1099790075Sobrien      if (host_integerp (DECL_VINDEX (func_decl), 0))
1099890075Sobrien	add_AT_loc (die, DW_AT_vtable_elem_location,
1099990075Sobrien		    new_loc_descr (DW_OP_constu,
1100090075Sobrien				   tree_low_cst (DECL_VINDEX (func_decl), 0),
1100190075Sobrien				   0));
1100290075Sobrien
1100350397Sobrien      /* GNU extension: Record what type this method came from originally.  */
1100450397Sobrien      if (debug_info_level > DINFO_LEVEL_TERSE)
1100550397Sobrien	add_AT_die_ref (die, DW_AT_containing_type,
1100650397Sobrien			lookup_type_die (DECL_CONTEXT (func_decl)));
1100750397Sobrien    }
1100850397Sobrien}
1100950397Sobrien
1101050397Sobrien/* Add source coordinate attributes for the given decl.  */
1101150397Sobrien
1101250397Sobrienstatic void
11013132718Skanadd_src_coords_attributes (dw_die_ref die, tree decl)
1101450397Sobrien{
11015169689Skan  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
1101650397Sobrien
11017169689Skan  add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11018169689Skan  add_AT_unsigned (die, DW_AT_decl_line, s.line);
1101950397Sobrien}
1102050397Sobrien
11021132718Skan/* Add a DW_AT_name attribute and source coordinate attribute for the
1102250397Sobrien   given decl, but only if it actually has a name.  */
1102350397Sobrien
1102450397Sobrienstatic void
11025132718Skanadd_name_and_src_coords_attributes (dw_die_ref die, tree decl)
1102650397Sobrien{
1102790075Sobrien  tree decl_name;
1102850397Sobrien
1102990075Sobrien  decl_name = DECL_NAME (decl);
1103050397Sobrien  if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
1103150397Sobrien    {
1103250397Sobrien      add_name_attribute (die, dwarf2_name (decl, 0));
1103390075Sobrien      if (! DECL_ARTIFICIAL (decl))
1103490075Sobrien	add_src_coords_attributes (die, decl);
1103590075Sobrien
1103650397Sobrien      if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
1103790075Sobrien	  && TREE_PUBLIC (decl)
1103890075Sobrien	  && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11039169689Skan	  && !DECL_ABSTRACT (decl)
11040169689Skan	  && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
1104150397Sobrien	add_AT_string (die, DW_AT_MIPS_linkage_name,
1104250397Sobrien		       IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1104350397Sobrien    }
1104490075Sobrien
1104590075Sobrien#ifdef VMS_DEBUGGING_INFO
1104690075Sobrien  /* Get the function's name, as described by its RTL.  This may be different
1104790075Sobrien     from the DECL_NAME name used in the source file.  */
1104890075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
1104990075Sobrien    {
1105090075Sobrien      add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
1105190075Sobrien		   XEXP (DECL_RTL (decl), 0));
11052169689Skan      VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
1105390075Sobrien    }
1105490075Sobrien#endif
1105550397Sobrien}
1105650397Sobrien
1105790075Sobrien/* Push a new declaration scope.  */
1105850397Sobrien
1105950397Sobrienstatic void
11060132718Skanpush_decl_scope (tree scope)
1106150397Sobrien{
11062169689Skan  VEC_safe_push (tree, gc, decl_scope_table, scope);
1106390075Sobrien}
1106450397Sobrien
1106590075Sobrien/* Pop a declaration scope.  */
1106650397Sobrien
1106790075Sobrienstatic inline void
11068132718Skanpop_decl_scope (void)
1106990075Sobrien{
11070169689Skan  VEC_pop (tree, decl_scope_table);
1107150397Sobrien}
1107250397Sobrien
1107390075Sobrien/* Return the DIE for the scope that immediately contains this type.
1107490075Sobrien   Non-named types get global scope.  Named types nested in other
1107590075Sobrien   types get their containing scope if it's open, or global scope
1107690075Sobrien   otherwise.  All other types (i.e. function-local named types) get
1107790075Sobrien   the current active scope.  */
1107850397Sobrien
1107950397Sobrienstatic dw_die_ref
11080132718Skanscope_die_for (tree t, dw_die_ref context_die)
1108150397Sobrien{
1108290075Sobrien  dw_die_ref scope_die = NULL;
1108390075Sobrien  tree containing_scope;
1108490075Sobrien  int i;
1108550397Sobrien
1108690075Sobrien  /* Non-types always go in the current scope.  */
11087169689Skan  gcc_assert (TYPE_P (t));
1108850397Sobrien
1108990075Sobrien  containing_scope = TYPE_CONTEXT (t);
1109090075Sobrien
11091132718Skan  /* Use the containing namespace if it was passed in (for a declaration).  */
1109250397Sobrien  if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11093132718Skan    {
11094132718Skan      if (context_die == lookup_decl_die (containing_scope))
11095132718Skan	/* OK */;
11096132718Skan      else
11097132718Skan	containing_scope = NULL_TREE;
11098132718Skan    }
1109950397Sobrien
1110052284Sobrien  /* Ignore function type "scopes" from the C frontend.  They mean that
1110152284Sobrien     a tagged type is local to a parmlist of a function declarator, but
1110252284Sobrien     that isn't useful to DWARF.  */
1110352284Sobrien  if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
1110452284Sobrien    containing_scope = NULL_TREE;
1110552284Sobrien
11106169689Skan  if (containing_scope == NULL_TREE)
1110750397Sobrien    scope_die = comp_unit_die;
1110890075Sobrien  else if (TYPE_P (containing_scope))
1110950397Sobrien    {
1111090075Sobrien      /* For types, we can just look up the appropriate DIE.  But
1111190075Sobrien	 first we check to see if we're in the middle of emitting it
1111290075Sobrien	 so we know where the new DIE should go.  */
11113169689Skan      for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11114169689Skan	if (VEC_index (tree, decl_scope_table, i) == containing_scope)
1111590075Sobrien	  break;
1111650397Sobrien
1111750397Sobrien      if (i < 0)
1111850397Sobrien	{
11119169689Skan	  gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11120169689Skan		      || TREE_ASM_WRITTEN (containing_scope));
1112152284Sobrien
1112252284Sobrien	  /* If none of the current dies are suitable, we get file scope.  */
1112352284Sobrien	  scope_die = comp_unit_die;
1112450397Sobrien	}
1112590075Sobrien      else
1112690075Sobrien	scope_die = lookup_type_die (containing_scope);
1112750397Sobrien    }
1112890075Sobrien  else
1112990075Sobrien    scope_die = context_die;
1113050397Sobrien
1113150397Sobrien  return scope_die;
1113250397Sobrien}
1113350397Sobrien
1113490075Sobrien/* Returns nonzero if CONTEXT_DIE is internal to a function.  */
1113590075Sobrien
1113690075Sobrienstatic inline int
11137132718Skanlocal_scope_p (dw_die_ref context_die)
1113850397Sobrien{
1113990075Sobrien  for (; context_die; context_die = context_die->die_parent)
1114090075Sobrien    if (context_die->die_tag == DW_TAG_inlined_subroutine
1114190075Sobrien	|| context_die->die_tag == DW_TAG_subprogram)
1114290075Sobrien      return 1;
1114390075Sobrien
1114490075Sobrien  return 0;
1114550397Sobrien}
1114650397Sobrien
11147132718Skan/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11148132718Skan   whether or not to treat a DIE in this context as a declaration.  */
1114990075Sobrien
1115090075Sobrienstatic inline int
11151132718Skanclass_or_namespace_scope_p (dw_die_ref context_die)
1115290075Sobrien{
1115390075Sobrien  return (context_die
1115490075Sobrien	  && (context_die->die_tag == DW_TAG_structure_type
11155132718Skan	      || context_die->die_tag == DW_TAG_union_type
11156132718Skan	      || context_die->die_tag == DW_TAG_namespace));
1115790075Sobrien}
1115890075Sobrien
1115950397Sobrien/* Many forms of DIEs require a "type description" attribute.  This
1116050397Sobrien   routine locates the proper "type descriptor" die for the type given
11161132718Skan   by 'type', and adds a DW_AT_type attribute below the given die.  */
1116250397Sobrien
1116350397Sobrienstatic void
11164132718Skanadd_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11165132718Skan		    int decl_volatile, dw_die_ref context_die)
1116650397Sobrien{
1116790075Sobrien  enum tree_code code  = TREE_CODE (type);
1116890075Sobrien  dw_die_ref type_die  = NULL;
1116950397Sobrien
1117050397Sobrien  /* ??? If this type is an unnamed subrange type of an integral or
1117150397Sobrien     floating-point type, use the inner type.  This is because we have no
1117250397Sobrien     support for unnamed types in base_type_die.  This can happen if this is
1117350397Sobrien     an Ada subrange type.  Correct solution is emit a subrange type die.  */
1117450397Sobrien  if ((code == INTEGER_TYPE || code == REAL_TYPE)
1117550397Sobrien      && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
1117650397Sobrien    type = TREE_TYPE (type), code = TREE_CODE (type);
1117750397Sobrien
1117890075Sobrien  if (code == ERROR_MARK
1117990075Sobrien      /* Handle a special case.  For functions whose return type is void, we
1118090075Sobrien	 generate *no* type attribute.  (Note that no object may have type
1118190075Sobrien	 `void', so this only applies to function return types).  */
1118290075Sobrien      || code == VOID_TYPE)
1118350397Sobrien    return;
1118450397Sobrien
1118550397Sobrien  type_die = modified_type_die (type,
1118650397Sobrien				decl_const || TYPE_READONLY (type),
1118750397Sobrien				decl_volatile || TYPE_VOLATILE (type),
1118850397Sobrien				context_die);
1118990075Sobrien
1119050397Sobrien  if (type_die != NULL)
1119150397Sobrien    add_AT_die_ref (object_die, DW_AT_type, type_die);
1119250397Sobrien}
1119350397Sobrien
11194169689Skan/* Given an object die, add the calling convention attribute for the
11195169689Skan   function call type.  */
11196169689Skanstatic void
11197169689Skanadd_calling_convention_attribute (dw_die_ref subr_die, tree type)
11198169689Skan{
11199169689Skan  enum dwarf_calling_convention value = DW_CC_normal;
11200169689Skan
11201169689Skan  value = targetm.dwarf_calling_convention (type);
11202169689Skan
11203169689Skan  /* Only add the attribute if the backend requests it, and
11204169689Skan     is not DW_CC_normal.  */
11205169689Skan  if (value && (value != DW_CC_normal))
11206169689Skan    add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11207169689Skan}
11208169689Skan
1120950397Sobrien/* Given a tree pointer to a struct, class, union, or enum type node, return
1121050397Sobrien   a pointer to the (string) tag name for the given type, or zero if the type
1121150397Sobrien   was declared without a tag.  */
1121250397Sobrien
1121390075Sobrienstatic const char *
11214132718Skantype_tag (tree type)
1121550397Sobrien{
1121690075Sobrien  const char *name = 0;
1121750397Sobrien
1121850397Sobrien  if (TYPE_NAME (type) != 0)
1121950397Sobrien    {
1122090075Sobrien      tree t = 0;
1122150397Sobrien
1122250397Sobrien      /* Find the IDENTIFIER_NODE for the type name.  */
1122350397Sobrien      if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1122450397Sobrien	t = TYPE_NAME (type);
1122550397Sobrien
1122690075Sobrien      /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11227132718Skan	 a TYPE_DECL node, regardless of whether or not a `typedef' was
11228132718Skan	 involved.  */
1122950397Sobrien      else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1123050397Sobrien	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
1123150397Sobrien	t = DECL_NAME (TYPE_NAME (type));
1123250397Sobrien
1123350397Sobrien      /* Now get the name as a string, or invent one.  */
1123450397Sobrien      if (t != 0)
1123550397Sobrien	name = IDENTIFIER_POINTER (t);
1123650397Sobrien    }
1123750397Sobrien
1123850397Sobrien  return (name == 0 || *name == '\0') ? 0 : name;
1123950397Sobrien}
1124050397Sobrien
1124150397Sobrien/* Return the type associated with a data member, make a special check
1124250397Sobrien   for bit field types.  */
1124350397Sobrien
1124450397Sobrienstatic inline tree
11245132718Skanmember_declared_type (tree member)
1124650397Sobrien{
1124750397Sobrien  return (DECL_BIT_FIELD_TYPE (member)
1124890075Sobrien	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
1124950397Sobrien}
1125050397Sobrien
1125150397Sobrien/* Get the decl's label, as described by its RTL. This may be different
1125250397Sobrien   from the DECL_NAME name used in the source file.  */
1125350397Sobrien
1125450397Sobrien#if 0
1125590075Sobrienstatic const char *
11256132718Skandecl_start_label (tree decl)
1125750397Sobrien{
1125850397Sobrien  rtx x;
1125990075Sobrien  const char *fnname;
1126090075Sobrien
1126150397Sobrien  x = DECL_RTL (decl);
11262169689Skan  gcc_assert (MEM_P (x));
1126350397Sobrien
1126450397Sobrien  x = XEXP (x, 0);
11265169689Skan  gcc_assert (GET_CODE (x) == SYMBOL_REF);
1126650397Sobrien
1126750397Sobrien  fnname = XSTR (x, 0);
1126850397Sobrien  return fnname;
1126950397Sobrien}
1127050397Sobrien#endif
1127150397Sobrien
1127250397Sobrien/* These routines generate the internal representation of the DIE's for
1127350397Sobrien   the compilation unit.  Debugging information is collected by walking
1127450397Sobrien   the declaration trees passed in from dwarf2out_decl().  */
1127550397Sobrien
1127650397Sobrienstatic void
11277132718Skangen_array_type_die (tree type, dw_die_ref context_die)
1127850397Sobrien{
1127990075Sobrien  dw_die_ref scope_die = scope_die_for (type, context_die);
1128090075Sobrien  dw_die_ref array_die;
1128190075Sobrien  tree element_type;
1128250397Sobrien
1128350397Sobrien  /* ??? The SGI dwarf reader fails for array of array of enum types unless
1128450397Sobrien     the inner array type comes before the outer array type.  Thus we must
1128550397Sobrien     call gen_type_die before we call new_die.  See below also.  */
1128650397Sobrien#ifdef MIPS_DEBUGGING_INFO
1128750397Sobrien  gen_type_die (TREE_TYPE (type), context_die);
1128850397Sobrien#endif
1128950397Sobrien
1129090075Sobrien  array_die = new_die (DW_TAG_array_type, scope_die, type);
1129196263Sobrien  add_name_attribute (array_die, type_tag (type));
1129296263Sobrien  equate_type_number_to_die (type, array_die);
1129350397Sobrien
1129496263Sobrien  if (TREE_CODE (type) == VECTOR_TYPE)
1129596263Sobrien    {
1129696263Sobrien      /* The frontend feeds us a representation for the vector as a struct
1129796263Sobrien	 containing an array.  Pull out the array type.  */
1129896263Sobrien      type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
1129996263Sobrien      add_AT_flag (array_die, DW_AT_GNU_vector, 1);
1130096263Sobrien    }
1130196263Sobrien
1130250397Sobrien#if 0
1130350397Sobrien  /* We default the array ordering.  SDB will probably do
1130450397Sobrien     the right things even if DW_AT_ordering is not present.  It's not even
1130550397Sobrien     an issue until we start to get into multidimensional arrays anyway.  If
1130650397Sobrien     SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
1130750397Sobrien     then we'll have to put the DW_AT_ordering attribute back in.  (But if
1130850397Sobrien     and when we find out that we need to put these in, we will only do so
1130950397Sobrien     for multidimensional arrays.  */
1131050397Sobrien  add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
1131150397Sobrien#endif
1131250397Sobrien
1131350397Sobrien#ifdef MIPS_DEBUGGING_INFO
1131450397Sobrien  /* The SGI compilers handle arrays of unknown bound by setting
1131550397Sobrien     AT_declaration and not emitting any subrange DIEs.  */
1131650397Sobrien  if (! TYPE_DOMAIN (type))
11317132718Skan    add_AT_flag (array_die, DW_AT_declaration, 1);
1131850397Sobrien  else
1131950397Sobrien#endif
1132050397Sobrien    add_subscript_info (array_die, type);
1132150397Sobrien
1132250397Sobrien  /* Add representation of the type of the elements of this array type.  */
1132350397Sobrien  element_type = TREE_TYPE (type);
1132450397Sobrien
1132550397Sobrien  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
1132650397Sobrien     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
1132750397Sobrien     We work around this by disabling this feature.  See also
1132850397Sobrien     add_subscript_info.  */
1132950397Sobrien#ifndef MIPS_DEBUGGING_INFO
1133050397Sobrien  while (TREE_CODE (element_type) == ARRAY_TYPE)
1133150397Sobrien    element_type = TREE_TYPE (element_type);
1133250397Sobrien
1133350397Sobrien  gen_type_die (element_type, context_die);
1133450397Sobrien#endif
1133550397Sobrien
1133650397Sobrien  add_type_attribute (array_die, element_type, 0, 0, context_die);
11337260396Spfg
11338260396Spfg  if (get_AT (array_die, DW_AT_name))
11339260396Spfg    add_pubtype (type, array_die);
1134050397Sobrien}
1134150397Sobrien
1134250397Sobrien#if 0
1134350397Sobrienstatic void
11344132718Skangen_entry_point_die (tree decl, dw_die_ref context_die)
1134550397Sobrien{
1134690075Sobrien  tree origin = decl_ultimate_origin (decl);
1134790075Sobrien  dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
1134890075Sobrien
1134950397Sobrien  if (origin != NULL)
1135050397Sobrien    add_abstract_origin_attribute (decl_die, origin);
1135150397Sobrien  else
1135250397Sobrien    {
1135350397Sobrien      add_name_and_src_coords_attributes (decl_die, decl);
1135450397Sobrien      add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
1135550397Sobrien			  0, 0, context_die);
1135650397Sobrien    }
1135750397Sobrien
1135850397Sobrien  if (DECL_ABSTRACT (decl))
1135950397Sobrien    equate_decl_number_to_die (decl, decl_die);
1136050397Sobrien  else
1136150397Sobrien    add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
1136250397Sobrien}
1136350397Sobrien#endif
1136450397Sobrien
1136552284Sobrien/* Walk through the list of incomplete types again, trying once more to
1136652284Sobrien   emit full debugging info for them.  */
1136752284Sobrien
1136852284Sobrienstatic void
11369132718Skanretry_incomplete_types (void)
1137052284Sobrien{
1137190075Sobrien  int i;
1137252284Sobrien
11373169689Skan  for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11374169689Skan    gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
1137552284Sobrien}
1137652284Sobrien
1137750397Sobrien/* Generate a DIE to represent an inlined instance of an enumeration type.  */
1137850397Sobrien
1137950397Sobrienstatic void
11380132718Skangen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
1138150397Sobrien{
1138290075Sobrien  dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
1138350397Sobrien
1138490075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1138590075Sobrien     be incomplete and such types are not marked.  */
1138650397Sobrien  add_abstract_origin_attribute (type_die, type);
1138750397Sobrien}
1138850397Sobrien
1138950397Sobrien/* Generate a DIE to represent an inlined instance of a structure type.  */
1139050397Sobrien
1139150397Sobrienstatic void
11392132718Skangen_inlined_structure_type_die (tree type, dw_die_ref context_die)
1139350397Sobrien{
1139490075Sobrien  dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
1139550397Sobrien
1139690075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1139790075Sobrien     be incomplete and such types are not marked.  */
1139850397Sobrien  add_abstract_origin_attribute (type_die, type);
1139950397Sobrien}
1140050397Sobrien
1140150397Sobrien/* Generate a DIE to represent an inlined instance of a union type.  */
1140250397Sobrien
1140350397Sobrienstatic void
11404132718Skangen_inlined_union_type_die (tree type, dw_die_ref context_die)
1140550397Sobrien{
1140690075Sobrien  dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
1140750397Sobrien
1140890075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1140990075Sobrien     be incomplete and such types are not marked.  */
1141050397Sobrien  add_abstract_origin_attribute (type_die, type);
1141150397Sobrien}
1141250397Sobrien
1141350397Sobrien/* Generate a DIE to represent an enumeration type.  Note that these DIEs
1141450397Sobrien   include all of the information about the enumeration values also. Each
1141550397Sobrien   enumerated type name/value is listed as a child of the enumerated type
1141650397Sobrien   DIE.  */
1141750397Sobrien
11418132718Skanstatic dw_die_ref
11419132718Skangen_enumeration_type_die (tree type, dw_die_ref context_die)
1142050397Sobrien{
1142190075Sobrien  dw_die_ref type_die = lookup_type_die (type);
1142250397Sobrien
1142350397Sobrien  if (type_die == NULL)
1142450397Sobrien    {
1142550397Sobrien      type_die = new_die (DW_TAG_enumeration_type,
1142690075Sobrien			  scope_die_for (type, context_die), type);
1142750397Sobrien      equate_type_number_to_die (type, type_die);
1142850397Sobrien      add_name_attribute (type_die, type_tag (type));
1142950397Sobrien    }
1143050397Sobrien  else if (! TYPE_SIZE (type))
11431132718Skan    return type_die;
1143250397Sobrien  else
1143350397Sobrien    remove_AT (type_die, DW_AT_declaration);
1143450397Sobrien
1143550397Sobrien  /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
1143650397Sobrien     given enum type is incomplete, do not generate the DW_AT_byte_size
1143750397Sobrien     attribute or the DW_AT_element_list attribute.  */
1143850397Sobrien  if (TYPE_SIZE (type))
1143950397Sobrien    {
1144090075Sobrien      tree link;
1144150397Sobrien
1144250397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1144350397Sobrien      add_byte_size_attribute (type_die, type);
1144450397Sobrien      if (TYPE_STUB_DECL (type) != NULL_TREE)
1144550397Sobrien	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
1144650397Sobrien
1144750397Sobrien      /* If the first reference to this type was as the return type of an
1144850397Sobrien	 inline function, then it may not have a parent.  Fix this now.  */
1144950397Sobrien      if (type_die->die_parent == NULL)
1145050397Sobrien	add_child_die (scope_die_for (type, context_die), type_die);
1145150397Sobrien
11452169689Skan      for (link = TYPE_VALUES (type);
1145350397Sobrien	   link != NULL; link = TREE_CHAIN (link))
1145450397Sobrien	{
1145590075Sobrien	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11456132718Skan	  tree value = TREE_VALUE (link);
1145750397Sobrien
1145850397Sobrien	  add_name_attribute (enum_die,
1145950397Sobrien			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
1146090075Sobrien
11461169689Skan	  if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11462132718Skan	    /* DWARF2 does not provide a way of indicating whether or
11463132718Skan	       not enumeration constants are signed or unsigned.  GDB
11464132718Skan	       always assumes the values are signed, so we output all
11465132718Skan	       values as if they were signed.  That means that
11466132718Skan	       enumeration constants with very large unsigned values
11467132718Skan	       will appear to have negative values in the debugger.  */
11468132718Skan	    add_AT_int (enum_die, DW_AT_const_value,
11469132718Skan			tree_low_cst (value, tree_int_cst_sgn (value) > 0));
1147050397Sobrien	}
1147150397Sobrien    }
1147250397Sobrien  else
1147350397Sobrien    add_AT_flag (type_die, DW_AT_declaration, 1);
11474132718Skan
11475260396Spfg  if (get_AT (type_die, DW_AT_name))
11476260396Spfg    add_pubtype (type, type_die);
11477260396Spfg
11478132718Skan  return type_die;
1147950397Sobrien}
1148050397Sobrien
1148150397Sobrien/* Generate a DIE to represent either a real live formal parameter decl or to
1148250397Sobrien   represent just the type of some formal parameter position in some function
1148350397Sobrien   type.
1148450397Sobrien
1148550397Sobrien   Note that this routine is a bit unusual because its argument may be a
1148650397Sobrien   ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
1148750397Sobrien   represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
1148850397Sobrien   node.  If it's the former then this function is being called to output a
1148950397Sobrien   DIE to represent a formal parameter object (or some inlining thereof).  If
1149050397Sobrien   it's the latter, then this function is only being called to output a
1149150397Sobrien   DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
1149250397Sobrien   argument type of some subprogram type.  */
1149350397Sobrien
1149450397Sobrienstatic dw_die_ref
11495132718Skangen_formal_parameter_die (tree node, dw_die_ref context_die)
1149650397Sobrien{
1149790075Sobrien  dw_die_ref parm_die
1149890075Sobrien    = new_die (DW_TAG_formal_parameter, context_die, node);
1149990075Sobrien  tree origin;
1150050397Sobrien
1150150397Sobrien  switch (TREE_CODE_CLASS (TREE_CODE (node)))
1150250397Sobrien    {
11503169689Skan    case tcc_declaration:
1150450397Sobrien      origin = decl_ultimate_origin (node);
1150550397Sobrien      if (origin != NULL)
1150650397Sobrien	add_abstract_origin_attribute (parm_die, origin);
1150750397Sobrien      else
1150850397Sobrien	{
1150950397Sobrien	  add_name_and_src_coords_attributes (parm_die, node);
1151050397Sobrien	  add_type_attribute (parm_die, TREE_TYPE (node),
1151150397Sobrien			      TREE_READONLY (node),
1151250397Sobrien			      TREE_THIS_VOLATILE (node),
1151350397Sobrien			      context_die);
1151450397Sobrien	  if (DECL_ARTIFICIAL (node))
1151550397Sobrien	    add_AT_flag (parm_die, DW_AT_artificial, 1);
1151650397Sobrien	}
1151750397Sobrien
1151850397Sobrien      equate_decl_number_to_die (node, parm_die);
1151950397Sobrien      if (! DECL_ABSTRACT (node))
11520169689Skan	add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
1152150397Sobrien
1152250397Sobrien      break;
1152350397Sobrien
11524169689Skan    case tcc_type:
1152550397Sobrien      /* We were called with some kind of a ..._TYPE node.  */
1152650397Sobrien      add_type_attribute (parm_die, node, 0, 0, context_die);
1152750397Sobrien      break;
1152850397Sobrien
1152950397Sobrien    default:
11530169689Skan      gcc_unreachable ();
1153150397Sobrien    }
1153250397Sobrien
1153350397Sobrien  return parm_die;
1153450397Sobrien}
1153550397Sobrien
1153650397Sobrien/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
1153750397Sobrien   at the end of an (ANSI prototyped) formal parameters list.  */
1153850397Sobrien
1153950397Sobrienstatic void
11540132718Skangen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
1154150397Sobrien{
1154290075Sobrien  new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
1154350397Sobrien}
1154450397Sobrien
1154550397Sobrien/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
1154650397Sobrien   DW_TAG_unspecified_parameters DIE) to represent the types of the formal
1154750397Sobrien   parameters as specified in some function type specification (except for
1154890075Sobrien   those which appear as part of a function *definition*).  */
1154950397Sobrien
1155050397Sobrienstatic void
11551132718Skangen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
1155250397Sobrien{
1155390075Sobrien  tree link;
1155490075Sobrien  tree formal_type = NULL;
1155590075Sobrien  tree first_parm_type;
1155690075Sobrien  tree arg;
1155750397Sobrien
1155890075Sobrien  if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
1155990075Sobrien    {
1156090075Sobrien      arg = DECL_ARGUMENTS (function_or_method_type);
1156190075Sobrien      function_or_method_type = TREE_TYPE (function_or_method_type);
1156290075Sobrien    }
1156390075Sobrien  else
1156490075Sobrien    arg = NULL_TREE;
11565117395Skan
1156690075Sobrien  first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
1156750397Sobrien
1156890075Sobrien  /* Make our first pass over the list of formal parameter types and output a
1156950397Sobrien     DW_TAG_formal_parameter DIE for each one.  */
1157090075Sobrien  for (link = first_parm_type; link; )
1157150397Sobrien    {
1157290075Sobrien      dw_die_ref parm_die;
1157390075Sobrien
1157450397Sobrien      formal_type = TREE_VALUE (link);
1157550397Sobrien      if (formal_type == void_type_node)
1157650397Sobrien	break;
1157750397Sobrien
1157850397Sobrien      /* Output a (nameless) DIE to represent the formal parameter itself.  */
1157950397Sobrien      parm_die = gen_formal_parameter_die (formal_type, context_die);
1158090075Sobrien      if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
1158190075Sobrien	   && link == first_parm_type)
1158290075Sobrien	  || (arg && DECL_ARTIFICIAL (arg)))
1158350397Sobrien	add_AT_flag (parm_die, DW_AT_artificial, 1);
1158490075Sobrien
1158590075Sobrien      link = TREE_CHAIN (link);
1158690075Sobrien      if (arg)
1158790075Sobrien	arg = TREE_CHAIN (arg);
1158850397Sobrien    }
1158950397Sobrien
1159050397Sobrien  /* If this function type has an ellipsis, add a
1159150397Sobrien     DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
1159250397Sobrien  if (formal_type != void_type_node)
1159350397Sobrien    gen_unspecified_parameters_die (function_or_method_type, context_die);
1159450397Sobrien
1159590075Sobrien  /* Make our second (and final) pass over the list of formal parameter types
1159650397Sobrien     and output DIEs to represent those types (as necessary).  */
1159750397Sobrien  for (link = TYPE_ARG_TYPES (function_or_method_type);
1159890075Sobrien       link && TREE_VALUE (link);
1159950397Sobrien       link = TREE_CHAIN (link))
1160090075Sobrien    gen_type_die (TREE_VALUE (link), context_die);
1160190075Sobrien}
1160290075Sobrien
1160390075Sobrien/* We want to generate the DIE for TYPE so that we can generate the
1160490075Sobrien   die for MEMBER, which has been defined; we will need to refer back
1160590075Sobrien   to the member declaration nested within TYPE.  If we're trying to
1160690075Sobrien   generate minimal debug info for TYPE, processing TYPE won't do the
1160790075Sobrien   trick; we need to attach the member declaration by hand.  */
1160890075Sobrien
1160990075Sobrienstatic void
11610132718Skangen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
1161190075Sobrien{
1161290075Sobrien  gen_type_die (type, context_die);
1161390075Sobrien
1161490075Sobrien  /* If we're trying to avoid duplicate debug info, we may not have
1161590075Sobrien     emitted the member decl for this function.  Emit it now.  */
1161690075Sobrien  if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
1161790075Sobrien      && ! lookup_decl_die (member))
1161850397Sobrien    {
11619169689Skan      dw_die_ref type_die;
11620169689Skan      gcc_assert (!decl_ultimate_origin (member));
1162150397Sobrien
1162290075Sobrien      push_decl_scope (type);
11623169689Skan      type_die = lookup_type_die (type);
1162490075Sobrien      if (TREE_CODE (member) == FUNCTION_DECL)
11625169689Skan	gen_subprogram_die (member, type_die);
11626169689Skan      else if (TREE_CODE (member) == FIELD_DECL)
11627169689Skan	{
11628169689Skan	  /* Ignore the nameless fields that are used to skip bits but handle
11629169689Skan	     C++ anonymous unions and structs.  */
11630169689Skan	  if (DECL_NAME (member) != NULL_TREE
11631169689Skan	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11632169689Skan	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11633169689Skan	    {
11634169689Skan	      gen_type_die (member_declared_type (member), type_die);
11635169689Skan	      gen_field_die (member, type_die);
11636169689Skan	    }
11637169689Skan	}
1163890075Sobrien      else
11639169689Skan	gen_variable_die (member, type_die);
1164090075Sobrien
1164190075Sobrien      pop_decl_scope ();
1164250397Sobrien    }
1164350397Sobrien}
1164450397Sobrien
1164590075Sobrien/* Generate the DWARF2 info for the "abstract" instance of a function which we
1164690075Sobrien   may later generate inlined and/or out-of-line instances of.  */
1164790075Sobrien
1164890075Sobrienstatic void
11649132718Skandwarf2out_abstract_function (tree decl)
1165090075Sobrien{
1165190075Sobrien  dw_die_ref old_die;
1165290075Sobrien  tree save_fn;
11653169689Skan  struct function *save_cfun;
1165490075Sobrien  tree context;
1165590075Sobrien  int was_abstract = DECL_ABSTRACT (decl);
1165690075Sobrien
1165790075Sobrien  /* Make sure we have the actual abstract inline, not a clone.  */
1165890075Sobrien  decl = DECL_ORIGIN (decl);
1165990075Sobrien
11660117395Skan  old_die = lookup_decl_die (decl);
11661132718Skan  if (old_die && get_AT (old_die, DW_AT_inline))
1166290075Sobrien    /* We've already generated the abstract instance.  */
1166390075Sobrien    return;
1166490075Sobrien
1166590075Sobrien  /* Be sure we've emitted the in-class declaration DIE (if any) first, so
1166690075Sobrien     we don't get confused by DECL_ABSTRACT.  */
1166790075Sobrien  if (debug_info_level > DINFO_LEVEL_TERSE)
1166890075Sobrien    {
1166990075Sobrien      context = decl_class_context (decl);
1167090075Sobrien      if (context)
1167190075Sobrien	gen_type_die_for_member
1167290075Sobrien	  (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
1167390075Sobrien    }
11674117395Skan
1167590075Sobrien  /* Pretend we've just finished compiling this function.  */
1167690075Sobrien  save_fn = current_function_decl;
11677169689Skan  save_cfun = cfun;
1167890075Sobrien  current_function_decl = decl;
11679169689Skan  cfun = DECL_STRUCT_FUNCTION (decl);
1168090075Sobrien
1168190075Sobrien  set_decl_abstract_flags (decl, 1);
1168290075Sobrien  dwarf2out_decl (decl);
1168390075Sobrien  if (! was_abstract)
1168490075Sobrien    set_decl_abstract_flags (decl, 0);
1168590075Sobrien
1168690075Sobrien  current_function_decl = save_fn;
11687169689Skan  cfun = save_cfun;
1168890075Sobrien}
1168990075Sobrien
11690169689Skan/* Helper function of premark_used_types() which gets called through
11691169689Skan   htab_traverse_resize().
11692169689Skan
11693169689Skan   Marks the DIE of a given type in *SLOT as perennial, so it never gets
11694169689Skan   marked as unused by prune_unused_types.  */
11695169689Skanstatic int
11696169689Skanpremark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11697169689Skan{
11698169689Skan  tree type;
11699169689Skan  dw_die_ref die;
11700169689Skan
11701169689Skan  type = *slot;
11702169689Skan  die = lookup_type_die (type);
11703169689Skan  if (die != NULL)
11704169689Skan    die->die_perennial_p = 1;
11705169689Skan  return 1;
11706169689Skan}
11707169689Skan
11708169689Skan/* Mark all members of used_types_hash as perennial.  */
11709169689Skanstatic void
11710169689Skanpremark_used_types (void)
11711169689Skan{
11712169689Skan  if (cfun && cfun->used_types_hash)
11713169689Skan    htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11714169689Skan}
11715169689Skan
1171650397Sobrien/* Generate a DIE to represent a declared function (either file-scope or
1171750397Sobrien   block-local).  */
1171850397Sobrien
1171950397Sobrienstatic void
11720132718Skangen_subprogram_die (tree decl, dw_die_ref context_die)
1172150397Sobrien{
1172250397Sobrien  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
1172390075Sobrien  tree origin = decl_ultimate_origin (decl);
1172490075Sobrien  dw_die_ref subr_die;
1172590075Sobrien  tree fn_arg_types;
1172690075Sobrien  tree outer_scope;
1172790075Sobrien  dw_die_ref old_die = lookup_decl_die (decl);
1172890075Sobrien  int declaration = (current_function_decl != decl
11729132718Skan		     || class_or_namespace_scope_p (context_die));
1173050397Sobrien
11731169689Skan  premark_used_types ();
11732169689Skan
1173390075Sobrien  /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
1173490075Sobrien     started to generate the abstract instance of an inline, decided to output
1173590075Sobrien     its containing class, and proceeded to emit the declaration of the inline
1173690075Sobrien     from the member list for the class.  If so, DECLARATION takes priority;
1173790075Sobrien     we'll get back to the abstract instance when done with the class.  */
1173890075Sobrien
1173990075Sobrien  /* The class-scope declaration DIE must be the primary DIE.  */
11740132718Skan  if (origin && declaration && class_or_namespace_scope_p (context_die))
1174150397Sobrien    {
1174290075Sobrien      origin = NULL;
11743169689Skan      gcc_assert (!old_die);
1174450397Sobrien    }
1174590075Sobrien
11746169689Skan  /* Now that the C++ front end lazily declares artificial member fns, we
11747169689Skan     might need to retrofit the declaration into its class.  */
11748169689Skan  if (!declaration && !origin && !old_die
11749169689Skan      && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11750169689Skan      && !class_or_namespace_scope_p (context_die)
11751169689Skan      && debug_info_level > DINFO_LEVEL_TERSE)
11752169689Skan    old_die = force_decl_die (decl);
11753169689Skan
1175490075Sobrien  if (origin != NULL)
1175550397Sobrien    {
11756169689Skan      gcc_assert (!declaration || local_scope_p (context_die));
1175750397Sobrien
1175890075Sobrien      /* Fixup die_parent for the abstract instance of a nested
1175990075Sobrien	 inline function.  */
1176090075Sobrien      if (old_die && old_die->die_parent == NULL)
1176190075Sobrien	add_child_die (context_die, old_die);
1176290075Sobrien
1176390075Sobrien      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
1176490075Sobrien      add_abstract_origin_attribute (subr_die, origin);
1176550397Sobrien    }
1176650397Sobrien  else if (old_die)
1176750397Sobrien    {
11768169689Skan      expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11769169689Skan      struct dwarf_file_data * file_index = lookup_filename (s.file);
1177050397Sobrien
1177190075Sobrien      if (!get_AT_flag (old_die, DW_AT_declaration)
1177290075Sobrien	  /* We can have a normal definition following an inline one in the
1177390075Sobrien	     case of redefinition of GNU C extern inlines.
1177490075Sobrien	     It seems reasonable to use AT_specification in this case.  */
11775132718Skan	  && !get_AT (old_die, DW_AT_inline))
1177652284Sobrien	{
11777169689Skan	  /* Detect and ignore this case, where we are trying to output
11778169689Skan	     something we have already output.  */
11779169689Skan	  return;
1178052284Sobrien	}
1178150397Sobrien
1178250397Sobrien      /* If the definition comes from the same place as the declaration,
1178350397Sobrien	 maybe use the old DIE.  We always want the DIE for this function
1178450397Sobrien	 that has the *_pc attributes to be under comp_unit_die so the
1178590075Sobrien	 debugger can find it.  We also need to do this for abstract
1178690075Sobrien	 instances of inlines, since the spec requires the out-of-line copy
1178790075Sobrien	 to have the same parent.  For local class methods, this doesn't
1178890075Sobrien	 apply; we just use the old DIE.  */
1178990075Sobrien      if ((old_die->die_parent == comp_unit_die || context_die == NULL)
1179090075Sobrien	  && (DECL_ARTIFICIAL (decl)
11791169689Skan	      || (get_AT_file (old_die, DW_AT_decl_file) == file_index
1179290075Sobrien		  && (get_AT_unsigned (old_die, DW_AT_decl_line)
11793169689Skan		      == (unsigned) s.line))))
1179450397Sobrien	{
1179550397Sobrien	  subr_die = old_die;
1179650397Sobrien
11797169689Skan	  /* Clear out the declaration attribute and the formal parameters.
11798169689Skan	     Do not remove all children, because it is possible that this
11799169689Skan	     declaration die was forced using force_decl_die(). In such
11800169689Skan	     cases die that forced declaration die (e.g. TAG_imported_module)
11801169689Skan	     is one of the children that we do not want to remove.  */
1180250397Sobrien	  remove_AT (subr_die, DW_AT_declaration);
11803146895Skan	  remove_child_TAG (subr_die, DW_TAG_formal_parameter);
1180450397Sobrien	}
1180550397Sobrien      else
1180650397Sobrien	{
1180790075Sobrien	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11808132718Skan	  add_AT_specification (subr_die, old_die);
11809169689Skan	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11810169689Skan	    add_AT_file (subr_die, DW_AT_decl_file, file_index);
11811169689Skan	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11812169689Skan	    add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
1181350397Sobrien	}
1181450397Sobrien    }
1181550397Sobrien  else
1181650397Sobrien    {
1181790075Sobrien      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
1181850397Sobrien
1181950397Sobrien      if (TREE_PUBLIC (decl))
1182050397Sobrien	add_AT_flag (subr_die, DW_AT_external, 1);
1182150397Sobrien
1182250397Sobrien      add_name_and_src_coords_attributes (subr_die, decl);
1182350397Sobrien      if (debug_info_level > DINFO_LEVEL_TERSE)
1182450397Sobrien	{
1182590075Sobrien	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
1182690075Sobrien	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
1182790075Sobrien			      0, 0, context_die);
1182850397Sobrien	}
1182950397Sobrien
1183050397Sobrien      add_pure_or_virtual_attribute (subr_die, decl);
1183150397Sobrien      if (DECL_ARTIFICIAL (decl))
1183250397Sobrien	add_AT_flag (subr_die, DW_AT_artificial, 1);
1183390075Sobrien
1183450397Sobrien      if (TREE_PROTECTED (decl))
1183550397Sobrien	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
1183650397Sobrien      else if (TREE_PRIVATE (decl))
1183750397Sobrien	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
1183850397Sobrien    }
1183950397Sobrien
1184050397Sobrien  if (declaration)
1184150397Sobrien    {
11842132718Skan      if (!old_die || !get_AT (old_die, DW_AT_inline))
1184390075Sobrien	{
1184490075Sobrien	  add_AT_flag (subr_die, DW_AT_declaration, 1);
1184550397Sobrien
1184690075Sobrien	  /* The first time we see a member function, it is in the context of
1184790075Sobrien	     the class to which it belongs.  We make sure of this by emitting
1184890075Sobrien	     the class first.  The next time is the definition, which is
11849169689Skan	     handled above.  The two may come from the same source text.
11850169689Skan
11851169689Skan	     Note that force_decl_die() forces function declaration die. It is
11852169689Skan	     later reused to represent definition.  */
11853169689Skan	  equate_decl_number_to_die (decl, subr_die);
1185490075Sobrien	}
1185550397Sobrien    }
1185650397Sobrien  else if (DECL_ABSTRACT (decl))
1185750397Sobrien    {
11858132718Skan      if (DECL_DECLARED_INLINE_P (decl))
1185950397Sobrien	{
11860132718Skan          if (cgraph_function_possibly_inlined_p (decl))
1186150397Sobrien	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
1186250397Sobrien	  else
11863132718Skan	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
1186450397Sobrien	}
1186550397Sobrien      else
11866132718Skan	{
11867132718Skan	  if (cgraph_function_possibly_inlined_p (decl))
11868132718Skan            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11869132718Skan	  else
11870132718Skan            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11871132718Skan	}
1187250397Sobrien
1187350397Sobrien      equate_decl_number_to_die (decl, subr_die);
1187450397Sobrien    }
1187550397Sobrien  else if (!DECL_EXTERNAL (decl))
1187650397Sobrien    {
11877169689Skan      HOST_WIDE_INT cfa_fb_offset;
11878169689Skan
11879132718Skan      if (!old_die || !get_AT (old_die, DW_AT_inline))
1188050397Sobrien	equate_decl_number_to_die (decl, subr_die);
1188150397Sobrien
11882169689Skan      if (!flag_reorder_blocks_and_partition)
11883169689Skan	{
11884169689Skan	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11885169689Skan				       current_function_funcdef_no);
11886169689Skan	  add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11887169689Skan	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11888169689Skan				       current_function_funcdef_no);
11889169689Skan	  add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11890169689Skan
11891169689Skan	  add_pubname (decl, subr_die);
11892169689Skan	  add_arange (decl, subr_die);
11893169689Skan	}
11894169689Skan      else
11895169689Skan	{  /* Do nothing for now; maybe need to duplicate die, one for
11896169689Skan	      hot section and ond for cold section, then use the hot/cold
11897169689Skan	      section begin/end labels to generate the aranges...  */
11898169689Skan	  /*
11899169689Skan	    add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11900169689Skan	    add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11901169689Skan	    add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11902169689Skan	    add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
1190350397Sobrien
11904169689Skan	    add_pubname (decl, subr_die);
11905169689Skan	    add_arange (decl, subr_die);
11906169689Skan	    add_arange (decl, subr_die);
11907169689Skan	   */
11908169689Skan	}
1190950397Sobrien
1191050397Sobrien#ifdef MIPS_DEBUGGING_INFO
1191150397Sobrien      /* Add a reference to the FDE for this routine.  */
1191250397Sobrien      add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
1191350397Sobrien#endif
1191450397Sobrien
11915169689Skan      cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
1191650397Sobrien
11917169689Skan      /* We define the "frame base" as the function's CFA.  This is more
11918169689Skan	 convenient for several reasons: (1) It's stable across the prologue
11919169689Skan	 and epilogue, which makes it better than just a frame pointer,
11920169689Skan	 (2) With dwarf3, there exists a one-byte encoding that allows us
11921169689Skan	 to reference the .debug_frame data by proxy, but failing that,
11922169689Skan	 (3) We can at least reuse the code inspection and interpretation
11923169689Skan	 code that determines the CFA position at various points in the
11924169689Skan	 function.  */
11925169689Skan      /* ??? Use some command-line or configury switch to enable the use
11926169689Skan	 of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11927169689Skan	 consumers that understand it; fall back to "pure" dwarf2 and
11928169689Skan	 convert the CFA data into a location list.  */
11929169689Skan      {
11930169689Skan	dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11931169689Skan	if (list->dw_loc_next)
11932169689Skan	  add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11933169689Skan	else
11934169689Skan	  add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11935169689Skan      }
11936169689Skan
11937169689Skan      /* Compute a displacement from the "steady-state frame pointer" to
11938169689Skan	 the CFA.  The former is what all stack slots and argument slots
11939169689Skan	 will reference in the rtl; the later is what we've told the
11940169689Skan	 debugger about.  We'll need to adjust all frame_base references
11941169689Skan	 by this displacement.  */
11942169689Skan      compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11943169689Skan
11944169689Skan      if (cfun->static_chain_decl)
1194550397Sobrien	add_AT_location_description (subr_die, DW_AT_static_link,
11946169689Skan		 loc_descriptor_from_tree (cfun->static_chain_decl));
1194750397Sobrien    }
1194850397Sobrien
1194950397Sobrien  /* Now output descriptions of the arguments for this function. This gets
1195090075Sobrien     (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
1195150397Sobrien     for a FUNCTION_DECL doesn't indicate cases where there was a trailing
1195250397Sobrien     `...' at the end of the formal parameter list.  In order to find out if
1195350397Sobrien     there was a trailing ellipsis or not, we must instead look at the type
1195450397Sobrien     associated with the FUNCTION_DECL.  This will be a node of type
1195550397Sobrien     FUNCTION_TYPE. If the chain of type nodes hanging off of this
1195690075Sobrien     FUNCTION_TYPE node ends with a void_type_node then there should *not* be
1195750397Sobrien     an ellipsis at the end.  */
1195850397Sobrien
1195950397Sobrien  /* In the case where we are describing a mere function declaration, all we
1196090075Sobrien     need to do here (and all we *can* do here) is to describe the *types* of
1196150397Sobrien     its formal parameters.  */
1196250397Sobrien  if (debug_info_level <= DINFO_LEVEL_TERSE)
1196350397Sobrien    ;
1196450397Sobrien  else if (declaration)
1196590075Sobrien    gen_formal_types_die (decl, subr_die);
1196650397Sobrien  else
1196750397Sobrien    {
11968132718Skan      /* Generate DIEs to represent all known formal parameters.  */
1196990075Sobrien      tree arg_decls = DECL_ARGUMENTS (decl);
1197090075Sobrien      tree parm;
1197150397Sobrien
1197250397Sobrien      /* When generating DIEs, generate the unspecified_parameters DIE
11973132718Skan	 instead if we come across the arg "__builtin_va_alist" */
1197450397Sobrien      for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
1197550397Sobrien	if (TREE_CODE (parm) == PARM_DECL)
1197650397Sobrien	  {
1197750397Sobrien	    if (DECL_NAME (parm)
1197850397Sobrien		&& !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
1197950397Sobrien			    "__builtin_va_alist"))
1198050397Sobrien	      gen_unspecified_parameters_die (parm, subr_die);
1198150397Sobrien	    else
1198250397Sobrien	      gen_decl_die (parm, subr_die);
1198350397Sobrien	  }
1198450397Sobrien
1198590075Sobrien      /* Decide whether we need an unspecified_parameters DIE at the end.
11986132718Skan	 There are 2 more cases to do this for: 1) the ansi ... declaration -
11987132718Skan	 this is detectable when the end of the arg list is not a
11988132718Skan	 void_type_node 2) an unprototyped function declaration (not a
11989132718Skan	 definition).  This just means that we have no info about the
11990132718Skan	 parameters at all.  */
1199150397Sobrien      fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1199250397Sobrien      if (fn_arg_types != NULL)
1199350397Sobrien	{
11994132718Skan	  /* This is the prototyped case, check for....  */
1199550397Sobrien	  if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
1199650397Sobrien	    gen_unspecified_parameters_die (decl, subr_die);
1199750397Sobrien	}
1199850397Sobrien      else if (DECL_INITIAL (decl) == NULL_TREE)
1199950397Sobrien	gen_unspecified_parameters_die (decl, subr_die);
1200050397Sobrien    }
1200150397Sobrien
1200250397Sobrien  /* Output Dwarf info for all of the stuff within the body of the function
1200350397Sobrien     (if it has one - it may be just a declaration).  */
1200450397Sobrien  outer_scope = DECL_INITIAL (decl);
1200550397Sobrien
1200690075Sobrien  /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
1200790075Sobrien     a function.  This BLOCK actually represents the outermost binding contour
1200890075Sobrien     for the function, i.e. the contour in which the function's formal
1200990075Sobrien     parameters and labels get declared. Curiously, it appears that the front
1201090075Sobrien     end doesn't actually put the PARM_DECL nodes for the current function onto
1201190075Sobrien     the BLOCK_VARS list for this outer scope, but are strung off of the
1201290075Sobrien     DECL_ARGUMENTS list for the function instead.
1201390075Sobrien
1201490075Sobrien     The BLOCK_VARS list for the `outer_scope' does provide us with a list of
1201590075Sobrien     the LABEL_DECL nodes for the function however, and we output DWARF info
1201690075Sobrien     for those in decls_for_scope.  Just within the `outer_scope' there will be
1201790075Sobrien     a BLOCK node representing the function's outermost pair of curly braces,
1201890075Sobrien     and any blocks used for the base and member initializers of a C++
1201950397Sobrien     constructor function.  */
1202050397Sobrien  if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
1202150397Sobrien    {
12022169689Skan      /* Emit a DW_TAG_variable DIE for a named return value.  */
12023169689Skan      if (DECL_NAME (DECL_RESULT (decl)))
12024169689Skan	gen_decl_die (DECL_RESULT (decl), subr_die);
12025169689Skan
1202650397Sobrien      current_function_has_inlines = 0;
1202750397Sobrien      decls_for_scope (outer_scope, subr_die, 0);
1202850397Sobrien
1202950397Sobrien#if 0 && defined (MIPS_DEBUGGING_INFO)
1203050397Sobrien      if (current_function_has_inlines)
1203150397Sobrien	{
1203250397Sobrien	  add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
1203350397Sobrien	  if (! comp_unit_has_inlines)
1203450397Sobrien	    {
1203550397Sobrien	      add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
1203650397Sobrien	      comp_unit_has_inlines = 1;
1203750397Sobrien	    }
1203850397Sobrien	}
1203950397Sobrien#endif
1204050397Sobrien    }
12041169689Skan  /* Add the calling convention attribute if requested.  */
12042169689Skan  add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12043169689Skan
1204450397Sobrien}
1204550397Sobrien
1204650397Sobrien/* Generate a DIE to represent a declared data object.  */
1204750397Sobrien
1204850397Sobrienstatic void
12049132718Skangen_variable_die (tree decl, dw_die_ref context_die)
1205050397Sobrien{
1205190075Sobrien  tree origin = decl_ultimate_origin (decl);
1205290075Sobrien  dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
1205350397Sobrien
1205450397Sobrien  dw_die_ref old_die = lookup_decl_die (decl);
1205590075Sobrien  int declaration = (DECL_EXTERNAL (decl)
12056169689Skan		     /* If DECL is COMDAT and has not actually been
12057169689Skan			emitted, we cannot take its address; there
12058169689Skan			might end up being no definition anywhere in
12059169689Skan			the program.  For example, consider the C++
12060169689Skan			test case:
12061169689Skan
12062169689Skan                          template <class T>
12063169689Skan                          struct S { static const int i = 7; };
12064169689Skan
12065169689Skan                          template <class T>
12066169689Skan                          const int S<T>::i;
12067169689Skan
12068169689Skan                          int f() { return S<int>::i; }
12069169689Skan
12070169689Skan			Here, S<int>::i is not DECL_EXTERNAL, but no
12071169689Skan			definition is required, so the compiler will
12072169689Skan			not emit a definition.  */
12073169689Skan		     || (TREE_CODE (decl) == VAR_DECL
12074169689Skan			 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12075132718Skan		     || class_or_namespace_scope_p (context_die));
1207650397Sobrien
1207750397Sobrien  if (origin != NULL)
1207850397Sobrien    add_abstract_origin_attribute (var_die, origin);
1207990075Sobrien
1208050397Sobrien  /* Loop unrolling can create multiple blocks that refer to the same
1208190075Sobrien     static variable, so we must test for the DW_AT_declaration flag.
1208290075Sobrien
1208390075Sobrien     ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
1208450397Sobrien     copy decls and set the DECL_ABSTRACT flag on them instead of
1208590075Sobrien     sharing them.
1208690075Sobrien
12087169689Skan     ??? Duplicated blocks have been rewritten to use .debug_ranges.
12088169689Skan
12089169689Skan     ??? The declare_in_namespace support causes us to get two DIEs for one
12090169689Skan     variable, both of which are declarations.  We want to avoid considering
12091169689Skan     one to be a specification, so we must test that this DIE is not a
12092169689Skan     declaration.  */
12093169689Skan  else if (old_die && TREE_STATIC (decl) && ! declaration
12094117395Skan	   && get_AT_flag (old_die, DW_AT_declaration) == 1)
1209550397Sobrien    {
1209690075Sobrien      /* This is a definition of a C++ class level static.  */
12097132718Skan      add_AT_specification (var_die, old_die);
1209850397Sobrien      if (DECL_NAME (decl))
1209950397Sobrien	{
12100169689Skan	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12101169689Skan	  struct dwarf_file_data * file_index = lookup_filename (s.file);
1210250397Sobrien
12103169689Skan	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12104169689Skan	    add_AT_file (var_die, DW_AT_decl_file, file_index);
1210550397Sobrien
12106169689Skan	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
1210750397Sobrien
12108169689Skan	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
1210950397Sobrien	}
1211050397Sobrien    }
1211150397Sobrien  else
1211250397Sobrien    {
1211350397Sobrien      add_name_and_src_coords_attributes (var_die, decl);
1211490075Sobrien      add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
1211550397Sobrien			  TREE_THIS_VOLATILE (decl), context_die);
1211650397Sobrien
1211750397Sobrien      if (TREE_PUBLIC (decl))
1211850397Sobrien	add_AT_flag (var_die, DW_AT_external, 1);
1211950397Sobrien
1212050397Sobrien      if (DECL_ARTIFICIAL (decl))
1212150397Sobrien	add_AT_flag (var_die, DW_AT_artificial, 1);
1212250397Sobrien
1212350397Sobrien      if (TREE_PROTECTED (decl))
1212450397Sobrien	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
1212550397Sobrien      else if (TREE_PRIVATE (decl))
1212650397Sobrien	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
1212750397Sobrien    }
1212850397Sobrien
1212950397Sobrien  if (declaration)
1213050397Sobrien    add_AT_flag (var_die, DW_AT_declaration, 1);
1213190075Sobrien
12132169689Skan  if (DECL_ABSTRACT (decl) || declaration)
1213350397Sobrien    equate_decl_number_to_die (decl, var_die);
1213450397Sobrien
1213550397Sobrien  if (! declaration && ! DECL_ABSTRACT (decl))
1213650397Sobrien    {
12137169689Skan      add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
1213850397Sobrien      add_pubname (decl, var_die);
1213950397Sobrien    }
1214090075Sobrien  else
1214190075Sobrien    tree_add_const_value_attribute (var_die, decl);
1214250397Sobrien}
1214350397Sobrien
1214450397Sobrien/* Generate a DIE to represent a label identifier.  */
1214550397Sobrien
1214650397Sobrienstatic void
12147132718Skangen_label_die (tree decl, dw_die_ref context_die)
1214850397Sobrien{
1214990075Sobrien  tree origin = decl_ultimate_origin (decl);
1215090075Sobrien  dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
1215190075Sobrien  rtx insn;
1215250397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
1215350397Sobrien
1215450397Sobrien  if (origin != NULL)
1215550397Sobrien    add_abstract_origin_attribute (lbl_die, origin);
1215650397Sobrien  else
1215750397Sobrien    add_name_and_src_coords_attributes (lbl_die, decl);
1215850397Sobrien
1215950397Sobrien  if (DECL_ABSTRACT (decl))
1216050397Sobrien    equate_decl_number_to_die (decl, lbl_die);
1216150397Sobrien  else
1216250397Sobrien    {
12163132718Skan      insn = DECL_RTL_IF_SET (decl);
1216452284Sobrien
1216552284Sobrien      /* Deleted labels are programmer specified labels which have been
12166132718Skan	 eliminated because of various optimizations.  We still emit them
1216752284Sobrien	 here so that it is possible to put breakpoints on them.  */
12168132718Skan      if (insn
12169169689Skan	  && (LABEL_P (insn)
12170169689Skan	      || ((NOTE_P (insn)
12171132718Skan	           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
1217250397Sobrien	{
1217390075Sobrien	  /* When optimization is enabled (via -O) some parts of the compiler
1217490075Sobrien	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
1217550397Sobrien	     represent source-level labels which were explicitly declared by
1217650397Sobrien	     the user.  This really shouldn't be happening though, so catch
1217750397Sobrien	     it if it ever does happen.  */
12178169689Skan	  gcc_assert (!INSN_DELETED_P (insn));
1217950397Sobrien
1218090075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
1218150397Sobrien	  add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
1218250397Sobrien	}
1218350397Sobrien    }
1218450397Sobrien}
1218550397Sobrien
12186169689Skan/* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12187169689Skan   attributes to the DIE for a block STMT, to describe where the inlined
12188169689Skan   function was called from.  This is similar to add_src_coords_attributes.  */
1218950397Sobrien
12190169689Skanstatic inline void
12191169689Skanadd_call_src_coords_attributes (tree stmt, dw_die_ref die)
1219250397Sobrien{
12193169689Skan  expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12194169689Skan
12195169689Skan  add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12196169689Skan  add_AT_unsigned (die, DW_AT_call_line, s.line);
12197169689Skan}
12198169689Skan
12199169689Skan/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12200169689Skan   Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12201169689Skan
12202169689Skanstatic inline void
12203169689Skanadd_high_low_attributes (tree stmt, dw_die_ref die)
12204169689Skan{
1220550397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
1220650397Sobrien
12207169689Skan  if (BLOCK_FRAGMENT_CHAIN (stmt))
1220850397Sobrien    {
12209169689Skan      tree chain;
1221090075Sobrien
12211169689Skan      add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
1221290075Sobrien
12213169689Skan      chain = BLOCK_FRAGMENT_CHAIN (stmt);
12214169689Skan      do
1221590075Sobrien	{
12216169689Skan	  add_ranges (chain);
12217169689Skan	  chain = BLOCK_FRAGMENT_CHAIN (chain);
1221890075Sobrien	}
12219169689Skan      while (chain);
12220169689Skan      add_ranges (NULL);
1222150397Sobrien    }
12222169689Skan  else
12223169689Skan    {
12224169689Skan      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12225169689Skan				   BLOCK_NUMBER (stmt));
12226169689Skan      add_AT_lbl_id (die, DW_AT_low_pc, label);
12227169689Skan      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12228169689Skan				   BLOCK_NUMBER (stmt));
12229169689Skan      add_AT_lbl_id (die, DW_AT_high_pc, label);
12230169689Skan    }
12231169689Skan}
1223250397Sobrien
12233169689Skan/* Generate a DIE for a lexical block.  */
12234169689Skan
12235169689Skanstatic void
12236169689Skangen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12237169689Skan{
12238169689Skan  dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12239169689Skan
12240169689Skan  if (! BLOCK_ABSTRACT (stmt))
12241169689Skan    add_high_low_attributes (stmt, stmt_die);
12242169689Skan
1224350397Sobrien  decls_for_scope (stmt, stmt_die, depth);
1224450397Sobrien}
1224550397Sobrien
1224650397Sobrien/* Generate a DIE for an inlined subprogram.  */
1224750397Sobrien
1224850397Sobrienstatic void
12249132718Skangen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
1225050397Sobrien{
12251122180Skan  tree decl = block_ultimate_origin (stmt);
12252122180Skan
12253122180Skan  /* Emit info for the abstract instance first, if we haven't yet.  We
12254122180Skan     must emit this even if the block is abstract, otherwise when we
12255122180Skan     emit the block below (or elsewhere), we may end up trying to emit
12256122180Skan     a die whose origin die hasn't been emitted, and crashing.  */
12257122180Skan  dwarf2out_abstract_function (decl);
12258122180Skan
1225950397Sobrien  if (! BLOCK_ABSTRACT (stmt))
1226050397Sobrien    {
1226190075Sobrien      dw_die_ref subr_die
1226290075Sobrien	= new_die (DW_TAG_inlined_subroutine, context_die, stmt);
1226350397Sobrien
1226450397Sobrien      add_abstract_origin_attribute (subr_die, decl);
12265169689Skan      add_high_low_attributes (stmt, subr_die);
12266169689Skan      add_call_src_coords_attributes (stmt, subr_die);
12267169689Skan
1226850397Sobrien      decls_for_scope (stmt, subr_die, depth);
1226950397Sobrien      current_function_has_inlines = 1;
1227050397Sobrien    }
1227196263Sobrien  else
1227296263Sobrien    /* We may get here if we're the outer block of function A that was
1227396263Sobrien       inlined into function B that was inlined into function C.  When
1227496263Sobrien       generating debugging info for C, dwarf2out_abstract_function(B)
1227596263Sobrien       would mark all inlined blocks as abstract, including this one.
1227696263Sobrien       So, we wouldn't (and shouldn't) expect labels to be generated
1227796263Sobrien       for this one.  Instead, just emit debugging info for
1227896263Sobrien       declarations within the block.  This is particularly important
1227996263Sobrien       in the case of initializers of arguments passed from B to us:
1228096263Sobrien       if they're statement expressions containing declarations, we
1228196263Sobrien       wouldn't generate dies for their abstract variables, and then,
1228296263Sobrien       when generating dies for the real variables, we'd die (pun
1228396263Sobrien       intended :-)  */
1228496263Sobrien    gen_lexical_block_die (stmt, context_die, depth);
1228550397Sobrien}
1228650397Sobrien
1228750397Sobrien/* Generate a DIE for a field in a record, or structure.  */
1228850397Sobrien
1228950397Sobrienstatic void
12290132718Skangen_field_die (tree decl, dw_die_ref context_die)
1229150397Sobrien{
12292117395Skan  dw_die_ref decl_die;
1229350397Sobrien
12294117395Skan  if (TREE_TYPE (decl) == error_mark_node)
12295117395Skan    return;
12296132718Skan
12297117395Skan  decl_die = new_die (DW_TAG_member, context_die, decl);
1229850397Sobrien  add_name_and_src_coords_attributes (decl_die, decl);
1229950397Sobrien  add_type_attribute (decl_die, member_declared_type (decl),
1230050397Sobrien		      TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
1230150397Sobrien		      context_die);
1230250397Sobrien
1230350397Sobrien  if (DECL_BIT_FIELD_TYPE (decl))
1230450397Sobrien    {
1230550397Sobrien      add_byte_size_attribute (decl_die, decl);
1230650397Sobrien      add_bit_size_attribute (decl_die, decl);
1230750397Sobrien      add_bit_offset_attribute (decl_die, decl);
1230850397Sobrien    }
1230950397Sobrien
1231050397Sobrien  if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
1231150397Sobrien    add_data_member_location_attribute (decl_die, decl);
1231250397Sobrien
1231350397Sobrien  if (DECL_ARTIFICIAL (decl))
1231450397Sobrien    add_AT_flag (decl_die, DW_AT_artificial, 1);
1231550397Sobrien
1231650397Sobrien  if (TREE_PROTECTED (decl))
1231750397Sobrien    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
1231850397Sobrien  else if (TREE_PRIVATE (decl))
1231950397Sobrien    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12320169689Skan
12321169689Skan  /* Equate decl number to die, so that we can look up this decl later on.  */
12322169689Skan  equate_decl_number_to_die (decl, decl_die);
1232350397Sobrien}
1232450397Sobrien
1232550397Sobrien#if 0
1232650397Sobrien/* Don't generate either pointer_type DIEs or reference_type DIEs here.
1232750397Sobrien   Use modified_type_die instead.
1232850397Sobrien   We keep this code here just in case these types of DIEs may be needed to
1232950397Sobrien   represent certain things in other languages (e.g. Pascal) someday.  */
1233090075Sobrien
1233150397Sobrienstatic void
12332132718Skangen_pointer_type_die (tree type, dw_die_ref context_die)
1233350397Sobrien{
1233490075Sobrien  dw_die_ref ptr_die
1233590075Sobrien    = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
1233650397Sobrien
1233750397Sobrien  equate_type_number_to_die (type, ptr_die);
1233850397Sobrien  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
1233950397Sobrien  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
1234050397Sobrien}
1234150397Sobrien
1234250397Sobrien/* Don't generate either pointer_type DIEs or reference_type DIEs here.
1234350397Sobrien   Use modified_type_die instead.
1234450397Sobrien   We keep this code here just in case these types of DIEs may be needed to
1234550397Sobrien   represent certain things in other languages (e.g. Pascal) someday.  */
1234690075Sobrien
1234750397Sobrienstatic void
12348132718Skangen_reference_type_die (tree type, dw_die_ref context_die)
1234950397Sobrien{
1235090075Sobrien  dw_die_ref ref_die
1235190075Sobrien    = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
1235250397Sobrien
1235350397Sobrien  equate_type_number_to_die (type, ref_die);
1235450397Sobrien  add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
1235550397Sobrien  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
1235650397Sobrien}
1235750397Sobrien#endif
1235850397Sobrien
1235950397Sobrien/* Generate a DIE for a pointer to a member type.  */
1236090075Sobrien
1236150397Sobrienstatic void
12362132718Skangen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
1236350397Sobrien{
1236490075Sobrien  dw_die_ref ptr_die
1236590075Sobrien    = new_die (DW_TAG_ptr_to_member_type,
1236690075Sobrien	       scope_die_for (type, context_die), type);
1236750397Sobrien
1236850397Sobrien  equate_type_number_to_die (type, ptr_die);
1236950397Sobrien  add_AT_die_ref (ptr_die, DW_AT_containing_type,
1237050397Sobrien		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
1237150397Sobrien  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
1237250397Sobrien}
1237350397Sobrien
1237450397Sobrien/* Generate the DIE for the compilation unit.  */
1237550397Sobrien
1237690075Sobrienstatic dw_die_ref
12377132718Skangen_compile_unit_die (const char *filename)
1237850397Sobrien{
1237990075Sobrien  dw_die_ref die;
1238050397Sobrien  char producer[250];
1238190075Sobrien  const char *language_string = lang_hooks.name;
1238290075Sobrien  int language;
1238350397Sobrien
1238490075Sobrien  die = new_die (DW_TAG_compile_unit, NULL, NULL);
1238550397Sobrien
12386132718Skan  if (filename)
12387132718Skan    {
12388132718Skan      add_name_attribute (die, filename);
12389132718Skan      /* Don't add cwd for <built-in>.  */
12390132718Skan      if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12391132718Skan	add_comp_dir_attribute (die);
12392132718Skan    }
1239350397Sobrien
1239450397Sobrien  sprintf (producer, "%s %s", language_string, version_string);
1239550397Sobrien
1239650397Sobrien#ifdef MIPS_DEBUGGING_INFO
1239750397Sobrien  /* The MIPS/SGI compilers place the 'cc' command line options in the producer
1239850397Sobrien     string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
1239950397Sobrien     not appear in the producer string, the debugger reaches the conclusion
1240050397Sobrien     that the object file is stripped and has no debugging information.
1240150397Sobrien     To get the MIPS/SGI debugger to believe that there is debugging
1240250397Sobrien     information in the object file, we add a -g to the producer string.  */
1240350397Sobrien  if (debug_info_level > DINFO_LEVEL_TERSE)
1240450397Sobrien    strcat (producer, " -g");
1240550397Sobrien#endif
1240650397Sobrien
1240790075Sobrien  add_AT_string (die, DW_AT_producer, producer);
1240850397Sobrien
1240950397Sobrien  if (strcmp (language_string, "GNU C++") == 0)
1241090075Sobrien    language = DW_LANG_C_plus_plus;
1241150397Sobrien  else if (strcmp (language_string, "GNU Ada") == 0)
12412132718Skan    language = DW_LANG_Ada95;
1241350397Sobrien  else if (strcmp (language_string, "GNU F77") == 0)
1241490075Sobrien    language = DW_LANG_Fortran77;
12415169689Skan  else if (strcmp (language_string, "GNU F95") == 0)
12416169689Skan    language = DW_LANG_Fortran95;
1241750397Sobrien  else if (strcmp (language_string, "GNU Pascal") == 0)
1241890075Sobrien    language = DW_LANG_Pascal83;
1241990075Sobrien  else if (strcmp (language_string, "GNU Java") == 0)
1242090075Sobrien    language = DW_LANG_Java;
12421169689Skan  else if (strcmp (language_string, "GNU Objective-C") == 0)
12422169689Skan    language = DW_LANG_ObjC;
12423169689Skan  else if (strcmp (language_string, "GNU Objective-C++") == 0)
12424169689Skan    language = DW_LANG_ObjC_plus_plus;
1242550397Sobrien  else
1242690075Sobrien    language = DW_LANG_C89;
1242750397Sobrien
1242890075Sobrien  add_AT_unsigned (die, DW_AT_language, language);
1242990075Sobrien  return die;
1243050397Sobrien}
1243150397Sobrien
1243250397Sobrien/* Generate the DIE for a base class.  */
1243350397Sobrien
1243450397Sobrienstatic void
12435132718Skangen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
1243650397Sobrien{
1243790075Sobrien  dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
1243850397Sobrien
1243950397Sobrien  add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
1244050397Sobrien  add_data_member_location_attribute (die, binfo);
1244150397Sobrien
12442169689Skan  if (BINFO_VIRTUAL_P (binfo))
1244350397Sobrien    add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
1244490075Sobrien
12445132718Skan  if (access == access_public_node)
1244650397Sobrien    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12447132718Skan  else if (access == access_protected_node)
1244850397Sobrien    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
1244950397Sobrien}
1245050397Sobrien
1245150397Sobrien/* Generate a DIE for a class member.  */
1245250397Sobrien
1245350397Sobrienstatic void
12454132718Skangen_member_die (tree type, dw_die_ref context_die)
1245550397Sobrien{
1245690075Sobrien  tree member;
12457132718Skan  tree binfo = TYPE_BINFO (type);
1245890075Sobrien  dw_die_ref child;
1245950397Sobrien
1246050397Sobrien  /* If this is not an incomplete type, output descriptions of each of its
1246150397Sobrien     members. Note that as we output the DIEs necessary to represent the
1246250397Sobrien     members of this record or union type, we will also be trying to output
1246350397Sobrien     DIEs to represent the *types* of those members. However the `type'
1246490075Sobrien     function (above) will specifically avoid generating type DIEs for member
1246590075Sobrien     types *within* the list of member DIEs for this (containing) type except
1246650397Sobrien     for those types (of members) which are explicitly marked as also being
1246750397Sobrien     members of this (containing) type themselves.  The g++ front- end can
1246890075Sobrien     force any given type to be treated as a member of some other (containing)
1246990075Sobrien     type by setting the TYPE_CONTEXT of the given (member) type to point to
1247090075Sobrien     the TREE node representing the appropriate (containing) type.  */
1247150397Sobrien
1247250397Sobrien  /* First output info about the base classes.  */
12473169689Skan  if (binfo)
1247450397Sobrien    {
12475169689Skan      VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
1247690075Sobrien      int i;
12477169689Skan      tree base;
1247850397Sobrien
12479169689Skan      for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12480169689Skan	gen_inheritance_die (base,
12481169689Skan			     (accesses ? VEC_index (tree, accesses, i)
12482132718Skan			      : access_public_node), context_die);
1248350397Sobrien    }
1248450397Sobrien
1248550397Sobrien  /* Now output info about the data members and type members.  */
1248650397Sobrien  for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
1248790075Sobrien    {
1248890075Sobrien      /* If we thought we were generating minimal debug info for TYPE
1248990075Sobrien	 and then changed our minds, some of the member declarations
1249090075Sobrien	 may have already been defined.  Don't define them again, but
1249190075Sobrien	 do put them in the right order.  */
1249250397Sobrien
1249390075Sobrien      child = lookup_decl_die (member);
1249490075Sobrien      if (child)
1249590075Sobrien	splice_child_die (context_die, child);
1249690075Sobrien      else
1249790075Sobrien	gen_decl_die (member, context_die);
1249890075Sobrien    }
1249990075Sobrien
1250050397Sobrien  /* Now output info about the function members (if any).  */
1250150397Sobrien  for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
1250290075Sobrien    {
1250390075Sobrien      /* Don't include clones in the member list.  */
1250490075Sobrien      if (DECL_ABSTRACT_ORIGIN (member))
1250590075Sobrien	continue;
1250690075Sobrien
1250790075Sobrien      child = lookup_decl_die (member);
1250890075Sobrien      if (child)
1250990075Sobrien	splice_child_die (context_die, child);
1251090075Sobrien      else
1251190075Sobrien	gen_decl_die (member, context_die);
1251290075Sobrien    }
1251350397Sobrien}
1251450397Sobrien
1251590075Sobrien/* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
1251690075Sobrien   is set, we pretend that the type was never defined, so we only get the
1251790075Sobrien   member DIEs needed by later specification DIEs.  */
1251850397Sobrien
1251950397Sobrienstatic void
12520259269Spfggen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12521259269Spfg				enum debug_info_usage usage)
1252250397Sobrien{
1252390075Sobrien  dw_die_ref type_die = lookup_type_die (type);
1252490075Sobrien  dw_die_ref scope_die = 0;
1252590075Sobrien  int nested = 0;
1252690075Sobrien  int complete = (TYPE_SIZE (type)
1252790075Sobrien		  && (! TYPE_STUB_DECL (type)
1252890075Sobrien		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12529132718Skan  int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12530259269Spfg  complete = complete && should_emit_struct_debug (type, usage);
1253150397Sobrien
1253290075Sobrien  if (type_die && ! complete)
1253350397Sobrien    return;
1253450397Sobrien
1253550397Sobrien  if (TYPE_CONTEXT (type) != NULL_TREE
12536132718Skan      && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12537132718Skan	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
1253850397Sobrien    nested = 1;
1253950397Sobrien
1254050397Sobrien  scope_die = scope_die_for (type, context_die);
1254150397Sobrien
1254250397Sobrien  if (! type_die || (nested && scope_die == comp_unit_die))
1254350397Sobrien    /* First occurrence of type or toplevel definition of nested class.  */
1254450397Sobrien    {
1254590075Sobrien      dw_die_ref old_die = type_die;
1254650397Sobrien
1254750397Sobrien      type_die = new_die (TREE_CODE (type) == RECORD_TYPE
1254850397Sobrien			  ? DW_TAG_structure_type : DW_TAG_union_type,
1254990075Sobrien			  scope_die, type);
1255050397Sobrien      equate_type_number_to_die (type, type_die);
1255150397Sobrien      if (old_die)
12552132718Skan	add_AT_specification (type_die, old_die);
1255390075Sobrien      else
1255490075Sobrien	add_name_attribute (type_die, type_tag (type));
1255550397Sobrien    }
1255650397Sobrien  else
1255750397Sobrien    remove_AT (type_die, DW_AT_declaration);
1255850397Sobrien
1255950397Sobrien  /* If this type has been completed, then give it a byte_size attribute and
1256050397Sobrien     then give a list of members.  */
12561132718Skan  if (complete && !ns_decl)
1256250397Sobrien    {
1256390075Sobrien      /* Prevent infinite recursion in cases where the type of some member of
12564132718Skan	 this type is expressed in terms of this type itself.  */
1256550397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1256650397Sobrien      add_byte_size_attribute (type_die, type);
1256750397Sobrien      if (TYPE_STUB_DECL (type) != NULL_TREE)
1256850397Sobrien	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
1256950397Sobrien
1257050397Sobrien      /* If the first reference to this type was as the return type of an
1257150397Sobrien	 inline function, then it may not have a parent.  Fix this now.  */
1257250397Sobrien      if (type_die->die_parent == NULL)
1257350397Sobrien	add_child_die (scope_die, type_die);
1257450397Sobrien
1257550397Sobrien      push_decl_scope (type);
1257650397Sobrien      gen_member_die (type, type_die);
1257750397Sobrien      pop_decl_scope ();
1257850397Sobrien
1257950397Sobrien      /* GNU extension: Record what type our vtable lives in.  */
1258050397Sobrien      if (TYPE_VFIELD (type))
1258150397Sobrien	{
1258250397Sobrien	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
1258350397Sobrien
1258450397Sobrien	  gen_type_die (vtype, context_die);
1258550397Sobrien	  add_AT_die_ref (type_die, DW_AT_containing_type,
1258650397Sobrien			  lookup_type_die (vtype));
1258750397Sobrien	}
1258850397Sobrien    }
1258950397Sobrien  else
1259052284Sobrien    {
1259152284Sobrien      add_AT_flag (type_die, DW_AT_declaration, 1);
1259252284Sobrien
1259390075Sobrien      /* We don't need to do this for function-local types.  */
1259490075Sobrien      if (TYPE_STUB_DECL (type)
1259590075Sobrien	  && ! decl_function_context (TYPE_STUB_DECL (type)))
12596169689Skan	VEC_safe_push (tree, gc, incomplete_types, type);
1259752284Sobrien    }
12598260396Spfg
12599260396Spfg  if (get_AT (type_die, DW_AT_name))
12600260396Spfg    add_pubtype (type, type_die);
1260150397Sobrien}
1260250397Sobrien
1260350397Sobrien/* Generate a DIE for a subroutine _type_.  */
1260450397Sobrien
1260550397Sobrienstatic void
12606132718Skangen_subroutine_type_die (tree type, dw_die_ref context_die)
1260750397Sobrien{
1260890075Sobrien  tree return_type = TREE_TYPE (type);
1260990075Sobrien  dw_die_ref subr_die
1261090075Sobrien    = new_die (DW_TAG_subroutine_type,
1261190075Sobrien	       scope_die_for (type, context_die), type);
1261250397Sobrien
1261350397Sobrien  equate_type_number_to_die (type, subr_die);
1261450397Sobrien  add_prototyped_attribute (subr_die, type);
1261550397Sobrien  add_type_attribute (subr_die, return_type, 0, 0, context_die);
1261650397Sobrien  gen_formal_types_die (type, subr_die);
12617260396Spfg
12618260396Spfg  if (get_AT (subr_die, DW_AT_name))
12619260396Spfg    add_pubtype (type, subr_die);
1262050397Sobrien}
1262150397Sobrien
12622132718Skan/* Generate a DIE for a type definition.  */
1262350397Sobrien
1262450397Sobrienstatic void
12625132718Skangen_typedef_die (tree decl, dw_die_ref context_die)
1262650397Sobrien{
1262790075Sobrien  dw_die_ref type_die;
1262890075Sobrien  tree origin;
1262950397Sobrien
1263050397Sobrien  if (TREE_ASM_WRITTEN (decl))
1263150397Sobrien    return;
1263290075Sobrien
1263350397Sobrien  TREE_ASM_WRITTEN (decl) = 1;
1263490075Sobrien  type_die = new_die (DW_TAG_typedef, context_die, decl);
1263550397Sobrien  origin = decl_ultimate_origin (decl);
1263650397Sobrien  if (origin != NULL)
1263750397Sobrien    add_abstract_origin_attribute (type_die, origin);
1263850397Sobrien  else
1263950397Sobrien    {
1264090075Sobrien      tree type;
1264190075Sobrien
1264250397Sobrien      add_name_and_src_coords_attributes (type_die, decl);
1264350397Sobrien      if (DECL_ORIGINAL_TYPE (decl))
1264450397Sobrien	{
1264550397Sobrien	  type = DECL_ORIGINAL_TYPE (decl);
1264690075Sobrien
12647169689Skan	  gcc_assert (type != TREE_TYPE (decl));
12648169689Skan	  equate_type_number_to_die (TREE_TYPE (decl), type_die);
1264950397Sobrien	}
1265050397Sobrien      else
1265150397Sobrien	type = TREE_TYPE (decl);
1265290075Sobrien
1265350397Sobrien      add_type_attribute (type_die, type, TREE_READONLY (decl),
1265450397Sobrien			  TREE_THIS_VOLATILE (decl), context_die);
1265550397Sobrien    }
1265650397Sobrien
1265750397Sobrien  if (DECL_ABSTRACT (decl))
1265850397Sobrien    equate_decl_number_to_die (decl, type_die);
12659260396Spfg
12660260396Spfg  if (get_AT (type_die, DW_AT_name))
12661260396Spfg    add_pubtype (decl, type_die);
1266250397Sobrien}
1266350397Sobrien
1266450397Sobrien/* Generate a type description DIE.  */
1266550397Sobrien
1266650397Sobrienstatic void
12667259269Spfggen_type_die_with_usage (tree type, dw_die_ref context_die,
12668259269Spfg				enum debug_info_usage usage)
1266950397Sobrien{
1267090075Sobrien  int need_pop;
1267190075Sobrien
1267250397Sobrien  if (type == NULL_TREE || type == error_mark_node)
1267350397Sobrien    return;
1267450397Sobrien
1267550397Sobrien  if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1267650397Sobrien      && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1267790075Sobrien    {
12678117395Skan      if (TREE_ASM_WRITTEN (type))
12679117395Skan	return;
12680117395Skan
12681117395Skan      /* Prevent broken recursion; we can't hand off to the same type.  */
12682169689Skan      gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12683117395Skan
1268450397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1268550397Sobrien      gen_decl_die (TYPE_NAME (type), context_die);
1268650397Sobrien      return;
1268750397Sobrien    }
1268850397Sobrien
12689117395Skan  /* We are going to output a DIE to represent the unqualified version
12690117395Skan     of this type (i.e. without any const or volatile qualifiers) so
12691117395Skan     get the main variant (i.e. the unqualified version) of this type
12692117395Skan     now.  (Vectors are special because the debugging info is in the
12693117395Skan     cloned type itself).  */
12694117395Skan  if (TREE_CODE (type) != VECTOR_TYPE)
12695117395Skan    type = type_main_variant (type);
12696117395Skan
12697117395Skan  if (TREE_ASM_WRITTEN (type))
12698117395Skan    return;
12699117395Skan
1270050397Sobrien  switch (TREE_CODE (type))
1270150397Sobrien    {
1270250397Sobrien    case ERROR_MARK:
1270350397Sobrien      break;
1270450397Sobrien
1270550397Sobrien    case POINTER_TYPE:
1270650397Sobrien    case REFERENCE_TYPE:
1270750397Sobrien      /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
1270850397Sobrien	 ensures that the gen_type_die recursion will terminate even if the
1270950397Sobrien	 type is recursive.  Recursive types are possible in Ada.  */
1271050397Sobrien      /* ??? We could perhaps do this for all types before the switch
1271150397Sobrien	 statement.  */
1271250397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1271350397Sobrien
1271450397Sobrien      /* For these types, all that is required is that we output a DIE (or a
12715132718Skan	 set of DIEs) to represent the "basis" type.  */
12716259269Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12717259269Spfg				DINFO_USAGE_IND_USE);
1271850397Sobrien      break;
1271950397Sobrien
1272050397Sobrien    case OFFSET_TYPE:
1272190075Sobrien      /* This code is used for C++ pointer-to-data-member types.
1272250397Sobrien	 Output a description of the relevant class type.  */
12723259269Spfg      gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
12724259269Spfg					DINFO_USAGE_IND_USE);
1272550397Sobrien
1272650397Sobrien      /* Output a description of the type of the object pointed to.  */
12727259269Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12728259269Spfg					DINFO_USAGE_IND_USE);
1272950397Sobrien
1273050397Sobrien      /* Now output a DIE to represent this pointer-to-data-member type
12731132718Skan	 itself.  */
1273250397Sobrien      gen_ptr_to_mbr_type_die (type, context_die);
1273350397Sobrien      break;
1273450397Sobrien
1273550397Sobrien    case FUNCTION_TYPE:
1273650397Sobrien      /* Force out return type (in case it wasn't forced out already).  */
12737259269Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12738259269Spfg					DINFO_USAGE_DIR_USE);
1273950397Sobrien      gen_subroutine_type_die (type, context_die);
1274050397Sobrien      break;
1274150397Sobrien
1274250397Sobrien    case METHOD_TYPE:
1274350397Sobrien      /* Force out return type (in case it wasn't forced out already).  */
12744259269Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12745259269Spfg					DINFO_USAGE_DIR_USE);
1274650397Sobrien      gen_subroutine_type_die (type, context_die);
1274750397Sobrien      break;
1274850397Sobrien
1274950397Sobrien    case ARRAY_TYPE:
12750169689Skan      gen_array_type_die (type, context_die);
1275150397Sobrien      break;
1275250397Sobrien
1275390075Sobrien    case VECTOR_TYPE:
1275496263Sobrien      gen_array_type_die (type, context_die);
1275590075Sobrien      break;
1275690075Sobrien
1275750397Sobrien    case ENUMERAL_TYPE:
1275850397Sobrien    case RECORD_TYPE:
1275950397Sobrien    case UNION_TYPE:
1276050397Sobrien    case QUAL_UNION_TYPE:
1276190075Sobrien      /* If this is a nested type whose containing class hasn't been written
12762132718Skan	 out yet, writing it out will cover this one, too.  This does not apply
12763132718Skan	 to instantiations of member class templates; they need to be added to
12764132718Skan	 the containing class as they are generated.  FIXME: This hurts the
12765132718Skan	 idea of combining type decls from multiple TUs, since we can't predict
12766132718Skan	 what set of template instantiations we'll get.  */
1276750397Sobrien      if (TYPE_CONTEXT (type)
1276852284Sobrien	  && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
1276950397Sobrien	  && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
1277050397Sobrien	{
12771259269Spfg	  gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
1277250397Sobrien
1277390075Sobrien	  if (TREE_ASM_WRITTEN (type))
1277450397Sobrien	    return;
1277550397Sobrien
1277650397Sobrien	  /* If that failed, attach ourselves to the stub.  */
1277750397Sobrien	  push_decl_scope (TYPE_CONTEXT (type));
1277850397Sobrien	  context_die = lookup_type_die (TYPE_CONTEXT (type));
1277990075Sobrien	  need_pop = 1;
1278050397Sobrien	}
1278190075Sobrien      else
12782132718Skan	{
12783132718Skan	  declare_in_namespace (type, context_die);
12784132718Skan	  need_pop = 0;
12785132718Skan	}
1278650397Sobrien
1278750397Sobrien      if (TREE_CODE (type) == ENUMERAL_TYPE)
12788169689Skan	{
12789169689Skan	  /* This might have been written out by the call to
12790169689Skan	     declare_in_namespace.  */
12791169689Skan	  if (!TREE_ASM_WRITTEN (type))
12792169689Skan	    gen_enumeration_type_die (type, context_die);
12793169689Skan	}
1279450397Sobrien      else
12795259269Spfg	gen_struct_or_union_type_die (type, context_die, usage);
1279650397Sobrien
1279790075Sobrien      if (need_pop)
1279850397Sobrien	pop_decl_scope ();
1279950397Sobrien
1280050397Sobrien      /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
1280150397Sobrien	 it up if it is ever completed.  gen_*_type_die will set it for us
1280250397Sobrien	 when appropriate.  */
1280350397Sobrien      return;
1280450397Sobrien
1280550397Sobrien    case VOID_TYPE:
1280650397Sobrien    case INTEGER_TYPE:
1280750397Sobrien    case REAL_TYPE:
1280850397Sobrien    case COMPLEX_TYPE:
1280950397Sobrien    case BOOLEAN_TYPE:
1281050397Sobrien      /* No DIEs needed for fundamental types.  */
1281150397Sobrien      break;
1281250397Sobrien
1281350397Sobrien    case LANG_TYPE:
1281450397Sobrien      /* No Dwarf representation currently defined.  */
1281550397Sobrien      break;
1281650397Sobrien
1281750397Sobrien    default:
12818169689Skan      gcc_unreachable ();
1281950397Sobrien    }
1282050397Sobrien
1282150397Sobrien  TREE_ASM_WRITTEN (type) = 1;
1282250397Sobrien}
1282350397Sobrien
12824259269Spfgstatic void
12825259269Spfggen_type_die (tree type, dw_die_ref context_die)
12826259269Spfg{
12827259269Spfg  gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
12828259269Spfg}
12829259269Spfg
1283050397Sobrien/* Generate a DIE for a tagged type instantiation.  */
1283150397Sobrien
1283250397Sobrienstatic void
12833132718Skangen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
1283450397Sobrien{
1283550397Sobrien  if (type == NULL_TREE || type == error_mark_node)
1283650397Sobrien    return;
1283750397Sobrien
1283850397Sobrien  /* We are going to output a DIE to represent the unqualified version of
1283950397Sobrien     this type (i.e. without any const or volatile qualifiers) so make sure
1284050397Sobrien     that we have the main variant (i.e. the unqualified version) of this
1284150397Sobrien     type now.  */
12842169689Skan  gcc_assert (type == type_main_variant (type));
1284350397Sobrien
1284490075Sobrien  /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
1284590075Sobrien     an instance of an unresolved type.  */
1284690075Sobrien
1284750397Sobrien  switch (TREE_CODE (type))
1284850397Sobrien    {
1284950397Sobrien    case ERROR_MARK:
1285050397Sobrien      break;
1285150397Sobrien
1285250397Sobrien    case ENUMERAL_TYPE:
1285350397Sobrien      gen_inlined_enumeration_type_die (type, context_die);
1285450397Sobrien      break;
1285550397Sobrien
1285650397Sobrien    case RECORD_TYPE:
1285750397Sobrien      gen_inlined_structure_type_die (type, context_die);
1285850397Sobrien      break;
1285950397Sobrien
1286050397Sobrien    case UNION_TYPE:
1286150397Sobrien    case QUAL_UNION_TYPE:
1286250397Sobrien      gen_inlined_union_type_die (type, context_die);
1286350397Sobrien      break;
1286450397Sobrien
1286550397Sobrien    default:
12866169689Skan      gcc_unreachable ();
1286750397Sobrien    }
1286850397Sobrien}
1286950397Sobrien
1287050397Sobrien/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
1287150397Sobrien   things which are local to the given block.  */
1287250397Sobrien
1287350397Sobrienstatic void
12874132718Skangen_block_die (tree stmt, dw_die_ref context_die, int depth)
1287550397Sobrien{
1287690075Sobrien  int must_output_die = 0;
1287790075Sobrien  tree origin;
1287890075Sobrien  tree decl;
1287990075Sobrien  enum tree_code origin_code;
1288050397Sobrien
12881169689Skan  /* Ignore blocks that are NULL.  */
12882169689Skan  if (stmt == NULL_TREE)
1288350397Sobrien    return;
1288450397Sobrien
1288590075Sobrien  /* If the block is one fragment of a non-contiguous block, do not
1288690075Sobrien     process the variables, since they will have been done by the
1288790075Sobrien     origin block.  Do process subblocks.  */
1288890075Sobrien  if (BLOCK_FRAGMENT_ORIGIN (stmt))
1288990075Sobrien    {
1289090075Sobrien      tree sub;
1289190075Sobrien
1289290075Sobrien      for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
1289390075Sobrien	gen_block_die (sub, context_die, depth + 1);
1289490075Sobrien
1289590075Sobrien      return;
1289690075Sobrien    }
1289790075Sobrien
1289850397Sobrien  /* Determine the "ultimate origin" of this block.  This block may be an
1289950397Sobrien     inlined instance of an inlined instance of inline function, so we have
1290050397Sobrien     to trace all of the way back through the origin chain to find out what
1290150397Sobrien     sort of node actually served as the original seed for the creation of
1290250397Sobrien     the current block.  */
1290350397Sobrien  origin = block_ultimate_origin (stmt);
1290450397Sobrien  origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
1290550397Sobrien
1290650397Sobrien  /* Determine if we need to output any Dwarf DIEs at all to represent this
1290750397Sobrien     block.  */
1290850397Sobrien  if (origin_code == FUNCTION_DECL)
1290950397Sobrien    /* The outer scopes for inlinings *must* always be represented.  We
1291050397Sobrien       generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
1291150397Sobrien    must_output_die = 1;
1291250397Sobrien  else
1291350397Sobrien    {
1291450397Sobrien      /* In the case where the current block represents an inlining of the
12915132718Skan	 "body block" of an inline function, we must *NOT* output any DIE for
12916132718Skan	 this block because we have already output a DIE to represent the whole
12917132718Skan	 inlined function scope and the "body block" of any function doesn't
12918132718Skan	 really represent a different scope according to ANSI C rules.  So we
12919132718Skan	 check here to make sure that this block does not represent a "body
12920132718Skan	 block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
1292150397Sobrien      if (! is_body_block (origin ? origin : stmt))
1292250397Sobrien	{
1292350397Sobrien	  /* Determine if this block directly contains any "significant"
1292450397Sobrien	     local declarations which we will need to output DIEs for.  */
1292550397Sobrien	  if (debug_info_level > DINFO_LEVEL_TERSE)
1292650397Sobrien	    /* We are not in terse mode so *any* local declaration counts
1292750397Sobrien	       as being a "significant" one.  */
12928169689Skan	    must_output_die = (BLOCK_VARS (stmt) != NULL
12929169689Skan			       && (TREE_USED (stmt)
12930169689Skan				   || TREE_ASM_WRITTEN (stmt)
12931169689Skan				   || BLOCK_ABSTRACT (stmt)));
1293250397Sobrien	  else
1293350397Sobrien	    /* We are in terse mode, so only local (nested) function
1293450397Sobrien	       definitions count as "significant" local declarations.  */
1293550397Sobrien	    for (decl = BLOCK_VARS (stmt);
1293650397Sobrien		 decl != NULL; decl = TREE_CHAIN (decl))
1293750397Sobrien	      if (TREE_CODE (decl) == FUNCTION_DECL
1293850397Sobrien		  && DECL_INITIAL (decl))
1293950397Sobrien		{
1294050397Sobrien		  must_output_die = 1;
1294150397Sobrien		  break;
1294250397Sobrien		}
1294350397Sobrien	}
1294450397Sobrien    }
1294550397Sobrien
1294650397Sobrien  /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
1294750397Sobrien     DIE for any block which contains no significant local declarations at
1294850397Sobrien     all.  Rather, in such cases we just call `decls_for_scope' so that any
1294950397Sobrien     needed Dwarf info for any sub-blocks will get properly generated. Note
1295050397Sobrien     that in terse mode, our definition of what constitutes a "significant"
1295150397Sobrien     local declaration gets restricted to include only inlined function
1295250397Sobrien     instances and local (nested) function definitions.  */
1295350397Sobrien  if (must_output_die)
1295450397Sobrien    {
1295550397Sobrien      if (origin_code == FUNCTION_DECL)
1295650397Sobrien	gen_inlined_subroutine_die (stmt, context_die, depth);
1295750397Sobrien      else
1295850397Sobrien	gen_lexical_block_die (stmt, context_die, depth);
1295950397Sobrien    }
1296050397Sobrien  else
1296150397Sobrien    decls_for_scope (stmt, context_die, depth);
1296250397Sobrien}
1296350397Sobrien
1296450397Sobrien/* Generate all of the decls declared within a given scope and (recursively)
1296550397Sobrien   all of its sub-blocks.  */
1296650397Sobrien
1296750397Sobrienstatic void
12968132718Skandecls_for_scope (tree stmt, dw_die_ref context_die, int depth)
1296950397Sobrien{
1297090075Sobrien  tree decl;
1297190075Sobrien  tree subblocks;
1297250397Sobrien
12973169689Skan  /* Ignore NULL blocks.  */
12974169689Skan  if (stmt == NULL_TREE)
1297550397Sobrien    return;
1297650397Sobrien
12977169689Skan  if (TREE_USED (stmt))
1297850397Sobrien    {
12979169689Skan      /* Output the DIEs to represent all of the data objects and typedefs
12980169689Skan	 declared directly within this block but not within any nested
12981169689Skan	 sub-blocks.  Also, nested function and tag DIEs have been
12982169689Skan	 generated with a parent of NULL; fix that up now.  */
12983169689Skan      for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12984169689Skan	{
12985169689Skan	  dw_die_ref die;
12986169689Skan
12987169689Skan	  if (TREE_CODE (decl) == FUNCTION_DECL)
12988169689Skan	    die = lookup_decl_die (decl);
12989169689Skan	  else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12990169689Skan	    die = lookup_type_die (TREE_TYPE (decl));
12991169689Skan	  else
12992169689Skan	    die = NULL;
12993169689Skan
12994169689Skan	  if (die != NULL && die->die_parent == NULL)
12995169689Skan	    add_child_die (context_die, die);
12996169689Skan	  /* Do not produce debug information for static variables since
12997169689Skan	     these might be optimized out.  We are called for these later
12998169689Skan	     in cgraph_varpool_analyze_pending_decls. */
12999169689Skan	  if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13000169689Skan	    ;
13001169689Skan	  else
13002169689Skan	    gen_decl_die (decl, context_die);
13003169689Skan	}
1300450397Sobrien    }
1300550397Sobrien
13006117395Skan  /* If we're at -g1, we're not interested in subblocks.  */
13007117395Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13008117395Skan    return;
13009117395Skan
1301050397Sobrien  /* Output the DIEs to represent all sub-blocks (and the items declared
1301150397Sobrien     therein) of this block.  */
1301250397Sobrien  for (subblocks = BLOCK_SUBBLOCKS (stmt);
1301350397Sobrien       subblocks != NULL;
1301450397Sobrien       subblocks = BLOCK_CHAIN (subblocks))
1301550397Sobrien    gen_block_die (subblocks, context_die, depth + 1);
1301650397Sobrien}
1301750397Sobrien
1301850397Sobrien/* Is this a typedef we can avoid emitting?  */
1301950397Sobrien
1302050397Sobrienstatic inline int
13021132718Skanis_redundant_typedef (tree decl)
1302250397Sobrien{
1302350397Sobrien  if (TYPE_DECL_IS_STUB (decl))
1302450397Sobrien    return 1;
1302550397Sobrien
1302650397Sobrien  if (DECL_ARTIFICIAL (decl)
1302750397Sobrien      && DECL_CONTEXT (decl)
1302850397Sobrien      && is_tagged_type (DECL_CONTEXT (decl))
1302950397Sobrien      && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
1303050397Sobrien      && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
1303150397Sobrien    /* Also ignore the artificial member typedef for the class name.  */
1303250397Sobrien    return 1;
1303350397Sobrien
1303450397Sobrien  return 0;
1303550397Sobrien}
1303650397Sobrien
13037169689Skan/* Returns the DIE for decl.  A DIE will always be returned.  */
13038132718Skan
13039169689Skanstatic dw_die_ref
13040169689Skanforce_decl_die (tree decl)
13041169689Skan{
13042169689Skan  dw_die_ref decl_die;
13043169689Skan  unsigned saved_external_flag;
13044169689Skan  tree save_fn = NULL_TREE;
13045169689Skan  decl_die = lookup_decl_die (decl);
13046169689Skan  if (!decl_die)
13047169689Skan    {
13048169689Skan      dw_die_ref context_die;
13049169689Skan      tree decl_context = DECL_CONTEXT (decl);
13050169689Skan      if (decl_context)
13051169689Skan	{
13052169689Skan	  /* Find die that represents this context.  */
13053169689Skan	  if (TYPE_P (decl_context))
13054169689Skan	    context_die = force_type_die (decl_context);
13055169689Skan	  else
13056169689Skan	    context_die = force_decl_die (decl_context);
13057169689Skan	}
13058169689Skan      else
13059169689Skan	context_die = comp_unit_die;
13060132718Skan
13061169689Skan      decl_die = lookup_decl_die (decl);
13062169689Skan      if (decl_die)
13063169689Skan	return decl_die;
13064169689Skan
13065169689Skan      switch (TREE_CODE (decl))
13066169689Skan	{
13067169689Skan	case FUNCTION_DECL:
13068169689Skan	  /* Clear current_function_decl, so that gen_subprogram_die thinks
13069169689Skan	     that this is a declaration. At this point, we just want to force
13070169689Skan	     declaration die.  */
13071169689Skan	  save_fn = current_function_decl;
13072169689Skan	  current_function_decl = NULL_TREE;
13073169689Skan	  gen_subprogram_die (decl, context_die);
13074169689Skan	  current_function_decl = save_fn;
13075169689Skan	  break;
13076169689Skan
13077169689Skan	case VAR_DECL:
13078169689Skan	  /* Set external flag to force declaration die. Restore it after
13079169689Skan	   gen_decl_die() call.  */
13080169689Skan	  saved_external_flag = DECL_EXTERNAL (decl);
13081169689Skan	  DECL_EXTERNAL (decl) = 1;
13082169689Skan	  gen_decl_die (decl, context_die);
13083169689Skan	  DECL_EXTERNAL (decl) = saved_external_flag;
13084169689Skan	  break;
13085169689Skan
13086169689Skan	case NAMESPACE_DECL:
13087169689Skan	  dwarf2out_decl (decl);
13088169689Skan	  break;
13089169689Skan
13090169689Skan	default:
13091169689Skan	  gcc_unreachable ();
13092169689Skan	}
13093169689Skan
13094169689Skan      /* We should be able to find the DIE now.  */
13095169689Skan      if (!decl_die)
13096169689Skan	decl_die = lookup_decl_die (decl);
13097169689Skan      gcc_assert (decl_die);
13098169689Skan    }
13099169689Skan
13100169689Skan  return decl_die;
13101169689Skan}
13102169689Skan
13103169689Skan/* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13104169689Skan   always returned.  */
13105169689Skan
13106132718Skanstatic dw_die_ref
13107169689Skanforce_type_die (tree type)
13108132718Skan{
13109169689Skan  dw_die_ref type_die;
13110132718Skan
13111169689Skan  type_die = lookup_type_die (type);
13112169689Skan  if (!type_die)
13113169689Skan    {
13114169689Skan      dw_die_ref context_die;
13115169689Skan      if (TYPE_CONTEXT (type))
13116169689Skan	{
13117169689Skan	  if (TYPE_P (TYPE_CONTEXT (type)))
13118169689Skan	    context_die = force_type_die (TYPE_CONTEXT (type));
13119169689Skan	  else
13120169689Skan	    context_die = force_decl_die (TYPE_CONTEXT (type));
13121169689Skan	}
13122169689Skan      else
13123169689Skan	context_die = comp_unit_die;
13124132718Skan
13125169689Skan      type_die = lookup_type_die (type);
13126169689Skan      if (type_die)
13127169689Skan	return type_die;
13128169689Skan      gen_type_die (type, context_die);
13129169689Skan      type_die = lookup_type_die (type);
13130169689Skan      gcc_assert (type_die);
13131169689Skan    }
13132169689Skan  return type_die;
13133132718Skan}
13134132718Skan
13135132718Skan/* Force out any required namespaces to be able to output DECL,
13136132718Skan   and return the new context_die for it, if it's changed.  */
13137132718Skan
13138132718Skanstatic dw_die_ref
13139132718Skansetup_namespace_context (tree thing, dw_die_ref context_die)
13140132718Skan{
13141169689Skan  tree context = (DECL_P (thing)
13142169689Skan		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13143132718Skan  if (context && TREE_CODE (context) == NAMESPACE_DECL)
13144132718Skan    /* Force out the namespace.  */
13145169689Skan    context_die = force_decl_die (context);
13146132718Skan
13147132718Skan  return context_die;
13148132718Skan}
13149132718Skan
13150132718Skan/* Emit a declaration DIE for THING (which is either a DECL or a tagged
13151132718Skan   type) within its namespace, if appropriate.
13152132718Skan
13153132718Skan   For compatibility with older debuggers, namespace DIEs only contain
13154132718Skan   declarations; all definitions are emitted at CU scope.  */
13155132718Skan
13156132718Skanstatic void
13157132718Skandeclare_in_namespace (tree thing, dw_die_ref context_die)
13158132718Skan{
13159132718Skan  dw_die_ref ns_context;
13160132718Skan
13161132718Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13162132718Skan    return;
13163132718Skan
13164169689Skan  /* If this decl is from an inlined function, then don't try to emit it in its
13165169689Skan     namespace, as we will get confused.  It would have already been emitted
13166169689Skan     when the abstract instance of the inline function was emitted anyways.  */
13167169689Skan  if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13168169689Skan    return;
13169169689Skan
13170132718Skan  ns_context = setup_namespace_context (thing, context_die);
13171132718Skan
13172132718Skan  if (ns_context != context_die)
13173132718Skan    {
13174132718Skan      if (DECL_P (thing))
13175132718Skan	gen_decl_die (thing, ns_context);
13176132718Skan      else
13177132718Skan	gen_type_die (thing, ns_context);
13178132718Skan    }
13179132718Skan}
13180132718Skan
13181132718Skan/* Generate a DIE for a namespace or namespace alias.  */
13182132718Skan
13183132718Skanstatic void
13184132718Skangen_namespace_die (tree decl)
13185132718Skan{
13186132718Skan  dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13187132718Skan
13188132718Skan  /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13189132718Skan     they are an alias of.  */
13190132718Skan  if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13191132718Skan    {
13192132718Skan      /* Output a real namespace.  */
13193132718Skan      dw_die_ref namespace_die
13194132718Skan	= new_die (DW_TAG_namespace, context_die, decl);
13195132718Skan      add_name_and_src_coords_attributes (namespace_die, decl);
13196132718Skan      equate_decl_number_to_die (decl, namespace_die);
13197132718Skan    }
13198132718Skan  else
13199132718Skan    {
13200132718Skan      /* Output a namespace alias.  */
13201132718Skan
13202132718Skan      /* Force out the namespace we are an alias of, if necessary.  */
13203132718Skan      dw_die_ref origin_die
13204169689Skan	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13205132718Skan
13206132718Skan      /* Now create the namespace alias DIE.  */
13207132718Skan      dw_die_ref namespace_die
13208132718Skan	= new_die (DW_TAG_imported_declaration, context_die, decl);
13209132718Skan      add_name_and_src_coords_attributes (namespace_die, decl);
13210132718Skan      add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13211132718Skan      equate_decl_number_to_die (decl, namespace_die);
13212132718Skan    }
13213132718Skan}
13214132718Skan
1321550397Sobrien/* Generate Dwarf debug information for a decl described by DECL.  */
1321650397Sobrien
1321750397Sobrienstatic void
13218132718Skangen_decl_die (tree decl, dw_die_ref context_die)
1321950397Sobrien{
1322090075Sobrien  tree origin;
1322150397Sobrien
1322290075Sobrien  if (DECL_P (decl) && DECL_IGNORED_P (decl))
1322350397Sobrien    return;
1322450397Sobrien
1322550397Sobrien  switch (TREE_CODE (decl))
1322650397Sobrien    {
1322790075Sobrien    case ERROR_MARK:
1322890075Sobrien      break;
1322990075Sobrien
1323050397Sobrien    case CONST_DECL:
1323190075Sobrien      /* The individual enumerators of an enum type get output when we output
13232132718Skan	 the Dwarf representation of the relevant enum type itself.  */
1323350397Sobrien      break;
1323450397Sobrien
1323550397Sobrien    case FUNCTION_DECL:
1323650397Sobrien      /* Don't output any DIEs to represent mere function declarations,
1323750397Sobrien	 unless they are class members or explicit block externs.  */
1323850397Sobrien      if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
1323990075Sobrien	  && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
1324050397Sobrien	break;
1324150397Sobrien
13242169689Skan#if 0
13243169689Skan      /* FIXME */
13244169689Skan      /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13245169689Skan	 on local redeclarations of global functions.  That seems broken.  */
13246169689Skan      if (current_function_decl != decl)
13247169689Skan	/* This is only a declaration.  */;
13248169689Skan#endif
13249169689Skan
1325090075Sobrien      /* If we're emitting a clone, emit info for the abstract instance.  */
1325190075Sobrien      if (DECL_ORIGIN (decl) != decl)
1325290075Sobrien	dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
1325390075Sobrien
1325490075Sobrien      /* If we're emitting an out-of-line copy of an inline function,
1325590075Sobrien	 emit info for the abstract instance and set up to refer to it.  */
13256132718Skan      else if (cgraph_function_possibly_inlined_p (decl)
13257132718Skan	       && ! DECL_ABSTRACT (decl)
13258132718Skan	       && ! class_or_namespace_scope_p (context_die)
1325990075Sobrien	       /* dwarf2out_abstract_function won't emit a die if this is just
1326090075Sobrien		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
1326190075Sobrien		  that case, because that works only if we have a die.  */
1326290075Sobrien	       && DECL_INITIAL (decl) != NULL_TREE)
1326350397Sobrien	{
1326490075Sobrien	  dwarf2out_abstract_function (decl);
1326590075Sobrien	  set_decl_origin_self (decl);
1326690075Sobrien	}
1326790075Sobrien
1326890075Sobrien      /* Otherwise we're emitting the primary DIE for this decl.  */
1326990075Sobrien      else if (debug_info_level > DINFO_LEVEL_TERSE)
1327090075Sobrien	{
1327150397Sobrien	  /* Before we describe the FUNCTION_DECL itself, make sure that we
1327250397Sobrien	     have described its return type.  */
1327350397Sobrien	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
1327450397Sobrien
1327590075Sobrien	  /* And its virtual context.  */
1327690075Sobrien	  if (DECL_VINDEX (decl) != NULL_TREE)
1327790075Sobrien	    gen_type_die (DECL_CONTEXT (decl), context_die);
1327890075Sobrien
1327950397Sobrien	  /* And its containing type.  */
1328050397Sobrien	  origin = decl_class_context (decl);
1328150397Sobrien	  if (origin != NULL_TREE)
1328290075Sobrien	    gen_type_die_for_member (origin, decl, context_die);
13283132718Skan
13284132718Skan	  /* And its containing namespace.  */
13285132718Skan	  declare_in_namespace (decl, context_die);
1328650397Sobrien	}
1328750397Sobrien
1328850397Sobrien      /* Now output a DIE to represent the function itself.  */
1328950397Sobrien      gen_subprogram_die (decl, context_die);
1329050397Sobrien      break;
1329150397Sobrien
1329250397Sobrien    case TYPE_DECL:
1329350397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13294132718Skan	 actual typedefs.  */
1329550397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1329650397Sobrien	break;
1329750397Sobrien
1329890075Sobrien      /* In the special case of a TYPE_DECL node representing the declaration
13299132718Skan	 of some type tag, if the given TYPE_DECL is marked as having been
13300132718Skan	 instantiated from some other (original) TYPE_DECL node (e.g. one which
13301132718Skan	 was generated within the original definition of an inline function) we
13302132718Skan	 have to generate a special (abbreviated) DW_TAG_structure_type,
13303132718Skan	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13304220150Smm      if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13305220150Smm	  && is_tagged_type (TREE_TYPE (decl)))
1330650397Sobrien	{
1330750397Sobrien	  gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
1330850397Sobrien	  break;
1330950397Sobrien	}
1331050397Sobrien
1331150397Sobrien      if (is_redundant_typedef (decl))
1331250397Sobrien	gen_type_die (TREE_TYPE (decl), context_die);
1331350397Sobrien      else
1331450397Sobrien	/* Output a DIE to represent the typedef itself.  */
1331550397Sobrien	gen_typedef_die (decl, context_die);
1331650397Sobrien      break;
1331750397Sobrien
1331850397Sobrien    case LABEL_DECL:
1331950397Sobrien      if (debug_info_level >= DINFO_LEVEL_NORMAL)
1332050397Sobrien	gen_label_die (decl, context_die);
1332150397Sobrien      break;
1332250397Sobrien
1332350397Sobrien    case VAR_DECL:
13324169689Skan    case RESULT_DECL:
1332550397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13326132718Skan	 variable declarations or definitions.  */
1332750397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1332850397Sobrien	break;
1332950397Sobrien
1333050397Sobrien      /* Output any DIEs that are needed to specify the type of this data
13331132718Skan	 object.  */
1333250397Sobrien      gen_type_die (TREE_TYPE (decl), context_die);
1333350397Sobrien
1333450397Sobrien      /* And its containing type.  */
1333550397Sobrien      origin = decl_class_context (decl);
1333650397Sobrien      if (origin != NULL_TREE)
1333790075Sobrien	gen_type_die_for_member (origin, decl, context_die);
1333850397Sobrien
13339132718Skan      /* And its containing namespace.  */
13340132718Skan      declare_in_namespace (decl, context_die);
13341132718Skan
1334250397Sobrien      /* Now output the DIE to represent the data object itself.  This gets
13343132718Skan	 complicated because of the possibility that the VAR_DECL really
13344132718Skan	 represents an inlined instance of a formal parameter for an inline
13345132718Skan	 function.  */
1334650397Sobrien      origin = decl_ultimate_origin (decl);
1334750397Sobrien      if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
1334850397Sobrien	gen_formal_parameter_die (decl, context_die);
1334950397Sobrien      else
1335050397Sobrien	gen_variable_die (decl, context_die);
1335150397Sobrien      break;
1335250397Sobrien
1335350397Sobrien    case FIELD_DECL:
1335490075Sobrien      /* Ignore the nameless fields that are used to skip bits but handle C++
13355169689Skan	 anonymous unions and structs.  */
1335650397Sobrien      if (DECL_NAME (decl) != NULL_TREE
13357169689Skan	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13358169689Skan	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
1335950397Sobrien	{
1336050397Sobrien	  gen_type_die (member_declared_type (decl), context_die);
1336150397Sobrien	  gen_field_die (decl, context_die);
1336250397Sobrien	}
1336350397Sobrien      break;
1336450397Sobrien
1336550397Sobrien    case PARM_DECL:
1336650397Sobrien      gen_type_die (TREE_TYPE (decl), context_die);
1336750397Sobrien      gen_formal_parameter_die (decl, context_die);
1336850397Sobrien      break;
1336950397Sobrien
1337090075Sobrien    case NAMESPACE_DECL:
13371132718Skan      gen_namespace_die (decl);
1337290075Sobrien      break;
1337390075Sobrien
1337450397Sobrien    default:
13375169689Skan      /* Probably some frontend-internal decl.  Assume we don't care.  */
13376169689Skan      gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13377169689Skan      break;
1337850397Sobrien    }
1337950397Sobrien}
1338050397Sobrien
1338190075Sobrien/* Output debug information for global decl DECL.  Called from toplev.c after
1338290075Sobrien   compilation proper has finished.  */
1338390075Sobrien
1338490075Sobrienstatic void
13385132718Skandwarf2out_global_decl (tree decl)
1338690075Sobrien{
1338790075Sobrien  /* Output DWARF2 information for file-scope tentative data object
1338890075Sobrien     declarations, file-scope (extern) function declarations (which had no
1338990075Sobrien     corresponding body) and file-scope tagged type declarations and
1339090075Sobrien     definitions which have not yet been forced out.  */
1339190075Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
1339290075Sobrien    dwarf2out_decl (decl);
1339390075Sobrien}
1339490075Sobrien
13395169689Skan/* Output debug information for type decl DECL.  Called from toplev.c
13396169689Skan   and from language front ends (to record built-in types).  */
13397169689Skanstatic void
13398169689Skandwarf2out_type_decl (tree decl, int local)
13399169689Skan{
13400169689Skan  if (!local)
13401169689Skan    dwarf2out_decl (decl);
13402169689Skan}
13403169689Skan
13404169689Skan/* Output debug information for imported module or decl.  */
13405169689Skan
13406169689Skanstatic void
13407169689Skandwarf2out_imported_module_or_decl (tree decl, tree context)
13408169689Skan{
13409169689Skan  dw_die_ref imported_die, at_import_die;
13410169689Skan  dw_die_ref scope_die;
13411169689Skan  expanded_location xloc;
13412169689Skan
13413169689Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13414169689Skan    return;
13415169689Skan
13416169689Skan  gcc_assert (decl);
13417169689Skan
13418169689Skan  /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13419169689Skan     We need decl DIE for reference and scope die. First, get DIE for the decl
13420169689Skan     itself.  */
13421169689Skan
13422169689Skan  /* Get the scope die for decl context. Use comp_unit_die for global module
13423169689Skan     or decl. If die is not found for non globals, force new die.  */
13424169689Skan  if (!context)
13425169689Skan    scope_die = comp_unit_die;
13426169689Skan  else if (TYPE_P (context))
13427259269Spfg    {
13428259269Spfg      if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13429259269Spfg	return;
13430169689Skan    scope_die = force_type_die (context);
13431259269Spfg    }
13432169689Skan  else
13433169689Skan    scope_die = force_decl_die (context);
13434169689Skan
13435169689Skan  /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13436169689Skan  if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13437169689Skan    {
13438169689Skan      if (is_base_type (TREE_TYPE (decl)))
13439169689Skan	at_import_die = base_type_die (TREE_TYPE (decl));
13440169689Skan      else
13441169689Skan	at_import_die = force_type_die (TREE_TYPE (decl));
13442169689Skan    }
13443169689Skan  else
13444169689Skan    {
13445169689Skan      at_import_die = lookup_decl_die (decl);
13446169689Skan      if (!at_import_die)
13447169689Skan	{
13448169689Skan	  /* If we're trying to avoid duplicate debug info, we may not have
13449169689Skan	     emitted the member decl for this field.  Emit it now.  */
13450169689Skan	  if (TREE_CODE (decl) == FIELD_DECL)
13451169689Skan	    {
13452169689Skan	      tree type = DECL_CONTEXT (decl);
13453169689Skan	      dw_die_ref type_context_die;
13454169689Skan
13455169689Skan	      if (TYPE_CONTEXT (type))
13456169689Skan		if (TYPE_P (TYPE_CONTEXT (type)))
13457259269Spfg		  {
13458259269Spfg		    if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13459259269Spfg						   DINFO_USAGE_DIR_USE))
13460259269Spfg		      return;
13461169689Skan		  type_context_die = force_type_die (TYPE_CONTEXT (type));
13462259269Spfg		  }
13463169689Skan	      else
13464169689Skan		type_context_die = force_decl_die (TYPE_CONTEXT (type));
13465169689Skan	      else
13466169689Skan		type_context_die = comp_unit_die;
13467169689Skan	      gen_type_die_for_member (type, decl, type_context_die);
13468169689Skan	    }
13469169689Skan	  at_import_die = force_decl_die (decl);
13470169689Skan	}
13471169689Skan    }
13472169689Skan
13473169689Skan  /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13474169689Skan  if (TREE_CODE (decl) == NAMESPACE_DECL)
13475169689Skan    imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13476169689Skan  else
13477169689Skan    imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13478169689Skan
13479169689Skan  xloc = expand_location (input_location);
13480169689Skan  add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13481169689Skan  add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13482169689Skan  add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13483169689Skan}
13484169689Skan
1348590075Sobrien/* Write the debugging output for DECL.  */
1348690075Sobrien
1348790075Sobrienvoid
13488132718Skandwarf2out_decl (tree decl)
1348990075Sobrien{
1349090075Sobrien  dw_die_ref context_die = comp_unit_die;
1349190075Sobrien
1349250397Sobrien  switch (TREE_CODE (decl))
1349350397Sobrien    {
1349490075Sobrien    case ERROR_MARK:
1349590075Sobrien      return;
1349690075Sobrien
1349750397Sobrien    case FUNCTION_DECL:
1349850397Sobrien      /* What we would really like to do here is to filter out all mere
13499132718Skan	 file-scope declarations of file-scope functions which are never
13500132718Skan	 referenced later within this translation unit (and keep all of ones
13501132718Skan	 that *are* referenced later on) but we aren't clairvoyant, so we have
13502132718Skan	 no idea which functions will be referenced in the future (i.e. later
13503132718Skan	 on within the current translation unit). So here we just ignore all
13504132718Skan	 file-scope function declarations which are not also definitions.  If
13505132718Skan	 and when the debugger needs to know something about these functions,
13506132718Skan	 it will have to hunt around and find the DWARF information associated
13507132718Skan	 with the definition of the function.
1350890075Sobrien
1350990075Sobrien	 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13510132718Skan	 nodes represent definitions and which ones represent mere
13511132718Skan	 declarations.  We have to check DECL_INITIAL instead. That's because
13512132718Skan	 the C front-end supports some weird semantics for "extern inline"
13513132718Skan	 function definitions.  These can get inlined within the current
13514169689Skan	 translation unit (and thus, we need to generate Dwarf info for their
13515132718Skan	 abstract instances so that the Dwarf info for the concrete inlined
13516132718Skan	 instances can have something to refer to) but the compiler never
13517132718Skan	 generates any out-of-lines instances of such things (despite the fact
13518132718Skan	 that they *are* definitions).
1351990075Sobrien
1352090075Sobrien	 The important point is that the C front-end marks these "extern
1352190075Sobrien	 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
1352290075Sobrien	 them anyway. Note that the C++ front-end also plays some similar games
1352390075Sobrien	 for inline function definitions appearing within include files which
1352490075Sobrien	 also contain `#pragma interface' pragmas.  */
1352550397Sobrien      if (DECL_INITIAL (decl) == NULL_TREE)
1352650397Sobrien	return;
1352750397Sobrien
1352850397Sobrien      /* If we're a nested function, initially use a parent of NULL; if we're
1352950397Sobrien	 a plain function, this will be fixed up in decls_for_scope.  If
1353050397Sobrien	 we're a method, it will be ignored, since we already have a DIE.  */
13531117395Skan      if (decl_function_context (decl)
13532117395Skan	  /* But if we're in terse mode, we don't care about scope.  */
13533117395Skan	  && debug_info_level > DINFO_LEVEL_TERSE)
1353450397Sobrien	context_die = NULL;
1353550397Sobrien      break;
1353650397Sobrien
1353750397Sobrien    case VAR_DECL:
1353890075Sobrien      /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13539132718Skan	 declaration and if the declaration was never even referenced from
13540132718Skan	 within this entire compilation unit.  We suppress these DIEs in
13541132718Skan	 order to save space in the .debug section (by eliminating entries
13542132718Skan	 which are probably useless).  Note that we must not suppress
13543132718Skan	 block-local extern declarations (whether used or not) because that
13544132718Skan	 would screw-up the debugger's name lookup mechanism and cause it to
13545132718Skan	 miss things which really ought to be in scope at a given point.  */
1354650397Sobrien      if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
1354750397Sobrien	return;
1354850397Sobrien
13549169689Skan      /* For local statics lookup proper context die.  */
13550169689Skan      if (TREE_STATIC (decl) && decl_function_context (decl))
13551169689Skan	context_die = lookup_decl_die (DECL_CONTEXT (decl));
13552169689Skan
1355350397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13554132718Skan	 variable declarations or definitions.  */
1355550397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1355650397Sobrien	return;
1355750397Sobrien      break;
1355850397Sobrien
13559132718Skan    case NAMESPACE_DECL:
13560132718Skan      if (debug_info_level <= DINFO_LEVEL_TERSE)
13561132718Skan	return;
13562132718Skan      if (lookup_decl_die (decl) != NULL)
13563132718Skan        return;
13564132718Skan      break;
13565132718Skan
1356650397Sobrien    case TYPE_DECL:
1356790075Sobrien      /* Don't emit stubs for types unless they are needed by other DIEs.  */
1356890075Sobrien      if (TYPE_DECL_SUPPRESS_DEBUG (decl))
1356990075Sobrien	return;
1357090075Sobrien
1357150397Sobrien      /* Don't bother trying to generate any DIEs to represent any of the
13572132718Skan	 normal built-in types for the language we are compiling.  */
13573169689Skan      if (DECL_IS_BUILTIN (decl))
1357450397Sobrien	{
1357550397Sobrien	  /* OK, we need to generate one for `bool' so GDB knows what type
13576132718Skan	     comparisons have.  */
13577169689Skan	  if (is_cxx ()
1357890075Sobrien	      && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
1357990075Sobrien	      && ! DECL_IGNORED_P (decl))
1358050397Sobrien	    modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
1358150397Sobrien
1358250397Sobrien	  return;
1358350397Sobrien	}
1358450397Sobrien
1358550397Sobrien      /* If we are in terse mode, don't generate any DIEs for types.  */
1358650397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1358750397Sobrien	return;
1358850397Sobrien
1358950397Sobrien      /* If we're a function-scope tag, initially use a parent of NULL;
1359050397Sobrien	 this will be fixed up in decls_for_scope.  */
1359150397Sobrien      if (decl_function_context (decl))
1359250397Sobrien	context_die = NULL;
1359350397Sobrien
1359450397Sobrien      break;
1359550397Sobrien
1359650397Sobrien    default:
1359750397Sobrien      return;
1359850397Sobrien    }
1359950397Sobrien
1360050397Sobrien  gen_decl_die (decl, context_die);
1360150397Sobrien}
1360250397Sobrien
1360350397Sobrien/* Output a marker (i.e. a label) for the beginning of the generated code for
1360450397Sobrien   a lexical block.  */
1360550397Sobrien
1360690075Sobrienstatic void
13607132718Skandwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13608132718Skan		       unsigned int blocknum)
1360950397Sobrien{
13610169689Skan  switch_to_section (current_function_section ());
1361190075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
1361250397Sobrien}
1361350397Sobrien
1361450397Sobrien/* Output a marker (i.e. a label) for the end of the generated code for a
1361550397Sobrien   lexical block.  */
1361650397Sobrien
1361790075Sobrienstatic void
13618132718Skandwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
1361950397Sobrien{
13620169689Skan  switch_to_section (current_function_section ());
1362190075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
1362250397Sobrien}
1362350397Sobrien
1362490075Sobrien/* Returns nonzero if it is appropriate not to emit any debugging
1362590075Sobrien   information for BLOCK, because it doesn't contain any instructions.
1362650397Sobrien
1362790075Sobrien   Don't allow this for blocks with nested functions or local classes
1362890075Sobrien   as we would end up with orphans, and in the presence of scheduling
1362990075Sobrien   we may end up calling them anyway.  */
1363090075Sobrien
1363190075Sobrienstatic bool
13632132718Skandwarf2out_ignore_block (tree block)
1363350397Sobrien{
1363490075Sobrien  tree decl;
1363550397Sobrien
1363690075Sobrien  for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
1363790075Sobrien    if (TREE_CODE (decl) == FUNCTION_DECL
1363890075Sobrien	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
1363990075Sobrien      return 0;
1364090075Sobrien
1364190075Sobrien  return 1;
1364250397Sobrien}
1364350397Sobrien
13644169689Skan/* Hash table routines for file_hash.  */
13645169689Skan
13646169689Skanstatic int
13647169689Skanfile_table_eq (const void *p1_p, const void *p2_p)
13648169689Skan{
13649169689Skan  const struct dwarf_file_data * p1 = p1_p;
13650169689Skan  const char * p2 = p2_p;
13651169689Skan  return strcmp (p1->filename, p2) == 0;
13652169689Skan}
13653169689Skan
13654169689Skanstatic hashval_t
13655169689Skanfile_table_hash (const void *p_p)
13656169689Skan{
13657169689Skan  const struct dwarf_file_data * p = p_p;
13658169689Skan  return htab_hash_string (p->filename);
13659169689Skan}
13660169689Skan
1366190075Sobrien/* Lookup FILE_NAME (in the list of filenames that we know about here in
1366250397Sobrien   dwarf2out.c) and return its "index".  The index of each (known) filename is
1366390075Sobrien   just a unique number which is associated with only that one filename.  We
1366490075Sobrien   need such numbers for the sake of generating labels (in the .debug_sfnames
1366590075Sobrien   section) and references to those files numbers (in the .debug_srcinfo
1366690075Sobrien   and.debug_macinfo sections).  If the filename given as an argument is not
1366790075Sobrien   found in our current list, add it to the list and assign it the next
1366890075Sobrien   available unique index number.  In order to speed up searches, we remember
1366990075Sobrien   the index of the filename was looked up last.  This handles the majority of
1367090075Sobrien   all searches.  */
1367150397Sobrien
13672169689Skanstatic struct dwarf_file_data *
13673132718Skanlookup_filename (const char *file_name)
1367450397Sobrien{
13675169689Skan  void ** slot;
13676169689Skan  struct dwarf_file_data * created;
1367750397Sobrien
1367890075Sobrien  /* Check to see if the file name that was searched on the previous
1367990075Sobrien     call matches this file name.  If so, return the index.  */
13680169689Skan  if (file_table_last_lookup
13681169689Skan      && (file_name == file_table_last_lookup->filename
13682169689Skan	  || strcmp (file_table_last_lookup->filename, file_name) == 0))
13683169689Skan    return file_table_last_lookup;
1368490075Sobrien
13685169689Skan  /* Didn't match the previous lookup, search the table.  */
13686169689Skan  slot = htab_find_slot_with_hash (file_table, file_name,
13687169689Skan				   htab_hash_string (file_name), INSERT);
13688169689Skan  if (*slot)
13689169689Skan    return *slot;
1369050397Sobrien
13691169689Skan  created = ggc_alloc (sizeof (struct dwarf_file_data));
13692169689Skan  created->filename = file_name;
13693169689Skan  created->emitted_number = 0;
13694169689Skan  *slot = created;
13695169689Skan  return created;
13696132718Skan}
13697132718Skan
13698169689Skan/* If the assembler will construct the file table, then translate the compiler
13699169689Skan   internal file table number into the assembler file table number, and emit
13700169689Skan   a .file directive if we haven't already emitted one yet.  The file table
13701169689Skan   numbers are different because we prune debug info for unused variables and
13702169689Skan   types, which may include filenames.  */
13703169689Skan
13704132718Skanstatic int
13705169689Skanmaybe_emit_file (struct dwarf_file_data * fd)
13706132718Skan{
13707169689Skan  if (! fd->emitted_number)
13708117395Skan    {
13709169689Skan      if (last_emitted_file)
13710169689Skan	fd->emitted_number = last_emitted_file->emitted_number + 1;
13711169689Skan      else
13712169689Skan	fd->emitted_number = 1;
13713169689Skan      last_emitted_file = fd;
13714169689Skan
13715169689Skan      if (DWARF2_ASM_LINE_DEBUG_INFO)
13716132718Skan	{
13717169689Skan	  fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13718169689Skan	  output_quoted_string (asm_out_file, fd->filename);
13719132718Skan	  fputc ('\n', asm_out_file);
13720132718Skan	}
13721117395Skan    }
13722169689Skan
13723169689Skan  return fd->emitted_number;
1372450397Sobrien}
1372550397Sobrien
13726169689Skan/* Called by the final INSN scan whenever we see a var location.  We
13727169689Skan   use it to drop labels in the right places, and throw the location in
13728169689Skan   our lookup table.  */
13729169689Skan
1373090075Sobrienstatic void
13731169689Skandwarf2out_var_location (rtx loc_note)
1373290075Sobrien{
13733169689Skan  char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13734169689Skan  struct var_loc_node *newloc;
13735169689Skan  rtx prev_insn;
13736169689Skan  static rtx last_insn;
13737169689Skan  static const char *last_label;
13738169689Skan  tree decl;
1373990075Sobrien
13740169689Skan  if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13741169689Skan    return;
13742169689Skan  prev_insn = PREV_INSN (loc_note);
13743169689Skan
13744169689Skan  newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13745169689Skan  /* If the insn we processed last time is the previous insn
13746169689Skan     and it is also a var location note, use the label we emitted
13747169689Skan     last time.  */
13748169689Skan  if (last_insn != NULL_RTX
13749169689Skan      && last_insn == prev_insn
13750169689Skan      && NOTE_P (prev_insn)
13751169689Skan      && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13752169689Skan    {
13753169689Skan      newloc->label = last_label;
13754169689Skan    }
13755169689Skan  else
13756169689Skan    {
13757169689Skan      ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13758169689Skan      ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13759169689Skan      loclabel_num++;
13760169689Skan      newloc->label = ggc_strdup (loclabel);
13761169689Skan    }
13762169689Skan  newloc->var_loc_note = loc_note;
13763169689Skan  newloc->next = NULL;
13764169689Skan
13765169689Skan  if (cfun && in_cold_section_p)
13766169689Skan    newloc->section_label = cfun->cold_section_label;
13767169689Skan  else
13768169689Skan    newloc->section_label = text_section_label;
13769169689Skan
13770169689Skan  last_insn = loc_note;
13771169689Skan  last_label = newloc->label;
13772169689Skan  decl = NOTE_VAR_LOCATION_DECL (loc_note);
13773169689Skan  add_var_loc_to_decl (decl, newloc);
1377490075Sobrien}
1377590075Sobrien
13776169689Skan/* We need to reset the locations at the beginning of each
13777169689Skan   function. We can't do this in the end_function hook, because the
13778169689Skan   declarations that use the locations won't have been output when
13779169689Skan   that hook is called.  Also compute have_multiple_function_sections here.  */
13780169689Skan
13781169689Skanstatic void
13782169689Skandwarf2out_begin_function (tree fun)
13783169689Skan{
13784169689Skan  htab_empty (decl_loc_table);
13785169689Skan
13786169689Skan  if (function_section (fun) != text_section)
13787169689Skan    have_multiple_function_sections = true;
13788169689Skan}
13789169689Skan
1379050397Sobrien/* Output a label to mark the beginning of a source code line entry
1379150397Sobrien   and record information relating to this source line, in
1379250397Sobrien   'line_info_table' for later output of the .debug_line section.  */
1379350397Sobrien
1379490075Sobrienstatic void
13795132718Skandwarf2out_source_line (unsigned int line, const char *filename)
1379650397Sobrien{
13797132718Skan  if (debug_info_level >= DINFO_LEVEL_NORMAL
13798132718Skan      && line != 0)
1379950397Sobrien    {
13800169689Skan      int file_num = maybe_emit_file (lookup_filename (filename));
13801169689Skan
13802169689Skan      switch_to_section (current_function_section ());
1380350397Sobrien
1380490075Sobrien      /* If requested, emit something human-readable.  */
1380590075Sobrien      if (flag_debug_asm)
1380690075Sobrien	fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
1380790075Sobrien		 filename, line);
1380890075Sobrien
1380990075Sobrien      if (DWARF2_ASM_LINE_DEBUG_INFO)
1381050397Sobrien	{
1381190075Sobrien	  /* Emit the .loc directive understood by GNU as.  */
1381290075Sobrien	  fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
1381390075Sobrien
1381490075Sobrien	  /* Indicate that line number info exists.  */
1381590075Sobrien	  line_info_table_in_use++;
1381690075Sobrien	}
13817169689Skan      else if (function_section (current_function_decl) != text_section)
1381890075Sobrien	{
1381990075Sobrien	  dw_separate_line_info_ref line_info;
13820169689Skan	  targetm.asm_out.internal_label (asm_out_file,
13821169689Skan					  SEPARATE_LINE_CODE_LABEL,
13822169689Skan					  separate_line_info_table_in_use);
1382350397Sobrien
13824169689Skan	  /* Expand the line info table if necessary.  */
1382550397Sobrien	  if (separate_line_info_table_in_use
1382650397Sobrien	      == separate_line_info_table_allocated)
1382750397Sobrien	    {
1382850397Sobrien	      separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1382950397Sobrien	      separate_line_info_table
13830132718Skan		= ggc_realloc (separate_line_info_table,
13831132718Skan			       separate_line_info_table_allocated
13832132718Skan			       * sizeof (dw_separate_line_info_entry));
13833132718Skan	      memset (separate_line_info_table
13834132718Skan		       + separate_line_info_table_in_use,
13835132718Skan		      0,
13836132718Skan		      (LINE_INFO_TABLE_INCREMENT
13837132718Skan		       * sizeof (dw_separate_line_info_entry)));
1383850397Sobrien	    }
1383950397Sobrien
1384050397Sobrien	  /* Add the new entry at the end of the line_info_table.  */
1384150397Sobrien	  line_info
1384250397Sobrien	    = &separate_line_info_table[separate_line_info_table_in_use++];
13843169689Skan	  line_info->dw_file_num = file_num;
1384450397Sobrien	  line_info->dw_line_num = line;
13845117395Skan	  line_info->function = current_function_funcdef_no;
1384650397Sobrien	}
1384750397Sobrien      else
1384850397Sobrien	{
1384990075Sobrien	  dw_line_info_ref line_info;
1385050397Sobrien
13851169689Skan	  targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
1385250397Sobrien				     line_info_table_in_use);
1385350397Sobrien
1385450397Sobrien	  /* Expand the line info table if necessary.  */
1385550397Sobrien	  if (line_info_table_in_use == line_info_table_allocated)
1385650397Sobrien	    {
1385750397Sobrien	      line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1385850397Sobrien	      line_info_table
13859132718Skan		= ggc_realloc (line_info_table,
13860132718Skan			       (line_info_table_allocated
13861132718Skan				* sizeof (dw_line_info_entry)));
13862132718Skan	      memset (line_info_table + line_info_table_in_use, 0,
13863132718Skan		      LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
1386450397Sobrien	    }
1386550397Sobrien
1386650397Sobrien	  /* Add the new entry at the end of the line_info_table.  */
1386750397Sobrien	  line_info = &line_info_table[line_info_table_in_use++];
13868169689Skan	  line_info->dw_file_num = file_num;
1386950397Sobrien	  line_info->dw_line_num = line;
1387050397Sobrien	}
1387150397Sobrien    }
1387250397Sobrien}
1387350397Sobrien
1387490075Sobrien/* Record the beginning of a new source file.  */
1387550397Sobrien
1387690075Sobrienstatic void
13877132718Skandwarf2out_start_source_file (unsigned int lineno, const char *filename)
1387850397Sobrien{
13879132718Skan  if (flag_eliminate_dwarf2_dups)
1388090075Sobrien    {
1388190075Sobrien      /* Record the beginning of the file for break_out_includes.  */
13882117395Skan      dw_die_ref bincl_die;
13883117395Skan
13884117395Skan      bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
1388590075Sobrien      add_AT_string (bincl_die, DW_AT_name, filename);
1388690075Sobrien    }
1388790075Sobrien
1388890075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1388990075Sobrien    {
13890169689Skan      int file_num = maybe_emit_file (lookup_filename (filename));
13891169689Skan
13892169689Skan      switch_to_section (debug_macinfo_section);
1389390075Sobrien      dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
1389490075Sobrien      dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
1389590075Sobrien				   lineno);
13896169689Skan
13897169689Skan      dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
1389890075Sobrien    }
1389950397Sobrien}
1390050397Sobrien
1390190075Sobrien/* Record the end of a source file.  */
1390250397Sobrien
1390390075Sobrienstatic void
13904132718Skandwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
1390550397Sobrien{
1390690075Sobrien  if (flag_eliminate_dwarf2_dups)
1390790075Sobrien    /* Record the end of the file for break_out_includes.  */
1390890075Sobrien    new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
1390990075Sobrien
1391090075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1391190075Sobrien    {
13912169689Skan      switch_to_section (debug_macinfo_section);
1391390075Sobrien      dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
1391490075Sobrien    }
1391550397Sobrien}
1391650397Sobrien
1391790075Sobrien/* Called from debug_define in toplev.c.  The `buffer' parameter contains
1391850397Sobrien   the tail part of the directive line, i.e. the part which is past the
1391950397Sobrien   initial whitespace, #, whitespace, directive-name, whitespace part.  */
1392050397Sobrien
1392190075Sobrienstatic void
13922132718Skandwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13923132718Skan		  const char *buffer ATTRIBUTE_UNUSED)
1392450397Sobrien{
1392590075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1392690075Sobrien    {
13927169689Skan      switch_to_section (debug_macinfo_section);
1392890075Sobrien      dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
1392990075Sobrien      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
1393090075Sobrien      dw2_asm_output_nstring (buffer, -1, "The macro");
1393190075Sobrien    }
1393250397Sobrien}
1393350397Sobrien
1393490075Sobrien/* Called from debug_undef in toplev.c.  The `buffer' parameter contains
1393550397Sobrien   the tail part of the directive line, i.e. the part which is past the
1393650397Sobrien   initial whitespace, #, whitespace, directive-name, whitespace part.  */
1393750397Sobrien
1393890075Sobrienstatic void
13939132718Skandwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13940132718Skan		 const char *buffer ATTRIBUTE_UNUSED)
1394150397Sobrien{
1394290075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1394390075Sobrien    {
13944169689Skan      switch_to_section (debug_macinfo_section);
1394590075Sobrien      dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
1394690075Sobrien      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
1394790075Sobrien      dw2_asm_output_nstring (buffer, -1, "The macro");
1394890075Sobrien    }
1394950397Sobrien}
1395050397Sobrien
1395150397Sobrien/* Set up for Dwarf output at the start of compilation.  */
1395250397Sobrien
1395390075Sobrienstatic void
13954132718Skandwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
1395550397Sobrien{
13956169689Skan  /* Allocate the file_table.  */
13957169689Skan  file_table = htab_create_ggc (50, file_table_hash,
13958169689Skan				file_table_eq, NULL);
1395990075Sobrien
13960169689Skan  /* Allocate the decl_die_table.  */
13961169689Skan  decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13962169689Skan				    decl_die_table_eq, NULL);
1396350397Sobrien
13964169689Skan  /* Allocate the decl_loc_table.  */
13965169689Skan  decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13966169689Skan				    decl_loc_table_eq, NULL);
13967169689Skan
1396850397Sobrien  /* Allocate the initial hunk of the decl_scope_table.  */
13969169689Skan  decl_scope_table = VEC_alloc (tree, gc, 256);
1397050397Sobrien
1397150397Sobrien  /* Allocate the initial hunk of the abbrev_die_table.  */
13972132718Skan  abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13973132718Skan					* sizeof (dw_die_ref));
1397450397Sobrien  abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13975169689Skan  /* Zero-th entry is allocated, but unused.  */
1397650397Sobrien  abbrev_die_table_in_use = 1;
1397750397Sobrien
1397850397Sobrien  /* Allocate the initial hunk of the line_info_table.  */
13979132718Skan  line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13980132718Skan				       * sizeof (dw_line_info_entry));
1398150397Sobrien  line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
1398290075Sobrien
13983169689Skan  /* Zero-th entry is allocated, but unused.  */
1398450397Sobrien  line_info_table_in_use = 1;
1398550397Sobrien
13986260396Spfg  /* Allocate the pubtypes and pubnames vectors.  */
13987260396Spfg  pubname_table = VEC_alloc (pubname_entry, gc, 32);
13988260396Spfg  pubtype_table = VEC_alloc (pubname_entry, gc, 32);
13989260396Spfg
1399090075Sobrien  /* Generate the initial DIE for the .debug section.  Note that the (string)
1399150397Sobrien     value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
1399290075Sobrien     will (typically) be a relative pathname and that this pathname should be
1399350397Sobrien     taken as being relative to the directory from which the compiler was
13994132718Skan     invoked when the given (base) source file was compiled.  We will fill
13995132718Skan     in this value in dwarf2out_finish.  */
13996132718Skan  comp_unit_die = gen_compile_unit_die (NULL);
1399750397Sobrien
13998169689Skan  incomplete_types = VEC_alloc (tree, gc, 64);
1399990075Sobrien
14000169689Skan  used_rtx_array = VEC_alloc (rtx, gc, 32);
1400190075Sobrien
14002169689Skan  debug_info_section = get_section (DEBUG_INFO_SECTION,
14003169689Skan				    SECTION_DEBUG, NULL);
14004169689Skan  debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14005169689Skan				      SECTION_DEBUG, NULL);
14006169689Skan  debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14007169689Skan				       SECTION_DEBUG, NULL);
14008169689Skan  debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14009169689Skan				       SECTION_DEBUG, NULL);
14010169689Skan  debug_line_section = get_section (DEBUG_LINE_SECTION,
14011169689Skan				    SECTION_DEBUG, NULL);
14012169689Skan  debug_loc_section = get_section (DEBUG_LOC_SECTION,
14013169689Skan				   SECTION_DEBUG, NULL);
14014169689Skan  debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14015169689Skan					SECTION_DEBUG, NULL);
14016260396Spfg#ifdef DEBUG_PUBTYPES_SECTION
14017260396Spfg  debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14018260396Spfg					SECTION_DEBUG, NULL);
14019260396Spfg#endif
14020169689Skan  debug_str_section = get_section (DEBUG_STR_SECTION,
14021169689Skan				   DEBUG_STR_SECTION_FLAGS, NULL);
14022169689Skan  debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14023169689Skan				      SECTION_DEBUG, NULL);
14024169689Skan  debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14025169689Skan				     SECTION_DEBUG, NULL);
14026169689Skan
1402750397Sobrien  ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
1402890075Sobrien  ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
1402990075Sobrien			       DEBUG_ABBREV_SECTION_LABEL, 0);
14030169689Skan  ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14031169689Skan  ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14032169689Skan			       COLD_TEXT_SECTION_LABEL, 0);
14033169689Skan  ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
1403490075Sobrien
1403590075Sobrien  ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
1403652284Sobrien			       DEBUG_INFO_SECTION_LABEL, 0);
1403790075Sobrien  ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
1403852284Sobrien			       DEBUG_LINE_SECTION_LABEL, 0);
1403990075Sobrien  ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
1404090075Sobrien			       DEBUG_RANGES_SECTION_LABEL, 0);
14041169689Skan  switch_to_section (debug_abbrev_section);
1404252284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14043169689Skan  switch_to_section (debug_info_section);
1404452284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14045169689Skan  switch_to_section (debug_line_section);
1404652284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
1404790075Sobrien
1404890075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1404990075Sobrien    {
14050169689Skan      switch_to_section (debug_macinfo_section);
1405190075Sobrien      ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
1405290075Sobrien				   DEBUG_MACINFO_SECTION_LABEL, 0);
1405390075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
1405490075Sobrien    }
1405590075Sobrien
14056169689Skan  switch_to_section (text_section);
14057169689Skan  ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14058169689Skan  if (flag_reorder_blocks_and_partition)
1405990075Sobrien    {
14060169689Skan      switch_to_section (unlikely_text_section ());
14061169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
1406290075Sobrien    }
1406350397Sobrien}
1406450397Sobrien
1406590075Sobrien/* A helper function for dwarf2out_finish called through
1406690075Sobrien   ht_forall.  Emit one queued .debug_str string.  */
1406790075Sobrien
1406890075Sobrienstatic int
14069132718Skanoutput_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
1407090075Sobrien{
14071132718Skan  struct indirect_string_node *node = (struct indirect_string_node *) *h;
1407290075Sobrien
1407390075Sobrien  if (node->form == DW_FORM_strp)
1407490075Sobrien    {
14075169689Skan      switch_to_section (debug_str_section);
1407690075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, node->label);
14077132718Skan      assemble_string (node->str, strlen (node->str) + 1);
1407890075Sobrien    }
1407990075Sobrien
1408090075Sobrien  return 1;
1408190075Sobrien}
1408290075Sobrien
14083169689Skan#if ENABLE_ASSERT_CHECKING
14084169689Skan/* Verify that all marks are clear.  */
14085132718Skan
14086169689Skanstatic void
14087169689Skanverify_marks_clear (dw_die_ref die)
14088169689Skan{
14089169689Skan  dw_die_ref c;
14090169689Skan
14091169689Skan  gcc_assert (! die->die_mark);
14092169689Skan  FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14093169689Skan}
14094169689Skan#endif /* ENABLE_ASSERT_CHECKING */
14095132718Skan
14096132718Skan/* Clear the marks for a die and its children.
14097132718Skan   Be cool if the mark isn't set.  */
14098132718Skan
14099132718Skanstatic void
14100132718Skanprune_unmark_dies (dw_die_ref die)
14101132718Skan{
14102132718Skan  dw_die_ref c;
14103169689Skan
14104169689Skan  if (die->die_mark)
14105169689Skan    die->die_mark = 0;
14106169689Skan  FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14107132718Skan}
14108132718Skan
14109132718Skan/* Given DIE that we're marking as used, find any other dies
14110132718Skan   it references as attributes and mark them as used.  */
14111132718Skan
14112132718Skanstatic void
14113132718Skanprune_unused_types_walk_attribs (dw_die_ref die)
14114132718Skan{
14115132718Skan  dw_attr_ref a;
14116169689Skan  unsigned ix;
14117132718Skan
14118169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14119132718Skan    {
14120132718Skan      if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14121132718Skan	{
14122132718Skan	  /* A reference to another DIE.
14123132718Skan	     Make sure that it will get emitted.  */
14124132718Skan	  prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14125132718Skan	}
14126169689Skan      /* Set the string's refcount to 0 so that prune_unused_types_mark
14127169689Skan	 accounts properly for it.  */
14128169689Skan      if (AT_class (a) == dw_val_class_str)
14129169689Skan	a->dw_attr_val.v.val_str->refcount = 0;
14130132718Skan    }
14131132718Skan}
14132132718Skan
14133132718Skan
14134132718Skan/* Mark DIE as being used.  If DOKIDS is true, then walk down
14135132718Skan   to DIE's children.  */
14136132718Skan
14137132718Skanstatic void
14138132718Skanprune_unused_types_mark (dw_die_ref die, int dokids)
14139132718Skan{
14140132718Skan  dw_die_ref c;
14141132718Skan
14142132718Skan  if (die->die_mark == 0)
14143132718Skan    {
14144132718Skan      /* We haven't done this node yet.  Mark it as used.  */
14145132718Skan      die->die_mark = 1;
14146132718Skan
14147132718Skan      /* We also have to mark its parents as used.
14148132718Skan	 (But we don't want to mark our parents' kids due to this.)  */
14149132718Skan      if (die->die_parent)
14150132718Skan	prune_unused_types_mark (die->die_parent, 0);
14151132718Skan
14152132718Skan      /* Mark any referenced nodes.  */
14153132718Skan      prune_unused_types_walk_attribs (die);
14154132718Skan
14155132718Skan      /* If this node is a specification,
14156132718Skan         also mark the definition, if it exists.  */
14157132718Skan      if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14158132718Skan        prune_unused_types_mark (die->die_definition, 1);
14159132718Skan    }
14160132718Skan
14161132718Skan  if (dokids && die->die_mark != 2)
14162132718Skan    {
14163132718Skan      /* We need to walk the children, but haven't done so yet.
14164132718Skan	 Remember that we've walked the kids.  */
14165132718Skan      die->die_mark = 2;
14166132718Skan
14167169689Skan      /* If this is an array type, we need to make sure our
14168169689Skan	 kids get marked, even if they're types.  */
14169169689Skan      if (die->die_tag == DW_TAG_array_type)
14170169689Skan	FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14171169689Skan      else
14172169689Skan	FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14173132718Skan    }
14174132718Skan}
14175132718Skan
14176132718Skan
14177132718Skan/* Walk the tree DIE and mark types that we actually use.  */
14178132718Skan
14179132718Skanstatic void
14180132718Skanprune_unused_types_walk (dw_die_ref die)
14181132718Skan{
14182132718Skan  dw_die_ref c;
14183132718Skan
14184132718Skan  /* Don't do anything if this node is already marked.  */
14185132718Skan  if (die->die_mark)
14186132718Skan    return;
14187132718Skan
14188132718Skan  switch (die->die_tag) {
14189132718Skan  case DW_TAG_const_type:
14190132718Skan  case DW_TAG_packed_type:
14191132718Skan  case DW_TAG_pointer_type:
14192132718Skan  case DW_TAG_reference_type:
14193132718Skan  case DW_TAG_volatile_type:
14194132718Skan  case DW_TAG_typedef:
14195132718Skan  case DW_TAG_array_type:
14196132718Skan  case DW_TAG_structure_type:
14197132718Skan  case DW_TAG_union_type:
14198132718Skan  case DW_TAG_class_type:
14199132718Skan  case DW_TAG_friend:
14200132718Skan  case DW_TAG_variant_part:
14201132718Skan  case DW_TAG_enumeration_type:
14202132718Skan  case DW_TAG_subroutine_type:
14203132718Skan  case DW_TAG_string_type:
14204132718Skan  case DW_TAG_set_type:
14205132718Skan  case DW_TAG_subrange_type:
14206132718Skan  case DW_TAG_ptr_to_member_type:
14207132718Skan  case DW_TAG_file_type:
14208169689Skan    if (die->die_perennial_p)
14209169689Skan      break;
14210169689Skan
14211132718Skan    /* It's a type node --- don't mark it.  */
14212132718Skan    return;
14213132718Skan
14214132718Skan  default:
14215132718Skan    /* Mark everything else.  */
14216132718Skan    break;
14217132718Skan  }
14218132718Skan
14219132718Skan  die->die_mark = 1;
14220132718Skan
14221132718Skan  /* Now, mark any dies referenced from here.  */
14222132718Skan  prune_unused_types_walk_attribs (die);
14223132718Skan
14224132718Skan  /* Mark children.  */
14225169689Skan  FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14226132718Skan}
14227132718Skan
14228169689Skan/* Increment the string counts on strings referred to from DIE's
14229169689Skan   attributes.  */
14230132718Skan
14231169689Skanstatic void
14232169689Skanprune_unused_types_update_strings (dw_die_ref die)
14233169689Skan{
14234169689Skan  dw_attr_ref a;
14235169689Skan  unsigned ix;
14236169689Skan
14237169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14238169689Skan    if (AT_class (a) == dw_val_class_str)
14239169689Skan      {
14240169689Skan	struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14241169689Skan	s->refcount++;
14242169689Skan	/* Avoid unnecessarily putting strings that are used less than
14243169689Skan	   twice in the hash table.  */
14244169689Skan	if (s->refcount
14245169689Skan	    == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14246169689Skan	  {
14247169689Skan	    void ** slot;
14248169689Skan	    slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14249169689Skan					     htab_hash_string (s->str),
14250169689Skan					     INSERT);
14251169689Skan	    gcc_assert (*slot == NULL);
14252169689Skan	    *slot = s;
14253169689Skan	  }
14254169689Skan      }
14255169689Skan}
14256169689Skan
14257132718Skan/* Remove from the tree DIE any dies that aren't marked.  */
14258132718Skan
14259132718Skanstatic void
14260132718Skanprune_unused_types_prune (dw_die_ref die)
14261132718Skan{
14262169689Skan  dw_die_ref c;
14263132718Skan
14264169689Skan  gcc_assert (die->die_mark);
14265169689Skan  prune_unused_types_update_strings (die);
14266169689Skan
14267169689Skan  if (! die->die_child)
14268169689Skan    return;
14269169689Skan
14270169689Skan  c = die->die_child;
14271169689Skan  do {
14272169689Skan    dw_die_ref prev = c;
14273169689Skan    for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14274169689Skan      if (c == die->die_child)
14275132718Skan	{
14276169689Skan	  /* No marked children between 'prev' and the end of the list.  */
14277169689Skan	  if (prev == c)
14278169689Skan	    /* No marked children at all.  */
14279169689Skan	    die->die_child = NULL;
14280132718Skan	  else
14281169689Skan	    {
14282169689Skan	      prev->die_sib = c->die_sib;
14283169689Skan	      die->die_child = prev;
14284169689Skan	    }
14285169689Skan	  return;
14286132718Skan	}
14287169689Skan
14288169689Skan    if (c != prev->die_sib)
14289169689Skan      prev->die_sib = c;
14290169689Skan    prune_unused_types_prune (c);
14291169689Skan  } while (c != die->die_child);
14292132718Skan}
14293132718Skan
14294132718Skan
14295132718Skan/* Remove dies representing declarations that we never use.  */
14296132718Skan
14297132718Skanstatic void
14298132718Skanprune_unused_types (void)
14299132718Skan{
14300132718Skan  unsigned int i;
14301132718Skan  limbo_die_node *node;
14302260396Spfg  pubname_ref pub;
14303132718Skan
14304169689Skan#if ENABLE_ASSERT_CHECKING
14305169689Skan  /* All the marks should already be clear.  */
14306169689Skan  verify_marks_clear (comp_unit_die);
14307132718Skan  for (node = limbo_die_list; node; node = node->next)
14308169689Skan    verify_marks_clear (node->die);
14309169689Skan#endif /* ENABLE_ASSERT_CHECKING */
14310132718Skan
14311132718Skan  /* Set the mark on nodes that are actually used.  */
14312132718Skan  prune_unused_types_walk (comp_unit_die);
14313132718Skan  for (node = limbo_die_list; node; node = node->next)
14314132718Skan    prune_unused_types_walk (node->die);
14315132718Skan
14316132718Skan  /* Also set the mark on nodes referenced from the
14317132718Skan     pubname_table or arange_table.  */
14318260396Spfg  for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14319260396Spfg    prune_unused_types_mark (pub->die, 1);
14320132718Skan  for (i = 0; i < arange_table_in_use; i++)
14321132718Skan    prune_unused_types_mark (arange_table[i], 1);
14322132718Skan
14323169689Skan  /* Get rid of nodes that aren't marked; and update the string counts.  */
14324169689Skan  if (debug_str_hash)
14325169689Skan    htab_empty (debug_str_hash);
14326132718Skan  prune_unused_types_prune (comp_unit_die);
14327132718Skan  for (node = limbo_die_list; node; node = node->next)
14328132718Skan    prune_unused_types_prune (node->die);
14329132718Skan
14330132718Skan  /* Leave the marks clear.  */
14331132718Skan  prune_unmark_dies (comp_unit_die);
14332132718Skan  for (node = limbo_die_list; node; node = node->next)
14333132718Skan    prune_unmark_dies (node->die);
14334132718Skan}
14335132718Skan
14336169689Skan/* Set the parameter to true if there are any relative pathnames in
14337169689Skan   the file table.  */
14338169689Skanstatic int
14339169689Skanfile_table_relative_p (void ** slot, void *param)
14340169689Skan{
14341169689Skan  bool *p = param;
14342169689Skan  struct dwarf_file_data *d = *slot;
14343169689Skan  if (d->emitted_number && d->filename[0] != DIR_SEPARATOR)
14344169689Skan    {
14345169689Skan      *p = true;
14346169689Skan      return 0;
14347169689Skan    }
14348169689Skan  return 1;
14349169689Skan}
14350169689Skan
1435150397Sobrien/* Output stuff that dwarf requires at the end of every file,
1435250397Sobrien   and generate the DWARF-2 debugging info.  */
1435350397Sobrien
1435490075Sobrienstatic void
14355132718Skandwarf2out_finish (const char *filename)
1435650397Sobrien{
1435750397Sobrien  limbo_die_node *node, *next_node;
1435890075Sobrien  dw_die_ref die = 0;
1435950397Sobrien
14360132718Skan  /* Add the name for the main input file now.  We delayed this from
14361132718Skan     dwarf2out_init to avoid complications with PCH.  */
14362132718Skan  add_name_attribute (comp_unit_die, filename);
14363132718Skan  if (filename[0] != DIR_SEPARATOR)
14364132718Skan    add_comp_dir_attribute (comp_unit_die);
14365132718Skan  else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14366132718Skan    {
14367169689Skan      bool p = false;
14368169689Skan      htab_traverse (file_table, file_table_relative_p, &p);
14369169689Skan      if (p)
14370169689Skan	add_comp_dir_attribute (comp_unit_die);
14371132718Skan    }
14372132718Skan
1437350397Sobrien  /* Traverse the limbo die list, and add parent/child links.  The only
1437450397Sobrien     dies without parents that should be here are concrete instances of
1437550397Sobrien     inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
1437650397Sobrien     For concrete instances, we can get the parent die from the abstract
1437750397Sobrien     instance.  */
1437850397Sobrien  for (node = limbo_die_list; node; node = next_node)
1437950397Sobrien    {
1438050397Sobrien      next_node = node->next;
1438150397Sobrien      die = node->die;
1438250397Sobrien
1438350397Sobrien      if (die->die_parent == NULL)
1438450397Sobrien	{
1438590075Sobrien	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
1438690075Sobrien
1438790075Sobrien	  if (origin)
1438890075Sobrien	    add_child_die (origin->die_parent, die);
1438950397Sobrien	  else if (die == comp_unit_die)
1439090075Sobrien	    ;
1439190075Sobrien	  else if (errorcount > 0 || sorrycount > 0)
1439290075Sobrien	    /* It's OK to be confused by errors in the input.  */
1439390075Sobrien	    add_child_die (comp_unit_die, die);
14394169689Skan	  else
1439590075Sobrien	    {
1439690075Sobrien	      /* In certain situations, the lexical block containing a
1439790075Sobrien		 nested function can be optimized away, which results
1439890075Sobrien		 in the nested function die being orphaned.  Likewise
1439990075Sobrien		 with the return type of that nested function.  Force
14400169689Skan		 this to be a child of the containing function.
14401169689Skan
14402169689Skan		 It may happen that even the containing function got fully
14403169689Skan		 inlined and optimized out.  In that case we are lost and
14404169689Skan		 assign the empty child.  This should not be big issue as
14405169689Skan		 the function is likely unreachable too.  */
14406169689Skan	      tree context = NULL_TREE;
14407169689Skan
14408169689Skan	      gcc_assert (node->created_for);
14409169689Skan
14410169689Skan	      if (DECL_P (node->created_for))
14411169689Skan		context = DECL_CONTEXT (node->created_for);
14412169689Skan	      else if (TYPE_P (node->created_for))
14413169689Skan		context = TYPE_CONTEXT (node->created_for);
14414169689Skan
14415171825Skan	      gcc_assert (context
14416171825Skan			  && (TREE_CODE (context) == FUNCTION_DECL
14417171825Skan			      || TREE_CODE (context) == NAMESPACE_DECL));
14418169689Skan
1441990075Sobrien	      origin = lookup_decl_die (context);
14420169689Skan	      if (origin)
14421169689Skan	        add_child_die (origin, die);
14422169689Skan	      else
14423169689Skan	        add_child_die (comp_unit_die, die);
1442490075Sobrien	    }
1442550397Sobrien	}
1442650397Sobrien    }
1442750397Sobrien
1442890075Sobrien  limbo_die_list = NULL;
1442990075Sobrien
1443052284Sobrien  /* Walk through the list of incomplete types again, trying once more to
1443152284Sobrien     emit full debugging info for them.  */
1443252284Sobrien  retry_incomplete_types ();
1443352284Sobrien
14434132718Skan  if (flag_eliminate_unused_debug_types)
14435132718Skan    prune_unused_types ();
14436132718Skan
1443790075Sobrien  /* Generate separate CUs for each of the include files we've seen.
1443890075Sobrien     They will go into limbo_die_list.  */
1443990075Sobrien  if (flag_eliminate_dwarf2_dups)
1444090075Sobrien    break_out_includes (comp_unit_die);
1444190075Sobrien
1444290075Sobrien  /* Traverse the DIE's and add add sibling attributes to those DIE's
1444350397Sobrien     that have children.  */
1444450397Sobrien  add_sibling_attributes (comp_unit_die);
1444590075Sobrien  for (node = limbo_die_list; node; node = node->next)
1444690075Sobrien    add_sibling_attributes (node->die);
1444750397Sobrien
1444850397Sobrien  /* Output a terminator label for the .text section.  */
14449169689Skan  switch_to_section (text_section);
14450169689Skan  targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14451169689Skan  if (flag_reorder_blocks_and_partition)
1445250397Sobrien    {
14453169689Skan      switch_to_section (unlikely_text_section ());
14454169689Skan      targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
1445590075Sobrien    }
1445650397Sobrien
1445790075Sobrien  /* We can only use the low/high_pc attributes if all of the code was
1445890075Sobrien     in .text.  */
14459169689Skan  if (!have_multiple_function_sections)
1446090075Sobrien    {
1446190075Sobrien      add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
1446290075Sobrien      add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
1446350397Sobrien    }
1446450397Sobrien
1446590075Sobrien  /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
1446690075Sobrien     "base address".  Use zero so that these addresses become absolute.  */
1446790075Sobrien  else if (have_location_lists || ranges_table_in_use)
1446890075Sobrien    add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
1446990075Sobrien
14470169689Skan  /* Output location list section if necessary.  */
14471169689Skan  if (have_location_lists)
14472169689Skan    {
14473169689Skan      /* Output the location lists info.  */
14474169689Skan      switch_to_section (debug_loc_section);
14475169689Skan      ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14476169689Skan				   DEBUG_LOC_SECTION_LABEL, 0);
14477169689Skan      ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14478169689Skan      output_location_lists (die);
14479169689Skan    }
14480169689Skan
1448190075Sobrien  if (debug_info_level >= DINFO_LEVEL_NORMAL)
14482169689Skan    add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14483169689Skan		    debug_line_section_label);
1448490075Sobrien
1448590075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14486169689Skan    add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
1448790075Sobrien
1448890075Sobrien  /* Output all of the compilation units.  We put the main one last so that
1448990075Sobrien     the offsets are available to output_pubnames.  */
1449090075Sobrien  for (node = limbo_die_list; node; node = node->next)
14491117395Skan    output_comp_unit (node->die, 0);
1449290075Sobrien
14493117395Skan  output_comp_unit (comp_unit_die, 0);
1449490075Sobrien
1449550397Sobrien  /* Output the abbreviation table.  */
14496169689Skan  switch_to_section (debug_abbrev_section);
1449750397Sobrien  output_abbrev_section ();
1449850397Sobrien
1449990075Sobrien  /* Output public names table if necessary.  */
14500260396Spfg  if (!VEC_empty (pubname_entry, pubname_table))
1450150397Sobrien    {
14502169689Skan      switch_to_section (debug_pubnames_section);
14503260396Spfg      output_pubnames (pubname_table);
1450450397Sobrien    }
1450550397Sobrien
14506260396Spfg#ifdef DEBUG_PUBTYPES_SECTION
14507260396Spfg  /* Output public types table if necessary.  */
14508260396Spfg  if (!VEC_empty (pubname_entry, pubtype_table))
14509260396Spfg    {
14510260396Spfg      switch_to_section (debug_pubtypes_section);
14511260396Spfg      output_pubnames (pubtype_table);
14512260396Spfg    }
14513260396Spfg#endif
14514260396Spfg
1451590075Sobrien  /* Output the address range information.  We only put functions in the arange
1451690075Sobrien     table, so don't write it out if we don't have any.  */
1451750397Sobrien  if (fde_table_in_use)
1451850397Sobrien    {
14519169689Skan      switch_to_section (debug_aranges_section);
1452050397Sobrien      output_aranges ();
1452150397Sobrien    }
1452290075Sobrien
1452390075Sobrien  /* Output ranges section if necessary.  */
1452490075Sobrien  if (ranges_table_in_use)
1452590075Sobrien    {
14526169689Skan      switch_to_section (debug_ranges_section);
1452790075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
1452890075Sobrien      output_ranges ();
1452990075Sobrien    }
1453090075Sobrien
14531169689Skan  /* Output the source line correspondence table.  We must do this
14532169689Skan     even if there is no line information.  Otherwise, on an empty
14533169689Skan     translation unit, we will generate a present, but empty,
14534169689Skan     .debug_info section.  IRIX 6.5 `nm' will then complain when
14535169689Skan     examining the file.  This is done late so that any filenames
14536169689Skan     used by the debug_info section are marked as 'used'.  */
14537169689Skan  if (! DWARF2_ASM_LINE_DEBUG_INFO)
14538169689Skan    {
14539169689Skan      switch_to_section (debug_line_section);
14540169689Skan      output_line_info ();
14541169689Skan    }
14542169689Skan
14543169689Skan  /* Have to end the macro section.  */
1454490075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14545117395Skan    {
14546169689Skan      switch_to_section (debug_macinfo_section);
14547117395Skan      dw2_asm_output_data (1, 0, "End compilation unit");
1454890075Sobrien    }
1454990075Sobrien
1455090075Sobrien  /* If we emitted any DW_FORM_strp form attribute, output the string
1455190075Sobrien     table too.  */
1455290075Sobrien  if (debug_str_hash)
14553132718Skan    htab_traverse (debug_str_hash, output_indirect_string, NULL);
1455450397Sobrien}
14555117395Skan#else
14556117395Skan
14557117395Skan/* This should never be used, but its address is needed for comparisons.  */
14558117395Skanconst struct gcc_debug_hooks dwarf2_debug_hooks;
14559117395Skan
14560117395Skan#endif /* DWARF2_DEBUGGING_INFO */
14561117395Skan
14562117395Skan#include "gt-dwarf2out.h"
14563