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
2196260919Spfg/* APPLE LOCAL begin for-fsf-4_4 5480287 */ \
2197260919Spfg      && flag_exceptions
2198260919Spfg/* APPLE LOCAL end for-fsf-4_4 5480287 */ \
2199169689Skan      && for_eh)
2200169689Skan    for (i = 0; i < fde_table_in_use; i++)
2201169689Skan      if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2202169689Skan          && !fde_table[i].uses_eh_lsda
2203169689Skan	  && ! DECL_WEAK (fde_table[i].decl))
2204169689Skan	targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2205169689Skan				      for_eh, /* empty */ 1);
2206169689Skan
2207117395Skan  /* If we don't have any functions we'll want to unwind out of, don't
2208117395Skan     emit any EH unwind information.  Note that if exceptions aren't
2209117395Skan     enabled, we won't have collected nothrow information, and if we
2210117395Skan     asked for asynchronous tables, we always want this info.  */
221190075Sobrien  if (for_eh)
221290075Sobrien    {
2213117395Skan      bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
221450397Sobrien
221590075Sobrien      for (i = 0; i < fde_table_in_use; i++)
221690075Sobrien	if (fde_table[i].uses_eh_lsda)
2217117395Skan	  any_eh_needed = any_lsda_needed = true;
2218169689Skan        else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2219169689Skan	  any_eh_needed = true;
2220132718Skan	else if (! fde_table[i].nothrow
2221132718Skan		 && ! fde_table[i].all_throwers_are_sibcalls)
2222117395Skan	  any_eh_needed = true;
222350397Sobrien
222490075Sobrien      if (! any_eh_needed)
222590075Sobrien	return;
222690075Sobrien    }
222790075Sobrien
222850397Sobrien  /* We're going to be generating comments, so turn on app.  */
222950397Sobrien  if (flag_debug_asm)
223050397Sobrien    app_enable ();
223150397Sobrien
223250397Sobrien  if (for_eh)
2233169689Skan    switch_to_eh_frame_section ();
223450397Sobrien  else
2235169689Skan    {
2236169689Skan      if (!debug_frame_section)
2237169689Skan	debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2238169689Skan					   SECTION_DEBUG, NULL);
2239169689Skan      switch_to_section (debug_frame_section);
2240169689Skan    }
224150397Sobrien
224290075Sobrien  ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
224390075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
224490075Sobrien
224590075Sobrien  /* Output the CIE.  */
224650397Sobrien  ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
224750397Sobrien  ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2248169689Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2249169689Skan    dw2_asm_output_data (4, 0xffffffff,
2250169689Skan      "Initial length escape value indicating 64-bit DWARF extension");
225190075Sobrien  dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
225290075Sobrien			"Length of Common Information Entry");
225350397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l1);
225450397Sobrien
225590075Sobrien  /* Now that the CIE pointer is PC-relative for EH,
225690075Sobrien     use 0 to identify the CIE.  */
225790075Sobrien  dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2258169689Skan		       (for_eh ? 0 : DWARF_CIE_ID),
225990075Sobrien		       "CIE Identifier Tag");
226050397Sobrien
226190075Sobrien  dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
226250397Sobrien
226390075Sobrien  augmentation[0] = 0;
226490075Sobrien  augmentation_size = 0;
226590075Sobrien  if (for_eh)
226650397Sobrien    {
226790075Sobrien      char *p;
226850397Sobrien
226990075Sobrien      /* Augmentation:
227090075Sobrien	 z	Indicates that a uleb128 is present to size the
2271132718Skan		augmentation section.
227290075Sobrien	 L	Indicates the encoding (and thus presence) of
227390075Sobrien		an LSDA pointer in the FDE augmentation.
227490075Sobrien	 R	Indicates a non-default pointer encoding for
227590075Sobrien		FDE code pointers.
227690075Sobrien	 P	Indicates the presence of an encoding + language
227790075Sobrien		personality routine in the CIE augmentation.  */
227850397Sobrien
227990075Sobrien      fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
228090075Sobrien      per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
228190075Sobrien      lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
228290075Sobrien
228390075Sobrien      p = augmentation + 1;
228490075Sobrien      if (eh_personality_libfunc)
228550397Sobrien	{
228690075Sobrien	  *p++ = 'P';
228790075Sobrien	  augmentation_size += 1 + size_of_encoded_value (per_encoding);
228850397Sobrien	}
228990075Sobrien      if (any_lsda_needed)
229050397Sobrien	{
229190075Sobrien	  *p++ = 'L';
229290075Sobrien	  augmentation_size += 1;
229350397Sobrien	}
229490075Sobrien      if (fde_encoding != DW_EH_PE_absptr)
229590075Sobrien	{
229690075Sobrien	  *p++ = 'R';
229790075Sobrien	  augmentation_size += 1;
229890075Sobrien	}
229990075Sobrien      if (p > augmentation + 1)
230090075Sobrien	{
230190075Sobrien	  augmentation[0] = 'z';
2302117395Skan	  *p = '\0';
230390075Sobrien	}
230450397Sobrien
230590075Sobrien      /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
230690075Sobrien      if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
230790075Sobrien	{
230890075Sobrien	  int offset = (  4		/* Length */
230990075Sobrien			+ 4		/* CIE Id */
231090075Sobrien			+ 1		/* CIE version */
231190075Sobrien			+ strlen (augmentation) + 1	/* Augmentation */
231290075Sobrien			+ size_of_uleb128 (1)		/* Code alignment */
231390075Sobrien			+ size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
231490075Sobrien			+ 1		/* RA column */
231590075Sobrien			+ 1		/* Augmentation size */
231690075Sobrien			+ 1		/* Personality encoding */ );
231790075Sobrien	  int pad = -offset & (PTR_SIZE - 1);
231890075Sobrien
231990075Sobrien	  augmentation_size += pad;
232090075Sobrien
232190075Sobrien	  /* Augmentations should be small, so there's scarce need to
232290075Sobrien	     iterate for a solution.  Die if we exceed one uleb128 byte.  */
2323169689Skan	  gcc_assert (size_of_uleb128 (augmentation_size) == 1);
232490075Sobrien	}
232550397Sobrien    }
232650397Sobrien
232790075Sobrien  dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
232890075Sobrien  dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
232990075Sobrien  dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
233090075Sobrien			       "CIE Data Alignment Factor");
233150397Sobrien
2332169689Skan  return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2333169689Skan  if (DW_CIE_VERSION == 1)
2334169689Skan    dw2_asm_output_data (1, return_reg, "CIE RA Column");
2335169689Skan  else
2336169689Skan    dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2337169689Skan
233890075Sobrien  if (augmentation[0])
233990075Sobrien    {
234090075Sobrien      dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
234190075Sobrien      if (eh_personality_libfunc)
234290075Sobrien	{
234390075Sobrien	  dw2_asm_output_data (1, per_encoding, "Personality (%s)",
234490075Sobrien			       eh_data_format_name (per_encoding));
234590075Sobrien	  dw2_asm_output_encoded_addr_rtx (per_encoding,
2346169689Skan					   eh_personality_libfunc,
2347169689Skan					   true, NULL);
234890075Sobrien	}
234950397Sobrien
235090075Sobrien      if (any_lsda_needed)
235190075Sobrien	dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
235290075Sobrien			     eh_data_format_name (lsda_encoding));
235350397Sobrien
235490075Sobrien      if (fde_encoding != DW_EH_PE_absptr)
235590075Sobrien	dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
235690075Sobrien			     eh_data_format_name (fde_encoding));
235790075Sobrien    }
235850397Sobrien
235950397Sobrien  for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
236090075Sobrien    output_cfi (cfi, NULL, for_eh);
236150397Sobrien
236250397Sobrien  /* Pad the CIE out to an address sized boundary.  */
2363117395Skan  ASM_OUTPUT_ALIGN (asm_out_file,
236490075Sobrien		    floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
236550397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l2);
236650397Sobrien
236750397Sobrien  /* Loop through all of the FDE's.  */
236890075Sobrien  for (i = 0; i < fde_table_in_use; i++)
236950397Sobrien    {
237050397Sobrien      fde = &fde_table[i];
237150397Sobrien
237290075Sobrien      /* Don't emit EH unwind info for leaf functions that don't need it.  */
2373117395Skan      if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2374117395Skan	  && (fde->nothrow || fde->all_throwers_are_sibcalls)
2375169689Skan	  && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2376117395Skan	  && !fde->uses_eh_lsda)
237790075Sobrien	continue;
237890075Sobrien
2379169689Skan      targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2380169689Skan      targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
238190075Sobrien      ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
238290075Sobrien      ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2383169689Skan      if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2384169689Skan	dw2_asm_output_data (4, 0xffffffff,
2385169689Skan			     "Initial length escape value indicating 64-bit DWARF extension");
238690075Sobrien      dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
238790075Sobrien			    "FDE Length");
238890075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, l1);
238990075Sobrien
239050397Sobrien      if (for_eh)
239190075Sobrien	dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
239250397Sobrien      else
239390075Sobrien	dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2394169689Skan			       debug_frame_section, "FDE CIE offset");
239550397Sobrien
239650397Sobrien      if (for_eh)
239790075Sobrien	{
2398169689Skan	  rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2399169689Skan	  SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
240090075Sobrien	  dw2_asm_output_encoded_addr_rtx (fde_encoding,
2401169689Skan					   sym_ref,
2402169689Skan					   false,
2403169689Skan					   "FDE initial location");
2404169689Skan	  if (fde->dw_fde_switched_sections)
2405169689Skan	    {
2406169689Skan	      rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2407169689Skan				      fde->dw_fde_unlikely_section_label);
2408169689Skan	      rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2409169689Skan				      fde->dw_fde_hot_section_label);
2410169689Skan	      SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2411169689Skan	      SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2412169689Skan	      dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2413169689Skan					       "FDE initial location");
2414169689Skan	      dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2415169689Skan				    fde->dw_fde_hot_section_end_label,
2416169689Skan				    fde->dw_fde_hot_section_label,
2417169689Skan				    "FDE address range");
2418169689Skan	      dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2419169689Skan					       "FDE initial location");
2420169689Skan	      dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2421169689Skan				    fde->dw_fde_unlikely_section_end_label,
2422169689Skan				    fde->dw_fde_unlikely_section_label,
2423169689Skan				    "FDE address range");
2424169689Skan	    }
2425169689Skan	  else
2426169689Skan	    dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2427169689Skan				  fde->dw_fde_end, fde->dw_fde_begin,
2428169689Skan				  "FDE address range");
242990075Sobrien	}
243050397Sobrien      else
243190075Sobrien	{
243290075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
243390075Sobrien			       "FDE initial location");
2434169689Skan	  if (fde->dw_fde_switched_sections)
2435169689Skan	    {
2436169689Skan	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2437169689Skan				   fde->dw_fde_hot_section_label,
2438169689Skan				   "FDE initial location");
2439169689Skan	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2440169689Skan				    fde->dw_fde_hot_section_end_label,
2441169689Skan				    fde->dw_fde_hot_section_label,
2442169689Skan				    "FDE address range");
2443169689Skan	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2444169689Skan				   fde->dw_fde_unlikely_section_label,
2445169689Skan				   "FDE initial location");
2446169689Skan	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2447169689Skan				    fde->dw_fde_unlikely_section_end_label,
2448169689Skan				    fde->dw_fde_unlikely_section_label,
2449169689Skan				    "FDE address range");
2450169689Skan	    }
2451169689Skan	  else
2452169689Skan	    dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2453169689Skan				  fde->dw_fde_end, fde->dw_fde_begin,
2454169689Skan				  "FDE address range");
245590075Sobrien	}
245650397Sobrien
245790075Sobrien      if (augmentation[0])
245890075Sobrien	{
245990075Sobrien	  if (any_lsda_needed)
246090075Sobrien	    {
246190075Sobrien	      int size = size_of_encoded_value (lsda_encoding);
246250397Sobrien
246390075Sobrien	      if (lsda_encoding == DW_EH_PE_aligned)
246490075Sobrien		{
246590075Sobrien		  int offset = (  4		/* Length */
246690075Sobrien				+ 4		/* CIE offset */
246790075Sobrien				+ 2 * size_of_encoded_value (fde_encoding)
246890075Sobrien				+ 1		/* Augmentation size */ );
246990075Sobrien		  int pad = -offset & (PTR_SIZE - 1);
247050397Sobrien
247190075Sobrien		  size += pad;
2472169689Skan		  gcc_assert (size_of_uleb128 (size) == 1);
247390075Sobrien		}
247450397Sobrien
247590075Sobrien	      dw2_asm_output_data_uleb128 (size, "Augmentation size");
247690075Sobrien
247790075Sobrien	      if (fde->uses_eh_lsda)
2478132718Skan		{
2479132718Skan		  ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
248090075Sobrien					       fde->funcdef_number);
2481132718Skan		  dw2_asm_output_encoded_addr_rtx (
248290075Sobrien			lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2483169689Skan			false, "Language Specific Data Area");
2484132718Skan		}
248590075Sobrien	      else
248690075Sobrien		{
248790075Sobrien		  if (lsda_encoding == DW_EH_PE_aligned)
248890075Sobrien		    ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
248990075Sobrien		  dw2_asm_output_data
249090075Sobrien		    (size_of_encoded_value (lsda_encoding), 0,
249190075Sobrien		     "Language Specific Data Area (none)");
249290075Sobrien		}
249390075Sobrien	    }
249490075Sobrien	  else
249590075Sobrien	    dw2_asm_output_data_uleb128 (0, "Augmentation size");
249690075Sobrien	}
249790075Sobrien
249850397Sobrien      /* Loop through the Call Frame Instructions associated with
249950397Sobrien	 this FDE.  */
250050397Sobrien      fde->dw_fde_current_label = fde->dw_fde_begin;
250150397Sobrien      for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
250290075Sobrien	output_cfi (cfi, fde, for_eh);
250350397Sobrien
250450397Sobrien      /* Pad the FDE out to an address sized boundary.  */
2505117395Skan      ASM_OUTPUT_ALIGN (asm_out_file,
2506132718Skan			floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
250750397Sobrien      ASM_OUTPUT_LABEL (asm_out_file, l2);
250850397Sobrien    }
250990075Sobrien
2510117395Skan  if (for_eh && targetm.terminate_dw2_eh_frame_info)
251190075Sobrien    dw2_asm_output_data (4, 0, "End of Table");
251250397Sobrien#ifdef MIPS_DEBUGGING_INFO
251350397Sobrien  /* Work around Irix 6 assembler bug whereby labels at the end of a section
251450397Sobrien     get a value of 0.  Putting .align 0 after the label fixes it.  */
251550397Sobrien  ASM_OUTPUT_ALIGN (asm_out_file, 0);
251650397Sobrien#endif
251750397Sobrien
251850397Sobrien  /* Turn off app to make assembly quicker.  */
251950397Sobrien  if (flag_debug_asm)
252050397Sobrien    app_disable ();
252150397Sobrien}
252250397Sobrien
252350397Sobrien/* Output a marker (i.e. a label) for the beginning of a function, before
252450397Sobrien   the prologue.  */
252550397Sobrien
252650397Sobrienvoid
2527132718Skandwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2528132718Skan			  const char *file ATTRIBUTE_UNUSED)
252950397Sobrien{
253050397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
2531169689Skan  char * dup_label;
253290075Sobrien  dw_fde_ref fde;
253350397Sobrien
2534169689Skan  current_function_func_begin_label = NULL;
253550397Sobrien
2536169689Skan#ifdef TARGET_UNWIND_INFO
253790075Sobrien  /* ??? current_function_func_begin_label is also used by except.c
253890075Sobrien     for call-site information.  We must emit this label if it might
253990075Sobrien     be used.  */
254090075Sobrien  if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
254190075Sobrien      && ! dwarf2out_do_frame ())
254290075Sobrien    return;
254390075Sobrien#else
254490075Sobrien  if (! dwarf2out_do_frame ())
254590075Sobrien    return;
254690075Sobrien#endif
254790075Sobrien
2548169689Skan  switch_to_section (function_section (current_function_decl));
254950397Sobrien  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2550117395Skan			       current_function_funcdef_no);
255190075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2552117395Skan			  current_function_funcdef_no);
2553169689Skan  dup_label = xstrdup (label);
2554169689Skan  current_function_func_begin_label = dup_label;
255550397Sobrien
2556169689Skan#ifdef TARGET_UNWIND_INFO
255790075Sobrien  /* We can elide the fde allocation if we're not emitting debug info.  */
255890075Sobrien  if (! dwarf2out_do_frame ())
255990075Sobrien    return;
256090075Sobrien#endif
256190075Sobrien
256250397Sobrien  /* Expand the fde table if necessary.  */
256350397Sobrien  if (fde_table_in_use == fde_table_allocated)
256450397Sobrien    {
256550397Sobrien      fde_table_allocated += FDE_TABLE_INCREMENT;
2566132718Skan      fde_table = ggc_realloc (fde_table,
2567132718Skan			       fde_table_allocated * sizeof (dw_fde_node));
2568132718Skan      memset (fde_table + fde_table_in_use, 0,
2569132718Skan	      FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
257050397Sobrien    }
257150397Sobrien
257250397Sobrien  /* Record the FDE associated with this function.  */
257350397Sobrien  current_funcdef_fde = fde_table_in_use;
257450397Sobrien
257550397Sobrien  /* Add the new FDE at the end of the fde_table.  */
257650397Sobrien  fde = &fde_table[fde_table_in_use++];
2577169689Skan  fde->decl = current_function_decl;
2578169689Skan  fde->dw_fde_begin = dup_label;
2579169689Skan  fde->dw_fde_current_label = dup_label;
2580169689Skan  fde->dw_fde_hot_section_label = NULL;
2581169689Skan  fde->dw_fde_hot_section_end_label = NULL;
2582169689Skan  fde->dw_fde_unlikely_section_label = NULL;
2583169689Skan  fde->dw_fde_unlikely_section_end_label = NULL;
2584169689Skan  fde->dw_fde_switched_sections = false;
258550397Sobrien  fde->dw_fde_end = NULL;
258650397Sobrien  fde->dw_fde_cfi = NULL;
2587117395Skan  fde->funcdef_number = current_function_funcdef_no;
2588169689Skan  fde->nothrow = TREE_NOTHROW (current_function_decl);
258990075Sobrien  fde->uses_eh_lsda = cfun->uses_eh_lsda;
2590117395Skan  fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
259150397Sobrien
259250397Sobrien  args_size = old_args_size = 0;
259390075Sobrien
259490075Sobrien  /* We only want to output line number information for the genuine dwarf2
259590075Sobrien     prologue case, not the eh frame case.  */
259690075Sobrien#ifdef DWARF2_DEBUGGING_INFO
259790075Sobrien  if (file)
259890075Sobrien    dwarf2out_source_line (line, file);
259990075Sobrien#endif
260050397Sobrien}
260150397Sobrien
260250397Sobrien/* Output a marker (i.e. a label) for the absolute end of the generated code
260350397Sobrien   for a function definition.  This gets called *after* the epilogue code has
260450397Sobrien   been generated.  */
260550397Sobrien
260650397Sobrienvoid
2607132718Skandwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2608132718Skan			const char *file ATTRIBUTE_UNUSED)
260950397Sobrien{
261050397Sobrien  dw_fde_ref fde;
261150397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
261250397Sobrien
261350397Sobrien  /* Output a label to mark the endpoint of the code generated for this
261490075Sobrien     function.  */
2615117395Skan  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2616117395Skan			       current_function_funcdef_no);
261750397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, label);
261850397Sobrien  fde = &fde_table[fde_table_in_use - 1];
261950397Sobrien  fde->dw_fde_end = xstrdup (label);
262050397Sobrien}
262150397Sobrien
262250397Sobrienvoid
2623132718Skandwarf2out_frame_init (void)
262450397Sobrien{
262550397Sobrien  /* Allocate the initial hunk of the fde_table.  */
2626132718Skan  fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
262750397Sobrien  fde_table_allocated = FDE_TABLE_INCREMENT;
262850397Sobrien  fde_table_in_use = 0;
262950397Sobrien
263050397Sobrien  /* Generate the CFA instructions common to all FDE's.  Do it now for the
263150397Sobrien     sake of lookup_cfa.  */
263250397Sobrien
263350397Sobrien  /* On entry, the Canonical Frame Address is at SP.  */
263450397Sobrien  dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2635169689Skan
2636169689Skan#ifdef DWARF2_UNWIND_INFO
2637169689Skan  if (DWARF2_UNWIND_INFO)
2638169689Skan    initial_return_save (INCOMING_RETURN_ADDR_RTX);
263950397Sobrien#endif
264050397Sobrien}
264150397Sobrien
264250397Sobrienvoid
2643132718Skandwarf2out_frame_finish (void)
264450397Sobrien{
264550397Sobrien  /* Output call frame information.  */
2646169689Skan  if (DWARF2_FRAME_INFO)
264750397Sobrien    output_call_frame_info (0);
264890075Sobrien
2649169689Skan#ifndef TARGET_UNWIND_INFO
2650169689Skan  /* Output another copy for the unwinder.  */
265190075Sobrien  if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
265250397Sobrien    output_call_frame_info (1);
2653169689Skan#endif
265490075Sobrien}
2655132718Skan#endif
265690075Sobrien
265790075Sobrien/* And now, the subset of the debugging information support code necessary
265890075Sobrien   for emitting location expressions.  */
265950397Sobrien
2660169689Skan/* Data about a single source file.  */
2661169689Skanstruct dwarf_file_data GTY(())
2662169689Skan{
2663169689Skan  const char * filename;
2664169689Skan  int emitted_number;
2665169689Skan};
2666169689Skan
2667117395Skan/* We need some way to distinguish DW_OP_addr with a direct symbol
2668117395Skan   relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2669117395Skan#define INTERNAL_DW_OP_tls_addr		(0x100 + DW_OP_addr)
2670117395Skan
2671117395Skan
267290075Sobrientypedef struct dw_val_struct *dw_val_ref;
267390075Sobrientypedef struct die_struct *dw_die_ref;
267490075Sobrientypedef struct dw_loc_descr_struct *dw_loc_descr_ref;
267590075Sobrientypedef struct dw_loc_list_struct *dw_loc_list_ref;
267650397Sobrien
267750397Sobrien/* Each DIE may have a series of attribute/value pairs.  Values
267850397Sobrien   can take on several forms.  The forms that are used in this
267950397Sobrien   implementation are listed below.  */
268050397Sobrien
2681132718Skanenum dw_val_class
268250397Sobrien{
268350397Sobrien  dw_val_class_addr,
268490075Sobrien  dw_val_class_offset,
268550397Sobrien  dw_val_class_loc,
268690075Sobrien  dw_val_class_loc_list,
268790075Sobrien  dw_val_class_range_list,
268850397Sobrien  dw_val_class_const,
268950397Sobrien  dw_val_class_unsigned_const,
269050397Sobrien  dw_val_class_long_long,
2691132718Skan  dw_val_class_vec,
269250397Sobrien  dw_val_class_flag,
269350397Sobrien  dw_val_class_die_ref,
269450397Sobrien  dw_val_class_fde_ref,
269550397Sobrien  dw_val_class_lbl_id,
2696169689Skan  dw_val_class_lineptr,
2697169689Skan  dw_val_class_str,
2698169689Skan  dw_val_class_macptr,
2699169689Skan  dw_val_class_file
2700132718Skan};
270150397Sobrien
270250397Sobrien/* Describe a double word constant value.  */
270390075Sobrien/* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
270450397Sobrien
2705132718Skantypedef struct dw_long_long_struct GTY(())
270650397Sobrien{
270750397Sobrien  unsigned long hi;
270850397Sobrien  unsigned long low;
270950397Sobrien}
271050397Sobriendw_long_long_const;
271150397Sobrien
2712132718Skan/* Describe a floating point constant value, or a vector constant value.  */
271350397Sobrien
2714132718Skantypedef struct dw_vec_struct GTY(())
271550397Sobrien{
2716132718Skan  unsigned char * GTY((length ("%h.length"))) array;
271750397Sobrien  unsigned length;
2718132718Skan  unsigned elt_size;
271950397Sobrien}
2720132718Skandw_vec_const;
272150397Sobrien
272250397Sobrien/* The dw_val_node describes an attribute's value, as it is
272350397Sobrien   represented internally.  */
272450397Sobrien
2725132718Skantypedef struct dw_val_struct GTY(())
272650397Sobrien{
2727132718Skan  enum dw_val_class val_class;
2728132718Skan  union dw_val_struct_union
272950397Sobrien    {
2730132718Skan      rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2731132718Skan      unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2732132718Skan      dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2733132718Skan      dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2734169689Skan      HOST_WIDE_INT GTY ((default)) val_int;
2735132718Skan      unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2736132718Skan      dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2737132718Skan      dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2738132718Skan      struct dw_val_die_union
273990075Sobrien	{
274090075Sobrien	  dw_die_ref die;
274190075Sobrien	  int external;
2742132718Skan	} GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2743132718Skan      unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2744132718Skan      struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2745132718Skan      char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2746132718Skan      unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2747169689Skan      struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
274850397Sobrien    }
2749132718Skan  GTY ((desc ("%1.val_class"))) v;
275050397Sobrien}
275150397Sobriendw_val_node;
275250397Sobrien
275350397Sobrien/* Locations in memory are described using a sequence of stack machine
275450397Sobrien   operations.  */
275550397Sobrien
2756132718Skantypedef struct dw_loc_descr_struct GTY(())
275750397Sobrien{
275850397Sobrien  dw_loc_descr_ref dw_loc_next;
275950397Sobrien  enum dwarf_location_atom dw_loc_opc;
276050397Sobrien  dw_val_node dw_loc_oprnd1;
276150397Sobrien  dw_val_node dw_loc_oprnd2;
276290075Sobrien  int dw_loc_addr;
276350397Sobrien}
276450397Sobriendw_loc_descr_node;
276550397Sobrien
276690075Sobrien/* Location lists are ranges + location descriptions for that range,
276790075Sobrien   so you can track variables that are in different places over
276890075Sobrien   their entire life.  */
2769132718Skantypedef struct dw_loc_list_struct GTY(())
277090075Sobrien{
277190075Sobrien  dw_loc_list_ref dw_loc_next;
277290075Sobrien  const char *begin; /* Label for begin address of range */
277390075Sobrien  const char *end;  /* Label for end address of range */
277490075Sobrien  char *ll_symbol; /* Label for beginning of location list.
277590075Sobrien		      Only on head of list */
277690075Sobrien  const char *section; /* Section this loclist is relative to */
277790075Sobrien  dw_loc_descr_ref expr;
277890075Sobrien} dw_loc_list_node;
277990075Sobrien
2780132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
278190075Sobrien
2782132718Skanstatic const char *dwarf_stack_op_name (unsigned);
2783132718Skanstatic dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2784132718Skan				       unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2785132718Skanstatic void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2786132718Skanstatic unsigned long size_of_loc_descr (dw_loc_descr_ref);
2787132718Skanstatic unsigned long size_of_locs (dw_loc_descr_ref);
2788132718Skanstatic void output_loc_operands (dw_loc_descr_ref);
2789132718Skanstatic void output_loc_sequence (dw_loc_descr_ref);
2790132718Skan
279190075Sobrien/* Convert a DWARF stack opcode into its string name.  */
279290075Sobrien
279390075Sobrienstatic const char *
2794132718Skandwarf_stack_op_name (unsigned int op)
279590075Sobrien{
279690075Sobrien  switch (op)
279790075Sobrien    {
279890075Sobrien    case DW_OP_addr:
2799117395Skan    case INTERNAL_DW_OP_tls_addr:
280090075Sobrien      return "DW_OP_addr";
280190075Sobrien    case DW_OP_deref:
280290075Sobrien      return "DW_OP_deref";
280390075Sobrien    case DW_OP_const1u:
280490075Sobrien      return "DW_OP_const1u";
280590075Sobrien    case DW_OP_const1s:
280690075Sobrien      return "DW_OP_const1s";
280790075Sobrien    case DW_OP_const2u:
280890075Sobrien      return "DW_OP_const2u";
280990075Sobrien    case DW_OP_const2s:
281090075Sobrien      return "DW_OP_const2s";
281190075Sobrien    case DW_OP_const4u:
281290075Sobrien      return "DW_OP_const4u";
281390075Sobrien    case DW_OP_const4s:
281490075Sobrien      return "DW_OP_const4s";
281590075Sobrien    case DW_OP_const8u:
281690075Sobrien      return "DW_OP_const8u";
281790075Sobrien    case DW_OP_const8s:
281890075Sobrien      return "DW_OP_const8s";
281990075Sobrien    case DW_OP_constu:
282090075Sobrien      return "DW_OP_constu";
282190075Sobrien    case DW_OP_consts:
282290075Sobrien      return "DW_OP_consts";
282390075Sobrien    case DW_OP_dup:
282490075Sobrien      return "DW_OP_dup";
282590075Sobrien    case DW_OP_drop:
282690075Sobrien      return "DW_OP_drop";
282790075Sobrien    case DW_OP_over:
282890075Sobrien      return "DW_OP_over";
282990075Sobrien    case DW_OP_pick:
283090075Sobrien      return "DW_OP_pick";
283190075Sobrien    case DW_OP_swap:
283290075Sobrien      return "DW_OP_swap";
283390075Sobrien    case DW_OP_rot:
283490075Sobrien      return "DW_OP_rot";
283590075Sobrien    case DW_OP_xderef:
283690075Sobrien      return "DW_OP_xderef";
283790075Sobrien    case DW_OP_abs:
283890075Sobrien      return "DW_OP_abs";
283990075Sobrien    case DW_OP_and:
284090075Sobrien      return "DW_OP_and";
284190075Sobrien    case DW_OP_div:
284290075Sobrien      return "DW_OP_div";
284390075Sobrien    case DW_OP_minus:
284490075Sobrien      return "DW_OP_minus";
284590075Sobrien    case DW_OP_mod:
284690075Sobrien      return "DW_OP_mod";
284790075Sobrien    case DW_OP_mul:
284890075Sobrien      return "DW_OP_mul";
284990075Sobrien    case DW_OP_neg:
285090075Sobrien      return "DW_OP_neg";
285190075Sobrien    case DW_OP_not:
285290075Sobrien      return "DW_OP_not";
285390075Sobrien    case DW_OP_or:
285490075Sobrien      return "DW_OP_or";
285590075Sobrien    case DW_OP_plus:
285690075Sobrien      return "DW_OP_plus";
285790075Sobrien    case DW_OP_plus_uconst:
285890075Sobrien      return "DW_OP_plus_uconst";
285990075Sobrien    case DW_OP_shl:
286090075Sobrien      return "DW_OP_shl";
286190075Sobrien    case DW_OP_shr:
286290075Sobrien      return "DW_OP_shr";
286390075Sobrien    case DW_OP_shra:
286490075Sobrien      return "DW_OP_shra";
286590075Sobrien    case DW_OP_xor:
286690075Sobrien      return "DW_OP_xor";
286790075Sobrien    case DW_OP_bra:
286890075Sobrien      return "DW_OP_bra";
286990075Sobrien    case DW_OP_eq:
287090075Sobrien      return "DW_OP_eq";
287190075Sobrien    case DW_OP_ge:
287290075Sobrien      return "DW_OP_ge";
287390075Sobrien    case DW_OP_gt:
287490075Sobrien      return "DW_OP_gt";
287590075Sobrien    case DW_OP_le:
287690075Sobrien      return "DW_OP_le";
287790075Sobrien    case DW_OP_lt:
287890075Sobrien      return "DW_OP_lt";
287990075Sobrien    case DW_OP_ne:
288090075Sobrien      return "DW_OP_ne";
288190075Sobrien    case DW_OP_skip:
288290075Sobrien      return "DW_OP_skip";
288390075Sobrien    case DW_OP_lit0:
288490075Sobrien      return "DW_OP_lit0";
288590075Sobrien    case DW_OP_lit1:
288690075Sobrien      return "DW_OP_lit1";
288790075Sobrien    case DW_OP_lit2:
288890075Sobrien      return "DW_OP_lit2";
288990075Sobrien    case DW_OP_lit3:
289090075Sobrien      return "DW_OP_lit3";
289190075Sobrien    case DW_OP_lit4:
289290075Sobrien      return "DW_OP_lit4";
289390075Sobrien    case DW_OP_lit5:
289490075Sobrien      return "DW_OP_lit5";
289590075Sobrien    case DW_OP_lit6:
289690075Sobrien      return "DW_OP_lit6";
289790075Sobrien    case DW_OP_lit7:
289890075Sobrien      return "DW_OP_lit7";
289990075Sobrien    case DW_OP_lit8:
290090075Sobrien      return "DW_OP_lit8";
290190075Sobrien    case DW_OP_lit9:
290290075Sobrien      return "DW_OP_lit9";
290390075Sobrien    case DW_OP_lit10:
290490075Sobrien      return "DW_OP_lit10";
290590075Sobrien    case DW_OP_lit11:
290690075Sobrien      return "DW_OP_lit11";
290790075Sobrien    case DW_OP_lit12:
290890075Sobrien      return "DW_OP_lit12";
290990075Sobrien    case DW_OP_lit13:
291090075Sobrien      return "DW_OP_lit13";
291190075Sobrien    case DW_OP_lit14:
291290075Sobrien      return "DW_OP_lit14";
291390075Sobrien    case DW_OP_lit15:
291490075Sobrien      return "DW_OP_lit15";
291590075Sobrien    case DW_OP_lit16:
291690075Sobrien      return "DW_OP_lit16";
291790075Sobrien    case DW_OP_lit17:
291890075Sobrien      return "DW_OP_lit17";
291990075Sobrien    case DW_OP_lit18:
292090075Sobrien      return "DW_OP_lit18";
292190075Sobrien    case DW_OP_lit19:
292290075Sobrien      return "DW_OP_lit19";
292390075Sobrien    case DW_OP_lit20:
292490075Sobrien      return "DW_OP_lit20";
292590075Sobrien    case DW_OP_lit21:
292690075Sobrien      return "DW_OP_lit21";
292790075Sobrien    case DW_OP_lit22:
292890075Sobrien      return "DW_OP_lit22";
292990075Sobrien    case DW_OP_lit23:
293090075Sobrien      return "DW_OP_lit23";
293190075Sobrien    case DW_OP_lit24:
293290075Sobrien      return "DW_OP_lit24";
293390075Sobrien    case DW_OP_lit25:
293490075Sobrien      return "DW_OP_lit25";
293590075Sobrien    case DW_OP_lit26:
293690075Sobrien      return "DW_OP_lit26";
293790075Sobrien    case DW_OP_lit27:
293890075Sobrien      return "DW_OP_lit27";
293990075Sobrien    case DW_OP_lit28:
294090075Sobrien      return "DW_OP_lit28";
294190075Sobrien    case DW_OP_lit29:
294290075Sobrien      return "DW_OP_lit29";
294390075Sobrien    case DW_OP_lit30:
294490075Sobrien      return "DW_OP_lit30";
294590075Sobrien    case DW_OP_lit31:
294690075Sobrien      return "DW_OP_lit31";
294790075Sobrien    case DW_OP_reg0:
294890075Sobrien      return "DW_OP_reg0";
294990075Sobrien    case DW_OP_reg1:
295090075Sobrien      return "DW_OP_reg1";
295190075Sobrien    case DW_OP_reg2:
295290075Sobrien      return "DW_OP_reg2";
295390075Sobrien    case DW_OP_reg3:
295490075Sobrien      return "DW_OP_reg3";
295590075Sobrien    case DW_OP_reg4:
295690075Sobrien      return "DW_OP_reg4";
295790075Sobrien    case DW_OP_reg5:
295890075Sobrien      return "DW_OP_reg5";
295990075Sobrien    case DW_OP_reg6:
296090075Sobrien      return "DW_OP_reg6";
296190075Sobrien    case DW_OP_reg7:
296290075Sobrien      return "DW_OP_reg7";
296390075Sobrien    case DW_OP_reg8:
296490075Sobrien      return "DW_OP_reg8";
296590075Sobrien    case DW_OP_reg9:
296690075Sobrien      return "DW_OP_reg9";
296790075Sobrien    case DW_OP_reg10:
296890075Sobrien      return "DW_OP_reg10";
296990075Sobrien    case DW_OP_reg11:
297090075Sobrien      return "DW_OP_reg11";
297190075Sobrien    case DW_OP_reg12:
297290075Sobrien      return "DW_OP_reg12";
297390075Sobrien    case DW_OP_reg13:
297490075Sobrien      return "DW_OP_reg13";
297590075Sobrien    case DW_OP_reg14:
297690075Sobrien      return "DW_OP_reg14";
297790075Sobrien    case DW_OP_reg15:
297890075Sobrien      return "DW_OP_reg15";
297990075Sobrien    case DW_OP_reg16:
298090075Sobrien      return "DW_OP_reg16";
298190075Sobrien    case DW_OP_reg17:
298290075Sobrien      return "DW_OP_reg17";
298390075Sobrien    case DW_OP_reg18:
298490075Sobrien      return "DW_OP_reg18";
298590075Sobrien    case DW_OP_reg19:
298690075Sobrien      return "DW_OP_reg19";
298790075Sobrien    case DW_OP_reg20:
298890075Sobrien      return "DW_OP_reg20";
298990075Sobrien    case DW_OP_reg21:
299090075Sobrien      return "DW_OP_reg21";
299190075Sobrien    case DW_OP_reg22:
299290075Sobrien      return "DW_OP_reg22";
299390075Sobrien    case DW_OP_reg23:
299490075Sobrien      return "DW_OP_reg23";
299590075Sobrien    case DW_OP_reg24:
299690075Sobrien      return "DW_OP_reg24";
299790075Sobrien    case DW_OP_reg25:
299890075Sobrien      return "DW_OP_reg25";
299990075Sobrien    case DW_OP_reg26:
300090075Sobrien      return "DW_OP_reg26";
300190075Sobrien    case DW_OP_reg27:
300290075Sobrien      return "DW_OP_reg27";
300390075Sobrien    case DW_OP_reg28:
300490075Sobrien      return "DW_OP_reg28";
300590075Sobrien    case DW_OP_reg29:
300690075Sobrien      return "DW_OP_reg29";
300790075Sobrien    case DW_OP_reg30:
300890075Sobrien      return "DW_OP_reg30";
300990075Sobrien    case DW_OP_reg31:
301090075Sobrien      return "DW_OP_reg31";
301190075Sobrien    case DW_OP_breg0:
301290075Sobrien      return "DW_OP_breg0";
301390075Sobrien    case DW_OP_breg1:
301490075Sobrien      return "DW_OP_breg1";
301590075Sobrien    case DW_OP_breg2:
301690075Sobrien      return "DW_OP_breg2";
301790075Sobrien    case DW_OP_breg3:
301890075Sobrien      return "DW_OP_breg3";
301990075Sobrien    case DW_OP_breg4:
302090075Sobrien      return "DW_OP_breg4";
302190075Sobrien    case DW_OP_breg5:
302290075Sobrien      return "DW_OP_breg5";
302390075Sobrien    case DW_OP_breg6:
302490075Sobrien      return "DW_OP_breg6";
302590075Sobrien    case DW_OP_breg7:
302690075Sobrien      return "DW_OP_breg7";
302790075Sobrien    case DW_OP_breg8:
302890075Sobrien      return "DW_OP_breg8";
302990075Sobrien    case DW_OP_breg9:
303090075Sobrien      return "DW_OP_breg9";
303190075Sobrien    case DW_OP_breg10:
303290075Sobrien      return "DW_OP_breg10";
303390075Sobrien    case DW_OP_breg11:
303490075Sobrien      return "DW_OP_breg11";
303590075Sobrien    case DW_OP_breg12:
303690075Sobrien      return "DW_OP_breg12";
303790075Sobrien    case DW_OP_breg13:
303890075Sobrien      return "DW_OP_breg13";
303990075Sobrien    case DW_OP_breg14:
304090075Sobrien      return "DW_OP_breg14";
304190075Sobrien    case DW_OP_breg15:
304290075Sobrien      return "DW_OP_breg15";
304390075Sobrien    case DW_OP_breg16:
304490075Sobrien      return "DW_OP_breg16";
304590075Sobrien    case DW_OP_breg17:
304690075Sobrien      return "DW_OP_breg17";
304790075Sobrien    case DW_OP_breg18:
304890075Sobrien      return "DW_OP_breg18";
304990075Sobrien    case DW_OP_breg19:
305090075Sobrien      return "DW_OP_breg19";
305190075Sobrien    case DW_OP_breg20:
305290075Sobrien      return "DW_OP_breg20";
305390075Sobrien    case DW_OP_breg21:
305490075Sobrien      return "DW_OP_breg21";
305590075Sobrien    case DW_OP_breg22:
305690075Sobrien      return "DW_OP_breg22";
305790075Sobrien    case DW_OP_breg23:
305890075Sobrien      return "DW_OP_breg23";
305990075Sobrien    case DW_OP_breg24:
306090075Sobrien      return "DW_OP_breg24";
306190075Sobrien    case DW_OP_breg25:
306290075Sobrien      return "DW_OP_breg25";
306390075Sobrien    case DW_OP_breg26:
306490075Sobrien      return "DW_OP_breg26";
306590075Sobrien    case DW_OP_breg27:
306690075Sobrien      return "DW_OP_breg27";
306790075Sobrien    case DW_OP_breg28:
306890075Sobrien      return "DW_OP_breg28";
306990075Sobrien    case DW_OP_breg29:
307090075Sobrien      return "DW_OP_breg29";
307190075Sobrien    case DW_OP_breg30:
307290075Sobrien      return "DW_OP_breg30";
307390075Sobrien    case DW_OP_breg31:
307490075Sobrien      return "DW_OP_breg31";
307590075Sobrien    case DW_OP_regx:
307690075Sobrien      return "DW_OP_regx";
307790075Sobrien    case DW_OP_fbreg:
307890075Sobrien      return "DW_OP_fbreg";
307990075Sobrien    case DW_OP_bregx:
308090075Sobrien      return "DW_OP_bregx";
308190075Sobrien    case DW_OP_piece:
308290075Sobrien      return "DW_OP_piece";
308390075Sobrien    case DW_OP_deref_size:
308490075Sobrien      return "DW_OP_deref_size";
308590075Sobrien    case DW_OP_xderef_size:
308690075Sobrien      return "DW_OP_xderef_size";
308790075Sobrien    case DW_OP_nop:
308890075Sobrien      return "DW_OP_nop";
3089117395Skan    case DW_OP_push_object_address:
3090117395Skan      return "DW_OP_push_object_address";
3091117395Skan    case DW_OP_call2:
3092117395Skan      return "DW_OP_call2";
3093117395Skan    case DW_OP_call4:
3094117395Skan      return "DW_OP_call4";
3095117395Skan    case DW_OP_call_ref:
3096117395Skan      return "DW_OP_call_ref";
3097117395Skan    case DW_OP_GNU_push_tls_address:
3098117395Skan      return "DW_OP_GNU_push_tls_address";
309990075Sobrien    default:
310090075Sobrien      return "OP_<unknown>";
310190075Sobrien    }
310290075Sobrien}
310390075Sobrien
310490075Sobrien/* Return a pointer to a newly allocated location description.  Location
310590075Sobrien   descriptions are simple expression terms that can be strung
310690075Sobrien   together to form more complicated location (address) descriptions.  */
310790075Sobrien
310890075Sobrienstatic inline dw_loc_descr_ref
3109132718Skannew_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3110132718Skan	       unsigned HOST_WIDE_INT oprnd2)
311190075Sobrien{
3112132718Skan  dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
311390075Sobrien
311490075Sobrien  descr->dw_loc_opc = op;
311590075Sobrien  descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
311690075Sobrien  descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
311790075Sobrien  descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
311890075Sobrien  descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
311990075Sobrien
312090075Sobrien  return descr;
312190075Sobrien}
312290075Sobrien
312390075Sobrien/* Add a location description term to a location description expression.  */
312490075Sobrien
312590075Sobrienstatic inline void
3126132718Skanadd_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
312790075Sobrien{
312890075Sobrien  dw_loc_descr_ref *d;
312990075Sobrien
313090075Sobrien  /* Find the end of the chain.  */
313190075Sobrien  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
313290075Sobrien    ;
313390075Sobrien
313490075Sobrien  *d = descr;
313590075Sobrien}
313690075Sobrien
313790075Sobrien/* Return the size of a location descriptor.  */
313890075Sobrien
313990075Sobrienstatic unsigned long
3140132718Skansize_of_loc_descr (dw_loc_descr_ref loc)
314190075Sobrien{
314290075Sobrien  unsigned long size = 1;
314390075Sobrien
314490075Sobrien  switch (loc->dw_loc_opc)
314590075Sobrien    {
314690075Sobrien    case DW_OP_addr:
3147117395Skan    case INTERNAL_DW_OP_tls_addr:
314890075Sobrien      size += DWARF2_ADDR_SIZE;
314990075Sobrien      break;
315090075Sobrien    case DW_OP_const1u:
315190075Sobrien    case DW_OP_const1s:
315290075Sobrien      size += 1;
315390075Sobrien      break;
315490075Sobrien    case DW_OP_const2u:
315590075Sobrien    case DW_OP_const2s:
315690075Sobrien      size += 2;
315790075Sobrien      break;
315890075Sobrien    case DW_OP_const4u:
315990075Sobrien    case DW_OP_const4s:
316090075Sobrien      size += 4;
316190075Sobrien      break;
316290075Sobrien    case DW_OP_const8u:
316390075Sobrien    case DW_OP_const8s:
316490075Sobrien      size += 8;
316590075Sobrien      break;
316690075Sobrien    case DW_OP_constu:
316790075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
316890075Sobrien      break;
316990075Sobrien    case DW_OP_consts:
317090075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
317190075Sobrien      break;
317290075Sobrien    case DW_OP_pick:
317390075Sobrien      size += 1;
317490075Sobrien      break;
317590075Sobrien    case DW_OP_plus_uconst:
317690075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
317790075Sobrien      break;
317890075Sobrien    case DW_OP_skip:
317990075Sobrien    case DW_OP_bra:
318090075Sobrien      size += 2;
318190075Sobrien      break;
318290075Sobrien    case DW_OP_breg0:
318390075Sobrien    case DW_OP_breg1:
318490075Sobrien    case DW_OP_breg2:
318590075Sobrien    case DW_OP_breg3:
318690075Sobrien    case DW_OP_breg4:
318790075Sobrien    case DW_OP_breg5:
318890075Sobrien    case DW_OP_breg6:
318990075Sobrien    case DW_OP_breg7:
319090075Sobrien    case DW_OP_breg8:
319190075Sobrien    case DW_OP_breg9:
319290075Sobrien    case DW_OP_breg10:
319390075Sobrien    case DW_OP_breg11:
319490075Sobrien    case DW_OP_breg12:
319590075Sobrien    case DW_OP_breg13:
319690075Sobrien    case DW_OP_breg14:
319790075Sobrien    case DW_OP_breg15:
319890075Sobrien    case DW_OP_breg16:
319990075Sobrien    case DW_OP_breg17:
320090075Sobrien    case DW_OP_breg18:
320190075Sobrien    case DW_OP_breg19:
320290075Sobrien    case DW_OP_breg20:
320390075Sobrien    case DW_OP_breg21:
320490075Sobrien    case DW_OP_breg22:
320590075Sobrien    case DW_OP_breg23:
320690075Sobrien    case DW_OP_breg24:
320790075Sobrien    case DW_OP_breg25:
320890075Sobrien    case DW_OP_breg26:
320990075Sobrien    case DW_OP_breg27:
321090075Sobrien    case DW_OP_breg28:
321190075Sobrien    case DW_OP_breg29:
321290075Sobrien    case DW_OP_breg30:
321390075Sobrien    case DW_OP_breg31:
321490075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
321590075Sobrien      break;
321690075Sobrien    case DW_OP_regx:
321790075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
321890075Sobrien      break;
321990075Sobrien    case DW_OP_fbreg:
322090075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
322190075Sobrien      break;
322290075Sobrien    case DW_OP_bregx:
322390075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
322490075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
322590075Sobrien      break;
322690075Sobrien    case DW_OP_piece:
322790075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
322890075Sobrien      break;
322990075Sobrien    case DW_OP_deref_size:
323090075Sobrien    case DW_OP_xderef_size:
323190075Sobrien      size += 1;
323290075Sobrien      break;
3233117395Skan    case DW_OP_call2:
3234117395Skan      size += 2;
3235117395Skan      break;
3236117395Skan    case DW_OP_call4:
3237117395Skan      size += 4;
3238117395Skan      break;
3239117395Skan    case DW_OP_call_ref:
3240117395Skan      size += DWARF2_ADDR_SIZE;
3241117395Skan      break;
324290075Sobrien    default:
324390075Sobrien      break;
324490075Sobrien    }
324590075Sobrien
324690075Sobrien  return size;
324790075Sobrien}
324890075Sobrien
324990075Sobrien/* Return the size of a series of location descriptors.  */
325090075Sobrien
325190075Sobrienstatic unsigned long
3252132718Skansize_of_locs (dw_loc_descr_ref loc)
325390075Sobrien{
3254169689Skan  dw_loc_descr_ref l;
325590075Sobrien  unsigned long size;
325690075Sobrien
3257169689Skan  /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3258169689Skan     field, to avoid writing to a PCH file.  */
3259169689Skan  for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
326090075Sobrien    {
3261169689Skan      if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3262169689Skan	break;
3263169689Skan      size += size_of_loc_descr (l);
326490075Sobrien    }
3265169689Skan  if (! l)
3266169689Skan    return size;
326790075Sobrien
3268169689Skan  for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3269169689Skan    {
3270169689Skan      l->dw_loc_addr = size;
3271169689Skan      size += size_of_loc_descr (l);
3272169689Skan    }
3273169689Skan
327490075Sobrien  return size;
327590075Sobrien}
327690075Sobrien
327790075Sobrien/* Output location description stack opcode's operands (if any).  */
327890075Sobrien
327990075Sobrienstatic void
3280132718Skanoutput_loc_operands (dw_loc_descr_ref loc)
328190075Sobrien{
328290075Sobrien  dw_val_ref val1 = &loc->dw_loc_oprnd1;
328390075Sobrien  dw_val_ref val2 = &loc->dw_loc_oprnd2;
328490075Sobrien
328590075Sobrien  switch (loc->dw_loc_opc)
328690075Sobrien    {
328790075Sobrien#ifdef DWARF2_DEBUGGING_INFO
328890075Sobrien    case DW_OP_addr:
328990075Sobrien      dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
329090075Sobrien      break;
329190075Sobrien    case DW_OP_const2u:
329290075Sobrien    case DW_OP_const2s:
329390075Sobrien      dw2_asm_output_data (2, val1->v.val_int, NULL);
329490075Sobrien      break;
329590075Sobrien    case DW_OP_const4u:
329690075Sobrien    case DW_OP_const4s:
329790075Sobrien      dw2_asm_output_data (4, val1->v.val_int, NULL);
329890075Sobrien      break;
329990075Sobrien    case DW_OP_const8u:
330090075Sobrien    case DW_OP_const8s:
3301169689Skan      gcc_assert (HOST_BITS_PER_LONG >= 64);
330290075Sobrien      dw2_asm_output_data (8, val1->v.val_int, NULL);
330390075Sobrien      break;
330490075Sobrien    case DW_OP_skip:
330590075Sobrien    case DW_OP_bra:
330690075Sobrien      {
330790075Sobrien	int offset;
330890075Sobrien
3309169689Skan	gcc_assert (val1->val_class == dw_val_class_loc);
3310169689Skan	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
331190075Sobrien
331290075Sobrien	dw2_asm_output_data (2, offset, NULL);
331390075Sobrien      }
331490075Sobrien      break;
331590075Sobrien#else
331690075Sobrien    case DW_OP_addr:
331790075Sobrien    case DW_OP_const2u:
331890075Sobrien    case DW_OP_const2s:
331990075Sobrien    case DW_OP_const4u:
332090075Sobrien    case DW_OP_const4s:
332190075Sobrien    case DW_OP_const8u:
332290075Sobrien    case DW_OP_const8s:
332390075Sobrien    case DW_OP_skip:
332490075Sobrien    case DW_OP_bra:
332590075Sobrien      /* We currently don't make any attempt to make sure these are
3326132718Skan	 aligned properly like we do for the main unwind info, so
3327132718Skan	 don't support emitting things larger than a byte if we're
3328132718Skan	 only doing unwinding.  */
3329169689Skan      gcc_unreachable ();
333090075Sobrien#endif
333190075Sobrien    case DW_OP_const1u:
333290075Sobrien    case DW_OP_const1s:
333390075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
333490075Sobrien      break;
333590075Sobrien    case DW_OP_constu:
333690075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
333790075Sobrien      break;
333890075Sobrien    case DW_OP_consts:
333990075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
334090075Sobrien      break;
334190075Sobrien    case DW_OP_pick:
334290075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
334390075Sobrien      break;
334490075Sobrien    case DW_OP_plus_uconst:
334590075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
334690075Sobrien      break;
334790075Sobrien    case DW_OP_breg0:
334890075Sobrien    case DW_OP_breg1:
334990075Sobrien    case DW_OP_breg2:
335090075Sobrien    case DW_OP_breg3:
335190075Sobrien    case DW_OP_breg4:
335290075Sobrien    case DW_OP_breg5:
335390075Sobrien    case DW_OP_breg6:
335490075Sobrien    case DW_OP_breg7:
335590075Sobrien    case DW_OP_breg8:
335690075Sobrien    case DW_OP_breg9:
335790075Sobrien    case DW_OP_breg10:
335890075Sobrien    case DW_OP_breg11:
335990075Sobrien    case DW_OP_breg12:
336090075Sobrien    case DW_OP_breg13:
336190075Sobrien    case DW_OP_breg14:
336290075Sobrien    case DW_OP_breg15:
336390075Sobrien    case DW_OP_breg16:
336490075Sobrien    case DW_OP_breg17:
336590075Sobrien    case DW_OP_breg18:
336690075Sobrien    case DW_OP_breg19:
336790075Sobrien    case DW_OP_breg20:
336890075Sobrien    case DW_OP_breg21:
336990075Sobrien    case DW_OP_breg22:
337090075Sobrien    case DW_OP_breg23:
337190075Sobrien    case DW_OP_breg24:
337290075Sobrien    case DW_OP_breg25:
337390075Sobrien    case DW_OP_breg26:
337490075Sobrien    case DW_OP_breg27:
337590075Sobrien    case DW_OP_breg28:
337690075Sobrien    case DW_OP_breg29:
337790075Sobrien    case DW_OP_breg30:
337890075Sobrien    case DW_OP_breg31:
337990075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
338090075Sobrien      break;
338190075Sobrien    case DW_OP_regx:
338290075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
338390075Sobrien      break;
338490075Sobrien    case DW_OP_fbreg:
338590075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
338690075Sobrien      break;
338790075Sobrien    case DW_OP_bregx:
338890075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
338990075Sobrien      dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
339090075Sobrien      break;
339190075Sobrien    case DW_OP_piece:
339290075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
339390075Sobrien      break;
339490075Sobrien    case DW_OP_deref_size:
339590075Sobrien    case DW_OP_xderef_size:
339690075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
339790075Sobrien      break;
3398117395Skan
3399117395Skan    case INTERNAL_DW_OP_tls_addr:
3400169689Skan      if (targetm.asm_out.output_dwarf_dtprel)
3401169689Skan	{
3402169689Skan	  targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3403169689Skan					       DWARF2_ADDR_SIZE,
3404169689Skan					       val1->v.val_addr);
3405169689Skan	  fputc ('\n', asm_out_file);
3406169689Skan	}
3407169689Skan      else
3408169689Skan	gcc_unreachable ();
3409117395Skan      break;
3410117395Skan
341190075Sobrien    default:
341290075Sobrien      /* Other codes have no operands.  */
341390075Sobrien      break;
341490075Sobrien    }
341590075Sobrien}
341690075Sobrien
341790075Sobrien/* Output a sequence of location operations.  */
341890075Sobrien
341990075Sobrienstatic void
3420132718Skanoutput_loc_sequence (dw_loc_descr_ref loc)
342190075Sobrien{
342290075Sobrien  for (; loc != NULL; loc = loc->dw_loc_next)
342390075Sobrien    {
342490075Sobrien      /* Output the opcode.  */
342590075Sobrien      dw2_asm_output_data (1, loc->dw_loc_opc,
342690075Sobrien			   "%s", dwarf_stack_op_name (loc->dw_loc_opc));
342790075Sobrien
342890075Sobrien      /* Output the operand(s) (if any).  */
342990075Sobrien      output_loc_operands (loc);
343090075Sobrien    }
343190075Sobrien}
343290075Sobrien
343390075Sobrien/* This routine will generate the correct assembly data for a location
343490075Sobrien   description based on a cfi entry with a complex address.  */
343590075Sobrien
343690075Sobrienstatic void
3437132718Skanoutput_cfa_loc (dw_cfi_ref cfi)
343890075Sobrien{
343990075Sobrien  dw_loc_descr_ref loc;
344090075Sobrien  unsigned long size;
344190075Sobrien
344290075Sobrien  /* Output the size of the block.  */
344390075Sobrien  loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
344490075Sobrien  size = size_of_locs (loc);
344590075Sobrien  dw2_asm_output_data_uleb128 (size, NULL);
344690075Sobrien
344790075Sobrien  /* Now output the operations themselves.  */
344890075Sobrien  output_loc_sequence (loc);
344990075Sobrien}
345090075Sobrien
3451169689Skan/* This function builds a dwarf location descriptor sequence from a
3452169689Skan   dw_cfa_location, adding the given OFFSET to the result of the
3453169689Skan   expression.  */
345490075Sobrien
345590075Sobrienstatic struct dw_loc_descr_struct *
3456169689Skanbuild_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
345790075Sobrien{
345890075Sobrien  struct dw_loc_descr_struct *head, *tmp;
345990075Sobrien
3460169689Skan  offset += cfa->offset;
346190075Sobrien
3462169689Skan  if (cfa->indirect)
346390075Sobrien    {
3464169689Skan      if (cfa->base_offset)
3465169689Skan	{
3466169689Skan	  if (cfa->reg <= 31)
3467169689Skan	    head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3468169689Skan	  else
3469169689Skan	    head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3470169689Skan	}
3471169689Skan      else if (cfa->reg <= 31)
3472169689Skan	head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
347390075Sobrien      else
3474169689Skan	head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3475169689Skan
3476169689Skan      head->dw_loc_oprnd1.val_class = dw_val_class_const;
3477169689Skan      tmp = new_loc_descr (DW_OP_deref, 0, 0);
3478169689Skan      add_loc_descr (&head, tmp);
3479169689Skan      if (offset != 0)
3480169689Skan	{
3481169689Skan	  tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3482169689Skan	  add_loc_descr (&head, tmp);
3483169689Skan	}
348490075Sobrien    }
348590075Sobrien  else
348690075Sobrien    {
3487169689Skan      if (offset == 0)
3488169689Skan	if (cfa->reg <= 31)
3489169689Skan	  head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3490169689Skan	else
3491169689Skan	  head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3492169689Skan      else if (cfa->reg <= 31)
3493169689Skan	head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3494169689Skan      else
3495169689Skan	head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
349690075Sobrien    }
349790075Sobrien
349890075Sobrien  return head;
349990075Sobrien}
350090075Sobrien
350190075Sobrien/* This function fills in aa dw_cfa_location structure from a dwarf location
350290075Sobrien   descriptor sequence.  */
350390075Sobrien
350490075Sobrienstatic void
3505132718Skanget_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
350690075Sobrien{
350790075Sobrien  struct dw_loc_descr_struct *ptr;
350890075Sobrien  cfa->offset = 0;
350990075Sobrien  cfa->base_offset = 0;
351090075Sobrien  cfa->indirect = 0;
351190075Sobrien  cfa->reg = -1;
351290075Sobrien
351390075Sobrien  for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
351490075Sobrien    {
351590075Sobrien      enum dwarf_location_atom op = ptr->dw_loc_opc;
351690075Sobrien
351790075Sobrien      switch (op)
351890075Sobrien	{
351990075Sobrien	case DW_OP_reg0:
352090075Sobrien	case DW_OP_reg1:
352190075Sobrien	case DW_OP_reg2:
352290075Sobrien	case DW_OP_reg3:
352390075Sobrien	case DW_OP_reg4:
352490075Sobrien	case DW_OP_reg5:
352590075Sobrien	case DW_OP_reg6:
352690075Sobrien	case DW_OP_reg7:
352790075Sobrien	case DW_OP_reg8:
352890075Sobrien	case DW_OP_reg9:
352990075Sobrien	case DW_OP_reg10:
353090075Sobrien	case DW_OP_reg11:
353190075Sobrien	case DW_OP_reg12:
353290075Sobrien	case DW_OP_reg13:
353390075Sobrien	case DW_OP_reg14:
353490075Sobrien	case DW_OP_reg15:
353590075Sobrien	case DW_OP_reg16:
353690075Sobrien	case DW_OP_reg17:
353790075Sobrien	case DW_OP_reg18:
353890075Sobrien	case DW_OP_reg19:
353990075Sobrien	case DW_OP_reg20:
354090075Sobrien	case DW_OP_reg21:
354190075Sobrien	case DW_OP_reg22:
354290075Sobrien	case DW_OP_reg23:
354390075Sobrien	case DW_OP_reg24:
354490075Sobrien	case DW_OP_reg25:
354590075Sobrien	case DW_OP_reg26:
354690075Sobrien	case DW_OP_reg27:
354790075Sobrien	case DW_OP_reg28:
354890075Sobrien	case DW_OP_reg29:
354990075Sobrien	case DW_OP_reg30:
355090075Sobrien	case DW_OP_reg31:
355190075Sobrien	  cfa->reg = op - DW_OP_reg0;
355290075Sobrien	  break;
355390075Sobrien	case DW_OP_regx:
355490075Sobrien	  cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
355590075Sobrien	  break;
355690075Sobrien	case DW_OP_breg0:
355790075Sobrien	case DW_OP_breg1:
355890075Sobrien	case DW_OP_breg2:
355990075Sobrien	case DW_OP_breg3:
356090075Sobrien	case DW_OP_breg4:
356190075Sobrien	case DW_OP_breg5:
356290075Sobrien	case DW_OP_breg6:
356390075Sobrien	case DW_OP_breg7:
356490075Sobrien	case DW_OP_breg8:
356590075Sobrien	case DW_OP_breg9:
356690075Sobrien	case DW_OP_breg10:
356790075Sobrien	case DW_OP_breg11:
356890075Sobrien	case DW_OP_breg12:
356990075Sobrien	case DW_OP_breg13:
357090075Sobrien	case DW_OP_breg14:
357190075Sobrien	case DW_OP_breg15:
357290075Sobrien	case DW_OP_breg16:
357390075Sobrien	case DW_OP_breg17:
357490075Sobrien	case DW_OP_breg18:
357590075Sobrien	case DW_OP_breg19:
357690075Sobrien	case DW_OP_breg20:
357790075Sobrien	case DW_OP_breg21:
357890075Sobrien	case DW_OP_breg22:
357990075Sobrien	case DW_OP_breg23:
358090075Sobrien	case DW_OP_breg24:
358190075Sobrien	case DW_OP_breg25:
358290075Sobrien	case DW_OP_breg26:
358390075Sobrien	case DW_OP_breg27:
358490075Sobrien	case DW_OP_breg28:
358590075Sobrien	case DW_OP_breg29:
358690075Sobrien	case DW_OP_breg30:
358790075Sobrien	case DW_OP_breg31:
358890075Sobrien	  cfa->reg = op - DW_OP_breg0;
358990075Sobrien	  cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
359090075Sobrien	  break;
359190075Sobrien	case DW_OP_bregx:
359290075Sobrien	  cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
359390075Sobrien	  cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
359490075Sobrien	  break;
359590075Sobrien	case DW_OP_deref:
359690075Sobrien	  cfa->indirect = 1;
359790075Sobrien	  break;
359890075Sobrien	case DW_OP_plus_uconst:
359990075Sobrien	  cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
360090075Sobrien	  break;
360190075Sobrien	default:
3602169689Skan	  internal_error ("DW_LOC_OP %s not implemented",
360390075Sobrien			  dwarf_stack_op_name (ptr->dw_loc_opc));
360490075Sobrien	}
360590075Sobrien    }
360690075Sobrien}
360790075Sobrien#endif /* .debug_frame support */
360890075Sobrien
360990075Sobrien/* And now, the support for symbolic debugging information.  */
361090075Sobrien#ifdef DWARF2_DEBUGGING_INFO
361190075Sobrien
361290075Sobrien/* .debug_str support.  */
3613132718Skanstatic int output_indirect_string (void **, void *);
361490075Sobrien
3615132718Skanstatic void dwarf2out_init (const char *);
3616132718Skanstatic void dwarf2out_finish (const char *);
3617132718Skanstatic void dwarf2out_define (unsigned int, const char *);
3618132718Skanstatic void dwarf2out_undef (unsigned int, const char *);
3619132718Skanstatic void dwarf2out_start_source_file (unsigned, const char *);
3620132718Skanstatic void dwarf2out_end_source_file (unsigned);
3621132718Skanstatic void dwarf2out_begin_block (unsigned, unsigned);
3622132718Skanstatic void dwarf2out_end_block (unsigned, unsigned);
3623132718Skanstatic bool dwarf2out_ignore_block (tree);
3624132718Skanstatic void dwarf2out_global_decl (tree);
3625169689Skanstatic void dwarf2out_type_decl (tree, int);
3626169689Skanstatic void dwarf2out_imported_module_or_decl (tree, tree);
3627132718Skanstatic void dwarf2out_abstract_function (tree);
3628169689Skanstatic void dwarf2out_var_location (rtx);
3629169689Skanstatic void dwarf2out_begin_function (tree);
3630169689Skanstatic void dwarf2out_switch_text_section (void);
363190075Sobrien
363290075Sobrien/* The debug hooks structure.  */
363390075Sobrien
3634117395Skanconst struct gcc_debug_hooks dwarf2_debug_hooks =
363590075Sobrien{
363690075Sobrien  dwarf2out_init,
363790075Sobrien  dwarf2out_finish,
363890075Sobrien  dwarf2out_define,
363990075Sobrien  dwarf2out_undef,
364090075Sobrien  dwarf2out_start_source_file,
364190075Sobrien  dwarf2out_end_source_file,
364290075Sobrien  dwarf2out_begin_block,
364390075Sobrien  dwarf2out_end_block,
364490075Sobrien  dwarf2out_ignore_block,
364590075Sobrien  dwarf2out_source_line,
364690075Sobrien  dwarf2out_begin_prologue,
3647117395Skan  debug_nothing_int_charstar,	/* end_prologue */
364890075Sobrien  dwarf2out_end_epilogue,
3649169689Skan  dwarf2out_begin_function,
365090075Sobrien  debug_nothing_int,		/* end_function */
365190075Sobrien  dwarf2out_decl,		/* function_decl */
365290075Sobrien  dwarf2out_global_decl,
3653169689Skan  dwarf2out_type_decl,		/* type_decl */
3654169689Skan  dwarf2out_imported_module_or_decl,
365590075Sobrien  debug_nothing_tree,		/* deferred_inline_function */
365690075Sobrien  /* The DWARF 2 backend tries to reduce debugging bloat by not
365790075Sobrien     emitting the abstract description of inline functions until
365890075Sobrien     something tries to reference them.  */
365990075Sobrien  dwarf2out_abstract_function,	/* outlining_inline_function */
3660132718Skan  debug_nothing_rtx,		/* label */
3661169689Skan  debug_nothing_int,		/* handle_pch */
3662169689Skan  dwarf2out_var_location,
3663169689Skan  dwarf2out_switch_text_section,
3664169689Skan  1                             /* start_end_main_source_file */
366590075Sobrien};
3666132718Skan#endif
366790075Sobrien
366890075Sobrien/* NOTE: In the comments in this file, many references are made to
366990075Sobrien   "Debugging Information Entries".  This term is abbreviated as `DIE'
367090075Sobrien   throughout the remainder of this file.  */
367190075Sobrien
367290075Sobrien/* An internal representation of the DWARF output is built, and then
367390075Sobrien   walked to generate the DWARF debugging info.  The walk of the internal
367490075Sobrien   representation is done after the entire program has been compiled.
367590075Sobrien   The types below are used to describe the internal representation.  */
367690075Sobrien
367790075Sobrien/* Various DIE's use offsets relative to the beginning of the
367890075Sobrien   .debug_info section to refer to each other.  */
367990075Sobrien
368090075Sobrientypedef long int dw_offset;
368190075Sobrien
368290075Sobrien/* Define typedefs here to avoid circular dependencies.  */
368390075Sobrien
368490075Sobrientypedef struct dw_attr_struct *dw_attr_ref;
368590075Sobrientypedef struct dw_line_info_struct *dw_line_info_ref;
368690075Sobrientypedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
368790075Sobrientypedef struct pubname_struct *pubname_ref;
368890075Sobrientypedef struct dw_ranges_struct *dw_ranges_ref;
368990075Sobrien
369090075Sobrien/* Each entry in the line_info_table maintains the file and
369190075Sobrien   line number associated with the label generated for that
369290075Sobrien   entry.  The label gives the PC value associated with
369390075Sobrien   the line number entry.  */
369490075Sobrien
3695132718Skantypedef struct dw_line_info_struct GTY(())
369690075Sobrien{
369790075Sobrien  unsigned long dw_file_num;
369890075Sobrien  unsigned long dw_line_num;
369990075Sobrien}
370090075Sobriendw_line_info_entry;
370190075Sobrien
370290075Sobrien/* Line information for functions in separate sections; each one gets its
370390075Sobrien   own sequence.  */
3704132718Skantypedef struct dw_separate_line_info_struct GTY(())
370590075Sobrien{
370690075Sobrien  unsigned long dw_file_num;
370790075Sobrien  unsigned long dw_line_num;
370890075Sobrien  unsigned long function;
370990075Sobrien}
371090075Sobriendw_separate_line_info_entry;
371190075Sobrien
371250397Sobrien/* Each DIE attribute has a field specifying the attribute kind,
371350397Sobrien   a link to the next attribute in the chain, and an attribute value.
371450397Sobrien   Attributes are typically linked below the DIE they modify.  */
371550397Sobrien
3716132718Skantypedef struct dw_attr_struct GTY(())
371750397Sobrien{
371850397Sobrien  enum dwarf_attribute dw_attr;
371950397Sobrien  dw_val_node dw_attr_val;
372050397Sobrien}
372150397Sobriendw_attr_node;
372250397Sobrien
3723169689SkanDEF_VEC_O(dw_attr_node);
3724169689SkanDEF_VEC_ALLOC_O(dw_attr_node,gc);
372550397Sobrien
3726169689Skan/* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3727169689Skan   The children of each node form a circular list linked by
3728169689Skan   die_sib.  die_child points to the node *before* the "first" child node.  */
3729169689Skan
3730132718Skantypedef struct die_struct GTY(())
373150397Sobrien{
373250397Sobrien  enum dwarf_tag die_tag;
373390075Sobrien  char *die_symbol;
3734169689Skan  VEC(dw_attr_node,gc) * die_attr;
373550397Sobrien  dw_die_ref die_parent;
373650397Sobrien  dw_die_ref die_child;
373750397Sobrien  dw_die_ref die_sib;
3738132718Skan  dw_die_ref die_definition; /* ref from a specification to its definition */
373950397Sobrien  dw_offset die_offset;
374050397Sobrien  unsigned long die_abbrev;
374190075Sobrien  int die_mark;
3742169689Skan  /* Die is used and must not be pruned as unused.  */
3743169689Skan  int die_perennial_p;
3744169689Skan  unsigned int decl_id;
374550397Sobrien}
374650397Sobriendie_node;
374750397Sobrien
3748169689Skan/* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3749169689Skan#define FOR_EACH_CHILD(die, c, expr) do {	\
3750169689Skan  c = die->die_child;				\
3751169689Skan  if (c) do {					\
3752169689Skan    c = c->die_sib;				\
3753169689Skan    expr;					\
3754169689Skan  } while (c != die->die_child);		\
3755169689Skan} while (0)
3756169689Skan
375750397Sobrien/* The pubname structure */
375850397Sobrien
3759132718Skantypedef struct pubname_struct GTY(())
376050397Sobrien{
376150397Sobrien  dw_die_ref die;
376290075Sobrien  char *name;
376350397Sobrien}
376450397Sobrienpubname_entry;
376550397Sobrien
3766260396SpfgDEF_VEC_O(pubname_entry);
3767260396SpfgDEF_VEC_ALLOC_O(pubname_entry, gc);
3768260396Spfg
3769132718Skanstruct dw_ranges_struct GTY(())
377090075Sobrien{
377190075Sobrien  int block_num;
377290075Sobrien};
377390075Sobrien
377450397Sobrien/* The limbo die list structure.  */
3775132718Skantypedef struct limbo_die_struct GTY(())
377650397Sobrien{
377750397Sobrien  dw_die_ref die;
377890075Sobrien  tree created_for;
377950397Sobrien  struct limbo_die_struct *next;
378050397Sobrien}
378150397Sobrienlimbo_die_node;
378250397Sobrien
378350397Sobrien/* How to start an assembler comment.  */
378450397Sobrien#ifndef ASM_COMMENT_START
378550397Sobrien#define ASM_COMMENT_START ";#"
378650397Sobrien#endif
378750397Sobrien
3788117395Skan/* Define a macro which returns nonzero for a TYPE_DECL which was
378950397Sobrien   implicitly generated for a tagged type.
379050397Sobrien
379150397Sobrien   Note that unlike the gcc front end (which generates a NULL named
379250397Sobrien   TYPE_DECL node for each complete tagged type, each array type, and
379350397Sobrien   each function type node created) the g++ front end generates a
379450397Sobrien   _named_ TYPE_DECL node for each tagged type node created.
379550397Sobrien   These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
379650397Sobrien   generate a DW_TAG_typedef DIE for them.  */
379750397Sobrien
379850397Sobrien#define TYPE_DECL_IS_STUB(decl)				\
379950397Sobrien  (DECL_NAME (decl) == NULL_TREE			\
380050397Sobrien   || (DECL_ARTIFICIAL (decl)				\
380150397Sobrien       && is_tagged_type (TREE_TYPE (decl))		\
380250397Sobrien       && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))	\
380350397Sobrien	   /* This is necessary for stub decls that	\
380450397Sobrien	      appear in nested inline functions.  */	\
380550397Sobrien	   || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE	\
380650397Sobrien	       && (decl_ultimate_origin (decl)		\
380750397Sobrien		   == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
380850397Sobrien
380950397Sobrien/* Information concerning the compilation unit's programming
381050397Sobrien   language, and compiler version.  */
381150397Sobrien
381250397Sobrien/* Fixed size portion of the DWARF compilation unit header.  */
3813132718Skan#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3814132718Skan  (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
381550397Sobrien
381650397Sobrien/* Fixed size portion of public names info.  */
381750397Sobrien#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
381850397Sobrien
381950397Sobrien/* Fixed size portion of the address range info.  */
382090075Sobrien#define DWARF_ARANGES_HEADER_SIZE					\
3821132718Skan  (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,	\
3822132718Skan                DWARF2_ADDR_SIZE * 2)					\
3823132718Skan   - DWARF_INITIAL_LENGTH_SIZE)
382450397Sobrien
382590075Sobrien/* Size of padding portion in the address range info.  It must be
382690075Sobrien   aligned to twice the pointer size.  */
382790075Sobrien#define DWARF_ARANGES_PAD_SIZE \
3828132718Skan  (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3829132718Skan                DWARF2_ADDR_SIZE * 2) \
3830132718Skan   - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
383190075Sobrien
383290075Sobrien/* Use assembler line directives if available.  */
383390075Sobrien#ifndef DWARF2_ASM_LINE_DEBUG_INFO
383490075Sobrien#ifdef HAVE_AS_DWARF2_DEBUG_LINE
383590075Sobrien#define DWARF2_ASM_LINE_DEBUG_INFO 1
383690075Sobrien#else
383790075Sobrien#define DWARF2_ASM_LINE_DEBUG_INFO 0
383890075Sobrien#endif
383990075Sobrien#endif
384090075Sobrien
384150397Sobrien/* Minimum line offset in a special line info. opcode.
384250397Sobrien   This value was chosen to give a reasonable range of values.  */
384350397Sobrien#define DWARF_LINE_BASE  -10
384450397Sobrien
384590075Sobrien/* First special line opcode - leave room for the standard opcodes.  */
384650397Sobrien#define DWARF_LINE_OPCODE_BASE  10
384750397Sobrien
384850397Sobrien/* Range of line offsets in a special line info. opcode.  */
384950397Sobrien#define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
385050397Sobrien
385150397Sobrien/* Flag that indicates the initial value of the is_stmt_start flag.
385250397Sobrien   In the present implementation, we do not mark any lines as
385350397Sobrien   the beginning of a source statement, because that information
385450397Sobrien   is not made available by the GCC front-end.  */
385550397Sobrien#define	DWARF_LINE_DEFAULT_IS_STMT_START 1
385650397Sobrien
3857132718Skan#ifdef DWARF2_DEBUGGING_INFO
385850397Sobrien/* This location is used by calc_die_sizes() to keep track
385950397Sobrien   the offset of each DIE within the .debug_info section.  */
386050397Sobrienstatic unsigned long next_die_offset;
3861132718Skan#endif
386250397Sobrien
386350397Sobrien/* Record the root of the DIE's built for the current compilation unit.  */
3864132718Skanstatic GTY(()) dw_die_ref comp_unit_die;
386550397Sobrien
386650397Sobrien/* A list of DIEs with a NULL parent waiting to be relocated.  */
3867132718Skanstatic GTY(()) limbo_die_node *limbo_die_list;
386850397Sobrien
386990075Sobrien/* Filenames referenced by this compilation unit.  */
3870169689Skanstatic GTY((param_is (struct dwarf_file_data))) htab_t file_table;
387190075Sobrien
3872169689Skan/* A hash table of references to DIE's that describe declarations.
3873169689Skan   The key is a DECL_UID() which is a unique number identifying each decl.  */
3874169689Skanstatic GTY ((param_is (struct die_struct))) htab_t decl_die_table;
387550397Sobrien
3876169689Skan/* Node of the variable location list.  */
3877169689Skanstruct var_loc_node GTY ((chain_next ("%h.next")))
3878169689Skan{
3879169689Skan  rtx GTY (()) var_loc_note;
3880169689Skan  const char * GTY (()) label;
3881169689Skan  const char * GTY (()) section_label;
3882169689Skan  struct var_loc_node * GTY (()) next;
3883169689Skan};
388450397Sobrien
3885169689Skan/* Variable location list.  */
3886169689Skanstruct var_loc_list_def GTY (())
3887169689Skan{
3888169689Skan  struct var_loc_node * GTY (()) first;
388950397Sobrien
3890169689Skan  /* Do not mark the last element of the chained list because
3891169689Skan     it is marked through the chain.  */
3892169689Skan  struct var_loc_node * GTY ((skip ("%h"))) last;
389350397Sobrien
3894169689Skan  /* DECL_UID of the variable decl.  */
3895169689Skan  unsigned int decl_id;
3896169689Skan};
3897169689Skantypedef struct var_loc_list_def var_loc_list;
3898169689Skan
3899169689Skan
3900169689Skan/* Table of decl location linked lists.  */
3901169689Skanstatic GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3902169689Skan
390350397Sobrien/* A pointer to the base of a list of references to DIE's that
390450397Sobrien   are uniquely identified by their tag, presence/absence of
390550397Sobrien   children DIE's, and list of attribute/value pairs.  */
3906132718Skanstatic GTY((length ("abbrev_die_table_allocated")))
3907132718Skan  dw_die_ref *abbrev_die_table;
390850397Sobrien
390950397Sobrien/* Number of elements currently allocated for abbrev_die_table.  */
3910132718Skanstatic GTY(()) unsigned abbrev_die_table_allocated;
391150397Sobrien
391250397Sobrien/* Number of elements in type_die_table currently in use.  */
3913132718Skanstatic GTY(()) unsigned abbrev_die_table_in_use;
391450397Sobrien
391550397Sobrien/* Size (in elements) of increments by which we may expand the
391650397Sobrien   abbrev_die_table.  */
391750397Sobrien#define ABBREV_DIE_TABLE_INCREMENT 256
391850397Sobrien
391950397Sobrien/* A pointer to the base of a table that contains line information
392050397Sobrien   for each source code line in .text in the compilation unit.  */
3921132718Skanstatic GTY((length ("line_info_table_allocated")))
3922132718Skan     dw_line_info_ref line_info_table;
392350397Sobrien
392450397Sobrien/* Number of elements currently allocated for line_info_table.  */
3925132718Skanstatic GTY(()) unsigned line_info_table_allocated;
392650397Sobrien
3927132718Skan/* Number of elements in line_info_table currently in use.  */
3928132718Skanstatic GTY(()) unsigned line_info_table_in_use;
392950397Sobrien
3930169689Skan/* True if the compilation unit places functions in more than one section.  */
3931169689Skanstatic GTY(()) bool have_multiple_function_sections = false;
3932169689Skan
393350397Sobrien/* A pointer to the base of a table that contains line information
393450397Sobrien   for each source code line outside of .text in the compilation unit.  */
3935132718Skanstatic GTY ((length ("separate_line_info_table_allocated")))
3936132718Skan     dw_separate_line_info_ref separate_line_info_table;
393750397Sobrien
393850397Sobrien/* Number of elements currently allocated for separate_line_info_table.  */
3939132718Skanstatic GTY(()) unsigned separate_line_info_table_allocated;
394050397Sobrien
3941132718Skan/* Number of elements in separate_line_info_table currently in use.  */
3942132718Skanstatic GTY(()) unsigned separate_line_info_table_in_use;
394350397Sobrien
394450397Sobrien/* Size (in elements) of increments by which we may expand the
394550397Sobrien   line_info_table.  */
394650397Sobrien#define LINE_INFO_TABLE_INCREMENT 1024
394750397Sobrien
394850397Sobrien/* A pointer to the base of a table that contains a list of publicly
394950397Sobrien   accessible names.  */
3950260396Spfgstatic GTY (()) VEC (pubname_entry, gc) *  pubname_table;
395150397Sobrien
3952260396Spfg/* A pointer to the base of a table that contains a list of publicly
3953260396Spfg   accessible types.  */
3954260396Spfgstatic GTY (()) VEC (pubname_entry, gc) * pubtype_table;
395550397Sobrien
395690075Sobrien/* Array of dies for which we should generate .debug_arange info.  */
3957132718Skanstatic GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
395850397Sobrien
395950397Sobrien/* Number of elements currently allocated for arange_table.  */
3960132718Skanstatic GTY(()) unsigned arange_table_allocated;
396150397Sobrien
396250397Sobrien/* Number of elements in arange_table currently in use.  */
3963132718Skanstatic GTY(()) unsigned arange_table_in_use;
396450397Sobrien
396550397Sobrien/* Size (in elements) of increments by which we may expand the
396650397Sobrien   arange_table.  */
396750397Sobrien#define ARANGE_TABLE_INCREMENT 64
396850397Sobrien
396990075Sobrien/* Array of dies for which we should generate .debug_ranges info.  */
3970132718Skanstatic GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
397150397Sobrien
397290075Sobrien/* Number of elements currently allocated for ranges_table.  */
3973132718Skanstatic GTY(()) unsigned ranges_table_allocated;
397450397Sobrien
397590075Sobrien/* Number of elements in ranges_table currently in use.  */
3976132718Skanstatic GTY(()) unsigned ranges_table_in_use;
397750397Sobrien
397890075Sobrien/* Size (in elements) of increments by which we may expand the
397990075Sobrien   ranges_table.  */
398090075Sobrien#define RANGES_TABLE_INCREMENT 64
398150397Sobrien
398290075Sobrien/* Whether we have location lists that need outputting */
3983169689Skanstatic GTY(()) bool have_location_lists;
398450397Sobrien
3985169689Skan/* Unique label counter.  */
3986169689Skanstatic GTY(()) unsigned int loclabel_num;
3987169689Skan
3988132718Skan#ifdef DWARF2_DEBUGGING_INFO
398950397Sobrien/* Record whether the function being analyzed contains inlined functions.  */
399050397Sobrienstatic int current_function_has_inlines;
3991132718Skan#endif
399250397Sobrien#if 0 && defined (MIPS_DEBUGGING_INFO)
399350397Sobrienstatic int comp_unit_has_inlines;
399450397Sobrien#endif
399550397Sobrien
3996169689Skan/* The last file entry emitted by maybe_emit_file().  */
3997169689Skanstatic GTY(()) struct dwarf_file_data * last_emitted_file;
3998132718Skan
3999132718Skan/* Number of internal labels generated by gen_internal_sym().  */
4000132718Skanstatic GTY(()) int label_num;
4001132718Skan
4002169689Skan/* Cached result of previous call to lookup_filename.  */
4003169689Skanstatic GTY(()) struct dwarf_file_data * file_table_last_lookup;
4004169689Skan
4005132718Skan#ifdef DWARF2_DEBUGGING_INFO
4006132718Skan
4007169689Skan/* Offset from the "steady-state frame pointer" to the frame base,
4008169689Skan   within the current function.  */
4009169689Skanstatic HOST_WIDE_INT frame_pointer_fb_offset;
4010169689Skan
401150397Sobrien/* Forward declarations for functions defined in this file.  */
401250397Sobrien
4013132718Skanstatic int is_pseudo_reg (rtx);
4014132718Skanstatic tree type_main_variant (tree);
4015132718Skanstatic int is_tagged_type (tree);
4016132718Skanstatic const char *dwarf_tag_name (unsigned);
4017132718Skanstatic const char *dwarf_attr_name (unsigned);
4018132718Skanstatic const char *dwarf_form_name (unsigned);
4019132718Skanstatic tree decl_ultimate_origin (tree);
4020132718Skanstatic tree block_ultimate_origin (tree);
4021132718Skanstatic tree decl_class_context (tree);
4022132718Skanstatic void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4023132718Skanstatic inline enum dw_val_class AT_class (dw_attr_ref);
4024132718Skanstatic void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4025132718Skanstatic inline unsigned AT_flag (dw_attr_ref);
4026132718Skanstatic void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4027132718Skanstatic inline HOST_WIDE_INT AT_int (dw_attr_ref);
4028132718Skanstatic void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4029132718Skanstatic inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4030132718Skanstatic void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4031132718Skan			      unsigned long);
4032132718Skanstatic inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4033132718Skan			       unsigned int, unsigned char *);
4034132718Skanstatic hashval_t debug_str_do_hash (const void *);
4035132718Skanstatic int debug_str_eq (const void *, const void *);
4036132718Skanstatic void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4037132718Skanstatic inline const char *AT_string (dw_attr_ref);
4038132718Skanstatic int AT_string_form (dw_attr_ref);
4039132718Skanstatic void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4040132718Skanstatic void add_AT_specification (dw_die_ref, dw_die_ref);
4041132718Skanstatic inline dw_die_ref AT_ref (dw_attr_ref);
4042132718Skanstatic inline int AT_ref_external (dw_attr_ref);
4043132718Skanstatic inline void set_AT_ref_external (dw_attr_ref, int);
4044132718Skanstatic void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4045132718Skanstatic void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4046132718Skanstatic inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4047132718Skanstatic void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4048132718Skan			     dw_loc_list_ref);
4049132718Skanstatic inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4050132718Skanstatic void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4051132718Skanstatic inline rtx AT_addr (dw_attr_ref);
4052132718Skanstatic void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4053169689Skanstatic void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4054169689Skanstatic void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4055132718Skanstatic void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4056132718Skan			   unsigned HOST_WIDE_INT);
4057132718Skanstatic void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4058132718Skan			       unsigned long);
4059132718Skanstatic inline const char *AT_lbl (dw_attr_ref);
4060132718Skanstatic dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4061132718Skanstatic const char *get_AT_low_pc (dw_die_ref);
4062132718Skanstatic const char *get_AT_hi_pc (dw_die_ref);
4063132718Skanstatic const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4064132718Skanstatic int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4065132718Skanstatic unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4066132718Skanstatic inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4067132718Skanstatic bool is_c_family (void);
4068132718Skanstatic bool is_cxx (void);
4069132718Skanstatic bool is_java (void);
4070132718Skanstatic bool is_fortran (void);
4071132718Skanstatic bool is_ada (void);
4072132718Skanstatic void remove_AT (dw_die_ref, enum dwarf_attribute);
4073146895Skanstatic void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4074132718Skanstatic void add_child_die (dw_die_ref, dw_die_ref);
4075132718Skanstatic dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4076132718Skanstatic dw_die_ref lookup_type_die (tree);
4077132718Skanstatic void equate_type_number_to_die (tree, dw_die_ref);
4078169689Skanstatic hashval_t decl_die_table_hash (const void *);
4079169689Skanstatic int decl_die_table_eq (const void *, const void *);
4080132718Skanstatic dw_die_ref lookup_decl_die (tree);
4081169689Skanstatic hashval_t decl_loc_table_hash (const void *);
4082169689Skanstatic int decl_loc_table_eq (const void *, const void *);
4083169689Skanstatic var_loc_list *lookup_decl_loc (tree);
4084132718Skanstatic void equate_decl_number_to_die (tree, dw_die_ref);
4085169689Skanstatic void add_var_loc_to_decl (tree, struct var_loc_node *);
4086132718Skanstatic void print_spaces (FILE *);
4087132718Skanstatic void print_die (dw_die_ref, FILE *);
4088132718Skanstatic void print_dwarf_line_table (FILE *);
4089132718Skanstatic dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4090132718Skanstatic dw_die_ref pop_compile_unit (dw_die_ref);
4091132718Skanstatic void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4092132718Skanstatic void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4093132718Skanstatic void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4094132718Skanstatic int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4095132718Skanstatic int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4096132718Skanstatic int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4097132718Skanstatic int same_die_p (dw_die_ref, dw_die_ref, int *);
4098132718Skanstatic int same_die_p_wrap (dw_die_ref, dw_die_ref);
4099132718Skanstatic void compute_section_prefix (dw_die_ref);
4100132718Skanstatic int is_type_die (dw_die_ref);
4101132718Skanstatic int is_comdat_die (dw_die_ref);
4102132718Skanstatic int is_symbol_die (dw_die_ref);
4103132718Skanstatic void assign_symbol_names (dw_die_ref);
4104132718Skanstatic void break_out_includes (dw_die_ref);
4105132718Skanstatic hashval_t htab_cu_hash (const void *);
4106132718Skanstatic int htab_cu_eq (const void *, const void *);
4107132718Skanstatic void htab_cu_del (void *);
4108132718Skanstatic int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4109132718Skanstatic void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4110132718Skanstatic void add_sibling_attributes (dw_die_ref);
4111132718Skanstatic void build_abbrev_table (dw_die_ref);
4112132718Skanstatic void output_location_lists (dw_die_ref);
4113132718Skanstatic int constant_size (long unsigned);
4114132718Skanstatic unsigned long size_of_die (dw_die_ref);
4115132718Skanstatic void calc_die_sizes (dw_die_ref);
4116132718Skanstatic void mark_dies (dw_die_ref);
4117132718Skanstatic void unmark_dies (dw_die_ref);
4118132718Skanstatic void unmark_all_dies (dw_die_ref);
4119260396Spfgstatic unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4120132718Skanstatic unsigned long size_of_aranges (void);
4121132718Skanstatic enum dwarf_form value_format (dw_attr_ref);
4122132718Skanstatic void output_value_format (dw_attr_ref);
4123132718Skanstatic void output_abbrev_section (void);
4124132718Skanstatic void output_die_symbol (dw_die_ref);
4125132718Skanstatic void output_die (dw_die_ref);
4126132718Skanstatic void output_compilation_unit_header (void);
4127132718Skanstatic void output_comp_unit (dw_die_ref, int);
4128132718Skanstatic const char *dwarf2_name (tree, int);
4129132718Skanstatic void add_pubname (tree, dw_die_ref);
4130260396Spfgstatic void add_pubtype (tree, dw_die_ref);
4131260396Spfgstatic void output_pubnames (VEC (pubname_entry,gc) *);
4132132718Skanstatic void add_arange (tree, dw_die_ref);
4133132718Skanstatic void output_aranges (void);
4134132718Skanstatic unsigned int add_ranges (tree);
4135132718Skanstatic void output_ranges (void);
4136132718Skanstatic void output_line_info (void);
4137132718Skanstatic void output_file_names (void);
4138132718Skanstatic dw_die_ref base_type_die (tree);
4139132718Skanstatic tree root_type (tree);
4140132718Skanstatic int is_base_type (tree);
4141132718Skanstatic bool is_subrange_type (tree);
4142132718Skanstatic dw_die_ref subrange_type_die (tree, dw_die_ref);
4143132718Skanstatic dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4144132718Skanstatic int type_is_enum (tree);
4145132718Skanstatic unsigned int dbx_reg_number (rtx);
4146169689Skanstatic void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4147132718Skanstatic dw_loc_descr_ref reg_loc_descriptor (rtx);
4148132718Skanstatic dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4149132718Skanstatic dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4150132718Skanstatic dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4151169689Skanstatic dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4152132718Skanstatic int is_based_loc (rtx);
4153132718Skanstatic dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4154132718Skanstatic dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4155132718Skanstatic dw_loc_descr_ref loc_descriptor (rtx);
4156169689Skanstatic dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4157169689Skanstatic dw_loc_descr_ref loc_descriptor_from_tree (tree);
4158132718Skanstatic HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4159132718Skanstatic tree field_type (tree);
4160132718Skanstatic unsigned int simple_type_align_in_bits (tree);
4161132718Skanstatic unsigned int simple_decl_align_in_bits (tree);
4162132718Skanstatic unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4163132718Skanstatic HOST_WIDE_INT field_byte_offset (tree);
4164132718Skanstatic void add_AT_location_description	(dw_die_ref, enum dwarf_attribute,
4165132718Skan					 dw_loc_descr_ref);
4166132718Skanstatic void add_data_member_location_attribute (dw_die_ref, tree);
4167132718Skanstatic void add_const_value_attribute (dw_die_ref, rtx);
4168132718Skanstatic void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4169132718Skanstatic HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4170132718Skanstatic void insert_float (rtx, unsigned char *);
4171132718Skanstatic rtx rtl_for_decl_location (tree);
4172169689Skanstatic void add_location_or_const_value_attribute (dw_die_ref, tree,
4173169689Skan						   enum dwarf_attribute);
4174132718Skanstatic void tree_add_const_value_attribute (dw_die_ref, tree);
4175132718Skanstatic void add_name_attribute (dw_die_ref, const char *);
4176132718Skanstatic void add_comp_dir_attribute (dw_die_ref);
4177132718Skanstatic void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4178132718Skanstatic void add_subscript_info (dw_die_ref, tree);
4179132718Skanstatic void add_byte_size_attribute (dw_die_ref, tree);
4180132718Skanstatic void add_bit_offset_attribute (dw_die_ref, tree);
4181132718Skanstatic void add_bit_size_attribute (dw_die_ref, tree);
4182132718Skanstatic void add_prototyped_attribute (dw_die_ref, tree);
4183132718Skanstatic void add_abstract_origin_attribute (dw_die_ref, tree);
4184132718Skanstatic void add_pure_or_virtual_attribute (dw_die_ref, tree);
4185132718Skanstatic void add_src_coords_attributes (dw_die_ref, tree);
4186132718Skanstatic void add_name_and_src_coords_attributes (dw_die_ref, tree);
4187132718Skanstatic void push_decl_scope (tree);
4188132718Skanstatic void pop_decl_scope (void);
4189132718Skanstatic dw_die_ref scope_die_for (tree, dw_die_ref);
4190132718Skanstatic inline int local_scope_p (dw_die_ref);
4191132718Skanstatic inline int class_or_namespace_scope_p (dw_die_ref);
4192132718Skanstatic void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4193169689Skanstatic void add_calling_convention_attribute (dw_die_ref, tree);
4194132718Skanstatic const char *type_tag (tree);
4195132718Skanstatic tree member_declared_type (tree);
419650397Sobrien#if 0
4197132718Skanstatic const char *decl_start_label (tree);
419850397Sobrien#endif
4199132718Skanstatic void gen_array_type_die (tree, dw_die_ref);
420050397Sobrien#if 0
4201132718Skanstatic void gen_entry_point_die (tree, dw_die_ref);
420250397Sobrien#endif
4203132718Skanstatic void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4204132718Skanstatic void gen_inlined_structure_type_die (tree, dw_die_ref);
4205132718Skanstatic void gen_inlined_union_type_die (tree, dw_die_ref);
4206132718Skanstatic dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4207132718Skanstatic dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4208132718Skanstatic void gen_unspecified_parameters_die (tree, dw_die_ref);
4209132718Skanstatic void gen_formal_types_die (tree, dw_die_ref);
4210132718Skanstatic void gen_subprogram_die (tree, dw_die_ref);
4211132718Skanstatic void gen_variable_die (tree, dw_die_ref);
4212132718Skanstatic void gen_label_die (tree, dw_die_ref);
4213132718Skanstatic void gen_lexical_block_die (tree, dw_die_ref, int);
4214132718Skanstatic void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4215132718Skanstatic void gen_field_die (tree, dw_die_ref);
4216132718Skanstatic void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4217132718Skanstatic dw_die_ref gen_compile_unit_die (const char *);
4218132718Skanstatic void gen_inheritance_die (tree, tree, dw_die_ref);
4219132718Skanstatic void gen_member_die (tree, dw_die_ref);
4220259269Spfgstatic void gen_struct_or_union_type_die (tree, dw_die_ref,
4221259269Spfg						enum debug_info_usage);
4222132718Skanstatic void gen_subroutine_type_die (tree, dw_die_ref);
4223132718Skanstatic void gen_typedef_die (tree, dw_die_ref);
4224132718Skanstatic void gen_type_die (tree, dw_die_ref);
4225132718Skanstatic void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4226132718Skanstatic void gen_block_die (tree, dw_die_ref, int);
4227132718Skanstatic void decls_for_scope (tree, dw_die_ref, int);
4228132718Skanstatic int is_redundant_typedef (tree);
4229132718Skanstatic void gen_namespace_die (tree);
4230132718Skanstatic void gen_decl_die (tree, dw_die_ref);
4231169689Skanstatic dw_die_ref force_decl_die (tree);
4232169689Skanstatic dw_die_ref force_type_die (tree);
4233132718Skanstatic dw_die_ref setup_namespace_context (tree, dw_die_ref);
4234132718Skanstatic void declare_in_namespace (tree, dw_die_ref);
4235169689Skanstatic struct dwarf_file_data * lookup_filename (const char *);
4236132718Skanstatic void retry_incomplete_types (void);
4237132718Skanstatic void gen_type_die_for_member (tree, tree, dw_die_ref);
4238132718Skanstatic void splice_child_die (dw_die_ref, dw_die_ref);
4239132718Skanstatic int file_info_cmp (const void *, const void *);
4240132718Skanstatic dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4241132718Skan				     const char *, const char *, unsigned);
4242132718Skanstatic void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4243132718Skan				       const char *, const char *,
4244132718Skan				       const char *);
4245132718Skanstatic void output_loc_list (dw_loc_list_ref);
4246132718Skanstatic char *gen_internal_sym (const char *);
424750397Sobrien
4248132718Skanstatic void prune_unmark_dies (dw_die_ref);
4249132718Skanstatic void prune_unused_types_mark (dw_die_ref, int);
4250132718Skanstatic void prune_unused_types_walk (dw_die_ref);
4251132718Skanstatic void prune_unused_types_walk_attribs (dw_die_ref);
4252132718Skanstatic void prune_unused_types_prune (dw_die_ref);
4253132718Skanstatic void prune_unused_types (void);
4254169689Skanstatic int maybe_emit_file (struct dwarf_file_data *fd);
4255132718Skan
425650397Sobrien/* Section names used to hold DWARF debugging information.  */
425750397Sobrien#ifndef DEBUG_INFO_SECTION
425850397Sobrien#define DEBUG_INFO_SECTION	".debug_info"
425950397Sobrien#endif
426090075Sobrien#ifndef DEBUG_ABBREV_SECTION
426190075Sobrien#define DEBUG_ABBREV_SECTION	".debug_abbrev"
426250397Sobrien#endif
426390075Sobrien#ifndef DEBUG_ARANGES_SECTION
426490075Sobrien#define DEBUG_ARANGES_SECTION	".debug_aranges"
426550397Sobrien#endif
426690075Sobrien#ifndef DEBUG_MACINFO_SECTION
426790075Sobrien#define DEBUG_MACINFO_SECTION	".debug_macinfo"
426850397Sobrien#endif
426950397Sobrien#ifndef DEBUG_LINE_SECTION
427050397Sobrien#define DEBUG_LINE_SECTION	".debug_line"
427150397Sobrien#endif
427290075Sobrien#ifndef DEBUG_LOC_SECTION
427390075Sobrien#define DEBUG_LOC_SECTION	".debug_loc"
427450397Sobrien#endif
427590075Sobrien#ifndef DEBUG_PUBNAMES_SECTION
427690075Sobrien#define DEBUG_PUBNAMES_SECTION	".debug_pubnames"
427750397Sobrien#endif
427890075Sobrien#ifndef DEBUG_STR_SECTION
427990075Sobrien#define DEBUG_STR_SECTION	".debug_str"
428050397Sobrien#endif
428190075Sobrien#ifndef DEBUG_RANGES_SECTION
428290075Sobrien#define DEBUG_RANGES_SECTION	".debug_ranges"
428390075Sobrien#endif
428450397Sobrien
428550397Sobrien/* Standard ELF section names for compiled code and data.  */
428690075Sobrien#ifndef TEXT_SECTION_NAME
428790075Sobrien#define TEXT_SECTION_NAME	".text"
428850397Sobrien#endif
428990075Sobrien
429090075Sobrien/* Section flags for .debug_str section.  */
429190075Sobrien#define DEBUG_STR_SECTION_FLAGS \
4292132718Skan  (HAVE_GAS_SHF_MERGE && flag_merge_constants			\
4293132718Skan   ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1	\
4294132718Skan   : SECTION_DEBUG)
429550397Sobrien
429652284Sobrien/* Labels we insert at beginning sections we can reference instead of
429790075Sobrien   the section names themselves.  */
429850397Sobrien
429952284Sobrien#ifndef TEXT_SECTION_LABEL
430090075Sobrien#define TEXT_SECTION_LABEL		"Ltext"
430152284Sobrien#endif
4302169689Skan#ifndef COLD_TEXT_SECTION_LABEL
4303169689Skan#define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4304169689Skan#endif
430552284Sobrien#ifndef DEBUG_LINE_SECTION_LABEL
430690075Sobrien#define DEBUG_LINE_SECTION_LABEL	"Ldebug_line"
430752284Sobrien#endif
430852284Sobrien#ifndef DEBUG_INFO_SECTION_LABEL
430990075Sobrien#define DEBUG_INFO_SECTION_LABEL	"Ldebug_info"
431052284Sobrien#endif
431190075Sobrien#ifndef DEBUG_ABBREV_SECTION_LABEL
431290075Sobrien#define DEBUG_ABBREV_SECTION_LABEL	"Ldebug_abbrev"
431352284Sobrien#endif
431490075Sobrien#ifndef DEBUG_LOC_SECTION_LABEL
431590075Sobrien#define DEBUG_LOC_SECTION_LABEL		"Ldebug_loc"
431690075Sobrien#endif
431790075Sobrien#ifndef DEBUG_RANGES_SECTION_LABEL
431890075Sobrien#define DEBUG_RANGES_SECTION_LABEL	"Ldebug_ranges"
431990075Sobrien#endif
432090075Sobrien#ifndef DEBUG_MACINFO_SECTION_LABEL
432190075Sobrien#define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
432290075Sobrien#endif
432352284Sobrien
432450397Sobrien/* Definitions of defaults for formats and names of various special
432550397Sobrien   (artificial) labels which may be generated within this file (when the -g
4326132718Skan   options is used and DWARF2_DEBUGGING_INFO is in effect.
432750397Sobrien   If necessary, these may be overridden from within the tm.h file, but
432850397Sobrien   typically, overriding these defaults is unnecessary.  */
432950397Sobrien
433050397Sobrienstatic char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
433152284Sobrienstatic char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4332169689Skanstatic char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4333169689Skanstatic char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
433452284Sobrienstatic char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433552284Sobrienstatic char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433652284Sobrienstatic char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433790075Sobrienstatic char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433890075Sobrienstatic char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433990075Sobrienstatic char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
434050397Sobrien
434150397Sobrien#ifndef TEXT_END_LABEL
434250397Sobrien#define TEXT_END_LABEL		"Letext"
434350397Sobrien#endif
4344169689Skan#ifndef COLD_END_LABEL
4345169689Skan#define COLD_END_LABEL          "Letext_cold"
4346169689Skan#endif
434750397Sobrien#ifndef BLOCK_BEGIN_LABEL
434850397Sobrien#define BLOCK_BEGIN_LABEL	"LBB"
434950397Sobrien#endif
435050397Sobrien#ifndef BLOCK_END_LABEL
435150397Sobrien#define BLOCK_END_LABEL		"LBE"
435250397Sobrien#endif
435350397Sobrien#ifndef LINE_CODE_LABEL
435450397Sobrien#define LINE_CODE_LABEL		"LM"
435550397Sobrien#endif
435650397Sobrien#ifndef SEPARATE_LINE_CODE_LABEL
435750397Sobrien#define SEPARATE_LINE_CODE_LABEL	"LSM"
435850397Sobrien#endif
435950397Sobrien
436090075Sobrien/* We allow a language front-end to designate a function that is to be
4361169689Skan   called to "demangle" any name before it is put into a DIE.  */
436250397Sobrien
4363132718Skanstatic const char *(*demangle_name_func) (const char *);
436450397Sobrien
436590075Sobrienvoid
4366132718Skandwarf2out_set_demangle_name_func (const char *(*func) (const char *))
436750397Sobrien{
436890075Sobrien  demangle_name_func = func;
436950397Sobrien}
437050397Sobrien
437150397Sobrien/* Test if rtl node points to a pseudo register.  */
437250397Sobrien
437350397Sobrienstatic inline int
4374132718Skanis_pseudo_reg (rtx rtl)
437550397Sobrien{
4376169689Skan  return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
437790075Sobrien	  || (GET_CODE (rtl) == SUBREG
437890075Sobrien	      && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
437950397Sobrien}
438050397Sobrien
438150397Sobrien/* Return a reference to a type, with its const and volatile qualifiers
438250397Sobrien   removed.  */
438350397Sobrien
438450397Sobrienstatic inline tree
4385132718Skantype_main_variant (tree type)
438650397Sobrien{
438750397Sobrien  type = TYPE_MAIN_VARIANT (type);
438850397Sobrien
438990075Sobrien  /* ??? There really should be only one main variant among any group of
439090075Sobrien     variants of a given type (and all of the MAIN_VARIANT values for all
439190075Sobrien     members of the group should point to that one type) but sometimes the C
439290075Sobrien     front-end messes this up for array types, so we work around that bug
439390075Sobrien     here.  */
439450397Sobrien  if (TREE_CODE (type) == ARRAY_TYPE)
439550397Sobrien    while (type != TYPE_MAIN_VARIANT (type))
439650397Sobrien      type = TYPE_MAIN_VARIANT (type);
439750397Sobrien
439850397Sobrien  return type;
439950397Sobrien}
440050397Sobrien
4401117395Skan/* Return nonzero if the given type node represents a tagged type.  */
440250397Sobrien
440350397Sobrienstatic inline int
4404132718Skanis_tagged_type (tree type)
440550397Sobrien{
440690075Sobrien  enum tree_code code = TREE_CODE (type);
440750397Sobrien
440850397Sobrien  return (code == RECORD_TYPE || code == UNION_TYPE
440950397Sobrien	  || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
441050397Sobrien}
441150397Sobrien
441250397Sobrien/* Convert a DIE tag into its string name.  */
441350397Sobrien
441490075Sobrienstatic const char *
4415132718Skandwarf_tag_name (unsigned int tag)
441650397Sobrien{
441750397Sobrien  switch (tag)
441850397Sobrien    {
441950397Sobrien    case DW_TAG_padding:
442050397Sobrien      return "DW_TAG_padding";
442150397Sobrien    case DW_TAG_array_type:
442250397Sobrien      return "DW_TAG_array_type";
442350397Sobrien    case DW_TAG_class_type:
442450397Sobrien      return "DW_TAG_class_type";
442550397Sobrien    case DW_TAG_entry_point:
442650397Sobrien      return "DW_TAG_entry_point";
442750397Sobrien    case DW_TAG_enumeration_type:
442850397Sobrien      return "DW_TAG_enumeration_type";
442950397Sobrien    case DW_TAG_formal_parameter:
443050397Sobrien      return "DW_TAG_formal_parameter";
443150397Sobrien    case DW_TAG_imported_declaration:
443250397Sobrien      return "DW_TAG_imported_declaration";
443350397Sobrien    case DW_TAG_label:
443450397Sobrien      return "DW_TAG_label";
443550397Sobrien    case DW_TAG_lexical_block:
443650397Sobrien      return "DW_TAG_lexical_block";
443750397Sobrien    case DW_TAG_member:
443850397Sobrien      return "DW_TAG_member";
443950397Sobrien    case DW_TAG_pointer_type:
444050397Sobrien      return "DW_TAG_pointer_type";
444150397Sobrien    case DW_TAG_reference_type:
444250397Sobrien      return "DW_TAG_reference_type";
444350397Sobrien    case DW_TAG_compile_unit:
444450397Sobrien      return "DW_TAG_compile_unit";
444550397Sobrien    case DW_TAG_string_type:
444650397Sobrien      return "DW_TAG_string_type";
444750397Sobrien    case DW_TAG_structure_type:
444850397Sobrien      return "DW_TAG_structure_type";
444950397Sobrien    case DW_TAG_subroutine_type:
445050397Sobrien      return "DW_TAG_subroutine_type";
445150397Sobrien    case DW_TAG_typedef:
445250397Sobrien      return "DW_TAG_typedef";
445350397Sobrien    case DW_TAG_union_type:
445450397Sobrien      return "DW_TAG_union_type";
445550397Sobrien    case DW_TAG_unspecified_parameters:
445650397Sobrien      return "DW_TAG_unspecified_parameters";
445750397Sobrien    case DW_TAG_variant:
445850397Sobrien      return "DW_TAG_variant";
445950397Sobrien    case DW_TAG_common_block:
446050397Sobrien      return "DW_TAG_common_block";
446150397Sobrien    case DW_TAG_common_inclusion:
446250397Sobrien      return "DW_TAG_common_inclusion";
446350397Sobrien    case DW_TAG_inheritance:
446450397Sobrien      return "DW_TAG_inheritance";
446550397Sobrien    case DW_TAG_inlined_subroutine:
446650397Sobrien      return "DW_TAG_inlined_subroutine";
446750397Sobrien    case DW_TAG_module:
446850397Sobrien      return "DW_TAG_module";
446950397Sobrien    case DW_TAG_ptr_to_member_type:
447050397Sobrien      return "DW_TAG_ptr_to_member_type";
447150397Sobrien    case DW_TAG_set_type:
447250397Sobrien      return "DW_TAG_set_type";
447350397Sobrien    case DW_TAG_subrange_type:
447450397Sobrien      return "DW_TAG_subrange_type";
447550397Sobrien    case DW_TAG_with_stmt:
447650397Sobrien      return "DW_TAG_with_stmt";
447750397Sobrien    case DW_TAG_access_declaration:
447850397Sobrien      return "DW_TAG_access_declaration";
447950397Sobrien    case DW_TAG_base_type:
448050397Sobrien      return "DW_TAG_base_type";
448150397Sobrien    case DW_TAG_catch_block:
448250397Sobrien      return "DW_TAG_catch_block";
448350397Sobrien    case DW_TAG_const_type:
448450397Sobrien      return "DW_TAG_const_type";
448550397Sobrien    case DW_TAG_constant:
448650397Sobrien      return "DW_TAG_constant";
448750397Sobrien    case DW_TAG_enumerator:
448850397Sobrien      return "DW_TAG_enumerator";
448950397Sobrien    case DW_TAG_file_type:
449050397Sobrien      return "DW_TAG_file_type";
449150397Sobrien    case DW_TAG_friend:
449250397Sobrien      return "DW_TAG_friend";
449350397Sobrien    case DW_TAG_namelist:
449450397Sobrien      return "DW_TAG_namelist";
449550397Sobrien    case DW_TAG_namelist_item:
449650397Sobrien      return "DW_TAG_namelist_item";
4497132718Skan    case DW_TAG_namespace:
4498132718Skan      return "DW_TAG_namespace";
449950397Sobrien    case DW_TAG_packed_type:
450050397Sobrien      return "DW_TAG_packed_type";
450150397Sobrien    case DW_TAG_subprogram:
450250397Sobrien      return "DW_TAG_subprogram";
450350397Sobrien    case DW_TAG_template_type_param:
450450397Sobrien      return "DW_TAG_template_type_param";
450550397Sobrien    case DW_TAG_template_value_param:
450650397Sobrien      return "DW_TAG_template_value_param";
450750397Sobrien    case DW_TAG_thrown_type:
450850397Sobrien      return "DW_TAG_thrown_type";
450950397Sobrien    case DW_TAG_try_block:
451050397Sobrien      return "DW_TAG_try_block";
451150397Sobrien    case DW_TAG_variant_part:
451250397Sobrien      return "DW_TAG_variant_part";
451350397Sobrien    case DW_TAG_variable:
451450397Sobrien      return "DW_TAG_variable";
451550397Sobrien    case DW_TAG_volatile_type:
451650397Sobrien      return "DW_TAG_volatile_type";
4517169689Skan    case DW_TAG_imported_module:
4518169689Skan      return "DW_TAG_imported_module";
451950397Sobrien    case DW_TAG_MIPS_loop:
452050397Sobrien      return "DW_TAG_MIPS_loop";
452150397Sobrien    case DW_TAG_format_label:
452250397Sobrien      return "DW_TAG_format_label";
452350397Sobrien    case DW_TAG_function_template:
452450397Sobrien      return "DW_TAG_function_template";
452550397Sobrien    case DW_TAG_class_template:
452650397Sobrien      return "DW_TAG_class_template";
452790075Sobrien    case DW_TAG_GNU_BINCL:
452890075Sobrien      return "DW_TAG_GNU_BINCL";
452990075Sobrien    case DW_TAG_GNU_EINCL:
453090075Sobrien      return "DW_TAG_GNU_EINCL";
453150397Sobrien    default:
453250397Sobrien      return "DW_TAG_<unknown>";
453350397Sobrien    }
453450397Sobrien}
453550397Sobrien
453650397Sobrien/* Convert a DWARF attribute code into its string name.  */
453750397Sobrien
453890075Sobrienstatic const char *
4539132718Skandwarf_attr_name (unsigned int attr)
454050397Sobrien{
454150397Sobrien  switch (attr)
454250397Sobrien    {
454350397Sobrien    case DW_AT_sibling:
454450397Sobrien      return "DW_AT_sibling";
454550397Sobrien    case DW_AT_location:
454650397Sobrien      return "DW_AT_location";
454750397Sobrien    case DW_AT_name:
454850397Sobrien      return "DW_AT_name";
454950397Sobrien    case DW_AT_ordering:
455050397Sobrien      return "DW_AT_ordering";
455150397Sobrien    case DW_AT_subscr_data:
455250397Sobrien      return "DW_AT_subscr_data";
455350397Sobrien    case DW_AT_byte_size:
455450397Sobrien      return "DW_AT_byte_size";
455550397Sobrien    case DW_AT_bit_offset:
455650397Sobrien      return "DW_AT_bit_offset";
455750397Sobrien    case DW_AT_bit_size:
455850397Sobrien      return "DW_AT_bit_size";
455950397Sobrien    case DW_AT_element_list:
456050397Sobrien      return "DW_AT_element_list";
456150397Sobrien    case DW_AT_stmt_list:
456250397Sobrien      return "DW_AT_stmt_list";
456350397Sobrien    case DW_AT_low_pc:
456450397Sobrien      return "DW_AT_low_pc";
456550397Sobrien    case DW_AT_high_pc:
456650397Sobrien      return "DW_AT_high_pc";
456750397Sobrien    case DW_AT_language:
456850397Sobrien      return "DW_AT_language";
456950397Sobrien    case DW_AT_member:
457050397Sobrien      return "DW_AT_member";
457150397Sobrien    case DW_AT_discr:
457250397Sobrien      return "DW_AT_discr";
457350397Sobrien    case DW_AT_discr_value:
457450397Sobrien      return "DW_AT_discr_value";
457550397Sobrien    case DW_AT_visibility:
457650397Sobrien      return "DW_AT_visibility";
457750397Sobrien    case DW_AT_import:
457850397Sobrien      return "DW_AT_import";
457950397Sobrien    case DW_AT_string_length:
458050397Sobrien      return "DW_AT_string_length";
458150397Sobrien    case DW_AT_common_reference:
458250397Sobrien      return "DW_AT_common_reference";
458350397Sobrien    case DW_AT_comp_dir:
458450397Sobrien      return "DW_AT_comp_dir";
458550397Sobrien    case DW_AT_const_value:
458650397Sobrien      return "DW_AT_const_value";
458750397Sobrien    case DW_AT_containing_type:
458850397Sobrien      return "DW_AT_containing_type";
458950397Sobrien    case DW_AT_default_value:
459050397Sobrien      return "DW_AT_default_value";
459150397Sobrien    case DW_AT_inline:
459250397Sobrien      return "DW_AT_inline";
459350397Sobrien    case DW_AT_is_optional:
459450397Sobrien      return "DW_AT_is_optional";
459550397Sobrien    case DW_AT_lower_bound:
459650397Sobrien      return "DW_AT_lower_bound";
459750397Sobrien    case DW_AT_producer:
459850397Sobrien      return "DW_AT_producer";
459950397Sobrien    case DW_AT_prototyped:
460050397Sobrien      return "DW_AT_prototyped";
460150397Sobrien    case DW_AT_return_addr:
460250397Sobrien      return "DW_AT_return_addr";
460350397Sobrien    case DW_AT_start_scope:
460450397Sobrien      return "DW_AT_start_scope";
460550397Sobrien    case DW_AT_stride_size:
460650397Sobrien      return "DW_AT_stride_size";
460750397Sobrien    case DW_AT_upper_bound:
460850397Sobrien      return "DW_AT_upper_bound";
460950397Sobrien    case DW_AT_abstract_origin:
461050397Sobrien      return "DW_AT_abstract_origin";
461150397Sobrien    case DW_AT_accessibility:
461250397Sobrien      return "DW_AT_accessibility";
461350397Sobrien    case DW_AT_address_class:
461450397Sobrien      return "DW_AT_address_class";
461550397Sobrien    case DW_AT_artificial:
461650397Sobrien      return "DW_AT_artificial";
461750397Sobrien    case DW_AT_base_types:
461850397Sobrien      return "DW_AT_base_types";
461950397Sobrien    case DW_AT_calling_convention:
462050397Sobrien      return "DW_AT_calling_convention";
462150397Sobrien    case DW_AT_count:
462250397Sobrien      return "DW_AT_count";
462350397Sobrien    case DW_AT_data_member_location:
462450397Sobrien      return "DW_AT_data_member_location";
462550397Sobrien    case DW_AT_decl_column:
462650397Sobrien      return "DW_AT_decl_column";
462750397Sobrien    case DW_AT_decl_file:
462850397Sobrien      return "DW_AT_decl_file";
462950397Sobrien    case DW_AT_decl_line:
463050397Sobrien      return "DW_AT_decl_line";
463150397Sobrien    case DW_AT_declaration:
463250397Sobrien      return "DW_AT_declaration";
463350397Sobrien    case DW_AT_discr_list:
463450397Sobrien      return "DW_AT_discr_list";
463550397Sobrien    case DW_AT_encoding:
463650397Sobrien      return "DW_AT_encoding";
463750397Sobrien    case DW_AT_external:
463850397Sobrien      return "DW_AT_external";
463950397Sobrien    case DW_AT_frame_base:
464050397Sobrien      return "DW_AT_frame_base";
464150397Sobrien    case DW_AT_friend:
464250397Sobrien      return "DW_AT_friend";
464350397Sobrien    case DW_AT_identifier_case:
464450397Sobrien      return "DW_AT_identifier_case";
464550397Sobrien    case DW_AT_macro_info:
464650397Sobrien      return "DW_AT_macro_info";
464750397Sobrien    case DW_AT_namelist_items:
464850397Sobrien      return "DW_AT_namelist_items";
464950397Sobrien    case DW_AT_priority:
465050397Sobrien      return "DW_AT_priority";
465150397Sobrien    case DW_AT_segment:
465250397Sobrien      return "DW_AT_segment";
465350397Sobrien    case DW_AT_specification:
465450397Sobrien      return "DW_AT_specification";
465550397Sobrien    case DW_AT_static_link:
465650397Sobrien      return "DW_AT_static_link";
465750397Sobrien    case DW_AT_type:
465850397Sobrien      return "DW_AT_type";
465950397Sobrien    case DW_AT_use_location:
466050397Sobrien      return "DW_AT_use_location";
466150397Sobrien    case DW_AT_variable_parameter:
466250397Sobrien      return "DW_AT_variable_parameter";
466350397Sobrien    case DW_AT_virtuality:
466450397Sobrien      return "DW_AT_virtuality";
466550397Sobrien    case DW_AT_vtable_elem_location:
466650397Sobrien      return "DW_AT_vtable_elem_location";
466750397Sobrien
466890075Sobrien    case DW_AT_allocated:
466990075Sobrien      return "DW_AT_allocated";
467090075Sobrien    case DW_AT_associated:
467190075Sobrien      return "DW_AT_associated";
467290075Sobrien    case DW_AT_data_location:
467390075Sobrien      return "DW_AT_data_location";
467490075Sobrien    case DW_AT_stride:
467590075Sobrien      return "DW_AT_stride";
467690075Sobrien    case DW_AT_entry_pc:
467790075Sobrien      return "DW_AT_entry_pc";
467890075Sobrien    case DW_AT_use_UTF8:
467990075Sobrien      return "DW_AT_use_UTF8";
468090075Sobrien    case DW_AT_extension:
468190075Sobrien      return "DW_AT_extension";
468290075Sobrien    case DW_AT_ranges:
468390075Sobrien      return "DW_AT_ranges";
468490075Sobrien    case DW_AT_trampoline:
468590075Sobrien      return "DW_AT_trampoline";
468690075Sobrien    case DW_AT_call_column:
468790075Sobrien      return "DW_AT_call_column";
468890075Sobrien    case DW_AT_call_file:
468990075Sobrien      return "DW_AT_call_file";
469090075Sobrien    case DW_AT_call_line:
469190075Sobrien      return "DW_AT_call_line";
469290075Sobrien
469350397Sobrien    case DW_AT_MIPS_fde:
469450397Sobrien      return "DW_AT_MIPS_fde";
469550397Sobrien    case DW_AT_MIPS_loop_begin:
469650397Sobrien      return "DW_AT_MIPS_loop_begin";
469750397Sobrien    case DW_AT_MIPS_tail_loop_begin:
469850397Sobrien      return "DW_AT_MIPS_tail_loop_begin";
469950397Sobrien    case DW_AT_MIPS_epilog_begin:
470050397Sobrien      return "DW_AT_MIPS_epilog_begin";
470150397Sobrien    case DW_AT_MIPS_loop_unroll_factor:
470250397Sobrien      return "DW_AT_MIPS_loop_unroll_factor";
470350397Sobrien    case DW_AT_MIPS_software_pipeline_depth:
470450397Sobrien      return "DW_AT_MIPS_software_pipeline_depth";
470550397Sobrien    case DW_AT_MIPS_linkage_name:
470650397Sobrien      return "DW_AT_MIPS_linkage_name";
470750397Sobrien    case DW_AT_MIPS_stride:
470850397Sobrien      return "DW_AT_MIPS_stride";
470950397Sobrien    case DW_AT_MIPS_abstract_name:
471050397Sobrien      return "DW_AT_MIPS_abstract_name";
471150397Sobrien    case DW_AT_MIPS_clone_origin:
471250397Sobrien      return "DW_AT_MIPS_clone_origin";
471350397Sobrien    case DW_AT_MIPS_has_inlines:
471450397Sobrien      return "DW_AT_MIPS_has_inlines";
471550397Sobrien
471650397Sobrien    case DW_AT_sf_names:
471750397Sobrien      return "DW_AT_sf_names";
471850397Sobrien    case DW_AT_src_info:
471950397Sobrien      return "DW_AT_src_info";
472050397Sobrien    case DW_AT_mac_info:
472150397Sobrien      return "DW_AT_mac_info";
472250397Sobrien    case DW_AT_src_coords:
472350397Sobrien      return "DW_AT_src_coords";
472450397Sobrien    case DW_AT_body_begin:
472550397Sobrien      return "DW_AT_body_begin";
472650397Sobrien    case DW_AT_body_end:
472750397Sobrien      return "DW_AT_body_end";
472896263Sobrien    case DW_AT_GNU_vector:
472996263Sobrien      return "DW_AT_GNU_vector";
473096263Sobrien
473190075Sobrien    case DW_AT_VMS_rtnbeg_pd_address:
473290075Sobrien      return "DW_AT_VMS_rtnbeg_pd_address";
473390075Sobrien
473450397Sobrien    default:
473550397Sobrien      return "DW_AT_<unknown>";
473650397Sobrien    }
473750397Sobrien}
473850397Sobrien
473950397Sobrien/* Convert a DWARF value form code into its string name.  */
474050397Sobrien
474190075Sobrienstatic const char *
4742132718Skandwarf_form_name (unsigned int form)
474350397Sobrien{
474450397Sobrien  switch (form)
474550397Sobrien    {
474650397Sobrien    case DW_FORM_addr:
474750397Sobrien      return "DW_FORM_addr";
474850397Sobrien    case DW_FORM_block2:
474950397Sobrien      return "DW_FORM_block2";
475050397Sobrien    case DW_FORM_block4:
475150397Sobrien      return "DW_FORM_block4";
475250397Sobrien    case DW_FORM_data2:
475350397Sobrien      return "DW_FORM_data2";
475450397Sobrien    case DW_FORM_data4:
475550397Sobrien      return "DW_FORM_data4";
475650397Sobrien    case DW_FORM_data8:
475750397Sobrien      return "DW_FORM_data8";
475850397Sobrien    case DW_FORM_string:
475950397Sobrien      return "DW_FORM_string";
476050397Sobrien    case DW_FORM_block:
476150397Sobrien      return "DW_FORM_block";
476250397Sobrien    case DW_FORM_block1:
476350397Sobrien      return "DW_FORM_block1";
476450397Sobrien    case DW_FORM_data1:
476550397Sobrien      return "DW_FORM_data1";
476650397Sobrien    case DW_FORM_flag:
476750397Sobrien      return "DW_FORM_flag";
476850397Sobrien    case DW_FORM_sdata:
476950397Sobrien      return "DW_FORM_sdata";
477050397Sobrien    case DW_FORM_strp:
477150397Sobrien      return "DW_FORM_strp";
477250397Sobrien    case DW_FORM_udata:
477350397Sobrien      return "DW_FORM_udata";
477450397Sobrien    case DW_FORM_ref_addr:
477550397Sobrien      return "DW_FORM_ref_addr";
477650397Sobrien    case DW_FORM_ref1:
477750397Sobrien      return "DW_FORM_ref1";
477850397Sobrien    case DW_FORM_ref2:
477950397Sobrien      return "DW_FORM_ref2";
478050397Sobrien    case DW_FORM_ref4:
478150397Sobrien      return "DW_FORM_ref4";
478250397Sobrien    case DW_FORM_ref8:
478350397Sobrien      return "DW_FORM_ref8";
478450397Sobrien    case DW_FORM_ref_udata:
478550397Sobrien      return "DW_FORM_ref_udata";
478650397Sobrien    case DW_FORM_indirect:
478750397Sobrien      return "DW_FORM_indirect";
478850397Sobrien    default:
478950397Sobrien      return "DW_FORM_<unknown>";
479050397Sobrien    }
479150397Sobrien}
479250397Sobrien
479350397Sobrien/* Determine the "ultimate origin" of a decl.  The decl may be an inlined
479450397Sobrien   instance of an inlined instance of a decl which is local to an inline
479550397Sobrien   function, so we have to trace all of the way back through the origin chain
479650397Sobrien   to find out what sort of node actually served as the original seed for the
479750397Sobrien   given block.  */
479850397Sobrien
479950397Sobrienstatic tree
4800132718Skandecl_ultimate_origin (tree decl)
480150397Sobrien{
4802169689Skan  if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4803169689Skan    return NULL_TREE;
4804169689Skan
480590075Sobrien  /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
480690075Sobrien     nodes in the function to point to themselves; ignore that if
480790075Sobrien     we're trying to output the abstract instance of this function.  */
480890075Sobrien  if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
480990075Sobrien    return NULL_TREE;
481090075Sobrien
4811169689Skan  /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4812169689Skan     most distant ancestor, this should never happen.  */
4813169689Skan  gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
481450397Sobrien
481552284Sobrien  return DECL_ABSTRACT_ORIGIN (decl);
481650397Sobrien}
481750397Sobrien
481850397Sobrien/* Determine the "ultimate origin" of a block.  The block may be an inlined
481950397Sobrien   instance of an inlined instance of a block which is local to an inline
482050397Sobrien   function, so we have to trace all of the way back through the origin chain
482150397Sobrien   to find out what sort of node actually served as the original seed for the
482250397Sobrien   given block.  */
482350397Sobrien
482450397Sobrienstatic tree
4825132718Skanblock_ultimate_origin (tree block)
482650397Sobrien{
482790075Sobrien  tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
482850397Sobrien
482990075Sobrien  /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
483090075Sobrien     nodes in the function to point to themselves; ignore that if
483190075Sobrien     we're trying to output the abstract instance of this function.  */
483290075Sobrien  if (BLOCK_ABSTRACT (block) && immediate_origin == block)
483390075Sobrien    return NULL_TREE;
483490075Sobrien
483550397Sobrien  if (immediate_origin == NULL_TREE)
483650397Sobrien    return NULL_TREE;
483750397Sobrien  else
483850397Sobrien    {
483990075Sobrien      tree ret_val;
484090075Sobrien      tree lookahead = immediate_origin;
484150397Sobrien
484250397Sobrien      do
484350397Sobrien	{
484450397Sobrien	  ret_val = lookahead;
484590075Sobrien	  lookahead = (TREE_CODE (ret_val) == BLOCK
484690075Sobrien		       ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
484750397Sobrien	}
484850397Sobrien      while (lookahead != NULL && lookahead != ret_val);
4849169689Skan
4850169689Skan      /* The block's abstract origin chain may not be the *ultimate* origin of
4851169689Skan	 the block. It could lead to a DECL that has an abstract origin set.
4852169689Skan	 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4853169689Skan	 will give us if it has one).  Note that DECL's abstract origins are
4854169689Skan	 supposed to be the most distant ancestor (or so decl_ultimate_origin
4855169689Skan	 claims), so we don't need to loop following the DECL origins.  */
4856169689Skan      if (DECL_P (ret_val))
4857169689Skan	return DECL_ORIGIN (ret_val);
485850397Sobrien
485950397Sobrien      return ret_val;
486050397Sobrien    }
486150397Sobrien}
486250397Sobrien
486350397Sobrien/* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
486450397Sobrien   of a virtual function may refer to a base class, so we check the 'this'
486550397Sobrien   parameter.  */
486650397Sobrien
486750397Sobrienstatic tree
4868132718Skandecl_class_context (tree decl)
486950397Sobrien{
487050397Sobrien  tree context = NULL_TREE;
487150397Sobrien
487250397Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
487350397Sobrien    context = DECL_CONTEXT (decl);
487450397Sobrien  else
487550397Sobrien    context = TYPE_MAIN_VARIANT
487650397Sobrien      (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
487750397Sobrien
487890075Sobrien  if (context && !TYPE_P (context))
487950397Sobrien    context = NULL_TREE;
488050397Sobrien
488150397Sobrien  return context;
488250397Sobrien}
488350397Sobrien
4884169689Skan/* Add an attribute/value pair to a DIE.  */
488550397Sobrien
488650397Sobrienstatic inline void
4887132718Skanadd_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
488850397Sobrien{
4889169689Skan  /* Maybe this should be an assert?  */
4890169689Skan  if (die == NULL)
4891169689Skan    return;
4892169689Skan
4893169689Skan  if (die->die_attr == NULL)
4894169689Skan    die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4895169689Skan  VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
489650397Sobrien}
489750397Sobrien
4898132718Skanstatic inline enum dw_val_class
4899132718SkanAT_class (dw_attr_ref a)
490090075Sobrien{
490190075Sobrien  return a->dw_attr_val.val_class;
490290075Sobrien}
490390075Sobrien
490450397Sobrien/* Add a flag value attribute to a DIE.  */
490550397Sobrien
490650397Sobrienstatic inline void
4907132718Skanadd_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
490850397Sobrien{
4909169689Skan  dw_attr_node attr;
491050397Sobrien
4911169689Skan  attr.dw_attr = attr_kind;
4912169689Skan  attr.dw_attr_val.val_class = dw_val_class_flag;
4913169689Skan  attr.dw_attr_val.v.val_flag = flag;
4914169689Skan  add_dwarf_attr (die, &attr);
491550397Sobrien}
491650397Sobrien
491790075Sobrienstatic inline unsigned
4918132718SkanAT_flag (dw_attr_ref a)
491990075Sobrien{
4920169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_flag);
4921169689Skan  return a->dw_attr_val.v.val_flag;
492290075Sobrien}
492390075Sobrien
492450397Sobrien/* Add a signed integer attribute value to a DIE.  */
492550397Sobrien
492650397Sobrienstatic inline void
4927132718Skanadd_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
492850397Sobrien{
4929169689Skan  dw_attr_node attr;
493050397Sobrien
4931169689Skan  attr.dw_attr = attr_kind;
4932169689Skan  attr.dw_attr_val.val_class = dw_val_class_const;
4933169689Skan  attr.dw_attr_val.v.val_int = int_val;
4934169689Skan  add_dwarf_attr (die, &attr);
493550397Sobrien}
493650397Sobrien
4937132718Skanstatic inline HOST_WIDE_INT
4938132718SkanAT_int (dw_attr_ref a)
493990075Sobrien{
4940169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_const);
4941169689Skan  return a->dw_attr_val.v.val_int;
494290075Sobrien}
494390075Sobrien
494450397Sobrien/* Add an unsigned integer attribute value to a DIE.  */
494550397Sobrien
494650397Sobrienstatic inline void
4947132718Skanadd_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4948132718Skan		 unsigned HOST_WIDE_INT unsigned_val)
494950397Sobrien{
4950169689Skan  dw_attr_node attr;
495150397Sobrien
4952169689Skan  attr.dw_attr = attr_kind;
4953169689Skan  attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4954169689Skan  attr.dw_attr_val.v.val_unsigned = unsigned_val;
4955169689Skan  add_dwarf_attr (die, &attr);
495650397Sobrien}
495750397Sobrien
4958132718Skanstatic inline unsigned HOST_WIDE_INT
4959132718SkanAT_unsigned (dw_attr_ref a)
496090075Sobrien{
4961169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4962169689Skan  return a->dw_attr_val.v.val_unsigned;
496390075Sobrien}
496490075Sobrien
496550397Sobrien/* Add an unsigned double integer attribute value to a DIE.  */
496650397Sobrien
496750397Sobrienstatic inline void
4968132718Skanadd_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4969132718Skan		  long unsigned int val_hi, long unsigned int val_low)
497050397Sobrien{
4971169689Skan  dw_attr_node attr;
497250397Sobrien
4973169689Skan  attr.dw_attr = attr_kind;
4974169689Skan  attr.dw_attr_val.val_class = dw_val_class_long_long;
4975169689Skan  attr.dw_attr_val.v.val_long_long.hi = val_hi;
4976169689Skan  attr.dw_attr_val.v.val_long_long.low = val_low;
4977169689Skan  add_dwarf_attr (die, &attr);
497850397Sobrien}
497950397Sobrien
498050397Sobrien/* Add a floating point attribute value to a DIE and return it.  */
498150397Sobrien
498250397Sobrienstatic inline void
4983132718Skanadd_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4984132718Skan	    unsigned int length, unsigned int elt_size, unsigned char *array)
498550397Sobrien{
4986169689Skan  dw_attr_node attr;
498750397Sobrien
4988169689Skan  attr.dw_attr = attr_kind;
4989169689Skan  attr.dw_attr_val.val_class = dw_val_class_vec;
4990169689Skan  attr.dw_attr_val.v.val_vec.length = length;
4991169689Skan  attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4992169689Skan  attr.dw_attr_val.v.val_vec.array = array;
4993169689Skan  add_dwarf_attr (die, &attr);
499450397Sobrien}
499550397Sobrien
4996132718Skan/* Hash and equality functions for debug_str_hash.  */
4997132718Skan
4998132718Skanstatic hashval_t
4999132718Skandebug_str_do_hash (const void *x)
5000132718Skan{
5001132718Skan  return htab_hash_string (((const struct indirect_string_node *)x)->str);
5002132718Skan}
5003132718Skan
5004132718Skanstatic int
5005132718Skandebug_str_eq (const void *x1, const void *x2)
5006132718Skan{
5007132718Skan  return strcmp ((((const struct indirect_string_node *)x1)->str),
5008132718Skan		 (const char *)x2) == 0;
5009132718Skan}
5010132718Skan
501150397Sobrien/* Add a string attribute value to a DIE.  */
501250397Sobrien
501350397Sobrienstatic inline void
5014132718Skanadd_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
501550397Sobrien{
5016169689Skan  dw_attr_node attr;
501790075Sobrien  struct indirect_string_node *node;
5018132718Skan  void **slot;
5019117395Skan
502090075Sobrien  if (! debug_str_hash)
5021132718Skan    debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5022132718Skan				      debug_str_eq, NULL);
502350397Sobrien
5024132718Skan  slot = htab_find_slot_with_hash (debug_str_hash, str,
5025132718Skan				   htab_hash_string (str), INSERT);
5026132718Skan  if (*slot == NULL)
5027132718Skan    *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
5028132718Skan  node = (struct indirect_string_node *) *slot;
5029132718Skan  node->str = ggc_strdup (str);
503090075Sobrien  node->refcount++;
503190075Sobrien
5032169689Skan  attr.dw_attr = attr_kind;
5033169689Skan  attr.dw_attr_val.val_class = dw_val_class_str;
5034169689Skan  attr.dw_attr_val.v.val_str = node;
5035169689Skan  add_dwarf_attr (die, &attr);
503650397Sobrien}
503750397Sobrien
503890075Sobrienstatic inline const char *
5039132718SkanAT_string (dw_attr_ref a)
504090075Sobrien{
5041169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_str);
5042169689Skan  return a->dw_attr_val.v.val_str->str;
504390075Sobrien}
504490075Sobrien
504590075Sobrien/* Find out whether a string should be output inline in DIE
504690075Sobrien   or out-of-line in .debug_str section.  */
504790075Sobrien
504890075Sobrienstatic int
5049132718SkanAT_string_form (dw_attr_ref a)
505090075Sobrien{
5051169689Skan  struct indirect_string_node *node;
5052169689Skan  unsigned int len;
5053169689Skan  char label[32];
505490075Sobrien
5055169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_str);
505690075Sobrien
5057169689Skan  node = a->dw_attr_val.v.val_str;
5058169689Skan  if (node->form)
5059169689Skan    return node->form;
506090075Sobrien
5061169689Skan  len = strlen (node->str) + 1;
506290075Sobrien
5063169689Skan  /* If the string is shorter or equal to the size of the reference, it is
5064169689Skan     always better to put it inline.  */
5065169689Skan  if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5066169689Skan    return node->form = DW_FORM_string;
506790075Sobrien
5068169689Skan  /* If we cannot expect the linker to merge strings in .debug_str
5069169689Skan     section, only put it into .debug_str if it is worth even in this
5070169689Skan     single module.  */
5071169689Skan  if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5072169689Skan      && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5073169689Skan    return node->form = DW_FORM_string;
507490075Sobrien
5075169689Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5076169689Skan  ++dw2_string_counter;
5077169689Skan  node->label = xstrdup (label);
507890075Sobrien
5079169689Skan  return node->form = DW_FORM_strp;
508090075Sobrien}
508190075Sobrien
508250397Sobrien/* Add a DIE reference attribute value to a DIE.  */
508350397Sobrien
508450397Sobrienstatic inline void
5085132718Skanadd_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
508650397Sobrien{
5087169689Skan  dw_attr_node attr;
508850397Sobrien
5089169689Skan  attr.dw_attr = attr_kind;
5090169689Skan  attr.dw_attr_val.val_class = dw_val_class_die_ref;
5091169689Skan  attr.dw_attr_val.v.val_die_ref.die = targ_die;
5092169689Skan  attr.dw_attr_val.v.val_die_ref.external = 0;
5093169689Skan  add_dwarf_attr (die, &attr);
509450397Sobrien}
509550397Sobrien
5096132718Skan/* Add an AT_specification attribute to a DIE, and also make the back
5097132718Skan   pointer from the specification to the definition.  */
5098132718Skan
5099132718Skanstatic inline void
5100132718Skanadd_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5101132718Skan{
5102132718Skan  add_AT_die_ref (die, DW_AT_specification, targ_die);
5103169689Skan  gcc_assert (!targ_die->die_definition);
5104132718Skan  targ_die->die_definition = die;
5105132718Skan}
5106132718Skan
510790075Sobrienstatic inline dw_die_ref
5108132718SkanAT_ref (dw_attr_ref a)
510990075Sobrien{
5110169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5111169689Skan  return a->dw_attr_val.v.val_die_ref.die;
511290075Sobrien}
511390075Sobrien
511490075Sobrienstatic inline int
5115132718SkanAT_ref_external (dw_attr_ref a)
511690075Sobrien{
511790075Sobrien  if (a && AT_class (a) == dw_val_class_die_ref)
511890075Sobrien    return a->dw_attr_val.v.val_die_ref.external;
511990075Sobrien
512090075Sobrien  return 0;
512190075Sobrien}
512290075Sobrien
512390075Sobrienstatic inline void
5124132718Skanset_AT_ref_external (dw_attr_ref a, int i)
512590075Sobrien{
5126169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5127169689Skan  a->dw_attr_val.v.val_die_ref.external = i;
512890075Sobrien}
512990075Sobrien
513050397Sobrien/* Add an FDE reference attribute value to a DIE.  */
513150397Sobrien
513250397Sobrienstatic inline void
5133132718Skanadd_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
513450397Sobrien{
5135169689Skan  dw_attr_node attr;
513650397Sobrien
5137169689Skan  attr.dw_attr = attr_kind;
5138169689Skan  attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5139169689Skan  attr.dw_attr_val.v.val_fde_index = targ_fde;
5140169689Skan  add_dwarf_attr (die, &attr);
514150397Sobrien}
514250397Sobrien
514350397Sobrien/* Add a location description attribute value to a DIE.  */
514450397Sobrien
514550397Sobrienstatic inline void
5146132718Skanadd_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
514750397Sobrien{
5148169689Skan  dw_attr_node attr;
514950397Sobrien
5150169689Skan  attr.dw_attr = attr_kind;
5151169689Skan  attr.dw_attr_val.val_class = dw_val_class_loc;
5152169689Skan  attr.dw_attr_val.v.val_loc = loc;
5153169689Skan  add_dwarf_attr (die, &attr);
515450397Sobrien}
515550397Sobrien
515690075Sobrienstatic inline dw_loc_descr_ref
5157132718SkanAT_loc (dw_attr_ref a)
515890075Sobrien{
5159169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_loc);
5160169689Skan  return a->dw_attr_val.v.val_loc;
516190075Sobrien}
516290075Sobrien
516390075Sobrienstatic inline void
5164132718Skanadd_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
516590075Sobrien{
5166169689Skan  dw_attr_node attr;
516790075Sobrien
5168169689Skan  attr.dw_attr = attr_kind;
5169169689Skan  attr.dw_attr_val.val_class = dw_val_class_loc_list;
5170169689Skan  attr.dw_attr_val.v.val_loc_list = loc_list;
5171169689Skan  add_dwarf_attr (die, &attr);
5172169689Skan  have_location_lists = true;
517390075Sobrien}
517490075Sobrien
517590075Sobrienstatic inline dw_loc_list_ref
5176132718SkanAT_loc_list (dw_attr_ref a)
517790075Sobrien{
5178169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5179169689Skan  return a->dw_attr_val.v.val_loc_list;
518090075Sobrien}
518190075Sobrien
518250397Sobrien/* Add an address constant attribute value to a DIE.  */
518350397Sobrien
518450397Sobrienstatic inline void
5185132718Skanadd_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
518650397Sobrien{
5187169689Skan  dw_attr_node attr;
518850397Sobrien
5189169689Skan  attr.dw_attr = attr_kind;
5190169689Skan  attr.dw_attr_val.val_class = dw_val_class_addr;
5191169689Skan  attr.dw_attr_val.v.val_addr = addr;
5192169689Skan  add_dwarf_attr (die, &attr);
519350397Sobrien}
519450397Sobrien
5195169689Skan/* Get the RTX from to an address DIE attribute.  */
5196169689Skan
519790075Sobrienstatic inline rtx
5198132718SkanAT_addr (dw_attr_ref a)
519990075Sobrien{
5200169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_addr);
5201169689Skan  return a->dw_attr_val.v.val_addr;
5202169689Skan}
520390075Sobrien
5204169689Skan/* Add a file attribute value to a DIE.  */
5205169689Skan
5206169689Skanstatic inline void
5207169689Skanadd_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5208169689Skan	     struct dwarf_file_data *fd)
5209169689Skan{
5210169689Skan  dw_attr_node attr;
5211169689Skan
5212169689Skan  attr.dw_attr = attr_kind;
5213169689Skan  attr.dw_attr_val.val_class = dw_val_class_file;
5214169689Skan  attr.dw_attr_val.v.val_file = fd;
5215169689Skan  add_dwarf_attr (die, &attr);
521690075Sobrien}
521790075Sobrien
5218169689Skan/* Get the dwarf_file_data from a file DIE attribute.  */
5219169689Skan
5220169689Skanstatic inline struct dwarf_file_data *
5221169689SkanAT_file (dw_attr_ref a)
5222169689Skan{
5223169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_file);
5224169689Skan  return a->dw_attr_val.v.val_file;
5225169689Skan}
5226169689Skan
522750397Sobrien/* Add a label identifier attribute value to a DIE.  */
522850397Sobrien
522950397Sobrienstatic inline void
5230132718Skanadd_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
523150397Sobrien{
5232169689Skan  dw_attr_node attr;
523350397Sobrien
5234169689Skan  attr.dw_attr = attr_kind;
5235169689Skan  attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5236169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5237169689Skan  add_dwarf_attr (die, &attr);
523850397Sobrien}
523950397Sobrien
5240169689Skan/* Add a section offset attribute value to a DIE, an offset into the
5241169689Skan   debug_line section.  */
524250397Sobrien
524350397Sobrienstatic inline void
5244169689Skanadd_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5245169689Skan		const char *label)
524650397Sobrien{
5247169689Skan  dw_attr_node attr;
524850397Sobrien
5249169689Skan  attr.dw_attr = attr_kind;
5250169689Skan  attr.dw_attr_val.val_class = dw_val_class_lineptr;
5251169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5252169689Skan  add_dwarf_attr (die, &attr);
525350397Sobrien}
525450397Sobrien
5255169689Skan/* Add a section offset attribute value to a DIE, an offset into the
5256169689Skan   debug_macinfo section.  */
5257169689Skan
5258169689Skanstatic inline void
5259169689Skanadd_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5260169689Skan	       const char *label)
5261169689Skan{
5262169689Skan  dw_attr_node attr;
5263169689Skan
5264169689Skan  attr.dw_attr = attr_kind;
5265169689Skan  attr.dw_attr_val.val_class = dw_val_class_macptr;
5266169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5267169689Skan  add_dwarf_attr (die, &attr);
5268169689Skan}
5269169689Skan
527090075Sobrien/* Add an offset attribute value to a DIE.  */
527150397Sobrien
527290075Sobrienstatic inline void
5273132718Skanadd_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5274132718Skan	       unsigned HOST_WIDE_INT offset)
527550397Sobrien{
5276169689Skan  dw_attr_node attr;
527750397Sobrien
5278169689Skan  attr.dw_attr = attr_kind;
5279169689Skan  attr.dw_attr_val.val_class = dw_val_class_offset;
5280169689Skan  attr.dw_attr_val.v.val_offset = offset;
5281169689Skan  add_dwarf_attr (die, &attr);
528290075Sobrien}
528350397Sobrien
528490075Sobrien/* Add an range_list attribute value to a DIE.  */
528590075Sobrien
528690075Sobrienstatic void
5287132718Skanadd_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5288132718Skan		   long unsigned int offset)
528990075Sobrien{
5290169689Skan  dw_attr_node attr;
529190075Sobrien
5292169689Skan  attr.dw_attr = attr_kind;
5293169689Skan  attr.dw_attr_val.val_class = dw_val_class_range_list;
5294169689Skan  attr.dw_attr_val.v.val_offset = offset;
5295169689Skan  add_dwarf_attr (die, &attr);
529650397Sobrien}
529750397Sobrien
529890075Sobrienstatic inline const char *
5299132718SkanAT_lbl (dw_attr_ref a)
530090075Sobrien{
5301169689Skan  gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5302169689Skan		    || AT_class (a) == dw_val_class_lineptr
5303169689Skan		    || AT_class (a) == dw_val_class_macptr));
5304169689Skan  return a->dw_attr_val.v.val_lbl_id;
530590075Sobrien}
530690075Sobrien
530750397Sobrien/* Get the attribute of type attr_kind.  */
530850397Sobrien
5309132718Skanstatic dw_attr_ref
5310132718Skanget_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
531150397Sobrien{
531290075Sobrien  dw_attr_ref a;
5313169689Skan  unsigned ix;
531490075Sobrien  dw_die_ref spec = NULL;
531590075Sobrien
5316169689Skan  if (! die)
5317169689Skan    return NULL;
531850397Sobrien
5319169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5320169689Skan    if (a->dw_attr == attr_kind)
5321169689Skan      return a;
5322169689Skan    else if (a->dw_attr == DW_AT_specification
5323169689Skan	     || a->dw_attr == DW_AT_abstract_origin)
5324169689Skan      spec = AT_ref (a);
5325169689Skan
5326169689Skan  if (spec)
5327169689Skan    return get_AT (spec, attr_kind);
532850397Sobrien
532950397Sobrien  return NULL;
533050397Sobrien}
533150397Sobrien
533290075Sobrien/* Return the "low pc" attribute value, typically associated with a subprogram
533390075Sobrien   DIE.  Return null if the "low pc" attribute is either not present, or if it
533490075Sobrien   cannot be represented as an assembler label identifier.  */
533550397Sobrien
533690075Sobrienstatic inline const char *
5337132718Skanget_AT_low_pc (dw_die_ref die)
533850397Sobrien{
533990075Sobrien  dw_attr_ref a = get_AT (die, DW_AT_low_pc);
534050397Sobrien
534190075Sobrien  return a ? AT_lbl (a) : NULL;
534250397Sobrien}
534350397Sobrien
534490075Sobrien/* Return the "high pc" attribute value, typically associated with a subprogram
534590075Sobrien   DIE.  Return null if the "high pc" attribute is either not present, or if it
534690075Sobrien   cannot be represented as an assembler label identifier.  */
534750397Sobrien
534890075Sobrienstatic inline const char *
5349132718Skanget_AT_hi_pc (dw_die_ref die)
535050397Sobrien{
535190075Sobrien  dw_attr_ref a = get_AT (die, DW_AT_high_pc);
535250397Sobrien
535390075Sobrien  return a ? AT_lbl (a) : NULL;
535450397Sobrien}
535550397Sobrien
535650397Sobrien/* Return the value of the string attribute designated by ATTR_KIND, or
535750397Sobrien   NULL if it is not present.  */
535850397Sobrien
535990075Sobrienstatic inline const char *
5360132718Skanget_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
536150397Sobrien{
536290075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
536350397Sobrien
536490075Sobrien  return a ? AT_string (a) : NULL;
536550397Sobrien}
536650397Sobrien
536750397Sobrien/* Return the value of the flag attribute designated by ATTR_KIND, or -1
536850397Sobrien   if it is not present.  */
536950397Sobrien
537050397Sobrienstatic inline int
5371132718Skanget_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
537250397Sobrien{
537390075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
537450397Sobrien
537590075Sobrien  return a ? AT_flag (a) : 0;
537650397Sobrien}
537750397Sobrien
537850397Sobrien/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
537950397Sobrien   if it is not present.  */
538050397Sobrien
538150397Sobrienstatic inline unsigned
5382132718Skanget_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
538350397Sobrien{
538490075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
538550397Sobrien
538690075Sobrien  return a ? AT_unsigned (a) : 0;
538790075Sobrien}
538850397Sobrien
538990075Sobrienstatic inline dw_die_ref
5390132718Skanget_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
539190075Sobrien{
539290075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
539390075Sobrien
539490075Sobrien  return a ? AT_ref (a) : NULL;
539550397Sobrien}
539650397Sobrien
5397169689Skanstatic inline struct dwarf_file_data *
5398169689Skanget_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5399169689Skan{
5400169689Skan  dw_attr_ref a = get_AT (die, attr_kind);
5401169689Skan
5402169689Skan  return a ? AT_file (a) : NULL;
5403169689Skan}
5404169689Skan
5405132718Skan/* Return TRUE if the language is C or C++.  */
5406132718Skan
5407132718Skanstatic inline bool
5408132718Skanis_c_family (void)
540950397Sobrien{
5410132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
541150397Sobrien
5412169689Skan  return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5413169689Skan	  || lang == DW_LANG_C99
5414169689Skan	  || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
541590075Sobrien}
541650397Sobrien
5417132718Skan/* Return TRUE if the language is C++.  */
5418132718Skan
5419132718Skanstatic inline bool
5420132718Skanis_cxx (void)
542190075Sobrien{
5422169689Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5423169689Skan
5424169689Skan  return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5425117395Skan}
542690075Sobrien
5427132718Skan/* Return TRUE if the language is Fortran.  */
5428132718Skan
5429132718Skanstatic inline bool
5430132718Skanis_fortran (void)
543150397Sobrien{
5432132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
543350397Sobrien
5434169689Skan  return (lang == DW_LANG_Fortran77
5435169689Skan	  || lang == DW_LANG_Fortran90
5436169689Skan	  || lang == DW_LANG_Fortran95);
543790075Sobrien}
543850397Sobrien
5439132718Skan/* Return TRUE if the language is Java.  */
5440132718Skan
5441132718Skanstatic inline bool
5442132718Skanis_java (void)
544390075Sobrien{
5444132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
544590075Sobrien
5446132718Skan  return lang == DW_LANG_Java;
544790075Sobrien}
544890075Sobrien
5449132718Skan/* Return TRUE if the language is Ada.  */
545090075Sobrien
5451132718Skanstatic inline bool
5452132718Skanis_ada (void)
545390075Sobrien{
5454132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
545590075Sobrien
5456132718Skan  return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5457132718Skan}
545890075Sobrien
545950397Sobrien/* Remove the specified attribute if present.  */
546050397Sobrien
546190075Sobrienstatic void
5462132718Skanremove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
546350397Sobrien{
5464169689Skan  dw_attr_ref a;
5465169689Skan  unsigned ix;
546650397Sobrien
5467169689Skan  if (! die)
5468169689Skan    return;
546950397Sobrien
5470169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5471169689Skan    if (a->dw_attr == attr_kind)
5472169689Skan      {
5473169689Skan	if (AT_class (a) == dw_val_class_str)
5474169689Skan	  if (a->dw_attr_val.v.val_str->refcount)
5475169689Skan	    a->dw_attr_val.v.val_str->refcount--;
5476169689Skan
5477169689Skan	/* VEC_ordered_remove should help reduce the number of abbrevs
5478169689Skan	   that are needed.  */
5479169689Skan	VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5480169689Skan	return;
5481169689Skan      }
548250397Sobrien}
548350397Sobrien
5484169689Skan/* Remove CHILD from its parent.  PREV must have the property that
5485169689Skan   PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5486146895Skan
5487146895Skanstatic void
5488169689Skanremove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5489146895Skan{
5490169689Skan  gcc_assert (child->die_parent == prev->die_parent);
5491169689Skan  gcc_assert (prev->die_sib == child);
5492169689Skan  if (prev == child)
5493146895Skan    {
5494169689Skan      gcc_assert (child->die_parent->die_child == child);
5495169689Skan      prev = NULL;
5496146895Skan    }
5497169689Skan  else
5498169689Skan    prev->die_sib = child->die_sib;
5499169689Skan  if (child->die_parent->die_child == child)
5500169689Skan    child->die_parent->die_child = prev;
5501146895Skan}
5502146895Skan
5503169689Skan/* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5504169689Skan   matches TAG.  */
550590075Sobrien
5506169689Skanstatic void
5507169689Skanremove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
550890075Sobrien{
5509169689Skan  dw_die_ref c;
5510169689Skan
5511169689Skan  c = die->die_child;
5512169689Skan  if (c) do {
5513169689Skan    dw_die_ref prev = c;
5514169689Skan    c = c->die_sib;
5515169689Skan    while (c->die_tag == tag)
5516169689Skan      {
5517169689Skan	remove_child_with_prev (c, prev);
5518169689Skan	/* Might have removed every child.  */
5519169689Skan	if (c == c->die_sib)
5520169689Skan	  return;
5521169689Skan	c = c->die_sib;
5522169689Skan      }
5523169689Skan  } while (c != die->die_child);
552490075Sobrien}
552590075Sobrien
5526169689Skan/* Add a CHILD_DIE as the last child of DIE.  */
552750397Sobrien
552890075Sobrienstatic void
5529169689Skanadd_child_die (dw_die_ref die, dw_die_ref child_die)
553050397Sobrien{
5531169689Skan  /* FIXME this should probably be an assert.  */
5532169689Skan  if (! die || ! child_die)
5533169689Skan    return;
5534169689Skan  gcc_assert (die != child_die);
553550397Sobrien
5536169689Skan  child_die->die_parent = die;
5537169689Skan  if (die->die_child)
553850397Sobrien    {
5539169689Skan      child_die->die_sib = die->die_child->die_sib;
5540169689Skan      die->die_child->die_sib = child_die;
554150397Sobrien    }
5542169689Skan  else
5543169689Skan    child_die->die_sib = child_die;
5544169689Skan  die->die_child = child_die;
554550397Sobrien}
554650397Sobrien
554790075Sobrien/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5548169689Skan   is the specification, to the end of PARENT's list of children.
5549169689Skan   This is done by removing and re-adding it.  */
555090075Sobrien
555190075Sobrienstatic void
5552132718Skansplice_child_die (dw_die_ref parent, dw_die_ref child)
555390075Sobrien{
5554169689Skan  dw_die_ref p;
555590075Sobrien
555690075Sobrien  /* We want the declaration DIE from inside the class, not the
555790075Sobrien     specification DIE at toplevel.  */
555890075Sobrien  if (child->die_parent != parent)
555990075Sobrien    {
556090075Sobrien      dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
556190075Sobrien
556290075Sobrien      if (tmp)
556390075Sobrien	child = tmp;
556450397Sobrien    }
556590075Sobrien
5566169689Skan  gcc_assert (child->die_parent == parent
5567169689Skan	      || (child->die_parent
5568169689Skan		  == get_AT_ref (parent, DW_AT_specification)));
5569169689Skan
5570169689Skan  for (p = child->die_parent->die_child; ; p = p->die_sib)
5571169689Skan    if (p->die_sib == child)
557290075Sobrien      {
5573169689Skan	remove_child_with_prev (child, p);
557490075Sobrien	break;
557590075Sobrien      }
557690075Sobrien
5577169689Skan  add_child_die (parent, child);
557850397Sobrien}
557950397Sobrien
558050397Sobrien/* Return a pointer to a newly created DIE node.  */
558150397Sobrien
558250397Sobrienstatic inline dw_die_ref
5583132718Skannew_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
558450397Sobrien{
5585132718Skan  dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
558650397Sobrien
558750397Sobrien  die->die_tag = tag_value;
558850397Sobrien
558950397Sobrien  if (parent_die != NULL)
559050397Sobrien    add_child_die (parent_die, die);
559150397Sobrien  else
559250397Sobrien    {
559350397Sobrien      limbo_die_node *limbo_node;
559450397Sobrien
5595132718Skan      limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
559650397Sobrien      limbo_node->die = die;
559790075Sobrien      limbo_node->created_for = t;
559850397Sobrien      limbo_node->next = limbo_die_list;
559950397Sobrien      limbo_die_list = limbo_node;
560050397Sobrien    }
560150397Sobrien
560250397Sobrien  return die;
560350397Sobrien}
560450397Sobrien
560550397Sobrien/* Return the DIE associated with the given type specifier.  */
560650397Sobrien
560750397Sobrienstatic inline dw_die_ref
5608132718Skanlookup_type_die (tree type)
560950397Sobrien{
5610117395Skan  return TYPE_SYMTAB_DIE (type);
561150397Sobrien}
561250397Sobrien
561350397Sobrien/* Equate a DIE to a given type specifier.  */
561450397Sobrien
561590075Sobrienstatic inline void
5616132718Skanequate_type_number_to_die (tree type, dw_die_ref type_die)
561750397Sobrien{
5618117395Skan  TYPE_SYMTAB_DIE (type) = type_die;
561950397Sobrien}
562050397Sobrien
5621169689Skan/* Returns a hash value for X (which really is a die_struct).  */
5622169689Skan
5623169689Skanstatic hashval_t
5624169689Skandecl_die_table_hash (const void *x)
5625169689Skan{
5626169689Skan  return (hashval_t) ((const dw_die_ref) x)->decl_id;
5627169689Skan}
5628169689Skan
5629169689Skan/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5630169689Skan
5631169689Skanstatic int
5632169689Skandecl_die_table_eq (const void *x, const void *y)
5633169689Skan{
5634169689Skan  return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5635169689Skan}
5636169689Skan
563750397Sobrien/* Return the DIE associated with a given declaration.  */
563850397Sobrien
563950397Sobrienstatic inline dw_die_ref
5640132718Skanlookup_decl_die (tree decl)
564150397Sobrien{
5642169689Skan  return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5643169689Skan}
564450397Sobrien
5645169689Skan/* Returns a hash value for X (which really is a var_loc_list).  */
5646169689Skan
5647169689Skanstatic hashval_t
5648169689Skandecl_loc_table_hash (const void *x)
5649169689Skan{
5650169689Skan  return (hashval_t) ((const var_loc_list *) x)->decl_id;
565150397Sobrien}
565250397Sobrien
5653169689Skan/* Return nonzero if decl_id of var_loc_list X is the same as
5654169689Skan   UID of decl *Y.  */
5655169689Skan
5656169689Skanstatic int
5657169689Skandecl_loc_table_eq (const void *x, const void *y)
5658169689Skan{
5659169689Skan  return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5660169689Skan}
5661169689Skan
5662169689Skan/* Return the var_loc list associated with a given declaration.  */
5663169689Skan
5664169689Skanstatic inline var_loc_list *
5665169689Skanlookup_decl_loc (tree decl)
5666169689Skan{
5667169689Skan  return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5668169689Skan}
5669169689Skan
567050397Sobrien/* Equate a DIE to a particular declaration.  */
567150397Sobrien
567250397Sobrienstatic void
5673132718Skanequate_decl_number_to_die (tree decl, dw_die_ref decl_die)
567450397Sobrien{
567590075Sobrien  unsigned int decl_id = DECL_UID (decl);
5676169689Skan  void **slot;
567750397Sobrien
5678169689Skan  slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5679169689Skan  *slot = decl_die;
5680169689Skan  decl_die->decl_id = decl_id;
5681169689Skan}
568250397Sobrien
5683169689Skan/* Add a variable location node to the linked list for DECL.  */
568450397Sobrien
5685169689Skanstatic void
5686169689Skanadd_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5687169689Skan{
5688169689Skan  unsigned int decl_id = DECL_UID (decl);
5689169689Skan  var_loc_list *temp;
5690169689Skan  void **slot;
5691169689Skan
5692169689Skan  slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5693169689Skan  if (*slot == NULL)
5694169689Skan    {
5695169689Skan      temp = ggc_alloc_cleared (sizeof (var_loc_list));
5696169689Skan      temp->decl_id = decl_id;
5697169689Skan      *slot = temp;
569850397Sobrien    }
5699169689Skan  else
5700169689Skan    temp = *slot;
570150397Sobrien
5702169689Skan  if (temp->last)
5703169689Skan    {
5704169689Skan      /* If the current location is the same as the end of the list,
5705169689Skan	 we have nothing to do.  */
5706169689Skan      if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5707169689Skan			NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5708169689Skan	{
5709169689Skan	  /* Add LOC to the end of list and update LAST.  */
5710169689Skan	  temp->last->next = loc;
5711169689Skan	  temp->last = loc;
5712169689Skan	}
5713169689Skan    }
5714169689Skan  /* Do not add empty location to the beginning of the list.  */
5715169689Skan  else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5716169689Skan    {
5717169689Skan      temp->first = loc;
5718169689Skan      temp->last = loc;
5719169689Skan    }
572050397Sobrien}
572150397Sobrien
572250397Sobrien/* Keep track of the number of spaces used to indent the
572350397Sobrien   output of the debugging routines that print the structure of
572450397Sobrien   the DIE internal representation.  */
572550397Sobrienstatic int print_indent;
572650397Sobrien
572750397Sobrien/* Indent the line the number of spaces given by print_indent.  */
572850397Sobrien
572950397Sobrienstatic inline void
5730132718Skanprint_spaces (FILE *outfile)
573150397Sobrien{
573250397Sobrien  fprintf (outfile, "%*s", print_indent, "");
573350397Sobrien}
573450397Sobrien
573550397Sobrien/* Print the information associated with a given DIE, and its children.
573650397Sobrien   This routine is a debugging aid only.  */
573750397Sobrien
573850397Sobrienstatic void
5739132718Skanprint_die (dw_die_ref die, FILE *outfile)
574050397Sobrien{
574190075Sobrien  dw_attr_ref a;
574290075Sobrien  dw_die_ref c;
5743169689Skan  unsigned ix;
574450397Sobrien
574550397Sobrien  print_spaces (outfile);
5746259948Spfg  fprintf (outfile, "DIE %4ld: %s\n",
574750397Sobrien	   die->die_offset, dwarf_tag_name (die->die_tag));
574850397Sobrien  print_spaces (outfile);
574950397Sobrien  fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5750259948Spfg  fprintf (outfile, " offset: %ld\n", die->die_offset);
575150397Sobrien
5752169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
575350397Sobrien    {
575450397Sobrien      print_spaces (outfile);
575550397Sobrien      fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
575650397Sobrien
575790075Sobrien      switch (AT_class (a))
575850397Sobrien	{
575950397Sobrien	case dw_val_class_addr:
576050397Sobrien	  fprintf (outfile, "address");
576150397Sobrien	  break;
576290075Sobrien	case dw_val_class_offset:
576390075Sobrien	  fprintf (outfile, "offset");
576490075Sobrien	  break;
576550397Sobrien	case dw_val_class_loc:
576650397Sobrien	  fprintf (outfile, "location descriptor");
576750397Sobrien	  break;
576890075Sobrien	case dw_val_class_loc_list:
576990075Sobrien	  fprintf (outfile, "location list -> label:%s",
577090075Sobrien		   AT_loc_list (a)->ll_symbol);
577190075Sobrien	  break;
577290075Sobrien	case dw_val_class_range_list:
577390075Sobrien	  fprintf (outfile, "range list");
577490075Sobrien	  break;
577550397Sobrien	case dw_val_class_const:
5776132718Skan	  fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
577750397Sobrien	  break;
577850397Sobrien	case dw_val_class_unsigned_const:
5779132718Skan	  fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
578050397Sobrien	  break;
578150397Sobrien	case dw_val_class_long_long:
578250397Sobrien	  fprintf (outfile, "constant (%lu,%lu)",
578390075Sobrien		   a->dw_attr_val.v.val_long_long.hi,
578490075Sobrien		   a->dw_attr_val.v.val_long_long.low);
578550397Sobrien	  break;
5786132718Skan	case dw_val_class_vec:
5787132718Skan	  fprintf (outfile, "floating-point or vector constant");
578850397Sobrien	  break;
578950397Sobrien	case dw_val_class_flag:
579090075Sobrien	  fprintf (outfile, "%u", AT_flag (a));
579150397Sobrien	  break;
579250397Sobrien	case dw_val_class_die_ref:
579390075Sobrien	  if (AT_ref (a) != NULL)
579490075Sobrien	    {
579590075Sobrien	      if (AT_ref (a)->die_symbol)
579690075Sobrien		fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
579790075Sobrien	      else
5798259948Spfg		fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
579990075Sobrien	    }
580050397Sobrien	  else
580150397Sobrien	    fprintf (outfile, "die -> <null>");
580250397Sobrien	  break;
580350397Sobrien	case dw_val_class_lbl_id:
5804169689Skan	case dw_val_class_lineptr:
5805169689Skan	case dw_val_class_macptr:
580690075Sobrien	  fprintf (outfile, "label: %s", AT_lbl (a));
580750397Sobrien	  break;
580850397Sobrien	case dw_val_class_str:
580990075Sobrien	  if (AT_string (a) != NULL)
581090075Sobrien	    fprintf (outfile, "\"%s\"", AT_string (a));
581150397Sobrien	  else
581250397Sobrien	    fprintf (outfile, "<null>");
581350397Sobrien	  break;
5814169689Skan	case dw_val_class_file:
5815169689Skan	  fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5816169689Skan		   AT_file (a)->emitted_number);
5817169689Skan	  break;
581850397Sobrien	default:
581950397Sobrien	  break;
582050397Sobrien	}
582150397Sobrien
582250397Sobrien      fprintf (outfile, "\n");
582350397Sobrien    }
582450397Sobrien
582550397Sobrien  if (die->die_child != NULL)
582650397Sobrien    {
582750397Sobrien      print_indent += 4;
5828169689Skan      FOR_EACH_CHILD (die, c, print_die (c, outfile));
582950397Sobrien      print_indent -= 4;
583050397Sobrien    }
583190075Sobrien  if (print_indent == 0)
583290075Sobrien    fprintf (outfile, "\n");
583350397Sobrien}
583450397Sobrien
583550397Sobrien/* Print the contents of the source code line number correspondence table.
583650397Sobrien   This routine is a debugging aid only.  */
583750397Sobrien
583850397Sobrienstatic void
5839132718Skanprint_dwarf_line_table (FILE *outfile)
584050397Sobrien{
584190075Sobrien  unsigned i;
584290075Sobrien  dw_line_info_ref line_info;
584350397Sobrien
584450397Sobrien  fprintf (outfile, "\n\nDWARF source line information\n");
584590075Sobrien  for (i = 1; i < line_info_table_in_use; i++)
584650397Sobrien    {
584750397Sobrien      line_info = &line_info_table[i];
5848169689Skan      fprintf (outfile, "%5d: %4ld %6ld\n", i,
5849169689Skan	       line_info->dw_file_num,
5850169689Skan	       line_info->dw_line_num);
585150397Sobrien    }
585250397Sobrien
585350397Sobrien  fprintf (outfile, "\n\n");
585450397Sobrien}
585550397Sobrien
585650397Sobrien/* Print the information collected for a given DIE.  */
585750397Sobrien
585850397Sobrienvoid
5859132718Skandebug_dwarf_die (dw_die_ref die)
586050397Sobrien{
586150397Sobrien  print_die (die, stderr);
586250397Sobrien}
586350397Sobrien
586450397Sobrien/* Print all DWARF information collected for the compilation unit.
586550397Sobrien   This routine is a debugging aid only.  */
586650397Sobrien
586750397Sobrienvoid
5868132718Skandebug_dwarf (void)
586950397Sobrien{
587050397Sobrien  print_indent = 0;
587150397Sobrien  print_die (comp_unit_die, stderr);
587290075Sobrien  if (! DWARF2_ASM_LINE_DEBUG_INFO)
587390075Sobrien    print_dwarf_line_table (stderr);
587450397Sobrien}
587550397Sobrien
587690075Sobrien/* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
587790075Sobrien   for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
587890075Sobrien   DIE that marks the start of the DIEs for this include file.  */
587990075Sobrien
588090075Sobrienstatic dw_die_ref
5881132718Skanpush_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
588290075Sobrien{
588390075Sobrien  const char *filename = get_AT_string (bincl_die, DW_AT_name);
588490075Sobrien  dw_die_ref new_unit = gen_compile_unit_die (filename);
588590075Sobrien
588690075Sobrien  new_unit->die_sib = old_unit;
588790075Sobrien  return new_unit;
588890075Sobrien}
588990075Sobrien
589090075Sobrien/* Close an include-file CU and reopen the enclosing one.  */
589190075Sobrien
589290075Sobrienstatic dw_die_ref
5893132718Skanpop_compile_unit (dw_die_ref old_unit)
589490075Sobrien{
589590075Sobrien  dw_die_ref new_unit = old_unit->die_sib;
589690075Sobrien
589790075Sobrien  old_unit->die_sib = NULL;
589890075Sobrien  return new_unit;
589990075Sobrien}
590090075Sobrien
590190075Sobrien#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
590290075Sobrien#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
590390075Sobrien
590490075Sobrien/* Calculate the checksum of a location expression.  */
590590075Sobrien
590690075Sobrienstatic inline void
5907132718Skanloc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
590890075Sobrien{
590990075Sobrien  CHECKSUM (loc->dw_loc_opc);
591090075Sobrien  CHECKSUM (loc->dw_loc_oprnd1);
591190075Sobrien  CHECKSUM (loc->dw_loc_oprnd2);
591290075Sobrien}
591390075Sobrien
591490075Sobrien/* Calculate the checksum of an attribute.  */
591590075Sobrien
591690075Sobrienstatic void
5917132718Skanattr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
591890075Sobrien{
591990075Sobrien  dw_loc_descr_ref loc;
592090075Sobrien  rtx r;
592190075Sobrien
592290075Sobrien  CHECKSUM (at->dw_attr);
592390075Sobrien
5924169689Skan  /* We don't care that this was compiled with a different compiler
5925169689Skan     snapshot; if the output is the same, that's what matters.  */
5926169689Skan  if (at->dw_attr == DW_AT_producer)
592790075Sobrien    return;
592890075Sobrien
592990075Sobrien  switch (AT_class (at))
593090075Sobrien    {
593190075Sobrien    case dw_val_class_const:
593290075Sobrien      CHECKSUM (at->dw_attr_val.v.val_int);
593390075Sobrien      break;
593490075Sobrien    case dw_val_class_unsigned_const:
593590075Sobrien      CHECKSUM (at->dw_attr_val.v.val_unsigned);
593690075Sobrien      break;
593790075Sobrien    case dw_val_class_long_long:
593890075Sobrien      CHECKSUM (at->dw_attr_val.v.val_long_long);
593990075Sobrien      break;
5940132718Skan    case dw_val_class_vec:
5941132718Skan      CHECKSUM (at->dw_attr_val.v.val_vec);
594290075Sobrien      break;
594390075Sobrien    case dw_val_class_flag:
594490075Sobrien      CHECKSUM (at->dw_attr_val.v.val_flag);
594590075Sobrien      break;
594690075Sobrien    case dw_val_class_str:
594790075Sobrien      CHECKSUM_STRING (AT_string (at));
594890075Sobrien      break;
594990075Sobrien
595090075Sobrien    case dw_val_class_addr:
595190075Sobrien      r = AT_addr (at);
5952169689Skan      gcc_assert (GET_CODE (r) == SYMBOL_REF);
5953169689Skan      CHECKSUM_STRING (XSTR (r, 0));
595490075Sobrien      break;
595590075Sobrien
595690075Sobrien    case dw_val_class_offset:
595790075Sobrien      CHECKSUM (at->dw_attr_val.v.val_offset);
595890075Sobrien      break;
595990075Sobrien
596090075Sobrien    case dw_val_class_loc:
596190075Sobrien      for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
596290075Sobrien	loc_checksum (loc, ctx);
596390075Sobrien      break;
596490075Sobrien
596590075Sobrien    case dw_val_class_die_ref:
5966117395Skan      die_checksum (AT_ref (at), ctx, mark);
5967117395Skan      break;
596890075Sobrien
596990075Sobrien    case dw_val_class_fde_ref:
597090075Sobrien    case dw_val_class_lbl_id:
5971169689Skan    case dw_val_class_lineptr:
5972169689Skan    case dw_val_class_macptr:
597390075Sobrien      break;
597490075Sobrien
5975169689Skan    case dw_val_class_file:
5976169689Skan      CHECKSUM_STRING (AT_file (at)->filename);
5977169689Skan      break;
5978169689Skan
597990075Sobrien    default:
598090075Sobrien      break;
598190075Sobrien    }
598290075Sobrien}
598390075Sobrien
598490075Sobrien/* Calculate the checksum of a DIE.  */
598590075Sobrien
598690075Sobrienstatic void
5987132718Skandie_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
598890075Sobrien{
598990075Sobrien  dw_die_ref c;
599090075Sobrien  dw_attr_ref a;
5991169689Skan  unsigned ix;
599290075Sobrien
5993117395Skan  /* To avoid infinite recursion.  */
5994117395Skan  if (die->die_mark)
5995117395Skan    {
5996117395Skan      CHECKSUM (die->die_mark);
5997117395Skan      return;
5998117395Skan    }
5999117395Skan  die->die_mark = ++(*mark);
6000117395Skan
600190075Sobrien  CHECKSUM (die->die_tag);
600290075Sobrien
6003169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6004117395Skan    attr_checksum (a, ctx, mark);
600590075Sobrien
6006169689Skan  FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
600790075Sobrien}
600890075Sobrien
600990075Sobrien#undef CHECKSUM
601090075Sobrien#undef CHECKSUM_STRING
601190075Sobrien
6012117395Skan/* Do the location expressions look same?  */
6013117395Skanstatic inline int
6014132718Skansame_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6015117395Skan{
6016117395Skan  return loc1->dw_loc_opc == loc2->dw_loc_opc
6017117395Skan	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6018117395Skan	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6019117395Skan}
6020117395Skan
6021117395Skan/* Do the values look the same?  */
6022117395Skanstatic int
6023132718Skansame_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6024117395Skan{
6025117395Skan  dw_loc_descr_ref loc1, loc2;
6026117395Skan  rtx r1, r2;
6027117395Skan
6028117395Skan  if (v1->val_class != v2->val_class)
6029117395Skan    return 0;
6030117395Skan
6031117395Skan  switch (v1->val_class)
6032117395Skan    {
6033117395Skan    case dw_val_class_const:
6034117395Skan      return v1->v.val_int == v2->v.val_int;
6035117395Skan    case dw_val_class_unsigned_const:
6036117395Skan      return v1->v.val_unsigned == v2->v.val_unsigned;
6037117395Skan    case dw_val_class_long_long:
6038117395Skan      return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6039132718Skan	     && v1->v.val_long_long.low == v2->v.val_long_long.low;
6040132718Skan    case dw_val_class_vec:
6041132718Skan      if (v1->v.val_vec.length != v2->v.val_vec.length
6042132718Skan	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6043117395Skan	return 0;
6044132718Skan      if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6045132718Skan		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
6046132718Skan	return 0;
6047117395Skan      return 1;
6048117395Skan    case dw_val_class_flag:
6049117395Skan      return v1->v.val_flag == v2->v.val_flag;
6050117395Skan    case dw_val_class_str:
6051132718Skan      return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6052117395Skan
6053117395Skan    case dw_val_class_addr:
6054117395Skan      r1 = v1->v.val_addr;
6055117395Skan      r2 = v2->v.val_addr;
6056117395Skan      if (GET_CODE (r1) != GET_CODE (r2))
6057117395Skan	return 0;
6058169689Skan      gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6059169689Skan      return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6060117395Skan
6061117395Skan    case dw_val_class_offset:
6062117395Skan      return v1->v.val_offset == v2->v.val_offset;
6063117395Skan
6064117395Skan    case dw_val_class_loc:
6065117395Skan      for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6066117395Skan	   loc1 && loc2;
6067117395Skan	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6068117395Skan	if (!same_loc_p (loc1, loc2, mark))
6069117395Skan	  return 0;
6070117395Skan      return !loc1 && !loc2;
6071117395Skan
6072117395Skan    case dw_val_class_die_ref:
6073117395Skan      return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6074117395Skan
6075117395Skan    case dw_val_class_fde_ref:
6076117395Skan    case dw_val_class_lbl_id:
6077169689Skan    case dw_val_class_lineptr:
6078169689Skan    case dw_val_class_macptr:
6079117395Skan      return 1;
6080117395Skan
6081169689Skan    case dw_val_class_file:
6082169689Skan      return v1->v.val_file == v2->v.val_file;
6083169689Skan
6084117395Skan    default:
6085117395Skan      return 1;
6086117395Skan    }
6087117395Skan}
6088117395Skan
6089117395Skan/* Do the attributes look the same?  */
6090117395Skan
6091117395Skanstatic int
6092132718Skansame_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6093117395Skan{
6094117395Skan  if (at1->dw_attr != at2->dw_attr)
6095117395Skan    return 0;
6096117395Skan
6097169689Skan  /* We don't care that this was compiled with a different compiler
6098169689Skan     snapshot; if the output is the same, that's what matters. */
6099169689Skan  if (at1->dw_attr == DW_AT_producer)
6100117395Skan    return 1;
6101117395Skan
6102117395Skan  return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6103117395Skan}
6104117395Skan
6105117395Skan/* Do the dies look the same?  */
6106117395Skan
6107117395Skanstatic int
6108132718Skansame_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6109117395Skan{
6110117395Skan  dw_die_ref c1, c2;
6111169689Skan  dw_attr_ref a1;
6112169689Skan  unsigned ix;
6113117395Skan
6114117395Skan  /* To avoid infinite recursion.  */
6115117395Skan  if (die1->die_mark)
6116117395Skan    return die1->die_mark == die2->die_mark;
6117117395Skan  die1->die_mark = die2->die_mark = ++(*mark);
6118117395Skan
6119117395Skan  if (die1->die_tag != die2->die_tag)
6120117395Skan    return 0;
6121117395Skan
6122169689Skan  if (VEC_length (dw_attr_node, die1->die_attr)
6123169689Skan      != VEC_length (dw_attr_node, die2->die_attr))
6124117395Skan    return 0;
6125169689Skan
6126169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6127169689Skan    if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6128117395Skan      return 0;
6129117395Skan
6130169689Skan  c1 = die1->die_child;
6131169689Skan  c2 = die2->die_child;
6132169689Skan  if (! c1)
6133169689Skan    {
6134169689Skan      if (c2)
6135169689Skan	return 0;
6136169689Skan    }
6137169689Skan  else
6138169689Skan    for (;;)
6139169689Skan      {
6140169689Skan	if (!same_die_p (c1, c2, mark))
6141169689Skan	  return 0;
6142169689Skan	c1 = c1->die_sib;
6143169689Skan	c2 = c2->die_sib;
6144169689Skan	if (c1 == die1->die_child)
6145169689Skan	  {
6146169689Skan	    if (c2 == die2->die_child)
6147169689Skan	      break;
6148169689Skan	    else
6149169689Skan	      return 0;
6150169689Skan	  }
6151169689Skan    }
6152169689Skan
6153117395Skan  return 1;
6154117395Skan}
6155117395Skan
6156117395Skan/* Do the dies look the same?  Wrapper around same_die_p.  */
6157117395Skan
6158117395Skanstatic int
6159132718Skansame_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6160117395Skan{
6161117395Skan  int mark = 0;
6162117395Skan  int ret = same_die_p (die1, die2, &mark);
6163117395Skan
6164117395Skan  unmark_all_dies (die1);
6165117395Skan  unmark_all_dies (die2);
6166117395Skan
6167117395Skan  return ret;
6168117395Skan}
6169117395Skan
617090075Sobrien/* The prefix to attach to symbols on DIEs in the current comdat debug
617190075Sobrien   info section.  */
617290075Sobrienstatic char *comdat_symbol_id;
617390075Sobrien
617490075Sobrien/* The index of the current symbol within the current comdat CU.  */
617590075Sobrienstatic unsigned int comdat_symbol_number;
617690075Sobrien
617790075Sobrien/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
617890075Sobrien   children, and set comdat_symbol_id accordingly.  */
617990075Sobrien
618090075Sobrienstatic void
6181132718Skancompute_section_prefix (dw_die_ref unit_die)
618290075Sobrien{
6183117395Skan  const char *die_name = get_AT_string (unit_die, DW_AT_name);
6184117395Skan  const char *base = die_name ? lbasename (die_name) : "anonymous";
6185132718Skan  char *name = alloca (strlen (base) + 64);
618690075Sobrien  char *p;
6187117395Skan  int i, mark;
618890075Sobrien  unsigned char checksum[16];
618990075Sobrien  struct md5_ctx ctx;
619090075Sobrien
619190075Sobrien  /* Compute the checksum of the DIE, then append part of it as hex digits to
619290075Sobrien     the name filename of the unit.  */
619390075Sobrien
619490075Sobrien  md5_init_ctx (&ctx);
6195117395Skan  mark = 0;
6196117395Skan  die_checksum (unit_die, &ctx, &mark);
6197117395Skan  unmark_all_dies (unit_die);
619890075Sobrien  md5_finish_ctx (&ctx, checksum);
619990075Sobrien
620090075Sobrien  sprintf (name, "%s.", base);
620190075Sobrien  clean_symbol_name (name);
620290075Sobrien
620390075Sobrien  p = name + strlen (name);
620490075Sobrien  for (i = 0; i < 4; i++)
620590075Sobrien    {
620690075Sobrien      sprintf (p, "%.2x", checksum[i]);
620790075Sobrien      p += 2;
620890075Sobrien    }
620990075Sobrien
621090075Sobrien  comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
621190075Sobrien  comdat_symbol_number = 0;
621290075Sobrien}
621390075Sobrien
621490075Sobrien/* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
621590075Sobrien
621690075Sobrienstatic int
6217132718Skanis_type_die (dw_die_ref die)
621890075Sobrien{
621990075Sobrien  switch (die->die_tag)
622090075Sobrien    {
622190075Sobrien    case DW_TAG_array_type:
622290075Sobrien    case DW_TAG_class_type:
622390075Sobrien    case DW_TAG_enumeration_type:
622490075Sobrien    case DW_TAG_pointer_type:
622590075Sobrien    case DW_TAG_reference_type:
622690075Sobrien    case DW_TAG_string_type:
622790075Sobrien    case DW_TAG_structure_type:
622890075Sobrien    case DW_TAG_subroutine_type:
622990075Sobrien    case DW_TAG_union_type:
623090075Sobrien    case DW_TAG_ptr_to_member_type:
623190075Sobrien    case DW_TAG_set_type:
623290075Sobrien    case DW_TAG_subrange_type:
623390075Sobrien    case DW_TAG_base_type:
623490075Sobrien    case DW_TAG_const_type:
623590075Sobrien    case DW_TAG_file_type:
623690075Sobrien    case DW_TAG_packed_type:
623790075Sobrien    case DW_TAG_volatile_type:
6238117395Skan    case DW_TAG_typedef:
623990075Sobrien      return 1;
624090075Sobrien    default:
624190075Sobrien      return 0;
624290075Sobrien    }
624390075Sobrien}
624490075Sobrien
624590075Sobrien/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
624690075Sobrien   Basically, we want to choose the bits that are likely to be shared between
624790075Sobrien   compilations (types) and leave out the bits that are specific to individual
624890075Sobrien   compilations (functions).  */
624990075Sobrien
625090075Sobrienstatic int
6251132718Skanis_comdat_die (dw_die_ref c)
625290075Sobrien{
625390075Sobrien  /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
625490075Sobrien     we do for stabs.  The advantage is a greater likelihood of sharing between
625590075Sobrien     objects that don't include headers in the same order (and therefore would
625690075Sobrien     put the base types in a different comdat).  jason 8/28/00 */
625790075Sobrien
625890075Sobrien  if (c->die_tag == DW_TAG_base_type)
625990075Sobrien    return 0;
626090075Sobrien
626190075Sobrien  if (c->die_tag == DW_TAG_pointer_type
626290075Sobrien      || c->die_tag == DW_TAG_reference_type
626390075Sobrien      || c->die_tag == DW_TAG_const_type
626490075Sobrien      || c->die_tag == DW_TAG_volatile_type)
626590075Sobrien    {
626690075Sobrien      dw_die_ref t = get_AT_ref (c, DW_AT_type);
626790075Sobrien
626890075Sobrien      return t ? is_comdat_die (t) : 0;
626990075Sobrien    }
627090075Sobrien
627190075Sobrien  return is_type_die (c);
627290075Sobrien}
627390075Sobrien
627490075Sobrien/* Returns 1 iff C is the sort of DIE that might be referred to from another
627590075Sobrien   compilation unit.  */
627690075Sobrien
627790075Sobrienstatic int
6278132718Skanis_symbol_die (dw_die_ref c)
627990075Sobrien{
628090075Sobrien  return (is_type_die (c)
6281117395Skan	  || (get_AT (c, DW_AT_declaration)
6282169689Skan	      && !get_AT (c, DW_AT_specification))
6283169689Skan	  || c->die_tag == DW_TAG_namespace);
628490075Sobrien}
628590075Sobrien
628690075Sobrienstatic char *
6287132718Skangen_internal_sym (const char *prefix)
628890075Sobrien{
628990075Sobrien  char buf[256];
629090075Sobrien
629190075Sobrien  ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
629290075Sobrien  return xstrdup (buf);
629390075Sobrien}
629490075Sobrien
629590075Sobrien/* Assign symbols to all worthy DIEs under DIE.  */
629690075Sobrien
629790075Sobrienstatic void
6298132718Skanassign_symbol_names (dw_die_ref die)
629990075Sobrien{
630090075Sobrien  dw_die_ref c;
630190075Sobrien
630290075Sobrien  if (is_symbol_die (die))
630390075Sobrien    {
630490075Sobrien      if (comdat_symbol_id)
630590075Sobrien	{
630690075Sobrien	  char *p = alloca (strlen (comdat_symbol_id) + 64);
630790075Sobrien
630890075Sobrien	  sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
630990075Sobrien		   comdat_symbol_id, comdat_symbol_number++);
631090075Sobrien	  die->die_symbol = xstrdup (p);
631190075Sobrien	}
631290075Sobrien      else
631390075Sobrien	die->die_symbol = gen_internal_sym ("LDIE");
631490075Sobrien    }
631590075Sobrien
6316169689Skan  FOR_EACH_CHILD (die, c, assign_symbol_names (c));
631790075Sobrien}
631890075Sobrien
6319117395Skanstruct cu_hash_table_entry
6320117395Skan{
6321117395Skan  dw_die_ref cu;
6322117395Skan  unsigned min_comdat_num, max_comdat_num;
6323117395Skan  struct cu_hash_table_entry *next;
6324117395Skan};
6325117395Skan
6326117395Skan/* Routines to manipulate hash table of CUs.  */
6327117395Skanstatic hashval_t
6328132718Skanhtab_cu_hash (const void *of)
6329117395Skan{
6330117395Skan  const struct cu_hash_table_entry *entry = of;
6331117395Skan
6332117395Skan  return htab_hash_string (entry->cu->die_symbol);
6333117395Skan}
6334117395Skan
6335117395Skanstatic int
6336132718Skanhtab_cu_eq (const void *of1, const void *of2)
6337117395Skan{
6338117395Skan  const struct cu_hash_table_entry *entry1 = of1;
6339117395Skan  const struct die_struct *entry2 = of2;
6340117395Skan
6341117395Skan  return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6342117395Skan}
6343117395Skan
6344117395Skanstatic void
6345132718Skanhtab_cu_del (void *what)
6346117395Skan{
6347117395Skan  struct cu_hash_table_entry *next, *entry = what;
6348117395Skan
6349117395Skan  while (entry)
6350117395Skan    {
6351117395Skan      next = entry->next;
6352117395Skan      free (entry);
6353117395Skan      entry = next;
6354117395Skan    }
6355117395Skan}
6356117395Skan
6357117395Skan/* Check whether we have already seen this CU and set up SYM_NUM
6358117395Skan   accordingly.  */
6359117395Skanstatic int
6360132718Skancheck_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6361117395Skan{
6362117395Skan  struct cu_hash_table_entry dummy;
6363117395Skan  struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6364117395Skan
6365117395Skan  dummy.max_comdat_num = 0;
6366117395Skan
6367117395Skan  slot = (struct cu_hash_table_entry **)
6368117395Skan    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6369117395Skan	INSERT);
6370117395Skan  entry = *slot;
6371117395Skan
6372117395Skan  for (; entry; last = entry, entry = entry->next)
6373117395Skan    {
6374117395Skan      if (same_die_p_wrap (cu, entry->cu))
6375117395Skan	break;
6376117395Skan    }
6377117395Skan
6378117395Skan  if (entry)
6379117395Skan    {
6380117395Skan      *sym_num = entry->min_comdat_num;
6381117395Skan      return 1;
6382117395Skan    }
6383117395Skan
6384169689Skan  entry = XCNEW (struct cu_hash_table_entry);
6385117395Skan  entry->cu = cu;
6386117395Skan  entry->min_comdat_num = *sym_num = last->max_comdat_num;
6387117395Skan  entry->next = *slot;
6388117395Skan  *slot = entry;
6389117395Skan
6390117395Skan  return 0;
6391117395Skan}
6392117395Skan
6393117395Skan/* Record SYM_NUM to record of CU in HTABLE.  */
6394117395Skanstatic void
6395132718Skanrecord_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6396117395Skan{
6397117395Skan  struct cu_hash_table_entry **slot, *entry;
6398117395Skan
6399117395Skan  slot = (struct cu_hash_table_entry **)
6400117395Skan    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6401117395Skan	NO_INSERT);
6402117395Skan  entry = *slot;
6403117395Skan
6404117395Skan  entry->max_comdat_num = sym_num;
6405117395Skan}
6406117395Skan
640790075Sobrien/* Traverse the DIE (which is always comp_unit_die), and set up
640890075Sobrien   additional compilation units for each of the include files we see
640990075Sobrien   bracketed by BINCL/EINCL.  */
641090075Sobrien
641190075Sobrienstatic void
6412132718Skanbreak_out_includes (dw_die_ref die)
641390075Sobrien{
6414169689Skan  dw_die_ref c;
641590075Sobrien  dw_die_ref unit = NULL;
6416117395Skan  limbo_die_node *node, **pnode;
6417117395Skan  htab_t cu_hash_table;
641890075Sobrien
6419169689Skan  c = die->die_child;
6420169689Skan  if (c) do {
6421169689Skan    dw_die_ref prev = c;
6422169689Skan    c = c->die_sib;
6423169689Skan    while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6424169689Skan	   || (unit && is_comdat_die (c)))
6425169689Skan      {
6426169689Skan	dw_die_ref next = c->die_sib;
642790075Sobrien
6428169689Skan	/* This DIE is for a secondary CU; remove it from the main one.  */
6429169689Skan	remove_child_with_prev (c, prev);
6430169689Skan
6431169689Skan	if (c->die_tag == DW_TAG_GNU_BINCL)
6432169689Skan	  unit = push_new_compile_unit (unit, c);
6433169689Skan	else if (c->die_tag == DW_TAG_GNU_EINCL)
6434169689Skan	  unit = pop_compile_unit (unit);
6435169689Skan	else
6436169689Skan	  add_child_die (unit, c);
6437169689Skan	c = next;
6438169689Skan	if (c == die->die_child)
6439169689Skan	  break;
6440169689Skan      }
6441169689Skan  } while (c != die->die_child);
644290075Sobrien
644390075Sobrien#if 0
644490075Sobrien  /* We can only use this in debugging, since the frontend doesn't check
644590075Sobrien     to make sure that we leave every include file we enter.  */
6446169689Skan  gcc_assert (!unit);
644790075Sobrien#endif
644890075Sobrien
644990075Sobrien  assign_symbol_names (die);
6450117395Skan  cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6451117395Skan  for (node = limbo_die_list, pnode = &limbo_die_list;
6452117395Skan       node;
6453117395Skan       node = node->next)
645490075Sobrien    {
6455117395Skan      int is_dupl;
6456117395Skan
645790075Sobrien      compute_section_prefix (node->die);
6458117395Skan      is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6459117395Skan			&comdat_symbol_number);
646090075Sobrien      assign_symbol_names (node->die);
6461117395Skan      if (is_dupl)
6462117395Skan	*pnode = node->next;
6463117395Skan      else
6464132718Skan	{
6465117395Skan	  pnode = &node->next;
6466117395Skan	  record_comdat_symbol_number (node->die, cu_hash_table,
6467117395Skan		comdat_symbol_number);
6468117395Skan	}
646990075Sobrien    }
6470117395Skan  htab_delete (cu_hash_table);
647190075Sobrien}
647290075Sobrien
647390075Sobrien/* Traverse the DIE and add a sibling attribute if it may have the
647490075Sobrien   effect of speeding up access to siblings.  To save some space,
647590075Sobrien   avoid generating sibling attributes for DIE's without children.  */
647690075Sobrien
647790075Sobrienstatic void
6478132718Skanadd_sibling_attributes (dw_die_ref die)
647990075Sobrien{
648090075Sobrien  dw_die_ref c;
648190075Sobrien
6482169689Skan  if (! die->die_child)
6483169689Skan    return;
6484169689Skan
6485169689Skan  if (die->die_parent && die != die->die_parent->die_child)
648690075Sobrien    add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
648790075Sobrien
6488169689Skan  FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
648950397Sobrien}
649050397Sobrien
649190075Sobrien/* Output all location lists for the DIE and its children.  */
649250397Sobrien
649350397Sobrienstatic void
6494132718Skanoutput_location_lists (dw_die_ref die)
649590075Sobrien{
649690075Sobrien  dw_die_ref c;
6497169689Skan  dw_attr_ref a;
6498169689Skan  unsigned ix;
649990075Sobrien
6500169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6501169689Skan    if (AT_class (a) == dw_val_class_loc_list)
6502169689Skan      output_loc_list (AT_loc_list (a));
650390075Sobrien
6504169689Skan  FOR_EACH_CHILD (die, c, output_location_lists (c));
650590075Sobrien}
6506117395Skan
650790075Sobrien/* The format of each DIE (and its attribute value pairs) is encoded in an
650890075Sobrien   abbreviation table.  This routine builds the abbreviation table and assigns
650990075Sobrien   a unique abbreviation id for each abbreviation entry.  The children of each
651090075Sobrien   die are visited recursively.  */
651190075Sobrien
651290075Sobrienstatic void
6513132718Skanbuild_abbrev_table (dw_die_ref die)
651450397Sobrien{
651590075Sobrien  unsigned long abbrev_id;
651690075Sobrien  unsigned int n_alloc;
651790075Sobrien  dw_die_ref c;
6518169689Skan  dw_attr_ref a;
6519169689Skan  unsigned ix;
652090075Sobrien
652190075Sobrien  /* Scan the DIE references, and mark as external any that refer to
652290075Sobrien     DIEs from other CUs (i.e. those which are not marked).  */
6523169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6524169689Skan    if (AT_class (a) == dw_val_class_die_ref
6525169689Skan	&& AT_ref (a)->die_mark == 0)
652690075Sobrien      {
6527169689Skan	gcc_assert (AT_ref (a)->die_symbol);
652890075Sobrien
6529169689Skan	set_AT_ref_external (a, 1);
653090075Sobrien      }
653190075Sobrien
653250397Sobrien  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
653350397Sobrien    {
653490075Sobrien      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6535169689Skan      dw_attr_ref die_a, abbrev_a;
6536169689Skan      unsigned ix;
6537169689Skan      bool ok = true;
6538169689Skan
6539169689Skan      if (abbrev->die_tag != die->die_tag)
6540169689Skan	continue;
6541169689Skan      if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6542169689Skan	continue;
6543169689Skan
6544169689Skan      if (VEC_length (dw_attr_node, abbrev->die_attr)
6545169689Skan	  != VEC_length (dw_attr_node, die->die_attr))
6546169689Skan	continue;
6547169689Skan
6548169689Skan      for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
654950397Sobrien	{
6550169689Skan	  abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6551169689Skan	  if ((abbrev_a->dw_attr != die_a->dw_attr)
6552169689Skan	      || (value_format (abbrev_a) != value_format (die_a)))
655350397Sobrien	    {
6554169689Skan	      ok = false;
6555169689Skan	      break;
655650397Sobrien	    }
655750397Sobrien	}
6558169689Skan      if (ok)
6559169689Skan	break;
656050397Sobrien    }
656150397Sobrien
656250397Sobrien  if (abbrev_id >= abbrev_die_table_in_use)
656350397Sobrien    {
656450397Sobrien      if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
656550397Sobrien	{
656650397Sobrien	  n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6567132718Skan	  abbrev_die_table = ggc_realloc (abbrev_die_table,
6568132718Skan					  sizeof (dw_die_ref) * n_alloc);
656950397Sobrien
6570132718Skan	  memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
657150397Sobrien		 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
657250397Sobrien	  abbrev_die_table_allocated = n_alloc;
657350397Sobrien	}
657450397Sobrien
657550397Sobrien      ++abbrev_die_table_in_use;
657650397Sobrien      abbrev_die_table[abbrev_id] = die;
657750397Sobrien    }
657850397Sobrien
657950397Sobrien  die->die_abbrev = abbrev_id;
6580169689Skan  FOR_EACH_CHILD (die, c, build_abbrev_table (c));
658150397Sobrien}
658250397Sobrien
658350397Sobrien/* Return the power-of-two number of bytes necessary to represent VALUE.  */
658450397Sobrien
658550397Sobrienstatic int
6586132718Skanconstant_size (long unsigned int value)
658750397Sobrien{
658850397Sobrien  int log;
658950397Sobrien
659050397Sobrien  if (value == 0)
659150397Sobrien    log = 0;
659250397Sobrien  else
659350397Sobrien    log = floor_log2 (value);
659450397Sobrien
659550397Sobrien  log = log / 8;
659650397Sobrien  log = 1 << (floor_log2 (log) + 1);
659750397Sobrien
659850397Sobrien  return log;
659950397Sobrien}
660050397Sobrien
660190075Sobrien/* Return the size of a DIE as it is represented in the
660250397Sobrien   .debug_info section.  */
660350397Sobrien
660450397Sobrienstatic unsigned long
6605132718Skansize_of_die (dw_die_ref die)
660650397Sobrien{
660790075Sobrien  unsigned long size = 0;
660890075Sobrien  dw_attr_ref a;
6609169689Skan  unsigned ix;
661050397Sobrien
661150397Sobrien  size += size_of_uleb128 (die->die_abbrev);
6612169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
661350397Sobrien    {
661490075Sobrien      switch (AT_class (a))
661550397Sobrien	{
661650397Sobrien	case dw_val_class_addr:
661790075Sobrien	  size += DWARF2_ADDR_SIZE;
661850397Sobrien	  break;
661990075Sobrien	case dw_val_class_offset:
662090075Sobrien	  size += DWARF_OFFSET_SIZE;
662190075Sobrien	  break;
662250397Sobrien	case dw_val_class_loc:
662350397Sobrien	  {
662490075Sobrien	    unsigned long lsize = size_of_locs (AT_loc (a));
662550397Sobrien
662650397Sobrien	    /* Block length.  */
662750397Sobrien	    size += constant_size (lsize);
662850397Sobrien	    size += lsize;
662950397Sobrien	  }
663050397Sobrien	  break;
663190075Sobrien	case dw_val_class_loc_list:
663290075Sobrien	  size += DWARF_OFFSET_SIZE;
663390075Sobrien	  break;
663490075Sobrien	case dw_val_class_range_list:
663590075Sobrien	  size += DWARF_OFFSET_SIZE;
663690075Sobrien	  break;
663750397Sobrien	case dw_val_class_const:
663890075Sobrien	  size += size_of_sleb128 (AT_int (a));
663950397Sobrien	  break;
664050397Sobrien	case dw_val_class_unsigned_const:
664190075Sobrien	  size += constant_size (AT_unsigned (a));
664250397Sobrien	  break;
664350397Sobrien	case dw_val_class_long_long:
664490075Sobrien	  size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
664550397Sobrien	  break;
6646132718Skan	case dw_val_class_vec:
6647132718Skan	  size += 1 + (a->dw_attr_val.v.val_vec.length
6648132718Skan		       * a->dw_attr_val.v.val_vec.elt_size); /* block */
664950397Sobrien	  break;
665050397Sobrien	case dw_val_class_flag:
665150397Sobrien	  size += 1;
665250397Sobrien	  break;
665350397Sobrien	case dw_val_class_die_ref:
6654132718Skan	  if (AT_ref_external (a))
6655132718Skan	    size += DWARF2_ADDR_SIZE;
6656132718Skan	  else
6657132718Skan	    size += DWARF_OFFSET_SIZE;
665850397Sobrien	  break;
665950397Sobrien	case dw_val_class_fde_ref:
666050397Sobrien	  size += DWARF_OFFSET_SIZE;
666150397Sobrien	  break;
666250397Sobrien	case dw_val_class_lbl_id:
666390075Sobrien	  size += DWARF2_ADDR_SIZE;
666450397Sobrien	  break;
6665169689Skan	case dw_val_class_lineptr:
6666169689Skan	case dw_val_class_macptr:
666750397Sobrien	  size += DWARF_OFFSET_SIZE;
666850397Sobrien	  break;
666950397Sobrien	case dw_val_class_str:
667090075Sobrien	  if (AT_string_form (a) == DW_FORM_strp)
667190075Sobrien	    size += DWARF_OFFSET_SIZE;
667290075Sobrien	  else
6673132718Skan	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
667450397Sobrien	  break;
6675169689Skan	case dw_val_class_file:
6676169689Skan	  size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6677169689Skan	  break;
667850397Sobrien	default:
6679169689Skan	  gcc_unreachable ();
668050397Sobrien	}
668150397Sobrien    }
668250397Sobrien
668350397Sobrien  return size;
668450397Sobrien}
668550397Sobrien
668690075Sobrien/* Size the debugging information associated with a given DIE.  Visits the
668790075Sobrien   DIE's children recursively.  Updates the global variable next_die_offset, on
668890075Sobrien   each time through.  Uses the current value of next_die_offset to update the
668990075Sobrien   die_offset field in each DIE.  */
669050397Sobrien
669150397Sobrienstatic void
6692132718Skancalc_die_sizes (dw_die_ref die)
669350397Sobrien{
669490075Sobrien  dw_die_ref c;
669590075Sobrien
669650397Sobrien  die->die_offset = next_die_offset;
669750397Sobrien  next_die_offset += size_of_die (die);
669850397Sobrien
6699169689Skan  FOR_EACH_CHILD (die, c, calc_die_sizes (c));
670050397Sobrien
670150397Sobrien  if (die->die_child != NULL)
670250397Sobrien    /* Count the null byte used to terminate sibling lists.  */
670350397Sobrien    next_die_offset += 1;
670450397Sobrien}
670550397Sobrien
670690075Sobrien/* Set the marks for a die and its children.  We do this so
670790075Sobrien   that we know whether or not a reference needs to use FORM_ref_addr; only
670890075Sobrien   DIEs in the same CU will be marked.  We used to clear out the offset
670990075Sobrien   and use that as the flag, but ran into ordering problems.  */
671050397Sobrien
671190075Sobrienstatic void
6712132718Skanmark_dies (dw_die_ref die)
671350397Sobrien{
671490075Sobrien  dw_die_ref c;
671550397Sobrien
6716169689Skan  gcc_assert (!die->die_mark);
6717132718Skan
671890075Sobrien  die->die_mark = 1;
6719169689Skan  FOR_EACH_CHILD (die, c, mark_dies (c));
672050397Sobrien}
672150397Sobrien
672290075Sobrien/* Clear the marks for a die and its children.  */
672350397Sobrien
672490075Sobrienstatic void
6725132718Skanunmark_dies (dw_die_ref die)
672650397Sobrien{
672790075Sobrien  dw_die_ref c;
672850397Sobrien
6729169689Skan  gcc_assert (die->die_mark);
6730132718Skan
673190075Sobrien  die->die_mark = 0;
6732169689Skan  FOR_EACH_CHILD (die, c, unmark_dies (c));
673350397Sobrien}
673450397Sobrien
6735117395Skan/* Clear the marks for a die, its children and referred dies.  */
6736117395Skan
6737117395Skanstatic void
6738132718Skanunmark_all_dies (dw_die_ref die)
6739117395Skan{
6740117395Skan  dw_die_ref c;
6741117395Skan  dw_attr_ref a;
6742169689Skan  unsigned ix;
6743117395Skan
6744117395Skan  if (!die->die_mark)
6745117395Skan    return;
6746117395Skan  die->die_mark = 0;
6747117395Skan
6748169689Skan  FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6749117395Skan
6750169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6751117395Skan    if (AT_class (a) == dw_val_class_die_ref)
6752117395Skan      unmark_all_dies (AT_ref (a));
6753117395Skan}
6754117395Skan
6755260396Spfg/* Return the size of the .debug_pubnames or .debug_pubtypes table
6756260396Spfg   generated for the compilation unit.  */
675750397Sobrien
675850397Sobrienstatic unsigned long
6759260396Spfgsize_of_pubnames (VEC (pubname_entry, gc) * names)
676050397Sobrien{
676190075Sobrien  unsigned long size;
676290075Sobrien  unsigned i;
6763260396Spfg  pubname_ref p;
676450397Sobrien
676550397Sobrien  size = DWARF_PUBNAMES_HEADER_SIZE;
6766260396Spfg  for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6767260396Spfg    if (names != pubtype_table
6768260396Spfg	|| p->die->die_offset != 0
6769260396Spfg	|| !flag_eliminate_unused_debug_types)
6770260396Spfg      size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
677150397Sobrien
677250397Sobrien  size += DWARF_OFFSET_SIZE;
677350397Sobrien  return size;
677450397Sobrien}
677550397Sobrien
677650397Sobrien/* Return the size of the information in the .debug_aranges section.  */
677750397Sobrien
677850397Sobrienstatic unsigned long
6779132718Skansize_of_aranges (void)
678050397Sobrien{
678190075Sobrien  unsigned long size;
678250397Sobrien
678350397Sobrien  size = DWARF_ARANGES_HEADER_SIZE;
678450397Sobrien
678550397Sobrien  /* Count the address/length pair for this compilation unit.  */
678690075Sobrien  size += 2 * DWARF2_ADDR_SIZE;
678790075Sobrien  size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
678850397Sobrien
678950397Sobrien  /* Count the two zero words used to terminated the address range table.  */
679090075Sobrien  size += 2 * DWARF2_ADDR_SIZE;
679150397Sobrien  return size;
679250397Sobrien}
679350397Sobrien
679450397Sobrien/* Select the encoding of an attribute value.  */
679550397Sobrien
679650397Sobrienstatic enum dwarf_form
6797132718Skanvalue_format (dw_attr_ref a)
679850397Sobrien{
679990075Sobrien  switch (a->dw_attr_val.val_class)
680050397Sobrien    {
680150397Sobrien    case dw_val_class_addr:
680250397Sobrien      return DW_FORM_addr;
680390075Sobrien    case dw_val_class_range_list:
680490075Sobrien    case dw_val_class_offset:
680590075Sobrien    case dw_val_class_loc_list:
6806169689Skan      switch (DWARF_OFFSET_SIZE)
6807169689Skan	{
6808169689Skan	case 4:
6809169689Skan	  return DW_FORM_data4;
6810169689Skan	case 8:
6811169689Skan	  return DW_FORM_data8;
6812169689Skan	default:
6813169689Skan	  gcc_unreachable ();
6814169689Skan	}
681550397Sobrien    case dw_val_class_loc:
681690075Sobrien      switch (constant_size (size_of_locs (AT_loc (a))))
681750397Sobrien	{
681850397Sobrien	case 1:
681950397Sobrien	  return DW_FORM_block1;
682050397Sobrien	case 2:
682150397Sobrien	  return DW_FORM_block2;
682250397Sobrien	default:
6823169689Skan	  gcc_unreachable ();
682450397Sobrien	}
682550397Sobrien    case dw_val_class_const:
682690075Sobrien      return DW_FORM_sdata;
682750397Sobrien    case dw_val_class_unsigned_const:
682890075Sobrien      switch (constant_size (AT_unsigned (a)))
682950397Sobrien	{
683050397Sobrien	case 1:
683150397Sobrien	  return DW_FORM_data1;
683250397Sobrien	case 2:
683350397Sobrien	  return DW_FORM_data2;
683450397Sobrien	case 4:
683550397Sobrien	  return DW_FORM_data4;
683650397Sobrien	case 8:
683750397Sobrien	  return DW_FORM_data8;
683850397Sobrien	default:
6839169689Skan	  gcc_unreachable ();
684050397Sobrien	}
684150397Sobrien    case dw_val_class_long_long:
684250397Sobrien      return DW_FORM_block1;
6843132718Skan    case dw_val_class_vec:
684450397Sobrien      return DW_FORM_block1;
684550397Sobrien    case dw_val_class_flag:
684650397Sobrien      return DW_FORM_flag;
684750397Sobrien    case dw_val_class_die_ref:
684890075Sobrien      if (AT_ref_external (a))
684990075Sobrien	return DW_FORM_ref_addr;
685090075Sobrien      else
685190075Sobrien	return DW_FORM_ref;
685250397Sobrien    case dw_val_class_fde_ref:
685350397Sobrien      return DW_FORM_data;
685450397Sobrien    case dw_val_class_lbl_id:
685550397Sobrien      return DW_FORM_addr;
6856169689Skan    case dw_val_class_lineptr:
6857169689Skan    case dw_val_class_macptr:
685850397Sobrien      return DW_FORM_data;
685950397Sobrien    case dw_val_class_str:
686090075Sobrien      return AT_string_form (a);
6861169689Skan    case dw_val_class_file:
6862169689Skan      switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6863169689Skan	{
6864169689Skan	case 1:
6865169689Skan	  return DW_FORM_data1;
6866169689Skan	case 2:
6867169689Skan	  return DW_FORM_data2;
6868169689Skan	case 4:
6869169689Skan	  return DW_FORM_data4;
6870169689Skan	default:
6871169689Skan	  gcc_unreachable ();
6872169689Skan	}
687390075Sobrien
687450397Sobrien    default:
6875169689Skan      gcc_unreachable ();
687650397Sobrien    }
687750397Sobrien}
687850397Sobrien
687950397Sobrien/* Output the encoding of an attribute value.  */
688050397Sobrien
688150397Sobrienstatic void
6882132718Skanoutput_value_format (dw_attr_ref a)
688350397Sobrien{
688490075Sobrien  enum dwarf_form form = value_format (a);
688550397Sobrien
688690075Sobrien  dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
688750397Sobrien}
688850397Sobrien
688950397Sobrien/* Output the .debug_abbrev section which defines the DIE abbreviation
689050397Sobrien   table.  */
689150397Sobrien
689250397Sobrienstatic void
6893132718Skanoutput_abbrev_section (void)
689450397Sobrien{
689550397Sobrien  unsigned long abbrev_id;
689650397Sobrien
689750397Sobrien  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
689850397Sobrien    {
689990075Sobrien      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6900169689Skan      unsigned ix;
6901169689Skan      dw_attr_ref a_attr;
690250397Sobrien
690390075Sobrien      dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
690490075Sobrien      dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
690590075Sobrien				   dwarf_tag_name (abbrev->die_tag));
690650397Sobrien
690790075Sobrien      if (abbrev->die_child != NULL)
690890075Sobrien	dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
690990075Sobrien      else
691090075Sobrien	dw2_asm_output_data (1, DW_children_no, "DW_children_no");
691150397Sobrien
6912169689Skan      for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6913169689Skan	   ix++)
691450397Sobrien	{
691590075Sobrien	  dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
691690075Sobrien				       dwarf_attr_name (a_attr->dw_attr));
691790075Sobrien	  output_value_format (a_attr);
691850397Sobrien	}
691950397Sobrien
692090075Sobrien      dw2_asm_output_data (1, 0, NULL);
692190075Sobrien      dw2_asm_output_data (1, 0, NULL);
692250397Sobrien    }
692352284Sobrien
692490075Sobrien  /* Terminate the table.  */
692590075Sobrien  dw2_asm_output_data (1, 0, NULL);
692650397Sobrien}
692750397Sobrien
692890075Sobrien/* Output a symbol we can use to refer to this DIE from another CU.  */
692950397Sobrien
693090075Sobrienstatic inline void
6931132718Skanoutput_die_symbol (dw_die_ref die)
693290075Sobrien{
693390075Sobrien  char *sym = die->die_symbol;
693490075Sobrien
693590075Sobrien  if (sym == 0)
693690075Sobrien    return;
693790075Sobrien
693890075Sobrien  if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
693990075Sobrien    /* We make these global, not weak; if the target doesn't support
694090075Sobrien       .linkonce, it doesn't support combining the sections, so debugging
694190075Sobrien       will break.  */
6942169689Skan    targetm.asm_out.globalize_label (asm_out_file, sym);
694390075Sobrien
694490075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, sym);
694590075Sobrien}
694690075Sobrien
694790075Sobrien/* Return a new location list, given the begin and end range, and the
694890075Sobrien   expression. gensym tells us whether to generate a new internal symbol for
694990075Sobrien   this location list node, which is done for the head of the list only.  */
695090075Sobrien
695190075Sobrienstatic inline dw_loc_list_ref
6952132718Skannew_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6953132718Skan	      const char *section, unsigned int gensym)
695490075Sobrien{
6955132718Skan  dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
695690075Sobrien
695790075Sobrien  retlist->begin = begin;
695890075Sobrien  retlist->end = end;
695990075Sobrien  retlist->expr = expr;
696090075Sobrien  retlist->section = section;
6961117395Skan  if (gensym)
696290075Sobrien    retlist->ll_symbol = gen_internal_sym ("LLST");
696390075Sobrien
696490075Sobrien  return retlist;
696590075Sobrien}
696690075Sobrien
6967132718Skan/* Add a location description expression to a location list.  */
696890075Sobrien
696990075Sobrienstatic inline void
6970132718Skanadd_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6971132718Skan			   const char *begin, const char *end,
6972132718Skan			   const char *section)
697390075Sobrien{
697490075Sobrien  dw_loc_list_ref *d;
6975117395Skan
697690075Sobrien  /* Find the end of the chain.  */
697790075Sobrien  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
697890075Sobrien    ;
697990075Sobrien
6980132718Skan  /* Add a new location list node to the list.  */
698190075Sobrien  *d = new_loc_list (descr, begin, end, section, 0);
698290075Sobrien}
698390075Sobrien
6984169689Skanstatic void
6985169689Skandwarf2out_switch_text_section (void)
6986169689Skan{
6987169689Skan  dw_fde_ref fde;
6988169689Skan
6989169689Skan  gcc_assert (cfun);
6990169689Skan
6991169689Skan  fde = &fde_table[fde_table_in_use - 1];
6992169689Skan  fde->dw_fde_switched_sections = true;
6993169689Skan  fde->dw_fde_hot_section_label = cfun->hot_section_label;
6994169689Skan  fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6995169689Skan  fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6996169689Skan  fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6997169689Skan  have_multiple_function_sections = true;
6998169689Skan
6999169689Skan  /* Reset the current label on switching text sections, so that we
7000169689Skan     don't attempt to advance_loc4 between labels in different sections.  */
7001169689Skan  fde->dw_fde_current_label = NULL;
7002169689Skan}
7003169689Skan
7004132718Skan/* Output the location list given to us.  */
700590075Sobrien
700650397Sobrienstatic void
7007132718Skanoutput_loc_list (dw_loc_list_ref list_head)
700850397Sobrien{
700990075Sobrien  dw_loc_list_ref curr = list_head;
701050397Sobrien
701190075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
701290075Sobrien
7013169689Skan  /* Walk the location list, and output each range + expression.  */
7014117395Skan  for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
701590075Sobrien    {
701690075Sobrien      unsigned long size;
7017169689Skan      if (!have_multiple_function_sections)
7018169689Skan	{
7019169689Skan	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7020169689Skan				"Location list begin address (%s)",
7021169689Skan				list_head->ll_symbol);
7022169689Skan	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7023169689Skan				"Location list end address (%s)",
7024169689Skan				list_head->ll_symbol);
7025169689Skan	}
7026169689Skan      else
7027169689Skan	{
7028169689Skan	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7029169689Skan			       "Location list begin address (%s)",
7030169689Skan			       list_head->ll_symbol);
7031169689Skan	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7032169689Skan			       "Location list end address (%s)",
7033169689Skan			       list_head->ll_symbol);
7034169689Skan	}
703590075Sobrien      size = size_of_locs (curr->expr);
7036117395Skan
703790075Sobrien      /* Output the block length for this list of location operations.  */
7038169689Skan      gcc_assert (size <= 0xffff);
703990075Sobrien      dw2_asm_output_data (2, size, "%s", "Location expression size");
704050397Sobrien
704190075Sobrien      output_loc_sequence (curr->expr);
704290075Sobrien    }
704350397Sobrien
7044169689Skan  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
704590075Sobrien		       "Location list terminator begin (%s)",
704690075Sobrien		       list_head->ll_symbol);
7047169689Skan  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
704890075Sobrien		       "Location list terminator end (%s)",
704990075Sobrien		       list_head->ll_symbol);
705050397Sobrien}
705150397Sobrien
705250397Sobrien/* Output the DIE and its attributes.  Called recursively to generate
705350397Sobrien   the definitions of each child DIE.  */
705450397Sobrien
705550397Sobrienstatic void
7056132718Skanoutput_die (dw_die_ref die)
705750397Sobrien{
705890075Sobrien  dw_attr_ref a;
705990075Sobrien  dw_die_ref c;
706090075Sobrien  unsigned long size;
7061169689Skan  unsigned ix;
706250397Sobrien
706390075Sobrien  /* If someone in another CU might refer to us, set up a symbol for
706490075Sobrien     them to point to.  */
706590075Sobrien  if (die->die_symbol)
706690075Sobrien    output_die_symbol (die);
706750397Sobrien
706890075Sobrien  dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7069259948Spfg			       (unsigned long)die->die_offset,
7070259948Spfg			       dwarf_tag_name (die->die_tag));
707150397Sobrien
7072169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
707350397Sobrien    {
707490075Sobrien      const char *name = dwarf_attr_name (a->dw_attr);
707590075Sobrien
707690075Sobrien      switch (AT_class (a))
707750397Sobrien	{
707850397Sobrien	case dw_val_class_addr:
707990075Sobrien	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
708050397Sobrien	  break;
708150397Sobrien
708290075Sobrien	case dw_val_class_offset:
708390075Sobrien	  dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
708490075Sobrien			       "%s", name);
708590075Sobrien	  break;
708650397Sobrien
708790075Sobrien	case dw_val_class_range_list:
708890075Sobrien	  {
708990075Sobrien	    char *p = strchr (ranges_section_label, '\0');
709050397Sobrien
7091132718Skan	    sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7092132718Skan		     a->dw_attr_val.v.val_offset);
709390075Sobrien	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7094169689Skan				   debug_ranges_section, "%s", name);
709590075Sobrien	    *p = '\0';
709690075Sobrien	  }
709790075Sobrien	  break;
709850397Sobrien
709990075Sobrien	case dw_val_class_loc:
710090075Sobrien	  size = size_of_locs (AT_loc (a));
710150397Sobrien
710290075Sobrien	  /* Output the block length for this list of location operations.  */
710390075Sobrien	  dw2_asm_output_data (constant_size (size), size, "%s", name);
710450397Sobrien
710590075Sobrien	  output_loc_sequence (AT_loc (a));
710650397Sobrien	  break;
710750397Sobrien
710850397Sobrien	case dw_val_class_const:
710990075Sobrien	  /* ??? It would be slightly more efficient to use a scheme like is
711090075Sobrien	     used for unsigned constants below, but gdb 4.x does not sign
711190075Sobrien	     extend.  Gdb 5.x does sign extend.  */
711290075Sobrien	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
711350397Sobrien	  break;
711450397Sobrien
711550397Sobrien	case dw_val_class_unsigned_const:
711690075Sobrien	  dw2_asm_output_data (constant_size (AT_unsigned (a)),
711790075Sobrien			       AT_unsigned (a), "%s", name);
711850397Sobrien	  break;
711950397Sobrien
712050397Sobrien	case dw_val_class_long_long:
712190075Sobrien	  {
712290075Sobrien	    unsigned HOST_WIDE_INT first, second;
712350397Sobrien
712490075Sobrien	    dw2_asm_output_data (1,
712590075Sobrien				 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7126117395Skan				 "%s", name);
712750397Sobrien
712890075Sobrien	    if (WORDS_BIG_ENDIAN)
712990075Sobrien	      {
713090075Sobrien		first = a->dw_attr_val.v.val_long_long.hi;
713190075Sobrien		second = a->dw_attr_val.v.val_long_long.low;
713290075Sobrien	      }
713390075Sobrien	    else
713490075Sobrien	      {
713590075Sobrien		first = a->dw_attr_val.v.val_long_long.low;
713690075Sobrien		second = a->dw_attr_val.v.val_long_long.hi;
713790075Sobrien	      }
713890075Sobrien
713990075Sobrien	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
714090075Sobrien				 first, "long long constant");
714190075Sobrien	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
714290075Sobrien				 second, NULL);
714390075Sobrien	  }
714450397Sobrien	  break;
714550397Sobrien
7146132718Skan	case dw_val_class_vec:
714752284Sobrien	  {
7148132718Skan	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7149132718Skan	    unsigned int len = a->dw_attr_val.v.val_vec.length;
715090075Sobrien	    unsigned int i;
7151132718Skan	    unsigned char *p;
715250397Sobrien
7153132718Skan	    dw2_asm_output_data (1, len * elt_size, "%s", name);
7154132718Skan	    if (elt_size > sizeof (HOST_WIDE_INT))
7155132718Skan	      {
7156132718Skan		elt_size /= 2;
7157132718Skan		len *= 2;
7158132718Skan	      }
7159132718Skan	    for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7160132718Skan		 i < len;
7161132718Skan		 i++, p += elt_size)
7162132718Skan	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7163132718Skan				   "fp or vector constant word %u", i);
716490075Sobrien	    break;
716552284Sobrien	  }
716650397Sobrien
716750397Sobrien	case dw_val_class_flag:
716890075Sobrien	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
716950397Sobrien	  break;
717050397Sobrien
7171117395Skan	case dw_val_class_loc_list:
717290075Sobrien	  {
717390075Sobrien	    char *sym = AT_loc_list (a)->ll_symbol;
717490075Sobrien
7175169689Skan	    gcc_assert (sym);
7176169689Skan	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7177169689Skan				   "%s", name);
717890075Sobrien	  }
717990075Sobrien	  break;
718090075Sobrien
718150397Sobrien	case dw_val_class_die_ref:
718290075Sobrien	  if (AT_ref_external (a))
718390075Sobrien	    {
718490075Sobrien	      char *sym = AT_ref (a)->die_symbol;
718590075Sobrien
7186169689Skan	      gcc_assert (sym);
7187169689Skan	      dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7188169689Skan				     "%s", name);
718990075Sobrien	    }
719050397Sobrien	  else
7191169689Skan	    {
7192169689Skan	      gcc_assert (AT_ref (a)->die_offset);
7193169689Skan	      dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7194169689Skan				   "%s", name);
7195169689Skan	    }
719650397Sobrien	  break;
719750397Sobrien
719850397Sobrien	case dw_val_class_fde_ref:
719950397Sobrien	  {
720050397Sobrien	    char l1[20];
720190075Sobrien
720290075Sobrien	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
720390075Sobrien					 a->dw_attr_val.v.val_fde_index * 2);
7204169689Skan	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7205169689Skan				   "%s", name);
720650397Sobrien	  }
720750397Sobrien	  break;
720850397Sobrien
720950397Sobrien	case dw_val_class_lbl_id:
721090075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
721150397Sobrien	  break;
721250397Sobrien
7213169689Skan	case dw_val_class_lineptr:
7214169689Skan	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7215169689Skan				 debug_line_section, "%s", name);
721650397Sobrien	  break;
721750397Sobrien
7218169689Skan	case dw_val_class_macptr:
7219169689Skan	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7220169689Skan				 debug_macinfo_section, "%s", name);
7221169689Skan	  break;
7222169689Skan
722350397Sobrien	case dw_val_class_str:
722490075Sobrien	  if (AT_string_form (a) == DW_FORM_strp)
722590075Sobrien	    dw2_asm_output_offset (DWARF_OFFSET_SIZE,
722690075Sobrien				   a->dw_attr_val.v.val_str->label,
7227169689Skan				   debug_str_section,
722890075Sobrien				   "%s: \"%s\"", name, AT_string (a));
722950397Sobrien	  else
723090075Sobrien	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
723150397Sobrien	  break;
723250397Sobrien
7233169689Skan	case dw_val_class_file:
7234169689Skan	  {
7235169689Skan	    int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7236169689Skan
7237169689Skan	    dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7238169689Skan				 a->dw_attr_val.v.val_file->filename);
7239169689Skan	    break;
7240169689Skan	  }
7241169689Skan
724250397Sobrien	default:
7243169689Skan	  gcc_unreachable ();
724450397Sobrien	}
724550397Sobrien    }
724650397Sobrien
7247169689Skan  FOR_EACH_CHILD (die, c, output_die (c));
724850397Sobrien
724990075Sobrien  /* Add null byte to terminate sibling list.  */
725050397Sobrien  if (die->die_child != NULL)
725190075Sobrien    dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7252259948Spfg			 (unsigned long) die->die_offset);
725350397Sobrien}
725450397Sobrien
725550397Sobrien/* Output the compilation unit that appears at the beginning of the
725650397Sobrien   .debug_info section, and precedes the DIE descriptions.  */
725750397Sobrien
725850397Sobrienstatic void
7259132718Skanoutput_compilation_unit_header (void)
726050397Sobrien{
7261132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7262132718Skan    dw2_asm_output_data (4, 0xffffffff,
7263132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
7264132718Skan  dw2_asm_output_data (DWARF_OFFSET_SIZE,
7265132718Skan                       next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
726690075Sobrien		       "Length of Compilation Unit Info");
726790075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
726890075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7269169689Skan			 debug_abbrev_section,
727090075Sobrien			 "Offset Into Abbrev. Section");
727190075Sobrien  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
727290075Sobrien}
727350397Sobrien
727490075Sobrien/* Output the compilation unit DIE and its children.  */
727550397Sobrien
727690075Sobrienstatic void
7277132718Skanoutput_comp_unit (dw_die_ref die, int output_if_empty)
727890075Sobrien{
727990075Sobrien  const char *secname;
7280117395Skan  char *oldsym, *tmp;
728150397Sobrien
7282117395Skan  /* Unless we are outputting main CU, we may throw away empty ones.  */
7283117395Skan  if (!output_if_empty && die->die_child == NULL)
7284117395Skan    return;
7285117395Skan
728690075Sobrien  /* Even if there are no children of this DIE, we must output the information
728790075Sobrien     about the compilation unit.  Otherwise, on an empty translation unit, we
728890075Sobrien     will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
728990075Sobrien     will then complain when examining the file.  First mark all the DIEs in
729090075Sobrien     this CU so we know which get local refs.  */
729190075Sobrien  mark_dies (die);
729250397Sobrien
729390075Sobrien  build_abbrev_table (die);
729490075Sobrien
729590075Sobrien  /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
729690075Sobrien  next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
729790075Sobrien  calc_die_sizes (die);
729890075Sobrien
7299117395Skan  oldsym = die->die_symbol;
7300117395Skan  if (oldsym)
730190075Sobrien    {
7302132718Skan      tmp = alloca (strlen (oldsym) + 24);
730390075Sobrien
7304117395Skan      sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
730590075Sobrien      secname = tmp;
730690075Sobrien      die->die_symbol = NULL;
7307169689Skan      switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
730890075Sobrien    }
730990075Sobrien  else
7310169689Skan    switch_to_section (debug_info_section);
731190075Sobrien
731290075Sobrien  /* Output debugging information.  */
731390075Sobrien  output_compilation_unit_header ();
731490075Sobrien  output_die (die);
731590075Sobrien
731690075Sobrien  /* Leave the marks on the main CU, so we can check them in
731790075Sobrien     output_pubnames.  */
7318117395Skan  if (oldsym)
7319117395Skan    {
7320117395Skan      unmark_dies (die);
7321117395Skan      die->die_symbol = oldsym;
7322117395Skan    }
732350397Sobrien}
732450397Sobrien
7325169689Skan/* Return the DWARF2/3 pubname associated with a decl.  */
732650397Sobrien
732790075Sobrienstatic const char *
7328132718Skandwarf2_name (tree decl, int scope)
732950397Sobrien{
7330169689Skan  return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
733150397Sobrien}
733250397Sobrien
733350397Sobrien/* Add a new entry to .debug_pubnames if appropriate.  */
733450397Sobrien
733550397Sobrienstatic void
7336132718Skanadd_pubname (tree decl, dw_die_ref die)
733750397Sobrien{
7338260396Spfg  pubname_entry e;
733950397Sobrien
734050397Sobrien  if (! TREE_PUBLIC (decl))
734150397Sobrien    return;
734250397Sobrien
7343260396Spfg  e.die = die;
7344260396Spfg  e.name = xstrdup (dwarf2_name (decl, 1));
7345260396Spfg  VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7346260396Spfg}
7347260396Spfg
7348260396Spfg/* Add a new entry to .debug_pubtypes if appropriate.  */
7349260396Spfg
7350260396Spfgstatic void
7351260396Spfgadd_pubtype (tree decl, dw_die_ref die)
7352260396Spfg{
7353260396Spfg  pubname_entry e;
7354260396Spfg
7355260396Spfg  e.name = NULL;
7356260396Spfg  if ((TREE_PUBLIC (decl)
7357260396Spfg       || die->die_parent == comp_unit_die)
7358260396Spfg      && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
735950397Sobrien    {
7360260396Spfg      e.die = die;
7361260396Spfg      if (TYPE_P (decl))
7362260396Spfg	{
7363260396Spfg	  if (TYPE_NAME (decl))
7364260396Spfg	    {
7365260396Spfg	      if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7366260396Spfg		e.name = xstrdup ((const char *) IDENTIFIER_POINTER
7367260396Spfg				                              (TYPE_NAME (decl)));
7368260396Spfg	      else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7369260396Spfg		       && DECL_NAME (TYPE_NAME (decl)))
7370260396Spfg		e.name = xstrdup ((const char *) IDENTIFIER_POINTER
7371260396Spfg				                  (DECL_NAME (TYPE_NAME (decl))));
7372260396Spfg             else
7373260396Spfg	       e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7374260396Spfg	    }
7375260396Spfg	}
7376260396Spfg      else
7377260396Spfg	e.name = xstrdup (dwarf2_name (decl, 1));
7378260396Spfg
7379260396Spfg      /* If we don't have a name for the type, there's no point in adding
7380260396Spfg	 it to the table.  */
7381260396Spfg      if (e.name && e.name[0] != '\0')
7382260396Spfg	VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
738350397Sobrien    }
738450397Sobrien}
738550397Sobrien
738650397Sobrien/* Output the public names table used to speed up access to externally
7387260396Spfg   visible names; or the public types table used to find type definitions.  */
738850397Sobrien
738950397Sobrienstatic void
7390260396Spfgoutput_pubnames (VEC (pubname_entry, gc) * names)
739150397Sobrien{
739290075Sobrien  unsigned i;
7393260396Spfg  unsigned long pubnames_length = size_of_pubnames (names);
7394260396Spfg  pubname_ref pub;
739550397Sobrien
7396132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7397132718Skan    dw2_asm_output_data (4, 0xffffffff,
7398132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
7399260396Spfg  if (names == pubname_table)
7400260396Spfg    dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7401260396Spfg			 "Length of Public Names Info");
7402260396Spfg  else
7403260396Spfg    dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7404260396Spfg			 "Length of Public Type Names Info");
740590075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
740690075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7407169689Skan			 debug_info_section,
740890075Sobrien			 "Offset of Compilation Unit Info");
740990075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
741090075Sobrien		       "Compilation Unit Length");
741150397Sobrien
7412260396Spfg  for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
741350397Sobrien    {
7414260396Spfg      /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7415260396Spfg      if (names == pubname_table)
7416260396Spfg	gcc_assert (pub->die->die_mark);
741750397Sobrien
7418260396Spfg      if (names != pubtype_table
7419260396Spfg	  || pub->die->die_offset != 0
7420260396Spfg	  || !flag_eliminate_unused_debug_types)
7421260396Spfg	{
7422260396Spfg	  dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7423260396Spfg			       "DIE offset");
742450397Sobrien
7425260396Spfg	  dw2_asm_output_nstring (pub->name, -1, "external name");
7426260396Spfg	}
742750397Sobrien    }
742850397Sobrien
742990075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
743050397Sobrien}
743150397Sobrien
743250397Sobrien/* Add a new entry to .debug_aranges if appropriate.  */
743350397Sobrien
743450397Sobrienstatic void
7435132718Skanadd_arange (tree decl, dw_die_ref die)
743650397Sobrien{
743750397Sobrien  if (! DECL_SECTION_NAME (decl))
743850397Sobrien    return;
743950397Sobrien
744050397Sobrien  if (arange_table_in_use == arange_table_allocated)
744150397Sobrien    {
744250397Sobrien      arange_table_allocated += ARANGE_TABLE_INCREMENT;
7443132718Skan      arange_table = ggc_realloc (arange_table,
7444132718Skan				  (arange_table_allocated
7445132718Skan				   * sizeof (dw_die_ref)));
7446132718Skan      memset (arange_table + arange_table_in_use, 0,
7447132718Skan	      ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
744850397Sobrien    }
744950397Sobrien
745050397Sobrien  arange_table[arange_table_in_use++] = die;
745150397Sobrien}
745250397Sobrien
745350397Sobrien/* Output the information that goes into the .debug_aranges table.
745450397Sobrien   Namely, define the beginning and ending address range of the
745550397Sobrien   text section generated for this compilation unit.  */
745650397Sobrien
745750397Sobrienstatic void
7458132718Skanoutput_aranges (void)
745950397Sobrien{
746090075Sobrien  unsigned i;
746190075Sobrien  unsigned long aranges_length = size_of_aranges ();
746250397Sobrien
7463132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7464132718Skan    dw2_asm_output_data (4, 0xffffffff,
7465132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
746690075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
746790075Sobrien		       "Length of Address Ranges Info");
746890075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
746990075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7470169689Skan			 debug_info_section,
747190075Sobrien			 "Offset of Compilation Unit Info");
747290075Sobrien  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
747390075Sobrien  dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
747450397Sobrien
747590075Sobrien  /* We need to align to twice the pointer size here.  */
747690075Sobrien  if (DWARF_ARANGES_PAD_SIZE)
747790075Sobrien    {
747890075Sobrien      /* Pad using a 2 byte words so that padding is correct for any
7479132718Skan	 pointer size.  */
748090075Sobrien      dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
748190075Sobrien			   2 * DWARF2_ADDR_SIZE);
748290075Sobrien      for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
748390075Sobrien	dw2_asm_output_data (2, 0, NULL);
748490075Sobrien    }
748550397Sobrien
748690075Sobrien  dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
748790075Sobrien  dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
748890075Sobrien			text_section_label, "Length");
7489169689Skan  if (flag_reorder_blocks_and_partition)
7490169689Skan    {
7491169689Skan      dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7492169689Skan			   "Address");
7493169689Skan      dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7494169689Skan			    cold_text_section_label, "Length");
7495169689Skan    }
749650397Sobrien
749790075Sobrien  for (i = 0; i < arange_table_in_use; i++)
749890075Sobrien    {
749990075Sobrien      dw_die_ref die = arange_table[i];
750050397Sobrien
750190075Sobrien      /* We shouldn't see aranges for DIEs outside of the main CU.  */
7502169689Skan      gcc_assert (die->die_mark);
750350397Sobrien
750490075Sobrien      if (die->die_tag == DW_TAG_subprogram)
750590075Sobrien	{
750690075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
750790075Sobrien			       "Address");
750890075Sobrien	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
750990075Sobrien				get_AT_low_pc (die), "Length");
751090075Sobrien	}
751190075Sobrien      else
751290075Sobrien	{
751390075Sobrien	  /* A static variable; extract the symbol from DW_AT_location.
751490075Sobrien	     Note that this code isn't currently hit, as we only emit
751590075Sobrien	     aranges for functions (jason 9/23/99).  */
751690075Sobrien	  dw_attr_ref a = get_AT (die, DW_AT_location);
751790075Sobrien	  dw_loc_descr_ref loc;
751850397Sobrien
7519169689Skan	  gcc_assert (a && AT_class (a) == dw_val_class_loc);
752050397Sobrien
752190075Sobrien	  loc = AT_loc (a);
7522169689Skan	  gcc_assert (loc->dw_loc_opc == DW_OP_addr);
752350397Sobrien
752490075Sobrien	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
752590075Sobrien				   loc->dw_loc_oprnd1.v.val_addr, "Address");
752690075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE,
752790075Sobrien			       get_AT_unsigned (die, DW_AT_byte_size),
752890075Sobrien			       "Length");
752990075Sobrien	}
753090075Sobrien    }
753150397Sobrien
753290075Sobrien  /* Output the terminator words.  */
753390075Sobrien  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
753490075Sobrien  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
753590075Sobrien}
753690075Sobrien
753790075Sobrien/* Add a new entry to .debug_ranges.  Return the offset at which it
753890075Sobrien   was placed.  */
753990075Sobrien
754090075Sobrienstatic unsigned int
7541132718Skanadd_ranges (tree block)
754290075Sobrien{
754390075Sobrien  unsigned int in_use = ranges_table_in_use;
754490075Sobrien
754590075Sobrien  if (in_use == ranges_table_allocated)
754650397Sobrien    {
754790075Sobrien      ranges_table_allocated += RANGES_TABLE_INCREMENT;
7548132718Skan      ranges_table
7549132718Skan	= ggc_realloc (ranges_table, (ranges_table_allocated
7550132718Skan				      * sizeof (struct dw_ranges_struct)));
7551132718Skan      memset (ranges_table + ranges_table_in_use, 0,
7552132718Skan	      RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
755390075Sobrien    }
755450397Sobrien
755590075Sobrien  ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
755690075Sobrien  ranges_table_in_use = in_use + 1;
755790075Sobrien
755890075Sobrien  return in_use * 2 * DWARF2_ADDR_SIZE;
755990075Sobrien}
756090075Sobrien
756190075Sobrienstatic void
7562132718Skanoutput_ranges (void)
756390075Sobrien{
756490075Sobrien  unsigned i;
756590075Sobrien  static const char *const start_fmt = "Offset 0x%x";
756690075Sobrien  const char *fmt = start_fmt;
756790075Sobrien
756890075Sobrien  for (i = 0; i < ranges_table_in_use; i++)
756990075Sobrien    {
757090075Sobrien      int block_num = ranges_table[i].block_num;
757190075Sobrien
757290075Sobrien      if (block_num)
757390075Sobrien	{
757490075Sobrien	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
757590075Sobrien	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
757690075Sobrien
757790075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
757890075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
757990075Sobrien
758090075Sobrien	  /* If all code is in the text section, then the compilation
758190075Sobrien	     unit base address defaults to DW_AT_low_pc, which is the
758290075Sobrien	     base of the text section.  */
7583169689Skan	  if (!have_multiple_function_sections)
758490075Sobrien	    {
758590075Sobrien	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
758690075Sobrien				    text_section_label,
758790075Sobrien				    fmt, i * 2 * DWARF2_ADDR_SIZE);
758890075Sobrien	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
758990075Sobrien				    text_section_label, NULL);
759090075Sobrien	    }
759190075Sobrien
759290075Sobrien	  /* Otherwise, we add a DW_AT_entry_pc attribute to force the
759390075Sobrien	     compilation unit base address to zero, which allows us to
759490075Sobrien	     use absolute addresses, and not worry about whether the
759590075Sobrien	     target supports cross-section arithmetic.  */
759690075Sobrien	  else
759790075Sobrien	    {
759890075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
759990075Sobrien				   fmt, i * 2 * DWARF2_ADDR_SIZE);
760090075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
760190075Sobrien	    }
760290075Sobrien
760390075Sobrien	  fmt = NULL;
760490075Sobrien	}
760550397Sobrien      else
760650397Sobrien	{
760790075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
760890075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
760990075Sobrien	  fmt = start_fmt;
761090075Sobrien	}
761190075Sobrien    }
761290075Sobrien}
761350397Sobrien
761490075Sobrien/* Data structure containing information about input files.  */
761590075Sobrienstruct file_info
761690075Sobrien{
7617169689Skan  const char *path;	/* Complete file name.  */
7618169689Skan  const char *fname;	/* File name part.  */
761990075Sobrien  int length;		/* Length of entire string.  */
7620169689Skan  struct dwarf_file_data * file_idx;	/* Index in input file table.  */
762190075Sobrien  int dir_idx;		/* Index in directory table.  */
762290075Sobrien};
762390075Sobrien
762490075Sobrien/* Data structure containing information about directories with source
762590075Sobrien   files.  */
762690075Sobrienstruct dir_info
762790075Sobrien{
7628169689Skan  const char *path;	/* Path including directory name.  */
762990075Sobrien  int length;		/* Path length.  */
763090075Sobrien  int prefix;		/* Index of directory entry which is a prefix.  */
763190075Sobrien  int count;		/* Number of files in this directory.  */
763290075Sobrien  int dir_idx;		/* Index of directory used as base.  */
763390075Sobrien};
763490075Sobrien
763590075Sobrien/* Callback function for file_info comparison.  We sort by looking at
763690075Sobrien   the directories in the path.  */
763790075Sobrien
763890075Sobrienstatic int
7639132718Skanfile_info_cmp (const void *p1, const void *p2)
764090075Sobrien{
764190075Sobrien  const struct file_info *s1 = p1;
764290075Sobrien  const struct file_info *s2 = p2;
764390075Sobrien  unsigned char *cp1;
764490075Sobrien  unsigned char *cp2;
764590075Sobrien
764690075Sobrien  /* Take care of file names without directories.  We need to make sure that
764790075Sobrien     we return consistent values to qsort since some will get confused if
764890075Sobrien     we return the same value when identical operands are passed in opposite
764990075Sobrien     orders.  So if neither has a directory, return 0 and otherwise return
765090075Sobrien     1 or -1 depending on which one has the directory.  */
765190075Sobrien  if ((s1->path == s1->fname || s2->path == s2->fname))
765290075Sobrien    return (s2->path == s2->fname) - (s1->path == s1->fname);
765390075Sobrien
765490075Sobrien  cp1 = (unsigned char *) s1->path;
765590075Sobrien  cp2 = (unsigned char *) s2->path;
765690075Sobrien
765790075Sobrien  while (1)
765890075Sobrien    {
765990075Sobrien      ++cp1;
766090075Sobrien      ++cp2;
766190075Sobrien      /* Reached the end of the first path?  If so, handle like above.  */
766290075Sobrien      if ((cp1 == (unsigned char *) s1->fname)
766390075Sobrien	  || (cp2 == (unsigned char *) s2->fname))
766490075Sobrien	return ((cp2 == (unsigned char *) s2->fname)
766590075Sobrien		- (cp1 == (unsigned char *) s1->fname));
766690075Sobrien
766790075Sobrien      /* Character of current path component the same?  */
766890075Sobrien      else if (*cp1 != *cp2)
766990075Sobrien	return *cp1 - *cp2;
767090075Sobrien    }
767190075Sobrien}
767290075Sobrien
7673169689Skanstruct file_name_acquire_data
7674169689Skan{
7675169689Skan  struct file_info *files;
7676169689Skan  int used_files;
7677169689Skan  int max_files;
7678169689Skan};
7679169689Skan
7680169689Skan/* Traversal function for the hash table.  */
7681169689Skan
7682169689Skanstatic int
7683169689Skanfile_name_acquire (void ** slot, void *data)
7684169689Skan{
7685169689Skan  struct file_name_acquire_data *fnad = data;
7686169689Skan  struct dwarf_file_data *d = *slot;
7687169689Skan  struct file_info *fi;
7688169689Skan  const char *f;
7689169689Skan
7690169689Skan  gcc_assert (fnad->max_files >= d->emitted_number);
7691169689Skan
7692169689Skan  if (! d->emitted_number)
7693169689Skan    return 1;
7694169689Skan
7695169689Skan  gcc_assert (fnad->max_files != fnad->used_files);
7696169689Skan
7697169689Skan  fi = fnad->files + fnad->used_files++;
7698169689Skan
7699169689Skan  /* Skip all leading "./".  */
7700169689Skan  f = d->filename;
7701169689Skan  while (f[0] == '.' && f[1] == '/')
7702169689Skan    f += 2;
7703169689Skan
7704169689Skan  /* Create a new array entry.  */
7705169689Skan  fi->path = f;
7706169689Skan  fi->length = strlen (f);
7707169689Skan  fi->file_idx = d;
7708169689Skan
7709169689Skan  /* Search for the file name part.  */
7710169689Skan  f = strrchr (f, '/');
7711169689Skan  fi->fname = f == NULL ? fi->path : f + 1;
7712169689Skan  return 1;
7713169689Skan}
7714169689Skan
771590075Sobrien/* Output the directory table and the file name table.  We try to minimize
771690075Sobrien   the total amount of memory needed.  A heuristic is used to avoid large
771790075Sobrien   slowdowns with many input files.  */
771890075Sobrien
771990075Sobrienstatic void
7720132718Skanoutput_file_names (void)
772190075Sobrien{
7722169689Skan  struct file_name_acquire_data fnad;
7723169689Skan  int numfiles;
772490075Sobrien  struct file_info *files;
772590075Sobrien  struct dir_info *dirs;
772690075Sobrien  int *saved;
772790075Sobrien  int *savehere;
772890075Sobrien  int *backmap;
7729169689Skan  int ndirs;
773090075Sobrien  int idx_offset;
7731169689Skan  int i;
773290075Sobrien  int idx;
773390075Sobrien
7734169689Skan  if (!last_emitted_file)
7735132718Skan    {
7736132718Skan      dw2_asm_output_data (1, 0, "End directory table");
7737132718Skan      dw2_asm_output_data (1, 0, "End file name table");
7738132718Skan      return;
7739132718Skan    }
7740132718Skan
7741169689Skan  numfiles = last_emitted_file->emitted_number;
7742169689Skan
774390075Sobrien  /* Allocate the various arrays we need.  */
7744169689Skan  files = alloca (numfiles * sizeof (struct file_info));
7745169689Skan  dirs = alloca (numfiles * sizeof (struct dir_info));
774690075Sobrien
7747169689Skan  fnad.files = files;
7748169689Skan  fnad.used_files = 0;
7749169689Skan  fnad.max_files = numfiles;
7750169689Skan  htab_traverse (file_table, file_name_acquire, &fnad);
7751169689Skan  gcc_assert (fnad.used_files == fnad.max_files);
775290075Sobrien
7753169689Skan  qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
775490075Sobrien
775590075Sobrien  /* Find all the different directories used.  */
7756169689Skan  dirs[0].path = files[0].path;
7757169689Skan  dirs[0].length = files[0].fname - files[0].path;
775890075Sobrien  dirs[0].prefix = -1;
775990075Sobrien  dirs[0].count = 1;
776090075Sobrien  dirs[0].dir_idx = 0;
7761169689Skan  files[0].dir_idx = 0;
776290075Sobrien  ndirs = 1;
776390075Sobrien
7764169689Skan  for (i = 1; i < numfiles; i++)
776590075Sobrien    if (files[i].fname - files[i].path == dirs[ndirs - 1].length
776690075Sobrien	&& memcmp (dirs[ndirs - 1].path, files[i].path,
776790075Sobrien		   dirs[ndirs - 1].length) == 0)
776890075Sobrien      {
776990075Sobrien	/* Same directory as last entry.  */
777090075Sobrien	files[i].dir_idx = ndirs - 1;
777190075Sobrien	++dirs[ndirs - 1].count;
777290075Sobrien      }
777390075Sobrien    else
777490075Sobrien      {
7775169689Skan	int j;
777690075Sobrien
777790075Sobrien	/* This is a new directory.  */
777890075Sobrien	dirs[ndirs].path = files[i].path;
777990075Sobrien	dirs[ndirs].length = files[i].fname - files[i].path;
778090075Sobrien	dirs[ndirs].count = 1;
778190075Sobrien	dirs[ndirs].dir_idx = ndirs;
778290075Sobrien	files[i].dir_idx = ndirs;
778390075Sobrien
778490075Sobrien	/* Search for a prefix.  */
778590075Sobrien	dirs[ndirs].prefix = -1;
778690075Sobrien	for (j = 0; j < ndirs; j++)
778790075Sobrien	  if (dirs[j].length < dirs[ndirs].length
778890075Sobrien	      && dirs[j].length > 1
778990075Sobrien	      && (dirs[ndirs].prefix == -1
779090075Sobrien		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
779190075Sobrien	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
779290075Sobrien	    dirs[ndirs].prefix = j;
779390075Sobrien
779490075Sobrien	++ndirs;
779590075Sobrien      }
779690075Sobrien
779790075Sobrien  /* Now to the actual work.  We have to find a subset of the directories which
779890075Sobrien     allow expressing the file name using references to the directory table
779990075Sobrien     with the least amount of characters.  We do not do an exhaustive search
780090075Sobrien     where we would have to check out every combination of every single
780190075Sobrien     possible prefix.  Instead we use a heuristic which provides nearly optimal
780290075Sobrien     results in most cases and never is much off.  */
7803132718Skan  saved = alloca (ndirs * sizeof (int));
7804132718Skan  savehere = alloca (ndirs * sizeof (int));
780590075Sobrien
780690075Sobrien  memset (saved, '\0', ndirs * sizeof (saved[0]));
780790075Sobrien  for (i = 0; i < ndirs; i++)
780890075Sobrien    {
7809169689Skan      int j;
781090075Sobrien      int total;
781190075Sobrien
781290075Sobrien      /* We can always save some space for the current directory.  But this
781390075Sobrien	 does not mean it will be enough to justify adding the directory.  */
781490075Sobrien      savehere[i] = dirs[i].length;
781590075Sobrien      total = (savehere[i] - saved[i]) * dirs[i].count;
781690075Sobrien
781790075Sobrien      for (j = i + 1; j < ndirs; j++)
781890075Sobrien	{
781990075Sobrien	  savehere[j] = 0;
782090075Sobrien	  if (saved[j] < dirs[i].length)
782190075Sobrien	    {
782290075Sobrien	      /* Determine whether the dirs[i] path is a prefix of the
782390075Sobrien		 dirs[j] path.  */
782490075Sobrien	      int k;
782590075Sobrien
782690075Sobrien	      k = dirs[j].prefix;
7827132718Skan	      while (k != -1 && k != (int) i)
782890075Sobrien		k = dirs[k].prefix;
782990075Sobrien
7830132718Skan	      if (k == (int) i)
783190075Sobrien		{
7832169689Skan		  /* Yes it is.  We can possibly save some memory by
783390075Sobrien		     writing the filenames in dirs[j] relative to
783490075Sobrien		     dirs[i].  */
783590075Sobrien		  savehere[j] = dirs[i].length;
783690075Sobrien		  total += (savehere[j] - saved[j]) * dirs[j].count;
783790075Sobrien		}
783890075Sobrien	    }
783950397Sobrien	}
784050397Sobrien
7841169689Skan      /* Check whether we can save enough to justify adding the dirs[i]
784290075Sobrien	 directory.  */
784390075Sobrien      if (total > dirs[i].length + 1)
784490075Sobrien	{
784590075Sobrien	  /* It's worthwhile adding.  */
7846117395Skan	  for (j = i; j < ndirs; j++)
784790075Sobrien	    if (savehere[j] > 0)
784890075Sobrien	      {
784990075Sobrien		/* Remember how much we saved for this directory so far.  */
785090075Sobrien		saved[j] = savehere[j];
785150397Sobrien
785290075Sobrien		/* Remember the prefix directory.  */
785390075Sobrien		dirs[j].dir_idx = i;
785490075Sobrien	      }
785590075Sobrien	}
785690075Sobrien    }
785750397Sobrien
7858169689Skan  /* Emit the directory name table.  */
785990075Sobrien  idx = 1;
786090075Sobrien  idx_offset = dirs[0].length > 0 ? 1 : 0;
786190075Sobrien  for (i = 1 - idx_offset; i < ndirs; i++)
7862169689Skan    dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7863169689Skan			    "Directory Entry: 0x%x", i + idx_offset);
786490075Sobrien
786590075Sobrien  dw2_asm_output_data (1, 0, "End directory table");
786690075Sobrien
7867169689Skan  /* We have to emit them in the order of emitted_number since that's
7868169689Skan     used in the debug info generation.  To do this efficiently we
7869169689Skan     generate a back-mapping of the indices first.  */
7870169689Skan  backmap = alloca (numfiles * sizeof (int));
7871169689Skan  for (i = 0; i < numfiles; i++)
7872169689Skan    backmap[files[i].file_idx->emitted_number - 1] = i;
787390075Sobrien
787490075Sobrien  /* Now write all the file names.  */
7875169689Skan  for (i = 0; i < numfiles; i++)
787690075Sobrien    {
787790075Sobrien      int file_idx = backmap[i];
787890075Sobrien      int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
787990075Sobrien
788090075Sobrien      dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7881169689Skan			      "File Entry: 0x%x", (unsigned) i + 1);
788290075Sobrien
788390075Sobrien      /* Include directory index.  */
7884169689Skan      dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
788590075Sobrien
788690075Sobrien      /* Modification time.  */
788790075Sobrien      dw2_asm_output_data_uleb128 (0, NULL);
788890075Sobrien
788990075Sobrien      /* File length in bytes.  */
789090075Sobrien      dw2_asm_output_data_uleb128 (0, NULL);
789190075Sobrien    }
789290075Sobrien
789390075Sobrien  dw2_asm_output_data (1, 0, "End file name table");
789450397Sobrien}
789550397Sobrien
789690075Sobrien
789750397Sobrien/* Output the source line number correspondence information.  This
789890075Sobrien   information goes into the .debug_line section.  */
789950397Sobrien
790050397Sobrienstatic void
7901132718Skanoutput_line_info (void)
790250397Sobrien{
790390075Sobrien  char l1[20], l2[20], p1[20], p2[20];
790450397Sobrien  char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
790550397Sobrien  char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
790690075Sobrien  unsigned opc;
790790075Sobrien  unsigned n_op_args;
790890075Sobrien  unsigned long lt_index;
790990075Sobrien  unsigned long current_line;
791090075Sobrien  long line_offset;
791190075Sobrien  long line_delta;
791290075Sobrien  unsigned long current_file;
791390075Sobrien  unsigned long function;
791450397Sobrien
791590075Sobrien  ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
791690075Sobrien  ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
791790075Sobrien  ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
791890075Sobrien  ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
791950397Sobrien
7920132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7921132718Skan    dw2_asm_output_data (4, 0xffffffff,
7922132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
792390075Sobrien  dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
792490075Sobrien			"Length of Source Line Info");
792590075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l1);
792650397Sobrien
792790075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
792890075Sobrien  dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
792990075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, p1);
793050397Sobrien
793196263Sobrien  /* Define the architecture-dependent minimum instruction length (in
793296263Sobrien   bytes).  In this implementation of DWARF, this field is used for
793396263Sobrien   information purposes only.  Since GCC generates assembly language,
793496263Sobrien   we have no a priori knowledge of how many instruction bytes are
793596263Sobrien   generated for each source line, and therefore can use only the
793696263Sobrien   DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
793796263Sobrien   commands.  Accordingly, we fix this as `1', which is "correct
793896263Sobrien   enough" for all architectures, and don't let the target override.  */
793996263Sobrien  dw2_asm_output_data (1, 1,
794090075Sobrien		       "Minimum Instruction Length");
794196263Sobrien
794290075Sobrien  dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
794390075Sobrien		       "Default is_stmt_start flag");
794490075Sobrien  dw2_asm_output_data (1, DWARF_LINE_BASE,
794590075Sobrien		       "Line Base Value (Special Opcodes)");
794690075Sobrien  dw2_asm_output_data (1, DWARF_LINE_RANGE,
794790075Sobrien		       "Line Range Value (Special Opcodes)");
794890075Sobrien  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
794990075Sobrien		       "Special Opcode Base");
795050397Sobrien
795190075Sobrien  for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
795250397Sobrien    {
795350397Sobrien      switch (opc)
795450397Sobrien	{
795550397Sobrien	case DW_LNS_advance_pc:
795650397Sobrien	case DW_LNS_advance_line:
795750397Sobrien	case DW_LNS_set_file:
795850397Sobrien	case DW_LNS_set_column:
795950397Sobrien	case DW_LNS_fixed_advance_pc:
796050397Sobrien	  n_op_args = 1;
796150397Sobrien	  break;
796250397Sobrien	default:
796350397Sobrien	  n_op_args = 0;
796450397Sobrien	  break;
796550397Sobrien	}
796650397Sobrien
796790075Sobrien      dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
796890075Sobrien			   opc, n_op_args);
796950397Sobrien    }
797050397Sobrien
797190075Sobrien  /* Write out the information about the files we use.  */
797290075Sobrien  output_file_names ();
797390075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, p2);
797450397Sobrien
797552284Sobrien  /* We used to set the address register to the first location in the text
797652284Sobrien     section here, but that didn't accomplish anything since we already
797752284Sobrien     have a line note for the opening brace of the first function.  */
797850397Sobrien
797950397Sobrien  /* Generate the line number to PC correspondence table, encoded as
798050397Sobrien     a series of state machine operations.  */
798150397Sobrien  current_file = 1;
798250397Sobrien  current_line = 1;
7983169689Skan
7984169689Skan  if (cfun && in_cold_section_p)
7985169689Skan    strcpy (prev_line_label, cfun->cold_section_label);
7986169689Skan  else
7987169689Skan    strcpy (prev_line_label, text_section_label);
798850397Sobrien  for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
798950397Sobrien    {
799090075Sobrien      dw_line_info_ref line_info = &line_info_table[lt_index];
799150397Sobrien
799290075Sobrien#if 0
799390075Sobrien      /* Disable this optimization for now; GDB wants to see two line notes
799490075Sobrien	 at the beginning of a function so it can find the end of the
799590075Sobrien	 prologue.  */
799690075Sobrien
799752284Sobrien      /* Don't emit anything for redundant notes.  Just updating the
7998132718Skan	 address doesn't accomplish anything, because we already assume
7999132718Skan	 that anything after the last address is this line.  */
800052284Sobrien      if (line_info->dw_line_num == current_line
800152284Sobrien	  && line_info->dw_file_num == current_file)
800252284Sobrien	continue;
800390075Sobrien#endif
800452284Sobrien
800590075Sobrien      /* Emit debug info for the address of the current line.
800690075Sobrien
800790075Sobrien	 Unfortunately, we have little choice here currently, and must always
800890075Sobrien	 use the most general form.  GCC does not know the address delta
800990075Sobrien	 itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
801090075Sobrien	 attributes which will give an upper bound on the address range.  We
801190075Sobrien	 could perhaps use length attributes to determine when it is safe to
801290075Sobrien	 use DW_LNS_fixed_advance_pc.  */
801390075Sobrien
801450397Sobrien      ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
801550397Sobrien      if (0)
801650397Sobrien	{
801750397Sobrien	  /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
801890075Sobrien	  dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
801990075Sobrien			       "DW_LNS_fixed_advance_pc");
802090075Sobrien	  dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
802150397Sobrien	}
802250397Sobrien      else
802350397Sobrien	{
802490075Sobrien	  /* This can handle any delta.  This takes
8025132718Skan	     4+DWARF2_ADDR_SIZE bytes.  */
802690075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
802790075Sobrien	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
802890075Sobrien	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
802990075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
803050397Sobrien	}
803190075Sobrien
803250397Sobrien      strcpy (prev_line_label, line_label);
803350397Sobrien
803450397Sobrien      /* Emit debug info for the source file of the current line, if
803550397Sobrien	 different from the previous line.  */
803650397Sobrien      if (line_info->dw_file_num != current_file)
803750397Sobrien	{
803850397Sobrien	  current_file = line_info->dw_file_num;
803990075Sobrien	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8040169689Skan	  dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
804150397Sobrien	}
804250397Sobrien
804350397Sobrien      /* Emit debug info for the current line number, choosing the encoding
804450397Sobrien	 that uses the least amount of space.  */
804552284Sobrien      if (line_info->dw_line_num != current_line)
804650397Sobrien	{
804752284Sobrien	  line_offset = line_info->dw_line_num - current_line;
804852284Sobrien	  line_delta = line_offset - DWARF_LINE_BASE;
804952284Sobrien	  current_line = line_info->dw_line_num;
805052284Sobrien	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
805190075Sobrien	    /* This can handle deltas from -10 to 234, using the current
805290075Sobrien	       definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
805390075Sobrien	       takes 1 byte.  */
805490075Sobrien	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
805590075Sobrien				 "line %lu", current_line);
805652284Sobrien	  else
805752284Sobrien	    {
805852284Sobrien	      /* This can handle any delta.  This takes at least 4 bytes,
805952284Sobrien		 depending on the value being encoded.  */
806090075Sobrien	      dw2_asm_output_data (1, DW_LNS_advance_line,
806190075Sobrien				   "advance to line %lu", current_line);
806290075Sobrien	      dw2_asm_output_data_sleb128 (line_offset, NULL);
806390075Sobrien	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
806452284Sobrien	    }
806550397Sobrien	}
806650397Sobrien      else
806790075Sobrien	/* We still need to start a new row, so output a copy insn.  */
806890075Sobrien	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
806950397Sobrien    }
807050397Sobrien
807150397Sobrien  /* Emit debug info for the address of the end of the function.  */
807250397Sobrien  if (0)
807350397Sobrien    {
807490075Sobrien      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
807590075Sobrien			   "DW_LNS_fixed_advance_pc");
807690075Sobrien      dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
807750397Sobrien    }
807850397Sobrien  else
807950397Sobrien    {
808090075Sobrien      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
808190075Sobrien      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
808290075Sobrien      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
808390075Sobrien      dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
808450397Sobrien    }
808550397Sobrien
808690075Sobrien  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
808790075Sobrien  dw2_asm_output_data_uleb128 (1, NULL);
808890075Sobrien  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
808950397Sobrien
809050397Sobrien  function = 0;
809150397Sobrien  current_file = 1;
809250397Sobrien  current_line = 1;
809390075Sobrien  for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
809450397Sobrien    {
809590075Sobrien      dw_separate_line_info_ref line_info
809650397Sobrien	= &separate_line_info_table[lt_index];
809750397Sobrien
809890075Sobrien#if 0
809952284Sobrien      /* Don't emit anything for redundant notes.  */
810052284Sobrien      if (line_info->dw_line_num == current_line
810152284Sobrien	  && line_info->dw_file_num == current_file
810252284Sobrien	  && line_info->function == function)
810352284Sobrien	goto cont;
810490075Sobrien#endif
810552284Sobrien
810650397Sobrien      /* Emit debug info for the address of the current line.  If this is
810750397Sobrien	 a new function, or the first line of a function, then we need
810850397Sobrien	 to handle it differently.  */
810950397Sobrien      ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
811050397Sobrien				   lt_index);
811150397Sobrien      if (function != line_info->function)
811250397Sobrien	{
811350397Sobrien	  function = line_info->function;
811450397Sobrien
8115132718Skan	  /* Set the address register to the first line in the function.  */
811690075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
811790075Sobrien	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
811890075Sobrien	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
811990075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
812050397Sobrien	}
812150397Sobrien      else
812250397Sobrien	{
812350397Sobrien	  /* ??? See the DW_LNS_advance_pc comment above.  */
812450397Sobrien	  if (0)
812550397Sobrien	    {
812690075Sobrien	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
812790075Sobrien				   "DW_LNS_fixed_advance_pc");
812890075Sobrien	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
812950397Sobrien	    }
813050397Sobrien	  else
813150397Sobrien	    {
813290075Sobrien	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
813390075Sobrien	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
813490075Sobrien	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
813590075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
813650397Sobrien	    }
813750397Sobrien	}
813890075Sobrien
813950397Sobrien      strcpy (prev_line_label, line_label);
814050397Sobrien
814150397Sobrien      /* Emit debug info for the source file of the current line, if
814250397Sobrien	 different from the previous line.  */
814350397Sobrien      if (line_info->dw_file_num != current_file)
814450397Sobrien	{
814550397Sobrien	  current_file = line_info->dw_file_num;
814690075Sobrien	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8147169689Skan	  dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
814850397Sobrien	}
814950397Sobrien
815050397Sobrien      /* Emit debug info for the current line number, choosing the encoding
815150397Sobrien	 that uses the least amount of space.  */
815250397Sobrien      if (line_info->dw_line_num != current_line)
815350397Sobrien	{
815450397Sobrien	  line_offset = line_info->dw_line_num - current_line;
815550397Sobrien	  line_delta = line_offset - DWARF_LINE_BASE;
815650397Sobrien	  current_line = line_info->dw_line_num;
815750397Sobrien	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
815890075Sobrien	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
815990075Sobrien				 "line %lu", current_line);
816050397Sobrien	  else
816150397Sobrien	    {
816290075Sobrien	      dw2_asm_output_data (1, DW_LNS_advance_line,
816390075Sobrien				   "advance to line %lu", current_line);
816490075Sobrien	      dw2_asm_output_data_sleb128 (line_offset, NULL);
816590075Sobrien	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
816650397Sobrien	    }
816750397Sobrien	}
816852284Sobrien      else
816990075Sobrien	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
817050397Sobrien
817190075Sobrien#if 0
817252284Sobrien    cont:
817390075Sobrien#endif
817450397Sobrien
817590075Sobrien      lt_index++;
817690075Sobrien
817750397Sobrien      /* If we're done with a function, end its sequence.  */
817850397Sobrien      if (lt_index == separate_line_info_table_in_use
817950397Sobrien	  || separate_line_info_table[lt_index].function != function)
818050397Sobrien	{
818150397Sobrien	  current_file = 1;
818250397Sobrien	  current_line = 1;
818350397Sobrien
818450397Sobrien	  /* Emit debug info for the address of the end of the function.  */
818550397Sobrien	  ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
818650397Sobrien	  if (0)
818750397Sobrien	    {
818890075Sobrien	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
818990075Sobrien				   "DW_LNS_fixed_advance_pc");
819090075Sobrien	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
819150397Sobrien	    }
819250397Sobrien	  else
819350397Sobrien	    {
819490075Sobrien	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
819590075Sobrien	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
819690075Sobrien	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
819790075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
819850397Sobrien	    }
819950397Sobrien
820050397Sobrien	  /* Output the marker for the end of this sequence.  */
820190075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
820290075Sobrien	  dw2_asm_output_data_uleb128 (1, NULL);
820390075Sobrien	  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
820450397Sobrien	}
820550397Sobrien    }
820690075Sobrien
820790075Sobrien  /* Output the marker for the end of the line number info.  */
820890075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l2);
820950397Sobrien}
821050397Sobrien
821150397Sobrien/* Given a pointer to a tree node for some base type, return a pointer to
821250397Sobrien   a DIE that describes the given type.
821350397Sobrien
821450397Sobrien   This routine must only be called for GCC type nodes that correspond to
821550397Sobrien   Dwarf base (fundamental) types.  */
821650397Sobrien
821750397Sobrienstatic dw_die_ref
8218132718Skanbase_type_die (tree type)
821950397Sobrien{
822090075Sobrien  dw_die_ref base_type_result;
822190075Sobrien  enum dwarf_type encoding;
822250397Sobrien
822390075Sobrien  if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
822450397Sobrien    return 0;
822550397Sobrien
822650397Sobrien  switch (TREE_CODE (type))
822750397Sobrien    {
822850397Sobrien    case INTEGER_TYPE:
8229169689Skan      if (TYPE_STRING_FLAG (type))
823050397Sobrien	{
8231169689Skan	  if (TYPE_UNSIGNED (type))
8232169689Skan	    encoding = DW_ATE_unsigned_char;
823350397Sobrien	  else
8234169689Skan	    encoding = DW_ATE_signed_char;
823550397Sobrien	}
8236169689Skan      else if (TYPE_UNSIGNED (type))
8237169689Skan	encoding = DW_ATE_unsigned;
823850397Sobrien      else
8239169689Skan	encoding = DW_ATE_signed;
824050397Sobrien      break;
824150397Sobrien
824250397Sobrien    case REAL_TYPE:
8243169689Skan      if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8244169689Skan	encoding = DW_ATE_decimal_float;
8245169689Skan      else
8246169689Skan	encoding = DW_ATE_float;
824750397Sobrien      break;
824850397Sobrien
824990075Sobrien      /* Dwarf2 doesn't know anything about complex ints, so use
825090075Sobrien	 a user defined type for it.  */
825150397Sobrien    case COMPLEX_TYPE:
825290075Sobrien      if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
825390075Sobrien	encoding = DW_ATE_complex_float;
825490075Sobrien      else
825590075Sobrien	encoding = DW_ATE_lo_user;
825650397Sobrien      break;
825750397Sobrien
825850397Sobrien    case BOOLEAN_TYPE:
825950397Sobrien      /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
826050397Sobrien      encoding = DW_ATE_boolean;
826150397Sobrien      break;
826250397Sobrien
826350397Sobrien    default:
826490075Sobrien      /* No other TREE_CODEs are Dwarf fundamental types.  */
8265169689Skan      gcc_unreachable ();
826650397Sobrien    }
826750397Sobrien
826890075Sobrien  base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
826990075Sobrien
8270169689Skan  /* This probably indicates a bug.  */
8271169689Skan  if (! TYPE_NAME (type))
8272169689Skan    add_name_attribute (base_type_result, "__unknown__");
8273169689Skan
827450397Sobrien  add_AT_unsigned (base_type_result, DW_AT_byte_size,
827550397Sobrien		   int_size_in_bytes (type));
827650397Sobrien  add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
827750397Sobrien
827850397Sobrien  return base_type_result;
827950397Sobrien}
828050397Sobrien
828150397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
828250397Sobrien   the Dwarf "root" type for the given input type.  The Dwarf "root" type of
828350397Sobrien   a given type is generally the same as the given type, except that if the
828450397Sobrien   given type is a pointer or reference type, then the root type of the given
828550397Sobrien   type is the root type of the "basis" type for the pointer or reference
828650397Sobrien   type.  (This definition of the "root" type is recursive.) Also, the root
828750397Sobrien   type of a `const' qualified type or a `volatile' qualified type is the
828850397Sobrien   root type of the given type without the qualifiers.  */
828950397Sobrien
829050397Sobrienstatic tree
8291132718Skanroot_type (tree type)
829250397Sobrien{
829350397Sobrien  if (TREE_CODE (type) == ERROR_MARK)
829450397Sobrien    return error_mark_node;
829550397Sobrien
829650397Sobrien  switch (TREE_CODE (type))
829750397Sobrien    {
829850397Sobrien    case ERROR_MARK:
829950397Sobrien      return error_mark_node;
830050397Sobrien
830150397Sobrien    case POINTER_TYPE:
830250397Sobrien    case REFERENCE_TYPE:
830350397Sobrien      return type_main_variant (root_type (TREE_TYPE (type)));
830450397Sobrien
830550397Sobrien    default:
830650397Sobrien      return type_main_variant (type);
830750397Sobrien    }
830850397Sobrien}
830950397Sobrien
8310117395Skan/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
831150397Sobrien   given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
831250397Sobrien
831350397Sobrienstatic inline int
8314132718Skanis_base_type (tree type)
831550397Sobrien{
831650397Sobrien  switch (TREE_CODE (type))
831750397Sobrien    {
831850397Sobrien    case ERROR_MARK:
831950397Sobrien    case VOID_TYPE:
832050397Sobrien    case INTEGER_TYPE:
832150397Sobrien    case REAL_TYPE:
832250397Sobrien    case COMPLEX_TYPE:
832350397Sobrien    case BOOLEAN_TYPE:
832450397Sobrien      return 1;
832550397Sobrien
832650397Sobrien    case ARRAY_TYPE:
832750397Sobrien    case RECORD_TYPE:
832850397Sobrien    case UNION_TYPE:
832950397Sobrien    case QUAL_UNION_TYPE:
833050397Sobrien    case ENUMERAL_TYPE:
833150397Sobrien    case FUNCTION_TYPE:
833250397Sobrien    case METHOD_TYPE:
833350397Sobrien    case POINTER_TYPE:
833450397Sobrien    case REFERENCE_TYPE:
833550397Sobrien    case OFFSET_TYPE:
833650397Sobrien    case LANG_TYPE:
833790075Sobrien    case VECTOR_TYPE:
833850397Sobrien      return 0;
833950397Sobrien
834050397Sobrien    default:
8341169689Skan      gcc_unreachable ();
834250397Sobrien    }
834350397Sobrien
834450397Sobrien  return 0;
834550397Sobrien}
834650397Sobrien
8347132718Skan/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8348132718Skan   node, return the size in bits for the type if it is a constant, or else
8349132718Skan   return the alignment for the type if the type's size is not constant, or
8350132718Skan   else return BITS_PER_WORD if the type actually turns out to be an
8351132718Skan   ERROR_MARK node.  */
8352132718Skan
8353132718Skanstatic inline unsigned HOST_WIDE_INT
8354132718Skansimple_type_size_in_bits (tree type)
8355132718Skan{
8356132718Skan  if (TREE_CODE (type) == ERROR_MARK)
8357132718Skan    return BITS_PER_WORD;
8358132718Skan  else if (TYPE_SIZE (type) == NULL_TREE)
8359132718Skan    return 0;
8360132718Skan  else if (host_integerp (TYPE_SIZE (type), 1))
8361132718Skan    return tree_low_cst (TYPE_SIZE (type), 1);
8362132718Skan  else
8363132718Skan    return TYPE_ALIGN (type);
8364132718Skan}
8365132718Skan
8366132718Skan/* Return true if the debug information for the given type should be
8367132718Skan   emitted as a subrange type.  */
8368132718Skan
8369132718Skanstatic inline bool
8370132718Skanis_subrange_type (tree type)
8371132718Skan{
8372132718Skan  tree subtype = TREE_TYPE (type);
8373132718Skan
8374169689Skan  /* Subrange types are identified by the fact that they are integer
8375169689Skan     types, and that they have a subtype which is either an integer type
8376169689Skan     or an enumeral type.  */
8377169689Skan
8378169689Skan  if (TREE_CODE (type) != INTEGER_TYPE
8379169689Skan      || subtype == NULL_TREE)
8380169689Skan    return false;
8381169689Skan
8382169689Skan  if (TREE_CODE (subtype) != INTEGER_TYPE
8383169689Skan      && TREE_CODE (subtype) != ENUMERAL_TYPE)
8384169689Skan    return false;
8385169689Skan
8386169689Skan  if (TREE_CODE (type) == TREE_CODE (subtype)
8387169689Skan      && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8388169689Skan      && TYPE_MIN_VALUE (type) != NULL
8389169689Skan      && TYPE_MIN_VALUE (subtype) != NULL
8390169689Skan      && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8391169689Skan      && TYPE_MAX_VALUE (type) != NULL
8392169689Skan      && TYPE_MAX_VALUE (subtype) != NULL
8393169689Skan      && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8394132718Skan    {
8395169689Skan      /* The type and its subtype have the same representation.  If in
8396169689Skan         addition the two types also have the same name, then the given
8397169689Skan         type is not a subrange type, but rather a plain base type.  */
8398169689Skan      /* FIXME: brobecker/2004-03-22:
8399169689Skan         Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8400169689Skan         therefore be sufficient to check the TYPE_SIZE node pointers
8401169689Skan         rather than checking the actual size.  Unfortunately, we have
8402169689Skan         found some cases, such as in the Ada "integer" type, where
8403169689Skan         this is not the case.  Until this problem is solved, we need to
8404169689Skan         keep checking the actual size.  */
8405169689Skan      tree type_name = TYPE_NAME (type);
8406169689Skan      tree subtype_name = TYPE_NAME (subtype);
8407169689Skan
8408169689Skan      if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8409169689Skan        type_name = DECL_NAME (type_name);
8410169689Skan
8411169689Skan      if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8412169689Skan        subtype_name = DECL_NAME (subtype_name);
8413169689Skan
8414169689Skan      if (type_name == subtype_name)
8415169689Skan        return false;
8416132718Skan    }
8417169689Skan
8418169689Skan  return true;
8419132718Skan}
8420132718Skan
8421132718Skan/*  Given a pointer to a tree node for a subrange type, return a pointer
8422132718Skan    to a DIE that describes the given type.  */
8423132718Skan
8424132718Skanstatic dw_die_ref
8425132718Skansubrange_type_die (tree type, dw_die_ref context_die)
8426132718Skan{
8427132718Skan  dw_die_ref subrange_die;
8428132718Skan  const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8429132718Skan
8430132718Skan  if (context_die == NULL)
8431132718Skan    context_die = comp_unit_die;
8432132718Skan
8433132718Skan  subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8434132718Skan
8435132718Skan  if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8436132718Skan    {
8437132718Skan      /* The size of the subrange type and its base type do not match,
8438132718Skan         so we need to generate a size attribute for the subrange type.  */
8439132718Skan      add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8440132718Skan    }
8441132718Skan
8442132718Skan  if (TYPE_MIN_VALUE (type) != NULL)
8443132718Skan    add_bound_info (subrange_die, DW_AT_lower_bound,
8444132718Skan                    TYPE_MIN_VALUE (type));
8445132718Skan  if (TYPE_MAX_VALUE (type) != NULL)
8446132718Skan    add_bound_info (subrange_die, DW_AT_upper_bound,
8447132718Skan                    TYPE_MAX_VALUE (type));
8448132718Skan
8449132718Skan  return subrange_die;
8450132718Skan}
8451132718Skan
845250397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
845350397Sobrien   entry that chains various modifiers in front of the given type.  */
845450397Sobrien
845550397Sobrienstatic dw_die_ref
8456132718Skanmodified_type_die (tree type, int is_const_type, int is_volatile_type,
8457132718Skan		   dw_die_ref context_die)
845850397Sobrien{
845990075Sobrien  enum tree_code code = TREE_CODE (type);
8460169689Skan  dw_die_ref mod_type_die;
846190075Sobrien  dw_die_ref sub_die = NULL;
846290075Sobrien  tree item_type = NULL;
8463169689Skan  tree qualified_type;
8464169689Skan  tree name;
846550397Sobrien
8466169689Skan  if (code == ERROR_MARK)
8467169689Skan    return NULL;
8468169689Skan
8469169689Skan  /* See if we already have the appropriately qualified variant of
8470169689Skan     this type.  */
8471169689Skan  qualified_type
8472169689Skan    = get_qualified_type (type,
8473169689Skan			  ((is_const_type ? TYPE_QUAL_CONST : 0)
8474169689Skan			   | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8475169689Skan
8476169689Skan  /* If we do, then we can just use its DIE, if it exists.  */
8477169689Skan  if (qualified_type)
847850397Sobrien    {
8479169689Skan      mod_type_die = lookup_type_die (qualified_type);
848050397Sobrien      if (mod_type_die)
8481169689Skan	return mod_type_die;
8482169689Skan    }
8483169689Skan
8484169689Skan  name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8485169689Skan
8486169689Skan  /* Handle C typedef types.  */
8487169689Skan  if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8488169689Skan    {
8489169689Skan      tree dtype = TREE_TYPE (name);
8490169689Skan
8491169689Skan      if (qualified_type == dtype)
849250397Sobrien	{
8493169689Skan	  /* For a named type, use the typedef.  */
8494169689Skan	  gen_type_die (qualified_type, context_die);
8495169689Skan	  return lookup_type_die (qualified_type);
849650397Sobrien	}
8497169689Skan      else if (is_const_type < TYPE_READONLY (dtype)
8498169689Skan	       || is_volatile_type < TYPE_VOLATILE (dtype)
8499169689Skan	       || (is_const_type <= TYPE_READONLY (dtype)
8500169689Skan		   && is_volatile_type <= TYPE_VOLATILE (dtype)
8501169689Skan		   && DECL_ORIGINAL_TYPE (name) != type))
8502169689Skan	/* cv-unqualified version of named type.  Just use the unnamed
8503169689Skan	   type to which it refers.  */
8504169689Skan	return modified_type_die (DECL_ORIGINAL_TYPE (name),
8505169689Skan				  is_const_type, is_volatile_type,
8506169689Skan				  context_die);
8507169689Skan      /* Else cv-qualified version of named type; fall through.  */
8508169689Skan    }
8509169689Skan
8510169689Skan  if (is_const_type)
8511169689Skan    {
8512169689Skan      mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8513169689Skan      sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8514169689Skan    }
8515169689Skan  else if (is_volatile_type)
8516169689Skan    {
8517169689Skan      mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8518169689Skan      sub_die = modified_type_die (type, 0, 0, context_die);
8519169689Skan    }
8520169689Skan  else if (code == POINTER_TYPE)
8521169689Skan    {
8522169689Skan      mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8523169689Skan      add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8524169689Skan		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
8525169689Skan      item_type = TREE_TYPE (type);
8526169689Skan    }
8527169689Skan  else if (code == REFERENCE_TYPE)
8528169689Skan    {
8529169689Skan      mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8530169689Skan      add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8531169689Skan		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
8532169689Skan      item_type = TREE_TYPE (type);
8533169689Skan    }
8534169689Skan  else if (is_subrange_type (type))
8535169689Skan    {
8536169689Skan      mod_type_die = subrange_type_die (type, context_die);
8537169689Skan      item_type = TREE_TYPE (type);
8538169689Skan    }
8539169689Skan  else if (is_base_type (type))
8540169689Skan    mod_type_die = base_type_die (type);
8541169689Skan  else
8542169689Skan    {
8543169689Skan      gen_type_die (type, context_die);
8544169689Skan
8545169689Skan      /* We have to get the type_main_variant here (and pass that to the
8546169689Skan	 `lookup_type_die' routine) because the ..._TYPE node we have
8547169689Skan	 might simply be a *copy* of some original type node (where the
8548169689Skan	 copy was created to help us keep track of typedef names) and
8549169689Skan	 that copy might have a different TYPE_UID from the original
8550169689Skan	 ..._TYPE node.  */
8551169689Skan      if (TREE_CODE (type) != VECTOR_TYPE)
8552169689Skan	return lookup_type_die (type_main_variant (type));
855350397Sobrien      else
8554169689Skan	/* Vectors have the debugging information in the type,
8555169689Skan	   not the main variant.  */
8556169689Skan	return lookup_type_die (type);
855750397Sobrien    }
8558169689Skan
8559169689Skan  /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8560169689Skan     don't output a DW_TAG_typedef, since there isn't one in the
8561169689Skan     user's program; just attach a DW_AT_name to the type.  */
8562169689Skan  if (name
8563169689Skan      && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8564169689Skan    {
8565169689Skan      if (TREE_CODE (name) == TYPE_DECL)
8566169689Skan	/* Could just call add_name_and_src_coords_attributes here,
8567169689Skan	   but since this is a builtin type it doesn't have any
8568169689Skan	   useful source coordinates anyway.  */
8569169689Skan	name = DECL_NAME (name);
8570169689Skan      add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8571169689Skan    }
8572169689Skan
8573169689Skan  if (qualified_type)
8574169689Skan    equate_type_number_to_die (qualified_type, mod_type_die);
857550397Sobrien
857650397Sobrien  if (item_type)
857750397Sobrien    /* We must do this after the equate_type_number_to_die call, in case
857850397Sobrien       this is a recursive type.  This ensures that the modified_type_die
857950397Sobrien       recursion will terminate even if the type is recursive.  Recursive
858050397Sobrien       types are possible in Ada.  */
858150397Sobrien    sub_die = modified_type_die (item_type,
858250397Sobrien				 TYPE_READONLY (item_type),
858350397Sobrien				 TYPE_VOLATILE (item_type),
858450397Sobrien				 context_die);
858550397Sobrien
858650397Sobrien  if (sub_die != NULL)
858750397Sobrien    add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
858850397Sobrien
858950397Sobrien  return mod_type_die;
859050397Sobrien}
859150397Sobrien
859250397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
859390075Sobrien   an enumerated type.  */
859450397Sobrien
859550397Sobrienstatic inline int
8596132718Skantype_is_enum (tree type)
859750397Sobrien{
859850397Sobrien  return TREE_CODE (type) == ENUMERAL_TYPE;
859950397Sobrien}
860050397Sobrien
8601132718Skan/* Return the DBX register number described by a given RTL node.  */
860250397Sobrien
860390075Sobrienstatic unsigned int
8604132718Skandbx_reg_number (rtx rtl)
860590075Sobrien{
860690075Sobrien  unsigned regno = REGNO (rtl);
860790075Sobrien
8608169689Skan  gcc_assert (regno < FIRST_PSEUDO_REGISTER);
860990075Sobrien
8610169689Skan#ifdef LEAF_REG_REMAP
8611169689Skan  if (current_function_uses_only_leaf_regs)
8612169689Skan    {
8613169689Skan      int leaf_reg = LEAF_REG_REMAP (regno);
8614169689Skan      if (leaf_reg != -1)
8615169689Skan	regno = (unsigned) leaf_reg;
8616169689Skan    }
8617169689Skan#endif
8618169689Skan
861990075Sobrien  return DBX_REGISTER_NUMBER (regno);
862090075Sobrien}
862190075Sobrien
8622169689Skan/* Optionally add a DW_OP_piece term to a location description expression.
8623169689Skan   DW_OP_piece is only added if the location description expression already
8624169689Skan   doesn't end with DW_OP_piece.  */
8625169689Skan
8626169689Skanstatic void
8627169689Skanadd_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8628169689Skan{
8629169689Skan  dw_loc_descr_ref loc;
8630169689Skan
8631169689Skan  if (*list_head != NULL)
8632169689Skan    {
8633169689Skan      /* Find the end of the chain.  */
8634169689Skan      for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8635169689Skan	;
8636169689Skan
8637169689Skan      if (loc->dw_loc_opc != DW_OP_piece)
8638169689Skan	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8639169689Skan    }
8640169689Skan}
8641169689Skan
864290075Sobrien/* Return a location descriptor that designates a machine register or
8643132718Skan   zero if there is none.  */
864490075Sobrien
864550397Sobrienstatic dw_loc_descr_ref
8646132718Skanreg_loc_descriptor (rtx rtl)
864750397Sobrien{
8648132718Skan  rtx regs;
864950397Sobrien
865090075Sobrien  if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
865190075Sobrien    return 0;
865290075Sobrien
8653169689Skan  regs = targetm.dwarf_register_span (rtl);
8654132718Skan
8655169689Skan  if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8656132718Skan    return multiple_reg_loc_descriptor (rtl, regs);
865750397Sobrien  else
8658169689Skan    return one_reg_loc_descriptor (dbx_reg_number (rtl));
8659132718Skan}
866050397Sobrien
8661132718Skan/* Return a location descriptor that designates a machine register for
8662132718Skan   a given hard register number.  */
8663132718Skan
8664132718Skanstatic dw_loc_descr_ref
8665132718Skanone_reg_loc_descriptor (unsigned int regno)
8666132718Skan{
8667132718Skan  if (regno <= 31)
8668132718Skan    return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8669132718Skan  else
8670132718Skan    return new_loc_descr (DW_OP_regx, regno, 0);
8671132718Skan}
8672132718Skan
8673132718Skan/* Given an RTL of a register, return a location descriptor that
8674132718Skan   designates a value that spans more than one register.  */
8675132718Skan
8676132718Skanstatic dw_loc_descr_ref
8677132718Skanmultiple_reg_loc_descriptor (rtx rtl, rtx regs)
8678132718Skan{
8679132718Skan  int nregs, size, i;
8680132718Skan  unsigned reg;
8681132718Skan  dw_loc_descr_ref loc_result = NULL;
8682132718Skan
8683169689Skan  reg = REGNO (rtl);
8684169689Skan#ifdef LEAF_REG_REMAP
8685169689Skan  if (current_function_uses_only_leaf_regs)
8686169689Skan    {
8687169689Skan      int leaf_reg = LEAF_REG_REMAP (reg);
8688169689Skan      if (leaf_reg != -1)
8689169689Skan	reg = (unsigned) leaf_reg;
8690169689Skan    }
8691169689Skan#endif
8692169689Skan  gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8693169689Skan  nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8694132718Skan
8695132718Skan  /* Simple, contiguous registers.  */
8696132718Skan  if (regs == NULL_RTX)
8697132718Skan    {
8698132718Skan      size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8699132718Skan
8700132718Skan      loc_result = NULL;
8701132718Skan      while (nregs--)
8702132718Skan	{
8703132718Skan	  dw_loc_descr_ref t;
8704132718Skan
8705169689Skan	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8706132718Skan	  add_loc_descr (&loc_result, t);
8707169689Skan	  add_loc_descr_op_piece (&loc_result, size);
8708132718Skan	  ++reg;
8709132718Skan	}
8710132718Skan      return loc_result;
8711132718Skan    }
8712132718Skan
8713132718Skan  /* Now onto stupid register sets in non contiguous locations.  */
8714132718Skan
8715169689Skan  gcc_assert (GET_CODE (regs) == PARALLEL);
8716132718Skan
8717132718Skan  size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8718132718Skan  loc_result = NULL;
8719132718Skan
8720132718Skan  for (i = 0; i < XVECLEN (regs, 0); ++i)
8721132718Skan    {
8722132718Skan      dw_loc_descr_ref t;
8723132718Skan
8724132718Skan      t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8725132718Skan      add_loc_descr (&loc_result, t);
8726132718Skan      size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8727169689Skan      add_loc_descr_op_piece (&loc_result, size);
8728132718Skan    }
872950397Sobrien  return loc_result;
873050397Sobrien}
873150397Sobrien
873290075Sobrien/* Return a location descriptor that designates a constant.  */
873390075Sobrien
873490075Sobrienstatic dw_loc_descr_ref
8735132718Skanint_loc_descriptor (HOST_WIDE_INT i)
873690075Sobrien{
873790075Sobrien  enum dwarf_location_atom op;
873890075Sobrien
873990075Sobrien  /* Pick the smallest representation of a constant, rather than just
874090075Sobrien     defaulting to the LEB encoding.  */
874190075Sobrien  if (i >= 0)
874290075Sobrien    {
874390075Sobrien      if (i <= 31)
874490075Sobrien	op = DW_OP_lit0 + i;
874590075Sobrien      else if (i <= 0xff)
874690075Sobrien	op = DW_OP_const1u;
874790075Sobrien      else if (i <= 0xffff)
874890075Sobrien	op = DW_OP_const2u;
874990075Sobrien      else if (HOST_BITS_PER_WIDE_INT == 32
875090075Sobrien	       || i <= 0xffffffff)
875190075Sobrien	op = DW_OP_const4u;
875290075Sobrien      else
875390075Sobrien	op = DW_OP_constu;
875490075Sobrien    }
875590075Sobrien  else
875690075Sobrien    {
875790075Sobrien      if (i >= -0x80)
875890075Sobrien	op = DW_OP_const1s;
875990075Sobrien      else if (i >= -0x8000)
876090075Sobrien	op = DW_OP_const2s;
876190075Sobrien      else if (HOST_BITS_PER_WIDE_INT == 32
876290075Sobrien	       || i >= -0x80000000)
876390075Sobrien	op = DW_OP_const4s;
876490075Sobrien      else
876590075Sobrien	op = DW_OP_consts;
876690075Sobrien    }
876790075Sobrien
876890075Sobrien  return new_loc_descr (op, i, 0);
876990075Sobrien}
877090075Sobrien
877150397Sobrien/* Return a location descriptor that designates a base+offset location.  */
877250397Sobrien
877350397Sobrienstatic dw_loc_descr_ref
8774169689Skanbased_loc_descr (rtx reg, HOST_WIDE_INT offset)
877550397Sobrien{
8776169689Skan  unsigned int regno;
877750397Sobrien
8778169689Skan  /* We only use "frame base" when we're sure we're talking about the
8779169689Skan     post-prologue local stack frame.  We do this by *not* running
8780169689Skan     register elimination until this point, and recognizing the special
8781169689Skan     argument pointer and soft frame pointer rtx's.  */
8782169689Skan  if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8783169689Skan    {
8784169689Skan      rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8785169689Skan
8786169689Skan      if (elim != reg)
8787169689Skan	{
8788169689Skan	  if (GET_CODE (elim) == PLUS)
8789169689Skan	    {
8790169689Skan	      offset += INTVAL (XEXP (elim, 1));
8791169689Skan	      elim = XEXP (elim, 0);
8792169689Skan	    }
8793169689Skan	  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8794169689Skan		      : stack_pointer_rtx));
8795169689Skan          offset += frame_pointer_fb_offset;
8796169689Skan
8797169689Skan          return new_loc_descr (DW_OP_fbreg, offset, 0);
8798169689Skan	}
8799169689Skan    }
8800169689Skan
8801169689Skan  regno = dbx_reg_number (reg);
8802169689Skan  if (regno <= 31)
8803169689Skan    return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
880450397Sobrien  else
8805169689Skan    return new_loc_descr (DW_OP_bregx, regno, offset);
880650397Sobrien}
880750397Sobrien
880850397Sobrien/* Return true if this RTL expression describes a base+offset calculation.  */
880950397Sobrien
881050397Sobrienstatic inline int
8811132718Skanis_based_loc (rtx rtl)
881250397Sobrien{
881390075Sobrien  return (GET_CODE (rtl) == PLUS
8814169689Skan	  && ((REG_P (XEXP (rtl, 0))
881590075Sobrien	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
881690075Sobrien	       && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
881750397Sobrien}
881850397Sobrien
881950397Sobrien/* The following routine converts the RTL for a variable or parameter
882050397Sobrien   (resident in memory) into an equivalent Dwarf representation of a
882150397Sobrien   mechanism for getting the address of that same variable onto the top of a
882250397Sobrien   hypothetical "address evaluation" stack.
882350397Sobrien
882450397Sobrien   When creating memory location descriptors, we are effectively transforming
882550397Sobrien   the RTL for a memory-resident object into its Dwarf postfix expression
882650397Sobrien   equivalent.  This routine recursively descends an RTL tree, turning
882790075Sobrien   it into Dwarf postfix code as it goes.
882850397Sobrien
882990075Sobrien   MODE is the mode of the memory reference, needed to handle some
883090075Sobrien   autoincrement addressing modes.
883190075Sobrien
8832169689Skan   CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8833169689Skan   location list for RTL.
8834169689Skan
883590075Sobrien   Return 0 if we can't represent the location.  */
883690075Sobrien
883750397Sobrienstatic dw_loc_descr_ref
8838132718Skanmem_loc_descriptor (rtx rtl, enum machine_mode mode)
883950397Sobrien{
884050397Sobrien  dw_loc_descr_ref mem_loc_result = NULL;
8841169689Skan  enum dwarf_location_atom op;
884290075Sobrien
884390075Sobrien  /* Note that for a dynamically sized array, the location we will generate a
884450397Sobrien     description of here will be the lowest numbered location which is
884550397Sobrien     actually within the array.  That's *not* necessarily the same as the
884650397Sobrien     zeroth element of the array.  */
884750397Sobrien
8848169689Skan  rtl = targetm.delegitimize_address (rtl);
884990075Sobrien
885050397Sobrien  switch (GET_CODE (rtl))
885150397Sobrien    {
885290075Sobrien    case POST_INC:
885390075Sobrien    case POST_DEC:
885490075Sobrien    case POST_MODIFY:
885590075Sobrien      /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
885690075Sobrien	 just fall into the SUBREG code.  */
885790075Sobrien
885890075Sobrien      /* ... fall through ...  */
885990075Sobrien
886050397Sobrien    case SUBREG:
886150397Sobrien      /* The case of a subreg may arise when we have a local (register)
8862132718Skan	 variable or a formal (register) parameter which doesn't quite fill
8863132718Skan	 up an entire register.  For now, just assume that it is
8864132718Skan	 legitimate to make the Dwarf info refer to the whole register which
8865132718Skan	 contains the given subreg.  */
8866169689Skan      rtl = XEXP (rtl, 0);
886750397Sobrien
886890075Sobrien      /* ... fall through ...  */
886950397Sobrien
887050397Sobrien    case REG:
887150397Sobrien      /* Whenever a register number forms a part of the description of the
8872132718Skan	 method for calculating the (dynamic) address of a memory resident
8873132718Skan	 object, DWARF rules require the register number be referred to as
8874132718Skan	 a "base register".  This distinction is not based in any way upon
8875132718Skan	 what category of register the hardware believes the given register
8876132718Skan	 belongs to.  This is strictly DWARF terminology we're dealing with
8877132718Skan	 here. Note that in cases where the location of a memory-resident
8878132718Skan	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8879132718Skan	 OP_CONST (0)) the actual DWARF location descriptor that we generate
8880132718Skan	 may just be OP_BASEREG (basereg).  This may look deceptively like
8881132718Skan	 the object in question was allocated to a register (rather than in
8882132718Skan	 memory) so DWARF consumers need to be aware of the subtle
8883132718Skan	 distinction between OP_REG and OP_BASEREG.  */
888490075Sobrien      if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8885169689Skan	mem_loc_result = based_loc_descr (rtl, 0);
888650397Sobrien      break;
888750397Sobrien
888850397Sobrien    case MEM:
888990075Sobrien      mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
889090075Sobrien      if (mem_loc_result != 0)
889190075Sobrien	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
889250397Sobrien      break;
889350397Sobrien
8894117395Skan    case LO_SUM:
8895117395Skan	 rtl = XEXP (rtl, 1);
8896117395Skan
8897117395Skan      /* ... fall through ...  */
8898117395Skan
889990075Sobrien    case LABEL_REF:
890090075Sobrien      /* Some ports can transform a symbol ref into a label ref, because
8901132718Skan	 the symbol ref is too far away and has to be dumped into a constant
8902132718Skan	 pool.  */
890350397Sobrien    case CONST:
890450397Sobrien    case SYMBOL_REF:
890590075Sobrien      /* Alternatively, the symbol in the constant pool might be referenced
890690075Sobrien	 by a different symbol.  */
890790075Sobrien      if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
890890075Sobrien	{
890996263Sobrien	  bool marked;
891096263Sobrien	  rtx tmp = get_pool_constant_mark (rtl, &marked);
891190075Sobrien
891290075Sobrien	  if (GET_CODE (tmp) == SYMBOL_REF)
891396263Sobrien	    {
891496263Sobrien	      rtl = tmp;
891596263Sobrien	      if (CONSTANT_POOL_ADDRESS_P (tmp))
891696263Sobrien		get_pool_constant_mark (tmp, &marked);
891796263Sobrien	      else
891896263Sobrien		marked = true;
891996263Sobrien	    }
892096263Sobrien
892196263Sobrien	  /* If all references to this pool constant were optimized away,
892296263Sobrien	     it was not output and thus we can't represent it.
892396263Sobrien	     FIXME: might try to use DW_OP_const_value here, though
892496263Sobrien	     DW_OP_piece complicates it.  */
892596263Sobrien	  if (!marked)
892696263Sobrien	    return 0;
892790075Sobrien	}
892890075Sobrien
892950397Sobrien      mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
893050397Sobrien      mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
893190075Sobrien      mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8932169689Skan      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
893350397Sobrien      break;
893450397Sobrien
893590075Sobrien    case PRE_MODIFY:
893690075Sobrien      /* Extract the PLUS expression nested inside and fall into
8937132718Skan	 PLUS code below.  */
893890075Sobrien      rtl = XEXP (rtl, 1);
893990075Sobrien      goto plus;
894090075Sobrien
894190075Sobrien    case PRE_INC:
894290075Sobrien    case PRE_DEC:
894390075Sobrien      /* Turn these into a PLUS expression and fall into the PLUS code
894490075Sobrien	 below.  */
894590075Sobrien      rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
894690075Sobrien			  GEN_INT (GET_CODE (rtl) == PRE_INC
894790075Sobrien				   ? GET_MODE_UNIT_SIZE (mode)
894890075Sobrien				   : -GET_MODE_UNIT_SIZE (mode)));
894990075Sobrien
895090075Sobrien      /* ... fall through ...  */
895190075Sobrien
895250397Sobrien    case PLUS:
895390075Sobrien    plus:
895450397Sobrien      if (is_based_loc (rtl))
8955169689Skan	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
895650397Sobrien					  INTVAL (XEXP (rtl, 1)));
895750397Sobrien      else
895850397Sobrien	{
895990075Sobrien	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
896090075Sobrien	  if (mem_loc_result == 0)
896190075Sobrien	    break;
896290075Sobrien
896390075Sobrien	  if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
896490075Sobrien	      && INTVAL (XEXP (rtl, 1)) >= 0)
896590075Sobrien	    add_loc_descr (&mem_loc_result,
896690075Sobrien			   new_loc_descr (DW_OP_plus_uconst,
896790075Sobrien					  INTVAL (XEXP (rtl, 1)), 0));
896890075Sobrien	  else
896990075Sobrien	    {
897090075Sobrien	      add_loc_descr (&mem_loc_result,
897190075Sobrien			     mem_loc_descriptor (XEXP (rtl, 1), mode));
897290075Sobrien	      add_loc_descr (&mem_loc_result,
897390075Sobrien			     new_loc_descr (DW_OP_plus, 0, 0));
897490075Sobrien	    }
897550397Sobrien	}
897650397Sobrien      break;
897750397Sobrien
8978169689Skan    /* If a pseudo-reg is optimized away, it is possible for it to
8979169689Skan       be replaced with a MEM containing a multiply or shift.  */
898050397Sobrien    case MULT:
8981169689Skan      op = DW_OP_mul;
8982169689Skan      goto do_binop;
8983169689Skan
8984169689Skan    case ASHIFT:
8985169689Skan      op = DW_OP_shl;
8986169689Skan      goto do_binop;
8987169689Skan
8988169689Skan    case ASHIFTRT:
8989169689Skan      op = DW_OP_shra;
8990169689Skan      goto do_binop;
8991169689Skan
8992169689Skan    case LSHIFTRT:
8993169689Skan      op = DW_OP_shr;
8994169689Skan      goto do_binop;
8995169689Skan
8996169689Skan    do_binop:
899790075Sobrien      {
899890075Sobrien	dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
899990075Sobrien	dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
900050397Sobrien
900190075Sobrien	if (op0 == 0 || op1 == 0)
900290075Sobrien	  break;
900390075Sobrien
900490075Sobrien	mem_loc_result = op0;
900590075Sobrien	add_loc_descr (&mem_loc_result, op1);
9006169689Skan	add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
900790075Sobrien	break;
900890075Sobrien      }
900990075Sobrien
901050397Sobrien    case CONST_INT:
901190075Sobrien      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
901250397Sobrien      break;
901350397Sobrien
901450397Sobrien    default:
9015169689Skan      gcc_unreachable ();
901650397Sobrien    }
901750397Sobrien
901850397Sobrien  return mem_loc_result;
901950397Sobrien}
902050397Sobrien
902150397Sobrien/* Return a descriptor that describes the concatenation of two locations.
902250397Sobrien   This is typically a complex variable.  */
902350397Sobrien
902450397Sobrienstatic dw_loc_descr_ref
9025132718Skanconcat_loc_descriptor (rtx x0, rtx x1)
902650397Sobrien{
902750397Sobrien  dw_loc_descr_ref cc_loc_result = NULL;
902890075Sobrien  dw_loc_descr_ref x0_ref = loc_descriptor (x0);
902990075Sobrien  dw_loc_descr_ref x1_ref = loc_descriptor (x1);
903050397Sobrien
903190075Sobrien  if (x0_ref == 0 || x1_ref == 0)
903290075Sobrien    return 0;
903390075Sobrien
903490075Sobrien  cc_loc_result = x0_ref;
9035169689Skan  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
903650397Sobrien
903790075Sobrien  add_loc_descr (&cc_loc_result, x1_ref);
9038169689Skan  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
903950397Sobrien
904050397Sobrien  return cc_loc_result;
904150397Sobrien}
904250397Sobrien
904350397Sobrien/* Output a proper Dwarf location descriptor for a variable or parameter
904450397Sobrien   which is either allocated in a register or in a memory location.  For a
904550397Sobrien   register, we just generate an OP_REG and the register number.  For a
904650397Sobrien   memory location we provide a Dwarf postfix expression describing how to
904790075Sobrien   generate the (dynamic) address of the object onto the address stack.
904850397Sobrien
904990075Sobrien   If we don't know how to describe it, return 0.  */
905090075Sobrien
905150397Sobrienstatic dw_loc_descr_ref
9052132718Skanloc_descriptor (rtx rtl)
905350397Sobrien{
905450397Sobrien  dw_loc_descr_ref loc_result = NULL;
905590075Sobrien
905650397Sobrien  switch (GET_CODE (rtl))
905750397Sobrien    {
905850397Sobrien    case SUBREG:
905950397Sobrien      /* The case of a subreg may arise when we have a local (register)
9060132718Skan	 variable or a formal (register) parameter which doesn't quite fill
9061132718Skan	 up an entire register.  For now, just assume that it is
9062132718Skan	 legitimate to make the Dwarf info refer to the whole register which
9063132718Skan	 contains the given subreg.  */
906490075Sobrien      rtl = SUBREG_REG (rtl);
906550397Sobrien
906690075Sobrien      /* ... fall through ...  */
906750397Sobrien
906850397Sobrien    case REG:
906950397Sobrien      loc_result = reg_loc_descriptor (rtl);
907050397Sobrien      break;
907150397Sobrien
907250397Sobrien    case MEM:
907390075Sobrien      loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
907450397Sobrien      break;
907550397Sobrien
907650397Sobrien    case CONCAT:
907750397Sobrien      loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
907850397Sobrien      break;
907950397Sobrien
9080169689Skan    case VAR_LOCATION:
9081169689Skan      /* Single part.  */
9082169689Skan      if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9083169689Skan	{
9084169689Skan	  loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9085169689Skan	  break;
9086169689Skan	}
9087169689Skan
9088169689Skan      rtl = XEXP (rtl, 1);
9089169689Skan      /* FALLTHRU */
9090169689Skan
9091169689Skan    case PARALLEL:
9092169689Skan      {
9093169689Skan	rtvec par_elems = XVEC (rtl, 0);
9094169689Skan	int num_elem = GET_NUM_ELEM (par_elems);
9095169689Skan	enum machine_mode mode;
9096169689Skan	int i;
9097169689Skan
9098169689Skan	/* Create the first one, so we have something to add to.  */
9099169689Skan	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9100169689Skan	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9101169689Skan	add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9102169689Skan	for (i = 1; i < num_elem; i++)
9103169689Skan	  {
9104169689Skan	    dw_loc_descr_ref temp;
9105169689Skan
9106169689Skan	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9107169689Skan	    add_loc_descr (&loc_result, temp);
9108169689Skan	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9109169689Skan	    add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9110169689Skan	  }
9111169689Skan      }
9112169689Skan      break;
9113169689Skan
911450397Sobrien    default:
9115169689Skan      gcc_unreachable ();
911650397Sobrien    }
911750397Sobrien
911850397Sobrien  return loc_result;
911950397Sobrien}
912050397Sobrien
912190075Sobrien/* Similar, but generate the descriptor from trees instead of rtl.  This comes
9122169689Skan   up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9123169689Skan   a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9124169689Skan   top-level invocation, and we require the address of LOC; is 0 if we require
9125169689Skan   the value of LOC.  */
912690075Sobrien
912790075Sobrienstatic dw_loc_descr_ref
9128169689Skanloc_descriptor_from_tree_1 (tree loc, int want_address)
912990075Sobrien{
913090075Sobrien  dw_loc_descr_ref ret, ret1;
9131169689Skan  int have_address = 0;
913290075Sobrien  enum dwarf_location_atom op;
913390075Sobrien
913490075Sobrien  /* ??? Most of the time we do not take proper care for sign/zero
913590075Sobrien     extending the values properly.  Hopefully this won't be a real
913690075Sobrien     problem...  */
913790075Sobrien
913890075Sobrien  switch (TREE_CODE (loc))
913990075Sobrien    {
914090075Sobrien    case ERROR_MARK:
914190075Sobrien      return 0;
914290075Sobrien
914390075Sobrien    case PLACEHOLDER_EXPR:
914490075Sobrien      /* This case involves extracting fields from an object to determine the
914590075Sobrien	 position of other fields.  We don't try to encode this here.  The
914690075Sobrien	 only user of this is Ada, which encodes the needed information using
914790075Sobrien	 the names of types.  */
914890075Sobrien      return 0;
914990075Sobrien
915090075Sobrien    case CALL_EXPR:
915190075Sobrien      return 0;
915290075Sobrien
9153132718Skan    case PREINCREMENT_EXPR:
9154132718Skan    case PREDECREMENT_EXPR:
9155132718Skan    case POSTINCREMENT_EXPR:
9156132718Skan    case POSTDECREMENT_EXPR:
9157132718Skan      /* There are no opcodes for these operations.  */
9158132718Skan      return 0;
9159132718Skan
916090075Sobrien    case ADDR_EXPR:
9161169689Skan      /* If we already want an address, there's nothing we can do.  */
9162169689Skan      if (want_address)
9163169689Skan	return 0;
916490075Sobrien
9165169689Skan      /* Otherwise, process the argument and look for the address.  */
9166169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
916790075Sobrien
916890075Sobrien    case VAR_DECL:
9169169689Skan      if (DECL_THREAD_LOCAL_P (loc))
9170117395Skan	{
9171117395Skan	  rtx rtl;
9172117395Skan
9173117395Skan	  /* If this is not defined, we have no way to emit the data.  */
9174169689Skan	  if (!targetm.asm_out.output_dwarf_dtprel)
9175169689Skan	    return 0;
9176117395Skan
9177117395Skan	  /* The way DW_OP_GNU_push_tls_address is specified, we can only
9178117395Skan	     look up addresses of objects in the current module.  */
9179117395Skan	  if (DECL_EXTERNAL (loc))
9180117395Skan	    return 0;
9181117395Skan
9182117395Skan	  rtl = rtl_for_decl_location (loc);
9183117395Skan	  if (rtl == NULL_RTX)
9184117395Skan	    return 0;
9185117395Skan
9186169689Skan	  if (!MEM_P (rtl))
9187117395Skan	    return 0;
9188117395Skan	  rtl = XEXP (rtl, 0);
9189117395Skan	  if (! CONSTANT_P (rtl))
9190117395Skan	    return 0;
9191117395Skan
9192117395Skan	  ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9193117395Skan	  ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9194117395Skan	  ret->dw_loc_oprnd1.v.val_addr = rtl;
9195117395Skan
9196117395Skan	  ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9197117395Skan	  add_loc_descr (&ret, ret1);
9198117395Skan
9199169689Skan	  have_address = 1;
9200117395Skan	  break;
9201117395Skan	}
9202169689Skan      /* FALLTHRU */
9203117395Skan
920490075Sobrien    case PARM_DECL:
9205169689Skan      if (DECL_HAS_VALUE_EXPR_P (loc))
9206169689Skan	return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9207169689Skan					   want_address);
9208169689Skan      /* FALLTHRU */
9209169689Skan
9210169689Skan    case RESULT_DECL:
9211169689Skan    case FUNCTION_DECL:
921290075Sobrien      {
921390075Sobrien	rtx rtl = rtl_for_decl_location (loc);
921490075Sobrien
921590075Sobrien	if (rtl == NULL_RTX)
921690075Sobrien	  return 0;
9217169689Skan        else if (GET_CODE (rtl) == CONST_INT)
9218169689Skan	  {
9219169689Skan	    HOST_WIDE_INT val = INTVAL (rtl);
9220169689Skan	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9221169689Skan	      val &= GET_MODE_MASK (DECL_MODE (loc));
9222169689Skan	    ret = int_loc_descriptor (val);
9223169689Skan	  }
9224169689Skan	else if (GET_CODE (rtl) == CONST_STRING)
9225169689Skan	  return 0;
922690075Sobrien	else if (CONSTANT_P (rtl))
922790075Sobrien	  {
922890075Sobrien	    ret = new_loc_descr (DW_OP_addr, 0, 0);
922990075Sobrien	    ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
923090075Sobrien	    ret->dw_loc_oprnd1.v.val_addr = rtl;
923190075Sobrien	  }
923290075Sobrien	else
923390075Sobrien	  {
9234169689Skan	    enum machine_mode mode;
923590075Sobrien
9236169689Skan	    /* Certain constructs can only be represented at top-level.  */
9237169689Skan	    if (want_address == 2)
9238169689Skan	      return loc_descriptor (rtl);
9239169689Skan
9240169689Skan	    mode = GET_MODE (rtl);
9241169689Skan	    if (MEM_P (rtl))
924290075Sobrien	      {
924390075Sobrien		rtl = XEXP (rtl, 0);
9244169689Skan		have_address = 1;
924590075Sobrien	      }
924690075Sobrien	    ret = mem_loc_descriptor (rtl, mode);
924790075Sobrien	  }
924890075Sobrien      }
924990075Sobrien      break;
925090075Sobrien
925190075Sobrien    case INDIRECT_REF:
9252169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9253169689Skan      have_address = 1;
925490075Sobrien      break;
925590075Sobrien
925690075Sobrien    case COMPOUND_EXPR:
9257169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
925890075Sobrien
925990075Sobrien    case NOP_EXPR:
926090075Sobrien    case CONVERT_EXPR:
926190075Sobrien    case NON_LVALUE_EXPR:
926290075Sobrien    case VIEW_CONVERT_EXPR:
926390075Sobrien    case SAVE_EXPR:
9264132718Skan    case MODIFY_EXPR:
9265169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
926690075Sobrien
926790075Sobrien    case COMPONENT_REF:
926890075Sobrien    case BIT_FIELD_REF:
926990075Sobrien    case ARRAY_REF:
927090075Sobrien    case ARRAY_RANGE_REF:
927190075Sobrien      {
927290075Sobrien	tree obj, offset;
927390075Sobrien	HOST_WIDE_INT bitsize, bitpos, bytepos;
927490075Sobrien	enum machine_mode mode;
927590075Sobrien	int volatilep;
9276169689Skan	int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
927790075Sobrien
927890075Sobrien	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9279169689Skan				   &unsignedp, &volatilep, false);
928090075Sobrien
928190075Sobrien	if (obj == loc)
928290075Sobrien	  return 0;
928390075Sobrien
9284169689Skan	ret = loc_descriptor_from_tree_1 (obj, 1);
928590075Sobrien	if (ret == 0
928690075Sobrien	    || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
928790075Sobrien	  return 0;
928890075Sobrien
928990075Sobrien	if (offset != NULL_TREE)
929090075Sobrien	  {
929190075Sobrien	    /* Variable offset.  */
9292169689Skan	    add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
929390075Sobrien	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
929490075Sobrien	  }
929590075Sobrien
929690075Sobrien	bytepos = bitpos / BITS_PER_UNIT;
929790075Sobrien	if (bytepos > 0)
929890075Sobrien	  add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
929990075Sobrien	else if (bytepos < 0)
930090075Sobrien	  {
930190075Sobrien	    add_loc_descr (&ret, int_loc_descriptor (bytepos));
930290075Sobrien	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
930390075Sobrien	  }
9304169689Skan
9305169689Skan	have_address = 1;
930690075Sobrien	break;
930790075Sobrien      }
930890075Sobrien
930990075Sobrien    case INTEGER_CST:
931090075Sobrien      if (host_integerp (loc, 0))
931190075Sobrien	ret = int_loc_descriptor (tree_low_cst (loc, 0));
931290075Sobrien      else
931390075Sobrien	return 0;
931490075Sobrien      break;
931590075Sobrien
9316132718Skan    case CONSTRUCTOR:
9317132718Skan      {
9318132718Skan	/* Get an RTL for this, if something has been emitted.  */
9319132718Skan	rtx rtl = lookup_constant_def (loc);
9320132718Skan	enum machine_mode mode;
9321132718Skan
9322169689Skan	if (!rtl || !MEM_P (rtl))
9323132718Skan	  return 0;
9324132718Skan	mode = GET_MODE (rtl);
9325132718Skan	rtl = XEXP (rtl, 0);
9326132718Skan	ret = mem_loc_descriptor (rtl, mode);
9327169689Skan	have_address = 1;
9328132718Skan	break;
9329132718Skan      }
9330132718Skan
9331117395Skan    case TRUTH_AND_EXPR:
933290075Sobrien    case TRUTH_ANDIF_EXPR:
933390075Sobrien    case BIT_AND_EXPR:
933490075Sobrien      op = DW_OP_and;
933590075Sobrien      goto do_binop;
933690075Sobrien
933790075Sobrien    case TRUTH_XOR_EXPR:
933890075Sobrien    case BIT_XOR_EXPR:
933990075Sobrien      op = DW_OP_xor;
934090075Sobrien      goto do_binop;
934190075Sobrien
934290075Sobrien    case TRUTH_OR_EXPR:
934390075Sobrien    case TRUTH_ORIF_EXPR:
934490075Sobrien    case BIT_IOR_EXPR:
934590075Sobrien      op = DW_OP_or;
934690075Sobrien      goto do_binop;
934790075Sobrien
9348132718Skan    case FLOOR_DIV_EXPR:
9349132718Skan    case CEIL_DIV_EXPR:
9350132718Skan    case ROUND_DIV_EXPR:
935190075Sobrien    case TRUNC_DIV_EXPR:
935290075Sobrien      op = DW_OP_div;
935390075Sobrien      goto do_binop;
935490075Sobrien
935590075Sobrien    case MINUS_EXPR:
935690075Sobrien      op = DW_OP_minus;
935790075Sobrien      goto do_binop;
935890075Sobrien
9359132718Skan    case FLOOR_MOD_EXPR:
9360132718Skan    case CEIL_MOD_EXPR:
9361132718Skan    case ROUND_MOD_EXPR:
936290075Sobrien    case TRUNC_MOD_EXPR:
936390075Sobrien      op = DW_OP_mod;
936490075Sobrien      goto do_binop;
936590075Sobrien
936690075Sobrien    case MULT_EXPR:
936790075Sobrien      op = DW_OP_mul;
936890075Sobrien      goto do_binop;
936990075Sobrien
937090075Sobrien    case LSHIFT_EXPR:
937190075Sobrien      op = DW_OP_shl;
937290075Sobrien      goto do_binop;
937390075Sobrien
937490075Sobrien    case RSHIFT_EXPR:
9375169689Skan      op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
937690075Sobrien      goto do_binop;
937790075Sobrien
937890075Sobrien    case PLUS_EXPR:
937990075Sobrien      if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
938090075Sobrien	  && host_integerp (TREE_OPERAND (loc, 1), 0))
938190075Sobrien	{
9382169689Skan	  ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
938390075Sobrien	  if (ret == 0)
938490075Sobrien	    return 0;
938590075Sobrien
938690075Sobrien	  add_loc_descr (&ret,
938790075Sobrien			 new_loc_descr (DW_OP_plus_uconst,
938890075Sobrien					tree_low_cst (TREE_OPERAND (loc, 1),
938990075Sobrien						      0),
939090075Sobrien					0));
939190075Sobrien	  break;
939290075Sobrien	}
939390075Sobrien
939490075Sobrien      op = DW_OP_plus;
939590075Sobrien      goto do_binop;
939690075Sobrien
939790075Sobrien    case LE_EXPR:
9398169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
939990075Sobrien	return 0;
940090075Sobrien
940190075Sobrien      op = DW_OP_le;
940290075Sobrien      goto do_binop;
940390075Sobrien
940490075Sobrien    case GE_EXPR:
9405169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
940690075Sobrien	return 0;
940790075Sobrien
940890075Sobrien      op = DW_OP_ge;
940990075Sobrien      goto do_binop;
941090075Sobrien
941190075Sobrien    case LT_EXPR:
9412169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
941390075Sobrien	return 0;
941490075Sobrien
941590075Sobrien      op = DW_OP_lt;
941690075Sobrien      goto do_binop;
941790075Sobrien
941890075Sobrien    case GT_EXPR:
9419169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
942090075Sobrien	return 0;
942190075Sobrien
942290075Sobrien      op = DW_OP_gt;
942390075Sobrien      goto do_binop;
942490075Sobrien
942590075Sobrien    case EQ_EXPR:
942690075Sobrien      op = DW_OP_eq;
942790075Sobrien      goto do_binop;
942890075Sobrien
942990075Sobrien    case NE_EXPR:
943090075Sobrien      op = DW_OP_ne;
943190075Sobrien      goto do_binop;
943290075Sobrien
943390075Sobrien    do_binop:
9434169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9435169689Skan      ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
943690075Sobrien      if (ret == 0 || ret1 == 0)
943790075Sobrien	return 0;
943890075Sobrien
943990075Sobrien      add_loc_descr (&ret, ret1);
944090075Sobrien      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
944190075Sobrien      break;
944290075Sobrien
944390075Sobrien    case TRUTH_NOT_EXPR:
944490075Sobrien    case BIT_NOT_EXPR:
944590075Sobrien      op = DW_OP_not;
944690075Sobrien      goto do_unop;
944790075Sobrien
944890075Sobrien    case ABS_EXPR:
944990075Sobrien      op = DW_OP_abs;
945090075Sobrien      goto do_unop;
945190075Sobrien
945290075Sobrien    case NEGATE_EXPR:
945390075Sobrien      op = DW_OP_neg;
945490075Sobrien      goto do_unop;
945590075Sobrien
945690075Sobrien    do_unop:
9457169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
945890075Sobrien      if (ret == 0)
945990075Sobrien	return 0;
946090075Sobrien
946190075Sobrien      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
946290075Sobrien      break;
946390075Sobrien
9464169689Skan    case MIN_EXPR:
946590075Sobrien    case MAX_EXPR:
9466169689Skan      {
9467169689Skan        const enum tree_code code =
9468169689Skan          TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
946990075Sobrien
9470169689Skan        loc = build3 (COND_EXPR, TREE_TYPE (loc),
9471169689Skan		      build2 (code, integer_type_node,
9472169689Skan			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9473169689Skan                      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9474169689Skan      }
9475169689Skan
947690075Sobrien      /* ... fall through ...  */
947790075Sobrien
947890075Sobrien    case COND_EXPR:
947990075Sobrien      {
948090075Sobrien	dw_loc_descr_ref lhs
9481169689Skan	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
948290075Sobrien	dw_loc_descr_ref rhs
9483169689Skan	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
948490075Sobrien	dw_loc_descr_ref bra_node, jump_node, tmp;
948590075Sobrien
9486169689Skan	ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
948790075Sobrien	if (ret == 0 || lhs == 0 || rhs == 0)
948890075Sobrien	  return 0;
948990075Sobrien
949090075Sobrien	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
949190075Sobrien	add_loc_descr (&ret, bra_node);
949290075Sobrien
949390075Sobrien	add_loc_descr (&ret, rhs);
949490075Sobrien	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
949590075Sobrien	add_loc_descr (&ret, jump_node);
949690075Sobrien
949790075Sobrien	add_loc_descr (&ret, lhs);
949890075Sobrien	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
949990075Sobrien	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
950090075Sobrien
950190075Sobrien	/* ??? Need a node to point the skip at.  Use a nop.  */
950290075Sobrien	tmp = new_loc_descr (DW_OP_nop, 0, 0);
950390075Sobrien	add_loc_descr (&ret, tmp);
950490075Sobrien	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
950590075Sobrien	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
950690075Sobrien      }
950790075Sobrien      break;
950890075Sobrien
9509146895Skan    case FIX_TRUNC_EXPR:
9510146895Skan    case FIX_CEIL_EXPR:
9511146895Skan    case FIX_FLOOR_EXPR:
9512146895Skan    case FIX_ROUND_EXPR:
9513146895Skan      return 0;
9514146895Skan
951590075Sobrien    default:
9516132718Skan      /* Leave front-end specific codes as simply unknown.  This comes
9517132718Skan	 up, for instance, with the C STMT_EXPR.  */
9518132718Skan      if ((unsigned int) TREE_CODE (loc)
9519132718Skan          >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9520132718Skan	return 0;
9521132718Skan
9522146895Skan#ifdef ENABLE_CHECKING
9523132718Skan      /* Otherwise this is a generic code; we should just lists all of
9524169689Skan	 these explicitly.  We forgot one.  */
9525169689Skan      gcc_unreachable ();
9526146895Skan#else
9527146895Skan      /* In a release build, we want to degrade gracefully: better to
9528146895Skan	 generate incomplete debugging information than to crash.  */
9529146895Skan      return NULL;
9530146895Skan#endif
953190075Sobrien    }
953290075Sobrien
953390075Sobrien  /* Show if we can't fill the request for an address.  */
9534169689Skan  if (want_address && !have_address)
953590075Sobrien    return 0;
953690075Sobrien
953790075Sobrien  /* If we've got an address and don't want one, dereference.  */
9538169689Skan  if (!want_address && have_address && ret)
953990075Sobrien    {
954090075Sobrien      HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
954190075Sobrien
954290075Sobrien      if (size > DWARF2_ADDR_SIZE || size == -1)
954390075Sobrien	return 0;
954490075Sobrien      else if (size == DWARF2_ADDR_SIZE)
954590075Sobrien	op = DW_OP_deref;
954690075Sobrien      else
954790075Sobrien	op = DW_OP_deref_size;
954890075Sobrien
954990075Sobrien      add_loc_descr (&ret, new_loc_descr (op, size, 0));
955090075Sobrien    }
955190075Sobrien
955290075Sobrien  return ret;
955390075Sobrien}
955490075Sobrien
9555169689Skanstatic inline dw_loc_descr_ref
9556169689Skanloc_descriptor_from_tree (tree loc)
9557169689Skan{
9558169689Skan  return loc_descriptor_from_tree_1 (loc, 2);
9559169689Skan}
9560169689Skan
956190075Sobrien/* Given a value, round it up to the lowest multiple of `boundary'
956250397Sobrien   which is not less than the value itself.  */
956350397Sobrien
956490075Sobrienstatic inline HOST_WIDE_INT
9565132718Skanceiling (HOST_WIDE_INT value, unsigned int boundary)
956650397Sobrien{
956750397Sobrien  return (((value + boundary - 1) / boundary) * boundary);
956850397Sobrien}
956950397Sobrien
957050397Sobrien/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
957150397Sobrien   pointer to the declared type for the relevant field variable, or return
957250397Sobrien   `integer_type_node' if the given node turns out to be an
957350397Sobrien   ERROR_MARK node.  */
957450397Sobrien
957550397Sobrienstatic inline tree
9576132718Skanfield_type (tree decl)
957750397Sobrien{
957890075Sobrien  tree type;
957950397Sobrien
958050397Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
958150397Sobrien    return integer_type_node;
958250397Sobrien
958350397Sobrien  type = DECL_BIT_FIELD_TYPE (decl);
958450397Sobrien  if (type == NULL_TREE)
958550397Sobrien    type = TREE_TYPE (decl);
958650397Sobrien
958750397Sobrien  return type;
958850397Sobrien}
958950397Sobrien
959090075Sobrien/* Given a pointer to a tree node, return the alignment in bits for
959190075Sobrien   it, or else return BITS_PER_WORD if the node actually turns out to
959290075Sobrien   be an ERROR_MARK node.  */
959350397Sobrien
959450397Sobrienstatic inline unsigned
9595132718Skansimple_type_align_in_bits (tree type)
959650397Sobrien{
959750397Sobrien  return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
959850397Sobrien}
959950397Sobrien
960090075Sobrienstatic inline unsigned
9601132718Skansimple_decl_align_in_bits (tree decl)
960290075Sobrien{
9603117395Skan  return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
960490075Sobrien}
960590075Sobrien
960690075Sobrien/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
960790075Sobrien   lowest addressed byte of the "containing object" for the given FIELD_DECL,
960890075Sobrien   or return 0 if we are unable to determine what that offset is, either
960990075Sobrien   because the argument turns out to be a pointer to an ERROR_MARK node, or
961090075Sobrien   because the offset is actually variable.  (We can't handle the latter case
961190075Sobrien   just yet).  */
961250397Sobrien
961390075Sobrienstatic HOST_WIDE_INT
9614132718Skanfield_byte_offset (tree decl)
961550397Sobrien{
961690075Sobrien  unsigned int type_align_in_bits;
961790075Sobrien  unsigned int decl_align_in_bits;
961890075Sobrien  unsigned HOST_WIDE_INT type_size_in_bits;
961990075Sobrien  HOST_WIDE_INT object_offset_in_bits;
962090075Sobrien  tree type;
962190075Sobrien  tree field_size_tree;
962290075Sobrien  HOST_WIDE_INT bitpos_int;
962390075Sobrien  HOST_WIDE_INT deepest_bitpos;
962490075Sobrien  unsigned HOST_WIDE_INT field_size_in_bits;
962550397Sobrien
962650397Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
962750397Sobrien    return 0;
962850397Sobrien
9629169689Skan  gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9630169689Skan
963150397Sobrien  type = field_type (decl);
963250397Sobrien  field_size_tree = DECL_SIZE (decl);
963350397Sobrien
963490075Sobrien  /* The size could be unspecified if there was an error, or for
963590075Sobrien     a flexible array member.  */
963690075Sobrien  if (! field_size_tree)
963790075Sobrien    field_size_tree = bitsize_zero_node;
963890075Sobrien
963990075Sobrien  /* We cannot yet cope with fields whose positions are variable, so
964050397Sobrien     for now, when we see such things, we simply return 0.  Someday, we may
964150397Sobrien     be able to handle such cases, but it will be damn difficult.  */
964290075Sobrien  if (! host_integerp (bit_position (decl), 0))
964350397Sobrien    return 0;
964450397Sobrien
964590075Sobrien  bitpos_int = int_bit_position (decl);
964650397Sobrien
964790075Sobrien  /* If we don't know the size of the field, pretend it's a full word.  */
964890075Sobrien  if (host_integerp (field_size_tree, 1))
964990075Sobrien    field_size_in_bits = tree_low_cst (field_size_tree, 1);
965090075Sobrien  else
965190075Sobrien    field_size_in_bits = BITS_PER_WORD;
965290075Sobrien
965350397Sobrien  type_size_in_bits = simple_type_size_in_bits (type);
965450397Sobrien  type_align_in_bits = simple_type_align_in_bits (type);
9655117395Skan  decl_align_in_bits = simple_decl_align_in_bits (decl);
965650397Sobrien
965790075Sobrien  /* The GCC front-end doesn't make any attempt to keep track of the starting
965890075Sobrien     bit offset (relative to the start of the containing structure type) of the
965990075Sobrien     hypothetical "containing object" for a bit-field.  Thus, when computing
966090075Sobrien     the byte offset value for the start of the "containing object" of a
966190075Sobrien     bit-field, we must deduce this information on our own. This can be rather
966290075Sobrien     tricky to do in some cases.  For example, handling the following structure
966390075Sobrien     type definition when compiling for an i386/i486 target (which only aligns
966490075Sobrien     long long's to 32-bit boundaries) can be very tricky:
966550397Sobrien
966650397Sobrien	 struct S { int field1; long long field2:31; };
966750397Sobrien
966890075Sobrien     Fortunately, there is a simple rule-of-thumb which can be used in such
966990075Sobrien     cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
967090075Sobrien     structure shown above.  It decides to do this based upon one simple rule
967190075Sobrien     for bit-field allocation.  GCC allocates each "containing object" for each
967290075Sobrien     bit-field at the first (i.e. lowest addressed) legitimate alignment
967390075Sobrien     boundary (based upon the required minimum alignment for the declared type
967490075Sobrien     of the field) which it can possibly use, subject to the condition that
967590075Sobrien     there is still enough available space remaining in the containing object
967690075Sobrien     (when allocated at the selected point) to fully accommodate all of the
967790075Sobrien     bits of the bit-field itself.
967850397Sobrien
967990075Sobrien     This simple rule makes it obvious why GCC allocates 8 bytes for each
968090075Sobrien     object of the structure type shown above.  When looking for a place to
968190075Sobrien     allocate the "containing object" for `field2', the compiler simply tries
968290075Sobrien     to allocate a 64-bit "containing object" at each successive 32-bit
968390075Sobrien     boundary (starting at zero) until it finds a place to allocate that 64-
968490075Sobrien     bit field such that at least 31 contiguous (and previously unallocated)
968590075Sobrien     bits remain within that selected 64 bit field.  (As it turns out, for the
968690075Sobrien     example above, the compiler finds it is OK to allocate the "containing
968790075Sobrien     object" 64-bit field at bit-offset zero within the structure type.)
968890075Sobrien
968990075Sobrien     Here we attempt to work backwards from the limited set of facts we're
969090075Sobrien     given, and we try to deduce from those facts, where GCC must have believed
969190075Sobrien     that the containing object started (within the structure type). The value
969290075Sobrien     we deduce is then used (by the callers of this routine) to generate
969390075Sobrien     DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
969490075Sobrien     and, in the case of DW_AT_location, regular fields as well).  */
969590075Sobrien
969650397Sobrien  /* Figure out the bit-distance from the start of the structure to the
969750397Sobrien     "deepest" bit of the bit-field.  */
969850397Sobrien  deepest_bitpos = bitpos_int + field_size_in_bits;
969950397Sobrien
970050397Sobrien  /* This is the tricky part.  Use some fancy footwork to deduce where the
970150397Sobrien     lowest addressed bit of the containing object must be.  */
970290075Sobrien  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
970350397Sobrien
970490075Sobrien  /* Round up to type_align by default.  This works best for bitfields.  */
970590075Sobrien  object_offset_in_bits += type_align_in_bits - 1;
970690075Sobrien  object_offset_in_bits /= type_align_in_bits;
970790075Sobrien  object_offset_in_bits *= type_align_in_bits;
970850397Sobrien
970990075Sobrien  if (object_offset_in_bits > bitpos_int)
971090075Sobrien    {
971190075Sobrien      /* Sigh, the decl must be packed.  */
971290075Sobrien      object_offset_in_bits = deepest_bitpos - type_size_in_bits;
971350397Sobrien
971490075Sobrien      /* Round up to decl_align instead.  */
971590075Sobrien      object_offset_in_bits += decl_align_in_bits - 1;
971690075Sobrien      object_offset_in_bits /= decl_align_in_bits;
971790075Sobrien      object_offset_in_bits *= decl_align_in_bits;
971890075Sobrien    }
971990075Sobrien
972090075Sobrien  return object_offset_in_bits / BITS_PER_UNIT;
972150397Sobrien}
972250397Sobrien
972350397Sobrien/* The following routines define various Dwarf attributes and any data
972450397Sobrien   associated with them.  */
972550397Sobrien
972650397Sobrien/* Add a location description attribute value to a DIE.
972750397Sobrien
972850397Sobrien   This emits location attributes suitable for whole variables and
972950397Sobrien   whole parameters.  Note that the location attributes for struct fields are
973050397Sobrien   generated by the routine `data_member_location_attribute' below.  */
973150397Sobrien
9732117395Skanstatic inline void
9733132718Skanadd_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9734132718Skan			     dw_loc_descr_ref descr)
973550397Sobrien{
973690075Sobrien  if (descr != 0)
973790075Sobrien    add_AT_loc (die, attr_kind, descr);
973850397Sobrien}
973950397Sobrien
974090075Sobrien/* Attach the specialized form of location attribute used for data members of
974190075Sobrien   struct and union types.  In the special case of a FIELD_DECL node which
974290075Sobrien   represents a bit-field, the "offset" part of this special location
974390075Sobrien   descriptor must indicate the distance in bytes from the lowest-addressed
974490075Sobrien   byte of the containing struct or union type to the lowest-addressed byte of
974590075Sobrien   the "containing object" for the bit-field.  (See the `field_byte_offset'
974690075Sobrien   function above).
974750397Sobrien
974890075Sobrien   For any given bit-field, the "containing object" is a hypothetical object
974990075Sobrien   (of some integral or enum type) within which the given bit-field lives.  The
975090075Sobrien   type of this hypothetical "containing object" is always the same as the
975190075Sobrien   declared type of the individual bit-field itself (for GCC anyway... the
975290075Sobrien   DWARF spec doesn't actually mandate this).  Note that it is the size (in
975390075Sobrien   bytes) of the hypothetical "containing object" which will be given in the
975490075Sobrien   DW_AT_byte_size attribute for this bit-field.  (See the
975590075Sobrien   `byte_size_attribute' function below.)  It is also used when calculating the
975690075Sobrien   value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
975790075Sobrien   function below.)  */
975890075Sobrien
975950397Sobrienstatic void
9760132718Skanadd_data_member_location_attribute (dw_die_ref die, tree decl)
976150397Sobrien{
9762132718Skan  HOST_WIDE_INT offset;
976390075Sobrien  dw_loc_descr_ref loc_descr = 0;
976450397Sobrien
9765169689Skan  if (TREE_CODE (decl) == TREE_BINFO)
976690075Sobrien    {
976790075Sobrien      /* We're working on the TAG_inheritance for a base class.  */
9768169689Skan      if (BINFO_VIRTUAL_P (decl) && is_cxx ())
976990075Sobrien	{
977090075Sobrien	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
977190075Sobrien	     aren't at a fixed offset from all (sub)objects of the same
977290075Sobrien	     type.  We need to extract the appropriate offset from our
977390075Sobrien	     vtable.  The following dwarf expression means
977490075Sobrien
977590075Sobrien	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
977690075Sobrien
977790075Sobrien	     This is specific to the V3 ABI, of course.  */
977890075Sobrien
977990075Sobrien	  dw_loc_descr_ref tmp;
978090075Sobrien
978190075Sobrien	  /* Make a copy of the object address.  */
978290075Sobrien	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
978390075Sobrien	  add_loc_descr (&loc_descr, tmp);
978490075Sobrien
978590075Sobrien	  /* Extract the vtable address.  */
978690075Sobrien	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
978790075Sobrien	  add_loc_descr (&loc_descr, tmp);
978890075Sobrien
978990075Sobrien	  /* Calculate the address of the offset.  */
979090075Sobrien	  offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9791169689Skan	  gcc_assert (offset < 0);
979290075Sobrien
979390075Sobrien	  tmp = int_loc_descriptor (-offset);
979490075Sobrien	  add_loc_descr (&loc_descr, tmp);
979590075Sobrien	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
979690075Sobrien	  add_loc_descr (&loc_descr, tmp);
979790075Sobrien
979890075Sobrien	  /* Extract the offset.  */
979990075Sobrien	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
980090075Sobrien	  add_loc_descr (&loc_descr, tmp);
980190075Sobrien
980290075Sobrien	  /* Add it to the object address.  */
980390075Sobrien	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
980490075Sobrien	  add_loc_descr (&loc_descr, tmp);
980590075Sobrien	}
980690075Sobrien      else
980790075Sobrien	offset = tree_low_cst (BINFO_OFFSET (decl), 0);
980890075Sobrien    }
980950397Sobrien  else
981050397Sobrien    offset = field_byte_offset (decl);
981150397Sobrien
981290075Sobrien  if (! loc_descr)
981390075Sobrien    {
981490075Sobrien      enum dwarf_location_atom op;
981550397Sobrien
981690075Sobrien      /* The DWARF2 standard says that we should assume that the structure
981790075Sobrien	 address is already on the stack, so we can specify a structure field
981890075Sobrien	 address by using DW_OP_plus_uconst.  */
981990075Sobrien
982050397Sobrien#ifdef MIPS_DEBUGGING_INFO
982190075Sobrien      /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
982290075Sobrien	 operator correctly.  It works only if we leave the offset on the
982390075Sobrien	 stack.  */
982490075Sobrien      op = DW_OP_constu;
982550397Sobrien#else
982690075Sobrien      op = DW_OP_plus_uconst;
982750397Sobrien#endif
982850397Sobrien
982990075Sobrien      loc_descr = new_loc_descr (op, offset, 0);
983090075Sobrien    }
983190075Sobrien
983250397Sobrien  add_AT_loc (die, DW_AT_data_member_location, loc_descr);
983350397Sobrien}
983450397Sobrien
9835132718Skan/* Writes integer values to dw_vec_const array.  */
9836132718Skan
9837132718Skanstatic void
9838132718Skaninsert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9839132718Skan{
9840132718Skan  while (size != 0)
9841132718Skan    {
9842132718Skan      *dest++ = val & 0xff;
9843132718Skan      val >>= 8;
9844132718Skan      --size;
9845132718Skan    }
9846132718Skan}
9847132718Skan
9848132718Skan/* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9849132718Skan
9850132718Skanstatic HOST_WIDE_INT
9851132718Skanextract_int (const unsigned char *src, unsigned int size)
9852132718Skan{
9853132718Skan  HOST_WIDE_INT val = 0;
9854132718Skan
9855132718Skan  src += size;
9856132718Skan  while (size != 0)
9857132718Skan    {
9858132718Skan      val <<= 8;
9859132718Skan      val |= *--src & 0xff;
9860132718Skan      --size;
9861132718Skan    }
9862132718Skan  return val;
9863132718Skan}
9864132718Skan
9865132718Skan/* Writes floating point values to dw_vec_const array.  */
9866132718Skan
9867132718Skanstatic void
9868132718Skaninsert_float (rtx rtl, unsigned char *array)
9869132718Skan{
9870132718Skan  REAL_VALUE_TYPE rv;
9871132718Skan  long val[4];
9872132718Skan  int i;
9873132718Skan
9874132718Skan  REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9875132718Skan  real_to_target (val, &rv, GET_MODE (rtl));
9876132718Skan
9877132718Skan  /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9878132718Skan  for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9879132718Skan    {
9880132718Skan      insert_int (val[i], 4, array);
9881132718Skan      array += 4;
9882132718Skan    }
9883132718Skan}
9884132718Skan
9885132718Skan/* Attach a DW_AT_const_value attribute for a variable or a parameter which
988650397Sobrien   does not have a "location" either in memory or in a register.  These
988750397Sobrien   things can arise in GNU C when a constant is passed as an actual parameter
988850397Sobrien   to an inlined function.  They can also arise in C++ where declared
988950397Sobrien   constants do not necessarily get memory "homes".  */
989050397Sobrien
989150397Sobrienstatic void
9892132718Skanadd_const_value_attribute (dw_die_ref die, rtx rtl)
989350397Sobrien{
989450397Sobrien  switch (GET_CODE (rtl))
989550397Sobrien    {
989650397Sobrien    case CONST_INT:
989790075Sobrien      {
989890075Sobrien	HOST_WIDE_INT val = INTVAL (rtl);
9899117395Skan
9900132718Skan	if (val < 0)
9901132718Skan	  add_AT_int (die, DW_AT_const_value, val);
9902169689Skan	else
9903132718Skan	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
990490075Sobrien      }
990550397Sobrien      break;
990650397Sobrien
990750397Sobrien    case CONST_DOUBLE:
990850397Sobrien      /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9909132718Skan	 floating-point constant.  A CONST_DOUBLE is used whenever the
9910132718Skan	 constant requires more than one word in order to be adequately
9911132718Skan	 represented.  We output CONST_DOUBLEs as blocks.  */
991250397Sobrien      {
991390075Sobrien	enum machine_mode mode = GET_MODE (rtl);
991450397Sobrien
9915169689Skan	if (SCALAR_FLOAT_MODE_P (mode))
991650397Sobrien	  {
9917132718Skan	    unsigned int length = GET_MODE_SIZE (mode);
9918132718Skan	    unsigned char *array = ggc_alloc (length);
991950397Sobrien
9920132718Skan	    insert_float (rtl, array);
9921132718Skan	    add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
992250397Sobrien	  }
992350397Sobrien	else
992490075Sobrien	  {
992590075Sobrien	    /* ??? We really should be using HOST_WIDE_INT throughout.  */
9926169689Skan	    gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
992790075Sobrien
992890075Sobrien	    add_AT_long_long (die, DW_AT_const_value,
992990075Sobrien			      CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
993090075Sobrien	  }
993150397Sobrien      }
993250397Sobrien      break;
993350397Sobrien
9934132718Skan    case CONST_VECTOR:
9935132718Skan      {
9936132718Skan	enum machine_mode mode = GET_MODE (rtl);
9937132718Skan	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9938132718Skan	unsigned int length = CONST_VECTOR_NUNITS (rtl);
9939132718Skan	unsigned char *array = ggc_alloc (length * elt_size);
9940132718Skan	unsigned int i;
9941132718Skan	unsigned char *p;
9942132718Skan
9943169689Skan	switch (GET_MODE_CLASS (mode))
9944132718Skan	  {
9945169689Skan	  case MODE_VECTOR_INT:
9946132718Skan	    for (i = 0, p = array; i < length; i++, p += elt_size)
9947132718Skan	      {
9948132718Skan		rtx elt = CONST_VECTOR_ELT (rtl, i);
9949132718Skan		HOST_WIDE_INT lo, hi;
9950169689Skan
9951169689Skan		switch (GET_CODE (elt))
9952132718Skan		  {
9953169689Skan		  case CONST_INT:
9954132718Skan		    lo = INTVAL (elt);
9955132718Skan		    hi = -(lo < 0);
9956169689Skan		    break;
9957169689Skan
9958169689Skan		  case CONST_DOUBLE:
9959132718Skan		    lo = CONST_DOUBLE_LOW (elt);
9960132718Skan		    hi = CONST_DOUBLE_HIGH (elt);
9961169689Skan		    break;
9962169689Skan
9963169689Skan		  default:
9964169689Skan		    gcc_unreachable ();
9965132718Skan		  }
9966132718Skan
9967132718Skan		if (elt_size <= sizeof (HOST_WIDE_INT))
9968132718Skan		  insert_int (lo, elt_size, p);
9969169689Skan		else
9970132718Skan		  {
9971132718Skan		    unsigned char *p0 = p;
9972132718Skan		    unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9973132718Skan
9974169689Skan		    gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9975132718Skan		    if (WORDS_BIG_ENDIAN)
9976132718Skan		      {
9977132718Skan			p0 = p1;
9978132718Skan			p1 = p;
9979132718Skan		      }
9980132718Skan		    insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9981132718Skan		    insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9982132718Skan		  }
9983132718Skan	      }
9984169689Skan	    break;
9985169689Skan
9986169689Skan	  case MODE_VECTOR_FLOAT:
9987132718Skan	    for (i = 0, p = array; i < length; i++, p += elt_size)
9988132718Skan	      {
9989132718Skan		rtx elt = CONST_VECTOR_ELT (rtl, i);
9990132718Skan		insert_float (elt, p);
9991132718Skan	      }
9992169689Skan	    break;
9993169689Skan
9994169689Skan	  default:
9995169689Skan	    gcc_unreachable ();
9996132718Skan	  }
9997132718Skan
9998132718Skan	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9999132718Skan      }
10000132718Skan      break;
10001132718Skan
1000250397Sobrien    case CONST_STRING:
1000350397Sobrien      add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
1000450397Sobrien      break;
1000550397Sobrien
1000650397Sobrien    case SYMBOL_REF:
1000750397Sobrien    case LABEL_REF:
1000850397Sobrien    case CONST:
1000990075Sobrien      add_AT_addr (die, DW_AT_const_value, rtl);
10010169689Skan      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
1001150397Sobrien      break;
1001250397Sobrien
1001350397Sobrien    case PLUS:
1001450397Sobrien      /* In cases where an inlined instance of an inline function is passed
10015132718Skan	 the address of an `auto' variable (which is local to the caller) we
10016132718Skan	 can get a situation where the DECL_RTL of the artificial local
10017132718Skan	 variable (for the inlining) which acts as a stand-in for the
10018132718Skan	 corresponding formal parameter (of the inline function) will look
10019132718Skan	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10020132718Skan	 exactly a compile-time constant expression, but it isn't the address
10021132718Skan	 of the (artificial) local variable either.  Rather, it represents the
10022132718Skan	 *value* which the artificial local variable always has during its
10023132718Skan	 lifetime.  We currently have no way to represent such quasi-constant
10024132718Skan	 values in Dwarf, so for now we just punt and generate nothing.  */
1002550397Sobrien      break;
1002650397Sobrien
1002750397Sobrien    default:
1002850397Sobrien      /* No other kinds of rtx should be possible here.  */
10029169689Skan      gcc_unreachable ();
1003050397Sobrien    }
1003150397Sobrien
1003250397Sobrien}
1003350397Sobrien
10034169689Skan/* Determine whether the evaluation of EXPR references any variables
10035169689Skan   or functions which aren't otherwise used (and therefore may not be
10036169689Skan   output).  */
10037169689Skanstatic tree
10038169689Skanreference_to_unused (tree * tp, int * walk_subtrees,
10039169689Skan		     void * data ATTRIBUTE_UNUSED)
10040169689Skan{
10041169689Skan  if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10042169689Skan    *walk_subtrees = 0;
10043169689Skan
10044169689Skan  if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10045169689Skan      && ! TREE_ASM_WRITTEN (*tp))
10046169689Skan    return *tp;
10047169689Skan  else if (!flag_unit_at_a_time)
10048169689Skan    return NULL_TREE;
10049169689Skan  else if (!cgraph_global_info_ready
10050169689Skan	   && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10051195815Sbms    return *tp;
10052169689Skan  else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10053169689Skan    {
10054169689Skan      struct cgraph_varpool_node *node = cgraph_varpool_node (*tp);
10055169689Skan      if (!node->needed)
10056169689Skan	return *tp;
10057169689Skan    }
10058169689Skan   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10059169689Skan	    && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10060169689Skan    {
10061169689Skan      struct cgraph_node *node = cgraph_node (*tp);
10062169689Skan      if (!node->output)
10063169689Skan        return *tp;
10064169689Skan    }
10065169689Skan
10066169689Skan  return NULL_TREE;
10067169689Skan}
10068169689Skan
10069169689Skan/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10070169689Skan   for use in a later add_const_value_attribute call.  */
10071169689Skan
1007290075Sobrienstatic rtx
10073169689Skanrtl_for_decl_init (tree init, tree type)
10074169689Skan{
10075169689Skan  rtx rtl = NULL_RTX;
10076169689Skan
10077169689Skan  /* If a variable is initialized with a string constant without embedded
10078169689Skan     zeros, build CONST_STRING.  */
10079169689Skan  if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10080169689Skan    {
10081169689Skan      tree enttype = TREE_TYPE (type);
10082169689Skan      tree domain = TYPE_DOMAIN (type);
10083169689Skan      enum machine_mode mode = TYPE_MODE (enttype);
10084169689Skan
10085169689Skan      if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10086169689Skan	  && domain
10087169689Skan	  && integer_zerop (TYPE_MIN_VALUE (domain))
10088169689Skan	  && compare_tree_int (TYPE_MAX_VALUE (domain),
10089169689Skan			       TREE_STRING_LENGTH (init) - 1) == 0
10090169689Skan	  && ((size_t) TREE_STRING_LENGTH (init)
10091169689Skan	      == strlen (TREE_STRING_POINTER (init)) + 1))
10092169689Skan	rtl = gen_rtx_CONST_STRING (VOIDmode,
10093169689Skan				    ggc_strdup (TREE_STRING_POINTER (init)));
10094169689Skan    }
10095169689Skan  /* Other aggregates, and complex values, could be represented using
10096169689Skan     CONCAT: FIXME!  */
10097169689Skan  else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10098169689Skan    ;
10099169689Skan  /* Vectors only work if their mode is supported by the target.
10100169689Skan     FIXME: generic vectors ought to work too.  */
10101169689Skan  else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10102169689Skan    ;
10103169689Skan  /* If the initializer is something that we know will expand into an
10104169689Skan     immediate RTL constant, expand it now.  We must be careful not to
10105169689Skan     reference variables which won't be output.  */
10106169689Skan  else if (initializer_constant_valid_p (init, type)
10107169689Skan	   && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10108169689Skan    {
10109220150Smm      /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10110220150Smm	 possible.  */
10111220150Smm      if (TREE_CODE (type) == VECTOR_TYPE)
10112220150Smm	switch (TREE_CODE (init))
10113220150Smm	  {
10114220150Smm	  case VECTOR_CST:
10115220150Smm	    break;
10116220150Smm	  case CONSTRUCTOR:
10117220150Smm	    if (TREE_CONSTANT (init))
10118220150Smm	      {
10119220150Smm		VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10120220150Smm		bool constant_p = true;
10121220150Smm		tree value;
10122220150Smm		unsigned HOST_WIDE_INT ix;
10123220150Smm
10124220150Smm		/* Even when ctor is constant, it might contain non-*_CST
10125220150Smm		   elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10126220150Smm		   belong into VECTOR_CST nodes.  */
10127220150Smm		FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10128220150Smm		  if (!CONSTANT_CLASS_P (value))
10129220150Smm		    {
10130220150Smm		      constant_p = false;
10131220150Smm		      break;
10132220150Smm		    }
10133220150Smm
10134220150Smm		if (constant_p)
10135220150Smm		  {
10136220150Smm		    init = build_vector_from_ctor (type, elts);
10137220150Smm		    break;
10138220150Smm		  }
10139220150Smm	      }
10140220150Smm	    /* FALLTHRU */
10141220150Smm
10142220150Smm	  default:
10143220150Smm	    return NULL;
10144220150Smm	  }
10145220150Smm
10146169689Skan      rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10147169689Skan
10148169689Skan      /* If expand_expr returns a MEM, it wasn't immediate.  */
10149169689Skan      gcc_assert (!rtl || !MEM_P (rtl));
10150169689Skan    }
10151169689Skan
10152169689Skan  return rtl;
10153169689Skan}
10154169689Skan
10155169689Skan/* Generate RTL for the variable DECL to represent its location.  */
10156169689Skan
10157169689Skanstatic rtx
10158132718Skanrtl_for_decl_location (tree decl)
1015950397Sobrien{
1016090075Sobrien  rtx rtl;
1016150397Sobrien
1016250397Sobrien  /* Here we have to decide where we are going to say the parameter "lives"
1016350397Sobrien     (as far as the debugger is concerned).  We only have a couple of
1016450397Sobrien     choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
1016550397Sobrien
1016690075Sobrien     DECL_RTL normally indicates where the parameter lives during most of the
1016750397Sobrien     activation of the function.  If optimization is enabled however, this
1016890075Sobrien     could be either NULL or else a pseudo-reg.  Both of those cases indicate
1016950397Sobrien     that the parameter doesn't really live anywhere (as far as the code
1017050397Sobrien     generation parts of GCC are concerned) during most of the function's
1017150397Sobrien     activation.  That will happen (for example) if the parameter is never
1017250397Sobrien     referenced within the function.
1017350397Sobrien
1017450397Sobrien     We could just generate a location descriptor here for all non-NULL
1017550397Sobrien     non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
1017650397Sobrien     a little nicer than that if we also consider DECL_INCOMING_RTL in cases
1017750397Sobrien     where DECL_RTL is NULL or is a pseudo-reg.
1017850397Sobrien
1017950397Sobrien     Note however that we can only get away with using DECL_INCOMING_RTL as
1018050397Sobrien     a backup substitute for DECL_RTL in certain limited cases.  In cases
1018150397Sobrien     where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
1018250397Sobrien     we can be sure that the parameter was passed using the same type as it is
1018350397Sobrien     declared to have within the function, and that its DECL_INCOMING_RTL
1018450397Sobrien     points us to a place where a value of that type is passed.
1018550397Sobrien
1018650397Sobrien     In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
1018750397Sobrien     we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
1018850397Sobrien     because in these cases DECL_INCOMING_RTL points us to a value of some
1018950397Sobrien     type which is *different* from the type of the parameter itself.  Thus,
1019050397Sobrien     if we tried to use DECL_INCOMING_RTL to generate a location attribute in
1019150397Sobrien     such cases, the debugger would end up (for example) trying to fetch a
1019250397Sobrien     `float' from a place which actually contains the first part of a
1019350397Sobrien     `double'.  That would lead to really incorrect and confusing
1019450397Sobrien     output at debug-time.
1019550397Sobrien
1019650397Sobrien     So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
1019750397Sobrien     in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
1019850397Sobrien     are a couple of exceptions however.  On little-endian machines we can
1019950397Sobrien     get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
1020050397Sobrien     not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
1020150397Sobrien     an integral type that is smaller than TREE_TYPE (decl). These cases arise
1020250397Sobrien     when (on a little-endian machine) a non-prototyped function has a
1020350397Sobrien     parameter declared to be of type `short' or `char'.  In such cases,
1020450397Sobrien     TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
1020550397Sobrien     be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
1020650397Sobrien     passed `int' value.  If the debugger then uses that address to fetch
1020750397Sobrien     a `short' or a `char' (on a little-endian machine) the result will be
1020850397Sobrien     the correct data, so we allow for such exceptional cases below.
1020950397Sobrien
1021050397Sobrien     Note that our goal here is to describe the place where the given formal
1021190075Sobrien     parameter lives during most of the function's activation (i.e. between the
1021290075Sobrien     end of the prologue and the start of the epilogue).  We'll do that as best
1021390075Sobrien     as we can. Note however that if the given formal parameter is modified
1021490075Sobrien     sometime during the execution of the function, then a stack backtrace (at
1021590075Sobrien     debug-time) will show the function as having been called with the *new*
1021690075Sobrien     value rather than the value which was originally passed in.  This happens
1021790075Sobrien     rarely enough that it is not a major problem, but it *is* a problem, and
1021890075Sobrien     I'd like to fix it.
1021950397Sobrien
1022090075Sobrien     A future version of dwarf2out.c may generate two additional attributes for
1022190075Sobrien     any given DW_TAG_formal_parameter DIE which will describe the "passed
1022290075Sobrien     type" and the "passed location" for the given formal parameter in addition
1022390075Sobrien     to the attributes we now generate to indicate the "declared type" and the
1022490075Sobrien     "active location" for each parameter.  This additional set of attributes
1022590075Sobrien     could be used by debuggers for stack backtraces. Separately, note that
1022690075Sobrien     sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
1022790075Sobrien     This happens (for example) for inlined-instances of inline function formal
1022890075Sobrien     parameters which are never referenced.  This really shouldn't be
1022990075Sobrien     happening.  All PARM_DECL nodes should get valid non-NULL
10230169689Skan     DECL_INCOMING_RTL values.  FIXME.  */
1023150397Sobrien
1023250397Sobrien  /* Use DECL_RTL as the "location" unless we find something better.  */
1023390075Sobrien  rtl = DECL_RTL_IF_SET (decl);
1023450397Sobrien
1023590075Sobrien  /* When generating abstract instances, ignore everything except
10236117395Skan     constants, symbols living in memory, and symbols living in
10237117395Skan     fixed registers.  */
1023890075Sobrien  if (! reload_completed)
1023950397Sobrien    {
1024090075Sobrien      if (rtl
1024190075Sobrien	  && (CONSTANT_P (rtl)
10242169689Skan	      || (MEM_P (rtl)
10243117395Skan	          && CONSTANT_P (XEXP (rtl, 0)))
10244169689Skan	      || (REG_P (rtl)
10245117395Skan	          && TREE_CODE (decl) == VAR_DECL
10246117395Skan		  && TREE_STATIC (decl))))
1024796263Sobrien	{
10248169689Skan	  rtl = targetm.delegitimize_address (rtl);
1024996263Sobrien	  return rtl;
1025096263Sobrien	}
1025190075Sobrien      rtl = NULL_RTX;
1025290075Sobrien    }
1025390075Sobrien  else if (TREE_CODE (decl) == PARM_DECL)
1025490075Sobrien    {
1025550397Sobrien      if (rtl == NULL_RTX || is_pseudo_reg (rtl))
1025650397Sobrien	{
10257146895Skan	  tree declared_type = TREE_TYPE (decl);
10258146895Skan	  tree passed_type = DECL_ARG_TYPE (decl);
10259146895Skan	  enum machine_mode dmode = TYPE_MODE (declared_type);
10260146895Skan	  enum machine_mode pmode = TYPE_MODE (passed_type);
1026150397Sobrien
1026250397Sobrien	  /* This decl represents a formal parameter which was optimized out.
1026350397Sobrien	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
1026490075Sobrien	     all cases where (rtl == NULL_RTX) just below.  */
10265146895Skan	  if (dmode == pmode)
1026650397Sobrien	    rtl = DECL_INCOMING_RTL (decl);
10267146895Skan	  else if (SCALAR_INT_MODE_P (dmode)
10268146895Skan		   && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10269146895Skan		   && DECL_INCOMING_RTL (decl))
10270146895Skan	    {
10271146895Skan	      rtx inc = DECL_INCOMING_RTL (decl);
10272146895Skan	      if (REG_P (inc))
10273146895Skan		rtl = inc;
10274169689Skan	      else if (MEM_P (inc))
10275146895Skan		{
10276146895Skan		  if (BYTES_BIG_ENDIAN)
10277146895Skan		    rtl = adjust_address_nv (inc, dmode,
10278146895Skan					     GET_MODE_SIZE (pmode)
10279146895Skan					     - GET_MODE_SIZE (dmode));
10280146895Skan		  else
10281146895Skan		    rtl = inc;
10282146895Skan		}
10283146895Skan	    }
1028450397Sobrien	}
1028552284Sobrien
1028652284Sobrien      /* If the parm was passed in registers, but lives on the stack, then
1028752284Sobrien	 make a big endian correction if the mode of the type of the
1028852284Sobrien	 parameter is not the same as the mode of the rtl.  */
1028952284Sobrien      /* ??? This is the same series of checks that are made in dbxout.c before
1029052284Sobrien	 we reach the big endian correction code there.  It isn't clear if all
1029152284Sobrien	 of these checks are necessary here, but keeping them all is the safe
1029252284Sobrien	 thing to do.  */
10293169689Skan      else if (MEM_P (rtl)
1029452284Sobrien	       && XEXP (rtl, 0) != const0_rtx
1029552284Sobrien	       && ! CONSTANT_P (XEXP (rtl, 0))
1029652284Sobrien	       /* Not passed in memory.  */
10297169689Skan	       && !MEM_P (DECL_INCOMING_RTL (decl))
1029852284Sobrien	       /* Not passed by invisible reference.  */
10299169689Skan	       && (!REG_P (XEXP (rtl, 0))
1030052284Sobrien		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
1030152284Sobrien		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
1030252284Sobrien#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1030352284Sobrien		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
1030452284Sobrien#endif
1030552284Sobrien		     )
1030652284Sobrien	       /* Big endian correction check.  */
1030752284Sobrien	       && BYTES_BIG_ENDIAN
1030852284Sobrien	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
1030952284Sobrien	       && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
1031052284Sobrien		   < UNITS_PER_WORD))
1031152284Sobrien	{
1031252284Sobrien	  int offset = (UNITS_PER_WORD
1031352284Sobrien			- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
1031490075Sobrien
1031552284Sobrien	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
1031652284Sobrien			     plus_constant (XEXP (rtl, 0), offset));
1031752284Sobrien	}
1031850397Sobrien    }
10319169689Skan  else if (TREE_CODE (decl) == VAR_DECL
10320169689Skan	   && rtl
10321169689Skan	   && MEM_P (rtl)
10322169689Skan	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10323169689Skan	   && BYTES_BIG_ENDIAN)
10324169689Skan    {
10325169689Skan      int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10326169689Skan      int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
1032750397Sobrien
10328169689Skan      /* If a variable is declared "register" yet is smaller than
10329169689Skan	 a register, then if we store the variable to memory, it
10330169689Skan	 looks like we're storing a register-sized value, when in
10331169689Skan	 fact we are not.  We need to adjust the offset of the
10332169689Skan	 storage location to reflect the actual value's bytes,
10333169689Skan	 else gdb will not be able to display it.  */
10334169689Skan      if (rsize > dsize)
10335169689Skan	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10336169689Skan			   plus_constant (XEXP (rtl, 0), rsize-dsize));
1033790075Sobrien    }
1033890075Sobrien
1033990075Sobrien  /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
1034090075Sobrien     and will have been substituted directly into all expressions that use it.
1034190075Sobrien     C does not have such a concept, but C++ and other languages do.  */
10342169689Skan  if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10343169689Skan    rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
1034490075Sobrien
1034596263Sobrien  if (rtl)
10346169689Skan    rtl = targetm.delegitimize_address (rtl);
10347117395Skan
10348117395Skan  /* If we don't look past the constant pool, we risk emitting a
10349117395Skan     reference to a constant pool entry that isn't referenced from
10350117395Skan     code, and thus is not emitted.  */
10351117395Skan  if (rtl)
10352117395Skan    rtl = avoid_constant_pool_reference (rtl);
10353117395Skan
1035490075Sobrien  return rtl;
1035590075Sobrien}
1035690075Sobrien
10357169689Skan/* We need to figure out what section we should use as the base for the
10358169689Skan   address ranges where a given location is valid.
10359169689Skan   1. If this particular DECL has a section associated with it, use that.
10360169689Skan   2. If this function has a section associated with it, use that.
10361169689Skan   3. Otherwise, use the text section.
10362169689Skan   XXX: If you split a variable across multiple sections, we won't notice.  */
10363169689Skan
10364169689Skanstatic const char *
10365169689Skansecname_for_decl (tree decl)
10366169689Skan{
10367169689Skan  const char *secname;
10368169689Skan
10369169689Skan  if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10370169689Skan    {
10371169689Skan      tree sectree = DECL_SECTION_NAME (decl);
10372169689Skan      secname = TREE_STRING_POINTER (sectree);
10373169689Skan    }
10374169689Skan  else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10375169689Skan    {
10376169689Skan      tree sectree = DECL_SECTION_NAME (current_function_decl);
10377169689Skan      secname = TREE_STRING_POINTER (sectree);
10378169689Skan    }
10379169689Skan  else if (cfun && in_cold_section_p)
10380169689Skan    secname = cfun->cold_section_label;
10381169689Skan  else
10382169689Skan    secname = text_section_label;
10383169689Skan
10384169689Skan  return secname;
10385169689Skan}
10386169689Skan
10387132718Skan/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
1038890075Sobrien   data attribute for a variable or a parameter.  We generate the
1038990075Sobrien   DW_AT_const_value attribute only in those cases where the given variable
1039090075Sobrien   or parameter does not have a true "location" either in memory or in a
1039190075Sobrien   register.  This can happen (for example) when a constant is passed as an
1039290075Sobrien   actual argument in a call to an inline function.  (It's possible that
1039390075Sobrien   these things can crop up in other ways also.)  Note that one type of
1039490075Sobrien   constant value which can be passed into an inlined function is a constant
1039590075Sobrien   pointer.  This can happen for example if an actual argument in an inlined
1039690075Sobrien   function call evaluates to a compile-time constant address.  */
1039790075Sobrien
1039890075Sobrienstatic void
10399169689Skanadd_location_or_const_value_attribute (dw_die_ref die, tree decl,
10400169689Skan				       enum dwarf_attribute attr)
1040190075Sobrien{
1040290075Sobrien  rtx rtl;
10403117395Skan  dw_loc_descr_ref descr;
10404169689Skan  var_loc_list *loc_list;
10405169689Skan  struct var_loc_node *node;
1040690075Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
1040790075Sobrien    return;
1040890075Sobrien
10409169689Skan  gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10410169689Skan	      || TREE_CODE (decl) == RESULT_DECL);
10411169689Skan
10412169689Skan  /* See if we possibly have multiple locations for this variable.  */
10413169689Skan  loc_list = lookup_decl_loc (decl);
1041450397Sobrien
10415169689Skan  /* If it truly has multiple locations, the first and last node will
10416169689Skan     differ.  */
10417169689Skan  if (loc_list && loc_list->first != loc_list->last)
1041850397Sobrien    {
10419169689Skan      const char *endname, *secname;
10420169689Skan      dw_loc_list_ref list;
10421169689Skan      rtx varloc;
1042250397Sobrien
10423169689Skan      /* Now that we know what section we are using for a base,
10424169689Skan         actually construct the list of locations.
10425169689Skan	 The first location information is what is passed to the
10426169689Skan	 function that creates the location list, and the remaining
10427169689Skan	 locations just get added on to that list.
10428169689Skan	 Note that we only know the start address for a location
10429169689Skan	 (IE location changes), so to build the range, we use
10430169689Skan	 the range [current location start, next location start].
10431169689Skan	 This means we have to special case the last node, and generate
10432169689Skan	 a range of [last location start, end of function label].  */
1043350397Sobrien
10434169689Skan      node = loc_list->first;
10435169689Skan      varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10436169689Skan      secname = secname_for_decl (decl);
10437132718Skan
10438169689Skan      list = new_loc_list (loc_descriptor (varloc),
10439169689Skan			   node->label, node->next->label, secname, 1);
10440169689Skan      node = node->next;
10441132718Skan
10442169689Skan      for (; node->next; node = node->next)
10443169689Skan	if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10444132718Skan	  {
10445169689Skan	    /* The variable has a location between NODE->LABEL and
10446169689Skan	       NODE->NEXT->LABEL.  */
10447169689Skan	    varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10448169689Skan	    add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10449169689Skan				       node->label, node->next->label, secname);
10450132718Skan	  }
10451132718Skan
10452169689Skan      /* If the variable has a location at the last label
10453169689Skan	 it keeps its location until the end of function.  */
10454169689Skan      if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10455169689Skan	{
10456169689Skan	  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10457169689Skan
10458169689Skan	  varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10459169689Skan	  if (!current_function_decl)
10460169689Skan	    endname = text_end_label;
10461169689Skan	  else
10462169689Skan	    {
10463169689Skan	      ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10464169689Skan					   current_function_funcdef_no);
10465169689Skan	      endname = ggc_strdup (label_id);
10466169689Skan	    }
10467169689Skan	  add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10468169689Skan				     node->label, endname, secname);
10469169689Skan	}
10470169689Skan
10471169689Skan      /* Finally, add the location list to the DIE, and we are done.  */
10472169689Skan      add_AT_loc_list (die, attr, list);
10473169689Skan      return;
1047450397Sobrien    }
10475169689Skan
10476169689Skan  /* Try to get some constant RTL for this decl, and use that as the value of
10477169689Skan     the location.  */
10478169689Skan
10479169689Skan  rtl = rtl_for_decl_location (decl);
10480169689Skan  if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10481169689Skan    {
10482169689Skan      add_const_value_attribute (die, rtl);
10483169689Skan      return;
10484169689Skan    }
10485169689Skan
10486169689Skan  /* If we have tried to generate the location otherwise, and it
10487169689Skan     didn't work out (we wouldn't be here if we did), and we have a one entry
10488169689Skan     location list, try generating a location from that.  */
10489169689Skan  if (loc_list && loc_list->first)
10490169689Skan    {
10491169689Skan      node = loc_list->first;
10492169689Skan      descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10493169689Skan      if (descr)
10494169689Skan	{
10495169689Skan	  add_AT_location_description (die, attr, descr);
10496169689Skan	  return;
10497169689Skan	}
10498169689Skan    }
10499169689Skan
10500169689Skan  /* We couldn't get any rtl, so try directly generating the location
10501169689Skan     description from the tree.  */
10502169689Skan  descr = loc_descriptor_from_tree (decl);
10503169689Skan  if (descr)
10504169689Skan    {
10505169689Skan      add_AT_location_description (die, attr, descr);
10506169689Skan      return;
10507169689Skan    }
10508169689Skan  /* None of that worked, so it must not really have a location;
10509169689Skan     try adding a constant value attribute from the DECL_INITIAL.  */
10510169689Skan  tree_add_const_value_attribute (die, decl);
1051150397Sobrien}
1051250397Sobrien
1051390075Sobrien/* If we don't have a copy of this variable in memory for some reason (such
1051490075Sobrien   as a C++ member constant that doesn't have an out-of-line definition),
1051590075Sobrien   we should tell the debugger about the constant value.  */
1051690075Sobrien
1051790075Sobrienstatic void
10518132718Skantree_add_const_value_attribute (dw_die_ref var_die, tree decl)
1051990075Sobrien{
1052090075Sobrien  tree init = DECL_INITIAL (decl);
1052190075Sobrien  tree type = TREE_TYPE (decl);
10522169689Skan  rtx rtl;
1052390075Sobrien
10524169689Skan  if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
1052590075Sobrien    /* OK */;
1052690075Sobrien  else
1052790075Sobrien    return;
1052890075Sobrien
10529169689Skan  rtl = rtl_for_decl_init (init, type);
10530169689Skan  if (rtl)
10531169689Skan    add_const_value_attribute (var_die, rtl);
10532169689Skan}
10533169689Skan
10534169689Skan/* Convert the CFI instructions for the current function into a
10535169689Skan   location list.  This is used for DW_AT_frame_base when we targeting
10536169689Skan   a dwarf2 consumer that does not support the dwarf3
10537169689Skan   DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10538169689Skan   expressions.  */
10539169689Skan
10540169689Skanstatic dw_loc_list_ref
10541169689Skanconvert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10542169689Skan{
10543169689Skan  dw_fde_ref fde;
10544169689Skan  dw_loc_list_ref list, *list_tail;
10545169689Skan  dw_cfi_ref cfi;
10546169689Skan  dw_cfa_location last_cfa, next_cfa;
10547169689Skan  const char *start_label, *last_label, *section;
10548169689Skan
10549169689Skan  fde = &fde_table[fde_table_in_use - 1];
10550169689Skan
10551169689Skan  section = secname_for_decl (current_function_decl);
10552169689Skan  list_tail = &list;
10553169689Skan  list = NULL;
10554169689Skan
10555169689Skan  next_cfa.reg = INVALID_REGNUM;
10556169689Skan  next_cfa.offset = 0;
10557169689Skan  next_cfa.indirect = 0;
10558169689Skan  next_cfa.base_offset = 0;
10559169689Skan
10560169689Skan  start_label = fde->dw_fde_begin;
10561169689Skan
10562169689Skan  /* ??? Bald assumption that the CIE opcode list does not contain
10563169689Skan     advance opcodes.  */
10564169689Skan  for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10565169689Skan    lookup_cfa_1 (cfi, &next_cfa);
10566169689Skan
10567169689Skan  last_cfa = next_cfa;
10568169689Skan  last_label = start_label;
10569169689Skan
10570169689Skan  for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10571169689Skan    switch (cfi->dw_cfi_opc)
10572169689Skan      {
10573169689Skan      case DW_CFA_set_loc:
10574169689Skan      case DW_CFA_advance_loc1:
10575169689Skan      case DW_CFA_advance_loc2:
10576169689Skan      case DW_CFA_advance_loc4:
10577169689Skan	if (!cfa_equal_p (&last_cfa, &next_cfa))
10578169689Skan	  {
10579169689Skan	    *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10580169689Skan				       start_label, last_label, section,
10581169689Skan				       list == NULL);
10582169689Skan
10583169689Skan	    list_tail = &(*list_tail)->dw_loc_next;
10584169689Skan	    last_cfa = next_cfa;
10585169689Skan	    start_label = last_label;
10586169689Skan	  }
10587169689Skan	last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10588169689Skan	break;
10589169689Skan
10590169689Skan      case DW_CFA_advance_loc:
10591169689Skan	/* The encoding is complex enough that we should never emit this.  */
10592169689Skan      case DW_CFA_remember_state:
10593169689Skan      case DW_CFA_restore_state:
10594169689Skan	/* We don't handle these two in this function.  It would be possible
10595169689Skan	   if it were to be required.  */
10596169689Skan	gcc_unreachable ();
10597169689Skan
10598169689Skan      default:
10599169689Skan	lookup_cfa_1 (cfi, &next_cfa);
10600169689Skan	break;
10601169689Skan      }
10602169689Skan
10603169689Skan  if (!cfa_equal_p (&last_cfa, &next_cfa))
1060490075Sobrien    {
10605169689Skan      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10606169689Skan				 start_label, last_label, section,
10607169689Skan				 list == NULL);
10608169689Skan      list_tail = &(*list_tail)->dw_loc_next;
10609169689Skan      start_label = last_label;
10610169689Skan    }
10611169689Skan  *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10612169689Skan			     start_label, fde->dw_fde_end, section,
10613169689Skan			     list == NULL);
1061490075Sobrien
10615169689Skan  return list;
10616169689Skan}
10617169689Skan
10618169689Skan/* Compute a displacement from the "steady-state frame pointer" to the
10619169689Skan   frame base (often the same as the CFA), and store it in
10620169689Skan   frame_pointer_fb_offset.  OFFSET is added to the displacement
10621169689Skan   before the latter is negated.  */
10622169689Skan
10623169689Skanstatic void
10624169689Skancompute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10625169689Skan{
10626169689Skan  rtx reg, elim;
10627169689Skan
10628169689Skan#ifdef FRAME_POINTER_CFA_OFFSET
10629169689Skan  reg = frame_pointer_rtx;
10630169689Skan  offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10631169689Skan#else
10632169689Skan  reg = arg_pointer_rtx;
10633169689Skan  offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10634169689Skan#endif
10635169689Skan
10636169689Skan  elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10637169689Skan  if (GET_CODE (elim) == PLUS)
10638169689Skan    {
10639169689Skan      offset += INTVAL (XEXP (elim, 1));
10640169689Skan      elim = XEXP (elim, 0);
1064190075Sobrien    }
10642169689Skan  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10643169689Skan		       : stack_pointer_rtx));
10644169689Skan
10645169689Skan  frame_pointer_fb_offset = -offset;
1064690075Sobrien}
1064790075Sobrien
10648132718Skan/* Generate a DW_AT_name attribute given some string value to be included as
1064950397Sobrien   the value of the attribute.  */
1065050397Sobrien
10651132718Skanstatic void
10652132718Skanadd_name_attribute (dw_die_ref die, const char *name_string)
1065350397Sobrien{
1065450397Sobrien  if (name_string != NULL && *name_string != 0)
1065590075Sobrien    {
1065690075Sobrien      if (demangle_name_func)
1065790075Sobrien	name_string = (*demangle_name_func) (name_string);
1065890075Sobrien
1065990075Sobrien      add_AT_string (die, DW_AT_name, name_string);
1066090075Sobrien    }
1066150397Sobrien}
1066250397Sobrien
10663132718Skan/* Generate a DW_AT_comp_dir attribute for DIE.  */
10664132718Skan
10665132718Skanstatic void
10666132718Skanadd_comp_dir_attribute (dw_die_ref die)
10667132718Skan{
10668132718Skan  const char *wd = get_src_pwd ();
10669132718Skan  if (wd != NULL)
10670132718Skan    add_AT_string (die, DW_AT_comp_dir, wd);
10671132718Skan}
10672132718Skan
1067350397Sobrien/* Given a tree node describing an array bound (either lower or upper) output
1067450397Sobrien   a representation for that bound.  */
1067550397Sobrien
1067650397Sobrienstatic void
10677132718Skanadd_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
1067850397Sobrien{
1067950397Sobrien  switch (TREE_CODE (bound))
1068050397Sobrien    {
1068150397Sobrien    case ERROR_MARK:
1068250397Sobrien      return;
1068350397Sobrien
1068490075Sobrien    /* All fixed-bounds are represented by INTEGER_CST nodes.  */
1068550397Sobrien    case INTEGER_CST:
1068690075Sobrien      if (! host_integerp (bound, 0)
1068790075Sobrien	  || (bound_attr == DW_AT_lower_bound
1068890075Sobrien	      && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
1068990075Sobrien		  || (is_fortran () && integer_onep (bound)))))
10690169689Skan	/* Use the default.  */
1069190075Sobrien	;
1069250397Sobrien      else
1069390075Sobrien	add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
1069450397Sobrien      break;
1069550397Sobrien
1069650397Sobrien    case CONVERT_EXPR:
1069750397Sobrien    case NOP_EXPR:
1069850397Sobrien    case NON_LVALUE_EXPR:
1069990075Sobrien    case VIEW_CONVERT_EXPR:
1070050397Sobrien      add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
1070150397Sobrien      break;
1070290075Sobrien
1070350397Sobrien    case SAVE_EXPR:
1070450397Sobrien      break;
1070550397Sobrien
1070650397Sobrien    case VAR_DECL:
1070790075Sobrien    case PARM_DECL:
10708169689Skan    case RESULT_DECL:
1070990075Sobrien      {
1071090075Sobrien	dw_die_ref decl_die = lookup_decl_die (bound);
1071150397Sobrien
1071290075Sobrien	/* ??? Can this happen, or should the variable have been bound
1071390075Sobrien	   first?  Probably it can, since I imagine that we try to create
1071490075Sobrien	   the types of parameters in the order in which they exist in
1071590075Sobrien	   the list, and won't have created a forward reference to a
1071690075Sobrien	   later parameter.  */
1071790075Sobrien	if (decl_die != NULL)
1071890075Sobrien	  add_AT_die_ref (subrange_die, bound_attr, decl_die);
1071990075Sobrien	break;
1072090075Sobrien      }
1072190075Sobrien
1072250397Sobrien    default:
1072390075Sobrien      {
1072490075Sobrien	/* Otherwise try to create a stack operation procedure to
1072590075Sobrien	   evaluate the value of the array bound.  */
1072690075Sobrien
1072790075Sobrien	dw_die_ref ctx, decl_die;
1072890075Sobrien	dw_loc_descr_ref loc;
1072990075Sobrien
10730169689Skan	loc = loc_descriptor_from_tree (bound);
1073190075Sobrien	if (loc == NULL)
1073290075Sobrien	  break;
1073390075Sobrien
1073490075Sobrien	if (current_function_decl == 0)
1073590075Sobrien	  ctx = comp_unit_die;
1073690075Sobrien	else
1073790075Sobrien	  ctx = lookup_decl_die (current_function_decl);
1073890075Sobrien
1073990075Sobrien	decl_die = new_die (DW_TAG_variable, ctx, bound);
1074090075Sobrien	add_AT_flag (decl_die, DW_AT_artificial, 1);
1074190075Sobrien	add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
1074290075Sobrien	add_AT_loc (decl_die, DW_AT_location, loc);
1074390075Sobrien
1074490075Sobrien	add_AT_die_ref (subrange_die, bound_attr, decl_die);
1074590075Sobrien	break;
1074690075Sobrien      }
1074750397Sobrien    }
1074850397Sobrien}
1074950397Sobrien
1075050397Sobrien/* Note that the block of subscript information for an array type also
1075150397Sobrien   includes information about the element type of type given array type.  */
1075250397Sobrien
1075350397Sobrienstatic void
10754132718Skanadd_subscript_info (dw_die_ref type_die, tree type)
1075550397Sobrien{
1075650397Sobrien#ifndef MIPS_DEBUGGING_INFO
1075790075Sobrien  unsigned dimension_number;
1075850397Sobrien#endif
1075990075Sobrien  tree lower, upper;
1076090075Sobrien  dw_die_ref subrange_die;
1076150397Sobrien
1076290075Sobrien  /* The GNU compilers represent multidimensional array types as sequences of
1076350397Sobrien     one dimensional array types whose element types are themselves array
1076450397Sobrien     types.  Here we squish that down, so that each multidimensional array
1076590075Sobrien     type gets only one array_type DIE in the Dwarf debugging info. The draft
1076650397Sobrien     Dwarf specification say that we are allowed to do this kind of
1076750397Sobrien     compression in C (because there is no difference between an array or
1076890075Sobrien     arrays and a multidimensional array in C) but for other source languages
1076950397Sobrien     (e.g. Ada) we probably shouldn't do this.  */
1077050397Sobrien
1077150397Sobrien  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
1077250397Sobrien     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
1077350397Sobrien     We work around this by disabling this feature.  See also
1077450397Sobrien     gen_array_type_die.  */
1077550397Sobrien#ifndef MIPS_DEBUGGING_INFO
1077650397Sobrien  for (dimension_number = 0;
1077750397Sobrien       TREE_CODE (type) == ARRAY_TYPE;
1077850397Sobrien       type = TREE_TYPE (type), dimension_number++)
1077990075Sobrien#endif
1078050397Sobrien    {
1078190075Sobrien      tree domain = TYPE_DOMAIN (type);
1078250397Sobrien
1078350397Sobrien      /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
1078490075Sobrien	 and (in GNU C only) variable bounds.  Handle all three forms
10785132718Skan	 here.  */
1078690075Sobrien      subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
1078750397Sobrien      if (domain)
1078850397Sobrien	{
1078950397Sobrien	  /* We have an array type with specified bounds.  */
1079050397Sobrien	  lower = TYPE_MIN_VALUE (domain);
1079150397Sobrien	  upper = TYPE_MAX_VALUE (domain);
1079250397Sobrien
10793132718Skan	  /* Define the index type.  */
1079450397Sobrien	  if (TREE_TYPE (domain))
1079550397Sobrien	    {
1079650397Sobrien	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
1079750397Sobrien		 TREE_TYPE field.  We can't emit debug info for this
1079850397Sobrien		 because it is an unnamed integral type.  */
1079950397Sobrien	      if (TREE_CODE (domain) == INTEGER_TYPE
1080050397Sobrien		  && TYPE_NAME (domain) == NULL_TREE
1080150397Sobrien		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
1080250397Sobrien		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
1080390075Sobrien		;
1080450397Sobrien	      else
1080550397Sobrien		add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
1080650397Sobrien				    type_die);
1080750397Sobrien	    }
1080850397Sobrien
1080950397Sobrien	  /* ??? If upper is NULL, the array has unspecified length,
1081050397Sobrien	     but it does have a lower bound.  This happens with Fortran
1081150397Sobrien	       dimension arr(N:*)
10812132718Skan	     Since the debugger is definitely going to need to know N
1081350397Sobrien	     to produce useful results, go ahead and output the lower
1081450397Sobrien	     bound solo, and hope the debugger can cope.  */
1081550397Sobrien
1081650397Sobrien	  add_bound_info (subrange_die, DW_AT_lower_bound, lower);
1081750397Sobrien	  if (upper)
1081850397Sobrien	    add_bound_info (subrange_die, DW_AT_upper_bound, upper);
1081950397Sobrien	}
1082050397Sobrien
1082190075Sobrien      /* Otherwise we have an array type with an unspecified length.  The
1082290075Sobrien	 DWARF-2 spec does not say how to handle this; let's just leave out the
1082390075Sobrien	 bounds.  */
1082450397Sobrien    }
1082550397Sobrien}
1082650397Sobrien
1082750397Sobrienstatic void
10828132718Skanadd_byte_size_attribute (dw_die_ref die, tree tree_node)
1082950397Sobrien{
1083090075Sobrien  unsigned size;
1083150397Sobrien
1083250397Sobrien  switch (TREE_CODE (tree_node))
1083350397Sobrien    {
1083450397Sobrien    case ERROR_MARK:
1083550397Sobrien      size = 0;
1083650397Sobrien      break;
1083750397Sobrien    case ENUMERAL_TYPE:
1083850397Sobrien    case RECORD_TYPE:
1083950397Sobrien    case UNION_TYPE:
1084050397Sobrien    case QUAL_UNION_TYPE:
1084150397Sobrien      size = int_size_in_bytes (tree_node);
1084250397Sobrien      break;
1084350397Sobrien    case FIELD_DECL:
1084450397Sobrien      /* For a data member of a struct or union, the DW_AT_byte_size is
10845132718Skan	 generally given as the number of bytes normally allocated for an
10846132718Skan	 object of the *declared* type of the member itself.  This is true
10847132718Skan	 even for bit-fields.  */
1084850397Sobrien      size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
1084950397Sobrien      break;
1085050397Sobrien    default:
10851169689Skan      gcc_unreachable ();
1085250397Sobrien    }
1085350397Sobrien
1085450397Sobrien  /* Note that `size' might be -1 when we get to this point.  If it is, that
1085550397Sobrien     indicates that the byte size of the entity in question is variable.  We
10856245023Spfg     have no good way of expressing this fact in Dwarf at the present time.
10857245023Spfg     GCC/35998: Avoid passing negative sizes to Dtrace and gdb.  */
10858245023Spfg  add_AT_unsigned (die, DW_AT_byte_size, (size != (unsigned)-1 ? size : 0));
1085950397Sobrien}
1086050397Sobrien
1086150397Sobrien/* For a FIELD_DECL node which represents a bit-field, output an attribute
1086250397Sobrien   which specifies the distance in bits from the highest order bit of the
1086350397Sobrien   "containing object" for the bit-field to the highest order bit of the
1086450397Sobrien   bit-field itself.
1086550397Sobrien
1086690075Sobrien   For any given bit-field, the "containing object" is a hypothetical object
1086790075Sobrien   (of some integral or enum type) within which the given bit-field lives.  The
1086890075Sobrien   type of this hypothetical "containing object" is always the same as the
1086990075Sobrien   declared type of the individual bit-field itself.  The determination of the
1087090075Sobrien   exact location of the "containing object" for a bit-field is rather
1087190075Sobrien   complicated.  It's handled by the `field_byte_offset' function (above).
1087250397Sobrien
1087350397Sobrien   Note that it is the size (in bytes) of the hypothetical "containing object"
1087450397Sobrien   which will be given in the DW_AT_byte_size attribute for this bit-field.
1087550397Sobrien   (See `byte_size_attribute' above).  */
1087650397Sobrien
1087750397Sobrienstatic inline void
10878132718Skanadd_bit_offset_attribute (dw_die_ref die, tree decl)
1087950397Sobrien{
1088090075Sobrien  HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
1088190075Sobrien  tree type = DECL_BIT_FIELD_TYPE (decl);
1088290075Sobrien  HOST_WIDE_INT bitpos_int;
1088390075Sobrien  HOST_WIDE_INT highest_order_object_bit_offset;
1088490075Sobrien  HOST_WIDE_INT highest_order_field_bit_offset;
1088590075Sobrien  HOST_WIDE_INT unsigned bit_offset;
1088650397Sobrien
1088750397Sobrien  /* Must be a field and a bit field.  */
10888169689Skan  gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
1088950397Sobrien
1089050397Sobrien  /* We can't yet handle bit-fields whose offsets are variable, so if we
1089150397Sobrien     encounter such things, just return without generating any attribute
1089290075Sobrien     whatsoever.  Likewise for variable or too large size.  */
1089390075Sobrien  if (! host_integerp (bit_position (decl), 0)
1089490075Sobrien      || ! host_integerp (DECL_SIZE (decl), 1))
1089550397Sobrien    return;
1089650397Sobrien
1089790075Sobrien  bitpos_int = int_bit_position (decl);
1089850397Sobrien
1089950397Sobrien  /* Note that the bit offset is always the distance (in bits) from the
1090090075Sobrien     highest-order bit of the "containing object" to the highest-order bit of
1090190075Sobrien     the bit-field itself.  Since the "high-order end" of any object or field
1090250397Sobrien     is different on big-endian and little-endian machines, the computation
1090350397Sobrien     below must take account of these differences.  */
1090450397Sobrien  highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
1090550397Sobrien  highest_order_field_bit_offset = bitpos_int;
1090650397Sobrien
1090750397Sobrien  if (! BYTES_BIG_ENDIAN)
1090850397Sobrien    {
1090990075Sobrien      highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
1091050397Sobrien      highest_order_object_bit_offset += simple_type_size_in_bits (type);
1091150397Sobrien    }
1091250397Sobrien
1091350397Sobrien  bit_offset
1091450397Sobrien    = (! BYTES_BIG_ENDIAN
1091550397Sobrien       ? highest_order_object_bit_offset - highest_order_field_bit_offset
1091650397Sobrien       : highest_order_field_bit_offset - highest_order_object_bit_offset);
1091750397Sobrien
1091850397Sobrien  add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
1091950397Sobrien}
1092050397Sobrien
1092150397Sobrien/* For a FIELD_DECL node which represents a bit field, output an attribute
1092250397Sobrien   which specifies the length in bits of the given field.  */
1092350397Sobrien
1092450397Sobrienstatic inline void
10925132718Skanadd_bit_size_attribute (dw_die_ref die, tree decl)
1092650397Sobrien{
1092750397Sobrien  /* Must be a field and a bit field.  */
10928169689Skan  gcc_assert (TREE_CODE (decl) == FIELD_DECL
10929169689Skan	      && DECL_BIT_FIELD_TYPE (decl));
1093090075Sobrien
1093190075Sobrien  if (host_integerp (DECL_SIZE (decl), 1))
1093290075Sobrien    add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
1093350397Sobrien}
1093450397Sobrien
1093550397Sobrien/* If the compiled language is ANSI C, then add a 'prototyped'
1093650397Sobrien   attribute, if arg types are given for the parameters of a function.  */
1093750397Sobrien
1093850397Sobrienstatic inline void
10939132718Skanadd_prototyped_attribute (dw_die_ref die, tree func_type)
1094050397Sobrien{
1094150397Sobrien  if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
1094250397Sobrien      && TYPE_ARG_TYPES (func_type) != NULL)
1094350397Sobrien    add_AT_flag (die, DW_AT_prototyped, 1);
1094450397Sobrien}
1094550397Sobrien
1094650397Sobrien/* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
1094750397Sobrien   by looking in either the type declaration or object declaration
1094850397Sobrien   equate table.  */
1094950397Sobrien
1095050397Sobrienstatic inline void
10951132718Skanadd_abstract_origin_attribute (dw_die_ref die, tree origin)
1095250397Sobrien{
1095350397Sobrien  dw_die_ref origin_die = NULL;
1095490075Sobrien
1095590075Sobrien  if (TREE_CODE (origin) != FUNCTION_DECL)
1095690075Sobrien    {
1095790075Sobrien      /* We may have gotten separated from the block for the inlined
1095890075Sobrien	 function, if we're in an exception handler or some such; make
1095990075Sobrien	 sure that the abstract function has been written out.
1096090075Sobrien
10961132718Skan	 Doing this for nested functions is wrong, however; functions are
1096290075Sobrien	 distinct units, and our context might not even be inline.  */
1096390075Sobrien      tree fn = origin;
1096490075Sobrien
1096590075Sobrien      if (TYPE_P (fn))
1096690075Sobrien	fn = TYPE_STUB_DECL (fn);
10967169689Skan
1096890075Sobrien      fn = decl_function_context (fn);
1096990075Sobrien      if (fn)
1097090075Sobrien	dwarf2out_abstract_function (fn);
1097190075Sobrien    }
1097290075Sobrien
1097390075Sobrien  if (DECL_P (origin))
1097450397Sobrien    origin_die = lookup_decl_die (origin);
1097590075Sobrien  else if (TYPE_P (origin))
1097650397Sobrien    origin_die = lookup_type_die (origin);
1097750397Sobrien
10978169689Skan  /* XXX: Functions that are never lowered don't always have correct block
10979169689Skan     trees (in the case of java, they simply have no block tree, in some other
10980169689Skan     languages).  For these functions, there is nothing we can really do to
10981169689Skan     output correct debug info for inlined functions in all cases.  Rather
10982169689Skan     than die, we'll just produce deficient debug info now, in that we will
10983169689Skan     have variables without a proper abstract origin.  In the future, when all
10984169689Skan     functions are lowered, we should re-add a gcc_assert (origin_die)
10985169689Skan     here.  */
1098690075Sobrien
10987169689Skan  if (origin_die)
10988169689Skan      add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
1098950397Sobrien}
1099050397Sobrien
1099150397Sobrien/* We do not currently support the pure_virtual attribute.  */
1099250397Sobrien
1099350397Sobrienstatic inline void
10994132718Skanadd_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
1099550397Sobrien{
1099650397Sobrien  if (DECL_VINDEX (func_decl))
1099750397Sobrien    {
1099850397Sobrien      add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
1099950397Sobrien
1100090075Sobrien      if (host_integerp (DECL_VINDEX (func_decl), 0))
1100190075Sobrien	add_AT_loc (die, DW_AT_vtable_elem_location,
1100290075Sobrien		    new_loc_descr (DW_OP_constu,
1100390075Sobrien				   tree_low_cst (DECL_VINDEX (func_decl), 0),
1100490075Sobrien				   0));
1100590075Sobrien
1100650397Sobrien      /* GNU extension: Record what type this method came from originally.  */
1100750397Sobrien      if (debug_info_level > DINFO_LEVEL_TERSE)
1100850397Sobrien	add_AT_die_ref (die, DW_AT_containing_type,
1100950397Sobrien			lookup_type_die (DECL_CONTEXT (func_decl)));
1101050397Sobrien    }
1101150397Sobrien}
1101250397Sobrien
1101350397Sobrien/* Add source coordinate attributes for the given decl.  */
1101450397Sobrien
1101550397Sobrienstatic void
11016132718Skanadd_src_coords_attributes (dw_die_ref die, tree decl)
1101750397Sobrien{
11018169689Skan  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
1101950397Sobrien
11020169689Skan  add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11021169689Skan  add_AT_unsigned (die, DW_AT_decl_line, s.line);
1102250397Sobrien}
1102350397Sobrien
11024132718Skan/* Add a DW_AT_name attribute and source coordinate attribute for the
1102550397Sobrien   given decl, but only if it actually has a name.  */
1102650397Sobrien
1102750397Sobrienstatic void
11028132718Skanadd_name_and_src_coords_attributes (dw_die_ref die, tree decl)
1102950397Sobrien{
1103090075Sobrien  tree decl_name;
1103150397Sobrien
1103290075Sobrien  decl_name = DECL_NAME (decl);
1103350397Sobrien  if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
1103450397Sobrien    {
1103550397Sobrien      add_name_attribute (die, dwarf2_name (decl, 0));
1103690075Sobrien      if (! DECL_ARTIFICIAL (decl))
1103790075Sobrien	add_src_coords_attributes (die, decl);
1103890075Sobrien
1103950397Sobrien      if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
1104090075Sobrien	  && TREE_PUBLIC (decl)
1104190075Sobrien	  && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11042169689Skan	  && !DECL_ABSTRACT (decl)
11043169689Skan	  && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
1104450397Sobrien	add_AT_string (die, DW_AT_MIPS_linkage_name,
1104550397Sobrien		       IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1104650397Sobrien    }
1104790075Sobrien
1104890075Sobrien#ifdef VMS_DEBUGGING_INFO
1104990075Sobrien  /* Get the function's name, as described by its RTL.  This may be different
1105090075Sobrien     from the DECL_NAME name used in the source file.  */
1105190075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
1105290075Sobrien    {
1105390075Sobrien      add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
1105490075Sobrien		   XEXP (DECL_RTL (decl), 0));
11055169689Skan      VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
1105690075Sobrien    }
1105790075Sobrien#endif
1105850397Sobrien}
1105950397Sobrien
1106090075Sobrien/* Push a new declaration scope.  */
1106150397Sobrien
1106250397Sobrienstatic void
11063132718Skanpush_decl_scope (tree scope)
1106450397Sobrien{
11065169689Skan  VEC_safe_push (tree, gc, decl_scope_table, scope);
1106690075Sobrien}
1106750397Sobrien
1106890075Sobrien/* Pop a declaration scope.  */
1106950397Sobrien
1107090075Sobrienstatic inline void
11071132718Skanpop_decl_scope (void)
1107290075Sobrien{
11073169689Skan  VEC_pop (tree, decl_scope_table);
1107450397Sobrien}
1107550397Sobrien
1107690075Sobrien/* Return the DIE for the scope that immediately contains this type.
1107790075Sobrien   Non-named types get global scope.  Named types nested in other
1107890075Sobrien   types get their containing scope if it's open, or global scope
1107990075Sobrien   otherwise.  All other types (i.e. function-local named types) get
1108090075Sobrien   the current active scope.  */
1108150397Sobrien
1108250397Sobrienstatic dw_die_ref
11083132718Skanscope_die_for (tree t, dw_die_ref context_die)
1108450397Sobrien{
1108590075Sobrien  dw_die_ref scope_die = NULL;
1108690075Sobrien  tree containing_scope;
1108790075Sobrien  int i;
1108850397Sobrien
1108990075Sobrien  /* Non-types always go in the current scope.  */
11090169689Skan  gcc_assert (TYPE_P (t));
1109150397Sobrien
1109290075Sobrien  containing_scope = TYPE_CONTEXT (t);
1109390075Sobrien
11094132718Skan  /* Use the containing namespace if it was passed in (for a declaration).  */
1109550397Sobrien  if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11096132718Skan    {
11097132718Skan      if (context_die == lookup_decl_die (containing_scope))
11098132718Skan	/* OK */;
11099132718Skan      else
11100132718Skan	containing_scope = NULL_TREE;
11101132718Skan    }
1110250397Sobrien
1110352284Sobrien  /* Ignore function type "scopes" from the C frontend.  They mean that
1110452284Sobrien     a tagged type is local to a parmlist of a function declarator, but
1110552284Sobrien     that isn't useful to DWARF.  */
1110652284Sobrien  if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
1110752284Sobrien    containing_scope = NULL_TREE;
1110852284Sobrien
11109169689Skan  if (containing_scope == NULL_TREE)
1111050397Sobrien    scope_die = comp_unit_die;
1111190075Sobrien  else if (TYPE_P (containing_scope))
1111250397Sobrien    {
1111390075Sobrien      /* For types, we can just look up the appropriate DIE.  But
1111490075Sobrien	 first we check to see if we're in the middle of emitting it
1111590075Sobrien	 so we know where the new DIE should go.  */
11116169689Skan      for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11117169689Skan	if (VEC_index (tree, decl_scope_table, i) == containing_scope)
1111890075Sobrien	  break;
1111950397Sobrien
1112050397Sobrien      if (i < 0)
1112150397Sobrien	{
11122169689Skan	  gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11123169689Skan		      || TREE_ASM_WRITTEN (containing_scope));
1112452284Sobrien
1112552284Sobrien	  /* If none of the current dies are suitable, we get file scope.  */
1112652284Sobrien	  scope_die = comp_unit_die;
1112750397Sobrien	}
1112890075Sobrien      else
1112990075Sobrien	scope_die = lookup_type_die (containing_scope);
1113050397Sobrien    }
1113190075Sobrien  else
1113290075Sobrien    scope_die = context_die;
1113350397Sobrien
1113450397Sobrien  return scope_die;
1113550397Sobrien}
1113650397Sobrien
1113790075Sobrien/* Returns nonzero if CONTEXT_DIE is internal to a function.  */
1113890075Sobrien
1113990075Sobrienstatic inline int
11140132718Skanlocal_scope_p (dw_die_ref context_die)
1114150397Sobrien{
1114290075Sobrien  for (; context_die; context_die = context_die->die_parent)
1114390075Sobrien    if (context_die->die_tag == DW_TAG_inlined_subroutine
1114490075Sobrien	|| context_die->die_tag == DW_TAG_subprogram)
1114590075Sobrien      return 1;
1114690075Sobrien
1114790075Sobrien  return 0;
1114850397Sobrien}
1114950397Sobrien
11150132718Skan/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11151132718Skan   whether or not to treat a DIE in this context as a declaration.  */
1115290075Sobrien
1115390075Sobrienstatic inline int
11154132718Skanclass_or_namespace_scope_p (dw_die_ref context_die)
1115590075Sobrien{
1115690075Sobrien  return (context_die
1115790075Sobrien	  && (context_die->die_tag == DW_TAG_structure_type
11158132718Skan	      || context_die->die_tag == DW_TAG_union_type
11159132718Skan	      || context_die->die_tag == DW_TAG_namespace));
1116090075Sobrien}
1116190075Sobrien
1116250397Sobrien/* Many forms of DIEs require a "type description" attribute.  This
1116350397Sobrien   routine locates the proper "type descriptor" die for the type given
11164132718Skan   by 'type', and adds a DW_AT_type attribute below the given die.  */
1116550397Sobrien
1116650397Sobrienstatic void
11167132718Skanadd_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11168132718Skan		    int decl_volatile, dw_die_ref context_die)
1116950397Sobrien{
1117090075Sobrien  enum tree_code code  = TREE_CODE (type);
1117190075Sobrien  dw_die_ref type_die  = NULL;
1117250397Sobrien
1117350397Sobrien  /* ??? If this type is an unnamed subrange type of an integral or
1117450397Sobrien     floating-point type, use the inner type.  This is because we have no
1117550397Sobrien     support for unnamed types in base_type_die.  This can happen if this is
1117650397Sobrien     an Ada subrange type.  Correct solution is emit a subrange type die.  */
1117750397Sobrien  if ((code == INTEGER_TYPE || code == REAL_TYPE)
1117850397Sobrien      && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
1117950397Sobrien    type = TREE_TYPE (type), code = TREE_CODE (type);
1118050397Sobrien
1118190075Sobrien  if (code == ERROR_MARK
1118290075Sobrien      /* Handle a special case.  For functions whose return type is void, we
1118390075Sobrien	 generate *no* type attribute.  (Note that no object may have type
1118490075Sobrien	 `void', so this only applies to function return types).  */
1118590075Sobrien      || code == VOID_TYPE)
1118650397Sobrien    return;
1118750397Sobrien
1118850397Sobrien  type_die = modified_type_die (type,
1118950397Sobrien				decl_const || TYPE_READONLY (type),
1119050397Sobrien				decl_volatile || TYPE_VOLATILE (type),
1119150397Sobrien				context_die);
1119290075Sobrien
1119350397Sobrien  if (type_die != NULL)
1119450397Sobrien    add_AT_die_ref (object_die, DW_AT_type, type_die);
1119550397Sobrien}
1119650397Sobrien
11197169689Skan/* Given an object die, add the calling convention attribute for the
11198169689Skan   function call type.  */
11199169689Skanstatic void
11200169689Skanadd_calling_convention_attribute (dw_die_ref subr_die, tree type)
11201169689Skan{
11202169689Skan  enum dwarf_calling_convention value = DW_CC_normal;
11203169689Skan
11204169689Skan  value = targetm.dwarf_calling_convention (type);
11205169689Skan
11206169689Skan  /* Only add the attribute if the backend requests it, and
11207169689Skan     is not DW_CC_normal.  */
11208169689Skan  if (value && (value != DW_CC_normal))
11209169689Skan    add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11210169689Skan}
11211169689Skan
1121250397Sobrien/* Given a tree pointer to a struct, class, union, or enum type node, return
1121350397Sobrien   a pointer to the (string) tag name for the given type, or zero if the type
1121450397Sobrien   was declared without a tag.  */
1121550397Sobrien
1121690075Sobrienstatic const char *
11217132718Skantype_tag (tree type)
1121850397Sobrien{
1121990075Sobrien  const char *name = 0;
1122050397Sobrien
1122150397Sobrien  if (TYPE_NAME (type) != 0)
1122250397Sobrien    {
1122390075Sobrien      tree t = 0;
1122450397Sobrien
1122550397Sobrien      /* Find the IDENTIFIER_NODE for the type name.  */
1122650397Sobrien      if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1122750397Sobrien	t = TYPE_NAME (type);
1122850397Sobrien
1122990075Sobrien      /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11230132718Skan	 a TYPE_DECL node, regardless of whether or not a `typedef' was
11231132718Skan	 involved.  */
1123250397Sobrien      else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1123350397Sobrien	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
1123450397Sobrien	t = DECL_NAME (TYPE_NAME (type));
1123550397Sobrien
1123650397Sobrien      /* Now get the name as a string, or invent one.  */
1123750397Sobrien      if (t != 0)
1123850397Sobrien	name = IDENTIFIER_POINTER (t);
1123950397Sobrien    }
1124050397Sobrien
1124150397Sobrien  return (name == 0 || *name == '\0') ? 0 : name;
1124250397Sobrien}
1124350397Sobrien
1124450397Sobrien/* Return the type associated with a data member, make a special check
1124550397Sobrien   for bit field types.  */
1124650397Sobrien
1124750397Sobrienstatic inline tree
11248132718Skanmember_declared_type (tree member)
1124950397Sobrien{
1125050397Sobrien  return (DECL_BIT_FIELD_TYPE (member)
1125190075Sobrien	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
1125250397Sobrien}
1125350397Sobrien
1125450397Sobrien/* Get the decl's label, as described by its RTL. This may be different
1125550397Sobrien   from the DECL_NAME name used in the source file.  */
1125650397Sobrien
1125750397Sobrien#if 0
1125890075Sobrienstatic const char *
11259132718Skandecl_start_label (tree decl)
1126050397Sobrien{
1126150397Sobrien  rtx x;
1126290075Sobrien  const char *fnname;
1126390075Sobrien
1126450397Sobrien  x = DECL_RTL (decl);
11265169689Skan  gcc_assert (MEM_P (x));
1126650397Sobrien
1126750397Sobrien  x = XEXP (x, 0);
11268169689Skan  gcc_assert (GET_CODE (x) == SYMBOL_REF);
1126950397Sobrien
1127050397Sobrien  fnname = XSTR (x, 0);
1127150397Sobrien  return fnname;
1127250397Sobrien}
1127350397Sobrien#endif
1127450397Sobrien
1127550397Sobrien/* These routines generate the internal representation of the DIE's for
1127650397Sobrien   the compilation unit.  Debugging information is collected by walking
1127750397Sobrien   the declaration trees passed in from dwarf2out_decl().  */
1127850397Sobrien
1127950397Sobrienstatic void
11280132718Skangen_array_type_die (tree type, dw_die_ref context_die)
1128150397Sobrien{
1128290075Sobrien  dw_die_ref scope_die = scope_die_for (type, context_die);
1128390075Sobrien  dw_die_ref array_die;
1128490075Sobrien  tree element_type;
1128550397Sobrien
1128650397Sobrien  /* ??? The SGI dwarf reader fails for array of array of enum types unless
1128750397Sobrien     the inner array type comes before the outer array type.  Thus we must
1128850397Sobrien     call gen_type_die before we call new_die.  See below also.  */
1128950397Sobrien#ifdef MIPS_DEBUGGING_INFO
1129050397Sobrien  gen_type_die (TREE_TYPE (type), context_die);
1129150397Sobrien#endif
1129250397Sobrien
1129390075Sobrien  array_die = new_die (DW_TAG_array_type, scope_die, type);
1129496263Sobrien  add_name_attribute (array_die, type_tag (type));
1129596263Sobrien  equate_type_number_to_die (type, array_die);
1129650397Sobrien
1129796263Sobrien  if (TREE_CODE (type) == VECTOR_TYPE)
1129896263Sobrien    {
1129996263Sobrien      /* The frontend feeds us a representation for the vector as a struct
1130096263Sobrien	 containing an array.  Pull out the array type.  */
1130196263Sobrien      type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
1130296263Sobrien      add_AT_flag (array_die, DW_AT_GNU_vector, 1);
1130396263Sobrien    }
1130496263Sobrien
1130550397Sobrien#if 0
1130650397Sobrien  /* We default the array ordering.  SDB will probably do
1130750397Sobrien     the right things even if DW_AT_ordering is not present.  It's not even
1130850397Sobrien     an issue until we start to get into multidimensional arrays anyway.  If
1130950397Sobrien     SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
1131050397Sobrien     then we'll have to put the DW_AT_ordering attribute back in.  (But if
1131150397Sobrien     and when we find out that we need to put these in, we will only do so
1131250397Sobrien     for multidimensional arrays.  */
1131350397Sobrien  add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
1131450397Sobrien#endif
1131550397Sobrien
1131650397Sobrien#ifdef MIPS_DEBUGGING_INFO
1131750397Sobrien  /* The SGI compilers handle arrays of unknown bound by setting
1131850397Sobrien     AT_declaration and not emitting any subrange DIEs.  */
1131950397Sobrien  if (! TYPE_DOMAIN (type))
11320132718Skan    add_AT_flag (array_die, DW_AT_declaration, 1);
1132150397Sobrien  else
1132250397Sobrien#endif
1132350397Sobrien    add_subscript_info (array_die, type);
1132450397Sobrien
1132550397Sobrien  /* Add representation of the type of the elements of this array type.  */
1132650397Sobrien  element_type = TREE_TYPE (type);
1132750397Sobrien
1132850397Sobrien  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
1132950397Sobrien     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
1133050397Sobrien     We work around this by disabling this feature.  See also
1133150397Sobrien     add_subscript_info.  */
1133250397Sobrien#ifndef MIPS_DEBUGGING_INFO
1133350397Sobrien  while (TREE_CODE (element_type) == ARRAY_TYPE)
1133450397Sobrien    element_type = TREE_TYPE (element_type);
1133550397Sobrien
1133650397Sobrien  gen_type_die (element_type, context_die);
1133750397Sobrien#endif
1133850397Sobrien
1133950397Sobrien  add_type_attribute (array_die, element_type, 0, 0, context_die);
11340260396Spfg
11341260396Spfg  if (get_AT (array_die, DW_AT_name))
11342260396Spfg    add_pubtype (type, array_die);
1134350397Sobrien}
1134450397Sobrien
1134550397Sobrien#if 0
1134650397Sobrienstatic void
11347132718Skangen_entry_point_die (tree decl, dw_die_ref context_die)
1134850397Sobrien{
1134990075Sobrien  tree origin = decl_ultimate_origin (decl);
1135090075Sobrien  dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
1135190075Sobrien
1135250397Sobrien  if (origin != NULL)
1135350397Sobrien    add_abstract_origin_attribute (decl_die, origin);
1135450397Sobrien  else
1135550397Sobrien    {
1135650397Sobrien      add_name_and_src_coords_attributes (decl_die, decl);
1135750397Sobrien      add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
1135850397Sobrien			  0, 0, context_die);
1135950397Sobrien    }
1136050397Sobrien
1136150397Sobrien  if (DECL_ABSTRACT (decl))
1136250397Sobrien    equate_decl_number_to_die (decl, decl_die);
1136350397Sobrien  else
1136450397Sobrien    add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
1136550397Sobrien}
1136650397Sobrien#endif
1136750397Sobrien
1136852284Sobrien/* Walk through the list of incomplete types again, trying once more to
1136952284Sobrien   emit full debugging info for them.  */
1137052284Sobrien
1137152284Sobrienstatic void
11372132718Skanretry_incomplete_types (void)
1137352284Sobrien{
1137490075Sobrien  int i;
1137552284Sobrien
11376169689Skan  for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11377169689Skan    gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
1137852284Sobrien}
1137952284Sobrien
1138050397Sobrien/* Generate a DIE to represent an inlined instance of an enumeration type.  */
1138150397Sobrien
1138250397Sobrienstatic void
11383132718Skangen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
1138450397Sobrien{
1138590075Sobrien  dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
1138650397Sobrien
1138790075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1138890075Sobrien     be incomplete and such types are not marked.  */
1138950397Sobrien  add_abstract_origin_attribute (type_die, type);
1139050397Sobrien}
1139150397Sobrien
1139250397Sobrien/* Generate a DIE to represent an inlined instance of a structure type.  */
1139350397Sobrien
1139450397Sobrienstatic void
11395132718Skangen_inlined_structure_type_die (tree type, dw_die_ref context_die)
1139650397Sobrien{
1139790075Sobrien  dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
1139850397Sobrien
1139990075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1140090075Sobrien     be incomplete and such types are not marked.  */
1140150397Sobrien  add_abstract_origin_attribute (type_die, type);
1140250397Sobrien}
1140350397Sobrien
1140450397Sobrien/* Generate a DIE to represent an inlined instance of a union type.  */
1140550397Sobrien
1140650397Sobrienstatic void
11407132718Skangen_inlined_union_type_die (tree type, dw_die_ref context_die)
1140850397Sobrien{
1140990075Sobrien  dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
1141050397Sobrien
1141190075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1141290075Sobrien     be incomplete and such types are not marked.  */
1141350397Sobrien  add_abstract_origin_attribute (type_die, type);
1141450397Sobrien}
1141550397Sobrien
1141650397Sobrien/* Generate a DIE to represent an enumeration type.  Note that these DIEs
1141750397Sobrien   include all of the information about the enumeration values also. Each
1141850397Sobrien   enumerated type name/value is listed as a child of the enumerated type
1141950397Sobrien   DIE.  */
1142050397Sobrien
11421132718Skanstatic dw_die_ref
11422132718Skangen_enumeration_type_die (tree type, dw_die_ref context_die)
1142350397Sobrien{
1142490075Sobrien  dw_die_ref type_die = lookup_type_die (type);
1142550397Sobrien
1142650397Sobrien  if (type_die == NULL)
1142750397Sobrien    {
1142850397Sobrien      type_die = new_die (DW_TAG_enumeration_type,
1142990075Sobrien			  scope_die_for (type, context_die), type);
1143050397Sobrien      equate_type_number_to_die (type, type_die);
1143150397Sobrien      add_name_attribute (type_die, type_tag (type));
1143250397Sobrien    }
1143350397Sobrien  else if (! TYPE_SIZE (type))
11434132718Skan    return type_die;
1143550397Sobrien  else
1143650397Sobrien    remove_AT (type_die, DW_AT_declaration);
1143750397Sobrien
1143850397Sobrien  /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
1143950397Sobrien     given enum type is incomplete, do not generate the DW_AT_byte_size
1144050397Sobrien     attribute or the DW_AT_element_list attribute.  */
1144150397Sobrien  if (TYPE_SIZE (type))
1144250397Sobrien    {
1144390075Sobrien      tree link;
1144450397Sobrien
1144550397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1144650397Sobrien      add_byte_size_attribute (type_die, type);
1144750397Sobrien      if (TYPE_STUB_DECL (type) != NULL_TREE)
1144850397Sobrien	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
1144950397Sobrien
1145050397Sobrien      /* If the first reference to this type was as the return type of an
1145150397Sobrien	 inline function, then it may not have a parent.  Fix this now.  */
1145250397Sobrien      if (type_die->die_parent == NULL)
1145350397Sobrien	add_child_die (scope_die_for (type, context_die), type_die);
1145450397Sobrien
11455169689Skan      for (link = TYPE_VALUES (type);
1145650397Sobrien	   link != NULL; link = TREE_CHAIN (link))
1145750397Sobrien	{
1145890075Sobrien	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11459132718Skan	  tree value = TREE_VALUE (link);
1146050397Sobrien
1146150397Sobrien	  add_name_attribute (enum_die,
1146250397Sobrien			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
1146390075Sobrien
11464169689Skan	  if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11465132718Skan	    /* DWARF2 does not provide a way of indicating whether or
11466132718Skan	       not enumeration constants are signed or unsigned.  GDB
11467132718Skan	       always assumes the values are signed, so we output all
11468132718Skan	       values as if they were signed.  That means that
11469132718Skan	       enumeration constants with very large unsigned values
11470132718Skan	       will appear to have negative values in the debugger.  */
11471132718Skan	    add_AT_int (enum_die, DW_AT_const_value,
11472132718Skan			tree_low_cst (value, tree_int_cst_sgn (value) > 0));
1147350397Sobrien	}
1147450397Sobrien    }
1147550397Sobrien  else
1147650397Sobrien    add_AT_flag (type_die, DW_AT_declaration, 1);
11477132718Skan
11478260396Spfg  if (get_AT (type_die, DW_AT_name))
11479260396Spfg    add_pubtype (type, type_die);
11480260396Spfg
11481132718Skan  return type_die;
1148250397Sobrien}
1148350397Sobrien
1148450397Sobrien/* Generate a DIE to represent either a real live formal parameter decl or to
1148550397Sobrien   represent just the type of some formal parameter position in some function
1148650397Sobrien   type.
1148750397Sobrien
1148850397Sobrien   Note that this routine is a bit unusual because its argument may be a
1148950397Sobrien   ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
1149050397Sobrien   represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
1149150397Sobrien   node.  If it's the former then this function is being called to output a
1149250397Sobrien   DIE to represent a formal parameter object (or some inlining thereof).  If
1149350397Sobrien   it's the latter, then this function is only being called to output a
1149450397Sobrien   DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
1149550397Sobrien   argument type of some subprogram type.  */
1149650397Sobrien
1149750397Sobrienstatic dw_die_ref
11498132718Skangen_formal_parameter_die (tree node, dw_die_ref context_die)
1149950397Sobrien{
1150090075Sobrien  dw_die_ref parm_die
1150190075Sobrien    = new_die (DW_TAG_formal_parameter, context_die, node);
1150290075Sobrien  tree origin;
1150350397Sobrien
1150450397Sobrien  switch (TREE_CODE_CLASS (TREE_CODE (node)))
1150550397Sobrien    {
11506169689Skan    case tcc_declaration:
1150750397Sobrien      origin = decl_ultimate_origin (node);
1150850397Sobrien      if (origin != NULL)
1150950397Sobrien	add_abstract_origin_attribute (parm_die, origin);
1151050397Sobrien      else
1151150397Sobrien	{
1151250397Sobrien	  add_name_and_src_coords_attributes (parm_die, node);
1151350397Sobrien	  add_type_attribute (parm_die, TREE_TYPE (node),
1151450397Sobrien			      TREE_READONLY (node),
1151550397Sobrien			      TREE_THIS_VOLATILE (node),
1151650397Sobrien			      context_die);
1151750397Sobrien	  if (DECL_ARTIFICIAL (node))
1151850397Sobrien	    add_AT_flag (parm_die, DW_AT_artificial, 1);
1151950397Sobrien	}
1152050397Sobrien
1152150397Sobrien      equate_decl_number_to_die (node, parm_die);
1152250397Sobrien      if (! DECL_ABSTRACT (node))
11523169689Skan	add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
1152450397Sobrien
1152550397Sobrien      break;
1152650397Sobrien
11527169689Skan    case tcc_type:
1152850397Sobrien      /* We were called with some kind of a ..._TYPE node.  */
1152950397Sobrien      add_type_attribute (parm_die, node, 0, 0, context_die);
1153050397Sobrien      break;
1153150397Sobrien
1153250397Sobrien    default:
11533169689Skan      gcc_unreachable ();
1153450397Sobrien    }
1153550397Sobrien
1153650397Sobrien  return parm_die;
1153750397Sobrien}
1153850397Sobrien
1153950397Sobrien/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
1154050397Sobrien   at the end of an (ANSI prototyped) formal parameters list.  */
1154150397Sobrien
1154250397Sobrienstatic void
11543132718Skangen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
1154450397Sobrien{
1154590075Sobrien  new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
1154650397Sobrien}
1154750397Sobrien
1154850397Sobrien/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
1154950397Sobrien   DW_TAG_unspecified_parameters DIE) to represent the types of the formal
1155050397Sobrien   parameters as specified in some function type specification (except for
1155190075Sobrien   those which appear as part of a function *definition*).  */
1155250397Sobrien
1155350397Sobrienstatic void
11554132718Skangen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
1155550397Sobrien{
1155690075Sobrien  tree link;
1155790075Sobrien  tree formal_type = NULL;
1155890075Sobrien  tree first_parm_type;
1155990075Sobrien  tree arg;
1156050397Sobrien
1156190075Sobrien  if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
1156290075Sobrien    {
1156390075Sobrien      arg = DECL_ARGUMENTS (function_or_method_type);
1156490075Sobrien      function_or_method_type = TREE_TYPE (function_or_method_type);
1156590075Sobrien    }
1156690075Sobrien  else
1156790075Sobrien    arg = NULL_TREE;
11568117395Skan
1156990075Sobrien  first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
1157050397Sobrien
1157190075Sobrien  /* Make our first pass over the list of formal parameter types and output a
1157250397Sobrien     DW_TAG_formal_parameter DIE for each one.  */
1157390075Sobrien  for (link = first_parm_type; link; )
1157450397Sobrien    {
1157590075Sobrien      dw_die_ref parm_die;
1157690075Sobrien
1157750397Sobrien      formal_type = TREE_VALUE (link);
1157850397Sobrien      if (formal_type == void_type_node)
1157950397Sobrien	break;
1158050397Sobrien
1158150397Sobrien      /* Output a (nameless) DIE to represent the formal parameter itself.  */
1158250397Sobrien      parm_die = gen_formal_parameter_die (formal_type, context_die);
1158390075Sobrien      if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
1158490075Sobrien	   && link == first_parm_type)
1158590075Sobrien	  || (arg && DECL_ARTIFICIAL (arg)))
1158650397Sobrien	add_AT_flag (parm_die, DW_AT_artificial, 1);
1158790075Sobrien
1158890075Sobrien      link = TREE_CHAIN (link);
1158990075Sobrien      if (arg)
1159090075Sobrien	arg = TREE_CHAIN (arg);
1159150397Sobrien    }
1159250397Sobrien
1159350397Sobrien  /* If this function type has an ellipsis, add a
1159450397Sobrien     DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
1159550397Sobrien  if (formal_type != void_type_node)
1159650397Sobrien    gen_unspecified_parameters_die (function_or_method_type, context_die);
1159750397Sobrien
1159890075Sobrien  /* Make our second (and final) pass over the list of formal parameter types
1159950397Sobrien     and output DIEs to represent those types (as necessary).  */
1160050397Sobrien  for (link = TYPE_ARG_TYPES (function_or_method_type);
1160190075Sobrien       link && TREE_VALUE (link);
1160250397Sobrien       link = TREE_CHAIN (link))
1160390075Sobrien    gen_type_die (TREE_VALUE (link), context_die);
1160490075Sobrien}
1160590075Sobrien
1160690075Sobrien/* We want to generate the DIE for TYPE so that we can generate the
1160790075Sobrien   die for MEMBER, which has been defined; we will need to refer back
1160890075Sobrien   to the member declaration nested within TYPE.  If we're trying to
1160990075Sobrien   generate minimal debug info for TYPE, processing TYPE won't do the
1161090075Sobrien   trick; we need to attach the member declaration by hand.  */
1161190075Sobrien
1161290075Sobrienstatic void
11613132718Skangen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
1161490075Sobrien{
1161590075Sobrien  gen_type_die (type, context_die);
1161690075Sobrien
1161790075Sobrien  /* If we're trying to avoid duplicate debug info, we may not have
1161890075Sobrien     emitted the member decl for this function.  Emit it now.  */
1161990075Sobrien  if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
1162090075Sobrien      && ! lookup_decl_die (member))
1162150397Sobrien    {
11622169689Skan      dw_die_ref type_die;
11623169689Skan      gcc_assert (!decl_ultimate_origin (member));
1162450397Sobrien
1162590075Sobrien      push_decl_scope (type);
11626169689Skan      type_die = lookup_type_die (type);
1162790075Sobrien      if (TREE_CODE (member) == FUNCTION_DECL)
11628169689Skan	gen_subprogram_die (member, type_die);
11629169689Skan      else if (TREE_CODE (member) == FIELD_DECL)
11630169689Skan	{
11631169689Skan	  /* Ignore the nameless fields that are used to skip bits but handle
11632169689Skan	     C++ anonymous unions and structs.  */
11633169689Skan	  if (DECL_NAME (member) != NULL_TREE
11634169689Skan	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11635169689Skan	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11636169689Skan	    {
11637169689Skan	      gen_type_die (member_declared_type (member), type_die);
11638169689Skan	      gen_field_die (member, type_die);
11639169689Skan	    }
11640169689Skan	}
1164190075Sobrien      else
11642169689Skan	gen_variable_die (member, type_die);
1164390075Sobrien
1164490075Sobrien      pop_decl_scope ();
1164550397Sobrien    }
1164650397Sobrien}
1164750397Sobrien
1164890075Sobrien/* Generate the DWARF2 info for the "abstract" instance of a function which we
1164990075Sobrien   may later generate inlined and/or out-of-line instances of.  */
1165090075Sobrien
1165190075Sobrienstatic void
11652132718Skandwarf2out_abstract_function (tree decl)
1165390075Sobrien{
1165490075Sobrien  dw_die_ref old_die;
1165590075Sobrien  tree save_fn;
11656169689Skan  struct function *save_cfun;
1165790075Sobrien  tree context;
1165890075Sobrien  int was_abstract = DECL_ABSTRACT (decl);
1165990075Sobrien
1166090075Sobrien  /* Make sure we have the actual abstract inline, not a clone.  */
1166190075Sobrien  decl = DECL_ORIGIN (decl);
1166290075Sobrien
11663117395Skan  old_die = lookup_decl_die (decl);
11664132718Skan  if (old_die && get_AT (old_die, DW_AT_inline))
1166590075Sobrien    /* We've already generated the abstract instance.  */
1166690075Sobrien    return;
1166790075Sobrien
1166890075Sobrien  /* Be sure we've emitted the in-class declaration DIE (if any) first, so
1166990075Sobrien     we don't get confused by DECL_ABSTRACT.  */
1167090075Sobrien  if (debug_info_level > DINFO_LEVEL_TERSE)
1167190075Sobrien    {
1167290075Sobrien      context = decl_class_context (decl);
1167390075Sobrien      if (context)
1167490075Sobrien	gen_type_die_for_member
1167590075Sobrien	  (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
1167690075Sobrien    }
11677117395Skan
1167890075Sobrien  /* Pretend we've just finished compiling this function.  */
1167990075Sobrien  save_fn = current_function_decl;
11680169689Skan  save_cfun = cfun;
1168190075Sobrien  current_function_decl = decl;
11682169689Skan  cfun = DECL_STRUCT_FUNCTION (decl);
1168390075Sobrien
1168490075Sobrien  set_decl_abstract_flags (decl, 1);
1168590075Sobrien  dwarf2out_decl (decl);
1168690075Sobrien  if (! was_abstract)
1168790075Sobrien    set_decl_abstract_flags (decl, 0);
1168890075Sobrien
1168990075Sobrien  current_function_decl = save_fn;
11690169689Skan  cfun = save_cfun;
1169190075Sobrien}
1169290075Sobrien
11693169689Skan/* Helper function of premark_used_types() which gets called through
11694169689Skan   htab_traverse_resize().
11695169689Skan
11696169689Skan   Marks the DIE of a given type in *SLOT as perennial, so it never gets
11697169689Skan   marked as unused by prune_unused_types.  */
11698169689Skanstatic int
11699169689Skanpremark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11700169689Skan{
11701169689Skan  tree type;
11702169689Skan  dw_die_ref die;
11703169689Skan
11704169689Skan  type = *slot;
11705169689Skan  die = lookup_type_die (type);
11706169689Skan  if (die != NULL)
11707169689Skan    die->die_perennial_p = 1;
11708169689Skan  return 1;
11709169689Skan}
11710169689Skan
11711169689Skan/* Mark all members of used_types_hash as perennial.  */
11712169689Skanstatic void
11713169689Skanpremark_used_types (void)
11714169689Skan{
11715169689Skan  if (cfun && cfun->used_types_hash)
11716169689Skan    htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11717169689Skan}
11718169689Skan
1171950397Sobrien/* Generate a DIE to represent a declared function (either file-scope or
1172050397Sobrien   block-local).  */
1172150397Sobrien
1172250397Sobrienstatic void
11723132718Skangen_subprogram_die (tree decl, dw_die_ref context_die)
1172450397Sobrien{
1172550397Sobrien  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
1172690075Sobrien  tree origin = decl_ultimate_origin (decl);
1172790075Sobrien  dw_die_ref subr_die;
1172890075Sobrien  tree fn_arg_types;
1172990075Sobrien  tree outer_scope;
1173090075Sobrien  dw_die_ref old_die = lookup_decl_die (decl);
1173190075Sobrien  int declaration = (current_function_decl != decl
11732132718Skan		     || class_or_namespace_scope_p (context_die));
1173350397Sobrien
11734169689Skan  premark_used_types ();
11735169689Skan
1173690075Sobrien  /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
1173790075Sobrien     started to generate the abstract instance of an inline, decided to output
1173890075Sobrien     its containing class, and proceeded to emit the declaration of the inline
1173990075Sobrien     from the member list for the class.  If so, DECLARATION takes priority;
1174090075Sobrien     we'll get back to the abstract instance when done with the class.  */
1174190075Sobrien
1174290075Sobrien  /* The class-scope declaration DIE must be the primary DIE.  */
11743132718Skan  if (origin && declaration && class_or_namespace_scope_p (context_die))
1174450397Sobrien    {
1174590075Sobrien      origin = NULL;
11746169689Skan      gcc_assert (!old_die);
1174750397Sobrien    }
1174890075Sobrien
11749169689Skan  /* Now that the C++ front end lazily declares artificial member fns, we
11750169689Skan     might need to retrofit the declaration into its class.  */
11751169689Skan  if (!declaration && !origin && !old_die
11752169689Skan      && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11753169689Skan      && !class_or_namespace_scope_p (context_die)
11754169689Skan      && debug_info_level > DINFO_LEVEL_TERSE)
11755169689Skan    old_die = force_decl_die (decl);
11756169689Skan
1175790075Sobrien  if (origin != NULL)
1175850397Sobrien    {
11759169689Skan      gcc_assert (!declaration || local_scope_p (context_die));
1176050397Sobrien
1176190075Sobrien      /* Fixup die_parent for the abstract instance of a nested
1176290075Sobrien	 inline function.  */
1176390075Sobrien      if (old_die && old_die->die_parent == NULL)
1176490075Sobrien	add_child_die (context_die, old_die);
1176590075Sobrien
1176690075Sobrien      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
1176790075Sobrien      add_abstract_origin_attribute (subr_die, origin);
1176850397Sobrien    }
1176950397Sobrien  else if (old_die)
1177050397Sobrien    {
11771169689Skan      expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11772169689Skan      struct dwarf_file_data * file_index = lookup_filename (s.file);
1177350397Sobrien
1177490075Sobrien      if (!get_AT_flag (old_die, DW_AT_declaration)
1177590075Sobrien	  /* We can have a normal definition following an inline one in the
1177690075Sobrien	     case of redefinition of GNU C extern inlines.
1177790075Sobrien	     It seems reasonable to use AT_specification in this case.  */
11778132718Skan	  && !get_AT (old_die, DW_AT_inline))
1177952284Sobrien	{
11780169689Skan	  /* Detect and ignore this case, where we are trying to output
11781169689Skan	     something we have already output.  */
11782169689Skan	  return;
1178352284Sobrien	}
1178450397Sobrien
1178550397Sobrien      /* If the definition comes from the same place as the declaration,
1178650397Sobrien	 maybe use the old DIE.  We always want the DIE for this function
1178750397Sobrien	 that has the *_pc attributes to be under comp_unit_die so the
1178890075Sobrien	 debugger can find it.  We also need to do this for abstract
1178990075Sobrien	 instances of inlines, since the spec requires the out-of-line copy
1179090075Sobrien	 to have the same parent.  For local class methods, this doesn't
1179190075Sobrien	 apply; we just use the old DIE.  */
1179290075Sobrien      if ((old_die->die_parent == comp_unit_die || context_die == NULL)
1179390075Sobrien	  && (DECL_ARTIFICIAL (decl)
11794169689Skan	      || (get_AT_file (old_die, DW_AT_decl_file) == file_index
1179590075Sobrien		  && (get_AT_unsigned (old_die, DW_AT_decl_line)
11796169689Skan		      == (unsigned) s.line))))
1179750397Sobrien	{
1179850397Sobrien	  subr_die = old_die;
1179950397Sobrien
11800169689Skan	  /* Clear out the declaration attribute and the formal parameters.
11801169689Skan	     Do not remove all children, because it is possible that this
11802169689Skan	     declaration die was forced using force_decl_die(). In such
11803169689Skan	     cases die that forced declaration die (e.g. TAG_imported_module)
11804169689Skan	     is one of the children that we do not want to remove.  */
1180550397Sobrien	  remove_AT (subr_die, DW_AT_declaration);
11806146895Skan	  remove_child_TAG (subr_die, DW_TAG_formal_parameter);
1180750397Sobrien	}
1180850397Sobrien      else
1180950397Sobrien	{
1181090075Sobrien	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11811132718Skan	  add_AT_specification (subr_die, old_die);
11812169689Skan	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11813169689Skan	    add_AT_file (subr_die, DW_AT_decl_file, file_index);
11814169689Skan	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11815169689Skan	    add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
1181650397Sobrien	}
1181750397Sobrien    }
1181850397Sobrien  else
1181950397Sobrien    {
1182090075Sobrien      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
1182150397Sobrien
1182250397Sobrien      if (TREE_PUBLIC (decl))
1182350397Sobrien	add_AT_flag (subr_die, DW_AT_external, 1);
1182450397Sobrien
1182550397Sobrien      add_name_and_src_coords_attributes (subr_die, decl);
1182650397Sobrien      if (debug_info_level > DINFO_LEVEL_TERSE)
1182750397Sobrien	{
1182890075Sobrien	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
1182990075Sobrien	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
1183090075Sobrien			      0, 0, context_die);
1183150397Sobrien	}
1183250397Sobrien
1183350397Sobrien      add_pure_or_virtual_attribute (subr_die, decl);
1183450397Sobrien      if (DECL_ARTIFICIAL (decl))
1183550397Sobrien	add_AT_flag (subr_die, DW_AT_artificial, 1);
1183690075Sobrien
1183750397Sobrien      if (TREE_PROTECTED (decl))
1183850397Sobrien	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
1183950397Sobrien      else if (TREE_PRIVATE (decl))
1184050397Sobrien	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
1184150397Sobrien    }
1184250397Sobrien
1184350397Sobrien  if (declaration)
1184450397Sobrien    {
11845132718Skan      if (!old_die || !get_AT (old_die, DW_AT_inline))
1184690075Sobrien	{
1184790075Sobrien	  add_AT_flag (subr_die, DW_AT_declaration, 1);
1184850397Sobrien
1184990075Sobrien	  /* The first time we see a member function, it is in the context of
1185090075Sobrien	     the class to which it belongs.  We make sure of this by emitting
1185190075Sobrien	     the class first.  The next time is the definition, which is
11852169689Skan	     handled above.  The two may come from the same source text.
11853169689Skan
11854169689Skan	     Note that force_decl_die() forces function declaration die. It is
11855169689Skan	     later reused to represent definition.  */
11856169689Skan	  equate_decl_number_to_die (decl, subr_die);
1185790075Sobrien	}
1185850397Sobrien    }
1185950397Sobrien  else if (DECL_ABSTRACT (decl))
1186050397Sobrien    {
11861132718Skan      if (DECL_DECLARED_INLINE_P (decl))
1186250397Sobrien	{
11863132718Skan          if (cgraph_function_possibly_inlined_p (decl))
1186450397Sobrien	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
1186550397Sobrien	  else
11866132718Skan	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
1186750397Sobrien	}
1186850397Sobrien      else
11869132718Skan	{
11870132718Skan	  if (cgraph_function_possibly_inlined_p (decl))
11871132718Skan            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11872132718Skan	  else
11873132718Skan            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11874132718Skan	}
1187550397Sobrien
1187650397Sobrien      equate_decl_number_to_die (decl, subr_die);
1187750397Sobrien    }
1187850397Sobrien  else if (!DECL_EXTERNAL (decl))
1187950397Sobrien    {
11880169689Skan      HOST_WIDE_INT cfa_fb_offset;
11881169689Skan
11882132718Skan      if (!old_die || !get_AT (old_die, DW_AT_inline))
1188350397Sobrien	equate_decl_number_to_die (decl, subr_die);
1188450397Sobrien
11885169689Skan      if (!flag_reorder_blocks_and_partition)
11886169689Skan	{
11887169689Skan	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11888169689Skan				       current_function_funcdef_no);
11889169689Skan	  add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11890169689Skan	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11891169689Skan				       current_function_funcdef_no);
11892169689Skan	  add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11893169689Skan
11894169689Skan	  add_pubname (decl, subr_die);
11895169689Skan	  add_arange (decl, subr_die);
11896169689Skan	}
11897169689Skan      else
11898169689Skan	{  /* Do nothing for now; maybe need to duplicate die, one for
11899169689Skan	      hot section and ond for cold section, then use the hot/cold
11900169689Skan	      section begin/end labels to generate the aranges...  */
11901169689Skan	  /*
11902169689Skan	    add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11903169689Skan	    add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11904169689Skan	    add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11905169689Skan	    add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
1190650397Sobrien
11907169689Skan	    add_pubname (decl, subr_die);
11908169689Skan	    add_arange (decl, subr_die);
11909169689Skan	    add_arange (decl, subr_die);
11910169689Skan	   */
11911169689Skan	}
1191250397Sobrien
1191350397Sobrien#ifdef MIPS_DEBUGGING_INFO
1191450397Sobrien      /* Add a reference to the FDE for this routine.  */
1191550397Sobrien      add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
1191650397Sobrien#endif
1191750397Sobrien
11918169689Skan      cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
1191950397Sobrien
11920169689Skan      /* We define the "frame base" as the function's CFA.  This is more
11921169689Skan	 convenient for several reasons: (1) It's stable across the prologue
11922169689Skan	 and epilogue, which makes it better than just a frame pointer,
11923169689Skan	 (2) With dwarf3, there exists a one-byte encoding that allows us
11924169689Skan	 to reference the .debug_frame data by proxy, but failing that,
11925169689Skan	 (3) We can at least reuse the code inspection and interpretation
11926169689Skan	 code that determines the CFA position at various points in the
11927169689Skan	 function.  */
11928169689Skan      /* ??? Use some command-line or configury switch to enable the use
11929169689Skan	 of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11930169689Skan	 consumers that understand it; fall back to "pure" dwarf2 and
11931169689Skan	 convert the CFA data into a location list.  */
11932169689Skan      {
11933169689Skan	dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11934169689Skan	if (list->dw_loc_next)
11935169689Skan	  add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11936169689Skan	else
11937169689Skan	  add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11938169689Skan      }
11939169689Skan
11940169689Skan      /* Compute a displacement from the "steady-state frame pointer" to
11941169689Skan	 the CFA.  The former is what all stack slots and argument slots
11942169689Skan	 will reference in the rtl; the later is what we've told the
11943169689Skan	 debugger about.  We'll need to adjust all frame_base references
11944169689Skan	 by this displacement.  */
11945169689Skan      compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11946169689Skan
11947169689Skan      if (cfun->static_chain_decl)
1194850397Sobrien	add_AT_location_description (subr_die, DW_AT_static_link,
11949169689Skan		 loc_descriptor_from_tree (cfun->static_chain_decl));
1195050397Sobrien    }
1195150397Sobrien
1195250397Sobrien  /* Now output descriptions of the arguments for this function. This gets
1195390075Sobrien     (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
1195450397Sobrien     for a FUNCTION_DECL doesn't indicate cases where there was a trailing
1195550397Sobrien     `...' at the end of the formal parameter list.  In order to find out if
1195650397Sobrien     there was a trailing ellipsis or not, we must instead look at the type
1195750397Sobrien     associated with the FUNCTION_DECL.  This will be a node of type
1195850397Sobrien     FUNCTION_TYPE. If the chain of type nodes hanging off of this
1195990075Sobrien     FUNCTION_TYPE node ends with a void_type_node then there should *not* be
1196050397Sobrien     an ellipsis at the end.  */
1196150397Sobrien
1196250397Sobrien  /* In the case where we are describing a mere function declaration, all we
1196390075Sobrien     need to do here (and all we *can* do here) is to describe the *types* of
1196450397Sobrien     its formal parameters.  */
1196550397Sobrien  if (debug_info_level <= DINFO_LEVEL_TERSE)
1196650397Sobrien    ;
1196750397Sobrien  else if (declaration)
1196890075Sobrien    gen_formal_types_die (decl, subr_die);
1196950397Sobrien  else
1197050397Sobrien    {
11971132718Skan      /* Generate DIEs to represent all known formal parameters.  */
1197290075Sobrien      tree arg_decls = DECL_ARGUMENTS (decl);
1197390075Sobrien      tree parm;
1197450397Sobrien
1197550397Sobrien      /* When generating DIEs, generate the unspecified_parameters DIE
11976132718Skan	 instead if we come across the arg "__builtin_va_alist" */
1197750397Sobrien      for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
1197850397Sobrien	if (TREE_CODE (parm) == PARM_DECL)
1197950397Sobrien	  {
1198050397Sobrien	    if (DECL_NAME (parm)
1198150397Sobrien		&& !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
1198250397Sobrien			    "__builtin_va_alist"))
1198350397Sobrien	      gen_unspecified_parameters_die (parm, subr_die);
1198450397Sobrien	    else
1198550397Sobrien	      gen_decl_die (parm, subr_die);
1198650397Sobrien	  }
1198750397Sobrien
1198890075Sobrien      /* Decide whether we need an unspecified_parameters DIE at the end.
11989132718Skan	 There are 2 more cases to do this for: 1) the ansi ... declaration -
11990132718Skan	 this is detectable when the end of the arg list is not a
11991132718Skan	 void_type_node 2) an unprototyped function declaration (not a
11992132718Skan	 definition).  This just means that we have no info about the
11993132718Skan	 parameters at all.  */
1199450397Sobrien      fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1199550397Sobrien      if (fn_arg_types != NULL)
1199650397Sobrien	{
11997132718Skan	  /* This is the prototyped case, check for....  */
1199850397Sobrien	  if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
1199950397Sobrien	    gen_unspecified_parameters_die (decl, subr_die);
1200050397Sobrien	}
1200150397Sobrien      else if (DECL_INITIAL (decl) == NULL_TREE)
1200250397Sobrien	gen_unspecified_parameters_die (decl, subr_die);
1200350397Sobrien    }
1200450397Sobrien
1200550397Sobrien  /* Output Dwarf info for all of the stuff within the body of the function
1200650397Sobrien     (if it has one - it may be just a declaration).  */
1200750397Sobrien  outer_scope = DECL_INITIAL (decl);
1200850397Sobrien
1200990075Sobrien  /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
1201090075Sobrien     a function.  This BLOCK actually represents the outermost binding contour
1201190075Sobrien     for the function, i.e. the contour in which the function's formal
1201290075Sobrien     parameters and labels get declared. Curiously, it appears that the front
1201390075Sobrien     end doesn't actually put the PARM_DECL nodes for the current function onto
1201490075Sobrien     the BLOCK_VARS list for this outer scope, but are strung off of the
1201590075Sobrien     DECL_ARGUMENTS list for the function instead.
1201690075Sobrien
1201790075Sobrien     The BLOCK_VARS list for the `outer_scope' does provide us with a list of
1201890075Sobrien     the LABEL_DECL nodes for the function however, and we output DWARF info
1201990075Sobrien     for those in decls_for_scope.  Just within the `outer_scope' there will be
1202090075Sobrien     a BLOCK node representing the function's outermost pair of curly braces,
1202190075Sobrien     and any blocks used for the base and member initializers of a C++
1202250397Sobrien     constructor function.  */
1202350397Sobrien  if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
1202450397Sobrien    {
12025169689Skan      /* Emit a DW_TAG_variable DIE for a named return value.  */
12026169689Skan      if (DECL_NAME (DECL_RESULT (decl)))
12027169689Skan	gen_decl_die (DECL_RESULT (decl), subr_die);
12028169689Skan
1202950397Sobrien      current_function_has_inlines = 0;
1203050397Sobrien      decls_for_scope (outer_scope, subr_die, 0);
1203150397Sobrien
1203250397Sobrien#if 0 && defined (MIPS_DEBUGGING_INFO)
1203350397Sobrien      if (current_function_has_inlines)
1203450397Sobrien	{
1203550397Sobrien	  add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
1203650397Sobrien	  if (! comp_unit_has_inlines)
1203750397Sobrien	    {
1203850397Sobrien	      add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
1203950397Sobrien	      comp_unit_has_inlines = 1;
1204050397Sobrien	    }
1204150397Sobrien	}
1204250397Sobrien#endif
1204350397Sobrien    }
12044169689Skan  /* Add the calling convention attribute if requested.  */
12045169689Skan  add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12046169689Skan
1204750397Sobrien}
1204850397Sobrien
1204950397Sobrien/* Generate a DIE to represent a declared data object.  */
1205050397Sobrien
1205150397Sobrienstatic void
12052132718Skangen_variable_die (tree decl, dw_die_ref context_die)
1205350397Sobrien{
1205490075Sobrien  tree origin = decl_ultimate_origin (decl);
1205590075Sobrien  dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
1205650397Sobrien
1205750397Sobrien  dw_die_ref old_die = lookup_decl_die (decl);
1205890075Sobrien  int declaration = (DECL_EXTERNAL (decl)
12059169689Skan		     /* If DECL is COMDAT and has not actually been
12060169689Skan			emitted, we cannot take its address; there
12061169689Skan			might end up being no definition anywhere in
12062169689Skan			the program.  For example, consider the C++
12063169689Skan			test case:
12064169689Skan
12065169689Skan                          template <class T>
12066169689Skan                          struct S { static const int i = 7; };
12067169689Skan
12068169689Skan                          template <class T>
12069169689Skan                          const int S<T>::i;
12070169689Skan
12071169689Skan                          int f() { return S<int>::i; }
12072169689Skan
12073169689Skan			Here, S<int>::i is not DECL_EXTERNAL, but no
12074169689Skan			definition is required, so the compiler will
12075169689Skan			not emit a definition.  */
12076169689Skan		     || (TREE_CODE (decl) == VAR_DECL
12077169689Skan			 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12078132718Skan		     || class_or_namespace_scope_p (context_die));
1207950397Sobrien
1208050397Sobrien  if (origin != NULL)
1208150397Sobrien    add_abstract_origin_attribute (var_die, origin);
1208290075Sobrien
1208350397Sobrien  /* Loop unrolling can create multiple blocks that refer to the same
1208490075Sobrien     static variable, so we must test for the DW_AT_declaration flag.
1208590075Sobrien
1208690075Sobrien     ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
1208750397Sobrien     copy decls and set the DECL_ABSTRACT flag on them instead of
1208890075Sobrien     sharing them.
1208990075Sobrien
12090169689Skan     ??? Duplicated blocks have been rewritten to use .debug_ranges.
12091169689Skan
12092169689Skan     ??? The declare_in_namespace support causes us to get two DIEs for one
12093169689Skan     variable, both of which are declarations.  We want to avoid considering
12094169689Skan     one to be a specification, so we must test that this DIE is not a
12095169689Skan     declaration.  */
12096169689Skan  else if (old_die && TREE_STATIC (decl) && ! declaration
12097117395Skan	   && get_AT_flag (old_die, DW_AT_declaration) == 1)
1209850397Sobrien    {
1209990075Sobrien      /* This is a definition of a C++ class level static.  */
12100132718Skan      add_AT_specification (var_die, old_die);
1210150397Sobrien      if (DECL_NAME (decl))
1210250397Sobrien	{
12103169689Skan	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12104169689Skan	  struct dwarf_file_data * file_index = lookup_filename (s.file);
1210550397Sobrien
12106169689Skan	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12107169689Skan	    add_AT_file (var_die, DW_AT_decl_file, file_index);
1210850397Sobrien
12109169689Skan	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
1211050397Sobrien
12111169689Skan	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
1211250397Sobrien	}
1211350397Sobrien    }
1211450397Sobrien  else
1211550397Sobrien    {
1211650397Sobrien      add_name_and_src_coords_attributes (var_die, decl);
1211790075Sobrien      add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
1211850397Sobrien			  TREE_THIS_VOLATILE (decl), context_die);
1211950397Sobrien
1212050397Sobrien      if (TREE_PUBLIC (decl))
1212150397Sobrien	add_AT_flag (var_die, DW_AT_external, 1);
1212250397Sobrien
1212350397Sobrien      if (DECL_ARTIFICIAL (decl))
1212450397Sobrien	add_AT_flag (var_die, DW_AT_artificial, 1);
1212550397Sobrien
1212650397Sobrien      if (TREE_PROTECTED (decl))
1212750397Sobrien	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
1212850397Sobrien      else if (TREE_PRIVATE (decl))
1212950397Sobrien	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
1213050397Sobrien    }
1213150397Sobrien
1213250397Sobrien  if (declaration)
1213350397Sobrien    add_AT_flag (var_die, DW_AT_declaration, 1);
1213490075Sobrien
12135169689Skan  if (DECL_ABSTRACT (decl) || declaration)
1213650397Sobrien    equate_decl_number_to_die (decl, var_die);
1213750397Sobrien
1213850397Sobrien  if (! declaration && ! DECL_ABSTRACT (decl))
1213950397Sobrien    {
12140169689Skan      add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
1214150397Sobrien      add_pubname (decl, var_die);
1214250397Sobrien    }
1214390075Sobrien  else
1214490075Sobrien    tree_add_const_value_attribute (var_die, decl);
1214550397Sobrien}
1214650397Sobrien
1214750397Sobrien/* Generate a DIE to represent a label identifier.  */
1214850397Sobrien
1214950397Sobrienstatic void
12150132718Skangen_label_die (tree decl, dw_die_ref context_die)
1215150397Sobrien{
1215290075Sobrien  tree origin = decl_ultimate_origin (decl);
1215390075Sobrien  dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
1215490075Sobrien  rtx insn;
1215550397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
1215650397Sobrien
1215750397Sobrien  if (origin != NULL)
1215850397Sobrien    add_abstract_origin_attribute (lbl_die, origin);
1215950397Sobrien  else
1216050397Sobrien    add_name_and_src_coords_attributes (lbl_die, decl);
1216150397Sobrien
1216250397Sobrien  if (DECL_ABSTRACT (decl))
1216350397Sobrien    equate_decl_number_to_die (decl, lbl_die);
1216450397Sobrien  else
1216550397Sobrien    {
12166132718Skan      insn = DECL_RTL_IF_SET (decl);
1216752284Sobrien
1216852284Sobrien      /* Deleted labels are programmer specified labels which have been
12169132718Skan	 eliminated because of various optimizations.  We still emit them
1217052284Sobrien	 here so that it is possible to put breakpoints on them.  */
12171132718Skan      if (insn
12172169689Skan	  && (LABEL_P (insn)
12173169689Skan	      || ((NOTE_P (insn)
12174132718Skan	           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
1217550397Sobrien	{
1217690075Sobrien	  /* When optimization is enabled (via -O) some parts of the compiler
1217790075Sobrien	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
1217850397Sobrien	     represent source-level labels which were explicitly declared by
1217950397Sobrien	     the user.  This really shouldn't be happening though, so catch
1218050397Sobrien	     it if it ever does happen.  */
12181169689Skan	  gcc_assert (!INSN_DELETED_P (insn));
1218250397Sobrien
1218390075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
1218450397Sobrien	  add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
1218550397Sobrien	}
1218650397Sobrien    }
1218750397Sobrien}
1218850397Sobrien
12189169689Skan/* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12190169689Skan   attributes to the DIE for a block STMT, to describe where the inlined
12191169689Skan   function was called from.  This is similar to add_src_coords_attributes.  */
1219250397Sobrien
12193169689Skanstatic inline void
12194169689Skanadd_call_src_coords_attributes (tree stmt, dw_die_ref die)
1219550397Sobrien{
12196169689Skan  expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12197169689Skan
12198169689Skan  add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12199169689Skan  add_AT_unsigned (die, DW_AT_call_line, s.line);
12200169689Skan}
12201169689Skan
12202169689Skan/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12203169689Skan   Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12204169689Skan
12205169689Skanstatic inline void
12206169689Skanadd_high_low_attributes (tree stmt, dw_die_ref die)
12207169689Skan{
1220850397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
1220950397Sobrien
12210169689Skan  if (BLOCK_FRAGMENT_CHAIN (stmt))
1221150397Sobrien    {
12212169689Skan      tree chain;
1221390075Sobrien
12214169689Skan      add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
1221590075Sobrien
12216169689Skan      chain = BLOCK_FRAGMENT_CHAIN (stmt);
12217169689Skan      do
1221890075Sobrien	{
12219169689Skan	  add_ranges (chain);
12220169689Skan	  chain = BLOCK_FRAGMENT_CHAIN (chain);
1222190075Sobrien	}
12222169689Skan      while (chain);
12223169689Skan      add_ranges (NULL);
1222450397Sobrien    }
12225169689Skan  else
12226169689Skan    {
12227169689Skan      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12228169689Skan				   BLOCK_NUMBER (stmt));
12229169689Skan      add_AT_lbl_id (die, DW_AT_low_pc, label);
12230169689Skan      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12231169689Skan				   BLOCK_NUMBER (stmt));
12232169689Skan      add_AT_lbl_id (die, DW_AT_high_pc, label);
12233169689Skan    }
12234169689Skan}
1223550397Sobrien
12236169689Skan/* Generate a DIE for a lexical block.  */
12237169689Skan
12238169689Skanstatic void
12239169689Skangen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12240169689Skan{
12241169689Skan  dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12242169689Skan
12243169689Skan  if (! BLOCK_ABSTRACT (stmt))
12244169689Skan    add_high_low_attributes (stmt, stmt_die);
12245169689Skan
1224650397Sobrien  decls_for_scope (stmt, stmt_die, depth);
1224750397Sobrien}
1224850397Sobrien
1224950397Sobrien/* Generate a DIE for an inlined subprogram.  */
1225050397Sobrien
1225150397Sobrienstatic void
12252132718Skangen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
1225350397Sobrien{
12254122180Skan  tree decl = block_ultimate_origin (stmt);
12255122180Skan
12256122180Skan  /* Emit info for the abstract instance first, if we haven't yet.  We
12257122180Skan     must emit this even if the block is abstract, otherwise when we
12258122180Skan     emit the block below (or elsewhere), we may end up trying to emit
12259122180Skan     a die whose origin die hasn't been emitted, and crashing.  */
12260122180Skan  dwarf2out_abstract_function (decl);
12261122180Skan
1226250397Sobrien  if (! BLOCK_ABSTRACT (stmt))
1226350397Sobrien    {
1226490075Sobrien      dw_die_ref subr_die
1226590075Sobrien	= new_die (DW_TAG_inlined_subroutine, context_die, stmt);
1226650397Sobrien
1226750397Sobrien      add_abstract_origin_attribute (subr_die, decl);
12268169689Skan      add_high_low_attributes (stmt, subr_die);
12269169689Skan      add_call_src_coords_attributes (stmt, subr_die);
12270169689Skan
1227150397Sobrien      decls_for_scope (stmt, subr_die, depth);
1227250397Sobrien      current_function_has_inlines = 1;
1227350397Sobrien    }
1227496263Sobrien  else
1227596263Sobrien    /* We may get here if we're the outer block of function A that was
1227696263Sobrien       inlined into function B that was inlined into function C.  When
1227796263Sobrien       generating debugging info for C, dwarf2out_abstract_function(B)
1227896263Sobrien       would mark all inlined blocks as abstract, including this one.
1227996263Sobrien       So, we wouldn't (and shouldn't) expect labels to be generated
1228096263Sobrien       for this one.  Instead, just emit debugging info for
1228196263Sobrien       declarations within the block.  This is particularly important
1228296263Sobrien       in the case of initializers of arguments passed from B to us:
1228396263Sobrien       if they're statement expressions containing declarations, we
1228496263Sobrien       wouldn't generate dies for their abstract variables, and then,
1228596263Sobrien       when generating dies for the real variables, we'd die (pun
1228696263Sobrien       intended :-)  */
1228796263Sobrien    gen_lexical_block_die (stmt, context_die, depth);
1228850397Sobrien}
1228950397Sobrien
1229050397Sobrien/* Generate a DIE for a field in a record, or structure.  */
1229150397Sobrien
1229250397Sobrienstatic void
12293132718Skangen_field_die (tree decl, dw_die_ref context_die)
1229450397Sobrien{
12295117395Skan  dw_die_ref decl_die;
1229650397Sobrien
12297117395Skan  if (TREE_TYPE (decl) == error_mark_node)
12298117395Skan    return;
12299132718Skan
12300117395Skan  decl_die = new_die (DW_TAG_member, context_die, decl);
1230150397Sobrien  add_name_and_src_coords_attributes (decl_die, decl);
1230250397Sobrien  add_type_attribute (decl_die, member_declared_type (decl),
1230350397Sobrien		      TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
1230450397Sobrien		      context_die);
1230550397Sobrien
1230650397Sobrien  if (DECL_BIT_FIELD_TYPE (decl))
1230750397Sobrien    {
1230850397Sobrien      add_byte_size_attribute (decl_die, decl);
1230950397Sobrien      add_bit_size_attribute (decl_die, decl);
1231050397Sobrien      add_bit_offset_attribute (decl_die, decl);
1231150397Sobrien    }
1231250397Sobrien
1231350397Sobrien  if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
1231450397Sobrien    add_data_member_location_attribute (decl_die, decl);
1231550397Sobrien
1231650397Sobrien  if (DECL_ARTIFICIAL (decl))
1231750397Sobrien    add_AT_flag (decl_die, DW_AT_artificial, 1);
1231850397Sobrien
1231950397Sobrien  if (TREE_PROTECTED (decl))
1232050397Sobrien    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
1232150397Sobrien  else if (TREE_PRIVATE (decl))
1232250397Sobrien    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12323169689Skan
12324169689Skan  /* Equate decl number to die, so that we can look up this decl later on.  */
12325169689Skan  equate_decl_number_to_die (decl, decl_die);
1232650397Sobrien}
1232750397Sobrien
1232850397Sobrien#if 0
1232950397Sobrien/* Don't generate either pointer_type DIEs or reference_type DIEs here.
1233050397Sobrien   Use modified_type_die instead.
1233150397Sobrien   We keep this code here just in case these types of DIEs may be needed to
1233250397Sobrien   represent certain things in other languages (e.g. Pascal) someday.  */
1233390075Sobrien
1233450397Sobrienstatic void
12335132718Skangen_pointer_type_die (tree type, dw_die_ref context_die)
1233650397Sobrien{
1233790075Sobrien  dw_die_ref ptr_die
1233890075Sobrien    = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
1233950397Sobrien
1234050397Sobrien  equate_type_number_to_die (type, ptr_die);
1234150397Sobrien  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
1234250397Sobrien  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
1234350397Sobrien}
1234450397Sobrien
1234550397Sobrien/* Don't generate either pointer_type DIEs or reference_type DIEs here.
1234650397Sobrien   Use modified_type_die instead.
1234750397Sobrien   We keep this code here just in case these types of DIEs may be needed to
1234850397Sobrien   represent certain things in other languages (e.g. Pascal) someday.  */
1234990075Sobrien
1235050397Sobrienstatic void
12351132718Skangen_reference_type_die (tree type, dw_die_ref context_die)
1235250397Sobrien{
1235390075Sobrien  dw_die_ref ref_die
1235490075Sobrien    = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
1235550397Sobrien
1235650397Sobrien  equate_type_number_to_die (type, ref_die);
1235750397Sobrien  add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
1235850397Sobrien  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
1235950397Sobrien}
1236050397Sobrien#endif
1236150397Sobrien
1236250397Sobrien/* Generate a DIE for a pointer to a member type.  */
1236390075Sobrien
1236450397Sobrienstatic void
12365132718Skangen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
1236650397Sobrien{
1236790075Sobrien  dw_die_ref ptr_die
1236890075Sobrien    = new_die (DW_TAG_ptr_to_member_type,
1236990075Sobrien	       scope_die_for (type, context_die), type);
1237050397Sobrien
1237150397Sobrien  equate_type_number_to_die (type, ptr_die);
1237250397Sobrien  add_AT_die_ref (ptr_die, DW_AT_containing_type,
1237350397Sobrien		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
1237450397Sobrien  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
1237550397Sobrien}
1237650397Sobrien
1237750397Sobrien/* Generate the DIE for the compilation unit.  */
1237850397Sobrien
1237990075Sobrienstatic dw_die_ref
12380132718Skangen_compile_unit_die (const char *filename)
1238150397Sobrien{
1238290075Sobrien  dw_die_ref die;
1238350397Sobrien  char producer[250];
1238490075Sobrien  const char *language_string = lang_hooks.name;
1238590075Sobrien  int language;
1238650397Sobrien
1238790075Sobrien  die = new_die (DW_TAG_compile_unit, NULL, NULL);
1238850397Sobrien
12389132718Skan  if (filename)
12390132718Skan    {
12391132718Skan      add_name_attribute (die, filename);
12392132718Skan      /* Don't add cwd for <built-in>.  */
12393132718Skan      if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12394132718Skan	add_comp_dir_attribute (die);
12395132718Skan    }
1239650397Sobrien
1239750397Sobrien  sprintf (producer, "%s %s", language_string, version_string);
1239850397Sobrien
1239950397Sobrien#ifdef MIPS_DEBUGGING_INFO
1240050397Sobrien  /* The MIPS/SGI compilers place the 'cc' command line options in the producer
1240150397Sobrien     string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
1240250397Sobrien     not appear in the producer string, the debugger reaches the conclusion
1240350397Sobrien     that the object file is stripped and has no debugging information.
1240450397Sobrien     To get the MIPS/SGI debugger to believe that there is debugging
1240550397Sobrien     information in the object file, we add a -g to the producer string.  */
1240650397Sobrien  if (debug_info_level > DINFO_LEVEL_TERSE)
1240750397Sobrien    strcat (producer, " -g");
1240850397Sobrien#endif
1240950397Sobrien
1241090075Sobrien  add_AT_string (die, DW_AT_producer, producer);
1241150397Sobrien
1241250397Sobrien  if (strcmp (language_string, "GNU C++") == 0)
1241390075Sobrien    language = DW_LANG_C_plus_plus;
1241450397Sobrien  else if (strcmp (language_string, "GNU Ada") == 0)
12415132718Skan    language = DW_LANG_Ada95;
1241650397Sobrien  else if (strcmp (language_string, "GNU F77") == 0)
1241790075Sobrien    language = DW_LANG_Fortran77;
12418169689Skan  else if (strcmp (language_string, "GNU F95") == 0)
12419169689Skan    language = DW_LANG_Fortran95;
1242050397Sobrien  else if (strcmp (language_string, "GNU Pascal") == 0)
1242190075Sobrien    language = DW_LANG_Pascal83;
1242290075Sobrien  else if (strcmp (language_string, "GNU Java") == 0)
1242390075Sobrien    language = DW_LANG_Java;
12424169689Skan  else if (strcmp (language_string, "GNU Objective-C") == 0)
12425169689Skan    language = DW_LANG_ObjC;
12426169689Skan  else if (strcmp (language_string, "GNU Objective-C++") == 0)
12427169689Skan    language = DW_LANG_ObjC_plus_plus;
1242850397Sobrien  else
1242990075Sobrien    language = DW_LANG_C89;
1243050397Sobrien
1243190075Sobrien  add_AT_unsigned (die, DW_AT_language, language);
1243290075Sobrien  return die;
1243350397Sobrien}
1243450397Sobrien
1243550397Sobrien/* Generate the DIE for a base class.  */
1243650397Sobrien
1243750397Sobrienstatic void
12438132718Skangen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
1243950397Sobrien{
1244090075Sobrien  dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
1244150397Sobrien
1244250397Sobrien  add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
1244350397Sobrien  add_data_member_location_attribute (die, binfo);
1244450397Sobrien
12445169689Skan  if (BINFO_VIRTUAL_P (binfo))
1244650397Sobrien    add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
1244790075Sobrien
12448132718Skan  if (access == access_public_node)
1244950397Sobrien    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12450132718Skan  else if (access == access_protected_node)
1245150397Sobrien    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
1245250397Sobrien}
1245350397Sobrien
1245450397Sobrien/* Generate a DIE for a class member.  */
1245550397Sobrien
1245650397Sobrienstatic void
12457132718Skangen_member_die (tree type, dw_die_ref context_die)
1245850397Sobrien{
1245990075Sobrien  tree member;
12460132718Skan  tree binfo = TYPE_BINFO (type);
1246190075Sobrien  dw_die_ref child;
1246250397Sobrien
1246350397Sobrien  /* If this is not an incomplete type, output descriptions of each of its
1246450397Sobrien     members. Note that as we output the DIEs necessary to represent the
1246550397Sobrien     members of this record or union type, we will also be trying to output
1246650397Sobrien     DIEs to represent the *types* of those members. However the `type'
1246790075Sobrien     function (above) will specifically avoid generating type DIEs for member
1246890075Sobrien     types *within* the list of member DIEs for this (containing) type except
1246950397Sobrien     for those types (of members) which are explicitly marked as also being
1247050397Sobrien     members of this (containing) type themselves.  The g++ front- end can
1247190075Sobrien     force any given type to be treated as a member of some other (containing)
1247290075Sobrien     type by setting the TYPE_CONTEXT of the given (member) type to point to
1247390075Sobrien     the TREE node representing the appropriate (containing) type.  */
1247450397Sobrien
1247550397Sobrien  /* First output info about the base classes.  */
12476169689Skan  if (binfo)
1247750397Sobrien    {
12478169689Skan      VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
1247990075Sobrien      int i;
12480169689Skan      tree base;
1248150397Sobrien
12482169689Skan      for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12483169689Skan	gen_inheritance_die (base,
12484169689Skan			     (accesses ? VEC_index (tree, accesses, i)
12485132718Skan			      : access_public_node), context_die);
1248650397Sobrien    }
1248750397Sobrien
1248850397Sobrien  /* Now output info about the data members and type members.  */
1248950397Sobrien  for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
1249090075Sobrien    {
1249190075Sobrien      /* If we thought we were generating minimal debug info for TYPE
1249290075Sobrien	 and then changed our minds, some of the member declarations
1249390075Sobrien	 may have already been defined.  Don't define them again, but
1249490075Sobrien	 do put them in the right order.  */
1249550397Sobrien
1249690075Sobrien      child = lookup_decl_die (member);
1249790075Sobrien      if (child)
1249890075Sobrien	splice_child_die (context_die, child);
1249990075Sobrien      else
1250090075Sobrien	gen_decl_die (member, context_die);
1250190075Sobrien    }
1250290075Sobrien
1250350397Sobrien  /* Now output info about the function members (if any).  */
1250450397Sobrien  for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
1250590075Sobrien    {
1250690075Sobrien      /* Don't include clones in the member list.  */
1250790075Sobrien      if (DECL_ABSTRACT_ORIGIN (member))
1250890075Sobrien	continue;
1250990075Sobrien
1251090075Sobrien      child = lookup_decl_die (member);
1251190075Sobrien      if (child)
1251290075Sobrien	splice_child_die (context_die, child);
1251390075Sobrien      else
1251490075Sobrien	gen_decl_die (member, context_die);
1251590075Sobrien    }
1251650397Sobrien}
1251750397Sobrien
1251890075Sobrien/* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
1251990075Sobrien   is set, we pretend that the type was never defined, so we only get the
1252090075Sobrien   member DIEs needed by later specification DIEs.  */
1252150397Sobrien
1252250397Sobrienstatic void
12523259269Spfggen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12524259269Spfg				enum debug_info_usage usage)
1252550397Sobrien{
1252690075Sobrien  dw_die_ref type_die = lookup_type_die (type);
1252790075Sobrien  dw_die_ref scope_die = 0;
1252890075Sobrien  int nested = 0;
1252990075Sobrien  int complete = (TYPE_SIZE (type)
1253090075Sobrien		  && (! TYPE_STUB_DECL (type)
1253190075Sobrien		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12532132718Skan  int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12533259269Spfg  complete = complete && should_emit_struct_debug (type, usage);
1253450397Sobrien
1253590075Sobrien  if (type_die && ! complete)
1253650397Sobrien    return;
1253750397Sobrien
1253850397Sobrien  if (TYPE_CONTEXT (type) != NULL_TREE
12539132718Skan      && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12540132718Skan	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
1254150397Sobrien    nested = 1;
1254250397Sobrien
1254350397Sobrien  scope_die = scope_die_for (type, context_die);
1254450397Sobrien
1254550397Sobrien  if (! type_die || (nested && scope_die == comp_unit_die))
1254650397Sobrien    /* First occurrence of type or toplevel definition of nested class.  */
1254750397Sobrien    {
1254890075Sobrien      dw_die_ref old_die = type_die;
1254950397Sobrien
1255050397Sobrien      type_die = new_die (TREE_CODE (type) == RECORD_TYPE
1255150397Sobrien			  ? DW_TAG_structure_type : DW_TAG_union_type,
1255290075Sobrien			  scope_die, type);
1255350397Sobrien      equate_type_number_to_die (type, type_die);
1255450397Sobrien      if (old_die)
12555132718Skan	add_AT_specification (type_die, old_die);
1255690075Sobrien      else
1255790075Sobrien	add_name_attribute (type_die, type_tag (type));
1255850397Sobrien    }
1255950397Sobrien  else
1256050397Sobrien    remove_AT (type_die, DW_AT_declaration);
1256150397Sobrien
1256250397Sobrien  /* If this type has been completed, then give it a byte_size attribute and
1256350397Sobrien     then give a list of members.  */
12564132718Skan  if (complete && !ns_decl)
1256550397Sobrien    {
1256690075Sobrien      /* Prevent infinite recursion in cases where the type of some member of
12567132718Skan	 this type is expressed in terms of this type itself.  */
1256850397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1256950397Sobrien      add_byte_size_attribute (type_die, type);
1257050397Sobrien      if (TYPE_STUB_DECL (type) != NULL_TREE)
1257150397Sobrien	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
1257250397Sobrien
1257350397Sobrien      /* If the first reference to this type was as the return type of an
1257450397Sobrien	 inline function, then it may not have a parent.  Fix this now.  */
1257550397Sobrien      if (type_die->die_parent == NULL)
1257650397Sobrien	add_child_die (scope_die, type_die);
1257750397Sobrien
1257850397Sobrien      push_decl_scope (type);
1257950397Sobrien      gen_member_die (type, type_die);
1258050397Sobrien      pop_decl_scope ();
1258150397Sobrien
1258250397Sobrien      /* GNU extension: Record what type our vtable lives in.  */
1258350397Sobrien      if (TYPE_VFIELD (type))
1258450397Sobrien	{
1258550397Sobrien	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
1258650397Sobrien
1258750397Sobrien	  gen_type_die (vtype, context_die);
1258850397Sobrien	  add_AT_die_ref (type_die, DW_AT_containing_type,
1258950397Sobrien			  lookup_type_die (vtype));
1259050397Sobrien	}
1259150397Sobrien    }
1259250397Sobrien  else
1259352284Sobrien    {
1259452284Sobrien      add_AT_flag (type_die, DW_AT_declaration, 1);
1259552284Sobrien
1259690075Sobrien      /* We don't need to do this for function-local types.  */
1259790075Sobrien      if (TYPE_STUB_DECL (type)
1259890075Sobrien	  && ! decl_function_context (TYPE_STUB_DECL (type)))
12599169689Skan	VEC_safe_push (tree, gc, incomplete_types, type);
1260052284Sobrien    }
12601260396Spfg
12602260396Spfg  if (get_AT (type_die, DW_AT_name))
12603260396Spfg    add_pubtype (type, type_die);
1260450397Sobrien}
1260550397Sobrien
1260650397Sobrien/* Generate a DIE for a subroutine _type_.  */
1260750397Sobrien
1260850397Sobrienstatic void
12609132718Skangen_subroutine_type_die (tree type, dw_die_ref context_die)
1261050397Sobrien{
1261190075Sobrien  tree return_type = TREE_TYPE (type);
1261290075Sobrien  dw_die_ref subr_die
1261390075Sobrien    = new_die (DW_TAG_subroutine_type,
1261490075Sobrien	       scope_die_for (type, context_die), type);
1261550397Sobrien
1261650397Sobrien  equate_type_number_to_die (type, subr_die);
1261750397Sobrien  add_prototyped_attribute (subr_die, type);
1261850397Sobrien  add_type_attribute (subr_die, return_type, 0, 0, context_die);
1261950397Sobrien  gen_formal_types_die (type, subr_die);
12620260396Spfg
12621260396Spfg  if (get_AT (subr_die, DW_AT_name))
12622260396Spfg    add_pubtype (type, subr_die);
1262350397Sobrien}
1262450397Sobrien
12625132718Skan/* Generate a DIE for a type definition.  */
1262650397Sobrien
1262750397Sobrienstatic void
12628132718Skangen_typedef_die (tree decl, dw_die_ref context_die)
1262950397Sobrien{
1263090075Sobrien  dw_die_ref type_die;
1263190075Sobrien  tree origin;
1263250397Sobrien
1263350397Sobrien  if (TREE_ASM_WRITTEN (decl))
1263450397Sobrien    return;
1263590075Sobrien
1263650397Sobrien  TREE_ASM_WRITTEN (decl) = 1;
1263790075Sobrien  type_die = new_die (DW_TAG_typedef, context_die, decl);
1263850397Sobrien  origin = decl_ultimate_origin (decl);
1263950397Sobrien  if (origin != NULL)
1264050397Sobrien    add_abstract_origin_attribute (type_die, origin);
1264150397Sobrien  else
1264250397Sobrien    {
1264390075Sobrien      tree type;
1264490075Sobrien
1264550397Sobrien      add_name_and_src_coords_attributes (type_die, decl);
1264650397Sobrien      if (DECL_ORIGINAL_TYPE (decl))
1264750397Sobrien	{
1264850397Sobrien	  type = DECL_ORIGINAL_TYPE (decl);
1264990075Sobrien
12650169689Skan	  gcc_assert (type != TREE_TYPE (decl));
12651169689Skan	  equate_type_number_to_die (TREE_TYPE (decl), type_die);
1265250397Sobrien	}
1265350397Sobrien      else
1265450397Sobrien	type = TREE_TYPE (decl);
1265590075Sobrien
1265650397Sobrien      add_type_attribute (type_die, type, TREE_READONLY (decl),
1265750397Sobrien			  TREE_THIS_VOLATILE (decl), context_die);
1265850397Sobrien    }
1265950397Sobrien
1266050397Sobrien  if (DECL_ABSTRACT (decl))
1266150397Sobrien    equate_decl_number_to_die (decl, type_die);
12662260396Spfg
12663260396Spfg  if (get_AT (type_die, DW_AT_name))
12664260396Spfg    add_pubtype (decl, type_die);
1266550397Sobrien}
1266650397Sobrien
1266750397Sobrien/* Generate a type description DIE.  */
1266850397Sobrien
1266950397Sobrienstatic void
12670259269Spfggen_type_die_with_usage (tree type, dw_die_ref context_die,
12671259269Spfg				enum debug_info_usage usage)
1267250397Sobrien{
1267390075Sobrien  int need_pop;
1267490075Sobrien
1267550397Sobrien  if (type == NULL_TREE || type == error_mark_node)
1267650397Sobrien    return;
1267750397Sobrien
1267850397Sobrien  if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1267950397Sobrien      && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1268090075Sobrien    {
12681117395Skan      if (TREE_ASM_WRITTEN (type))
12682117395Skan	return;
12683117395Skan
12684117395Skan      /* Prevent broken recursion; we can't hand off to the same type.  */
12685169689Skan      gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12686117395Skan
1268750397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1268850397Sobrien      gen_decl_die (TYPE_NAME (type), context_die);
1268950397Sobrien      return;
1269050397Sobrien    }
1269150397Sobrien
12692117395Skan  /* We are going to output a DIE to represent the unqualified version
12693117395Skan     of this type (i.e. without any const or volatile qualifiers) so
12694117395Skan     get the main variant (i.e. the unqualified version) of this type
12695117395Skan     now.  (Vectors are special because the debugging info is in the
12696117395Skan     cloned type itself).  */
12697117395Skan  if (TREE_CODE (type) != VECTOR_TYPE)
12698117395Skan    type = type_main_variant (type);
12699117395Skan
12700117395Skan  if (TREE_ASM_WRITTEN (type))
12701117395Skan    return;
12702117395Skan
1270350397Sobrien  switch (TREE_CODE (type))
1270450397Sobrien    {
1270550397Sobrien    case ERROR_MARK:
1270650397Sobrien      break;
1270750397Sobrien
1270850397Sobrien    case POINTER_TYPE:
1270950397Sobrien    case REFERENCE_TYPE:
1271050397Sobrien      /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
1271150397Sobrien	 ensures that the gen_type_die recursion will terminate even if the
1271250397Sobrien	 type is recursive.  Recursive types are possible in Ada.  */
1271350397Sobrien      /* ??? We could perhaps do this for all types before the switch
1271450397Sobrien	 statement.  */
1271550397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1271650397Sobrien
1271750397Sobrien      /* For these types, all that is required is that we output a DIE (or a
12718132718Skan	 set of DIEs) to represent the "basis" type.  */
12719259269Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12720259269Spfg				DINFO_USAGE_IND_USE);
1272150397Sobrien      break;
1272250397Sobrien
1272350397Sobrien    case OFFSET_TYPE:
1272490075Sobrien      /* This code is used for C++ pointer-to-data-member types.
1272550397Sobrien	 Output a description of the relevant class type.  */
12726259269Spfg      gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
12727259269Spfg					DINFO_USAGE_IND_USE);
1272850397Sobrien
1272950397Sobrien      /* Output a description of the type of the object pointed to.  */
12730259269Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12731259269Spfg					DINFO_USAGE_IND_USE);
1273250397Sobrien
1273350397Sobrien      /* Now output a DIE to represent this pointer-to-data-member type
12734132718Skan	 itself.  */
1273550397Sobrien      gen_ptr_to_mbr_type_die (type, context_die);
1273650397Sobrien      break;
1273750397Sobrien
1273850397Sobrien    case FUNCTION_TYPE:
1273950397Sobrien      /* Force out return type (in case it wasn't forced out already).  */
12740259269Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12741259269Spfg					DINFO_USAGE_DIR_USE);
1274250397Sobrien      gen_subroutine_type_die (type, context_die);
1274350397Sobrien      break;
1274450397Sobrien
1274550397Sobrien    case METHOD_TYPE:
1274650397Sobrien      /* Force out return type (in case it wasn't forced out already).  */
12747259269Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12748259269Spfg					DINFO_USAGE_DIR_USE);
1274950397Sobrien      gen_subroutine_type_die (type, context_die);
1275050397Sobrien      break;
1275150397Sobrien
1275250397Sobrien    case ARRAY_TYPE:
12753169689Skan      gen_array_type_die (type, context_die);
1275450397Sobrien      break;
1275550397Sobrien
1275690075Sobrien    case VECTOR_TYPE:
1275796263Sobrien      gen_array_type_die (type, context_die);
1275890075Sobrien      break;
1275990075Sobrien
1276050397Sobrien    case ENUMERAL_TYPE:
1276150397Sobrien    case RECORD_TYPE:
1276250397Sobrien    case UNION_TYPE:
1276350397Sobrien    case QUAL_UNION_TYPE:
1276490075Sobrien      /* If this is a nested type whose containing class hasn't been written
12765132718Skan	 out yet, writing it out will cover this one, too.  This does not apply
12766132718Skan	 to instantiations of member class templates; they need to be added to
12767132718Skan	 the containing class as they are generated.  FIXME: This hurts the
12768132718Skan	 idea of combining type decls from multiple TUs, since we can't predict
12769132718Skan	 what set of template instantiations we'll get.  */
1277050397Sobrien      if (TYPE_CONTEXT (type)
1277152284Sobrien	  && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
1277250397Sobrien	  && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
1277350397Sobrien	{
12774259269Spfg	  gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
1277550397Sobrien
1277690075Sobrien	  if (TREE_ASM_WRITTEN (type))
1277750397Sobrien	    return;
1277850397Sobrien
1277950397Sobrien	  /* If that failed, attach ourselves to the stub.  */
1278050397Sobrien	  push_decl_scope (TYPE_CONTEXT (type));
1278150397Sobrien	  context_die = lookup_type_die (TYPE_CONTEXT (type));
1278290075Sobrien	  need_pop = 1;
1278350397Sobrien	}
1278490075Sobrien      else
12785132718Skan	{
12786132718Skan	  declare_in_namespace (type, context_die);
12787132718Skan	  need_pop = 0;
12788132718Skan	}
1278950397Sobrien
1279050397Sobrien      if (TREE_CODE (type) == ENUMERAL_TYPE)
12791169689Skan	{
12792169689Skan	  /* This might have been written out by the call to
12793169689Skan	     declare_in_namespace.  */
12794169689Skan	  if (!TREE_ASM_WRITTEN (type))
12795169689Skan	    gen_enumeration_type_die (type, context_die);
12796169689Skan	}
1279750397Sobrien      else
12798259269Spfg	gen_struct_or_union_type_die (type, context_die, usage);
1279950397Sobrien
1280090075Sobrien      if (need_pop)
1280150397Sobrien	pop_decl_scope ();
1280250397Sobrien
1280350397Sobrien      /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
1280450397Sobrien	 it up if it is ever completed.  gen_*_type_die will set it for us
1280550397Sobrien	 when appropriate.  */
1280650397Sobrien      return;
1280750397Sobrien
1280850397Sobrien    case VOID_TYPE:
1280950397Sobrien    case INTEGER_TYPE:
1281050397Sobrien    case REAL_TYPE:
1281150397Sobrien    case COMPLEX_TYPE:
1281250397Sobrien    case BOOLEAN_TYPE:
1281350397Sobrien      /* No DIEs needed for fundamental types.  */
1281450397Sobrien      break;
1281550397Sobrien
1281650397Sobrien    case LANG_TYPE:
1281750397Sobrien      /* No Dwarf representation currently defined.  */
1281850397Sobrien      break;
1281950397Sobrien
1282050397Sobrien    default:
12821169689Skan      gcc_unreachable ();
1282250397Sobrien    }
1282350397Sobrien
1282450397Sobrien  TREE_ASM_WRITTEN (type) = 1;
1282550397Sobrien}
1282650397Sobrien
12827259269Spfgstatic void
12828259269Spfggen_type_die (tree type, dw_die_ref context_die)
12829259269Spfg{
12830259269Spfg  gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
12831259269Spfg}
12832259269Spfg
1283350397Sobrien/* Generate a DIE for a tagged type instantiation.  */
1283450397Sobrien
1283550397Sobrienstatic void
12836132718Skangen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
1283750397Sobrien{
1283850397Sobrien  if (type == NULL_TREE || type == error_mark_node)
1283950397Sobrien    return;
1284050397Sobrien
1284150397Sobrien  /* We are going to output a DIE to represent the unqualified version of
1284250397Sobrien     this type (i.e. without any const or volatile qualifiers) so make sure
1284350397Sobrien     that we have the main variant (i.e. the unqualified version) of this
1284450397Sobrien     type now.  */
12845169689Skan  gcc_assert (type == type_main_variant (type));
1284650397Sobrien
1284790075Sobrien  /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
1284890075Sobrien     an instance of an unresolved type.  */
1284990075Sobrien
1285050397Sobrien  switch (TREE_CODE (type))
1285150397Sobrien    {
1285250397Sobrien    case ERROR_MARK:
1285350397Sobrien      break;
1285450397Sobrien
1285550397Sobrien    case ENUMERAL_TYPE:
1285650397Sobrien      gen_inlined_enumeration_type_die (type, context_die);
1285750397Sobrien      break;
1285850397Sobrien
1285950397Sobrien    case RECORD_TYPE:
1286050397Sobrien      gen_inlined_structure_type_die (type, context_die);
1286150397Sobrien      break;
1286250397Sobrien
1286350397Sobrien    case UNION_TYPE:
1286450397Sobrien    case QUAL_UNION_TYPE:
1286550397Sobrien      gen_inlined_union_type_die (type, context_die);
1286650397Sobrien      break;
1286750397Sobrien
1286850397Sobrien    default:
12869169689Skan      gcc_unreachable ();
1287050397Sobrien    }
1287150397Sobrien}
1287250397Sobrien
1287350397Sobrien/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
1287450397Sobrien   things which are local to the given block.  */
1287550397Sobrien
1287650397Sobrienstatic void
12877132718Skangen_block_die (tree stmt, dw_die_ref context_die, int depth)
1287850397Sobrien{
1287990075Sobrien  int must_output_die = 0;
1288090075Sobrien  tree origin;
1288190075Sobrien  tree decl;
1288290075Sobrien  enum tree_code origin_code;
1288350397Sobrien
12884169689Skan  /* Ignore blocks that are NULL.  */
12885169689Skan  if (stmt == NULL_TREE)
1288650397Sobrien    return;
1288750397Sobrien
1288890075Sobrien  /* If the block is one fragment of a non-contiguous block, do not
1288990075Sobrien     process the variables, since they will have been done by the
1289090075Sobrien     origin block.  Do process subblocks.  */
1289190075Sobrien  if (BLOCK_FRAGMENT_ORIGIN (stmt))
1289290075Sobrien    {
1289390075Sobrien      tree sub;
1289490075Sobrien
1289590075Sobrien      for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
1289690075Sobrien	gen_block_die (sub, context_die, depth + 1);
1289790075Sobrien
1289890075Sobrien      return;
1289990075Sobrien    }
1290090075Sobrien
1290150397Sobrien  /* Determine the "ultimate origin" of this block.  This block may be an
1290250397Sobrien     inlined instance of an inlined instance of inline function, so we have
1290350397Sobrien     to trace all of the way back through the origin chain to find out what
1290450397Sobrien     sort of node actually served as the original seed for the creation of
1290550397Sobrien     the current block.  */
1290650397Sobrien  origin = block_ultimate_origin (stmt);
1290750397Sobrien  origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
1290850397Sobrien
1290950397Sobrien  /* Determine if we need to output any Dwarf DIEs at all to represent this
1291050397Sobrien     block.  */
1291150397Sobrien  if (origin_code == FUNCTION_DECL)
1291250397Sobrien    /* The outer scopes for inlinings *must* always be represented.  We
1291350397Sobrien       generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
1291450397Sobrien    must_output_die = 1;
1291550397Sobrien  else
1291650397Sobrien    {
1291750397Sobrien      /* In the case where the current block represents an inlining of the
12918132718Skan	 "body block" of an inline function, we must *NOT* output any DIE for
12919132718Skan	 this block because we have already output a DIE to represent the whole
12920132718Skan	 inlined function scope and the "body block" of any function doesn't
12921132718Skan	 really represent a different scope according to ANSI C rules.  So we
12922132718Skan	 check here to make sure that this block does not represent a "body
12923132718Skan	 block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
1292450397Sobrien      if (! is_body_block (origin ? origin : stmt))
1292550397Sobrien	{
1292650397Sobrien	  /* Determine if this block directly contains any "significant"
1292750397Sobrien	     local declarations which we will need to output DIEs for.  */
1292850397Sobrien	  if (debug_info_level > DINFO_LEVEL_TERSE)
1292950397Sobrien	    /* We are not in terse mode so *any* local declaration counts
1293050397Sobrien	       as being a "significant" one.  */
12931169689Skan	    must_output_die = (BLOCK_VARS (stmt) != NULL
12932169689Skan			       && (TREE_USED (stmt)
12933169689Skan				   || TREE_ASM_WRITTEN (stmt)
12934169689Skan				   || BLOCK_ABSTRACT (stmt)));
1293550397Sobrien	  else
1293650397Sobrien	    /* We are in terse mode, so only local (nested) function
1293750397Sobrien	       definitions count as "significant" local declarations.  */
1293850397Sobrien	    for (decl = BLOCK_VARS (stmt);
1293950397Sobrien		 decl != NULL; decl = TREE_CHAIN (decl))
1294050397Sobrien	      if (TREE_CODE (decl) == FUNCTION_DECL
1294150397Sobrien		  && DECL_INITIAL (decl))
1294250397Sobrien		{
1294350397Sobrien		  must_output_die = 1;
1294450397Sobrien		  break;
1294550397Sobrien		}
1294650397Sobrien	}
1294750397Sobrien    }
1294850397Sobrien
1294950397Sobrien  /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
1295050397Sobrien     DIE for any block which contains no significant local declarations at
1295150397Sobrien     all.  Rather, in such cases we just call `decls_for_scope' so that any
1295250397Sobrien     needed Dwarf info for any sub-blocks will get properly generated. Note
1295350397Sobrien     that in terse mode, our definition of what constitutes a "significant"
1295450397Sobrien     local declaration gets restricted to include only inlined function
1295550397Sobrien     instances and local (nested) function definitions.  */
1295650397Sobrien  if (must_output_die)
1295750397Sobrien    {
1295850397Sobrien      if (origin_code == FUNCTION_DECL)
1295950397Sobrien	gen_inlined_subroutine_die (stmt, context_die, depth);
1296050397Sobrien      else
1296150397Sobrien	gen_lexical_block_die (stmt, context_die, depth);
1296250397Sobrien    }
1296350397Sobrien  else
1296450397Sobrien    decls_for_scope (stmt, context_die, depth);
1296550397Sobrien}
1296650397Sobrien
1296750397Sobrien/* Generate all of the decls declared within a given scope and (recursively)
1296850397Sobrien   all of its sub-blocks.  */
1296950397Sobrien
1297050397Sobrienstatic void
12971132718Skandecls_for_scope (tree stmt, dw_die_ref context_die, int depth)
1297250397Sobrien{
1297390075Sobrien  tree decl;
1297490075Sobrien  tree subblocks;
1297550397Sobrien
12976169689Skan  /* Ignore NULL blocks.  */
12977169689Skan  if (stmt == NULL_TREE)
1297850397Sobrien    return;
1297950397Sobrien
12980169689Skan  if (TREE_USED (stmt))
1298150397Sobrien    {
12982169689Skan      /* Output the DIEs to represent all of the data objects and typedefs
12983169689Skan	 declared directly within this block but not within any nested
12984169689Skan	 sub-blocks.  Also, nested function and tag DIEs have been
12985169689Skan	 generated with a parent of NULL; fix that up now.  */
12986169689Skan      for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12987169689Skan	{
12988169689Skan	  dw_die_ref die;
12989169689Skan
12990169689Skan	  if (TREE_CODE (decl) == FUNCTION_DECL)
12991169689Skan	    die = lookup_decl_die (decl);
12992169689Skan	  else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12993169689Skan	    die = lookup_type_die (TREE_TYPE (decl));
12994169689Skan	  else
12995169689Skan	    die = NULL;
12996169689Skan
12997169689Skan	  if (die != NULL && die->die_parent == NULL)
12998169689Skan	    add_child_die (context_die, die);
12999169689Skan	  /* Do not produce debug information for static variables since
13000169689Skan	     these might be optimized out.  We are called for these later
13001169689Skan	     in cgraph_varpool_analyze_pending_decls. */
13002169689Skan	  if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13003169689Skan	    ;
13004169689Skan	  else
13005169689Skan	    gen_decl_die (decl, context_die);
13006169689Skan	}
1300750397Sobrien    }
1300850397Sobrien
13009117395Skan  /* If we're at -g1, we're not interested in subblocks.  */
13010117395Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13011117395Skan    return;
13012117395Skan
1301350397Sobrien  /* Output the DIEs to represent all sub-blocks (and the items declared
1301450397Sobrien     therein) of this block.  */
1301550397Sobrien  for (subblocks = BLOCK_SUBBLOCKS (stmt);
1301650397Sobrien       subblocks != NULL;
1301750397Sobrien       subblocks = BLOCK_CHAIN (subblocks))
1301850397Sobrien    gen_block_die (subblocks, context_die, depth + 1);
1301950397Sobrien}
1302050397Sobrien
1302150397Sobrien/* Is this a typedef we can avoid emitting?  */
1302250397Sobrien
1302350397Sobrienstatic inline int
13024132718Skanis_redundant_typedef (tree decl)
1302550397Sobrien{
1302650397Sobrien  if (TYPE_DECL_IS_STUB (decl))
1302750397Sobrien    return 1;
1302850397Sobrien
1302950397Sobrien  if (DECL_ARTIFICIAL (decl)
1303050397Sobrien      && DECL_CONTEXT (decl)
1303150397Sobrien      && is_tagged_type (DECL_CONTEXT (decl))
1303250397Sobrien      && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
1303350397Sobrien      && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
1303450397Sobrien    /* Also ignore the artificial member typedef for the class name.  */
1303550397Sobrien    return 1;
1303650397Sobrien
1303750397Sobrien  return 0;
1303850397Sobrien}
1303950397Sobrien
13040169689Skan/* Returns the DIE for decl.  A DIE will always be returned.  */
13041132718Skan
13042169689Skanstatic dw_die_ref
13043169689Skanforce_decl_die (tree decl)
13044169689Skan{
13045169689Skan  dw_die_ref decl_die;
13046169689Skan  unsigned saved_external_flag;
13047169689Skan  tree save_fn = NULL_TREE;
13048169689Skan  decl_die = lookup_decl_die (decl);
13049169689Skan  if (!decl_die)
13050169689Skan    {
13051169689Skan      dw_die_ref context_die;
13052169689Skan      tree decl_context = DECL_CONTEXT (decl);
13053169689Skan      if (decl_context)
13054169689Skan	{
13055169689Skan	  /* Find die that represents this context.  */
13056169689Skan	  if (TYPE_P (decl_context))
13057169689Skan	    context_die = force_type_die (decl_context);
13058169689Skan	  else
13059169689Skan	    context_die = force_decl_die (decl_context);
13060169689Skan	}
13061169689Skan      else
13062169689Skan	context_die = comp_unit_die;
13063132718Skan
13064169689Skan      decl_die = lookup_decl_die (decl);
13065169689Skan      if (decl_die)
13066169689Skan	return decl_die;
13067169689Skan
13068169689Skan      switch (TREE_CODE (decl))
13069169689Skan	{
13070169689Skan	case FUNCTION_DECL:
13071169689Skan	  /* Clear current_function_decl, so that gen_subprogram_die thinks
13072169689Skan	     that this is a declaration. At this point, we just want to force
13073169689Skan	     declaration die.  */
13074169689Skan	  save_fn = current_function_decl;
13075169689Skan	  current_function_decl = NULL_TREE;
13076169689Skan	  gen_subprogram_die (decl, context_die);
13077169689Skan	  current_function_decl = save_fn;
13078169689Skan	  break;
13079169689Skan
13080169689Skan	case VAR_DECL:
13081169689Skan	  /* Set external flag to force declaration die. Restore it after
13082169689Skan	   gen_decl_die() call.  */
13083169689Skan	  saved_external_flag = DECL_EXTERNAL (decl);
13084169689Skan	  DECL_EXTERNAL (decl) = 1;
13085169689Skan	  gen_decl_die (decl, context_die);
13086169689Skan	  DECL_EXTERNAL (decl) = saved_external_flag;
13087169689Skan	  break;
13088169689Skan
13089169689Skan	case NAMESPACE_DECL:
13090169689Skan	  dwarf2out_decl (decl);
13091169689Skan	  break;
13092169689Skan
13093169689Skan	default:
13094169689Skan	  gcc_unreachable ();
13095169689Skan	}
13096169689Skan
13097169689Skan      /* We should be able to find the DIE now.  */
13098169689Skan      if (!decl_die)
13099169689Skan	decl_die = lookup_decl_die (decl);
13100169689Skan      gcc_assert (decl_die);
13101169689Skan    }
13102169689Skan
13103169689Skan  return decl_die;
13104169689Skan}
13105169689Skan
13106169689Skan/* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13107169689Skan   always returned.  */
13108169689Skan
13109132718Skanstatic dw_die_ref
13110169689Skanforce_type_die (tree type)
13111132718Skan{
13112169689Skan  dw_die_ref type_die;
13113132718Skan
13114169689Skan  type_die = lookup_type_die (type);
13115169689Skan  if (!type_die)
13116169689Skan    {
13117169689Skan      dw_die_ref context_die;
13118169689Skan      if (TYPE_CONTEXT (type))
13119169689Skan	{
13120169689Skan	  if (TYPE_P (TYPE_CONTEXT (type)))
13121169689Skan	    context_die = force_type_die (TYPE_CONTEXT (type));
13122169689Skan	  else
13123169689Skan	    context_die = force_decl_die (TYPE_CONTEXT (type));
13124169689Skan	}
13125169689Skan      else
13126169689Skan	context_die = comp_unit_die;
13127132718Skan
13128169689Skan      type_die = lookup_type_die (type);
13129169689Skan      if (type_die)
13130169689Skan	return type_die;
13131169689Skan      gen_type_die (type, context_die);
13132169689Skan      type_die = lookup_type_die (type);
13133169689Skan      gcc_assert (type_die);
13134169689Skan    }
13135169689Skan  return type_die;
13136132718Skan}
13137132718Skan
13138132718Skan/* Force out any required namespaces to be able to output DECL,
13139132718Skan   and return the new context_die for it, if it's changed.  */
13140132718Skan
13141132718Skanstatic dw_die_ref
13142132718Skansetup_namespace_context (tree thing, dw_die_ref context_die)
13143132718Skan{
13144169689Skan  tree context = (DECL_P (thing)
13145169689Skan		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13146132718Skan  if (context && TREE_CODE (context) == NAMESPACE_DECL)
13147132718Skan    /* Force out the namespace.  */
13148169689Skan    context_die = force_decl_die (context);
13149132718Skan
13150132718Skan  return context_die;
13151132718Skan}
13152132718Skan
13153132718Skan/* Emit a declaration DIE for THING (which is either a DECL or a tagged
13154132718Skan   type) within its namespace, if appropriate.
13155132718Skan
13156132718Skan   For compatibility with older debuggers, namespace DIEs only contain
13157132718Skan   declarations; all definitions are emitted at CU scope.  */
13158132718Skan
13159132718Skanstatic void
13160132718Skandeclare_in_namespace (tree thing, dw_die_ref context_die)
13161132718Skan{
13162132718Skan  dw_die_ref ns_context;
13163132718Skan
13164132718Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13165132718Skan    return;
13166132718Skan
13167169689Skan  /* If this decl is from an inlined function, then don't try to emit it in its
13168169689Skan     namespace, as we will get confused.  It would have already been emitted
13169169689Skan     when the abstract instance of the inline function was emitted anyways.  */
13170169689Skan  if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13171169689Skan    return;
13172169689Skan
13173132718Skan  ns_context = setup_namespace_context (thing, context_die);
13174132718Skan
13175132718Skan  if (ns_context != context_die)
13176132718Skan    {
13177132718Skan      if (DECL_P (thing))
13178132718Skan	gen_decl_die (thing, ns_context);
13179132718Skan      else
13180132718Skan	gen_type_die (thing, ns_context);
13181132718Skan    }
13182132718Skan}
13183132718Skan
13184132718Skan/* Generate a DIE for a namespace or namespace alias.  */
13185132718Skan
13186132718Skanstatic void
13187132718Skangen_namespace_die (tree decl)
13188132718Skan{
13189132718Skan  dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13190132718Skan
13191132718Skan  /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13192132718Skan     they are an alias of.  */
13193132718Skan  if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13194132718Skan    {
13195132718Skan      /* Output a real namespace.  */
13196132718Skan      dw_die_ref namespace_die
13197132718Skan	= new_die (DW_TAG_namespace, context_die, decl);
13198132718Skan      add_name_and_src_coords_attributes (namespace_die, decl);
13199132718Skan      equate_decl_number_to_die (decl, namespace_die);
13200132718Skan    }
13201132718Skan  else
13202132718Skan    {
13203132718Skan      /* Output a namespace alias.  */
13204132718Skan
13205132718Skan      /* Force out the namespace we are an alias of, if necessary.  */
13206132718Skan      dw_die_ref origin_die
13207169689Skan	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13208132718Skan
13209132718Skan      /* Now create the namespace alias DIE.  */
13210132718Skan      dw_die_ref namespace_die
13211132718Skan	= new_die (DW_TAG_imported_declaration, context_die, decl);
13212132718Skan      add_name_and_src_coords_attributes (namespace_die, decl);
13213132718Skan      add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13214132718Skan      equate_decl_number_to_die (decl, namespace_die);
13215132718Skan    }
13216132718Skan}
13217132718Skan
1321850397Sobrien/* Generate Dwarf debug information for a decl described by DECL.  */
1321950397Sobrien
1322050397Sobrienstatic void
13221132718Skangen_decl_die (tree decl, dw_die_ref context_die)
1322250397Sobrien{
1322390075Sobrien  tree origin;
1322450397Sobrien
1322590075Sobrien  if (DECL_P (decl) && DECL_IGNORED_P (decl))
1322650397Sobrien    return;
1322750397Sobrien
1322850397Sobrien  switch (TREE_CODE (decl))
1322950397Sobrien    {
1323090075Sobrien    case ERROR_MARK:
1323190075Sobrien      break;
1323290075Sobrien
1323350397Sobrien    case CONST_DECL:
1323490075Sobrien      /* The individual enumerators of an enum type get output when we output
13235132718Skan	 the Dwarf representation of the relevant enum type itself.  */
1323650397Sobrien      break;
1323750397Sobrien
1323850397Sobrien    case FUNCTION_DECL:
1323950397Sobrien      /* Don't output any DIEs to represent mere function declarations,
1324050397Sobrien	 unless they are class members or explicit block externs.  */
1324150397Sobrien      if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
1324290075Sobrien	  && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
1324350397Sobrien	break;
1324450397Sobrien
13245169689Skan#if 0
13246169689Skan      /* FIXME */
13247169689Skan      /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13248169689Skan	 on local redeclarations of global functions.  That seems broken.  */
13249169689Skan      if (current_function_decl != decl)
13250169689Skan	/* This is only a declaration.  */;
13251169689Skan#endif
13252169689Skan
1325390075Sobrien      /* If we're emitting a clone, emit info for the abstract instance.  */
1325490075Sobrien      if (DECL_ORIGIN (decl) != decl)
1325590075Sobrien	dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
1325690075Sobrien
1325790075Sobrien      /* If we're emitting an out-of-line copy of an inline function,
1325890075Sobrien	 emit info for the abstract instance and set up to refer to it.  */
13259132718Skan      else if (cgraph_function_possibly_inlined_p (decl)
13260132718Skan	       && ! DECL_ABSTRACT (decl)
13261132718Skan	       && ! class_or_namespace_scope_p (context_die)
1326290075Sobrien	       /* dwarf2out_abstract_function won't emit a die if this is just
1326390075Sobrien		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
1326490075Sobrien		  that case, because that works only if we have a die.  */
1326590075Sobrien	       && DECL_INITIAL (decl) != NULL_TREE)
1326650397Sobrien	{
1326790075Sobrien	  dwarf2out_abstract_function (decl);
1326890075Sobrien	  set_decl_origin_self (decl);
1326990075Sobrien	}
1327090075Sobrien
1327190075Sobrien      /* Otherwise we're emitting the primary DIE for this decl.  */
1327290075Sobrien      else if (debug_info_level > DINFO_LEVEL_TERSE)
1327390075Sobrien	{
1327450397Sobrien	  /* Before we describe the FUNCTION_DECL itself, make sure that we
1327550397Sobrien	     have described its return type.  */
1327650397Sobrien	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
1327750397Sobrien
1327890075Sobrien	  /* And its virtual context.  */
1327990075Sobrien	  if (DECL_VINDEX (decl) != NULL_TREE)
1328090075Sobrien	    gen_type_die (DECL_CONTEXT (decl), context_die);
1328190075Sobrien
1328250397Sobrien	  /* And its containing type.  */
1328350397Sobrien	  origin = decl_class_context (decl);
1328450397Sobrien	  if (origin != NULL_TREE)
1328590075Sobrien	    gen_type_die_for_member (origin, decl, context_die);
13286132718Skan
13287132718Skan	  /* And its containing namespace.  */
13288132718Skan	  declare_in_namespace (decl, context_die);
1328950397Sobrien	}
1329050397Sobrien
1329150397Sobrien      /* Now output a DIE to represent the function itself.  */
1329250397Sobrien      gen_subprogram_die (decl, context_die);
1329350397Sobrien      break;
1329450397Sobrien
1329550397Sobrien    case TYPE_DECL:
1329650397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13297132718Skan	 actual typedefs.  */
1329850397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1329950397Sobrien	break;
1330050397Sobrien
1330190075Sobrien      /* In the special case of a TYPE_DECL node representing the declaration
13302132718Skan	 of some type tag, if the given TYPE_DECL is marked as having been
13303132718Skan	 instantiated from some other (original) TYPE_DECL node (e.g. one which
13304132718Skan	 was generated within the original definition of an inline function) we
13305132718Skan	 have to generate a special (abbreviated) DW_TAG_structure_type,
13306132718Skan	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13307220150Smm      if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13308220150Smm	  && is_tagged_type (TREE_TYPE (decl)))
1330950397Sobrien	{
1331050397Sobrien	  gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
1331150397Sobrien	  break;
1331250397Sobrien	}
1331350397Sobrien
1331450397Sobrien      if (is_redundant_typedef (decl))
1331550397Sobrien	gen_type_die (TREE_TYPE (decl), context_die);
1331650397Sobrien      else
1331750397Sobrien	/* Output a DIE to represent the typedef itself.  */
1331850397Sobrien	gen_typedef_die (decl, context_die);
1331950397Sobrien      break;
1332050397Sobrien
1332150397Sobrien    case LABEL_DECL:
1332250397Sobrien      if (debug_info_level >= DINFO_LEVEL_NORMAL)
1332350397Sobrien	gen_label_die (decl, context_die);
1332450397Sobrien      break;
1332550397Sobrien
1332650397Sobrien    case VAR_DECL:
13327169689Skan    case RESULT_DECL:
1332850397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13329132718Skan	 variable declarations or definitions.  */
1333050397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1333150397Sobrien	break;
1333250397Sobrien
1333350397Sobrien      /* Output any DIEs that are needed to specify the type of this data
13334132718Skan	 object.  */
1333550397Sobrien      gen_type_die (TREE_TYPE (decl), context_die);
1333650397Sobrien
1333750397Sobrien      /* And its containing type.  */
1333850397Sobrien      origin = decl_class_context (decl);
1333950397Sobrien      if (origin != NULL_TREE)
1334090075Sobrien	gen_type_die_for_member (origin, decl, context_die);
1334150397Sobrien
13342132718Skan      /* And its containing namespace.  */
13343132718Skan      declare_in_namespace (decl, context_die);
13344132718Skan
1334550397Sobrien      /* Now output the DIE to represent the data object itself.  This gets
13346132718Skan	 complicated because of the possibility that the VAR_DECL really
13347132718Skan	 represents an inlined instance of a formal parameter for an inline
13348132718Skan	 function.  */
1334950397Sobrien      origin = decl_ultimate_origin (decl);
1335050397Sobrien      if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
1335150397Sobrien	gen_formal_parameter_die (decl, context_die);
1335250397Sobrien      else
1335350397Sobrien	gen_variable_die (decl, context_die);
1335450397Sobrien      break;
1335550397Sobrien
1335650397Sobrien    case FIELD_DECL:
1335790075Sobrien      /* Ignore the nameless fields that are used to skip bits but handle C++
13358169689Skan	 anonymous unions and structs.  */
1335950397Sobrien      if (DECL_NAME (decl) != NULL_TREE
13360169689Skan	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13361169689Skan	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
1336250397Sobrien	{
1336350397Sobrien	  gen_type_die (member_declared_type (decl), context_die);
1336450397Sobrien	  gen_field_die (decl, context_die);
1336550397Sobrien	}
1336650397Sobrien      break;
1336750397Sobrien
1336850397Sobrien    case PARM_DECL:
1336950397Sobrien      gen_type_die (TREE_TYPE (decl), context_die);
1337050397Sobrien      gen_formal_parameter_die (decl, context_die);
1337150397Sobrien      break;
1337250397Sobrien
1337390075Sobrien    case NAMESPACE_DECL:
13374132718Skan      gen_namespace_die (decl);
1337590075Sobrien      break;
1337690075Sobrien
1337750397Sobrien    default:
13378169689Skan      /* Probably some frontend-internal decl.  Assume we don't care.  */
13379169689Skan      gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13380169689Skan      break;
1338150397Sobrien    }
1338250397Sobrien}
1338350397Sobrien
1338490075Sobrien/* Output debug information for global decl DECL.  Called from toplev.c after
1338590075Sobrien   compilation proper has finished.  */
1338690075Sobrien
1338790075Sobrienstatic void
13388132718Skandwarf2out_global_decl (tree decl)
1338990075Sobrien{
1339090075Sobrien  /* Output DWARF2 information for file-scope tentative data object
1339190075Sobrien     declarations, file-scope (extern) function declarations (which had no
1339290075Sobrien     corresponding body) and file-scope tagged type declarations and
1339390075Sobrien     definitions which have not yet been forced out.  */
1339490075Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
1339590075Sobrien    dwarf2out_decl (decl);
1339690075Sobrien}
1339790075Sobrien
13398169689Skan/* Output debug information for type decl DECL.  Called from toplev.c
13399169689Skan   and from language front ends (to record built-in types).  */
13400169689Skanstatic void
13401169689Skandwarf2out_type_decl (tree decl, int local)
13402169689Skan{
13403169689Skan  if (!local)
13404169689Skan    dwarf2out_decl (decl);
13405169689Skan}
13406169689Skan
13407169689Skan/* Output debug information for imported module or decl.  */
13408169689Skan
13409169689Skanstatic void
13410169689Skandwarf2out_imported_module_or_decl (tree decl, tree context)
13411169689Skan{
13412169689Skan  dw_die_ref imported_die, at_import_die;
13413169689Skan  dw_die_ref scope_die;
13414169689Skan  expanded_location xloc;
13415169689Skan
13416169689Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13417169689Skan    return;
13418169689Skan
13419169689Skan  gcc_assert (decl);
13420169689Skan
13421169689Skan  /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13422169689Skan     We need decl DIE for reference and scope die. First, get DIE for the decl
13423169689Skan     itself.  */
13424169689Skan
13425169689Skan  /* Get the scope die for decl context. Use comp_unit_die for global module
13426169689Skan     or decl. If die is not found for non globals, force new die.  */
13427169689Skan  if (!context)
13428169689Skan    scope_die = comp_unit_die;
13429169689Skan  else if (TYPE_P (context))
13430259269Spfg    {
13431259269Spfg      if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13432259269Spfg	return;
13433169689Skan    scope_die = force_type_die (context);
13434259269Spfg    }
13435169689Skan  else
13436169689Skan    scope_die = force_decl_die (context);
13437169689Skan
13438169689Skan  /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13439169689Skan  if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13440169689Skan    {
13441169689Skan      if (is_base_type (TREE_TYPE (decl)))
13442169689Skan	at_import_die = base_type_die (TREE_TYPE (decl));
13443169689Skan      else
13444169689Skan	at_import_die = force_type_die (TREE_TYPE (decl));
13445169689Skan    }
13446169689Skan  else
13447169689Skan    {
13448169689Skan      at_import_die = lookup_decl_die (decl);
13449169689Skan      if (!at_import_die)
13450169689Skan	{
13451169689Skan	  /* If we're trying to avoid duplicate debug info, we may not have
13452169689Skan	     emitted the member decl for this field.  Emit it now.  */
13453169689Skan	  if (TREE_CODE (decl) == FIELD_DECL)
13454169689Skan	    {
13455169689Skan	      tree type = DECL_CONTEXT (decl);
13456169689Skan	      dw_die_ref type_context_die;
13457169689Skan
13458169689Skan	      if (TYPE_CONTEXT (type))
13459169689Skan		if (TYPE_P (TYPE_CONTEXT (type)))
13460259269Spfg		  {
13461259269Spfg		    if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13462259269Spfg						   DINFO_USAGE_DIR_USE))
13463259269Spfg		      return;
13464169689Skan		  type_context_die = force_type_die (TYPE_CONTEXT (type));
13465259269Spfg		  }
13466169689Skan	      else
13467169689Skan		type_context_die = force_decl_die (TYPE_CONTEXT (type));
13468169689Skan	      else
13469169689Skan		type_context_die = comp_unit_die;
13470169689Skan	      gen_type_die_for_member (type, decl, type_context_die);
13471169689Skan	    }
13472169689Skan	  at_import_die = force_decl_die (decl);
13473169689Skan	}
13474169689Skan    }
13475169689Skan
13476169689Skan  /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13477169689Skan  if (TREE_CODE (decl) == NAMESPACE_DECL)
13478169689Skan    imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13479169689Skan  else
13480169689Skan    imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13481169689Skan
13482169689Skan  xloc = expand_location (input_location);
13483169689Skan  add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13484169689Skan  add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13485169689Skan  add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13486169689Skan}
13487169689Skan
1348890075Sobrien/* Write the debugging output for DECL.  */
1348990075Sobrien
1349090075Sobrienvoid
13491132718Skandwarf2out_decl (tree decl)
1349290075Sobrien{
1349390075Sobrien  dw_die_ref context_die = comp_unit_die;
1349490075Sobrien
1349550397Sobrien  switch (TREE_CODE (decl))
1349650397Sobrien    {
1349790075Sobrien    case ERROR_MARK:
1349890075Sobrien      return;
1349990075Sobrien
1350050397Sobrien    case FUNCTION_DECL:
1350150397Sobrien      /* What we would really like to do here is to filter out all mere
13502132718Skan	 file-scope declarations of file-scope functions which are never
13503132718Skan	 referenced later within this translation unit (and keep all of ones
13504132718Skan	 that *are* referenced later on) but we aren't clairvoyant, so we have
13505132718Skan	 no idea which functions will be referenced in the future (i.e. later
13506132718Skan	 on within the current translation unit). So here we just ignore all
13507132718Skan	 file-scope function declarations which are not also definitions.  If
13508132718Skan	 and when the debugger needs to know something about these functions,
13509132718Skan	 it will have to hunt around and find the DWARF information associated
13510132718Skan	 with the definition of the function.
1351190075Sobrien
1351290075Sobrien	 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13513132718Skan	 nodes represent definitions and which ones represent mere
13514132718Skan	 declarations.  We have to check DECL_INITIAL instead. That's because
13515132718Skan	 the C front-end supports some weird semantics for "extern inline"
13516132718Skan	 function definitions.  These can get inlined within the current
13517169689Skan	 translation unit (and thus, we need to generate Dwarf info for their
13518132718Skan	 abstract instances so that the Dwarf info for the concrete inlined
13519132718Skan	 instances can have something to refer to) but the compiler never
13520132718Skan	 generates any out-of-lines instances of such things (despite the fact
13521132718Skan	 that they *are* definitions).
1352290075Sobrien
1352390075Sobrien	 The important point is that the C front-end marks these "extern
1352490075Sobrien	 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
1352590075Sobrien	 them anyway. Note that the C++ front-end also plays some similar games
1352690075Sobrien	 for inline function definitions appearing within include files which
1352790075Sobrien	 also contain `#pragma interface' pragmas.  */
1352850397Sobrien      if (DECL_INITIAL (decl) == NULL_TREE)
1352950397Sobrien	return;
1353050397Sobrien
1353150397Sobrien      /* If we're a nested function, initially use a parent of NULL; if we're
1353250397Sobrien	 a plain function, this will be fixed up in decls_for_scope.  If
1353350397Sobrien	 we're a method, it will be ignored, since we already have a DIE.  */
13534117395Skan      if (decl_function_context (decl)
13535117395Skan	  /* But if we're in terse mode, we don't care about scope.  */
13536117395Skan	  && debug_info_level > DINFO_LEVEL_TERSE)
1353750397Sobrien	context_die = NULL;
1353850397Sobrien      break;
1353950397Sobrien
1354050397Sobrien    case VAR_DECL:
1354190075Sobrien      /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13542132718Skan	 declaration and if the declaration was never even referenced from
13543132718Skan	 within this entire compilation unit.  We suppress these DIEs in
13544132718Skan	 order to save space in the .debug section (by eliminating entries
13545132718Skan	 which are probably useless).  Note that we must not suppress
13546132718Skan	 block-local extern declarations (whether used or not) because that
13547132718Skan	 would screw-up the debugger's name lookup mechanism and cause it to
13548132718Skan	 miss things which really ought to be in scope at a given point.  */
1354950397Sobrien      if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
1355050397Sobrien	return;
1355150397Sobrien
13552169689Skan      /* For local statics lookup proper context die.  */
13553169689Skan      if (TREE_STATIC (decl) && decl_function_context (decl))
13554169689Skan	context_die = lookup_decl_die (DECL_CONTEXT (decl));
13555169689Skan
1355650397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13557132718Skan	 variable declarations or definitions.  */
1355850397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1355950397Sobrien	return;
1356050397Sobrien      break;
1356150397Sobrien
13562132718Skan    case NAMESPACE_DECL:
13563132718Skan      if (debug_info_level <= DINFO_LEVEL_TERSE)
13564132718Skan	return;
13565132718Skan      if (lookup_decl_die (decl) != NULL)
13566132718Skan        return;
13567132718Skan      break;
13568132718Skan
1356950397Sobrien    case TYPE_DECL:
1357090075Sobrien      /* Don't emit stubs for types unless they are needed by other DIEs.  */
1357190075Sobrien      if (TYPE_DECL_SUPPRESS_DEBUG (decl))
1357290075Sobrien	return;
1357390075Sobrien
1357450397Sobrien      /* Don't bother trying to generate any DIEs to represent any of the
13575132718Skan	 normal built-in types for the language we are compiling.  */
13576169689Skan      if (DECL_IS_BUILTIN (decl))
1357750397Sobrien	{
1357850397Sobrien	  /* OK, we need to generate one for `bool' so GDB knows what type
13579132718Skan	     comparisons have.  */
13580169689Skan	  if (is_cxx ()
1358190075Sobrien	      && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
1358290075Sobrien	      && ! DECL_IGNORED_P (decl))
1358350397Sobrien	    modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
1358450397Sobrien
1358550397Sobrien	  return;
1358650397Sobrien	}
1358750397Sobrien
1358850397Sobrien      /* If we are in terse mode, don't generate any DIEs for types.  */
1358950397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1359050397Sobrien	return;
1359150397Sobrien
1359250397Sobrien      /* If we're a function-scope tag, initially use a parent of NULL;
1359350397Sobrien	 this will be fixed up in decls_for_scope.  */
1359450397Sobrien      if (decl_function_context (decl))
1359550397Sobrien	context_die = NULL;
1359650397Sobrien
1359750397Sobrien      break;
1359850397Sobrien
1359950397Sobrien    default:
1360050397Sobrien      return;
1360150397Sobrien    }
1360250397Sobrien
1360350397Sobrien  gen_decl_die (decl, context_die);
1360450397Sobrien}
1360550397Sobrien
1360650397Sobrien/* Output a marker (i.e. a label) for the beginning of the generated code for
1360750397Sobrien   a lexical block.  */
1360850397Sobrien
1360990075Sobrienstatic void
13610132718Skandwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13611132718Skan		       unsigned int blocknum)
1361250397Sobrien{
13613169689Skan  switch_to_section (current_function_section ());
1361490075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
1361550397Sobrien}
1361650397Sobrien
1361750397Sobrien/* Output a marker (i.e. a label) for the end of the generated code for a
1361850397Sobrien   lexical block.  */
1361950397Sobrien
1362090075Sobrienstatic void
13621132718Skandwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
1362250397Sobrien{
13623169689Skan  switch_to_section (current_function_section ());
1362490075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
1362550397Sobrien}
1362650397Sobrien
1362790075Sobrien/* Returns nonzero if it is appropriate not to emit any debugging
1362890075Sobrien   information for BLOCK, because it doesn't contain any instructions.
1362950397Sobrien
1363090075Sobrien   Don't allow this for blocks with nested functions or local classes
1363190075Sobrien   as we would end up with orphans, and in the presence of scheduling
1363290075Sobrien   we may end up calling them anyway.  */
1363390075Sobrien
1363490075Sobrienstatic bool
13635132718Skandwarf2out_ignore_block (tree block)
1363650397Sobrien{
1363790075Sobrien  tree decl;
1363850397Sobrien
1363990075Sobrien  for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
1364090075Sobrien    if (TREE_CODE (decl) == FUNCTION_DECL
1364190075Sobrien	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
1364290075Sobrien      return 0;
1364390075Sobrien
1364490075Sobrien  return 1;
1364550397Sobrien}
1364650397Sobrien
13647169689Skan/* Hash table routines for file_hash.  */
13648169689Skan
13649169689Skanstatic int
13650169689Skanfile_table_eq (const void *p1_p, const void *p2_p)
13651169689Skan{
13652169689Skan  const struct dwarf_file_data * p1 = p1_p;
13653169689Skan  const char * p2 = p2_p;
13654169689Skan  return strcmp (p1->filename, p2) == 0;
13655169689Skan}
13656169689Skan
13657169689Skanstatic hashval_t
13658169689Skanfile_table_hash (const void *p_p)
13659169689Skan{
13660169689Skan  const struct dwarf_file_data * p = p_p;
13661169689Skan  return htab_hash_string (p->filename);
13662169689Skan}
13663169689Skan
1366490075Sobrien/* Lookup FILE_NAME (in the list of filenames that we know about here in
1366550397Sobrien   dwarf2out.c) and return its "index".  The index of each (known) filename is
1366690075Sobrien   just a unique number which is associated with only that one filename.  We
1366790075Sobrien   need such numbers for the sake of generating labels (in the .debug_sfnames
1366890075Sobrien   section) and references to those files numbers (in the .debug_srcinfo
1366990075Sobrien   and.debug_macinfo sections).  If the filename given as an argument is not
1367090075Sobrien   found in our current list, add it to the list and assign it the next
1367190075Sobrien   available unique index number.  In order to speed up searches, we remember
1367290075Sobrien   the index of the filename was looked up last.  This handles the majority of
1367390075Sobrien   all searches.  */
1367450397Sobrien
13675169689Skanstatic struct dwarf_file_data *
13676132718Skanlookup_filename (const char *file_name)
1367750397Sobrien{
13678169689Skan  void ** slot;
13679169689Skan  struct dwarf_file_data * created;
1368050397Sobrien
1368190075Sobrien  /* Check to see if the file name that was searched on the previous
1368290075Sobrien     call matches this file name.  If so, return the index.  */
13683169689Skan  if (file_table_last_lookup
13684169689Skan      && (file_name == file_table_last_lookup->filename
13685169689Skan	  || strcmp (file_table_last_lookup->filename, file_name) == 0))
13686169689Skan    return file_table_last_lookup;
1368790075Sobrien
13688169689Skan  /* Didn't match the previous lookup, search the table.  */
13689169689Skan  slot = htab_find_slot_with_hash (file_table, file_name,
13690169689Skan				   htab_hash_string (file_name), INSERT);
13691169689Skan  if (*slot)
13692169689Skan    return *slot;
1369350397Sobrien
13694169689Skan  created = ggc_alloc (sizeof (struct dwarf_file_data));
13695169689Skan  created->filename = file_name;
13696169689Skan  created->emitted_number = 0;
13697169689Skan  *slot = created;
13698169689Skan  return created;
13699132718Skan}
13700132718Skan
13701169689Skan/* If the assembler will construct the file table, then translate the compiler
13702169689Skan   internal file table number into the assembler file table number, and emit
13703169689Skan   a .file directive if we haven't already emitted one yet.  The file table
13704169689Skan   numbers are different because we prune debug info for unused variables and
13705169689Skan   types, which may include filenames.  */
13706169689Skan
13707132718Skanstatic int
13708169689Skanmaybe_emit_file (struct dwarf_file_data * fd)
13709132718Skan{
13710169689Skan  if (! fd->emitted_number)
13711117395Skan    {
13712169689Skan      if (last_emitted_file)
13713169689Skan	fd->emitted_number = last_emitted_file->emitted_number + 1;
13714169689Skan      else
13715169689Skan	fd->emitted_number = 1;
13716169689Skan      last_emitted_file = fd;
13717169689Skan
13718169689Skan      if (DWARF2_ASM_LINE_DEBUG_INFO)
13719132718Skan	{
13720169689Skan	  fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13721169689Skan	  output_quoted_string (asm_out_file, fd->filename);
13722132718Skan	  fputc ('\n', asm_out_file);
13723132718Skan	}
13724117395Skan    }
13725169689Skan
13726169689Skan  return fd->emitted_number;
1372750397Sobrien}
1372850397Sobrien
13729169689Skan/* Called by the final INSN scan whenever we see a var location.  We
13730169689Skan   use it to drop labels in the right places, and throw the location in
13731169689Skan   our lookup table.  */
13732169689Skan
1373390075Sobrienstatic void
13734169689Skandwarf2out_var_location (rtx loc_note)
1373590075Sobrien{
13736169689Skan  char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13737169689Skan  struct var_loc_node *newloc;
13738169689Skan  rtx prev_insn;
13739169689Skan  static rtx last_insn;
13740169689Skan  static const char *last_label;
13741169689Skan  tree decl;
1374290075Sobrien
13743169689Skan  if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13744169689Skan    return;
13745169689Skan  prev_insn = PREV_INSN (loc_note);
13746169689Skan
13747169689Skan  newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13748169689Skan  /* If the insn we processed last time is the previous insn
13749169689Skan     and it is also a var location note, use the label we emitted
13750169689Skan     last time.  */
13751169689Skan  if (last_insn != NULL_RTX
13752169689Skan      && last_insn == prev_insn
13753169689Skan      && NOTE_P (prev_insn)
13754169689Skan      && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13755169689Skan    {
13756169689Skan      newloc->label = last_label;
13757169689Skan    }
13758169689Skan  else
13759169689Skan    {
13760169689Skan      ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13761169689Skan      ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13762169689Skan      loclabel_num++;
13763169689Skan      newloc->label = ggc_strdup (loclabel);
13764169689Skan    }
13765169689Skan  newloc->var_loc_note = loc_note;
13766169689Skan  newloc->next = NULL;
13767169689Skan
13768169689Skan  if (cfun && in_cold_section_p)
13769169689Skan    newloc->section_label = cfun->cold_section_label;
13770169689Skan  else
13771169689Skan    newloc->section_label = text_section_label;
13772169689Skan
13773169689Skan  last_insn = loc_note;
13774169689Skan  last_label = newloc->label;
13775169689Skan  decl = NOTE_VAR_LOCATION_DECL (loc_note);
13776169689Skan  add_var_loc_to_decl (decl, newloc);
1377790075Sobrien}
1377890075Sobrien
13779169689Skan/* We need to reset the locations at the beginning of each
13780169689Skan   function. We can't do this in the end_function hook, because the
13781169689Skan   declarations that use the locations won't have been output when
13782169689Skan   that hook is called.  Also compute have_multiple_function_sections here.  */
13783169689Skan
13784169689Skanstatic void
13785169689Skandwarf2out_begin_function (tree fun)
13786169689Skan{
13787169689Skan  htab_empty (decl_loc_table);
13788169689Skan
13789169689Skan  if (function_section (fun) != text_section)
13790169689Skan    have_multiple_function_sections = true;
13791169689Skan}
13792169689Skan
1379350397Sobrien/* Output a label to mark the beginning of a source code line entry
1379450397Sobrien   and record information relating to this source line, in
1379550397Sobrien   'line_info_table' for later output of the .debug_line section.  */
1379650397Sobrien
1379790075Sobrienstatic void
13798132718Skandwarf2out_source_line (unsigned int line, const char *filename)
1379950397Sobrien{
13800132718Skan  if (debug_info_level >= DINFO_LEVEL_NORMAL
13801132718Skan      && line != 0)
1380250397Sobrien    {
13803169689Skan      int file_num = maybe_emit_file (lookup_filename (filename));
13804169689Skan
13805169689Skan      switch_to_section (current_function_section ());
1380650397Sobrien
1380790075Sobrien      /* If requested, emit something human-readable.  */
1380890075Sobrien      if (flag_debug_asm)
1380990075Sobrien	fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
1381090075Sobrien		 filename, line);
1381190075Sobrien
1381290075Sobrien      if (DWARF2_ASM_LINE_DEBUG_INFO)
1381350397Sobrien	{
1381490075Sobrien	  /* Emit the .loc directive understood by GNU as.  */
1381590075Sobrien	  fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
1381690075Sobrien
1381790075Sobrien	  /* Indicate that line number info exists.  */
1381890075Sobrien	  line_info_table_in_use++;
1381990075Sobrien	}
13820169689Skan      else if (function_section (current_function_decl) != text_section)
1382190075Sobrien	{
1382290075Sobrien	  dw_separate_line_info_ref line_info;
13823169689Skan	  targetm.asm_out.internal_label (asm_out_file,
13824169689Skan					  SEPARATE_LINE_CODE_LABEL,
13825169689Skan					  separate_line_info_table_in_use);
1382650397Sobrien
13827169689Skan	  /* Expand the line info table if necessary.  */
1382850397Sobrien	  if (separate_line_info_table_in_use
1382950397Sobrien	      == separate_line_info_table_allocated)
1383050397Sobrien	    {
1383150397Sobrien	      separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1383250397Sobrien	      separate_line_info_table
13833132718Skan		= ggc_realloc (separate_line_info_table,
13834132718Skan			       separate_line_info_table_allocated
13835132718Skan			       * sizeof (dw_separate_line_info_entry));
13836132718Skan	      memset (separate_line_info_table
13837132718Skan		       + separate_line_info_table_in_use,
13838132718Skan		      0,
13839132718Skan		      (LINE_INFO_TABLE_INCREMENT
13840132718Skan		       * sizeof (dw_separate_line_info_entry)));
1384150397Sobrien	    }
1384250397Sobrien
1384350397Sobrien	  /* Add the new entry at the end of the line_info_table.  */
1384450397Sobrien	  line_info
1384550397Sobrien	    = &separate_line_info_table[separate_line_info_table_in_use++];
13846169689Skan	  line_info->dw_file_num = file_num;
1384750397Sobrien	  line_info->dw_line_num = line;
13848117395Skan	  line_info->function = current_function_funcdef_no;
1384950397Sobrien	}
1385050397Sobrien      else
1385150397Sobrien	{
1385290075Sobrien	  dw_line_info_ref line_info;
1385350397Sobrien
13854169689Skan	  targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
1385550397Sobrien				     line_info_table_in_use);
1385650397Sobrien
1385750397Sobrien	  /* Expand the line info table if necessary.  */
1385850397Sobrien	  if (line_info_table_in_use == line_info_table_allocated)
1385950397Sobrien	    {
1386050397Sobrien	      line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1386150397Sobrien	      line_info_table
13862132718Skan		= ggc_realloc (line_info_table,
13863132718Skan			       (line_info_table_allocated
13864132718Skan				* sizeof (dw_line_info_entry)));
13865132718Skan	      memset (line_info_table + line_info_table_in_use, 0,
13866132718Skan		      LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
1386750397Sobrien	    }
1386850397Sobrien
1386950397Sobrien	  /* Add the new entry at the end of the line_info_table.  */
1387050397Sobrien	  line_info = &line_info_table[line_info_table_in_use++];
13871169689Skan	  line_info->dw_file_num = file_num;
1387250397Sobrien	  line_info->dw_line_num = line;
1387350397Sobrien	}
1387450397Sobrien    }
1387550397Sobrien}
1387650397Sobrien
1387790075Sobrien/* Record the beginning of a new source file.  */
1387850397Sobrien
1387990075Sobrienstatic void
13880132718Skandwarf2out_start_source_file (unsigned int lineno, const char *filename)
1388150397Sobrien{
13882132718Skan  if (flag_eliminate_dwarf2_dups)
1388390075Sobrien    {
1388490075Sobrien      /* Record the beginning of the file for break_out_includes.  */
13885117395Skan      dw_die_ref bincl_die;
13886117395Skan
13887117395Skan      bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
1388890075Sobrien      add_AT_string (bincl_die, DW_AT_name, filename);
1388990075Sobrien    }
1389090075Sobrien
1389190075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1389290075Sobrien    {
13893169689Skan      int file_num = maybe_emit_file (lookup_filename (filename));
13894169689Skan
13895169689Skan      switch_to_section (debug_macinfo_section);
1389690075Sobrien      dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
1389790075Sobrien      dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
1389890075Sobrien				   lineno);
13899169689Skan
13900169689Skan      dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
1390190075Sobrien    }
1390250397Sobrien}
1390350397Sobrien
1390490075Sobrien/* Record the end of a source file.  */
1390550397Sobrien
1390690075Sobrienstatic void
13907132718Skandwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
1390850397Sobrien{
1390990075Sobrien  if (flag_eliminate_dwarf2_dups)
1391090075Sobrien    /* Record the end of the file for break_out_includes.  */
1391190075Sobrien    new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
1391290075Sobrien
1391390075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1391490075Sobrien    {
13915169689Skan      switch_to_section (debug_macinfo_section);
1391690075Sobrien      dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
1391790075Sobrien    }
1391850397Sobrien}
1391950397Sobrien
1392090075Sobrien/* Called from debug_define in toplev.c.  The `buffer' parameter contains
1392150397Sobrien   the tail part of the directive line, i.e. the part which is past the
1392250397Sobrien   initial whitespace, #, whitespace, directive-name, whitespace part.  */
1392350397Sobrien
1392490075Sobrienstatic void
13925132718Skandwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13926132718Skan		  const char *buffer ATTRIBUTE_UNUSED)
1392750397Sobrien{
1392890075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1392990075Sobrien    {
13930169689Skan      switch_to_section (debug_macinfo_section);
1393190075Sobrien      dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
1393290075Sobrien      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
1393390075Sobrien      dw2_asm_output_nstring (buffer, -1, "The macro");
1393490075Sobrien    }
1393550397Sobrien}
1393650397Sobrien
1393790075Sobrien/* Called from debug_undef in toplev.c.  The `buffer' parameter contains
1393850397Sobrien   the tail part of the directive line, i.e. the part which is past the
1393950397Sobrien   initial whitespace, #, whitespace, directive-name, whitespace part.  */
1394050397Sobrien
1394190075Sobrienstatic void
13942132718Skandwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13943132718Skan		 const char *buffer ATTRIBUTE_UNUSED)
1394450397Sobrien{
1394590075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1394690075Sobrien    {
13947169689Skan      switch_to_section (debug_macinfo_section);
1394890075Sobrien      dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
1394990075Sobrien      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
1395090075Sobrien      dw2_asm_output_nstring (buffer, -1, "The macro");
1395190075Sobrien    }
1395250397Sobrien}
1395350397Sobrien
1395450397Sobrien/* Set up for Dwarf output at the start of compilation.  */
1395550397Sobrien
1395690075Sobrienstatic void
13957132718Skandwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
1395850397Sobrien{
13959169689Skan  /* Allocate the file_table.  */
13960169689Skan  file_table = htab_create_ggc (50, file_table_hash,
13961169689Skan				file_table_eq, NULL);
1396290075Sobrien
13963169689Skan  /* Allocate the decl_die_table.  */
13964169689Skan  decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13965169689Skan				    decl_die_table_eq, NULL);
1396650397Sobrien
13967169689Skan  /* Allocate the decl_loc_table.  */
13968169689Skan  decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13969169689Skan				    decl_loc_table_eq, NULL);
13970169689Skan
1397150397Sobrien  /* Allocate the initial hunk of the decl_scope_table.  */
13972169689Skan  decl_scope_table = VEC_alloc (tree, gc, 256);
1397350397Sobrien
1397450397Sobrien  /* Allocate the initial hunk of the abbrev_die_table.  */
13975132718Skan  abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13976132718Skan					* sizeof (dw_die_ref));
1397750397Sobrien  abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13978169689Skan  /* Zero-th entry is allocated, but unused.  */
1397950397Sobrien  abbrev_die_table_in_use = 1;
1398050397Sobrien
1398150397Sobrien  /* Allocate the initial hunk of the line_info_table.  */
13982132718Skan  line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13983132718Skan				       * sizeof (dw_line_info_entry));
1398450397Sobrien  line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
1398590075Sobrien
13986169689Skan  /* Zero-th entry is allocated, but unused.  */
1398750397Sobrien  line_info_table_in_use = 1;
1398850397Sobrien
13989260396Spfg  /* Allocate the pubtypes and pubnames vectors.  */
13990260396Spfg  pubname_table = VEC_alloc (pubname_entry, gc, 32);
13991260396Spfg  pubtype_table = VEC_alloc (pubname_entry, gc, 32);
13992260396Spfg
1399390075Sobrien  /* Generate the initial DIE for the .debug section.  Note that the (string)
1399450397Sobrien     value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
1399590075Sobrien     will (typically) be a relative pathname and that this pathname should be
1399650397Sobrien     taken as being relative to the directory from which the compiler was
13997132718Skan     invoked when the given (base) source file was compiled.  We will fill
13998132718Skan     in this value in dwarf2out_finish.  */
13999132718Skan  comp_unit_die = gen_compile_unit_die (NULL);
1400050397Sobrien
14001169689Skan  incomplete_types = VEC_alloc (tree, gc, 64);
1400290075Sobrien
14003169689Skan  used_rtx_array = VEC_alloc (rtx, gc, 32);
1400490075Sobrien
14005169689Skan  debug_info_section = get_section (DEBUG_INFO_SECTION,
14006169689Skan				    SECTION_DEBUG, NULL);
14007169689Skan  debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14008169689Skan				      SECTION_DEBUG, NULL);
14009169689Skan  debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14010169689Skan				       SECTION_DEBUG, NULL);
14011169689Skan  debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14012169689Skan				       SECTION_DEBUG, NULL);
14013169689Skan  debug_line_section = get_section (DEBUG_LINE_SECTION,
14014169689Skan				    SECTION_DEBUG, NULL);
14015169689Skan  debug_loc_section = get_section (DEBUG_LOC_SECTION,
14016169689Skan				   SECTION_DEBUG, NULL);
14017169689Skan  debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14018169689Skan					SECTION_DEBUG, NULL);
14019260396Spfg#ifdef DEBUG_PUBTYPES_SECTION
14020260396Spfg  debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14021260396Spfg					SECTION_DEBUG, NULL);
14022260396Spfg#endif
14023169689Skan  debug_str_section = get_section (DEBUG_STR_SECTION,
14024169689Skan				   DEBUG_STR_SECTION_FLAGS, NULL);
14025169689Skan  debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14026169689Skan				      SECTION_DEBUG, NULL);
14027169689Skan  debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14028169689Skan				     SECTION_DEBUG, NULL);
14029169689Skan
1403050397Sobrien  ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
1403190075Sobrien  ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
1403290075Sobrien			       DEBUG_ABBREV_SECTION_LABEL, 0);
14033169689Skan  ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14034169689Skan  ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14035169689Skan			       COLD_TEXT_SECTION_LABEL, 0);
14036169689Skan  ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
1403790075Sobrien
1403890075Sobrien  ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
1403952284Sobrien			       DEBUG_INFO_SECTION_LABEL, 0);
1404090075Sobrien  ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
1404152284Sobrien			       DEBUG_LINE_SECTION_LABEL, 0);
1404290075Sobrien  ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
1404390075Sobrien			       DEBUG_RANGES_SECTION_LABEL, 0);
14044169689Skan  switch_to_section (debug_abbrev_section);
1404552284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14046169689Skan  switch_to_section (debug_info_section);
1404752284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14048169689Skan  switch_to_section (debug_line_section);
1404952284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
1405090075Sobrien
1405190075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1405290075Sobrien    {
14053169689Skan      switch_to_section (debug_macinfo_section);
1405490075Sobrien      ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
1405590075Sobrien				   DEBUG_MACINFO_SECTION_LABEL, 0);
1405690075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
1405790075Sobrien    }
1405890075Sobrien
14059169689Skan  switch_to_section (text_section);
14060169689Skan  ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14061169689Skan  if (flag_reorder_blocks_and_partition)
1406290075Sobrien    {
14063169689Skan      switch_to_section (unlikely_text_section ());
14064169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
1406590075Sobrien    }
1406650397Sobrien}
1406750397Sobrien
1406890075Sobrien/* A helper function for dwarf2out_finish called through
1406990075Sobrien   ht_forall.  Emit one queued .debug_str string.  */
1407090075Sobrien
1407190075Sobrienstatic int
14072132718Skanoutput_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
1407390075Sobrien{
14074132718Skan  struct indirect_string_node *node = (struct indirect_string_node *) *h;
1407590075Sobrien
1407690075Sobrien  if (node->form == DW_FORM_strp)
1407790075Sobrien    {
14078169689Skan      switch_to_section (debug_str_section);
1407990075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, node->label);
14080132718Skan      assemble_string (node->str, strlen (node->str) + 1);
1408190075Sobrien    }
1408290075Sobrien
1408390075Sobrien  return 1;
1408490075Sobrien}
1408590075Sobrien
14086169689Skan#if ENABLE_ASSERT_CHECKING
14087169689Skan/* Verify that all marks are clear.  */
14088132718Skan
14089169689Skanstatic void
14090169689Skanverify_marks_clear (dw_die_ref die)
14091169689Skan{
14092169689Skan  dw_die_ref c;
14093169689Skan
14094169689Skan  gcc_assert (! die->die_mark);
14095169689Skan  FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14096169689Skan}
14097169689Skan#endif /* ENABLE_ASSERT_CHECKING */
14098132718Skan
14099132718Skan/* Clear the marks for a die and its children.
14100132718Skan   Be cool if the mark isn't set.  */
14101132718Skan
14102132718Skanstatic void
14103132718Skanprune_unmark_dies (dw_die_ref die)
14104132718Skan{
14105132718Skan  dw_die_ref c;
14106169689Skan
14107169689Skan  if (die->die_mark)
14108169689Skan    die->die_mark = 0;
14109169689Skan  FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14110132718Skan}
14111132718Skan
14112132718Skan/* Given DIE that we're marking as used, find any other dies
14113132718Skan   it references as attributes and mark them as used.  */
14114132718Skan
14115132718Skanstatic void
14116132718Skanprune_unused_types_walk_attribs (dw_die_ref die)
14117132718Skan{
14118132718Skan  dw_attr_ref a;
14119169689Skan  unsigned ix;
14120132718Skan
14121169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14122132718Skan    {
14123132718Skan      if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14124132718Skan	{
14125132718Skan	  /* A reference to another DIE.
14126132718Skan	     Make sure that it will get emitted.  */
14127132718Skan	  prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14128132718Skan	}
14129169689Skan      /* Set the string's refcount to 0 so that prune_unused_types_mark
14130169689Skan	 accounts properly for it.  */
14131169689Skan      if (AT_class (a) == dw_val_class_str)
14132169689Skan	a->dw_attr_val.v.val_str->refcount = 0;
14133132718Skan    }
14134132718Skan}
14135132718Skan
14136132718Skan
14137132718Skan/* Mark DIE as being used.  If DOKIDS is true, then walk down
14138132718Skan   to DIE's children.  */
14139132718Skan
14140132718Skanstatic void
14141132718Skanprune_unused_types_mark (dw_die_ref die, int dokids)
14142132718Skan{
14143132718Skan  dw_die_ref c;
14144132718Skan
14145132718Skan  if (die->die_mark == 0)
14146132718Skan    {
14147132718Skan      /* We haven't done this node yet.  Mark it as used.  */
14148132718Skan      die->die_mark = 1;
14149132718Skan
14150132718Skan      /* We also have to mark its parents as used.
14151132718Skan	 (But we don't want to mark our parents' kids due to this.)  */
14152132718Skan      if (die->die_parent)
14153132718Skan	prune_unused_types_mark (die->die_parent, 0);
14154132718Skan
14155132718Skan      /* Mark any referenced nodes.  */
14156132718Skan      prune_unused_types_walk_attribs (die);
14157132718Skan
14158132718Skan      /* If this node is a specification,
14159132718Skan         also mark the definition, if it exists.  */
14160132718Skan      if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14161132718Skan        prune_unused_types_mark (die->die_definition, 1);
14162132718Skan    }
14163132718Skan
14164132718Skan  if (dokids && die->die_mark != 2)
14165132718Skan    {
14166132718Skan      /* We need to walk the children, but haven't done so yet.
14167132718Skan	 Remember that we've walked the kids.  */
14168132718Skan      die->die_mark = 2;
14169132718Skan
14170169689Skan      /* If this is an array type, we need to make sure our
14171169689Skan	 kids get marked, even if they're types.  */
14172169689Skan      if (die->die_tag == DW_TAG_array_type)
14173169689Skan	FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14174169689Skan      else
14175169689Skan	FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14176132718Skan    }
14177132718Skan}
14178132718Skan
14179132718Skan
14180132718Skan/* Walk the tree DIE and mark types that we actually use.  */
14181132718Skan
14182132718Skanstatic void
14183132718Skanprune_unused_types_walk (dw_die_ref die)
14184132718Skan{
14185132718Skan  dw_die_ref c;
14186132718Skan
14187132718Skan  /* Don't do anything if this node is already marked.  */
14188132718Skan  if (die->die_mark)
14189132718Skan    return;
14190132718Skan
14191132718Skan  switch (die->die_tag) {
14192132718Skan  case DW_TAG_const_type:
14193132718Skan  case DW_TAG_packed_type:
14194132718Skan  case DW_TAG_pointer_type:
14195132718Skan  case DW_TAG_reference_type:
14196132718Skan  case DW_TAG_volatile_type:
14197132718Skan  case DW_TAG_typedef:
14198132718Skan  case DW_TAG_array_type:
14199132718Skan  case DW_TAG_structure_type:
14200132718Skan  case DW_TAG_union_type:
14201132718Skan  case DW_TAG_class_type:
14202132718Skan  case DW_TAG_friend:
14203132718Skan  case DW_TAG_variant_part:
14204132718Skan  case DW_TAG_enumeration_type:
14205132718Skan  case DW_TAG_subroutine_type:
14206132718Skan  case DW_TAG_string_type:
14207132718Skan  case DW_TAG_set_type:
14208132718Skan  case DW_TAG_subrange_type:
14209132718Skan  case DW_TAG_ptr_to_member_type:
14210132718Skan  case DW_TAG_file_type:
14211169689Skan    if (die->die_perennial_p)
14212169689Skan      break;
14213169689Skan
14214132718Skan    /* It's a type node --- don't mark it.  */
14215132718Skan    return;
14216132718Skan
14217132718Skan  default:
14218132718Skan    /* Mark everything else.  */
14219132718Skan    break;
14220132718Skan  }
14221132718Skan
14222132718Skan  die->die_mark = 1;
14223132718Skan
14224132718Skan  /* Now, mark any dies referenced from here.  */
14225132718Skan  prune_unused_types_walk_attribs (die);
14226132718Skan
14227132718Skan  /* Mark children.  */
14228169689Skan  FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14229132718Skan}
14230132718Skan
14231169689Skan/* Increment the string counts on strings referred to from DIE's
14232169689Skan   attributes.  */
14233132718Skan
14234169689Skanstatic void
14235169689Skanprune_unused_types_update_strings (dw_die_ref die)
14236169689Skan{
14237169689Skan  dw_attr_ref a;
14238169689Skan  unsigned ix;
14239169689Skan
14240169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14241169689Skan    if (AT_class (a) == dw_val_class_str)
14242169689Skan      {
14243169689Skan	struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14244169689Skan	s->refcount++;
14245169689Skan	/* Avoid unnecessarily putting strings that are used less than
14246169689Skan	   twice in the hash table.  */
14247169689Skan	if (s->refcount
14248169689Skan	    == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14249169689Skan	  {
14250169689Skan	    void ** slot;
14251169689Skan	    slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14252169689Skan					     htab_hash_string (s->str),
14253169689Skan					     INSERT);
14254169689Skan	    gcc_assert (*slot == NULL);
14255169689Skan	    *slot = s;
14256169689Skan	  }
14257169689Skan      }
14258169689Skan}
14259169689Skan
14260132718Skan/* Remove from the tree DIE any dies that aren't marked.  */
14261132718Skan
14262132718Skanstatic void
14263132718Skanprune_unused_types_prune (dw_die_ref die)
14264132718Skan{
14265169689Skan  dw_die_ref c;
14266132718Skan
14267169689Skan  gcc_assert (die->die_mark);
14268169689Skan  prune_unused_types_update_strings (die);
14269169689Skan
14270169689Skan  if (! die->die_child)
14271169689Skan    return;
14272169689Skan
14273169689Skan  c = die->die_child;
14274169689Skan  do {
14275169689Skan    dw_die_ref prev = c;
14276169689Skan    for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14277169689Skan      if (c == die->die_child)
14278132718Skan	{
14279169689Skan	  /* No marked children between 'prev' and the end of the list.  */
14280169689Skan	  if (prev == c)
14281169689Skan	    /* No marked children at all.  */
14282169689Skan	    die->die_child = NULL;
14283132718Skan	  else
14284169689Skan	    {
14285169689Skan	      prev->die_sib = c->die_sib;
14286169689Skan	      die->die_child = prev;
14287169689Skan	    }
14288169689Skan	  return;
14289132718Skan	}
14290169689Skan
14291169689Skan    if (c != prev->die_sib)
14292169689Skan      prev->die_sib = c;
14293169689Skan    prune_unused_types_prune (c);
14294169689Skan  } while (c != die->die_child);
14295132718Skan}
14296132718Skan
14297132718Skan
14298132718Skan/* Remove dies representing declarations that we never use.  */
14299132718Skan
14300132718Skanstatic void
14301132718Skanprune_unused_types (void)
14302132718Skan{
14303132718Skan  unsigned int i;
14304132718Skan  limbo_die_node *node;
14305260396Spfg  pubname_ref pub;
14306132718Skan
14307169689Skan#if ENABLE_ASSERT_CHECKING
14308169689Skan  /* All the marks should already be clear.  */
14309169689Skan  verify_marks_clear (comp_unit_die);
14310132718Skan  for (node = limbo_die_list; node; node = node->next)
14311169689Skan    verify_marks_clear (node->die);
14312169689Skan#endif /* ENABLE_ASSERT_CHECKING */
14313132718Skan
14314132718Skan  /* Set the mark on nodes that are actually used.  */
14315132718Skan  prune_unused_types_walk (comp_unit_die);
14316132718Skan  for (node = limbo_die_list; node; node = node->next)
14317132718Skan    prune_unused_types_walk (node->die);
14318132718Skan
14319132718Skan  /* Also set the mark on nodes referenced from the
14320132718Skan     pubname_table or arange_table.  */
14321260396Spfg  for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14322260396Spfg    prune_unused_types_mark (pub->die, 1);
14323132718Skan  for (i = 0; i < arange_table_in_use; i++)
14324132718Skan    prune_unused_types_mark (arange_table[i], 1);
14325132718Skan
14326169689Skan  /* Get rid of nodes that aren't marked; and update the string counts.  */
14327169689Skan  if (debug_str_hash)
14328169689Skan    htab_empty (debug_str_hash);
14329132718Skan  prune_unused_types_prune (comp_unit_die);
14330132718Skan  for (node = limbo_die_list; node; node = node->next)
14331132718Skan    prune_unused_types_prune (node->die);
14332132718Skan
14333132718Skan  /* Leave the marks clear.  */
14334132718Skan  prune_unmark_dies (comp_unit_die);
14335132718Skan  for (node = limbo_die_list; node; node = node->next)
14336132718Skan    prune_unmark_dies (node->die);
14337132718Skan}
14338132718Skan
14339169689Skan/* Set the parameter to true if there are any relative pathnames in
14340169689Skan   the file table.  */
14341169689Skanstatic int
14342169689Skanfile_table_relative_p (void ** slot, void *param)
14343169689Skan{
14344169689Skan  bool *p = param;
14345169689Skan  struct dwarf_file_data *d = *slot;
14346169689Skan  if (d->emitted_number && d->filename[0] != DIR_SEPARATOR)
14347169689Skan    {
14348169689Skan      *p = true;
14349169689Skan      return 0;
14350169689Skan    }
14351169689Skan  return 1;
14352169689Skan}
14353169689Skan
1435450397Sobrien/* Output stuff that dwarf requires at the end of every file,
1435550397Sobrien   and generate the DWARF-2 debugging info.  */
1435650397Sobrien
1435790075Sobrienstatic void
14358132718Skandwarf2out_finish (const char *filename)
1435950397Sobrien{
1436050397Sobrien  limbo_die_node *node, *next_node;
1436190075Sobrien  dw_die_ref die = 0;
1436250397Sobrien
14363132718Skan  /* Add the name for the main input file now.  We delayed this from
14364132718Skan     dwarf2out_init to avoid complications with PCH.  */
14365132718Skan  add_name_attribute (comp_unit_die, filename);
14366132718Skan  if (filename[0] != DIR_SEPARATOR)
14367132718Skan    add_comp_dir_attribute (comp_unit_die);
14368132718Skan  else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14369132718Skan    {
14370169689Skan      bool p = false;
14371169689Skan      htab_traverse (file_table, file_table_relative_p, &p);
14372169689Skan      if (p)
14373169689Skan	add_comp_dir_attribute (comp_unit_die);
14374132718Skan    }
14375132718Skan
1437650397Sobrien  /* Traverse the limbo die list, and add parent/child links.  The only
1437750397Sobrien     dies without parents that should be here are concrete instances of
1437850397Sobrien     inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
1437950397Sobrien     For concrete instances, we can get the parent die from the abstract
1438050397Sobrien     instance.  */
1438150397Sobrien  for (node = limbo_die_list; node; node = next_node)
1438250397Sobrien    {
1438350397Sobrien      next_node = node->next;
1438450397Sobrien      die = node->die;
1438550397Sobrien
1438650397Sobrien      if (die->die_parent == NULL)
1438750397Sobrien	{
1438890075Sobrien	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
1438990075Sobrien
1439090075Sobrien	  if (origin)
1439190075Sobrien	    add_child_die (origin->die_parent, die);
1439250397Sobrien	  else if (die == comp_unit_die)
1439390075Sobrien	    ;
1439490075Sobrien	  else if (errorcount > 0 || sorrycount > 0)
1439590075Sobrien	    /* It's OK to be confused by errors in the input.  */
1439690075Sobrien	    add_child_die (comp_unit_die, die);
14397169689Skan	  else
1439890075Sobrien	    {
1439990075Sobrien	      /* In certain situations, the lexical block containing a
1440090075Sobrien		 nested function can be optimized away, which results
1440190075Sobrien		 in the nested function die being orphaned.  Likewise
1440290075Sobrien		 with the return type of that nested function.  Force
14403169689Skan		 this to be a child of the containing function.
14404169689Skan
14405169689Skan		 It may happen that even the containing function got fully
14406169689Skan		 inlined and optimized out.  In that case we are lost and
14407169689Skan		 assign the empty child.  This should not be big issue as
14408169689Skan		 the function is likely unreachable too.  */
14409169689Skan	      tree context = NULL_TREE;
14410169689Skan
14411169689Skan	      gcc_assert (node->created_for);
14412169689Skan
14413169689Skan	      if (DECL_P (node->created_for))
14414169689Skan		context = DECL_CONTEXT (node->created_for);
14415169689Skan	      else if (TYPE_P (node->created_for))
14416169689Skan		context = TYPE_CONTEXT (node->created_for);
14417169689Skan
14418171825Skan	      gcc_assert (context
14419171825Skan			  && (TREE_CODE (context) == FUNCTION_DECL
14420171825Skan			      || TREE_CODE (context) == NAMESPACE_DECL));
14421169689Skan
1442290075Sobrien	      origin = lookup_decl_die (context);
14423169689Skan	      if (origin)
14424169689Skan	        add_child_die (origin, die);
14425169689Skan	      else
14426169689Skan	        add_child_die (comp_unit_die, die);
1442790075Sobrien	    }
1442850397Sobrien	}
1442950397Sobrien    }
1443050397Sobrien
1443190075Sobrien  limbo_die_list = NULL;
1443290075Sobrien
1443352284Sobrien  /* Walk through the list of incomplete types again, trying once more to
1443452284Sobrien     emit full debugging info for them.  */
1443552284Sobrien  retry_incomplete_types ();
1443652284Sobrien
14437132718Skan  if (flag_eliminate_unused_debug_types)
14438132718Skan    prune_unused_types ();
14439132718Skan
1444090075Sobrien  /* Generate separate CUs for each of the include files we've seen.
1444190075Sobrien     They will go into limbo_die_list.  */
1444290075Sobrien  if (flag_eliminate_dwarf2_dups)
1444390075Sobrien    break_out_includes (comp_unit_die);
1444490075Sobrien
1444590075Sobrien  /* Traverse the DIE's and add add sibling attributes to those DIE's
1444650397Sobrien     that have children.  */
1444750397Sobrien  add_sibling_attributes (comp_unit_die);
1444890075Sobrien  for (node = limbo_die_list; node; node = node->next)
1444990075Sobrien    add_sibling_attributes (node->die);
1445050397Sobrien
1445150397Sobrien  /* Output a terminator label for the .text section.  */
14452169689Skan  switch_to_section (text_section);
14453169689Skan  targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14454169689Skan  if (flag_reorder_blocks_and_partition)
1445550397Sobrien    {
14456169689Skan      switch_to_section (unlikely_text_section ());
14457169689Skan      targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
1445890075Sobrien    }
1445950397Sobrien
1446090075Sobrien  /* We can only use the low/high_pc attributes if all of the code was
1446190075Sobrien     in .text.  */
14462169689Skan  if (!have_multiple_function_sections)
1446390075Sobrien    {
1446490075Sobrien      add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
1446590075Sobrien      add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
1446650397Sobrien    }
1446750397Sobrien
1446890075Sobrien  /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
1446990075Sobrien     "base address".  Use zero so that these addresses become absolute.  */
1447090075Sobrien  else if (have_location_lists || ranges_table_in_use)
1447190075Sobrien    add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
1447290075Sobrien
14473169689Skan  /* Output location list section if necessary.  */
14474169689Skan  if (have_location_lists)
14475169689Skan    {
14476169689Skan      /* Output the location lists info.  */
14477169689Skan      switch_to_section (debug_loc_section);
14478169689Skan      ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14479169689Skan				   DEBUG_LOC_SECTION_LABEL, 0);
14480169689Skan      ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14481169689Skan      output_location_lists (die);
14482169689Skan    }
14483169689Skan
1448490075Sobrien  if (debug_info_level >= DINFO_LEVEL_NORMAL)
14485169689Skan    add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14486169689Skan		    debug_line_section_label);
1448790075Sobrien
1448890075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14489169689Skan    add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
1449090075Sobrien
1449190075Sobrien  /* Output all of the compilation units.  We put the main one last so that
1449290075Sobrien     the offsets are available to output_pubnames.  */
1449390075Sobrien  for (node = limbo_die_list; node; node = node->next)
14494117395Skan    output_comp_unit (node->die, 0);
1449590075Sobrien
14496117395Skan  output_comp_unit (comp_unit_die, 0);
1449790075Sobrien
1449850397Sobrien  /* Output the abbreviation table.  */
14499169689Skan  switch_to_section (debug_abbrev_section);
1450050397Sobrien  output_abbrev_section ();
1450150397Sobrien
1450290075Sobrien  /* Output public names table if necessary.  */
14503260396Spfg  if (!VEC_empty (pubname_entry, pubname_table))
1450450397Sobrien    {
14505169689Skan      switch_to_section (debug_pubnames_section);
14506260396Spfg      output_pubnames (pubname_table);
1450750397Sobrien    }
1450850397Sobrien
14509260396Spfg#ifdef DEBUG_PUBTYPES_SECTION
14510260396Spfg  /* Output public types table if necessary.  */
14511260396Spfg  if (!VEC_empty (pubname_entry, pubtype_table))
14512260396Spfg    {
14513260396Spfg      switch_to_section (debug_pubtypes_section);
14514260396Spfg      output_pubnames (pubtype_table);
14515260396Spfg    }
14516260396Spfg#endif
14517260396Spfg
1451890075Sobrien  /* Output the address range information.  We only put functions in the arange
1451990075Sobrien     table, so don't write it out if we don't have any.  */
1452050397Sobrien  if (fde_table_in_use)
1452150397Sobrien    {
14522169689Skan      switch_to_section (debug_aranges_section);
1452350397Sobrien      output_aranges ();
1452450397Sobrien    }
1452590075Sobrien
1452690075Sobrien  /* Output ranges section if necessary.  */
1452790075Sobrien  if (ranges_table_in_use)
1452890075Sobrien    {
14529169689Skan      switch_to_section (debug_ranges_section);
1453090075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
1453190075Sobrien      output_ranges ();
1453290075Sobrien    }
1453390075Sobrien
14534169689Skan  /* Output the source line correspondence table.  We must do this
14535169689Skan     even if there is no line information.  Otherwise, on an empty
14536169689Skan     translation unit, we will generate a present, but empty,
14537169689Skan     .debug_info section.  IRIX 6.5 `nm' will then complain when
14538169689Skan     examining the file.  This is done late so that any filenames
14539169689Skan     used by the debug_info section are marked as 'used'.  */
14540169689Skan  if (! DWARF2_ASM_LINE_DEBUG_INFO)
14541169689Skan    {
14542169689Skan      switch_to_section (debug_line_section);
14543169689Skan      output_line_info ();
14544169689Skan    }
14545169689Skan
14546169689Skan  /* Have to end the macro section.  */
1454790075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14548117395Skan    {
14549169689Skan      switch_to_section (debug_macinfo_section);
14550117395Skan      dw2_asm_output_data (1, 0, "End compilation unit");
1455190075Sobrien    }
1455290075Sobrien
1455390075Sobrien  /* If we emitted any DW_FORM_strp form attribute, output the string
1455490075Sobrien     table too.  */
1455590075Sobrien  if (debug_str_hash)
14556132718Skan    htab_traverse (debug_str_hash, output_indirect_string, NULL);
1455750397Sobrien}
14558117395Skan#else
14559117395Skan
14560117395Skan/* This should never be used, but its address is needed for comparisons.  */
14561117395Skanconst struct gcc_debug_hooks dwarf2_debug_hooks;
14562117395Skan
14563117395Skan#endif /* DWARF2_DEBUGGING_INFO */
14564117395Skan
14565117395Skan#include "gt-dwarf2out.h"
14566