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;
159260395Spfgstatic 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);
2034259563Spfg      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
2196260918Spfg/* APPLE LOCAL begin for-fsf-4_4 5480287 */ \
2197260918Spfg      && flag_exceptions
2198260918Spfg/* 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
3766260395SpfgDEF_VEC_O(pubname_entry);
3767260395SpfgDEF_VEC_ALLOC_O(pubname_entry, gc);
3768260395Spfg
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.  */
3950260395Spfgstatic GTY (()) VEC (pubname_entry, gc) *  pubname_table;
395150397Sobrien
3952260395Spfg/* A pointer to the base of a table that contains a list of publicly
3953260395Spfg   accessible types.  */
3954260395Spfgstatic 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);
4119260395Spfgstatic 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);
4130260395Spfgstatic void add_pubtype (tree, dw_die_ref);
4131260395Spfgstatic 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);
4220259268Spfgstatic void gen_struct_or_union_type_die (tree, dw_die_ref,
4221259268Spfg						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
4734261188Spfg    /* APPLE LOCAL begin radar 5811943 - Fix type of pointers to Blocks  */
4735261188Spfg    case DW_AT_APPLE_block:
4736261188Spfg      return "DW_AT_APPLE_block";
4737261188Spfg    /* APPLE LOCAL end radar 5811943 - Fix type of pointers to Blocks  */
4738261188Spfg
473950397Sobrien    default:
474050397Sobrien      return "DW_AT_<unknown>";
474150397Sobrien    }
474250397Sobrien}
474350397Sobrien
474450397Sobrien/* Convert a DWARF value form code into its string name.  */
474550397Sobrien
474690075Sobrienstatic const char *
4747132718Skandwarf_form_name (unsigned int form)
474850397Sobrien{
474950397Sobrien  switch (form)
475050397Sobrien    {
475150397Sobrien    case DW_FORM_addr:
475250397Sobrien      return "DW_FORM_addr";
475350397Sobrien    case DW_FORM_block2:
475450397Sobrien      return "DW_FORM_block2";
475550397Sobrien    case DW_FORM_block4:
475650397Sobrien      return "DW_FORM_block4";
475750397Sobrien    case DW_FORM_data2:
475850397Sobrien      return "DW_FORM_data2";
475950397Sobrien    case DW_FORM_data4:
476050397Sobrien      return "DW_FORM_data4";
476150397Sobrien    case DW_FORM_data8:
476250397Sobrien      return "DW_FORM_data8";
476350397Sobrien    case DW_FORM_string:
476450397Sobrien      return "DW_FORM_string";
476550397Sobrien    case DW_FORM_block:
476650397Sobrien      return "DW_FORM_block";
476750397Sobrien    case DW_FORM_block1:
476850397Sobrien      return "DW_FORM_block1";
476950397Sobrien    case DW_FORM_data1:
477050397Sobrien      return "DW_FORM_data1";
477150397Sobrien    case DW_FORM_flag:
477250397Sobrien      return "DW_FORM_flag";
477350397Sobrien    case DW_FORM_sdata:
477450397Sobrien      return "DW_FORM_sdata";
477550397Sobrien    case DW_FORM_strp:
477650397Sobrien      return "DW_FORM_strp";
477750397Sobrien    case DW_FORM_udata:
477850397Sobrien      return "DW_FORM_udata";
477950397Sobrien    case DW_FORM_ref_addr:
478050397Sobrien      return "DW_FORM_ref_addr";
478150397Sobrien    case DW_FORM_ref1:
478250397Sobrien      return "DW_FORM_ref1";
478350397Sobrien    case DW_FORM_ref2:
478450397Sobrien      return "DW_FORM_ref2";
478550397Sobrien    case DW_FORM_ref4:
478650397Sobrien      return "DW_FORM_ref4";
478750397Sobrien    case DW_FORM_ref8:
478850397Sobrien      return "DW_FORM_ref8";
478950397Sobrien    case DW_FORM_ref_udata:
479050397Sobrien      return "DW_FORM_ref_udata";
479150397Sobrien    case DW_FORM_indirect:
479250397Sobrien      return "DW_FORM_indirect";
479350397Sobrien    default:
479450397Sobrien      return "DW_FORM_<unknown>";
479550397Sobrien    }
479650397Sobrien}
479750397Sobrien
479850397Sobrien/* Determine the "ultimate origin" of a decl.  The decl may be an inlined
479950397Sobrien   instance of an inlined instance of a decl which is local to an inline
480050397Sobrien   function, so we have to trace all of the way back through the origin chain
480150397Sobrien   to find out what sort of node actually served as the original seed for the
480250397Sobrien   given block.  */
480350397Sobrien
480450397Sobrienstatic tree
4805132718Skandecl_ultimate_origin (tree decl)
480650397Sobrien{
4807169689Skan  if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4808169689Skan    return NULL_TREE;
4809169689Skan
481090075Sobrien  /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
481190075Sobrien     nodes in the function to point to themselves; ignore that if
481290075Sobrien     we're trying to output the abstract instance of this function.  */
481390075Sobrien  if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
481490075Sobrien    return NULL_TREE;
481590075Sobrien
4816169689Skan  /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4817169689Skan     most distant ancestor, this should never happen.  */
4818169689Skan  gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
481950397Sobrien
482052284Sobrien  return DECL_ABSTRACT_ORIGIN (decl);
482150397Sobrien}
482250397Sobrien
482350397Sobrien/* Determine the "ultimate origin" of a block.  The block may be an inlined
482450397Sobrien   instance of an inlined instance of a block which is local to an inline
482550397Sobrien   function, so we have to trace all of the way back through the origin chain
482650397Sobrien   to find out what sort of node actually served as the original seed for the
482750397Sobrien   given block.  */
482850397Sobrien
482950397Sobrienstatic tree
4830132718Skanblock_ultimate_origin (tree block)
483150397Sobrien{
483290075Sobrien  tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
483350397Sobrien
483490075Sobrien  /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
483590075Sobrien     nodes in the function to point to themselves; ignore that if
483690075Sobrien     we're trying to output the abstract instance of this function.  */
483790075Sobrien  if (BLOCK_ABSTRACT (block) && immediate_origin == block)
483890075Sobrien    return NULL_TREE;
483990075Sobrien
484050397Sobrien  if (immediate_origin == NULL_TREE)
484150397Sobrien    return NULL_TREE;
484250397Sobrien  else
484350397Sobrien    {
484490075Sobrien      tree ret_val;
484590075Sobrien      tree lookahead = immediate_origin;
484650397Sobrien
484750397Sobrien      do
484850397Sobrien	{
484950397Sobrien	  ret_val = lookahead;
485090075Sobrien	  lookahead = (TREE_CODE (ret_val) == BLOCK
485190075Sobrien		       ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
485250397Sobrien	}
485350397Sobrien      while (lookahead != NULL && lookahead != ret_val);
4854169689Skan
4855169689Skan      /* The block's abstract origin chain may not be the *ultimate* origin of
4856169689Skan	 the block. It could lead to a DECL that has an abstract origin set.
4857169689Skan	 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4858169689Skan	 will give us if it has one).  Note that DECL's abstract origins are
4859169689Skan	 supposed to be the most distant ancestor (or so decl_ultimate_origin
4860169689Skan	 claims), so we don't need to loop following the DECL origins.  */
4861169689Skan      if (DECL_P (ret_val))
4862169689Skan	return DECL_ORIGIN (ret_val);
486350397Sobrien
486450397Sobrien      return ret_val;
486550397Sobrien    }
486650397Sobrien}
486750397Sobrien
486850397Sobrien/* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
486950397Sobrien   of a virtual function may refer to a base class, so we check the 'this'
487050397Sobrien   parameter.  */
487150397Sobrien
487250397Sobrienstatic tree
4873132718Skandecl_class_context (tree decl)
487450397Sobrien{
487550397Sobrien  tree context = NULL_TREE;
487650397Sobrien
487750397Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
487850397Sobrien    context = DECL_CONTEXT (decl);
487950397Sobrien  else
488050397Sobrien    context = TYPE_MAIN_VARIANT
488150397Sobrien      (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
488250397Sobrien
488390075Sobrien  if (context && !TYPE_P (context))
488450397Sobrien    context = NULL_TREE;
488550397Sobrien
488650397Sobrien  return context;
488750397Sobrien}
488850397Sobrien
4889169689Skan/* Add an attribute/value pair to a DIE.  */
489050397Sobrien
489150397Sobrienstatic inline void
4892132718Skanadd_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
489350397Sobrien{
4894169689Skan  /* Maybe this should be an assert?  */
4895169689Skan  if (die == NULL)
4896169689Skan    return;
4897169689Skan
4898169689Skan  if (die->die_attr == NULL)
4899169689Skan    die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4900169689Skan  VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
490150397Sobrien}
490250397Sobrien
4903132718Skanstatic inline enum dw_val_class
4904132718SkanAT_class (dw_attr_ref a)
490590075Sobrien{
490690075Sobrien  return a->dw_attr_val.val_class;
490790075Sobrien}
490890075Sobrien
490950397Sobrien/* Add a flag value attribute to a DIE.  */
491050397Sobrien
491150397Sobrienstatic inline void
4912132718Skanadd_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
491350397Sobrien{
4914169689Skan  dw_attr_node attr;
491550397Sobrien
4916169689Skan  attr.dw_attr = attr_kind;
4917169689Skan  attr.dw_attr_val.val_class = dw_val_class_flag;
4918169689Skan  attr.dw_attr_val.v.val_flag = flag;
4919169689Skan  add_dwarf_attr (die, &attr);
492050397Sobrien}
492150397Sobrien
492290075Sobrienstatic inline unsigned
4923132718SkanAT_flag (dw_attr_ref a)
492490075Sobrien{
4925169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_flag);
4926169689Skan  return a->dw_attr_val.v.val_flag;
492790075Sobrien}
492890075Sobrien
492950397Sobrien/* Add a signed integer attribute value to a DIE.  */
493050397Sobrien
493150397Sobrienstatic inline void
4932132718Skanadd_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
493350397Sobrien{
4934169689Skan  dw_attr_node attr;
493550397Sobrien
4936169689Skan  attr.dw_attr = attr_kind;
4937169689Skan  attr.dw_attr_val.val_class = dw_val_class_const;
4938169689Skan  attr.dw_attr_val.v.val_int = int_val;
4939169689Skan  add_dwarf_attr (die, &attr);
494050397Sobrien}
494150397Sobrien
4942132718Skanstatic inline HOST_WIDE_INT
4943132718SkanAT_int (dw_attr_ref a)
494490075Sobrien{
4945169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_const);
4946169689Skan  return a->dw_attr_val.v.val_int;
494790075Sobrien}
494890075Sobrien
494950397Sobrien/* Add an unsigned integer attribute value to a DIE.  */
495050397Sobrien
495150397Sobrienstatic inline void
4952132718Skanadd_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4953132718Skan		 unsigned HOST_WIDE_INT unsigned_val)
495450397Sobrien{
4955169689Skan  dw_attr_node attr;
495650397Sobrien
4957169689Skan  attr.dw_attr = attr_kind;
4958169689Skan  attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4959169689Skan  attr.dw_attr_val.v.val_unsigned = unsigned_val;
4960169689Skan  add_dwarf_attr (die, &attr);
496150397Sobrien}
496250397Sobrien
4963132718Skanstatic inline unsigned HOST_WIDE_INT
4964132718SkanAT_unsigned (dw_attr_ref a)
496590075Sobrien{
4966169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4967169689Skan  return a->dw_attr_val.v.val_unsigned;
496890075Sobrien}
496990075Sobrien
497050397Sobrien/* Add an unsigned double integer attribute value to a DIE.  */
497150397Sobrien
497250397Sobrienstatic inline void
4973132718Skanadd_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4974132718Skan		  long unsigned int val_hi, long unsigned int val_low)
497550397Sobrien{
4976169689Skan  dw_attr_node attr;
497750397Sobrien
4978169689Skan  attr.dw_attr = attr_kind;
4979169689Skan  attr.dw_attr_val.val_class = dw_val_class_long_long;
4980169689Skan  attr.dw_attr_val.v.val_long_long.hi = val_hi;
4981169689Skan  attr.dw_attr_val.v.val_long_long.low = val_low;
4982169689Skan  add_dwarf_attr (die, &attr);
498350397Sobrien}
498450397Sobrien
498550397Sobrien/* Add a floating point attribute value to a DIE and return it.  */
498650397Sobrien
498750397Sobrienstatic inline void
4988132718Skanadd_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4989132718Skan	    unsigned int length, unsigned int elt_size, unsigned char *array)
499050397Sobrien{
4991169689Skan  dw_attr_node attr;
499250397Sobrien
4993169689Skan  attr.dw_attr = attr_kind;
4994169689Skan  attr.dw_attr_val.val_class = dw_val_class_vec;
4995169689Skan  attr.dw_attr_val.v.val_vec.length = length;
4996169689Skan  attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4997169689Skan  attr.dw_attr_val.v.val_vec.array = array;
4998169689Skan  add_dwarf_attr (die, &attr);
499950397Sobrien}
500050397Sobrien
5001132718Skan/* Hash and equality functions for debug_str_hash.  */
5002132718Skan
5003132718Skanstatic hashval_t
5004132718Skandebug_str_do_hash (const void *x)
5005132718Skan{
5006132718Skan  return htab_hash_string (((const struct indirect_string_node *)x)->str);
5007132718Skan}
5008132718Skan
5009132718Skanstatic int
5010132718Skandebug_str_eq (const void *x1, const void *x2)
5011132718Skan{
5012132718Skan  return strcmp ((((const struct indirect_string_node *)x1)->str),
5013132718Skan		 (const char *)x2) == 0;
5014132718Skan}
5015132718Skan
501650397Sobrien/* Add a string attribute value to a DIE.  */
501750397Sobrien
501850397Sobrienstatic inline void
5019132718Skanadd_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
502050397Sobrien{
5021169689Skan  dw_attr_node attr;
502290075Sobrien  struct indirect_string_node *node;
5023132718Skan  void **slot;
5024117395Skan
502590075Sobrien  if (! debug_str_hash)
5026132718Skan    debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5027132718Skan				      debug_str_eq, NULL);
502850397Sobrien
5029132718Skan  slot = htab_find_slot_with_hash (debug_str_hash, str,
5030132718Skan				   htab_hash_string (str), INSERT);
5031132718Skan  if (*slot == NULL)
5032132718Skan    *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
5033132718Skan  node = (struct indirect_string_node *) *slot;
5034132718Skan  node->str = ggc_strdup (str);
503590075Sobrien  node->refcount++;
503690075Sobrien
5037169689Skan  attr.dw_attr = attr_kind;
5038169689Skan  attr.dw_attr_val.val_class = dw_val_class_str;
5039169689Skan  attr.dw_attr_val.v.val_str = node;
5040169689Skan  add_dwarf_attr (die, &attr);
504150397Sobrien}
504250397Sobrien
504390075Sobrienstatic inline const char *
5044132718SkanAT_string (dw_attr_ref a)
504590075Sobrien{
5046169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_str);
5047169689Skan  return a->dw_attr_val.v.val_str->str;
504890075Sobrien}
504990075Sobrien
505090075Sobrien/* Find out whether a string should be output inline in DIE
505190075Sobrien   or out-of-line in .debug_str section.  */
505290075Sobrien
505390075Sobrienstatic int
5054132718SkanAT_string_form (dw_attr_ref a)
505590075Sobrien{
5056169689Skan  struct indirect_string_node *node;
5057169689Skan  unsigned int len;
5058169689Skan  char label[32];
505990075Sobrien
5060169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_str);
506190075Sobrien
5062169689Skan  node = a->dw_attr_val.v.val_str;
5063169689Skan  if (node->form)
5064169689Skan    return node->form;
506590075Sobrien
5066169689Skan  len = strlen (node->str) + 1;
506790075Sobrien
5068169689Skan  /* If the string is shorter or equal to the size of the reference, it is
5069169689Skan     always better to put it inline.  */
5070169689Skan  if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5071169689Skan    return node->form = DW_FORM_string;
507290075Sobrien
5073169689Skan  /* If we cannot expect the linker to merge strings in .debug_str
5074169689Skan     section, only put it into .debug_str if it is worth even in this
5075169689Skan     single module.  */
5076169689Skan  if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5077169689Skan      && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5078169689Skan    return node->form = DW_FORM_string;
507990075Sobrien
5080169689Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5081169689Skan  ++dw2_string_counter;
5082169689Skan  node->label = xstrdup (label);
508390075Sobrien
5084169689Skan  return node->form = DW_FORM_strp;
508590075Sobrien}
508690075Sobrien
508750397Sobrien/* Add a DIE reference attribute value to a DIE.  */
508850397Sobrien
508950397Sobrienstatic inline void
5090132718Skanadd_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
509150397Sobrien{
5092169689Skan  dw_attr_node attr;
509350397Sobrien
5094169689Skan  attr.dw_attr = attr_kind;
5095169689Skan  attr.dw_attr_val.val_class = dw_val_class_die_ref;
5096169689Skan  attr.dw_attr_val.v.val_die_ref.die = targ_die;
5097169689Skan  attr.dw_attr_val.v.val_die_ref.external = 0;
5098169689Skan  add_dwarf_attr (die, &attr);
509950397Sobrien}
510050397Sobrien
5101132718Skan/* Add an AT_specification attribute to a DIE, and also make the back
5102132718Skan   pointer from the specification to the definition.  */
5103132718Skan
5104132718Skanstatic inline void
5105132718Skanadd_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5106132718Skan{
5107132718Skan  add_AT_die_ref (die, DW_AT_specification, targ_die);
5108169689Skan  gcc_assert (!targ_die->die_definition);
5109132718Skan  targ_die->die_definition = die;
5110132718Skan}
5111132718Skan
511290075Sobrienstatic inline dw_die_ref
5113132718SkanAT_ref (dw_attr_ref a)
511490075Sobrien{
5115169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5116169689Skan  return a->dw_attr_val.v.val_die_ref.die;
511790075Sobrien}
511890075Sobrien
511990075Sobrienstatic inline int
5120132718SkanAT_ref_external (dw_attr_ref a)
512190075Sobrien{
512290075Sobrien  if (a && AT_class (a) == dw_val_class_die_ref)
512390075Sobrien    return a->dw_attr_val.v.val_die_ref.external;
512490075Sobrien
512590075Sobrien  return 0;
512690075Sobrien}
512790075Sobrien
512890075Sobrienstatic inline void
5129132718Skanset_AT_ref_external (dw_attr_ref a, int i)
513090075Sobrien{
5131169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5132169689Skan  a->dw_attr_val.v.val_die_ref.external = i;
513390075Sobrien}
513490075Sobrien
513550397Sobrien/* Add an FDE reference attribute value to a DIE.  */
513650397Sobrien
513750397Sobrienstatic inline void
5138132718Skanadd_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
513950397Sobrien{
5140169689Skan  dw_attr_node attr;
514150397Sobrien
5142169689Skan  attr.dw_attr = attr_kind;
5143169689Skan  attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5144169689Skan  attr.dw_attr_val.v.val_fde_index = targ_fde;
5145169689Skan  add_dwarf_attr (die, &attr);
514650397Sobrien}
514750397Sobrien
514850397Sobrien/* Add a location description attribute value to a DIE.  */
514950397Sobrien
515050397Sobrienstatic inline void
5151132718Skanadd_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
515250397Sobrien{
5153169689Skan  dw_attr_node attr;
515450397Sobrien
5155169689Skan  attr.dw_attr = attr_kind;
5156169689Skan  attr.dw_attr_val.val_class = dw_val_class_loc;
5157169689Skan  attr.dw_attr_val.v.val_loc = loc;
5158169689Skan  add_dwarf_attr (die, &attr);
515950397Sobrien}
516050397Sobrien
516190075Sobrienstatic inline dw_loc_descr_ref
5162132718SkanAT_loc (dw_attr_ref a)
516390075Sobrien{
5164169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_loc);
5165169689Skan  return a->dw_attr_val.v.val_loc;
516690075Sobrien}
516790075Sobrien
516890075Sobrienstatic inline void
5169132718Skanadd_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
517090075Sobrien{
5171169689Skan  dw_attr_node attr;
517290075Sobrien
5173169689Skan  attr.dw_attr = attr_kind;
5174169689Skan  attr.dw_attr_val.val_class = dw_val_class_loc_list;
5175169689Skan  attr.dw_attr_val.v.val_loc_list = loc_list;
5176169689Skan  add_dwarf_attr (die, &attr);
5177169689Skan  have_location_lists = true;
517890075Sobrien}
517990075Sobrien
518090075Sobrienstatic inline dw_loc_list_ref
5181132718SkanAT_loc_list (dw_attr_ref a)
518290075Sobrien{
5183169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5184169689Skan  return a->dw_attr_val.v.val_loc_list;
518590075Sobrien}
518690075Sobrien
518750397Sobrien/* Add an address constant attribute value to a DIE.  */
518850397Sobrien
518950397Sobrienstatic inline void
5190132718Skanadd_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
519150397Sobrien{
5192169689Skan  dw_attr_node attr;
519350397Sobrien
5194169689Skan  attr.dw_attr = attr_kind;
5195169689Skan  attr.dw_attr_val.val_class = dw_val_class_addr;
5196169689Skan  attr.dw_attr_val.v.val_addr = addr;
5197169689Skan  add_dwarf_attr (die, &attr);
519850397Sobrien}
519950397Sobrien
5200169689Skan/* Get the RTX from to an address DIE attribute.  */
5201169689Skan
520290075Sobrienstatic inline rtx
5203132718SkanAT_addr (dw_attr_ref a)
520490075Sobrien{
5205169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_addr);
5206169689Skan  return a->dw_attr_val.v.val_addr;
5207169689Skan}
520890075Sobrien
5209169689Skan/* Add a file attribute value to a DIE.  */
5210169689Skan
5211169689Skanstatic inline void
5212169689Skanadd_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5213169689Skan	     struct dwarf_file_data *fd)
5214169689Skan{
5215169689Skan  dw_attr_node attr;
5216169689Skan
5217169689Skan  attr.dw_attr = attr_kind;
5218169689Skan  attr.dw_attr_val.val_class = dw_val_class_file;
5219169689Skan  attr.dw_attr_val.v.val_file = fd;
5220169689Skan  add_dwarf_attr (die, &attr);
522190075Sobrien}
522290075Sobrien
5223169689Skan/* Get the dwarf_file_data from a file DIE attribute.  */
5224169689Skan
5225169689Skanstatic inline struct dwarf_file_data *
5226169689SkanAT_file (dw_attr_ref a)
5227169689Skan{
5228169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_file);
5229169689Skan  return a->dw_attr_val.v.val_file;
5230169689Skan}
5231169689Skan
523250397Sobrien/* Add a label identifier attribute value to a DIE.  */
523350397Sobrien
523450397Sobrienstatic inline void
5235132718Skanadd_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
523650397Sobrien{
5237169689Skan  dw_attr_node attr;
523850397Sobrien
5239169689Skan  attr.dw_attr = attr_kind;
5240169689Skan  attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5241169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5242169689Skan  add_dwarf_attr (die, &attr);
524350397Sobrien}
524450397Sobrien
5245169689Skan/* Add a section offset attribute value to a DIE, an offset into the
5246169689Skan   debug_line section.  */
524750397Sobrien
524850397Sobrienstatic inline void
5249169689Skanadd_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5250169689Skan		const char *label)
525150397Sobrien{
5252169689Skan  dw_attr_node attr;
525350397Sobrien
5254169689Skan  attr.dw_attr = attr_kind;
5255169689Skan  attr.dw_attr_val.val_class = dw_val_class_lineptr;
5256169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5257169689Skan  add_dwarf_attr (die, &attr);
525850397Sobrien}
525950397Sobrien
5260169689Skan/* Add a section offset attribute value to a DIE, an offset into the
5261169689Skan   debug_macinfo section.  */
5262169689Skan
5263169689Skanstatic inline void
5264169689Skanadd_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5265169689Skan	       const char *label)
5266169689Skan{
5267169689Skan  dw_attr_node attr;
5268169689Skan
5269169689Skan  attr.dw_attr = attr_kind;
5270169689Skan  attr.dw_attr_val.val_class = dw_val_class_macptr;
5271169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5272169689Skan  add_dwarf_attr (die, &attr);
5273169689Skan}
5274169689Skan
527590075Sobrien/* Add an offset attribute value to a DIE.  */
527650397Sobrien
527790075Sobrienstatic inline void
5278132718Skanadd_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5279132718Skan	       unsigned HOST_WIDE_INT offset)
528050397Sobrien{
5281169689Skan  dw_attr_node attr;
528250397Sobrien
5283169689Skan  attr.dw_attr = attr_kind;
5284169689Skan  attr.dw_attr_val.val_class = dw_val_class_offset;
5285169689Skan  attr.dw_attr_val.v.val_offset = offset;
5286169689Skan  add_dwarf_attr (die, &attr);
528790075Sobrien}
528850397Sobrien
528990075Sobrien/* Add an range_list attribute value to a DIE.  */
529090075Sobrien
529190075Sobrienstatic void
5292132718Skanadd_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5293132718Skan		   long unsigned int offset)
529490075Sobrien{
5295169689Skan  dw_attr_node attr;
529690075Sobrien
5297169689Skan  attr.dw_attr = attr_kind;
5298169689Skan  attr.dw_attr_val.val_class = dw_val_class_range_list;
5299169689Skan  attr.dw_attr_val.v.val_offset = offset;
5300169689Skan  add_dwarf_attr (die, &attr);
530150397Sobrien}
530250397Sobrien
530390075Sobrienstatic inline const char *
5304132718SkanAT_lbl (dw_attr_ref a)
530590075Sobrien{
5306169689Skan  gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5307169689Skan		    || AT_class (a) == dw_val_class_lineptr
5308169689Skan		    || AT_class (a) == dw_val_class_macptr));
5309169689Skan  return a->dw_attr_val.v.val_lbl_id;
531090075Sobrien}
531190075Sobrien
531250397Sobrien/* Get the attribute of type attr_kind.  */
531350397Sobrien
5314132718Skanstatic dw_attr_ref
5315132718Skanget_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
531650397Sobrien{
531790075Sobrien  dw_attr_ref a;
5318169689Skan  unsigned ix;
531990075Sobrien  dw_die_ref spec = NULL;
532090075Sobrien
5321169689Skan  if (! die)
5322169689Skan    return NULL;
532350397Sobrien
5324169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5325169689Skan    if (a->dw_attr == attr_kind)
5326169689Skan      return a;
5327169689Skan    else if (a->dw_attr == DW_AT_specification
5328169689Skan	     || a->dw_attr == DW_AT_abstract_origin)
5329169689Skan      spec = AT_ref (a);
5330169689Skan
5331169689Skan  if (spec)
5332169689Skan    return get_AT (spec, attr_kind);
533350397Sobrien
533450397Sobrien  return NULL;
533550397Sobrien}
533650397Sobrien
533790075Sobrien/* Return the "low pc" attribute value, typically associated with a subprogram
533890075Sobrien   DIE.  Return null if the "low pc" attribute is either not present, or if it
533990075Sobrien   cannot be represented as an assembler label identifier.  */
534050397Sobrien
534190075Sobrienstatic inline const char *
5342132718Skanget_AT_low_pc (dw_die_ref die)
534350397Sobrien{
534490075Sobrien  dw_attr_ref a = get_AT (die, DW_AT_low_pc);
534550397Sobrien
534690075Sobrien  return a ? AT_lbl (a) : NULL;
534750397Sobrien}
534850397Sobrien
534990075Sobrien/* Return the "high pc" attribute value, typically associated with a subprogram
535090075Sobrien   DIE.  Return null if the "high pc" attribute is either not present, or if it
535190075Sobrien   cannot be represented as an assembler label identifier.  */
535250397Sobrien
535390075Sobrienstatic inline const char *
5354132718Skanget_AT_hi_pc (dw_die_ref die)
535550397Sobrien{
535690075Sobrien  dw_attr_ref a = get_AT (die, DW_AT_high_pc);
535750397Sobrien
535890075Sobrien  return a ? AT_lbl (a) : NULL;
535950397Sobrien}
536050397Sobrien
536150397Sobrien/* Return the value of the string attribute designated by ATTR_KIND, or
536250397Sobrien   NULL if it is not present.  */
536350397Sobrien
536490075Sobrienstatic inline const char *
5365132718Skanget_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
536650397Sobrien{
536790075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
536850397Sobrien
536990075Sobrien  return a ? AT_string (a) : NULL;
537050397Sobrien}
537150397Sobrien
537250397Sobrien/* Return the value of the flag attribute designated by ATTR_KIND, or -1
537350397Sobrien   if it is not present.  */
537450397Sobrien
537550397Sobrienstatic inline int
5376132718Skanget_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
537750397Sobrien{
537890075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
537950397Sobrien
538090075Sobrien  return a ? AT_flag (a) : 0;
538150397Sobrien}
538250397Sobrien
538350397Sobrien/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
538450397Sobrien   if it is not present.  */
538550397Sobrien
538650397Sobrienstatic inline unsigned
5387132718Skanget_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
538850397Sobrien{
538990075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
539050397Sobrien
539190075Sobrien  return a ? AT_unsigned (a) : 0;
539290075Sobrien}
539350397Sobrien
539490075Sobrienstatic inline dw_die_ref
5395132718Skanget_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
539690075Sobrien{
539790075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
539890075Sobrien
539990075Sobrien  return a ? AT_ref (a) : NULL;
540050397Sobrien}
540150397Sobrien
5402169689Skanstatic inline struct dwarf_file_data *
5403169689Skanget_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5404169689Skan{
5405169689Skan  dw_attr_ref a = get_AT (die, attr_kind);
5406169689Skan
5407169689Skan  return a ? AT_file (a) : NULL;
5408169689Skan}
5409169689Skan
5410132718Skan/* Return TRUE if the language is C or C++.  */
5411132718Skan
5412132718Skanstatic inline bool
5413132718Skanis_c_family (void)
541450397Sobrien{
5415132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
541650397Sobrien
5417169689Skan  return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5418169689Skan	  || lang == DW_LANG_C99
5419169689Skan	  || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
542090075Sobrien}
542150397Sobrien
5422132718Skan/* Return TRUE if the language is C++.  */
5423132718Skan
5424132718Skanstatic inline bool
5425132718Skanis_cxx (void)
542690075Sobrien{
5427169689Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5428169689Skan
5429169689Skan  return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5430117395Skan}
543190075Sobrien
5432132718Skan/* Return TRUE if the language is Fortran.  */
5433132718Skan
5434132718Skanstatic inline bool
5435132718Skanis_fortran (void)
543650397Sobrien{
5437132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
543850397Sobrien
5439169689Skan  return (lang == DW_LANG_Fortran77
5440169689Skan	  || lang == DW_LANG_Fortran90
5441169689Skan	  || lang == DW_LANG_Fortran95);
544290075Sobrien}
544350397Sobrien
5444132718Skan/* Return TRUE if the language is Java.  */
5445132718Skan
5446132718Skanstatic inline bool
5447132718Skanis_java (void)
544890075Sobrien{
5449132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
545090075Sobrien
5451132718Skan  return lang == DW_LANG_Java;
545290075Sobrien}
545390075Sobrien
5454132718Skan/* Return TRUE if the language is Ada.  */
545590075Sobrien
5456132718Skanstatic inline bool
5457132718Skanis_ada (void)
545890075Sobrien{
5459132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
546090075Sobrien
5461132718Skan  return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5462132718Skan}
546390075Sobrien
546450397Sobrien/* Remove the specified attribute if present.  */
546550397Sobrien
546690075Sobrienstatic void
5467132718Skanremove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
546850397Sobrien{
5469169689Skan  dw_attr_ref a;
5470169689Skan  unsigned ix;
547150397Sobrien
5472169689Skan  if (! die)
5473169689Skan    return;
547450397Sobrien
5475169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5476169689Skan    if (a->dw_attr == attr_kind)
5477169689Skan      {
5478169689Skan	if (AT_class (a) == dw_val_class_str)
5479169689Skan	  if (a->dw_attr_val.v.val_str->refcount)
5480169689Skan	    a->dw_attr_val.v.val_str->refcount--;
5481169689Skan
5482169689Skan	/* VEC_ordered_remove should help reduce the number of abbrevs
5483169689Skan	   that are needed.  */
5484169689Skan	VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5485169689Skan	return;
5486169689Skan      }
548750397Sobrien}
548850397Sobrien
5489169689Skan/* Remove CHILD from its parent.  PREV must have the property that
5490169689Skan   PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5491146895Skan
5492146895Skanstatic void
5493169689Skanremove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5494146895Skan{
5495169689Skan  gcc_assert (child->die_parent == prev->die_parent);
5496169689Skan  gcc_assert (prev->die_sib == child);
5497169689Skan  if (prev == child)
5498146895Skan    {
5499169689Skan      gcc_assert (child->die_parent->die_child == child);
5500169689Skan      prev = NULL;
5501146895Skan    }
5502169689Skan  else
5503169689Skan    prev->die_sib = child->die_sib;
5504169689Skan  if (child->die_parent->die_child == child)
5505169689Skan    child->die_parent->die_child = prev;
5506146895Skan}
5507146895Skan
5508169689Skan/* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5509169689Skan   matches TAG.  */
551090075Sobrien
5511169689Skanstatic void
5512169689Skanremove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
551390075Sobrien{
5514169689Skan  dw_die_ref c;
5515169689Skan
5516169689Skan  c = die->die_child;
5517169689Skan  if (c) do {
5518169689Skan    dw_die_ref prev = c;
5519169689Skan    c = c->die_sib;
5520169689Skan    while (c->die_tag == tag)
5521169689Skan      {
5522169689Skan	remove_child_with_prev (c, prev);
5523169689Skan	/* Might have removed every child.  */
5524169689Skan	if (c == c->die_sib)
5525169689Skan	  return;
5526169689Skan	c = c->die_sib;
5527169689Skan      }
5528169689Skan  } while (c != die->die_child);
552990075Sobrien}
553090075Sobrien
5531169689Skan/* Add a CHILD_DIE as the last child of DIE.  */
553250397Sobrien
553390075Sobrienstatic void
5534169689Skanadd_child_die (dw_die_ref die, dw_die_ref child_die)
553550397Sobrien{
5536169689Skan  /* FIXME this should probably be an assert.  */
5537169689Skan  if (! die || ! child_die)
5538169689Skan    return;
5539169689Skan  gcc_assert (die != child_die);
554050397Sobrien
5541169689Skan  child_die->die_parent = die;
5542169689Skan  if (die->die_child)
554350397Sobrien    {
5544169689Skan      child_die->die_sib = die->die_child->die_sib;
5545169689Skan      die->die_child->die_sib = child_die;
554650397Sobrien    }
5547169689Skan  else
5548169689Skan    child_die->die_sib = child_die;
5549169689Skan  die->die_child = child_die;
555050397Sobrien}
555150397Sobrien
555290075Sobrien/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5553169689Skan   is the specification, to the end of PARENT's list of children.
5554169689Skan   This is done by removing and re-adding it.  */
555590075Sobrien
555690075Sobrienstatic void
5557132718Skansplice_child_die (dw_die_ref parent, dw_die_ref child)
555890075Sobrien{
5559169689Skan  dw_die_ref p;
556090075Sobrien
556190075Sobrien  /* We want the declaration DIE from inside the class, not the
556290075Sobrien     specification DIE at toplevel.  */
556390075Sobrien  if (child->die_parent != parent)
556490075Sobrien    {
556590075Sobrien      dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
556690075Sobrien
556790075Sobrien      if (tmp)
556890075Sobrien	child = tmp;
556950397Sobrien    }
557090075Sobrien
5571169689Skan  gcc_assert (child->die_parent == parent
5572169689Skan	      || (child->die_parent
5573169689Skan		  == get_AT_ref (parent, DW_AT_specification)));
5574169689Skan
5575169689Skan  for (p = child->die_parent->die_child; ; p = p->die_sib)
5576169689Skan    if (p->die_sib == child)
557790075Sobrien      {
5578169689Skan	remove_child_with_prev (child, p);
557990075Sobrien	break;
558090075Sobrien      }
558190075Sobrien
5582169689Skan  add_child_die (parent, child);
558350397Sobrien}
558450397Sobrien
558550397Sobrien/* Return a pointer to a newly created DIE node.  */
558650397Sobrien
558750397Sobrienstatic inline dw_die_ref
5588132718Skannew_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
558950397Sobrien{
5590132718Skan  dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
559150397Sobrien
559250397Sobrien  die->die_tag = tag_value;
559350397Sobrien
559450397Sobrien  if (parent_die != NULL)
559550397Sobrien    add_child_die (parent_die, die);
559650397Sobrien  else
559750397Sobrien    {
559850397Sobrien      limbo_die_node *limbo_node;
559950397Sobrien
5600132718Skan      limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
560150397Sobrien      limbo_node->die = die;
560290075Sobrien      limbo_node->created_for = t;
560350397Sobrien      limbo_node->next = limbo_die_list;
560450397Sobrien      limbo_die_list = limbo_node;
560550397Sobrien    }
560650397Sobrien
560750397Sobrien  return die;
560850397Sobrien}
560950397Sobrien
561050397Sobrien/* Return the DIE associated with the given type specifier.  */
561150397Sobrien
561250397Sobrienstatic inline dw_die_ref
5613132718Skanlookup_type_die (tree type)
561450397Sobrien{
5615117395Skan  return TYPE_SYMTAB_DIE (type);
561650397Sobrien}
561750397Sobrien
561850397Sobrien/* Equate a DIE to a given type specifier.  */
561950397Sobrien
562090075Sobrienstatic inline void
5621132718Skanequate_type_number_to_die (tree type, dw_die_ref type_die)
562250397Sobrien{
5623117395Skan  TYPE_SYMTAB_DIE (type) = type_die;
562450397Sobrien}
562550397Sobrien
5626169689Skan/* Returns a hash value for X (which really is a die_struct).  */
5627169689Skan
5628169689Skanstatic hashval_t
5629169689Skandecl_die_table_hash (const void *x)
5630169689Skan{
5631169689Skan  return (hashval_t) ((const dw_die_ref) x)->decl_id;
5632169689Skan}
5633169689Skan
5634169689Skan/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5635169689Skan
5636169689Skanstatic int
5637169689Skandecl_die_table_eq (const void *x, const void *y)
5638169689Skan{
5639169689Skan  return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5640169689Skan}
5641169689Skan
564250397Sobrien/* Return the DIE associated with a given declaration.  */
564350397Sobrien
564450397Sobrienstatic inline dw_die_ref
5645132718Skanlookup_decl_die (tree decl)
564650397Sobrien{
5647169689Skan  return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5648169689Skan}
564950397Sobrien
5650169689Skan/* Returns a hash value for X (which really is a var_loc_list).  */
5651169689Skan
5652169689Skanstatic hashval_t
5653169689Skandecl_loc_table_hash (const void *x)
5654169689Skan{
5655169689Skan  return (hashval_t) ((const var_loc_list *) x)->decl_id;
565650397Sobrien}
565750397Sobrien
5658169689Skan/* Return nonzero if decl_id of var_loc_list X is the same as
5659169689Skan   UID of decl *Y.  */
5660169689Skan
5661169689Skanstatic int
5662169689Skandecl_loc_table_eq (const void *x, const void *y)
5663169689Skan{
5664169689Skan  return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5665169689Skan}
5666169689Skan
5667169689Skan/* Return the var_loc list associated with a given declaration.  */
5668169689Skan
5669169689Skanstatic inline var_loc_list *
5670169689Skanlookup_decl_loc (tree decl)
5671169689Skan{
5672169689Skan  return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5673169689Skan}
5674169689Skan
567550397Sobrien/* Equate a DIE to a particular declaration.  */
567650397Sobrien
567750397Sobrienstatic void
5678132718Skanequate_decl_number_to_die (tree decl, dw_die_ref decl_die)
567950397Sobrien{
568090075Sobrien  unsigned int decl_id = DECL_UID (decl);
5681169689Skan  void **slot;
568250397Sobrien
5683169689Skan  slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5684169689Skan  *slot = decl_die;
5685169689Skan  decl_die->decl_id = decl_id;
5686169689Skan}
568750397Sobrien
5688169689Skan/* Add a variable location node to the linked list for DECL.  */
568950397Sobrien
5690169689Skanstatic void
5691169689Skanadd_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5692169689Skan{
5693169689Skan  unsigned int decl_id = DECL_UID (decl);
5694169689Skan  var_loc_list *temp;
5695169689Skan  void **slot;
5696169689Skan
5697169689Skan  slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5698169689Skan  if (*slot == NULL)
5699169689Skan    {
5700169689Skan      temp = ggc_alloc_cleared (sizeof (var_loc_list));
5701169689Skan      temp->decl_id = decl_id;
5702169689Skan      *slot = temp;
570350397Sobrien    }
5704169689Skan  else
5705169689Skan    temp = *slot;
570650397Sobrien
5707169689Skan  if (temp->last)
5708169689Skan    {
5709169689Skan      /* If the current location is the same as the end of the list,
5710169689Skan	 we have nothing to do.  */
5711169689Skan      if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5712169689Skan			NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5713169689Skan	{
5714169689Skan	  /* Add LOC to the end of list and update LAST.  */
5715169689Skan	  temp->last->next = loc;
5716169689Skan	  temp->last = loc;
5717169689Skan	}
5718169689Skan    }
5719169689Skan  /* Do not add empty location to the beginning of the list.  */
5720169689Skan  else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5721169689Skan    {
5722169689Skan      temp->first = loc;
5723169689Skan      temp->last = loc;
5724169689Skan    }
572550397Sobrien}
572650397Sobrien
572750397Sobrien/* Keep track of the number of spaces used to indent the
572850397Sobrien   output of the debugging routines that print the structure of
572950397Sobrien   the DIE internal representation.  */
573050397Sobrienstatic int print_indent;
573150397Sobrien
573250397Sobrien/* Indent the line the number of spaces given by print_indent.  */
573350397Sobrien
573450397Sobrienstatic inline void
5735132718Skanprint_spaces (FILE *outfile)
573650397Sobrien{
573750397Sobrien  fprintf (outfile, "%*s", print_indent, "");
573850397Sobrien}
573950397Sobrien
574050397Sobrien/* Print the information associated with a given DIE, and its children.
574150397Sobrien   This routine is a debugging aid only.  */
574250397Sobrien
574350397Sobrienstatic void
5744132718Skanprint_die (dw_die_ref die, FILE *outfile)
574550397Sobrien{
574690075Sobrien  dw_attr_ref a;
574790075Sobrien  dw_die_ref c;
5748169689Skan  unsigned ix;
574950397Sobrien
575050397Sobrien  print_spaces (outfile);
5751259947Spfg  fprintf (outfile, "DIE %4ld: %s\n",
575250397Sobrien	   die->die_offset, dwarf_tag_name (die->die_tag));
575350397Sobrien  print_spaces (outfile);
575450397Sobrien  fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5755259947Spfg  fprintf (outfile, " offset: %ld\n", die->die_offset);
575650397Sobrien
5757169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
575850397Sobrien    {
575950397Sobrien      print_spaces (outfile);
576050397Sobrien      fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
576150397Sobrien
576290075Sobrien      switch (AT_class (a))
576350397Sobrien	{
576450397Sobrien	case dw_val_class_addr:
576550397Sobrien	  fprintf (outfile, "address");
576650397Sobrien	  break;
576790075Sobrien	case dw_val_class_offset:
576890075Sobrien	  fprintf (outfile, "offset");
576990075Sobrien	  break;
577050397Sobrien	case dw_val_class_loc:
577150397Sobrien	  fprintf (outfile, "location descriptor");
577250397Sobrien	  break;
577390075Sobrien	case dw_val_class_loc_list:
577490075Sobrien	  fprintf (outfile, "location list -> label:%s",
577590075Sobrien		   AT_loc_list (a)->ll_symbol);
577690075Sobrien	  break;
577790075Sobrien	case dw_val_class_range_list:
577890075Sobrien	  fprintf (outfile, "range list");
577990075Sobrien	  break;
578050397Sobrien	case dw_val_class_const:
5781132718Skan	  fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
578250397Sobrien	  break;
578350397Sobrien	case dw_val_class_unsigned_const:
5784132718Skan	  fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
578550397Sobrien	  break;
578650397Sobrien	case dw_val_class_long_long:
578750397Sobrien	  fprintf (outfile, "constant (%lu,%lu)",
578890075Sobrien		   a->dw_attr_val.v.val_long_long.hi,
578990075Sobrien		   a->dw_attr_val.v.val_long_long.low);
579050397Sobrien	  break;
5791132718Skan	case dw_val_class_vec:
5792132718Skan	  fprintf (outfile, "floating-point or vector constant");
579350397Sobrien	  break;
579450397Sobrien	case dw_val_class_flag:
579590075Sobrien	  fprintf (outfile, "%u", AT_flag (a));
579650397Sobrien	  break;
579750397Sobrien	case dw_val_class_die_ref:
579890075Sobrien	  if (AT_ref (a) != NULL)
579990075Sobrien	    {
580090075Sobrien	      if (AT_ref (a)->die_symbol)
580190075Sobrien		fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
580290075Sobrien	      else
5803259947Spfg		fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
580490075Sobrien	    }
580550397Sobrien	  else
580650397Sobrien	    fprintf (outfile, "die -> <null>");
580750397Sobrien	  break;
580850397Sobrien	case dw_val_class_lbl_id:
5809169689Skan	case dw_val_class_lineptr:
5810169689Skan	case dw_val_class_macptr:
581190075Sobrien	  fprintf (outfile, "label: %s", AT_lbl (a));
581250397Sobrien	  break;
581350397Sobrien	case dw_val_class_str:
581490075Sobrien	  if (AT_string (a) != NULL)
581590075Sobrien	    fprintf (outfile, "\"%s\"", AT_string (a));
581650397Sobrien	  else
581750397Sobrien	    fprintf (outfile, "<null>");
581850397Sobrien	  break;
5819169689Skan	case dw_val_class_file:
5820169689Skan	  fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5821169689Skan		   AT_file (a)->emitted_number);
5822169689Skan	  break;
582350397Sobrien	default:
582450397Sobrien	  break;
582550397Sobrien	}
582650397Sobrien
582750397Sobrien      fprintf (outfile, "\n");
582850397Sobrien    }
582950397Sobrien
583050397Sobrien  if (die->die_child != NULL)
583150397Sobrien    {
583250397Sobrien      print_indent += 4;
5833169689Skan      FOR_EACH_CHILD (die, c, print_die (c, outfile));
583450397Sobrien      print_indent -= 4;
583550397Sobrien    }
583690075Sobrien  if (print_indent == 0)
583790075Sobrien    fprintf (outfile, "\n");
583850397Sobrien}
583950397Sobrien
584050397Sobrien/* Print the contents of the source code line number correspondence table.
584150397Sobrien   This routine is a debugging aid only.  */
584250397Sobrien
584350397Sobrienstatic void
5844132718Skanprint_dwarf_line_table (FILE *outfile)
584550397Sobrien{
584690075Sobrien  unsigned i;
584790075Sobrien  dw_line_info_ref line_info;
584850397Sobrien
584950397Sobrien  fprintf (outfile, "\n\nDWARF source line information\n");
585090075Sobrien  for (i = 1; i < line_info_table_in_use; i++)
585150397Sobrien    {
585250397Sobrien      line_info = &line_info_table[i];
5853169689Skan      fprintf (outfile, "%5d: %4ld %6ld\n", i,
5854169689Skan	       line_info->dw_file_num,
5855169689Skan	       line_info->dw_line_num);
585650397Sobrien    }
585750397Sobrien
585850397Sobrien  fprintf (outfile, "\n\n");
585950397Sobrien}
586050397Sobrien
586150397Sobrien/* Print the information collected for a given DIE.  */
586250397Sobrien
586350397Sobrienvoid
5864132718Skandebug_dwarf_die (dw_die_ref die)
586550397Sobrien{
586650397Sobrien  print_die (die, stderr);
586750397Sobrien}
586850397Sobrien
586950397Sobrien/* Print all DWARF information collected for the compilation unit.
587050397Sobrien   This routine is a debugging aid only.  */
587150397Sobrien
587250397Sobrienvoid
5873132718Skandebug_dwarf (void)
587450397Sobrien{
587550397Sobrien  print_indent = 0;
587650397Sobrien  print_die (comp_unit_die, stderr);
587790075Sobrien  if (! DWARF2_ASM_LINE_DEBUG_INFO)
587890075Sobrien    print_dwarf_line_table (stderr);
587950397Sobrien}
588050397Sobrien
588190075Sobrien/* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
588290075Sobrien   for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
588390075Sobrien   DIE that marks the start of the DIEs for this include file.  */
588490075Sobrien
588590075Sobrienstatic dw_die_ref
5886132718Skanpush_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
588790075Sobrien{
588890075Sobrien  const char *filename = get_AT_string (bincl_die, DW_AT_name);
588990075Sobrien  dw_die_ref new_unit = gen_compile_unit_die (filename);
589090075Sobrien
589190075Sobrien  new_unit->die_sib = old_unit;
589290075Sobrien  return new_unit;
589390075Sobrien}
589490075Sobrien
589590075Sobrien/* Close an include-file CU and reopen the enclosing one.  */
589690075Sobrien
589790075Sobrienstatic dw_die_ref
5898132718Skanpop_compile_unit (dw_die_ref old_unit)
589990075Sobrien{
590090075Sobrien  dw_die_ref new_unit = old_unit->die_sib;
590190075Sobrien
590290075Sobrien  old_unit->die_sib = NULL;
590390075Sobrien  return new_unit;
590490075Sobrien}
590590075Sobrien
590690075Sobrien#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
590790075Sobrien#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
590890075Sobrien
590990075Sobrien/* Calculate the checksum of a location expression.  */
591090075Sobrien
591190075Sobrienstatic inline void
5912132718Skanloc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
591390075Sobrien{
591490075Sobrien  CHECKSUM (loc->dw_loc_opc);
591590075Sobrien  CHECKSUM (loc->dw_loc_oprnd1);
591690075Sobrien  CHECKSUM (loc->dw_loc_oprnd2);
591790075Sobrien}
591890075Sobrien
591990075Sobrien/* Calculate the checksum of an attribute.  */
592090075Sobrien
592190075Sobrienstatic void
5922132718Skanattr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
592390075Sobrien{
592490075Sobrien  dw_loc_descr_ref loc;
592590075Sobrien  rtx r;
592690075Sobrien
592790075Sobrien  CHECKSUM (at->dw_attr);
592890075Sobrien
5929169689Skan  /* We don't care that this was compiled with a different compiler
5930169689Skan     snapshot; if the output is the same, that's what matters.  */
5931169689Skan  if (at->dw_attr == DW_AT_producer)
593290075Sobrien    return;
593390075Sobrien
593490075Sobrien  switch (AT_class (at))
593590075Sobrien    {
593690075Sobrien    case dw_val_class_const:
593790075Sobrien      CHECKSUM (at->dw_attr_val.v.val_int);
593890075Sobrien      break;
593990075Sobrien    case dw_val_class_unsigned_const:
594090075Sobrien      CHECKSUM (at->dw_attr_val.v.val_unsigned);
594190075Sobrien      break;
594290075Sobrien    case dw_val_class_long_long:
594390075Sobrien      CHECKSUM (at->dw_attr_val.v.val_long_long);
594490075Sobrien      break;
5945132718Skan    case dw_val_class_vec:
5946132718Skan      CHECKSUM (at->dw_attr_val.v.val_vec);
594790075Sobrien      break;
594890075Sobrien    case dw_val_class_flag:
594990075Sobrien      CHECKSUM (at->dw_attr_val.v.val_flag);
595090075Sobrien      break;
595190075Sobrien    case dw_val_class_str:
595290075Sobrien      CHECKSUM_STRING (AT_string (at));
595390075Sobrien      break;
595490075Sobrien
595590075Sobrien    case dw_val_class_addr:
595690075Sobrien      r = AT_addr (at);
5957169689Skan      gcc_assert (GET_CODE (r) == SYMBOL_REF);
5958169689Skan      CHECKSUM_STRING (XSTR (r, 0));
595990075Sobrien      break;
596090075Sobrien
596190075Sobrien    case dw_val_class_offset:
596290075Sobrien      CHECKSUM (at->dw_attr_val.v.val_offset);
596390075Sobrien      break;
596490075Sobrien
596590075Sobrien    case dw_val_class_loc:
596690075Sobrien      for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
596790075Sobrien	loc_checksum (loc, ctx);
596890075Sobrien      break;
596990075Sobrien
597090075Sobrien    case dw_val_class_die_ref:
5971117395Skan      die_checksum (AT_ref (at), ctx, mark);
5972117395Skan      break;
597390075Sobrien
597490075Sobrien    case dw_val_class_fde_ref:
597590075Sobrien    case dw_val_class_lbl_id:
5976169689Skan    case dw_val_class_lineptr:
5977169689Skan    case dw_val_class_macptr:
597890075Sobrien      break;
597990075Sobrien
5980169689Skan    case dw_val_class_file:
5981169689Skan      CHECKSUM_STRING (AT_file (at)->filename);
5982169689Skan      break;
5983169689Skan
598490075Sobrien    default:
598590075Sobrien      break;
598690075Sobrien    }
598790075Sobrien}
598890075Sobrien
598990075Sobrien/* Calculate the checksum of a DIE.  */
599090075Sobrien
599190075Sobrienstatic void
5992132718Skandie_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
599390075Sobrien{
599490075Sobrien  dw_die_ref c;
599590075Sobrien  dw_attr_ref a;
5996169689Skan  unsigned ix;
599790075Sobrien
5998117395Skan  /* To avoid infinite recursion.  */
5999117395Skan  if (die->die_mark)
6000117395Skan    {
6001117395Skan      CHECKSUM (die->die_mark);
6002117395Skan      return;
6003117395Skan    }
6004117395Skan  die->die_mark = ++(*mark);
6005117395Skan
600690075Sobrien  CHECKSUM (die->die_tag);
600790075Sobrien
6008169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6009117395Skan    attr_checksum (a, ctx, mark);
601090075Sobrien
6011169689Skan  FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
601290075Sobrien}
601390075Sobrien
601490075Sobrien#undef CHECKSUM
601590075Sobrien#undef CHECKSUM_STRING
601690075Sobrien
6017117395Skan/* Do the location expressions look same?  */
6018117395Skanstatic inline int
6019132718Skansame_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6020117395Skan{
6021117395Skan  return loc1->dw_loc_opc == loc2->dw_loc_opc
6022117395Skan	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6023117395Skan	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6024117395Skan}
6025117395Skan
6026117395Skan/* Do the values look the same?  */
6027117395Skanstatic int
6028132718Skansame_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6029117395Skan{
6030117395Skan  dw_loc_descr_ref loc1, loc2;
6031117395Skan  rtx r1, r2;
6032117395Skan
6033117395Skan  if (v1->val_class != v2->val_class)
6034117395Skan    return 0;
6035117395Skan
6036117395Skan  switch (v1->val_class)
6037117395Skan    {
6038117395Skan    case dw_val_class_const:
6039117395Skan      return v1->v.val_int == v2->v.val_int;
6040117395Skan    case dw_val_class_unsigned_const:
6041117395Skan      return v1->v.val_unsigned == v2->v.val_unsigned;
6042117395Skan    case dw_val_class_long_long:
6043117395Skan      return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6044132718Skan	     && v1->v.val_long_long.low == v2->v.val_long_long.low;
6045132718Skan    case dw_val_class_vec:
6046132718Skan      if (v1->v.val_vec.length != v2->v.val_vec.length
6047132718Skan	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6048117395Skan	return 0;
6049132718Skan      if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6050132718Skan		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
6051132718Skan	return 0;
6052117395Skan      return 1;
6053117395Skan    case dw_val_class_flag:
6054117395Skan      return v1->v.val_flag == v2->v.val_flag;
6055117395Skan    case dw_val_class_str:
6056132718Skan      return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6057117395Skan
6058117395Skan    case dw_val_class_addr:
6059117395Skan      r1 = v1->v.val_addr;
6060117395Skan      r2 = v2->v.val_addr;
6061117395Skan      if (GET_CODE (r1) != GET_CODE (r2))
6062117395Skan	return 0;
6063169689Skan      gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6064169689Skan      return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6065117395Skan
6066117395Skan    case dw_val_class_offset:
6067117395Skan      return v1->v.val_offset == v2->v.val_offset;
6068117395Skan
6069117395Skan    case dw_val_class_loc:
6070117395Skan      for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6071117395Skan	   loc1 && loc2;
6072117395Skan	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6073117395Skan	if (!same_loc_p (loc1, loc2, mark))
6074117395Skan	  return 0;
6075117395Skan      return !loc1 && !loc2;
6076117395Skan
6077117395Skan    case dw_val_class_die_ref:
6078117395Skan      return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6079117395Skan
6080117395Skan    case dw_val_class_fde_ref:
6081117395Skan    case dw_val_class_lbl_id:
6082169689Skan    case dw_val_class_lineptr:
6083169689Skan    case dw_val_class_macptr:
6084117395Skan      return 1;
6085117395Skan
6086169689Skan    case dw_val_class_file:
6087169689Skan      return v1->v.val_file == v2->v.val_file;
6088169689Skan
6089117395Skan    default:
6090117395Skan      return 1;
6091117395Skan    }
6092117395Skan}
6093117395Skan
6094117395Skan/* Do the attributes look the same?  */
6095117395Skan
6096117395Skanstatic int
6097132718Skansame_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6098117395Skan{
6099117395Skan  if (at1->dw_attr != at2->dw_attr)
6100117395Skan    return 0;
6101117395Skan
6102169689Skan  /* We don't care that this was compiled with a different compiler
6103169689Skan     snapshot; if the output is the same, that's what matters. */
6104169689Skan  if (at1->dw_attr == DW_AT_producer)
6105117395Skan    return 1;
6106117395Skan
6107117395Skan  return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6108117395Skan}
6109117395Skan
6110117395Skan/* Do the dies look the same?  */
6111117395Skan
6112117395Skanstatic int
6113132718Skansame_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6114117395Skan{
6115117395Skan  dw_die_ref c1, c2;
6116169689Skan  dw_attr_ref a1;
6117169689Skan  unsigned ix;
6118117395Skan
6119117395Skan  /* To avoid infinite recursion.  */
6120117395Skan  if (die1->die_mark)
6121117395Skan    return die1->die_mark == die2->die_mark;
6122117395Skan  die1->die_mark = die2->die_mark = ++(*mark);
6123117395Skan
6124117395Skan  if (die1->die_tag != die2->die_tag)
6125117395Skan    return 0;
6126117395Skan
6127169689Skan  if (VEC_length (dw_attr_node, die1->die_attr)
6128169689Skan      != VEC_length (dw_attr_node, die2->die_attr))
6129117395Skan    return 0;
6130169689Skan
6131169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6132169689Skan    if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6133117395Skan      return 0;
6134117395Skan
6135169689Skan  c1 = die1->die_child;
6136169689Skan  c2 = die2->die_child;
6137169689Skan  if (! c1)
6138169689Skan    {
6139169689Skan      if (c2)
6140169689Skan	return 0;
6141169689Skan    }
6142169689Skan  else
6143169689Skan    for (;;)
6144169689Skan      {
6145169689Skan	if (!same_die_p (c1, c2, mark))
6146169689Skan	  return 0;
6147169689Skan	c1 = c1->die_sib;
6148169689Skan	c2 = c2->die_sib;
6149169689Skan	if (c1 == die1->die_child)
6150169689Skan	  {
6151169689Skan	    if (c2 == die2->die_child)
6152169689Skan	      break;
6153169689Skan	    else
6154169689Skan	      return 0;
6155169689Skan	  }
6156169689Skan    }
6157169689Skan
6158117395Skan  return 1;
6159117395Skan}
6160117395Skan
6161117395Skan/* Do the dies look the same?  Wrapper around same_die_p.  */
6162117395Skan
6163117395Skanstatic int
6164132718Skansame_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6165117395Skan{
6166117395Skan  int mark = 0;
6167117395Skan  int ret = same_die_p (die1, die2, &mark);
6168117395Skan
6169117395Skan  unmark_all_dies (die1);
6170117395Skan  unmark_all_dies (die2);
6171117395Skan
6172117395Skan  return ret;
6173117395Skan}
6174117395Skan
617590075Sobrien/* The prefix to attach to symbols on DIEs in the current comdat debug
617690075Sobrien   info section.  */
617790075Sobrienstatic char *comdat_symbol_id;
617890075Sobrien
617990075Sobrien/* The index of the current symbol within the current comdat CU.  */
618090075Sobrienstatic unsigned int comdat_symbol_number;
618190075Sobrien
618290075Sobrien/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
618390075Sobrien   children, and set comdat_symbol_id accordingly.  */
618490075Sobrien
618590075Sobrienstatic void
6186132718Skancompute_section_prefix (dw_die_ref unit_die)
618790075Sobrien{
6188117395Skan  const char *die_name = get_AT_string (unit_die, DW_AT_name);
6189117395Skan  const char *base = die_name ? lbasename (die_name) : "anonymous";
6190132718Skan  char *name = alloca (strlen (base) + 64);
619190075Sobrien  char *p;
6192117395Skan  int i, mark;
619390075Sobrien  unsigned char checksum[16];
619490075Sobrien  struct md5_ctx ctx;
619590075Sobrien
619690075Sobrien  /* Compute the checksum of the DIE, then append part of it as hex digits to
619790075Sobrien     the name filename of the unit.  */
619890075Sobrien
619990075Sobrien  md5_init_ctx (&ctx);
6200117395Skan  mark = 0;
6201117395Skan  die_checksum (unit_die, &ctx, &mark);
6202117395Skan  unmark_all_dies (unit_die);
620390075Sobrien  md5_finish_ctx (&ctx, checksum);
620490075Sobrien
620590075Sobrien  sprintf (name, "%s.", base);
620690075Sobrien  clean_symbol_name (name);
620790075Sobrien
620890075Sobrien  p = name + strlen (name);
620990075Sobrien  for (i = 0; i < 4; i++)
621090075Sobrien    {
621190075Sobrien      sprintf (p, "%.2x", checksum[i]);
621290075Sobrien      p += 2;
621390075Sobrien    }
621490075Sobrien
621590075Sobrien  comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
621690075Sobrien  comdat_symbol_number = 0;
621790075Sobrien}
621890075Sobrien
621990075Sobrien/* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
622090075Sobrien
622190075Sobrienstatic int
6222132718Skanis_type_die (dw_die_ref die)
622390075Sobrien{
622490075Sobrien  switch (die->die_tag)
622590075Sobrien    {
622690075Sobrien    case DW_TAG_array_type:
622790075Sobrien    case DW_TAG_class_type:
622890075Sobrien    case DW_TAG_enumeration_type:
622990075Sobrien    case DW_TAG_pointer_type:
623090075Sobrien    case DW_TAG_reference_type:
623190075Sobrien    case DW_TAG_string_type:
623290075Sobrien    case DW_TAG_structure_type:
623390075Sobrien    case DW_TAG_subroutine_type:
623490075Sobrien    case DW_TAG_union_type:
623590075Sobrien    case DW_TAG_ptr_to_member_type:
623690075Sobrien    case DW_TAG_set_type:
623790075Sobrien    case DW_TAG_subrange_type:
623890075Sobrien    case DW_TAG_base_type:
623990075Sobrien    case DW_TAG_const_type:
624090075Sobrien    case DW_TAG_file_type:
624190075Sobrien    case DW_TAG_packed_type:
624290075Sobrien    case DW_TAG_volatile_type:
6243117395Skan    case DW_TAG_typedef:
624490075Sobrien      return 1;
624590075Sobrien    default:
624690075Sobrien      return 0;
624790075Sobrien    }
624890075Sobrien}
624990075Sobrien
625090075Sobrien/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
625190075Sobrien   Basically, we want to choose the bits that are likely to be shared between
625290075Sobrien   compilations (types) and leave out the bits that are specific to individual
625390075Sobrien   compilations (functions).  */
625490075Sobrien
625590075Sobrienstatic int
6256132718Skanis_comdat_die (dw_die_ref c)
625790075Sobrien{
625890075Sobrien  /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
625990075Sobrien     we do for stabs.  The advantage is a greater likelihood of sharing between
626090075Sobrien     objects that don't include headers in the same order (and therefore would
626190075Sobrien     put the base types in a different comdat).  jason 8/28/00 */
626290075Sobrien
626390075Sobrien  if (c->die_tag == DW_TAG_base_type)
626490075Sobrien    return 0;
626590075Sobrien
626690075Sobrien  if (c->die_tag == DW_TAG_pointer_type
626790075Sobrien      || c->die_tag == DW_TAG_reference_type
626890075Sobrien      || c->die_tag == DW_TAG_const_type
626990075Sobrien      || c->die_tag == DW_TAG_volatile_type)
627090075Sobrien    {
627190075Sobrien      dw_die_ref t = get_AT_ref (c, DW_AT_type);
627290075Sobrien
627390075Sobrien      return t ? is_comdat_die (t) : 0;
627490075Sobrien    }
627590075Sobrien
627690075Sobrien  return is_type_die (c);
627790075Sobrien}
627890075Sobrien
627990075Sobrien/* Returns 1 iff C is the sort of DIE that might be referred to from another
628090075Sobrien   compilation unit.  */
628190075Sobrien
628290075Sobrienstatic int
6283132718Skanis_symbol_die (dw_die_ref c)
628490075Sobrien{
628590075Sobrien  return (is_type_die (c)
6286117395Skan	  || (get_AT (c, DW_AT_declaration)
6287169689Skan	      && !get_AT (c, DW_AT_specification))
6288169689Skan	  || c->die_tag == DW_TAG_namespace);
628990075Sobrien}
629090075Sobrien
629190075Sobrienstatic char *
6292132718Skangen_internal_sym (const char *prefix)
629390075Sobrien{
629490075Sobrien  char buf[256];
629590075Sobrien
629690075Sobrien  ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
629790075Sobrien  return xstrdup (buf);
629890075Sobrien}
629990075Sobrien
630090075Sobrien/* Assign symbols to all worthy DIEs under DIE.  */
630190075Sobrien
630290075Sobrienstatic void
6303132718Skanassign_symbol_names (dw_die_ref die)
630490075Sobrien{
630590075Sobrien  dw_die_ref c;
630690075Sobrien
630790075Sobrien  if (is_symbol_die (die))
630890075Sobrien    {
630990075Sobrien      if (comdat_symbol_id)
631090075Sobrien	{
631190075Sobrien	  char *p = alloca (strlen (comdat_symbol_id) + 64);
631290075Sobrien
631390075Sobrien	  sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
631490075Sobrien		   comdat_symbol_id, comdat_symbol_number++);
631590075Sobrien	  die->die_symbol = xstrdup (p);
631690075Sobrien	}
631790075Sobrien      else
631890075Sobrien	die->die_symbol = gen_internal_sym ("LDIE");
631990075Sobrien    }
632090075Sobrien
6321169689Skan  FOR_EACH_CHILD (die, c, assign_symbol_names (c));
632290075Sobrien}
632390075Sobrien
6324117395Skanstruct cu_hash_table_entry
6325117395Skan{
6326117395Skan  dw_die_ref cu;
6327117395Skan  unsigned min_comdat_num, max_comdat_num;
6328117395Skan  struct cu_hash_table_entry *next;
6329117395Skan};
6330117395Skan
6331117395Skan/* Routines to manipulate hash table of CUs.  */
6332117395Skanstatic hashval_t
6333132718Skanhtab_cu_hash (const void *of)
6334117395Skan{
6335117395Skan  const struct cu_hash_table_entry *entry = of;
6336117395Skan
6337117395Skan  return htab_hash_string (entry->cu->die_symbol);
6338117395Skan}
6339117395Skan
6340117395Skanstatic int
6341132718Skanhtab_cu_eq (const void *of1, const void *of2)
6342117395Skan{
6343117395Skan  const struct cu_hash_table_entry *entry1 = of1;
6344117395Skan  const struct die_struct *entry2 = of2;
6345117395Skan
6346117395Skan  return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6347117395Skan}
6348117395Skan
6349117395Skanstatic void
6350132718Skanhtab_cu_del (void *what)
6351117395Skan{
6352117395Skan  struct cu_hash_table_entry *next, *entry = what;
6353117395Skan
6354117395Skan  while (entry)
6355117395Skan    {
6356117395Skan      next = entry->next;
6357117395Skan      free (entry);
6358117395Skan      entry = next;
6359117395Skan    }
6360117395Skan}
6361117395Skan
6362117395Skan/* Check whether we have already seen this CU and set up SYM_NUM
6363117395Skan   accordingly.  */
6364117395Skanstatic int
6365132718Skancheck_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6366117395Skan{
6367117395Skan  struct cu_hash_table_entry dummy;
6368117395Skan  struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6369117395Skan
6370117395Skan  dummy.max_comdat_num = 0;
6371117395Skan
6372117395Skan  slot = (struct cu_hash_table_entry **)
6373117395Skan    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6374117395Skan	INSERT);
6375117395Skan  entry = *slot;
6376117395Skan
6377117395Skan  for (; entry; last = entry, entry = entry->next)
6378117395Skan    {
6379117395Skan      if (same_die_p_wrap (cu, entry->cu))
6380117395Skan	break;
6381117395Skan    }
6382117395Skan
6383117395Skan  if (entry)
6384117395Skan    {
6385117395Skan      *sym_num = entry->min_comdat_num;
6386117395Skan      return 1;
6387117395Skan    }
6388117395Skan
6389169689Skan  entry = XCNEW (struct cu_hash_table_entry);
6390117395Skan  entry->cu = cu;
6391117395Skan  entry->min_comdat_num = *sym_num = last->max_comdat_num;
6392117395Skan  entry->next = *slot;
6393117395Skan  *slot = entry;
6394117395Skan
6395117395Skan  return 0;
6396117395Skan}
6397117395Skan
6398117395Skan/* Record SYM_NUM to record of CU in HTABLE.  */
6399117395Skanstatic void
6400132718Skanrecord_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6401117395Skan{
6402117395Skan  struct cu_hash_table_entry **slot, *entry;
6403117395Skan
6404117395Skan  slot = (struct cu_hash_table_entry **)
6405117395Skan    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6406117395Skan	NO_INSERT);
6407117395Skan  entry = *slot;
6408117395Skan
6409117395Skan  entry->max_comdat_num = sym_num;
6410117395Skan}
6411117395Skan
641290075Sobrien/* Traverse the DIE (which is always comp_unit_die), and set up
641390075Sobrien   additional compilation units for each of the include files we see
641490075Sobrien   bracketed by BINCL/EINCL.  */
641590075Sobrien
641690075Sobrienstatic void
6417132718Skanbreak_out_includes (dw_die_ref die)
641890075Sobrien{
6419169689Skan  dw_die_ref c;
642090075Sobrien  dw_die_ref unit = NULL;
6421117395Skan  limbo_die_node *node, **pnode;
6422117395Skan  htab_t cu_hash_table;
642390075Sobrien
6424169689Skan  c = die->die_child;
6425169689Skan  if (c) do {
6426169689Skan    dw_die_ref prev = c;
6427169689Skan    c = c->die_sib;
6428169689Skan    while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6429169689Skan	   || (unit && is_comdat_die (c)))
6430169689Skan      {
6431169689Skan	dw_die_ref next = c->die_sib;
643290075Sobrien
6433169689Skan	/* This DIE is for a secondary CU; remove it from the main one.  */
6434169689Skan	remove_child_with_prev (c, prev);
6435169689Skan
6436169689Skan	if (c->die_tag == DW_TAG_GNU_BINCL)
6437169689Skan	  unit = push_new_compile_unit (unit, c);
6438169689Skan	else if (c->die_tag == DW_TAG_GNU_EINCL)
6439169689Skan	  unit = pop_compile_unit (unit);
6440169689Skan	else
6441169689Skan	  add_child_die (unit, c);
6442169689Skan	c = next;
6443169689Skan	if (c == die->die_child)
6444169689Skan	  break;
6445169689Skan      }
6446169689Skan  } while (c != die->die_child);
644790075Sobrien
644890075Sobrien#if 0
644990075Sobrien  /* We can only use this in debugging, since the frontend doesn't check
645090075Sobrien     to make sure that we leave every include file we enter.  */
6451169689Skan  gcc_assert (!unit);
645290075Sobrien#endif
645390075Sobrien
645490075Sobrien  assign_symbol_names (die);
6455117395Skan  cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6456117395Skan  for (node = limbo_die_list, pnode = &limbo_die_list;
6457117395Skan       node;
6458117395Skan       node = node->next)
645990075Sobrien    {
6460117395Skan      int is_dupl;
6461117395Skan
646290075Sobrien      compute_section_prefix (node->die);
6463117395Skan      is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6464117395Skan			&comdat_symbol_number);
646590075Sobrien      assign_symbol_names (node->die);
6466117395Skan      if (is_dupl)
6467117395Skan	*pnode = node->next;
6468117395Skan      else
6469132718Skan	{
6470117395Skan	  pnode = &node->next;
6471117395Skan	  record_comdat_symbol_number (node->die, cu_hash_table,
6472117395Skan		comdat_symbol_number);
6473117395Skan	}
647490075Sobrien    }
6475117395Skan  htab_delete (cu_hash_table);
647690075Sobrien}
647790075Sobrien
647890075Sobrien/* Traverse the DIE and add a sibling attribute if it may have the
647990075Sobrien   effect of speeding up access to siblings.  To save some space,
648090075Sobrien   avoid generating sibling attributes for DIE's without children.  */
648190075Sobrien
648290075Sobrienstatic void
6483132718Skanadd_sibling_attributes (dw_die_ref die)
648490075Sobrien{
648590075Sobrien  dw_die_ref c;
648690075Sobrien
6487169689Skan  if (! die->die_child)
6488169689Skan    return;
6489169689Skan
6490169689Skan  if (die->die_parent && die != die->die_parent->die_child)
649190075Sobrien    add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
649290075Sobrien
6493169689Skan  FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
649450397Sobrien}
649550397Sobrien
649690075Sobrien/* Output all location lists for the DIE and its children.  */
649750397Sobrien
649850397Sobrienstatic void
6499132718Skanoutput_location_lists (dw_die_ref die)
650090075Sobrien{
650190075Sobrien  dw_die_ref c;
6502169689Skan  dw_attr_ref a;
6503169689Skan  unsigned ix;
650490075Sobrien
6505169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6506169689Skan    if (AT_class (a) == dw_val_class_loc_list)
6507169689Skan      output_loc_list (AT_loc_list (a));
650890075Sobrien
6509169689Skan  FOR_EACH_CHILD (die, c, output_location_lists (c));
651090075Sobrien}
6511117395Skan
651290075Sobrien/* The format of each DIE (and its attribute value pairs) is encoded in an
651390075Sobrien   abbreviation table.  This routine builds the abbreviation table and assigns
651490075Sobrien   a unique abbreviation id for each abbreviation entry.  The children of each
651590075Sobrien   die are visited recursively.  */
651690075Sobrien
651790075Sobrienstatic void
6518132718Skanbuild_abbrev_table (dw_die_ref die)
651950397Sobrien{
652090075Sobrien  unsigned long abbrev_id;
652190075Sobrien  unsigned int n_alloc;
652290075Sobrien  dw_die_ref c;
6523169689Skan  dw_attr_ref a;
6524169689Skan  unsigned ix;
652590075Sobrien
652690075Sobrien  /* Scan the DIE references, and mark as external any that refer to
652790075Sobrien     DIEs from other CUs (i.e. those which are not marked).  */
6528169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6529169689Skan    if (AT_class (a) == dw_val_class_die_ref
6530169689Skan	&& AT_ref (a)->die_mark == 0)
653190075Sobrien      {
6532169689Skan	gcc_assert (AT_ref (a)->die_symbol);
653390075Sobrien
6534169689Skan	set_AT_ref_external (a, 1);
653590075Sobrien      }
653690075Sobrien
653750397Sobrien  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
653850397Sobrien    {
653990075Sobrien      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6540169689Skan      dw_attr_ref die_a, abbrev_a;
6541169689Skan      unsigned ix;
6542169689Skan      bool ok = true;
6543169689Skan
6544169689Skan      if (abbrev->die_tag != die->die_tag)
6545169689Skan	continue;
6546169689Skan      if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6547169689Skan	continue;
6548169689Skan
6549169689Skan      if (VEC_length (dw_attr_node, abbrev->die_attr)
6550169689Skan	  != VEC_length (dw_attr_node, die->die_attr))
6551169689Skan	continue;
6552169689Skan
6553169689Skan      for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
655450397Sobrien	{
6555169689Skan	  abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6556169689Skan	  if ((abbrev_a->dw_attr != die_a->dw_attr)
6557169689Skan	      || (value_format (abbrev_a) != value_format (die_a)))
655850397Sobrien	    {
6559169689Skan	      ok = false;
6560169689Skan	      break;
656150397Sobrien	    }
656250397Sobrien	}
6563169689Skan      if (ok)
6564169689Skan	break;
656550397Sobrien    }
656650397Sobrien
656750397Sobrien  if (abbrev_id >= abbrev_die_table_in_use)
656850397Sobrien    {
656950397Sobrien      if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
657050397Sobrien	{
657150397Sobrien	  n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6572132718Skan	  abbrev_die_table = ggc_realloc (abbrev_die_table,
6573132718Skan					  sizeof (dw_die_ref) * n_alloc);
657450397Sobrien
6575132718Skan	  memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
657650397Sobrien		 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
657750397Sobrien	  abbrev_die_table_allocated = n_alloc;
657850397Sobrien	}
657950397Sobrien
658050397Sobrien      ++abbrev_die_table_in_use;
658150397Sobrien      abbrev_die_table[abbrev_id] = die;
658250397Sobrien    }
658350397Sobrien
658450397Sobrien  die->die_abbrev = abbrev_id;
6585169689Skan  FOR_EACH_CHILD (die, c, build_abbrev_table (c));
658650397Sobrien}
658750397Sobrien
658850397Sobrien/* Return the power-of-two number of bytes necessary to represent VALUE.  */
658950397Sobrien
659050397Sobrienstatic int
6591132718Skanconstant_size (long unsigned int value)
659250397Sobrien{
659350397Sobrien  int log;
659450397Sobrien
659550397Sobrien  if (value == 0)
659650397Sobrien    log = 0;
659750397Sobrien  else
659850397Sobrien    log = floor_log2 (value);
659950397Sobrien
660050397Sobrien  log = log / 8;
660150397Sobrien  log = 1 << (floor_log2 (log) + 1);
660250397Sobrien
660350397Sobrien  return log;
660450397Sobrien}
660550397Sobrien
660690075Sobrien/* Return the size of a DIE as it is represented in the
660750397Sobrien   .debug_info section.  */
660850397Sobrien
660950397Sobrienstatic unsigned long
6610132718Skansize_of_die (dw_die_ref die)
661150397Sobrien{
661290075Sobrien  unsigned long size = 0;
661390075Sobrien  dw_attr_ref a;
6614169689Skan  unsigned ix;
661550397Sobrien
661650397Sobrien  size += size_of_uleb128 (die->die_abbrev);
6617169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
661850397Sobrien    {
661990075Sobrien      switch (AT_class (a))
662050397Sobrien	{
662150397Sobrien	case dw_val_class_addr:
662290075Sobrien	  size += DWARF2_ADDR_SIZE;
662350397Sobrien	  break;
662490075Sobrien	case dw_val_class_offset:
662590075Sobrien	  size += DWARF_OFFSET_SIZE;
662690075Sobrien	  break;
662750397Sobrien	case dw_val_class_loc:
662850397Sobrien	  {
662990075Sobrien	    unsigned long lsize = size_of_locs (AT_loc (a));
663050397Sobrien
663150397Sobrien	    /* Block length.  */
663250397Sobrien	    size += constant_size (lsize);
663350397Sobrien	    size += lsize;
663450397Sobrien	  }
663550397Sobrien	  break;
663690075Sobrien	case dw_val_class_loc_list:
663790075Sobrien	  size += DWARF_OFFSET_SIZE;
663890075Sobrien	  break;
663990075Sobrien	case dw_val_class_range_list:
664090075Sobrien	  size += DWARF_OFFSET_SIZE;
664190075Sobrien	  break;
664250397Sobrien	case dw_val_class_const:
664390075Sobrien	  size += size_of_sleb128 (AT_int (a));
664450397Sobrien	  break;
664550397Sobrien	case dw_val_class_unsigned_const:
664690075Sobrien	  size += constant_size (AT_unsigned (a));
664750397Sobrien	  break;
664850397Sobrien	case dw_val_class_long_long:
664990075Sobrien	  size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
665050397Sobrien	  break;
6651132718Skan	case dw_val_class_vec:
6652132718Skan	  size += 1 + (a->dw_attr_val.v.val_vec.length
6653132718Skan		       * a->dw_attr_val.v.val_vec.elt_size); /* block */
665450397Sobrien	  break;
665550397Sobrien	case dw_val_class_flag:
665650397Sobrien	  size += 1;
665750397Sobrien	  break;
665850397Sobrien	case dw_val_class_die_ref:
6659132718Skan	  if (AT_ref_external (a))
6660132718Skan	    size += DWARF2_ADDR_SIZE;
6661132718Skan	  else
6662132718Skan	    size += DWARF_OFFSET_SIZE;
666350397Sobrien	  break;
666450397Sobrien	case dw_val_class_fde_ref:
666550397Sobrien	  size += DWARF_OFFSET_SIZE;
666650397Sobrien	  break;
666750397Sobrien	case dw_val_class_lbl_id:
666890075Sobrien	  size += DWARF2_ADDR_SIZE;
666950397Sobrien	  break;
6670169689Skan	case dw_val_class_lineptr:
6671169689Skan	case dw_val_class_macptr:
667250397Sobrien	  size += DWARF_OFFSET_SIZE;
667350397Sobrien	  break;
667450397Sobrien	case dw_val_class_str:
667590075Sobrien	  if (AT_string_form (a) == DW_FORM_strp)
667690075Sobrien	    size += DWARF_OFFSET_SIZE;
667790075Sobrien	  else
6678132718Skan	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
667950397Sobrien	  break;
6680169689Skan	case dw_val_class_file:
6681169689Skan	  size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6682169689Skan	  break;
668350397Sobrien	default:
6684169689Skan	  gcc_unreachable ();
668550397Sobrien	}
668650397Sobrien    }
668750397Sobrien
668850397Sobrien  return size;
668950397Sobrien}
669050397Sobrien
669190075Sobrien/* Size the debugging information associated with a given DIE.  Visits the
669290075Sobrien   DIE's children recursively.  Updates the global variable next_die_offset, on
669390075Sobrien   each time through.  Uses the current value of next_die_offset to update the
669490075Sobrien   die_offset field in each DIE.  */
669550397Sobrien
669650397Sobrienstatic void
6697132718Skancalc_die_sizes (dw_die_ref die)
669850397Sobrien{
669990075Sobrien  dw_die_ref c;
670090075Sobrien
670150397Sobrien  die->die_offset = next_die_offset;
670250397Sobrien  next_die_offset += size_of_die (die);
670350397Sobrien
6704169689Skan  FOR_EACH_CHILD (die, c, calc_die_sizes (c));
670550397Sobrien
670650397Sobrien  if (die->die_child != NULL)
670750397Sobrien    /* Count the null byte used to terminate sibling lists.  */
670850397Sobrien    next_die_offset += 1;
670950397Sobrien}
671050397Sobrien
671190075Sobrien/* Set the marks for a die and its children.  We do this so
671290075Sobrien   that we know whether or not a reference needs to use FORM_ref_addr; only
671390075Sobrien   DIEs in the same CU will be marked.  We used to clear out the offset
671490075Sobrien   and use that as the flag, but ran into ordering problems.  */
671550397Sobrien
671690075Sobrienstatic void
6717132718Skanmark_dies (dw_die_ref die)
671850397Sobrien{
671990075Sobrien  dw_die_ref c;
672050397Sobrien
6721169689Skan  gcc_assert (!die->die_mark);
6722132718Skan
672390075Sobrien  die->die_mark = 1;
6724169689Skan  FOR_EACH_CHILD (die, c, mark_dies (c));
672550397Sobrien}
672650397Sobrien
672790075Sobrien/* Clear the marks for a die and its children.  */
672850397Sobrien
672990075Sobrienstatic void
6730132718Skanunmark_dies (dw_die_ref die)
673150397Sobrien{
673290075Sobrien  dw_die_ref c;
673350397Sobrien
6734169689Skan  gcc_assert (die->die_mark);
6735132718Skan
673690075Sobrien  die->die_mark = 0;
6737169689Skan  FOR_EACH_CHILD (die, c, unmark_dies (c));
673850397Sobrien}
673950397Sobrien
6740117395Skan/* Clear the marks for a die, its children and referred dies.  */
6741117395Skan
6742117395Skanstatic void
6743132718Skanunmark_all_dies (dw_die_ref die)
6744117395Skan{
6745117395Skan  dw_die_ref c;
6746117395Skan  dw_attr_ref a;
6747169689Skan  unsigned ix;
6748117395Skan
6749117395Skan  if (!die->die_mark)
6750117395Skan    return;
6751117395Skan  die->die_mark = 0;
6752117395Skan
6753169689Skan  FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6754117395Skan
6755169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6756117395Skan    if (AT_class (a) == dw_val_class_die_ref)
6757117395Skan      unmark_all_dies (AT_ref (a));
6758117395Skan}
6759117395Skan
6760260395Spfg/* Return the size of the .debug_pubnames or .debug_pubtypes table
6761260395Spfg   generated for the compilation unit.  */
676250397Sobrien
676350397Sobrienstatic unsigned long
6764260395Spfgsize_of_pubnames (VEC (pubname_entry, gc) * names)
676550397Sobrien{
676690075Sobrien  unsigned long size;
676790075Sobrien  unsigned i;
6768260395Spfg  pubname_ref p;
676950397Sobrien
677050397Sobrien  size = DWARF_PUBNAMES_HEADER_SIZE;
6771260395Spfg  for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6772260395Spfg    if (names != pubtype_table
6773260395Spfg	|| p->die->die_offset != 0
6774260395Spfg	|| !flag_eliminate_unused_debug_types)
6775260395Spfg      size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
677650397Sobrien
677750397Sobrien  size += DWARF_OFFSET_SIZE;
677850397Sobrien  return size;
677950397Sobrien}
678050397Sobrien
678150397Sobrien/* Return the size of the information in the .debug_aranges section.  */
678250397Sobrien
678350397Sobrienstatic unsigned long
6784132718Skansize_of_aranges (void)
678550397Sobrien{
678690075Sobrien  unsigned long size;
678750397Sobrien
678850397Sobrien  size = DWARF_ARANGES_HEADER_SIZE;
678950397Sobrien
679050397Sobrien  /* Count the address/length pair for this compilation unit.  */
679190075Sobrien  size += 2 * DWARF2_ADDR_SIZE;
679290075Sobrien  size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
679350397Sobrien
679450397Sobrien  /* Count the two zero words used to terminated the address range table.  */
679590075Sobrien  size += 2 * DWARF2_ADDR_SIZE;
679650397Sobrien  return size;
679750397Sobrien}
679850397Sobrien
679950397Sobrien/* Select the encoding of an attribute value.  */
680050397Sobrien
680150397Sobrienstatic enum dwarf_form
6802132718Skanvalue_format (dw_attr_ref a)
680350397Sobrien{
680490075Sobrien  switch (a->dw_attr_val.val_class)
680550397Sobrien    {
680650397Sobrien    case dw_val_class_addr:
680750397Sobrien      return DW_FORM_addr;
680890075Sobrien    case dw_val_class_range_list:
680990075Sobrien    case dw_val_class_offset:
681090075Sobrien    case dw_val_class_loc_list:
6811169689Skan      switch (DWARF_OFFSET_SIZE)
6812169689Skan	{
6813169689Skan	case 4:
6814169689Skan	  return DW_FORM_data4;
6815169689Skan	case 8:
6816169689Skan	  return DW_FORM_data8;
6817169689Skan	default:
6818169689Skan	  gcc_unreachable ();
6819169689Skan	}
682050397Sobrien    case dw_val_class_loc:
682190075Sobrien      switch (constant_size (size_of_locs (AT_loc (a))))
682250397Sobrien	{
682350397Sobrien	case 1:
682450397Sobrien	  return DW_FORM_block1;
682550397Sobrien	case 2:
682650397Sobrien	  return DW_FORM_block2;
682750397Sobrien	default:
6828169689Skan	  gcc_unreachable ();
682950397Sobrien	}
683050397Sobrien    case dw_val_class_const:
683190075Sobrien      return DW_FORM_sdata;
683250397Sobrien    case dw_val_class_unsigned_const:
683390075Sobrien      switch (constant_size (AT_unsigned (a)))
683450397Sobrien	{
683550397Sobrien	case 1:
683650397Sobrien	  return DW_FORM_data1;
683750397Sobrien	case 2:
683850397Sobrien	  return DW_FORM_data2;
683950397Sobrien	case 4:
684050397Sobrien	  return DW_FORM_data4;
684150397Sobrien	case 8:
684250397Sobrien	  return DW_FORM_data8;
684350397Sobrien	default:
6844169689Skan	  gcc_unreachable ();
684550397Sobrien	}
684650397Sobrien    case dw_val_class_long_long:
684750397Sobrien      return DW_FORM_block1;
6848132718Skan    case dw_val_class_vec:
684950397Sobrien      return DW_FORM_block1;
685050397Sobrien    case dw_val_class_flag:
685150397Sobrien      return DW_FORM_flag;
685250397Sobrien    case dw_val_class_die_ref:
685390075Sobrien      if (AT_ref_external (a))
685490075Sobrien	return DW_FORM_ref_addr;
685590075Sobrien      else
685690075Sobrien	return DW_FORM_ref;
685750397Sobrien    case dw_val_class_fde_ref:
685850397Sobrien      return DW_FORM_data;
685950397Sobrien    case dw_val_class_lbl_id:
686050397Sobrien      return DW_FORM_addr;
6861169689Skan    case dw_val_class_lineptr:
6862169689Skan    case dw_val_class_macptr:
686350397Sobrien      return DW_FORM_data;
686450397Sobrien    case dw_val_class_str:
686590075Sobrien      return AT_string_form (a);
6866169689Skan    case dw_val_class_file:
6867169689Skan      switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6868169689Skan	{
6869169689Skan	case 1:
6870169689Skan	  return DW_FORM_data1;
6871169689Skan	case 2:
6872169689Skan	  return DW_FORM_data2;
6873169689Skan	case 4:
6874169689Skan	  return DW_FORM_data4;
6875169689Skan	default:
6876169689Skan	  gcc_unreachable ();
6877169689Skan	}
687890075Sobrien
687950397Sobrien    default:
6880169689Skan      gcc_unreachable ();
688150397Sobrien    }
688250397Sobrien}
688350397Sobrien
688450397Sobrien/* Output the encoding of an attribute value.  */
688550397Sobrien
688650397Sobrienstatic void
6887132718Skanoutput_value_format (dw_attr_ref a)
688850397Sobrien{
688990075Sobrien  enum dwarf_form form = value_format (a);
689050397Sobrien
689190075Sobrien  dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
689250397Sobrien}
689350397Sobrien
689450397Sobrien/* Output the .debug_abbrev section which defines the DIE abbreviation
689550397Sobrien   table.  */
689650397Sobrien
689750397Sobrienstatic void
6898132718Skanoutput_abbrev_section (void)
689950397Sobrien{
690050397Sobrien  unsigned long abbrev_id;
690150397Sobrien
690250397Sobrien  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
690350397Sobrien    {
690490075Sobrien      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6905169689Skan      unsigned ix;
6906169689Skan      dw_attr_ref a_attr;
690750397Sobrien
690890075Sobrien      dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
690990075Sobrien      dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
691090075Sobrien				   dwarf_tag_name (abbrev->die_tag));
691150397Sobrien
691290075Sobrien      if (abbrev->die_child != NULL)
691390075Sobrien	dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
691490075Sobrien      else
691590075Sobrien	dw2_asm_output_data (1, DW_children_no, "DW_children_no");
691650397Sobrien
6917169689Skan      for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6918169689Skan	   ix++)
691950397Sobrien	{
692090075Sobrien	  dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
692190075Sobrien				       dwarf_attr_name (a_attr->dw_attr));
692290075Sobrien	  output_value_format (a_attr);
692350397Sobrien	}
692450397Sobrien
692590075Sobrien      dw2_asm_output_data (1, 0, NULL);
692690075Sobrien      dw2_asm_output_data (1, 0, NULL);
692750397Sobrien    }
692852284Sobrien
692990075Sobrien  /* Terminate the table.  */
693090075Sobrien  dw2_asm_output_data (1, 0, NULL);
693150397Sobrien}
693250397Sobrien
693390075Sobrien/* Output a symbol we can use to refer to this DIE from another CU.  */
693450397Sobrien
693590075Sobrienstatic inline void
6936132718Skanoutput_die_symbol (dw_die_ref die)
693790075Sobrien{
693890075Sobrien  char *sym = die->die_symbol;
693990075Sobrien
694090075Sobrien  if (sym == 0)
694190075Sobrien    return;
694290075Sobrien
694390075Sobrien  if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
694490075Sobrien    /* We make these global, not weak; if the target doesn't support
694590075Sobrien       .linkonce, it doesn't support combining the sections, so debugging
694690075Sobrien       will break.  */
6947169689Skan    targetm.asm_out.globalize_label (asm_out_file, sym);
694890075Sobrien
694990075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, sym);
695090075Sobrien}
695190075Sobrien
695290075Sobrien/* Return a new location list, given the begin and end range, and the
695390075Sobrien   expression. gensym tells us whether to generate a new internal symbol for
695490075Sobrien   this location list node, which is done for the head of the list only.  */
695590075Sobrien
695690075Sobrienstatic inline dw_loc_list_ref
6957132718Skannew_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6958132718Skan	      const char *section, unsigned int gensym)
695990075Sobrien{
6960132718Skan  dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
696190075Sobrien
696290075Sobrien  retlist->begin = begin;
696390075Sobrien  retlist->end = end;
696490075Sobrien  retlist->expr = expr;
696590075Sobrien  retlist->section = section;
6966117395Skan  if (gensym)
696790075Sobrien    retlist->ll_symbol = gen_internal_sym ("LLST");
696890075Sobrien
696990075Sobrien  return retlist;
697090075Sobrien}
697190075Sobrien
6972132718Skan/* Add a location description expression to a location list.  */
697390075Sobrien
697490075Sobrienstatic inline void
6975132718Skanadd_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6976132718Skan			   const char *begin, const char *end,
6977132718Skan			   const char *section)
697890075Sobrien{
697990075Sobrien  dw_loc_list_ref *d;
6980117395Skan
698190075Sobrien  /* Find the end of the chain.  */
698290075Sobrien  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
698390075Sobrien    ;
698490075Sobrien
6985132718Skan  /* Add a new location list node to the list.  */
698690075Sobrien  *d = new_loc_list (descr, begin, end, section, 0);
698790075Sobrien}
698890075Sobrien
6989169689Skanstatic void
6990169689Skandwarf2out_switch_text_section (void)
6991169689Skan{
6992169689Skan  dw_fde_ref fde;
6993169689Skan
6994169689Skan  gcc_assert (cfun);
6995169689Skan
6996169689Skan  fde = &fde_table[fde_table_in_use - 1];
6997169689Skan  fde->dw_fde_switched_sections = true;
6998169689Skan  fde->dw_fde_hot_section_label = cfun->hot_section_label;
6999169689Skan  fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7000169689Skan  fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7001169689Skan  fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7002169689Skan  have_multiple_function_sections = true;
7003169689Skan
7004169689Skan  /* Reset the current label on switching text sections, so that we
7005169689Skan     don't attempt to advance_loc4 between labels in different sections.  */
7006169689Skan  fde->dw_fde_current_label = NULL;
7007169689Skan}
7008169689Skan
7009132718Skan/* Output the location list given to us.  */
701090075Sobrien
701150397Sobrienstatic void
7012132718Skanoutput_loc_list (dw_loc_list_ref list_head)
701350397Sobrien{
701490075Sobrien  dw_loc_list_ref curr = list_head;
701550397Sobrien
701690075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
701790075Sobrien
7018169689Skan  /* Walk the location list, and output each range + expression.  */
7019117395Skan  for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
702090075Sobrien    {
702190075Sobrien      unsigned long size;
7022169689Skan      if (!have_multiple_function_sections)
7023169689Skan	{
7024169689Skan	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7025169689Skan				"Location list begin address (%s)",
7026169689Skan				list_head->ll_symbol);
7027169689Skan	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7028169689Skan				"Location list end address (%s)",
7029169689Skan				list_head->ll_symbol);
7030169689Skan	}
7031169689Skan      else
7032169689Skan	{
7033169689Skan	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7034169689Skan			       "Location list begin address (%s)",
7035169689Skan			       list_head->ll_symbol);
7036169689Skan	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7037169689Skan			       "Location list end address (%s)",
7038169689Skan			       list_head->ll_symbol);
7039169689Skan	}
704090075Sobrien      size = size_of_locs (curr->expr);
7041117395Skan
704290075Sobrien      /* Output the block length for this list of location operations.  */
7043169689Skan      gcc_assert (size <= 0xffff);
704490075Sobrien      dw2_asm_output_data (2, size, "%s", "Location expression size");
704550397Sobrien
704690075Sobrien      output_loc_sequence (curr->expr);
704790075Sobrien    }
704850397Sobrien
7049169689Skan  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
705090075Sobrien		       "Location list terminator begin (%s)",
705190075Sobrien		       list_head->ll_symbol);
7052169689Skan  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
705390075Sobrien		       "Location list terminator end (%s)",
705490075Sobrien		       list_head->ll_symbol);
705550397Sobrien}
705650397Sobrien
705750397Sobrien/* Output the DIE and its attributes.  Called recursively to generate
705850397Sobrien   the definitions of each child DIE.  */
705950397Sobrien
706050397Sobrienstatic void
7061132718Skanoutput_die (dw_die_ref die)
706250397Sobrien{
706390075Sobrien  dw_attr_ref a;
706490075Sobrien  dw_die_ref c;
706590075Sobrien  unsigned long size;
7066169689Skan  unsigned ix;
706750397Sobrien
706890075Sobrien  /* If someone in another CU might refer to us, set up a symbol for
706990075Sobrien     them to point to.  */
707090075Sobrien  if (die->die_symbol)
707190075Sobrien    output_die_symbol (die);
707250397Sobrien
707390075Sobrien  dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7074259947Spfg			       (unsigned long)die->die_offset,
7075259947Spfg			       dwarf_tag_name (die->die_tag));
707650397Sobrien
7077169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
707850397Sobrien    {
707990075Sobrien      const char *name = dwarf_attr_name (a->dw_attr);
708090075Sobrien
708190075Sobrien      switch (AT_class (a))
708250397Sobrien	{
708350397Sobrien	case dw_val_class_addr:
708490075Sobrien	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
708550397Sobrien	  break;
708650397Sobrien
708790075Sobrien	case dw_val_class_offset:
708890075Sobrien	  dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
708990075Sobrien			       "%s", name);
709090075Sobrien	  break;
709150397Sobrien
709290075Sobrien	case dw_val_class_range_list:
709390075Sobrien	  {
709490075Sobrien	    char *p = strchr (ranges_section_label, '\0');
709550397Sobrien
7096132718Skan	    sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7097132718Skan		     a->dw_attr_val.v.val_offset);
709890075Sobrien	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7099169689Skan				   debug_ranges_section, "%s", name);
710090075Sobrien	    *p = '\0';
710190075Sobrien	  }
710290075Sobrien	  break;
710350397Sobrien
710490075Sobrien	case dw_val_class_loc:
710590075Sobrien	  size = size_of_locs (AT_loc (a));
710650397Sobrien
710790075Sobrien	  /* Output the block length for this list of location operations.  */
710890075Sobrien	  dw2_asm_output_data (constant_size (size), size, "%s", name);
710950397Sobrien
711090075Sobrien	  output_loc_sequence (AT_loc (a));
711150397Sobrien	  break;
711250397Sobrien
711350397Sobrien	case dw_val_class_const:
711490075Sobrien	  /* ??? It would be slightly more efficient to use a scheme like is
711590075Sobrien	     used for unsigned constants below, but gdb 4.x does not sign
711690075Sobrien	     extend.  Gdb 5.x does sign extend.  */
711790075Sobrien	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
711850397Sobrien	  break;
711950397Sobrien
712050397Sobrien	case dw_val_class_unsigned_const:
712190075Sobrien	  dw2_asm_output_data (constant_size (AT_unsigned (a)),
712290075Sobrien			       AT_unsigned (a), "%s", name);
712350397Sobrien	  break;
712450397Sobrien
712550397Sobrien	case dw_val_class_long_long:
712690075Sobrien	  {
712790075Sobrien	    unsigned HOST_WIDE_INT first, second;
712850397Sobrien
712990075Sobrien	    dw2_asm_output_data (1,
713090075Sobrien				 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7131117395Skan				 "%s", name);
713250397Sobrien
713390075Sobrien	    if (WORDS_BIG_ENDIAN)
713490075Sobrien	      {
713590075Sobrien		first = a->dw_attr_val.v.val_long_long.hi;
713690075Sobrien		second = a->dw_attr_val.v.val_long_long.low;
713790075Sobrien	      }
713890075Sobrien	    else
713990075Sobrien	      {
714090075Sobrien		first = a->dw_attr_val.v.val_long_long.low;
714190075Sobrien		second = a->dw_attr_val.v.val_long_long.hi;
714290075Sobrien	      }
714390075Sobrien
714490075Sobrien	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
714590075Sobrien				 first, "long long constant");
714690075Sobrien	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
714790075Sobrien				 second, NULL);
714890075Sobrien	  }
714950397Sobrien	  break;
715050397Sobrien
7151132718Skan	case dw_val_class_vec:
715252284Sobrien	  {
7153132718Skan	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7154132718Skan	    unsigned int len = a->dw_attr_val.v.val_vec.length;
715590075Sobrien	    unsigned int i;
7156132718Skan	    unsigned char *p;
715750397Sobrien
7158132718Skan	    dw2_asm_output_data (1, len * elt_size, "%s", name);
7159132718Skan	    if (elt_size > sizeof (HOST_WIDE_INT))
7160132718Skan	      {
7161132718Skan		elt_size /= 2;
7162132718Skan		len *= 2;
7163132718Skan	      }
7164132718Skan	    for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7165132718Skan		 i < len;
7166132718Skan		 i++, p += elt_size)
7167132718Skan	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7168132718Skan				   "fp or vector constant word %u", i);
716990075Sobrien	    break;
717052284Sobrien	  }
717150397Sobrien
717250397Sobrien	case dw_val_class_flag:
717390075Sobrien	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
717450397Sobrien	  break;
717550397Sobrien
7176117395Skan	case dw_val_class_loc_list:
717790075Sobrien	  {
717890075Sobrien	    char *sym = AT_loc_list (a)->ll_symbol;
717990075Sobrien
7180169689Skan	    gcc_assert (sym);
7181169689Skan	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7182169689Skan				   "%s", name);
718390075Sobrien	  }
718490075Sobrien	  break;
718590075Sobrien
718650397Sobrien	case dw_val_class_die_ref:
718790075Sobrien	  if (AT_ref_external (a))
718890075Sobrien	    {
718990075Sobrien	      char *sym = AT_ref (a)->die_symbol;
719090075Sobrien
7191169689Skan	      gcc_assert (sym);
7192169689Skan	      dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7193169689Skan				     "%s", name);
719490075Sobrien	    }
719550397Sobrien	  else
7196169689Skan	    {
7197169689Skan	      gcc_assert (AT_ref (a)->die_offset);
7198169689Skan	      dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7199169689Skan				   "%s", name);
7200169689Skan	    }
720150397Sobrien	  break;
720250397Sobrien
720350397Sobrien	case dw_val_class_fde_ref:
720450397Sobrien	  {
720550397Sobrien	    char l1[20];
720690075Sobrien
720790075Sobrien	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
720890075Sobrien					 a->dw_attr_val.v.val_fde_index * 2);
7209169689Skan	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7210169689Skan				   "%s", name);
721150397Sobrien	  }
721250397Sobrien	  break;
721350397Sobrien
721450397Sobrien	case dw_val_class_lbl_id:
721590075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
721650397Sobrien	  break;
721750397Sobrien
7218169689Skan	case dw_val_class_lineptr:
7219169689Skan	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7220169689Skan				 debug_line_section, "%s", name);
722150397Sobrien	  break;
722250397Sobrien
7223169689Skan	case dw_val_class_macptr:
7224169689Skan	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7225169689Skan				 debug_macinfo_section, "%s", name);
7226169689Skan	  break;
7227169689Skan
722850397Sobrien	case dw_val_class_str:
722990075Sobrien	  if (AT_string_form (a) == DW_FORM_strp)
723090075Sobrien	    dw2_asm_output_offset (DWARF_OFFSET_SIZE,
723190075Sobrien				   a->dw_attr_val.v.val_str->label,
7232169689Skan				   debug_str_section,
723390075Sobrien				   "%s: \"%s\"", name, AT_string (a));
723450397Sobrien	  else
723590075Sobrien	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
723650397Sobrien	  break;
723750397Sobrien
7238169689Skan	case dw_val_class_file:
7239169689Skan	  {
7240169689Skan	    int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7241169689Skan
7242169689Skan	    dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7243169689Skan				 a->dw_attr_val.v.val_file->filename);
7244169689Skan	    break;
7245169689Skan	  }
7246169689Skan
724750397Sobrien	default:
7248169689Skan	  gcc_unreachable ();
724950397Sobrien	}
725050397Sobrien    }
725150397Sobrien
7252169689Skan  FOR_EACH_CHILD (die, c, output_die (c));
725350397Sobrien
725490075Sobrien  /* Add null byte to terminate sibling list.  */
725550397Sobrien  if (die->die_child != NULL)
725690075Sobrien    dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7257259947Spfg			 (unsigned long) die->die_offset);
725850397Sobrien}
725950397Sobrien
726050397Sobrien/* Output the compilation unit that appears at the beginning of the
726150397Sobrien   .debug_info section, and precedes the DIE descriptions.  */
726250397Sobrien
726350397Sobrienstatic void
7264132718Skanoutput_compilation_unit_header (void)
726550397Sobrien{
7266132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7267132718Skan    dw2_asm_output_data (4, 0xffffffff,
7268132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
7269132718Skan  dw2_asm_output_data (DWARF_OFFSET_SIZE,
7270132718Skan                       next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
727190075Sobrien		       "Length of Compilation Unit Info");
727290075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
727390075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7274169689Skan			 debug_abbrev_section,
727590075Sobrien			 "Offset Into Abbrev. Section");
727690075Sobrien  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
727790075Sobrien}
727850397Sobrien
727990075Sobrien/* Output the compilation unit DIE and its children.  */
728050397Sobrien
728190075Sobrienstatic void
7282132718Skanoutput_comp_unit (dw_die_ref die, int output_if_empty)
728390075Sobrien{
728490075Sobrien  const char *secname;
7285117395Skan  char *oldsym, *tmp;
728650397Sobrien
7287117395Skan  /* Unless we are outputting main CU, we may throw away empty ones.  */
7288117395Skan  if (!output_if_empty && die->die_child == NULL)
7289117395Skan    return;
7290117395Skan
729190075Sobrien  /* Even if there are no children of this DIE, we must output the information
729290075Sobrien     about the compilation unit.  Otherwise, on an empty translation unit, we
729390075Sobrien     will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
729490075Sobrien     will then complain when examining the file.  First mark all the DIEs in
729590075Sobrien     this CU so we know which get local refs.  */
729690075Sobrien  mark_dies (die);
729750397Sobrien
729890075Sobrien  build_abbrev_table (die);
729990075Sobrien
730090075Sobrien  /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
730190075Sobrien  next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
730290075Sobrien  calc_die_sizes (die);
730390075Sobrien
7304117395Skan  oldsym = die->die_symbol;
7305117395Skan  if (oldsym)
730690075Sobrien    {
7307132718Skan      tmp = alloca (strlen (oldsym) + 24);
730890075Sobrien
7309117395Skan      sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
731090075Sobrien      secname = tmp;
731190075Sobrien      die->die_symbol = NULL;
7312169689Skan      switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
731390075Sobrien    }
731490075Sobrien  else
7315169689Skan    switch_to_section (debug_info_section);
731690075Sobrien
731790075Sobrien  /* Output debugging information.  */
731890075Sobrien  output_compilation_unit_header ();
731990075Sobrien  output_die (die);
732090075Sobrien
732190075Sobrien  /* Leave the marks on the main CU, so we can check them in
732290075Sobrien     output_pubnames.  */
7323117395Skan  if (oldsym)
7324117395Skan    {
7325117395Skan      unmark_dies (die);
7326117395Skan      die->die_symbol = oldsym;
7327117395Skan    }
732850397Sobrien}
732950397Sobrien
7330169689Skan/* Return the DWARF2/3 pubname associated with a decl.  */
733150397Sobrien
733290075Sobrienstatic const char *
7333132718Skandwarf2_name (tree decl, int scope)
733450397Sobrien{
7335169689Skan  return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
733650397Sobrien}
733750397Sobrien
733850397Sobrien/* Add a new entry to .debug_pubnames if appropriate.  */
733950397Sobrien
734050397Sobrienstatic void
7341132718Skanadd_pubname (tree decl, dw_die_ref die)
734250397Sobrien{
7343260395Spfg  pubname_entry e;
734450397Sobrien
734550397Sobrien  if (! TREE_PUBLIC (decl))
734650397Sobrien    return;
734750397Sobrien
7348260395Spfg  e.die = die;
7349260395Spfg  e.name = xstrdup (dwarf2_name (decl, 1));
7350260395Spfg  VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7351260395Spfg}
7352260395Spfg
7353260395Spfg/* Add a new entry to .debug_pubtypes if appropriate.  */
7354260395Spfg
7355260395Spfgstatic void
7356260395Spfgadd_pubtype (tree decl, dw_die_ref die)
7357260395Spfg{
7358260395Spfg  pubname_entry e;
7359260395Spfg
7360260395Spfg  e.name = NULL;
7361260395Spfg  if ((TREE_PUBLIC (decl)
7362260395Spfg       || die->die_parent == comp_unit_die)
7363260395Spfg      && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
736450397Sobrien    {
7365260395Spfg      e.die = die;
7366260395Spfg      if (TYPE_P (decl))
7367260395Spfg	{
7368260395Spfg	  if (TYPE_NAME (decl))
7369260395Spfg	    {
7370260395Spfg	      if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7371260395Spfg		e.name = xstrdup ((const char *) IDENTIFIER_POINTER
7372260395Spfg				                              (TYPE_NAME (decl)));
7373260395Spfg	      else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7374260395Spfg		       && DECL_NAME (TYPE_NAME (decl)))
7375260395Spfg		e.name = xstrdup ((const char *) IDENTIFIER_POINTER
7376260395Spfg				                  (DECL_NAME (TYPE_NAME (decl))));
7377260395Spfg             else
7378260395Spfg	       e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7379260395Spfg	    }
7380260395Spfg	}
7381260395Spfg      else
7382260395Spfg	e.name = xstrdup (dwarf2_name (decl, 1));
7383260395Spfg
7384260395Spfg      /* If we don't have a name for the type, there's no point in adding
7385260395Spfg	 it to the table.  */
7386260395Spfg      if (e.name && e.name[0] != '\0')
7387260395Spfg	VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
738850397Sobrien    }
738950397Sobrien}
739050397Sobrien
739150397Sobrien/* Output the public names table used to speed up access to externally
7392260395Spfg   visible names; or the public types table used to find type definitions.  */
739350397Sobrien
739450397Sobrienstatic void
7395260395Spfgoutput_pubnames (VEC (pubname_entry, gc) * names)
739650397Sobrien{
739790075Sobrien  unsigned i;
7398260395Spfg  unsigned long pubnames_length = size_of_pubnames (names);
7399260395Spfg  pubname_ref pub;
740050397Sobrien
7401132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7402132718Skan    dw2_asm_output_data (4, 0xffffffff,
7403132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
7404260395Spfg  if (names == pubname_table)
7405260395Spfg    dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7406260395Spfg			 "Length of Public Names Info");
7407260395Spfg  else
7408260395Spfg    dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7409260395Spfg			 "Length of Public Type Names Info");
741090075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
741190075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7412169689Skan			 debug_info_section,
741390075Sobrien			 "Offset of Compilation Unit Info");
741490075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
741590075Sobrien		       "Compilation Unit Length");
741650397Sobrien
7417260395Spfg  for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
741850397Sobrien    {
7419260395Spfg      /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7420260395Spfg      if (names == pubname_table)
7421260395Spfg	gcc_assert (pub->die->die_mark);
742250397Sobrien
7423260395Spfg      if (names != pubtype_table
7424260395Spfg	  || pub->die->die_offset != 0
7425260395Spfg	  || !flag_eliminate_unused_debug_types)
7426260395Spfg	{
7427260395Spfg	  dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7428260395Spfg			       "DIE offset");
742950397Sobrien
7430260395Spfg	  dw2_asm_output_nstring (pub->name, -1, "external name");
7431260395Spfg	}
743250397Sobrien    }
743350397Sobrien
743490075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
743550397Sobrien}
743650397Sobrien
743750397Sobrien/* Add a new entry to .debug_aranges if appropriate.  */
743850397Sobrien
743950397Sobrienstatic void
7440132718Skanadd_arange (tree decl, dw_die_ref die)
744150397Sobrien{
744250397Sobrien  if (! DECL_SECTION_NAME (decl))
744350397Sobrien    return;
744450397Sobrien
744550397Sobrien  if (arange_table_in_use == arange_table_allocated)
744650397Sobrien    {
744750397Sobrien      arange_table_allocated += ARANGE_TABLE_INCREMENT;
7448132718Skan      arange_table = ggc_realloc (arange_table,
7449132718Skan				  (arange_table_allocated
7450132718Skan				   * sizeof (dw_die_ref)));
7451132718Skan      memset (arange_table + arange_table_in_use, 0,
7452132718Skan	      ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
745350397Sobrien    }
745450397Sobrien
745550397Sobrien  arange_table[arange_table_in_use++] = die;
745650397Sobrien}
745750397Sobrien
745850397Sobrien/* Output the information that goes into the .debug_aranges table.
745950397Sobrien   Namely, define the beginning and ending address range of the
746050397Sobrien   text section generated for this compilation unit.  */
746150397Sobrien
746250397Sobrienstatic void
7463132718Skanoutput_aranges (void)
746450397Sobrien{
746590075Sobrien  unsigned i;
746690075Sobrien  unsigned long aranges_length = size_of_aranges ();
746750397Sobrien
7468132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7469132718Skan    dw2_asm_output_data (4, 0xffffffff,
7470132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
747190075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
747290075Sobrien		       "Length of Address Ranges Info");
747390075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
747490075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7475169689Skan			 debug_info_section,
747690075Sobrien			 "Offset of Compilation Unit Info");
747790075Sobrien  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
747890075Sobrien  dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
747950397Sobrien
748090075Sobrien  /* We need to align to twice the pointer size here.  */
748190075Sobrien  if (DWARF_ARANGES_PAD_SIZE)
748290075Sobrien    {
748390075Sobrien      /* Pad using a 2 byte words so that padding is correct for any
7484132718Skan	 pointer size.  */
748590075Sobrien      dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
748690075Sobrien			   2 * DWARF2_ADDR_SIZE);
748790075Sobrien      for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
748890075Sobrien	dw2_asm_output_data (2, 0, NULL);
748990075Sobrien    }
749050397Sobrien
749190075Sobrien  dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
749290075Sobrien  dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
749390075Sobrien			text_section_label, "Length");
7494169689Skan  if (flag_reorder_blocks_and_partition)
7495169689Skan    {
7496169689Skan      dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7497169689Skan			   "Address");
7498169689Skan      dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7499169689Skan			    cold_text_section_label, "Length");
7500169689Skan    }
750150397Sobrien
750290075Sobrien  for (i = 0; i < arange_table_in_use; i++)
750390075Sobrien    {
750490075Sobrien      dw_die_ref die = arange_table[i];
750550397Sobrien
750690075Sobrien      /* We shouldn't see aranges for DIEs outside of the main CU.  */
7507169689Skan      gcc_assert (die->die_mark);
750850397Sobrien
750990075Sobrien      if (die->die_tag == DW_TAG_subprogram)
751090075Sobrien	{
751190075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
751290075Sobrien			       "Address");
751390075Sobrien	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
751490075Sobrien				get_AT_low_pc (die), "Length");
751590075Sobrien	}
751690075Sobrien      else
751790075Sobrien	{
751890075Sobrien	  /* A static variable; extract the symbol from DW_AT_location.
751990075Sobrien	     Note that this code isn't currently hit, as we only emit
752090075Sobrien	     aranges for functions (jason 9/23/99).  */
752190075Sobrien	  dw_attr_ref a = get_AT (die, DW_AT_location);
752290075Sobrien	  dw_loc_descr_ref loc;
752350397Sobrien
7524169689Skan	  gcc_assert (a && AT_class (a) == dw_val_class_loc);
752550397Sobrien
752690075Sobrien	  loc = AT_loc (a);
7527169689Skan	  gcc_assert (loc->dw_loc_opc == DW_OP_addr);
752850397Sobrien
752990075Sobrien	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
753090075Sobrien				   loc->dw_loc_oprnd1.v.val_addr, "Address");
753190075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE,
753290075Sobrien			       get_AT_unsigned (die, DW_AT_byte_size),
753390075Sobrien			       "Length");
753490075Sobrien	}
753590075Sobrien    }
753650397Sobrien
753790075Sobrien  /* Output the terminator words.  */
753890075Sobrien  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
753990075Sobrien  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
754090075Sobrien}
754190075Sobrien
754290075Sobrien/* Add a new entry to .debug_ranges.  Return the offset at which it
754390075Sobrien   was placed.  */
754490075Sobrien
754590075Sobrienstatic unsigned int
7546132718Skanadd_ranges (tree block)
754790075Sobrien{
754890075Sobrien  unsigned int in_use = ranges_table_in_use;
754990075Sobrien
755090075Sobrien  if (in_use == ranges_table_allocated)
755150397Sobrien    {
755290075Sobrien      ranges_table_allocated += RANGES_TABLE_INCREMENT;
7553132718Skan      ranges_table
7554132718Skan	= ggc_realloc (ranges_table, (ranges_table_allocated
7555132718Skan				      * sizeof (struct dw_ranges_struct)));
7556132718Skan      memset (ranges_table + ranges_table_in_use, 0,
7557132718Skan	      RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
755890075Sobrien    }
755950397Sobrien
756090075Sobrien  ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
756190075Sobrien  ranges_table_in_use = in_use + 1;
756290075Sobrien
756390075Sobrien  return in_use * 2 * DWARF2_ADDR_SIZE;
756490075Sobrien}
756590075Sobrien
756690075Sobrienstatic void
7567132718Skanoutput_ranges (void)
756890075Sobrien{
756990075Sobrien  unsigned i;
757090075Sobrien  static const char *const start_fmt = "Offset 0x%x";
757190075Sobrien  const char *fmt = start_fmt;
757290075Sobrien
757390075Sobrien  for (i = 0; i < ranges_table_in_use; i++)
757490075Sobrien    {
757590075Sobrien      int block_num = ranges_table[i].block_num;
757690075Sobrien
757790075Sobrien      if (block_num)
757890075Sobrien	{
757990075Sobrien	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
758090075Sobrien	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
758190075Sobrien
758290075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
758390075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
758490075Sobrien
758590075Sobrien	  /* If all code is in the text section, then the compilation
758690075Sobrien	     unit base address defaults to DW_AT_low_pc, which is the
758790075Sobrien	     base of the text section.  */
7588169689Skan	  if (!have_multiple_function_sections)
758990075Sobrien	    {
759090075Sobrien	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
759190075Sobrien				    text_section_label,
759290075Sobrien				    fmt, i * 2 * DWARF2_ADDR_SIZE);
759390075Sobrien	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
759490075Sobrien				    text_section_label, NULL);
759590075Sobrien	    }
759690075Sobrien
759790075Sobrien	  /* Otherwise, we add a DW_AT_entry_pc attribute to force the
759890075Sobrien	     compilation unit base address to zero, which allows us to
759990075Sobrien	     use absolute addresses, and not worry about whether the
760090075Sobrien	     target supports cross-section arithmetic.  */
760190075Sobrien	  else
760290075Sobrien	    {
760390075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
760490075Sobrien				   fmt, i * 2 * DWARF2_ADDR_SIZE);
760590075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
760690075Sobrien	    }
760790075Sobrien
760890075Sobrien	  fmt = NULL;
760990075Sobrien	}
761050397Sobrien      else
761150397Sobrien	{
761290075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
761390075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
761490075Sobrien	  fmt = start_fmt;
761590075Sobrien	}
761690075Sobrien    }
761790075Sobrien}
761850397Sobrien
761990075Sobrien/* Data structure containing information about input files.  */
762090075Sobrienstruct file_info
762190075Sobrien{
7622169689Skan  const char *path;	/* Complete file name.  */
7623169689Skan  const char *fname;	/* File name part.  */
762490075Sobrien  int length;		/* Length of entire string.  */
7625169689Skan  struct dwarf_file_data * file_idx;	/* Index in input file table.  */
762690075Sobrien  int dir_idx;		/* Index in directory table.  */
762790075Sobrien};
762890075Sobrien
762990075Sobrien/* Data structure containing information about directories with source
763090075Sobrien   files.  */
763190075Sobrienstruct dir_info
763290075Sobrien{
7633169689Skan  const char *path;	/* Path including directory name.  */
763490075Sobrien  int length;		/* Path length.  */
763590075Sobrien  int prefix;		/* Index of directory entry which is a prefix.  */
763690075Sobrien  int count;		/* Number of files in this directory.  */
763790075Sobrien  int dir_idx;		/* Index of directory used as base.  */
763890075Sobrien};
763990075Sobrien
764090075Sobrien/* Callback function for file_info comparison.  We sort by looking at
764190075Sobrien   the directories in the path.  */
764290075Sobrien
764390075Sobrienstatic int
7644132718Skanfile_info_cmp (const void *p1, const void *p2)
764590075Sobrien{
764690075Sobrien  const struct file_info *s1 = p1;
764790075Sobrien  const struct file_info *s2 = p2;
764890075Sobrien  unsigned char *cp1;
764990075Sobrien  unsigned char *cp2;
765090075Sobrien
765190075Sobrien  /* Take care of file names without directories.  We need to make sure that
765290075Sobrien     we return consistent values to qsort since some will get confused if
765390075Sobrien     we return the same value when identical operands are passed in opposite
765490075Sobrien     orders.  So if neither has a directory, return 0 and otherwise return
765590075Sobrien     1 or -1 depending on which one has the directory.  */
765690075Sobrien  if ((s1->path == s1->fname || s2->path == s2->fname))
765790075Sobrien    return (s2->path == s2->fname) - (s1->path == s1->fname);
765890075Sobrien
765990075Sobrien  cp1 = (unsigned char *) s1->path;
766090075Sobrien  cp2 = (unsigned char *) s2->path;
766190075Sobrien
766290075Sobrien  while (1)
766390075Sobrien    {
766490075Sobrien      ++cp1;
766590075Sobrien      ++cp2;
766690075Sobrien      /* Reached the end of the first path?  If so, handle like above.  */
766790075Sobrien      if ((cp1 == (unsigned char *) s1->fname)
766890075Sobrien	  || (cp2 == (unsigned char *) s2->fname))
766990075Sobrien	return ((cp2 == (unsigned char *) s2->fname)
767090075Sobrien		- (cp1 == (unsigned char *) s1->fname));
767190075Sobrien
767290075Sobrien      /* Character of current path component the same?  */
767390075Sobrien      else if (*cp1 != *cp2)
767490075Sobrien	return *cp1 - *cp2;
767590075Sobrien    }
767690075Sobrien}
767790075Sobrien
7678169689Skanstruct file_name_acquire_data
7679169689Skan{
7680169689Skan  struct file_info *files;
7681169689Skan  int used_files;
7682169689Skan  int max_files;
7683169689Skan};
7684169689Skan
7685169689Skan/* Traversal function for the hash table.  */
7686169689Skan
7687169689Skanstatic int
7688169689Skanfile_name_acquire (void ** slot, void *data)
7689169689Skan{
7690169689Skan  struct file_name_acquire_data *fnad = data;
7691169689Skan  struct dwarf_file_data *d = *slot;
7692169689Skan  struct file_info *fi;
7693169689Skan  const char *f;
7694169689Skan
7695169689Skan  gcc_assert (fnad->max_files >= d->emitted_number);
7696169689Skan
7697169689Skan  if (! d->emitted_number)
7698169689Skan    return 1;
7699169689Skan
7700169689Skan  gcc_assert (fnad->max_files != fnad->used_files);
7701169689Skan
7702169689Skan  fi = fnad->files + fnad->used_files++;
7703169689Skan
7704169689Skan  /* Skip all leading "./".  */
7705169689Skan  f = d->filename;
7706169689Skan  while (f[0] == '.' && f[1] == '/')
7707169689Skan    f += 2;
7708169689Skan
7709169689Skan  /* Create a new array entry.  */
7710169689Skan  fi->path = f;
7711169689Skan  fi->length = strlen (f);
7712169689Skan  fi->file_idx = d;
7713169689Skan
7714169689Skan  /* Search for the file name part.  */
7715169689Skan  f = strrchr (f, '/');
7716169689Skan  fi->fname = f == NULL ? fi->path : f + 1;
7717169689Skan  return 1;
7718169689Skan}
7719169689Skan
772090075Sobrien/* Output the directory table and the file name table.  We try to minimize
772190075Sobrien   the total amount of memory needed.  A heuristic is used to avoid large
772290075Sobrien   slowdowns with many input files.  */
772390075Sobrien
772490075Sobrienstatic void
7725132718Skanoutput_file_names (void)
772690075Sobrien{
7727169689Skan  struct file_name_acquire_data fnad;
7728169689Skan  int numfiles;
772990075Sobrien  struct file_info *files;
773090075Sobrien  struct dir_info *dirs;
773190075Sobrien  int *saved;
773290075Sobrien  int *savehere;
773390075Sobrien  int *backmap;
7734169689Skan  int ndirs;
773590075Sobrien  int idx_offset;
7736169689Skan  int i;
773790075Sobrien  int idx;
773890075Sobrien
7739169689Skan  if (!last_emitted_file)
7740132718Skan    {
7741132718Skan      dw2_asm_output_data (1, 0, "End directory table");
7742132718Skan      dw2_asm_output_data (1, 0, "End file name table");
7743132718Skan      return;
7744132718Skan    }
7745132718Skan
7746169689Skan  numfiles = last_emitted_file->emitted_number;
7747169689Skan
774890075Sobrien  /* Allocate the various arrays we need.  */
7749169689Skan  files = alloca (numfiles * sizeof (struct file_info));
7750169689Skan  dirs = alloca (numfiles * sizeof (struct dir_info));
775190075Sobrien
7752169689Skan  fnad.files = files;
7753169689Skan  fnad.used_files = 0;
7754169689Skan  fnad.max_files = numfiles;
7755169689Skan  htab_traverse (file_table, file_name_acquire, &fnad);
7756169689Skan  gcc_assert (fnad.used_files == fnad.max_files);
775790075Sobrien
7758169689Skan  qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
775990075Sobrien
776090075Sobrien  /* Find all the different directories used.  */
7761169689Skan  dirs[0].path = files[0].path;
7762169689Skan  dirs[0].length = files[0].fname - files[0].path;
776390075Sobrien  dirs[0].prefix = -1;
776490075Sobrien  dirs[0].count = 1;
776590075Sobrien  dirs[0].dir_idx = 0;
7766169689Skan  files[0].dir_idx = 0;
776790075Sobrien  ndirs = 1;
776890075Sobrien
7769169689Skan  for (i = 1; i < numfiles; i++)
777090075Sobrien    if (files[i].fname - files[i].path == dirs[ndirs - 1].length
777190075Sobrien	&& memcmp (dirs[ndirs - 1].path, files[i].path,
777290075Sobrien		   dirs[ndirs - 1].length) == 0)
777390075Sobrien      {
777490075Sobrien	/* Same directory as last entry.  */
777590075Sobrien	files[i].dir_idx = ndirs - 1;
777690075Sobrien	++dirs[ndirs - 1].count;
777790075Sobrien      }
777890075Sobrien    else
777990075Sobrien      {
7780169689Skan	int j;
778190075Sobrien
778290075Sobrien	/* This is a new directory.  */
778390075Sobrien	dirs[ndirs].path = files[i].path;
778490075Sobrien	dirs[ndirs].length = files[i].fname - files[i].path;
778590075Sobrien	dirs[ndirs].count = 1;
778690075Sobrien	dirs[ndirs].dir_idx = ndirs;
778790075Sobrien	files[i].dir_idx = ndirs;
778890075Sobrien
778990075Sobrien	/* Search for a prefix.  */
779090075Sobrien	dirs[ndirs].prefix = -1;
779190075Sobrien	for (j = 0; j < ndirs; j++)
779290075Sobrien	  if (dirs[j].length < dirs[ndirs].length
779390075Sobrien	      && dirs[j].length > 1
779490075Sobrien	      && (dirs[ndirs].prefix == -1
779590075Sobrien		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
779690075Sobrien	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
779790075Sobrien	    dirs[ndirs].prefix = j;
779890075Sobrien
779990075Sobrien	++ndirs;
780090075Sobrien      }
780190075Sobrien
780290075Sobrien  /* Now to the actual work.  We have to find a subset of the directories which
780390075Sobrien     allow expressing the file name using references to the directory table
780490075Sobrien     with the least amount of characters.  We do not do an exhaustive search
780590075Sobrien     where we would have to check out every combination of every single
780690075Sobrien     possible prefix.  Instead we use a heuristic which provides nearly optimal
780790075Sobrien     results in most cases and never is much off.  */
7808132718Skan  saved = alloca (ndirs * sizeof (int));
7809132718Skan  savehere = alloca (ndirs * sizeof (int));
781090075Sobrien
781190075Sobrien  memset (saved, '\0', ndirs * sizeof (saved[0]));
781290075Sobrien  for (i = 0; i < ndirs; i++)
781390075Sobrien    {
7814169689Skan      int j;
781590075Sobrien      int total;
781690075Sobrien
781790075Sobrien      /* We can always save some space for the current directory.  But this
781890075Sobrien	 does not mean it will be enough to justify adding the directory.  */
781990075Sobrien      savehere[i] = dirs[i].length;
782090075Sobrien      total = (savehere[i] - saved[i]) * dirs[i].count;
782190075Sobrien
782290075Sobrien      for (j = i + 1; j < ndirs; j++)
782390075Sobrien	{
782490075Sobrien	  savehere[j] = 0;
782590075Sobrien	  if (saved[j] < dirs[i].length)
782690075Sobrien	    {
782790075Sobrien	      /* Determine whether the dirs[i] path is a prefix of the
782890075Sobrien		 dirs[j] path.  */
782990075Sobrien	      int k;
783090075Sobrien
783190075Sobrien	      k = dirs[j].prefix;
7832132718Skan	      while (k != -1 && k != (int) i)
783390075Sobrien		k = dirs[k].prefix;
783490075Sobrien
7835132718Skan	      if (k == (int) i)
783690075Sobrien		{
7837169689Skan		  /* Yes it is.  We can possibly save some memory by
783890075Sobrien		     writing the filenames in dirs[j] relative to
783990075Sobrien		     dirs[i].  */
784090075Sobrien		  savehere[j] = dirs[i].length;
784190075Sobrien		  total += (savehere[j] - saved[j]) * dirs[j].count;
784290075Sobrien		}
784390075Sobrien	    }
784450397Sobrien	}
784550397Sobrien
7846169689Skan      /* Check whether we can save enough to justify adding the dirs[i]
784790075Sobrien	 directory.  */
784890075Sobrien      if (total > dirs[i].length + 1)
784990075Sobrien	{
785090075Sobrien	  /* It's worthwhile adding.  */
7851117395Skan	  for (j = i; j < ndirs; j++)
785290075Sobrien	    if (savehere[j] > 0)
785390075Sobrien	      {
785490075Sobrien		/* Remember how much we saved for this directory so far.  */
785590075Sobrien		saved[j] = savehere[j];
785650397Sobrien
785790075Sobrien		/* Remember the prefix directory.  */
785890075Sobrien		dirs[j].dir_idx = i;
785990075Sobrien	      }
786090075Sobrien	}
786190075Sobrien    }
786250397Sobrien
7863169689Skan  /* Emit the directory name table.  */
786490075Sobrien  idx = 1;
786590075Sobrien  idx_offset = dirs[0].length > 0 ? 1 : 0;
786690075Sobrien  for (i = 1 - idx_offset; i < ndirs; i++)
7867169689Skan    dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7868169689Skan			    "Directory Entry: 0x%x", i + idx_offset);
786990075Sobrien
787090075Sobrien  dw2_asm_output_data (1, 0, "End directory table");
787190075Sobrien
7872169689Skan  /* We have to emit them in the order of emitted_number since that's
7873169689Skan     used in the debug info generation.  To do this efficiently we
7874169689Skan     generate a back-mapping of the indices first.  */
7875169689Skan  backmap = alloca (numfiles * sizeof (int));
7876169689Skan  for (i = 0; i < numfiles; i++)
7877169689Skan    backmap[files[i].file_idx->emitted_number - 1] = i;
787890075Sobrien
787990075Sobrien  /* Now write all the file names.  */
7880169689Skan  for (i = 0; i < numfiles; i++)
788190075Sobrien    {
788290075Sobrien      int file_idx = backmap[i];
788390075Sobrien      int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
788490075Sobrien
788590075Sobrien      dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7886169689Skan			      "File Entry: 0x%x", (unsigned) i + 1);
788790075Sobrien
788890075Sobrien      /* Include directory index.  */
7889169689Skan      dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
789090075Sobrien
789190075Sobrien      /* Modification time.  */
789290075Sobrien      dw2_asm_output_data_uleb128 (0, NULL);
789390075Sobrien
789490075Sobrien      /* File length in bytes.  */
789590075Sobrien      dw2_asm_output_data_uleb128 (0, NULL);
789690075Sobrien    }
789790075Sobrien
789890075Sobrien  dw2_asm_output_data (1, 0, "End file name table");
789950397Sobrien}
790050397Sobrien
790190075Sobrien
790250397Sobrien/* Output the source line number correspondence information.  This
790390075Sobrien   information goes into the .debug_line section.  */
790450397Sobrien
790550397Sobrienstatic void
7906132718Skanoutput_line_info (void)
790750397Sobrien{
790890075Sobrien  char l1[20], l2[20], p1[20], p2[20];
790950397Sobrien  char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
791050397Sobrien  char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
791190075Sobrien  unsigned opc;
791290075Sobrien  unsigned n_op_args;
791390075Sobrien  unsigned long lt_index;
791490075Sobrien  unsigned long current_line;
791590075Sobrien  long line_offset;
791690075Sobrien  long line_delta;
791790075Sobrien  unsigned long current_file;
791890075Sobrien  unsigned long function;
791950397Sobrien
792090075Sobrien  ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
792190075Sobrien  ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
792290075Sobrien  ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
792390075Sobrien  ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
792450397Sobrien
7925132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7926132718Skan    dw2_asm_output_data (4, 0xffffffff,
7927132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
792890075Sobrien  dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
792990075Sobrien			"Length of Source Line Info");
793090075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l1);
793150397Sobrien
793290075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
793390075Sobrien  dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
793490075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, p1);
793550397Sobrien
793696263Sobrien  /* Define the architecture-dependent minimum instruction length (in
793796263Sobrien   bytes).  In this implementation of DWARF, this field is used for
793896263Sobrien   information purposes only.  Since GCC generates assembly language,
793996263Sobrien   we have no a priori knowledge of how many instruction bytes are
794096263Sobrien   generated for each source line, and therefore can use only the
794196263Sobrien   DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
794296263Sobrien   commands.  Accordingly, we fix this as `1', which is "correct
794396263Sobrien   enough" for all architectures, and don't let the target override.  */
794496263Sobrien  dw2_asm_output_data (1, 1,
794590075Sobrien		       "Minimum Instruction Length");
794696263Sobrien
794790075Sobrien  dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
794890075Sobrien		       "Default is_stmt_start flag");
794990075Sobrien  dw2_asm_output_data (1, DWARF_LINE_BASE,
795090075Sobrien		       "Line Base Value (Special Opcodes)");
795190075Sobrien  dw2_asm_output_data (1, DWARF_LINE_RANGE,
795290075Sobrien		       "Line Range Value (Special Opcodes)");
795390075Sobrien  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
795490075Sobrien		       "Special Opcode Base");
795550397Sobrien
795690075Sobrien  for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
795750397Sobrien    {
795850397Sobrien      switch (opc)
795950397Sobrien	{
796050397Sobrien	case DW_LNS_advance_pc:
796150397Sobrien	case DW_LNS_advance_line:
796250397Sobrien	case DW_LNS_set_file:
796350397Sobrien	case DW_LNS_set_column:
796450397Sobrien	case DW_LNS_fixed_advance_pc:
796550397Sobrien	  n_op_args = 1;
796650397Sobrien	  break;
796750397Sobrien	default:
796850397Sobrien	  n_op_args = 0;
796950397Sobrien	  break;
797050397Sobrien	}
797150397Sobrien
797290075Sobrien      dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
797390075Sobrien			   opc, n_op_args);
797450397Sobrien    }
797550397Sobrien
797690075Sobrien  /* Write out the information about the files we use.  */
797790075Sobrien  output_file_names ();
797890075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, p2);
797950397Sobrien
798052284Sobrien  /* We used to set the address register to the first location in the text
798152284Sobrien     section here, but that didn't accomplish anything since we already
798252284Sobrien     have a line note for the opening brace of the first function.  */
798350397Sobrien
798450397Sobrien  /* Generate the line number to PC correspondence table, encoded as
798550397Sobrien     a series of state machine operations.  */
798650397Sobrien  current_file = 1;
798750397Sobrien  current_line = 1;
7988169689Skan
7989169689Skan  if (cfun && in_cold_section_p)
7990169689Skan    strcpy (prev_line_label, cfun->cold_section_label);
7991169689Skan  else
7992169689Skan    strcpy (prev_line_label, text_section_label);
799350397Sobrien  for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
799450397Sobrien    {
799590075Sobrien      dw_line_info_ref line_info = &line_info_table[lt_index];
799650397Sobrien
799790075Sobrien#if 0
799890075Sobrien      /* Disable this optimization for now; GDB wants to see two line notes
799990075Sobrien	 at the beginning of a function so it can find the end of the
800090075Sobrien	 prologue.  */
800190075Sobrien
800252284Sobrien      /* Don't emit anything for redundant notes.  Just updating the
8003132718Skan	 address doesn't accomplish anything, because we already assume
8004132718Skan	 that anything after the last address is this line.  */
800552284Sobrien      if (line_info->dw_line_num == current_line
800652284Sobrien	  && line_info->dw_file_num == current_file)
800752284Sobrien	continue;
800890075Sobrien#endif
800952284Sobrien
801090075Sobrien      /* Emit debug info for the address of the current line.
801190075Sobrien
801290075Sobrien	 Unfortunately, we have little choice here currently, and must always
801390075Sobrien	 use the most general form.  GCC does not know the address delta
801490075Sobrien	 itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
801590075Sobrien	 attributes which will give an upper bound on the address range.  We
801690075Sobrien	 could perhaps use length attributes to determine when it is safe to
801790075Sobrien	 use DW_LNS_fixed_advance_pc.  */
801890075Sobrien
801950397Sobrien      ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
802050397Sobrien      if (0)
802150397Sobrien	{
802250397Sobrien	  /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
802390075Sobrien	  dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
802490075Sobrien			       "DW_LNS_fixed_advance_pc");
802590075Sobrien	  dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
802650397Sobrien	}
802750397Sobrien      else
802850397Sobrien	{
802990075Sobrien	  /* This can handle any delta.  This takes
8030132718Skan	     4+DWARF2_ADDR_SIZE bytes.  */
803190075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
803290075Sobrien	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
803390075Sobrien	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
803490075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
803550397Sobrien	}
803690075Sobrien
803750397Sobrien      strcpy (prev_line_label, line_label);
803850397Sobrien
803950397Sobrien      /* Emit debug info for the source file of the current line, if
804050397Sobrien	 different from the previous line.  */
804150397Sobrien      if (line_info->dw_file_num != current_file)
804250397Sobrien	{
804350397Sobrien	  current_file = line_info->dw_file_num;
804490075Sobrien	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8045169689Skan	  dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
804650397Sobrien	}
804750397Sobrien
804850397Sobrien      /* Emit debug info for the current line number, choosing the encoding
804950397Sobrien	 that uses the least amount of space.  */
805052284Sobrien      if (line_info->dw_line_num != current_line)
805150397Sobrien	{
805252284Sobrien	  line_offset = line_info->dw_line_num - current_line;
805352284Sobrien	  line_delta = line_offset - DWARF_LINE_BASE;
805452284Sobrien	  current_line = line_info->dw_line_num;
805552284Sobrien	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
805690075Sobrien	    /* This can handle deltas from -10 to 234, using the current
805790075Sobrien	       definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
805890075Sobrien	       takes 1 byte.  */
805990075Sobrien	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
806090075Sobrien				 "line %lu", current_line);
806152284Sobrien	  else
806252284Sobrien	    {
806352284Sobrien	      /* This can handle any delta.  This takes at least 4 bytes,
806452284Sobrien		 depending on the value being encoded.  */
806590075Sobrien	      dw2_asm_output_data (1, DW_LNS_advance_line,
806690075Sobrien				   "advance to line %lu", current_line);
806790075Sobrien	      dw2_asm_output_data_sleb128 (line_offset, NULL);
806890075Sobrien	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
806952284Sobrien	    }
807050397Sobrien	}
807150397Sobrien      else
807290075Sobrien	/* We still need to start a new row, so output a copy insn.  */
807390075Sobrien	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
807450397Sobrien    }
807550397Sobrien
807650397Sobrien  /* Emit debug info for the address of the end of the function.  */
807750397Sobrien  if (0)
807850397Sobrien    {
807990075Sobrien      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
808090075Sobrien			   "DW_LNS_fixed_advance_pc");
808190075Sobrien      dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
808250397Sobrien    }
808350397Sobrien  else
808450397Sobrien    {
808590075Sobrien      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
808690075Sobrien      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
808790075Sobrien      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
808890075Sobrien      dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
808950397Sobrien    }
809050397Sobrien
809190075Sobrien  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
809290075Sobrien  dw2_asm_output_data_uleb128 (1, NULL);
809390075Sobrien  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
809450397Sobrien
809550397Sobrien  function = 0;
809650397Sobrien  current_file = 1;
809750397Sobrien  current_line = 1;
809890075Sobrien  for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
809950397Sobrien    {
810090075Sobrien      dw_separate_line_info_ref line_info
810150397Sobrien	= &separate_line_info_table[lt_index];
810250397Sobrien
810390075Sobrien#if 0
810452284Sobrien      /* Don't emit anything for redundant notes.  */
810552284Sobrien      if (line_info->dw_line_num == current_line
810652284Sobrien	  && line_info->dw_file_num == current_file
810752284Sobrien	  && line_info->function == function)
810852284Sobrien	goto cont;
810990075Sobrien#endif
811052284Sobrien
811150397Sobrien      /* Emit debug info for the address of the current line.  If this is
811250397Sobrien	 a new function, or the first line of a function, then we need
811350397Sobrien	 to handle it differently.  */
811450397Sobrien      ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
811550397Sobrien				   lt_index);
811650397Sobrien      if (function != line_info->function)
811750397Sobrien	{
811850397Sobrien	  function = line_info->function;
811950397Sobrien
8120132718Skan	  /* Set the address register to the first line in the function.  */
812190075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
812290075Sobrien	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
812390075Sobrien	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
812490075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
812550397Sobrien	}
812650397Sobrien      else
812750397Sobrien	{
812850397Sobrien	  /* ??? See the DW_LNS_advance_pc comment above.  */
812950397Sobrien	  if (0)
813050397Sobrien	    {
813190075Sobrien	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
813290075Sobrien				   "DW_LNS_fixed_advance_pc");
813390075Sobrien	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
813450397Sobrien	    }
813550397Sobrien	  else
813650397Sobrien	    {
813790075Sobrien	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
813890075Sobrien	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
813990075Sobrien	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
814090075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
814150397Sobrien	    }
814250397Sobrien	}
814390075Sobrien
814450397Sobrien      strcpy (prev_line_label, line_label);
814550397Sobrien
814650397Sobrien      /* Emit debug info for the source file of the current line, if
814750397Sobrien	 different from the previous line.  */
814850397Sobrien      if (line_info->dw_file_num != current_file)
814950397Sobrien	{
815050397Sobrien	  current_file = line_info->dw_file_num;
815190075Sobrien	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8152169689Skan	  dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
815350397Sobrien	}
815450397Sobrien
815550397Sobrien      /* Emit debug info for the current line number, choosing the encoding
815650397Sobrien	 that uses the least amount of space.  */
815750397Sobrien      if (line_info->dw_line_num != current_line)
815850397Sobrien	{
815950397Sobrien	  line_offset = line_info->dw_line_num - current_line;
816050397Sobrien	  line_delta = line_offset - DWARF_LINE_BASE;
816150397Sobrien	  current_line = line_info->dw_line_num;
816250397Sobrien	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
816390075Sobrien	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
816490075Sobrien				 "line %lu", current_line);
816550397Sobrien	  else
816650397Sobrien	    {
816790075Sobrien	      dw2_asm_output_data (1, DW_LNS_advance_line,
816890075Sobrien				   "advance to line %lu", current_line);
816990075Sobrien	      dw2_asm_output_data_sleb128 (line_offset, NULL);
817090075Sobrien	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
817150397Sobrien	    }
817250397Sobrien	}
817352284Sobrien      else
817490075Sobrien	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
817550397Sobrien
817690075Sobrien#if 0
817752284Sobrien    cont:
817890075Sobrien#endif
817950397Sobrien
818090075Sobrien      lt_index++;
818190075Sobrien
818250397Sobrien      /* If we're done with a function, end its sequence.  */
818350397Sobrien      if (lt_index == separate_line_info_table_in_use
818450397Sobrien	  || separate_line_info_table[lt_index].function != function)
818550397Sobrien	{
818650397Sobrien	  current_file = 1;
818750397Sobrien	  current_line = 1;
818850397Sobrien
818950397Sobrien	  /* Emit debug info for the address of the end of the function.  */
819050397Sobrien	  ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
819150397Sobrien	  if (0)
819250397Sobrien	    {
819390075Sobrien	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
819490075Sobrien				   "DW_LNS_fixed_advance_pc");
819590075Sobrien	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
819650397Sobrien	    }
819750397Sobrien	  else
819850397Sobrien	    {
819990075Sobrien	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
820090075Sobrien	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
820190075Sobrien	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
820290075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
820350397Sobrien	    }
820450397Sobrien
820550397Sobrien	  /* Output the marker for the end of this sequence.  */
820690075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
820790075Sobrien	  dw2_asm_output_data_uleb128 (1, NULL);
820890075Sobrien	  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
820950397Sobrien	}
821050397Sobrien    }
821190075Sobrien
821290075Sobrien  /* Output the marker for the end of the line number info.  */
821390075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l2);
821450397Sobrien}
821550397Sobrien
821650397Sobrien/* Given a pointer to a tree node for some base type, return a pointer to
821750397Sobrien   a DIE that describes the given type.
821850397Sobrien
821950397Sobrien   This routine must only be called for GCC type nodes that correspond to
822050397Sobrien   Dwarf base (fundamental) types.  */
822150397Sobrien
822250397Sobrienstatic dw_die_ref
8223132718Skanbase_type_die (tree type)
822450397Sobrien{
822590075Sobrien  dw_die_ref base_type_result;
822690075Sobrien  enum dwarf_type encoding;
822750397Sobrien
822890075Sobrien  if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
822950397Sobrien    return 0;
823050397Sobrien
823150397Sobrien  switch (TREE_CODE (type))
823250397Sobrien    {
823350397Sobrien    case INTEGER_TYPE:
8234169689Skan      if (TYPE_STRING_FLAG (type))
823550397Sobrien	{
8236169689Skan	  if (TYPE_UNSIGNED (type))
8237169689Skan	    encoding = DW_ATE_unsigned_char;
823850397Sobrien	  else
8239169689Skan	    encoding = DW_ATE_signed_char;
824050397Sobrien	}
8241169689Skan      else if (TYPE_UNSIGNED (type))
8242169689Skan	encoding = DW_ATE_unsigned;
824350397Sobrien      else
8244169689Skan	encoding = DW_ATE_signed;
824550397Sobrien      break;
824650397Sobrien
824750397Sobrien    case REAL_TYPE:
8248169689Skan      if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8249169689Skan	encoding = DW_ATE_decimal_float;
8250169689Skan      else
8251169689Skan	encoding = DW_ATE_float;
825250397Sobrien      break;
825350397Sobrien
825490075Sobrien      /* Dwarf2 doesn't know anything about complex ints, so use
825590075Sobrien	 a user defined type for it.  */
825650397Sobrien    case COMPLEX_TYPE:
825790075Sobrien      if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
825890075Sobrien	encoding = DW_ATE_complex_float;
825990075Sobrien      else
826090075Sobrien	encoding = DW_ATE_lo_user;
826150397Sobrien      break;
826250397Sobrien
826350397Sobrien    case BOOLEAN_TYPE:
826450397Sobrien      /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
826550397Sobrien      encoding = DW_ATE_boolean;
826650397Sobrien      break;
826750397Sobrien
826850397Sobrien    default:
826990075Sobrien      /* No other TREE_CODEs are Dwarf fundamental types.  */
8270169689Skan      gcc_unreachable ();
827150397Sobrien    }
827250397Sobrien
827390075Sobrien  base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
827490075Sobrien
8275169689Skan  /* This probably indicates a bug.  */
8276169689Skan  if (! TYPE_NAME (type))
8277169689Skan    add_name_attribute (base_type_result, "__unknown__");
8278169689Skan
827950397Sobrien  add_AT_unsigned (base_type_result, DW_AT_byte_size,
828050397Sobrien		   int_size_in_bytes (type));
828150397Sobrien  add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
828250397Sobrien
828350397Sobrien  return base_type_result;
828450397Sobrien}
828550397Sobrien
828650397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
828750397Sobrien   the Dwarf "root" type for the given input type.  The Dwarf "root" type of
828850397Sobrien   a given type is generally the same as the given type, except that if the
828950397Sobrien   given type is a pointer or reference type, then the root type of the given
829050397Sobrien   type is the root type of the "basis" type for the pointer or reference
829150397Sobrien   type.  (This definition of the "root" type is recursive.) Also, the root
829250397Sobrien   type of a `const' qualified type or a `volatile' qualified type is the
829350397Sobrien   root type of the given type without the qualifiers.  */
829450397Sobrien
829550397Sobrienstatic tree
8296132718Skanroot_type (tree type)
829750397Sobrien{
829850397Sobrien  if (TREE_CODE (type) == ERROR_MARK)
829950397Sobrien    return error_mark_node;
830050397Sobrien
830150397Sobrien  switch (TREE_CODE (type))
830250397Sobrien    {
830350397Sobrien    case ERROR_MARK:
830450397Sobrien      return error_mark_node;
830550397Sobrien
8306261188Spfg    /* APPLE LOCAL radar 5732232 - blocks */
8307261188Spfg    case BLOCK_POINTER_TYPE:
830850397Sobrien    case POINTER_TYPE:
830950397Sobrien    case REFERENCE_TYPE:
831050397Sobrien      return type_main_variant (root_type (TREE_TYPE (type)));
831150397Sobrien
831250397Sobrien    default:
831350397Sobrien      return type_main_variant (type);
831450397Sobrien    }
831550397Sobrien}
831650397Sobrien
8317117395Skan/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
831850397Sobrien   given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
831950397Sobrien
832050397Sobrienstatic inline int
8321132718Skanis_base_type (tree type)
832250397Sobrien{
832350397Sobrien  switch (TREE_CODE (type))
832450397Sobrien    {
832550397Sobrien    case ERROR_MARK:
832650397Sobrien    case VOID_TYPE:
832750397Sobrien    case INTEGER_TYPE:
832850397Sobrien    case REAL_TYPE:
832950397Sobrien    case COMPLEX_TYPE:
833050397Sobrien    case BOOLEAN_TYPE:
833150397Sobrien      return 1;
833250397Sobrien
833350397Sobrien    case ARRAY_TYPE:
833450397Sobrien    case RECORD_TYPE:
833550397Sobrien    case UNION_TYPE:
833650397Sobrien    case QUAL_UNION_TYPE:
833750397Sobrien    case ENUMERAL_TYPE:
833850397Sobrien    case FUNCTION_TYPE:
833950397Sobrien    case METHOD_TYPE:
8340261188Spfg	/* APPLE LOCAL radar 5732232 - blocks */
8341261188Spfg    case BLOCK_POINTER_TYPE:
834250397Sobrien    case POINTER_TYPE:
834350397Sobrien    case REFERENCE_TYPE:
834450397Sobrien    case OFFSET_TYPE:
834550397Sobrien    case LANG_TYPE:
834690075Sobrien    case VECTOR_TYPE:
834750397Sobrien      return 0;
834850397Sobrien
834950397Sobrien    default:
8350169689Skan      gcc_unreachable ();
835150397Sobrien    }
835250397Sobrien
835350397Sobrien  return 0;
835450397Sobrien}
835550397Sobrien
8356132718Skan/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8357132718Skan   node, return the size in bits for the type if it is a constant, or else
8358132718Skan   return the alignment for the type if the type's size is not constant, or
8359132718Skan   else return BITS_PER_WORD if the type actually turns out to be an
8360132718Skan   ERROR_MARK node.  */
8361132718Skan
8362132718Skanstatic inline unsigned HOST_WIDE_INT
8363132718Skansimple_type_size_in_bits (tree type)
8364132718Skan{
8365132718Skan  if (TREE_CODE (type) == ERROR_MARK)
8366132718Skan    return BITS_PER_WORD;
8367132718Skan  else if (TYPE_SIZE (type) == NULL_TREE)
8368132718Skan    return 0;
8369132718Skan  else if (host_integerp (TYPE_SIZE (type), 1))
8370132718Skan    return tree_low_cst (TYPE_SIZE (type), 1);
8371132718Skan  else
8372132718Skan    return TYPE_ALIGN (type);
8373132718Skan}
8374132718Skan
8375132718Skan/* Return true if the debug information for the given type should be
8376132718Skan   emitted as a subrange type.  */
8377132718Skan
8378132718Skanstatic inline bool
8379132718Skanis_subrange_type (tree type)
8380132718Skan{
8381132718Skan  tree subtype = TREE_TYPE (type);
8382132718Skan
8383169689Skan  /* Subrange types are identified by the fact that they are integer
8384169689Skan     types, and that they have a subtype which is either an integer type
8385169689Skan     or an enumeral type.  */
8386169689Skan
8387169689Skan  if (TREE_CODE (type) != INTEGER_TYPE
8388169689Skan      || subtype == NULL_TREE)
8389169689Skan    return false;
8390169689Skan
8391169689Skan  if (TREE_CODE (subtype) != INTEGER_TYPE
8392169689Skan      && TREE_CODE (subtype) != ENUMERAL_TYPE)
8393169689Skan    return false;
8394169689Skan
8395169689Skan  if (TREE_CODE (type) == TREE_CODE (subtype)
8396169689Skan      && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8397169689Skan      && TYPE_MIN_VALUE (type) != NULL
8398169689Skan      && TYPE_MIN_VALUE (subtype) != NULL
8399169689Skan      && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8400169689Skan      && TYPE_MAX_VALUE (type) != NULL
8401169689Skan      && TYPE_MAX_VALUE (subtype) != NULL
8402169689Skan      && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8403132718Skan    {
8404169689Skan      /* The type and its subtype have the same representation.  If in
8405169689Skan         addition the two types also have the same name, then the given
8406169689Skan         type is not a subrange type, but rather a plain base type.  */
8407169689Skan      /* FIXME: brobecker/2004-03-22:
8408169689Skan         Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8409169689Skan         therefore be sufficient to check the TYPE_SIZE node pointers
8410169689Skan         rather than checking the actual size.  Unfortunately, we have
8411169689Skan         found some cases, such as in the Ada "integer" type, where
8412169689Skan         this is not the case.  Until this problem is solved, we need to
8413169689Skan         keep checking the actual size.  */
8414169689Skan      tree type_name = TYPE_NAME (type);
8415169689Skan      tree subtype_name = TYPE_NAME (subtype);
8416169689Skan
8417169689Skan      if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8418169689Skan        type_name = DECL_NAME (type_name);
8419169689Skan
8420169689Skan      if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8421169689Skan        subtype_name = DECL_NAME (subtype_name);
8422169689Skan
8423169689Skan      if (type_name == subtype_name)
8424169689Skan        return false;
8425132718Skan    }
8426169689Skan
8427169689Skan  return true;
8428132718Skan}
8429132718Skan
8430132718Skan/*  Given a pointer to a tree node for a subrange type, return a pointer
8431132718Skan    to a DIE that describes the given type.  */
8432132718Skan
8433132718Skanstatic dw_die_ref
8434132718Skansubrange_type_die (tree type, dw_die_ref context_die)
8435132718Skan{
8436132718Skan  dw_die_ref subrange_die;
8437132718Skan  const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8438132718Skan
8439132718Skan  if (context_die == NULL)
8440132718Skan    context_die = comp_unit_die;
8441132718Skan
8442132718Skan  subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8443132718Skan
8444132718Skan  if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8445132718Skan    {
8446132718Skan      /* The size of the subrange type and its base type do not match,
8447132718Skan         so we need to generate a size attribute for the subrange type.  */
8448132718Skan      add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8449132718Skan    }
8450132718Skan
8451132718Skan  if (TYPE_MIN_VALUE (type) != NULL)
8452132718Skan    add_bound_info (subrange_die, DW_AT_lower_bound,
8453132718Skan                    TYPE_MIN_VALUE (type));
8454132718Skan  if (TYPE_MAX_VALUE (type) != NULL)
8455132718Skan    add_bound_info (subrange_die, DW_AT_upper_bound,
8456132718Skan                    TYPE_MAX_VALUE (type));
8457132718Skan
8458132718Skan  return subrange_die;
8459132718Skan}
8460132718Skan
846150397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
846250397Sobrien   entry that chains various modifiers in front of the given type.  */
846350397Sobrien
846450397Sobrienstatic dw_die_ref
8465132718Skanmodified_type_die (tree type, int is_const_type, int is_volatile_type,
8466132718Skan		   dw_die_ref context_die)
846750397Sobrien{
846890075Sobrien  enum tree_code code = TREE_CODE (type);
8469169689Skan  dw_die_ref mod_type_die;
847090075Sobrien  dw_die_ref sub_die = NULL;
847190075Sobrien  tree item_type = NULL;
8472169689Skan  tree qualified_type;
8473169689Skan  tree name;
847450397Sobrien
8475169689Skan  if (code == ERROR_MARK)
8476169689Skan    return NULL;
8477169689Skan
8478169689Skan  /* See if we already have the appropriately qualified variant of
8479169689Skan     this type.  */
8480169689Skan  qualified_type
8481169689Skan    = get_qualified_type (type,
8482169689Skan			  ((is_const_type ? TYPE_QUAL_CONST : 0)
8483169689Skan			   | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8484169689Skan
8485169689Skan  /* If we do, then we can just use its DIE, if it exists.  */
8486169689Skan  if (qualified_type)
848750397Sobrien    {
8488169689Skan      mod_type_die = lookup_type_die (qualified_type);
848950397Sobrien      if (mod_type_die)
8490169689Skan	return mod_type_die;
8491169689Skan    }
8492169689Skan
8493169689Skan  name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8494169689Skan
8495169689Skan  /* Handle C typedef types.  */
8496169689Skan  if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8497169689Skan    {
8498169689Skan      tree dtype = TREE_TYPE (name);
8499169689Skan
8500169689Skan      if (qualified_type == dtype)
850150397Sobrien	{
8502169689Skan	  /* For a named type, use the typedef.  */
8503169689Skan	  gen_type_die (qualified_type, context_die);
8504169689Skan	  return lookup_type_die (qualified_type);
850550397Sobrien	}
8506169689Skan      else if (is_const_type < TYPE_READONLY (dtype)
8507169689Skan	       || is_volatile_type < TYPE_VOLATILE (dtype)
8508169689Skan	       || (is_const_type <= TYPE_READONLY (dtype)
8509169689Skan		   && is_volatile_type <= TYPE_VOLATILE (dtype)
8510169689Skan		   && DECL_ORIGINAL_TYPE (name) != type))
8511169689Skan	/* cv-unqualified version of named type.  Just use the unnamed
8512169689Skan	   type to which it refers.  */
8513169689Skan	return modified_type_die (DECL_ORIGINAL_TYPE (name),
8514169689Skan				  is_const_type, is_volatile_type,
8515169689Skan				  context_die);
8516169689Skan      /* Else cv-qualified version of named type; fall through.  */
8517169689Skan    }
8518169689Skan
8519169689Skan  if (is_const_type)
8520169689Skan    {
8521169689Skan      mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8522169689Skan      sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8523169689Skan    }
8524169689Skan  else if (is_volatile_type)
8525169689Skan    {
8526169689Skan      mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8527169689Skan      sub_die = modified_type_die (type, 0, 0, context_die);
8528169689Skan    }
8529261188Spfg  /* APPLE LOCAL radar 5732232 - blocks */
8530261188Spfg  else if (code == POINTER_TYPE || code == BLOCK_POINTER_TYPE)
8531169689Skan    {
8532169689Skan      mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8533169689Skan      add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8534169689Skan		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
8535169689Skan      item_type = TREE_TYPE (type);
8536169689Skan    }
8537169689Skan  else if (code == REFERENCE_TYPE)
8538169689Skan    {
8539169689Skan      mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8540169689Skan      add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8541169689Skan		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
8542169689Skan      item_type = TREE_TYPE (type);
8543169689Skan    }
8544169689Skan  else if (is_subrange_type (type))
8545169689Skan    {
8546169689Skan      mod_type_die = subrange_type_die (type, context_die);
8547169689Skan      item_type = TREE_TYPE (type);
8548169689Skan    }
8549169689Skan  else if (is_base_type (type))
8550169689Skan    mod_type_die = base_type_die (type);
8551169689Skan  else
8552169689Skan    {
8553169689Skan      gen_type_die (type, context_die);
8554169689Skan
8555169689Skan      /* We have to get the type_main_variant here (and pass that to the
8556169689Skan	 `lookup_type_die' routine) because the ..._TYPE node we have
8557169689Skan	 might simply be a *copy* of some original type node (where the
8558169689Skan	 copy was created to help us keep track of typedef names) and
8559169689Skan	 that copy might have a different TYPE_UID from the original
8560169689Skan	 ..._TYPE node.  */
8561169689Skan      if (TREE_CODE (type) != VECTOR_TYPE)
8562169689Skan	return lookup_type_die (type_main_variant (type));
856350397Sobrien      else
8564169689Skan	/* Vectors have the debugging information in the type,
8565169689Skan	   not the main variant.  */
8566169689Skan	return lookup_type_die (type);
856750397Sobrien    }
8568169689Skan
8569169689Skan  /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8570169689Skan     don't output a DW_TAG_typedef, since there isn't one in the
8571169689Skan     user's program; just attach a DW_AT_name to the type.  */
8572169689Skan  if (name
8573169689Skan      && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8574169689Skan    {
8575169689Skan      if (TREE_CODE (name) == TYPE_DECL)
8576169689Skan	/* Could just call add_name_and_src_coords_attributes here,
8577169689Skan	   but since this is a builtin type it doesn't have any
8578169689Skan	   useful source coordinates anyway.  */
8579169689Skan	name = DECL_NAME (name);
8580169689Skan      add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8581169689Skan    }
8582169689Skan
8583169689Skan  if (qualified_type)
8584169689Skan    equate_type_number_to_die (qualified_type, mod_type_die);
858550397Sobrien
858650397Sobrien  if (item_type)
858750397Sobrien    /* We must do this after the equate_type_number_to_die call, in case
858850397Sobrien       this is a recursive type.  This ensures that the modified_type_die
858950397Sobrien       recursion will terminate even if the type is recursive.  Recursive
859050397Sobrien       types are possible in Ada.  */
859150397Sobrien    sub_die = modified_type_die (item_type,
859250397Sobrien				 TYPE_READONLY (item_type),
859350397Sobrien				 TYPE_VOLATILE (item_type),
859450397Sobrien				 context_die);
859550397Sobrien
859650397Sobrien  if (sub_die != NULL)
859750397Sobrien    add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
859850397Sobrien
859950397Sobrien  return mod_type_die;
860050397Sobrien}
860150397Sobrien
860250397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
860390075Sobrien   an enumerated type.  */
860450397Sobrien
860550397Sobrienstatic inline int
8606132718Skantype_is_enum (tree type)
860750397Sobrien{
860850397Sobrien  return TREE_CODE (type) == ENUMERAL_TYPE;
860950397Sobrien}
861050397Sobrien
8611132718Skan/* Return the DBX register number described by a given RTL node.  */
861250397Sobrien
861390075Sobrienstatic unsigned int
8614132718Skandbx_reg_number (rtx rtl)
861590075Sobrien{
861690075Sobrien  unsigned regno = REGNO (rtl);
861790075Sobrien
8618169689Skan  gcc_assert (regno < FIRST_PSEUDO_REGISTER);
861990075Sobrien
8620169689Skan#ifdef LEAF_REG_REMAP
8621169689Skan  if (current_function_uses_only_leaf_regs)
8622169689Skan    {
8623169689Skan      int leaf_reg = LEAF_REG_REMAP (regno);
8624169689Skan      if (leaf_reg != -1)
8625169689Skan	regno = (unsigned) leaf_reg;
8626169689Skan    }
8627169689Skan#endif
8628169689Skan
862990075Sobrien  return DBX_REGISTER_NUMBER (regno);
863090075Sobrien}
863190075Sobrien
8632169689Skan/* Optionally add a DW_OP_piece term to a location description expression.
8633169689Skan   DW_OP_piece is only added if the location description expression already
8634169689Skan   doesn't end with DW_OP_piece.  */
8635169689Skan
8636169689Skanstatic void
8637169689Skanadd_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8638169689Skan{
8639169689Skan  dw_loc_descr_ref loc;
8640169689Skan
8641169689Skan  if (*list_head != NULL)
8642169689Skan    {
8643169689Skan      /* Find the end of the chain.  */
8644169689Skan      for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8645169689Skan	;
8646169689Skan
8647169689Skan      if (loc->dw_loc_opc != DW_OP_piece)
8648169689Skan	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8649169689Skan    }
8650169689Skan}
8651169689Skan
865290075Sobrien/* Return a location descriptor that designates a machine register or
8653132718Skan   zero if there is none.  */
865490075Sobrien
865550397Sobrienstatic dw_loc_descr_ref
8656132718Skanreg_loc_descriptor (rtx rtl)
865750397Sobrien{
8658132718Skan  rtx regs;
865950397Sobrien
866090075Sobrien  if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
866190075Sobrien    return 0;
866290075Sobrien
8663169689Skan  regs = targetm.dwarf_register_span (rtl);
8664132718Skan
8665169689Skan  if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8666132718Skan    return multiple_reg_loc_descriptor (rtl, regs);
866750397Sobrien  else
8668169689Skan    return one_reg_loc_descriptor (dbx_reg_number (rtl));
8669132718Skan}
867050397Sobrien
8671132718Skan/* Return a location descriptor that designates a machine register for
8672132718Skan   a given hard register number.  */
8673132718Skan
8674132718Skanstatic dw_loc_descr_ref
8675132718Skanone_reg_loc_descriptor (unsigned int regno)
8676132718Skan{
8677132718Skan  if (regno <= 31)
8678132718Skan    return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8679132718Skan  else
8680132718Skan    return new_loc_descr (DW_OP_regx, regno, 0);
8681132718Skan}
8682132718Skan
8683132718Skan/* Given an RTL of a register, return a location descriptor that
8684132718Skan   designates a value that spans more than one register.  */
8685132718Skan
8686132718Skanstatic dw_loc_descr_ref
8687132718Skanmultiple_reg_loc_descriptor (rtx rtl, rtx regs)
8688132718Skan{
8689132718Skan  int nregs, size, i;
8690132718Skan  unsigned reg;
8691132718Skan  dw_loc_descr_ref loc_result = NULL;
8692132718Skan
8693169689Skan  reg = REGNO (rtl);
8694169689Skan#ifdef LEAF_REG_REMAP
8695169689Skan  if (current_function_uses_only_leaf_regs)
8696169689Skan    {
8697169689Skan      int leaf_reg = LEAF_REG_REMAP (reg);
8698169689Skan      if (leaf_reg != -1)
8699169689Skan	reg = (unsigned) leaf_reg;
8700169689Skan    }
8701169689Skan#endif
8702169689Skan  gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8703169689Skan  nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8704132718Skan
8705132718Skan  /* Simple, contiguous registers.  */
8706132718Skan  if (regs == NULL_RTX)
8707132718Skan    {
8708132718Skan      size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8709132718Skan
8710132718Skan      loc_result = NULL;
8711132718Skan      while (nregs--)
8712132718Skan	{
8713132718Skan	  dw_loc_descr_ref t;
8714132718Skan
8715169689Skan	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8716132718Skan	  add_loc_descr (&loc_result, t);
8717169689Skan	  add_loc_descr_op_piece (&loc_result, size);
8718132718Skan	  ++reg;
8719132718Skan	}
8720132718Skan      return loc_result;
8721132718Skan    }
8722132718Skan
8723132718Skan  /* Now onto stupid register sets in non contiguous locations.  */
8724132718Skan
8725169689Skan  gcc_assert (GET_CODE (regs) == PARALLEL);
8726132718Skan
8727132718Skan  size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8728132718Skan  loc_result = NULL;
8729132718Skan
8730132718Skan  for (i = 0; i < XVECLEN (regs, 0); ++i)
8731132718Skan    {
8732132718Skan      dw_loc_descr_ref t;
8733132718Skan
8734132718Skan      t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8735132718Skan      add_loc_descr (&loc_result, t);
8736132718Skan      size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8737169689Skan      add_loc_descr_op_piece (&loc_result, size);
8738132718Skan    }
873950397Sobrien  return loc_result;
874050397Sobrien}
874150397Sobrien
874290075Sobrien/* Return a location descriptor that designates a constant.  */
874390075Sobrien
874490075Sobrienstatic dw_loc_descr_ref
8745132718Skanint_loc_descriptor (HOST_WIDE_INT i)
874690075Sobrien{
874790075Sobrien  enum dwarf_location_atom op;
874890075Sobrien
874990075Sobrien  /* Pick the smallest representation of a constant, rather than just
875090075Sobrien     defaulting to the LEB encoding.  */
875190075Sobrien  if (i >= 0)
875290075Sobrien    {
875390075Sobrien      if (i <= 31)
875490075Sobrien	op = DW_OP_lit0 + i;
875590075Sobrien      else if (i <= 0xff)
875690075Sobrien	op = DW_OP_const1u;
875790075Sobrien      else if (i <= 0xffff)
875890075Sobrien	op = DW_OP_const2u;
875990075Sobrien      else if (HOST_BITS_PER_WIDE_INT == 32
876090075Sobrien	       || i <= 0xffffffff)
876190075Sobrien	op = DW_OP_const4u;
876290075Sobrien      else
876390075Sobrien	op = DW_OP_constu;
876490075Sobrien    }
876590075Sobrien  else
876690075Sobrien    {
876790075Sobrien      if (i >= -0x80)
876890075Sobrien	op = DW_OP_const1s;
876990075Sobrien      else if (i >= -0x8000)
877090075Sobrien	op = DW_OP_const2s;
877190075Sobrien      else if (HOST_BITS_PER_WIDE_INT == 32
877290075Sobrien	       || i >= -0x80000000)
877390075Sobrien	op = DW_OP_const4s;
877490075Sobrien      else
877590075Sobrien	op = DW_OP_consts;
877690075Sobrien    }
877790075Sobrien
877890075Sobrien  return new_loc_descr (op, i, 0);
877990075Sobrien}
878090075Sobrien
878150397Sobrien/* Return a location descriptor that designates a base+offset location.  */
878250397Sobrien
878350397Sobrienstatic dw_loc_descr_ref
8784169689Skanbased_loc_descr (rtx reg, HOST_WIDE_INT offset)
878550397Sobrien{
8786169689Skan  unsigned int regno;
878750397Sobrien
8788169689Skan  /* We only use "frame base" when we're sure we're talking about the
8789169689Skan     post-prologue local stack frame.  We do this by *not* running
8790169689Skan     register elimination until this point, and recognizing the special
8791169689Skan     argument pointer and soft frame pointer rtx's.  */
8792169689Skan  if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8793169689Skan    {
8794169689Skan      rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8795169689Skan
8796169689Skan      if (elim != reg)
8797169689Skan	{
8798169689Skan	  if (GET_CODE (elim) == PLUS)
8799169689Skan	    {
8800169689Skan	      offset += INTVAL (XEXP (elim, 1));
8801169689Skan	      elim = XEXP (elim, 0);
8802169689Skan	    }
8803169689Skan	  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8804169689Skan		      : stack_pointer_rtx));
8805169689Skan          offset += frame_pointer_fb_offset;
8806169689Skan
8807169689Skan          return new_loc_descr (DW_OP_fbreg, offset, 0);
8808169689Skan	}
8809169689Skan    }
8810169689Skan
8811169689Skan  regno = dbx_reg_number (reg);
8812169689Skan  if (regno <= 31)
8813169689Skan    return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
881450397Sobrien  else
8815169689Skan    return new_loc_descr (DW_OP_bregx, regno, offset);
881650397Sobrien}
881750397Sobrien
881850397Sobrien/* Return true if this RTL expression describes a base+offset calculation.  */
881950397Sobrien
882050397Sobrienstatic inline int
8821132718Skanis_based_loc (rtx rtl)
882250397Sobrien{
882390075Sobrien  return (GET_CODE (rtl) == PLUS
8824169689Skan	  && ((REG_P (XEXP (rtl, 0))
882590075Sobrien	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
882690075Sobrien	       && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
882750397Sobrien}
882850397Sobrien
882950397Sobrien/* The following routine converts the RTL for a variable or parameter
883050397Sobrien   (resident in memory) into an equivalent Dwarf representation of a
883150397Sobrien   mechanism for getting the address of that same variable onto the top of a
883250397Sobrien   hypothetical "address evaluation" stack.
883350397Sobrien
883450397Sobrien   When creating memory location descriptors, we are effectively transforming
883550397Sobrien   the RTL for a memory-resident object into its Dwarf postfix expression
883650397Sobrien   equivalent.  This routine recursively descends an RTL tree, turning
883790075Sobrien   it into Dwarf postfix code as it goes.
883850397Sobrien
883990075Sobrien   MODE is the mode of the memory reference, needed to handle some
884090075Sobrien   autoincrement addressing modes.
884190075Sobrien
8842169689Skan   CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8843169689Skan   location list for RTL.
8844169689Skan
884590075Sobrien   Return 0 if we can't represent the location.  */
884690075Sobrien
884750397Sobrienstatic dw_loc_descr_ref
8848132718Skanmem_loc_descriptor (rtx rtl, enum machine_mode mode)
884950397Sobrien{
885050397Sobrien  dw_loc_descr_ref mem_loc_result = NULL;
8851169689Skan  enum dwarf_location_atom op;
885290075Sobrien
885390075Sobrien  /* Note that for a dynamically sized array, the location we will generate a
885450397Sobrien     description of here will be the lowest numbered location which is
885550397Sobrien     actually within the array.  That's *not* necessarily the same as the
885650397Sobrien     zeroth element of the array.  */
885750397Sobrien
8858169689Skan  rtl = targetm.delegitimize_address (rtl);
885990075Sobrien
886050397Sobrien  switch (GET_CODE (rtl))
886150397Sobrien    {
886290075Sobrien    case POST_INC:
886390075Sobrien    case POST_DEC:
886490075Sobrien    case POST_MODIFY:
886590075Sobrien      /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
886690075Sobrien	 just fall into the SUBREG code.  */
886790075Sobrien
886890075Sobrien      /* ... fall through ...  */
886990075Sobrien
887050397Sobrien    case SUBREG:
887150397Sobrien      /* The case of a subreg may arise when we have a local (register)
8872132718Skan	 variable or a formal (register) parameter which doesn't quite fill
8873132718Skan	 up an entire register.  For now, just assume that it is
8874132718Skan	 legitimate to make the Dwarf info refer to the whole register which
8875132718Skan	 contains the given subreg.  */
8876169689Skan      rtl = XEXP (rtl, 0);
887750397Sobrien
887890075Sobrien      /* ... fall through ...  */
887950397Sobrien
888050397Sobrien    case REG:
888150397Sobrien      /* Whenever a register number forms a part of the description of the
8882132718Skan	 method for calculating the (dynamic) address of a memory resident
8883132718Skan	 object, DWARF rules require the register number be referred to as
8884132718Skan	 a "base register".  This distinction is not based in any way upon
8885132718Skan	 what category of register the hardware believes the given register
8886132718Skan	 belongs to.  This is strictly DWARF terminology we're dealing with
8887132718Skan	 here. Note that in cases where the location of a memory-resident
8888132718Skan	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8889132718Skan	 OP_CONST (0)) the actual DWARF location descriptor that we generate
8890132718Skan	 may just be OP_BASEREG (basereg).  This may look deceptively like
8891132718Skan	 the object in question was allocated to a register (rather than in
8892132718Skan	 memory) so DWARF consumers need to be aware of the subtle
8893132718Skan	 distinction between OP_REG and OP_BASEREG.  */
889490075Sobrien      if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8895169689Skan	mem_loc_result = based_loc_descr (rtl, 0);
889650397Sobrien      break;
889750397Sobrien
889850397Sobrien    case MEM:
889990075Sobrien      mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
890090075Sobrien      if (mem_loc_result != 0)
890190075Sobrien	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
890250397Sobrien      break;
890350397Sobrien
8904117395Skan    case LO_SUM:
8905117395Skan	 rtl = XEXP (rtl, 1);
8906117395Skan
8907117395Skan      /* ... fall through ...  */
8908117395Skan
890990075Sobrien    case LABEL_REF:
891090075Sobrien      /* Some ports can transform a symbol ref into a label ref, because
8911132718Skan	 the symbol ref is too far away and has to be dumped into a constant
8912132718Skan	 pool.  */
891350397Sobrien    case CONST:
891450397Sobrien    case SYMBOL_REF:
891590075Sobrien      /* Alternatively, the symbol in the constant pool might be referenced
891690075Sobrien	 by a different symbol.  */
891790075Sobrien      if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
891890075Sobrien	{
891996263Sobrien	  bool marked;
892096263Sobrien	  rtx tmp = get_pool_constant_mark (rtl, &marked);
892190075Sobrien
892290075Sobrien	  if (GET_CODE (tmp) == SYMBOL_REF)
892396263Sobrien	    {
892496263Sobrien	      rtl = tmp;
892596263Sobrien	      if (CONSTANT_POOL_ADDRESS_P (tmp))
892696263Sobrien		get_pool_constant_mark (tmp, &marked);
892796263Sobrien	      else
892896263Sobrien		marked = true;
892996263Sobrien	    }
893096263Sobrien
893196263Sobrien	  /* If all references to this pool constant were optimized away,
893296263Sobrien	     it was not output and thus we can't represent it.
893396263Sobrien	     FIXME: might try to use DW_OP_const_value here, though
893496263Sobrien	     DW_OP_piece complicates it.  */
893596263Sobrien	  if (!marked)
893696263Sobrien	    return 0;
893790075Sobrien	}
893890075Sobrien
893950397Sobrien      mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
894050397Sobrien      mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
894190075Sobrien      mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8942169689Skan      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
894350397Sobrien      break;
894450397Sobrien
894590075Sobrien    case PRE_MODIFY:
894690075Sobrien      /* Extract the PLUS expression nested inside and fall into
8947132718Skan	 PLUS code below.  */
894890075Sobrien      rtl = XEXP (rtl, 1);
894990075Sobrien      goto plus;
895090075Sobrien
895190075Sobrien    case PRE_INC:
895290075Sobrien    case PRE_DEC:
895390075Sobrien      /* Turn these into a PLUS expression and fall into the PLUS code
895490075Sobrien	 below.  */
895590075Sobrien      rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
895690075Sobrien			  GEN_INT (GET_CODE (rtl) == PRE_INC
895790075Sobrien				   ? GET_MODE_UNIT_SIZE (mode)
895890075Sobrien				   : -GET_MODE_UNIT_SIZE (mode)));
895990075Sobrien
896090075Sobrien      /* ... fall through ...  */
896190075Sobrien
896250397Sobrien    case PLUS:
896390075Sobrien    plus:
896450397Sobrien      if (is_based_loc (rtl))
8965169689Skan	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
896650397Sobrien					  INTVAL (XEXP (rtl, 1)));
896750397Sobrien      else
896850397Sobrien	{
896990075Sobrien	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
897090075Sobrien	  if (mem_loc_result == 0)
897190075Sobrien	    break;
897290075Sobrien
897390075Sobrien	  if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
897490075Sobrien	      && INTVAL (XEXP (rtl, 1)) >= 0)
897590075Sobrien	    add_loc_descr (&mem_loc_result,
897690075Sobrien			   new_loc_descr (DW_OP_plus_uconst,
897790075Sobrien					  INTVAL (XEXP (rtl, 1)), 0));
897890075Sobrien	  else
897990075Sobrien	    {
898090075Sobrien	      add_loc_descr (&mem_loc_result,
898190075Sobrien			     mem_loc_descriptor (XEXP (rtl, 1), mode));
898290075Sobrien	      add_loc_descr (&mem_loc_result,
898390075Sobrien			     new_loc_descr (DW_OP_plus, 0, 0));
898490075Sobrien	    }
898550397Sobrien	}
898650397Sobrien      break;
898750397Sobrien
8988169689Skan    /* If a pseudo-reg is optimized away, it is possible for it to
8989169689Skan       be replaced with a MEM containing a multiply or shift.  */
899050397Sobrien    case MULT:
8991169689Skan      op = DW_OP_mul;
8992169689Skan      goto do_binop;
8993169689Skan
8994169689Skan    case ASHIFT:
8995169689Skan      op = DW_OP_shl;
8996169689Skan      goto do_binop;
8997169689Skan
8998169689Skan    case ASHIFTRT:
8999169689Skan      op = DW_OP_shra;
9000169689Skan      goto do_binop;
9001169689Skan
9002169689Skan    case LSHIFTRT:
9003169689Skan      op = DW_OP_shr;
9004169689Skan      goto do_binop;
9005169689Skan
9006169689Skan    do_binop:
900790075Sobrien      {
900890075Sobrien	dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
900990075Sobrien	dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
901050397Sobrien
901190075Sobrien	if (op0 == 0 || op1 == 0)
901290075Sobrien	  break;
901390075Sobrien
901490075Sobrien	mem_loc_result = op0;
901590075Sobrien	add_loc_descr (&mem_loc_result, op1);
9016169689Skan	add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
901790075Sobrien	break;
901890075Sobrien      }
901990075Sobrien
902050397Sobrien    case CONST_INT:
902190075Sobrien      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
902250397Sobrien      break;
902350397Sobrien
902450397Sobrien    default:
9025169689Skan      gcc_unreachable ();
902650397Sobrien    }
902750397Sobrien
902850397Sobrien  return mem_loc_result;
902950397Sobrien}
903050397Sobrien
903150397Sobrien/* Return a descriptor that describes the concatenation of two locations.
903250397Sobrien   This is typically a complex variable.  */
903350397Sobrien
903450397Sobrienstatic dw_loc_descr_ref
9035132718Skanconcat_loc_descriptor (rtx x0, rtx x1)
903650397Sobrien{
903750397Sobrien  dw_loc_descr_ref cc_loc_result = NULL;
903890075Sobrien  dw_loc_descr_ref x0_ref = loc_descriptor (x0);
903990075Sobrien  dw_loc_descr_ref x1_ref = loc_descriptor (x1);
904050397Sobrien
904190075Sobrien  if (x0_ref == 0 || x1_ref == 0)
904290075Sobrien    return 0;
904390075Sobrien
904490075Sobrien  cc_loc_result = x0_ref;
9045169689Skan  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
904650397Sobrien
904790075Sobrien  add_loc_descr (&cc_loc_result, x1_ref);
9048169689Skan  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
904950397Sobrien
905050397Sobrien  return cc_loc_result;
905150397Sobrien}
905250397Sobrien
905350397Sobrien/* Output a proper Dwarf location descriptor for a variable or parameter
905450397Sobrien   which is either allocated in a register or in a memory location.  For a
905550397Sobrien   register, we just generate an OP_REG and the register number.  For a
905650397Sobrien   memory location we provide a Dwarf postfix expression describing how to
905790075Sobrien   generate the (dynamic) address of the object onto the address stack.
905850397Sobrien
905990075Sobrien   If we don't know how to describe it, return 0.  */
906090075Sobrien
906150397Sobrienstatic dw_loc_descr_ref
9062132718Skanloc_descriptor (rtx rtl)
906350397Sobrien{
906450397Sobrien  dw_loc_descr_ref loc_result = NULL;
906590075Sobrien
906650397Sobrien  switch (GET_CODE (rtl))
906750397Sobrien    {
906850397Sobrien    case SUBREG:
906950397Sobrien      /* The case of a subreg may arise when we have a local (register)
9070132718Skan	 variable or a formal (register) parameter which doesn't quite fill
9071132718Skan	 up an entire register.  For now, just assume that it is
9072132718Skan	 legitimate to make the Dwarf info refer to the whole register which
9073132718Skan	 contains the given subreg.  */
907490075Sobrien      rtl = SUBREG_REG (rtl);
907550397Sobrien
907690075Sobrien      /* ... fall through ...  */
907750397Sobrien
907850397Sobrien    case REG:
907950397Sobrien      loc_result = reg_loc_descriptor (rtl);
908050397Sobrien      break;
908150397Sobrien
908250397Sobrien    case MEM:
908390075Sobrien      loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
908450397Sobrien      break;
908550397Sobrien
908650397Sobrien    case CONCAT:
908750397Sobrien      loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
908850397Sobrien      break;
908950397Sobrien
9090169689Skan    case VAR_LOCATION:
9091169689Skan      /* Single part.  */
9092169689Skan      if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9093169689Skan	{
9094169689Skan	  loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9095169689Skan	  break;
9096169689Skan	}
9097169689Skan
9098169689Skan      rtl = XEXP (rtl, 1);
9099169689Skan      /* FALLTHRU */
9100169689Skan
9101169689Skan    case PARALLEL:
9102169689Skan      {
9103169689Skan	rtvec par_elems = XVEC (rtl, 0);
9104169689Skan	int num_elem = GET_NUM_ELEM (par_elems);
9105169689Skan	enum machine_mode mode;
9106169689Skan	int i;
9107169689Skan
9108169689Skan	/* Create the first one, so we have something to add to.  */
9109169689Skan	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9110169689Skan	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9111169689Skan	add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9112169689Skan	for (i = 1; i < num_elem; i++)
9113169689Skan	  {
9114169689Skan	    dw_loc_descr_ref temp;
9115169689Skan
9116169689Skan	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9117169689Skan	    add_loc_descr (&loc_result, temp);
9118169689Skan	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9119169689Skan	    add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9120169689Skan	  }
9121169689Skan      }
9122169689Skan      break;
9123169689Skan
912450397Sobrien    default:
9125169689Skan      gcc_unreachable ();
912650397Sobrien    }
912750397Sobrien
912850397Sobrien  return loc_result;
912950397Sobrien}
913050397Sobrien
913190075Sobrien/* Similar, but generate the descriptor from trees instead of rtl.  This comes
9132169689Skan   up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9133169689Skan   a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9134169689Skan   top-level invocation, and we require the address of LOC; is 0 if we require
9135169689Skan   the value of LOC.  */
913690075Sobrien
913790075Sobrienstatic dw_loc_descr_ref
9138169689Skanloc_descriptor_from_tree_1 (tree loc, int want_address)
913990075Sobrien{
914090075Sobrien  dw_loc_descr_ref ret, ret1;
9141169689Skan  int have_address = 0;
914290075Sobrien  enum dwarf_location_atom op;
914390075Sobrien
914490075Sobrien  /* ??? Most of the time we do not take proper care for sign/zero
914590075Sobrien     extending the values properly.  Hopefully this won't be a real
914690075Sobrien     problem...  */
914790075Sobrien
914890075Sobrien  switch (TREE_CODE (loc))
914990075Sobrien    {
915090075Sobrien    case ERROR_MARK:
915190075Sobrien      return 0;
915290075Sobrien
915390075Sobrien    case PLACEHOLDER_EXPR:
915490075Sobrien      /* This case involves extracting fields from an object to determine the
915590075Sobrien	 position of other fields.  We don't try to encode this here.  The
915690075Sobrien	 only user of this is Ada, which encodes the needed information using
915790075Sobrien	 the names of types.  */
915890075Sobrien      return 0;
915990075Sobrien
916090075Sobrien    case CALL_EXPR:
916190075Sobrien      return 0;
916290075Sobrien
9163132718Skan    case PREINCREMENT_EXPR:
9164132718Skan    case PREDECREMENT_EXPR:
9165132718Skan    case POSTINCREMENT_EXPR:
9166132718Skan    case POSTDECREMENT_EXPR:
9167132718Skan      /* There are no opcodes for these operations.  */
9168132718Skan      return 0;
9169132718Skan
917090075Sobrien    case ADDR_EXPR:
9171169689Skan      /* If we already want an address, there's nothing we can do.  */
9172169689Skan      if (want_address)
9173169689Skan	return 0;
917490075Sobrien
9175169689Skan      /* Otherwise, process the argument and look for the address.  */
9176169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
917790075Sobrien
917890075Sobrien    case VAR_DECL:
9179169689Skan      if (DECL_THREAD_LOCAL_P (loc))
9180117395Skan	{
9181117395Skan	  rtx rtl;
9182117395Skan
9183117395Skan	  /* If this is not defined, we have no way to emit the data.  */
9184169689Skan	  if (!targetm.asm_out.output_dwarf_dtprel)
9185169689Skan	    return 0;
9186117395Skan
9187117395Skan	  /* The way DW_OP_GNU_push_tls_address is specified, we can only
9188117395Skan	     look up addresses of objects in the current module.  */
9189117395Skan	  if (DECL_EXTERNAL (loc))
9190117395Skan	    return 0;
9191117395Skan
9192117395Skan	  rtl = rtl_for_decl_location (loc);
9193117395Skan	  if (rtl == NULL_RTX)
9194117395Skan	    return 0;
9195117395Skan
9196169689Skan	  if (!MEM_P (rtl))
9197117395Skan	    return 0;
9198117395Skan	  rtl = XEXP (rtl, 0);
9199117395Skan	  if (! CONSTANT_P (rtl))
9200117395Skan	    return 0;
9201117395Skan
9202117395Skan	  ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9203117395Skan	  ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9204117395Skan	  ret->dw_loc_oprnd1.v.val_addr = rtl;
9205117395Skan
9206117395Skan	  ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9207117395Skan	  add_loc_descr (&ret, ret1);
9208117395Skan
9209169689Skan	  have_address = 1;
9210117395Skan	  break;
9211117395Skan	}
9212169689Skan      /* FALLTHRU */
9213117395Skan
921490075Sobrien    case PARM_DECL:
9215169689Skan      if (DECL_HAS_VALUE_EXPR_P (loc))
9216169689Skan	return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9217169689Skan					   want_address);
9218169689Skan      /* FALLTHRU */
9219169689Skan
9220169689Skan    case RESULT_DECL:
9221169689Skan    case FUNCTION_DECL:
922290075Sobrien      {
922390075Sobrien	rtx rtl = rtl_for_decl_location (loc);
922490075Sobrien
922590075Sobrien	if (rtl == NULL_RTX)
922690075Sobrien	  return 0;
9227169689Skan        else if (GET_CODE (rtl) == CONST_INT)
9228169689Skan	  {
9229169689Skan	    HOST_WIDE_INT val = INTVAL (rtl);
9230169689Skan	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9231169689Skan	      val &= GET_MODE_MASK (DECL_MODE (loc));
9232169689Skan	    ret = int_loc_descriptor (val);
9233169689Skan	  }
9234169689Skan	else if (GET_CODE (rtl) == CONST_STRING)
9235169689Skan	  return 0;
923690075Sobrien	else if (CONSTANT_P (rtl))
923790075Sobrien	  {
923890075Sobrien	    ret = new_loc_descr (DW_OP_addr, 0, 0);
923990075Sobrien	    ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
924090075Sobrien	    ret->dw_loc_oprnd1.v.val_addr = rtl;
924190075Sobrien	  }
924290075Sobrien	else
924390075Sobrien	  {
9244169689Skan	    enum machine_mode mode;
924590075Sobrien
9246169689Skan	    /* Certain constructs can only be represented at top-level.  */
9247169689Skan	    if (want_address == 2)
9248169689Skan	      return loc_descriptor (rtl);
9249169689Skan
9250169689Skan	    mode = GET_MODE (rtl);
9251169689Skan	    if (MEM_P (rtl))
925290075Sobrien	      {
925390075Sobrien		rtl = XEXP (rtl, 0);
9254169689Skan		have_address = 1;
925590075Sobrien	      }
925690075Sobrien	    ret = mem_loc_descriptor (rtl, mode);
925790075Sobrien	  }
925890075Sobrien      }
925990075Sobrien      break;
926090075Sobrien
926190075Sobrien    case INDIRECT_REF:
9262169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9263169689Skan      have_address = 1;
926490075Sobrien      break;
926590075Sobrien
926690075Sobrien    case COMPOUND_EXPR:
9267169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
926890075Sobrien
926990075Sobrien    case NOP_EXPR:
927090075Sobrien    case CONVERT_EXPR:
927190075Sobrien    case NON_LVALUE_EXPR:
927290075Sobrien    case VIEW_CONVERT_EXPR:
927390075Sobrien    case SAVE_EXPR:
9274132718Skan    case MODIFY_EXPR:
9275169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
927690075Sobrien
927790075Sobrien    case COMPONENT_REF:
927890075Sobrien    case BIT_FIELD_REF:
927990075Sobrien    case ARRAY_REF:
928090075Sobrien    case ARRAY_RANGE_REF:
928190075Sobrien      {
928290075Sobrien	tree obj, offset;
928390075Sobrien	HOST_WIDE_INT bitsize, bitpos, bytepos;
928490075Sobrien	enum machine_mode mode;
928590075Sobrien	int volatilep;
9286169689Skan	int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
928790075Sobrien
928890075Sobrien	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9289169689Skan				   &unsignedp, &volatilep, false);
929090075Sobrien
929190075Sobrien	if (obj == loc)
929290075Sobrien	  return 0;
929390075Sobrien
9294169689Skan	ret = loc_descriptor_from_tree_1 (obj, 1);
929590075Sobrien	if (ret == 0
929690075Sobrien	    || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
929790075Sobrien	  return 0;
929890075Sobrien
929990075Sobrien	if (offset != NULL_TREE)
930090075Sobrien	  {
930190075Sobrien	    /* Variable offset.  */
9302169689Skan	    add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
930390075Sobrien	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
930490075Sobrien	  }
930590075Sobrien
930690075Sobrien	bytepos = bitpos / BITS_PER_UNIT;
930790075Sobrien	if (bytepos > 0)
930890075Sobrien	  add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
930990075Sobrien	else if (bytepos < 0)
931090075Sobrien	  {
931190075Sobrien	    add_loc_descr (&ret, int_loc_descriptor (bytepos));
931290075Sobrien	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
931390075Sobrien	  }
9314169689Skan
9315169689Skan	have_address = 1;
931690075Sobrien	break;
931790075Sobrien      }
931890075Sobrien
931990075Sobrien    case INTEGER_CST:
932090075Sobrien      if (host_integerp (loc, 0))
932190075Sobrien	ret = int_loc_descriptor (tree_low_cst (loc, 0));
932290075Sobrien      else
932390075Sobrien	return 0;
932490075Sobrien      break;
932590075Sobrien
9326132718Skan    case CONSTRUCTOR:
9327132718Skan      {
9328132718Skan	/* Get an RTL for this, if something has been emitted.  */
9329132718Skan	rtx rtl = lookup_constant_def (loc);
9330132718Skan	enum machine_mode mode;
9331132718Skan
9332169689Skan	if (!rtl || !MEM_P (rtl))
9333132718Skan	  return 0;
9334132718Skan	mode = GET_MODE (rtl);
9335132718Skan	rtl = XEXP (rtl, 0);
9336132718Skan	ret = mem_loc_descriptor (rtl, mode);
9337169689Skan	have_address = 1;
9338132718Skan	break;
9339132718Skan      }
9340132718Skan
9341117395Skan    case TRUTH_AND_EXPR:
934290075Sobrien    case TRUTH_ANDIF_EXPR:
934390075Sobrien    case BIT_AND_EXPR:
934490075Sobrien      op = DW_OP_and;
934590075Sobrien      goto do_binop;
934690075Sobrien
934790075Sobrien    case TRUTH_XOR_EXPR:
934890075Sobrien    case BIT_XOR_EXPR:
934990075Sobrien      op = DW_OP_xor;
935090075Sobrien      goto do_binop;
935190075Sobrien
935290075Sobrien    case TRUTH_OR_EXPR:
935390075Sobrien    case TRUTH_ORIF_EXPR:
935490075Sobrien    case BIT_IOR_EXPR:
935590075Sobrien      op = DW_OP_or;
935690075Sobrien      goto do_binop;
935790075Sobrien
9358132718Skan    case FLOOR_DIV_EXPR:
9359132718Skan    case CEIL_DIV_EXPR:
9360132718Skan    case ROUND_DIV_EXPR:
936190075Sobrien    case TRUNC_DIV_EXPR:
936290075Sobrien      op = DW_OP_div;
936390075Sobrien      goto do_binop;
936490075Sobrien
936590075Sobrien    case MINUS_EXPR:
936690075Sobrien      op = DW_OP_minus;
936790075Sobrien      goto do_binop;
936890075Sobrien
9369132718Skan    case FLOOR_MOD_EXPR:
9370132718Skan    case CEIL_MOD_EXPR:
9371132718Skan    case ROUND_MOD_EXPR:
937290075Sobrien    case TRUNC_MOD_EXPR:
937390075Sobrien      op = DW_OP_mod;
937490075Sobrien      goto do_binop;
937590075Sobrien
937690075Sobrien    case MULT_EXPR:
937790075Sobrien      op = DW_OP_mul;
937890075Sobrien      goto do_binop;
937990075Sobrien
938090075Sobrien    case LSHIFT_EXPR:
938190075Sobrien      op = DW_OP_shl;
938290075Sobrien      goto do_binop;
938390075Sobrien
938490075Sobrien    case RSHIFT_EXPR:
9385169689Skan      op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
938690075Sobrien      goto do_binop;
938790075Sobrien
938890075Sobrien    case PLUS_EXPR:
938990075Sobrien      if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
939090075Sobrien	  && host_integerp (TREE_OPERAND (loc, 1), 0))
939190075Sobrien	{
9392169689Skan	  ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
939390075Sobrien	  if (ret == 0)
939490075Sobrien	    return 0;
939590075Sobrien
939690075Sobrien	  add_loc_descr (&ret,
939790075Sobrien			 new_loc_descr (DW_OP_plus_uconst,
939890075Sobrien					tree_low_cst (TREE_OPERAND (loc, 1),
939990075Sobrien						      0),
940090075Sobrien					0));
940190075Sobrien	  break;
940290075Sobrien	}
940390075Sobrien
940490075Sobrien      op = DW_OP_plus;
940590075Sobrien      goto do_binop;
940690075Sobrien
940790075Sobrien    case LE_EXPR:
9408169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
940990075Sobrien	return 0;
941090075Sobrien
941190075Sobrien      op = DW_OP_le;
941290075Sobrien      goto do_binop;
941390075Sobrien
941490075Sobrien    case GE_EXPR:
9415169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
941690075Sobrien	return 0;
941790075Sobrien
941890075Sobrien      op = DW_OP_ge;
941990075Sobrien      goto do_binop;
942090075Sobrien
942190075Sobrien    case LT_EXPR:
9422169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
942390075Sobrien	return 0;
942490075Sobrien
942590075Sobrien      op = DW_OP_lt;
942690075Sobrien      goto do_binop;
942790075Sobrien
942890075Sobrien    case GT_EXPR:
9429169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
943090075Sobrien	return 0;
943190075Sobrien
943290075Sobrien      op = DW_OP_gt;
943390075Sobrien      goto do_binop;
943490075Sobrien
943590075Sobrien    case EQ_EXPR:
943690075Sobrien      op = DW_OP_eq;
943790075Sobrien      goto do_binop;
943890075Sobrien
943990075Sobrien    case NE_EXPR:
944090075Sobrien      op = DW_OP_ne;
944190075Sobrien      goto do_binop;
944290075Sobrien
944390075Sobrien    do_binop:
9444169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9445169689Skan      ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
944690075Sobrien      if (ret == 0 || ret1 == 0)
944790075Sobrien	return 0;
944890075Sobrien
944990075Sobrien      add_loc_descr (&ret, ret1);
945090075Sobrien      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
945190075Sobrien      break;
945290075Sobrien
945390075Sobrien    case TRUTH_NOT_EXPR:
945490075Sobrien    case BIT_NOT_EXPR:
945590075Sobrien      op = DW_OP_not;
945690075Sobrien      goto do_unop;
945790075Sobrien
945890075Sobrien    case ABS_EXPR:
945990075Sobrien      op = DW_OP_abs;
946090075Sobrien      goto do_unop;
946190075Sobrien
946290075Sobrien    case NEGATE_EXPR:
946390075Sobrien      op = DW_OP_neg;
946490075Sobrien      goto do_unop;
946590075Sobrien
946690075Sobrien    do_unop:
9467169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
946890075Sobrien      if (ret == 0)
946990075Sobrien	return 0;
947090075Sobrien
947190075Sobrien      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
947290075Sobrien      break;
947390075Sobrien
9474169689Skan    case MIN_EXPR:
947590075Sobrien    case MAX_EXPR:
9476169689Skan      {
9477169689Skan        const enum tree_code code =
9478169689Skan          TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
947990075Sobrien
9480169689Skan        loc = build3 (COND_EXPR, TREE_TYPE (loc),
9481169689Skan		      build2 (code, integer_type_node,
9482169689Skan			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9483169689Skan                      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9484169689Skan      }
9485169689Skan
948690075Sobrien      /* ... fall through ...  */
948790075Sobrien
948890075Sobrien    case COND_EXPR:
948990075Sobrien      {
949090075Sobrien	dw_loc_descr_ref lhs
9491169689Skan	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
949290075Sobrien	dw_loc_descr_ref rhs
9493169689Skan	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
949490075Sobrien	dw_loc_descr_ref bra_node, jump_node, tmp;
949590075Sobrien
9496169689Skan	ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
949790075Sobrien	if (ret == 0 || lhs == 0 || rhs == 0)
949890075Sobrien	  return 0;
949990075Sobrien
950090075Sobrien	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
950190075Sobrien	add_loc_descr (&ret, bra_node);
950290075Sobrien
950390075Sobrien	add_loc_descr (&ret, rhs);
950490075Sobrien	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
950590075Sobrien	add_loc_descr (&ret, jump_node);
950690075Sobrien
950790075Sobrien	add_loc_descr (&ret, lhs);
950890075Sobrien	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
950990075Sobrien	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
951090075Sobrien
951190075Sobrien	/* ??? Need a node to point the skip at.  Use a nop.  */
951290075Sobrien	tmp = new_loc_descr (DW_OP_nop, 0, 0);
951390075Sobrien	add_loc_descr (&ret, tmp);
951490075Sobrien	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
951590075Sobrien	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
951690075Sobrien      }
951790075Sobrien      break;
951890075Sobrien
9519146895Skan    case FIX_TRUNC_EXPR:
9520146895Skan    case FIX_CEIL_EXPR:
9521146895Skan    case FIX_FLOOR_EXPR:
9522146895Skan    case FIX_ROUND_EXPR:
9523146895Skan      return 0;
9524146895Skan
952590075Sobrien    default:
9526132718Skan      /* Leave front-end specific codes as simply unknown.  This comes
9527132718Skan	 up, for instance, with the C STMT_EXPR.  */
9528132718Skan      if ((unsigned int) TREE_CODE (loc)
9529132718Skan          >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9530132718Skan	return 0;
9531132718Skan
9532146895Skan#ifdef ENABLE_CHECKING
9533132718Skan      /* Otherwise this is a generic code; we should just lists all of
9534169689Skan	 these explicitly.  We forgot one.  */
9535169689Skan      gcc_unreachable ();
9536146895Skan#else
9537146895Skan      /* In a release build, we want to degrade gracefully: better to
9538146895Skan	 generate incomplete debugging information than to crash.  */
9539146895Skan      return NULL;
9540146895Skan#endif
954190075Sobrien    }
954290075Sobrien
954390075Sobrien  /* Show if we can't fill the request for an address.  */
9544169689Skan  if (want_address && !have_address)
954590075Sobrien    return 0;
954690075Sobrien
954790075Sobrien  /* If we've got an address and don't want one, dereference.  */
9548169689Skan  if (!want_address && have_address && ret)
954990075Sobrien    {
955090075Sobrien      HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
955190075Sobrien
955290075Sobrien      if (size > DWARF2_ADDR_SIZE || size == -1)
955390075Sobrien	return 0;
955490075Sobrien      else if (size == DWARF2_ADDR_SIZE)
955590075Sobrien	op = DW_OP_deref;
955690075Sobrien      else
955790075Sobrien	op = DW_OP_deref_size;
955890075Sobrien
955990075Sobrien      add_loc_descr (&ret, new_loc_descr (op, size, 0));
956090075Sobrien    }
956190075Sobrien
956290075Sobrien  return ret;
956390075Sobrien}
956490075Sobrien
9565169689Skanstatic inline dw_loc_descr_ref
9566169689Skanloc_descriptor_from_tree (tree loc)
9567169689Skan{
9568169689Skan  return loc_descriptor_from_tree_1 (loc, 2);
9569169689Skan}
9570169689Skan
957190075Sobrien/* Given a value, round it up to the lowest multiple of `boundary'
957250397Sobrien   which is not less than the value itself.  */
957350397Sobrien
957490075Sobrienstatic inline HOST_WIDE_INT
9575132718Skanceiling (HOST_WIDE_INT value, unsigned int boundary)
957650397Sobrien{
957750397Sobrien  return (((value + boundary - 1) / boundary) * boundary);
957850397Sobrien}
957950397Sobrien
958050397Sobrien/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
958150397Sobrien   pointer to the declared type for the relevant field variable, or return
958250397Sobrien   `integer_type_node' if the given node turns out to be an
958350397Sobrien   ERROR_MARK node.  */
958450397Sobrien
958550397Sobrienstatic inline tree
9586132718Skanfield_type (tree decl)
958750397Sobrien{
958890075Sobrien  tree type;
958950397Sobrien
959050397Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
959150397Sobrien    return integer_type_node;
959250397Sobrien
959350397Sobrien  type = DECL_BIT_FIELD_TYPE (decl);
959450397Sobrien  if (type == NULL_TREE)
959550397Sobrien    type = TREE_TYPE (decl);
959650397Sobrien
959750397Sobrien  return type;
959850397Sobrien}
959950397Sobrien
960090075Sobrien/* Given a pointer to a tree node, return the alignment in bits for
960190075Sobrien   it, or else return BITS_PER_WORD if the node actually turns out to
960290075Sobrien   be an ERROR_MARK node.  */
960350397Sobrien
960450397Sobrienstatic inline unsigned
9605132718Skansimple_type_align_in_bits (tree type)
960650397Sobrien{
960750397Sobrien  return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
960850397Sobrien}
960950397Sobrien
961090075Sobrienstatic inline unsigned
9611132718Skansimple_decl_align_in_bits (tree decl)
961290075Sobrien{
9613117395Skan  return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
961490075Sobrien}
961590075Sobrien
961690075Sobrien/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
961790075Sobrien   lowest addressed byte of the "containing object" for the given FIELD_DECL,
961890075Sobrien   or return 0 if we are unable to determine what that offset is, either
961990075Sobrien   because the argument turns out to be a pointer to an ERROR_MARK node, or
962090075Sobrien   because the offset is actually variable.  (We can't handle the latter case
962190075Sobrien   just yet).  */
962250397Sobrien
962390075Sobrienstatic HOST_WIDE_INT
9624132718Skanfield_byte_offset (tree decl)
962550397Sobrien{
962690075Sobrien  unsigned int type_align_in_bits;
962790075Sobrien  unsigned int decl_align_in_bits;
962890075Sobrien  unsigned HOST_WIDE_INT type_size_in_bits;
962990075Sobrien  HOST_WIDE_INT object_offset_in_bits;
963090075Sobrien  tree type;
963190075Sobrien  tree field_size_tree;
963290075Sobrien  HOST_WIDE_INT bitpos_int;
963390075Sobrien  HOST_WIDE_INT deepest_bitpos;
963490075Sobrien  unsigned HOST_WIDE_INT field_size_in_bits;
963550397Sobrien
963650397Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
963750397Sobrien    return 0;
963850397Sobrien
9639169689Skan  gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9640169689Skan
964150397Sobrien  type = field_type (decl);
964250397Sobrien  field_size_tree = DECL_SIZE (decl);
964350397Sobrien
964490075Sobrien  /* The size could be unspecified if there was an error, or for
964590075Sobrien     a flexible array member.  */
964690075Sobrien  if (! field_size_tree)
964790075Sobrien    field_size_tree = bitsize_zero_node;
964890075Sobrien
964990075Sobrien  /* We cannot yet cope with fields whose positions are variable, so
965050397Sobrien     for now, when we see such things, we simply return 0.  Someday, we may
965150397Sobrien     be able to handle such cases, but it will be damn difficult.  */
965290075Sobrien  if (! host_integerp (bit_position (decl), 0))
965350397Sobrien    return 0;
965450397Sobrien
965590075Sobrien  bitpos_int = int_bit_position (decl);
965650397Sobrien
965790075Sobrien  /* If we don't know the size of the field, pretend it's a full word.  */
965890075Sobrien  if (host_integerp (field_size_tree, 1))
965990075Sobrien    field_size_in_bits = tree_low_cst (field_size_tree, 1);
966090075Sobrien  else
966190075Sobrien    field_size_in_bits = BITS_PER_WORD;
966290075Sobrien
966350397Sobrien  type_size_in_bits = simple_type_size_in_bits (type);
966450397Sobrien  type_align_in_bits = simple_type_align_in_bits (type);
9665117395Skan  decl_align_in_bits = simple_decl_align_in_bits (decl);
966650397Sobrien
966790075Sobrien  /* The GCC front-end doesn't make any attempt to keep track of the starting
966890075Sobrien     bit offset (relative to the start of the containing structure type) of the
966990075Sobrien     hypothetical "containing object" for a bit-field.  Thus, when computing
967090075Sobrien     the byte offset value for the start of the "containing object" of a
967190075Sobrien     bit-field, we must deduce this information on our own. This can be rather
967290075Sobrien     tricky to do in some cases.  For example, handling the following structure
967390075Sobrien     type definition when compiling for an i386/i486 target (which only aligns
967490075Sobrien     long long's to 32-bit boundaries) can be very tricky:
967550397Sobrien
967650397Sobrien	 struct S { int field1; long long field2:31; };
967750397Sobrien
967890075Sobrien     Fortunately, there is a simple rule-of-thumb which can be used in such
967990075Sobrien     cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
968090075Sobrien     structure shown above.  It decides to do this based upon one simple rule
968190075Sobrien     for bit-field allocation.  GCC allocates each "containing object" for each
968290075Sobrien     bit-field at the first (i.e. lowest addressed) legitimate alignment
968390075Sobrien     boundary (based upon the required minimum alignment for the declared type
968490075Sobrien     of the field) which it can possibly use, subject to the condition that
968590075Sobrien     there is still enough available space remaining in the containing object
968690075Sobrien     (when allocated at the selected point) to fully accommodate all of the
968790075Sobrien     bits of the bit-field itself.
968850397Sobrien
968990075Sobrien     This simple rule makes it obvious why GCC allocates 8 bytes for each
969090075Sobrien     object of the structure type shown above.  When looking for a place to
969190075Sobrien     allocate the "containing object" for `field2', the compiler simply tries
969290075Sobrien     to allocate a 64-bit "containing object" at each successive 32-bit
969390075Sobrien     boundary (starting at zero) until it finds a place to allocate that 64-
969490075Sobrien     bit field such that at least 31 contiguous (and previously unallocated)
969590075Sobrien     bits remain within that selected 64 bit field.  (As it turns out, for the
969690075Sobrien     example above, the compiler finds it is OK to allocate the "containing
969790075Sobrien     object" 64-bit field at bit-offset zero within the structure type.)
969890075Sobrien
969990075Sobrien     Here we attempt to work backwards from the limited set of facts we're
970090075Sobrien     given, and we try to deduce from those facts, where GCC must have believed
970190075Sobrien     that the containing object started (within the structure type). The value
970290075Sobrien     we deduce is then used (by the callers of this routine) to generate
970390075Sobrien     DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
970490075Sobrien     and, in the case of DW_AT_location, regular fields as well).  */
970590075Sobrien
970650397Sobrien  /* Figure out the bit-distance from the start of the structure to the
970750397Sobrien     "deepest" bit of the bit-field.  */
970850397Sobrien  deepest_bitpos = bitpos_int + field_size_in_bits;
970950397Sobrien
971050397Sobrien  /* This is the tricky part.  Use some fancy footwork to deduce where the
971150397Sobrien     lowest addressed bit of the containing object must be.  */
971290075Sobrien  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
971350397Sobrien
971490075Sobrien  /* Round up to type_align by default.  This works best for bitfields.  */
971590075Sobrien  object_offset_in_bits += type_align_in_bits - 1;
971690075Sobrien  object_offset_in_bits /= type_align_in_bits;
971790075Sobrien  object_offset_in_bits *= type_align_in_bits;
971850397Sobrien
971990075Sobrien  if (object_offset_in_bits > bitpos_int)
972090075Sobrien    {
972190075Sobrien      /* Sigh, the decl must be packed.  */
972290075Sobrien      object_offset_in_bits = deepest_bitpos - type_size_in_bits;
972350397Sobrien
972490075Sobrien      /* Round up to decl_align instead.  */
972590075Sobrien      object_offset_in_bits += decl_align_in_bits - 1;
972690075Sobrien      object_offset_in_bits /= decl_align_in_bits;
972790075Sobrien      object_offset_in_bits *= decl_align_in_bits;
972890075Sobrien    }
972990075Sobrien
973090075Sobrien  return object_offset_in_bits / BITS_PER_UNIT;
973150397Sobrien}
973250397Sobrien
973350397Sobrien/* The following routines define various Dwarf attributes and any data
973450397Sobrien   associated with them.  */
973550397Sobrien
973650397Sobrien/* Add a location description attribute value to a DIE.
973750397Sobrien
973850397Sobrien   This emits location attributes suitable for whole variables and
973950397Sobrien   whole parameters.  Note that the location attributes for struct fields are
974050397Sobrien   generated by the routine `data_member_location_attribute' below.  */
974150397Sobrien
9742117395Skanstatic inline void
9743132718Skanadd_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9744132718Skan			     dw_loc_descr_ref descr)
974550397Sobrien{
974690075Sobrien  if (descr != 0)
974790075Sobrien    add_AT_loc (die, attr_kind, descr);
974850397Sobrien}
974950397Sobrien
975090075Sobrien/* Attach the specialized form of location attribute used for data members of
975190075Sobrien   struct and union types.  In the special case of a FIELD_DECL node which
975290075Sobrien   represents a bit-field, the "offset" part of this special location
975390075Sobrien   descriptor must indicate the distance in bytes from the lowest-addressed
975490075Sobrien   byte of the containing struct or union type to the lowest-addressed byte of
975590075Sobrien   the "containing object" for the bit-field.  (See the `field_byte_offset'
975690075Sobrien   function above).
975750397Sobrien
975890075Sobrien   For any given bit-field, the "containing object" is a hypothetical object
975990075Sobrien   (of some integral or enum type) within which the given bit-field lives.  The
976090075Sobrien   type of this hypothetical "containing object" is always the same as the
976190075Sobrien   declared type of the individual bit-field itself (for GCC anyway... the
976290075Sobrien   DWARF spec doesn't actually mandate this).  Note that it is the size (in
976390075Sobrien   bytes) of the hypothetical "containing object" which will be given in the
976490075Sobrien   DW_AT_byte_size attribute for this bit-field.  (See the
976590075Sobrien   `byte_size_attribute' function below.)  It is also used when calculating the
976690075Sobrien   value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
976790075Sobrien   function below.)  */
976890075Sobrien
976950397Sobrienstatic void
9770132718Skanadd_data_member_location_attribute (dw_die_ref die, tree decl)
977150397Sobrien{
9772132718Skan  HOST_WIDE_INT offset;
977390075Sobrien  dw_loc_descr_ref loc_descr = 0;
977450397Sobrien
9775169689Skan  if (TREE_CODE (decl) == TREE_BINFO)
977690075Sobrien    {
977790075Sobrien      /* We're working on the TAG_inheritance for a base class.  */
9778169689Skan      if (BINFO_VIRTUAL_P (decl) && is_cxx ())
977990075Sobrien	{
978090075Sobrien	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
978190075Sobrien	     aren't at a fixed offset from all (sub)objects of the same
978290075Sobrien	     type.  We need to extract the appropriate offset from our
978390075Sobrien	     vtable.  The following dwarf expression means
978490075Sobrien
978590075Sobrien	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
978690075Sobrien
978790075Sobrien	     This is specific to the V3 ABI, of course.  */
978890075Sobrien
978990075Sobrien	  dw_loc_descr_ref tmp;
979090075Sobrien
979190075Sobrien	  /* Make a copy of the object address.  */
979290075Sobrien	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
979390075Sobrien	  add_loc_descr (&loc_descr, tmp);
979490075Sobrien
979590075Sobrien	  /* Extract the vtable address.  */
979690075Sobrien	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
979790075Sobrien	  add_loc_descr (&loc_descr, tmp);
979890075Sobrien
979990075Sobrien	  /* Calculate the address of the offset.  */
980090075Sobrien	  offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9801169689Skan	  gcc_assert (offset < 0);
980290075Sobrien
980390075Sobrien	  tmp = int_loc_descriptor (-offset);
980490075Sobrien	  add_loc_descr (&loc_descr, tmp);
980590075Sobrien	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
980690075Sobrien	  add_loc_descr (&loc_descr, tmp);
980790075Sobrien
980890075Sobrien	  /* Extract the offset.  */
980990075Sobrien	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
981090075Sobrien	  add_loc_descr (&loc_descr, tmp);
981190075Sobrien
981290075Sobrien	  /* Add it to the object address.  */
981390075Sobrien	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
981490075Sobrien	  add_loc_descr (&loc_descr, tmp);
981590075Sobrien	}
981690075Sobrien      else
981790075Sobrien	offset = tree_low_cst (BINFO_OFFSET (decl), 0);
981890075Sobrien    }
981950397Sobrien  else
982050397Sobrien    offset = field_byte_offset (decl);
982150397Sobrien
982290075Sobrien  if (! loc_descr)
982390075Sobrien    {
982490075Sobrien      enum dwarf_location_atom op;
982550397Sobrien
982690075Sobrien      /* The DWARF2 standard says that we should assume that the structure
982790075Sobrien	 address is already on the stack, so we can specify a structure field
982890075Sobrien	 address by using DW_OP_plus_uconst.  */
982990075Sobrien
983050397Sobrien#ifdef MIPS_DEBUGGING_INFO
983190075Sobrien      /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
983290075Sobrien	 operator correctly.  It works only if we leave the offset on the
983390075Sobrien	 stack.  */
983490075Sobrien      op = DW_OP_constu;
983550397Sobrien#else
983690075Sobrien      op = DW_OP_plus_uconst;
983750397Sobrien#endif
983850397Sobrien
983990075Sobrien      loc_descr = new_loc_descr (op, offset, 0);
984090075Sobrien    }
984190075Sobrien
984250397Sobrien  add_AT_loc (die, DW_AT_data_member_location, loc_descr);
984350397Sobrien}
984450397Sobrien
9845132718Skan/* Writes integer values to dw_vec_const array.  */
9846132718Skan
9847132718Skanstatic void
9848132718Skaninsert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9849132718Skan{
9850132718Skan  while (size != 0)
9851132718Skan    {
9852132718Skan      *dest++ = val & 0xff;
9853132718Skan      val >>= 8;
9854132718Skan      --size;
9855132718Skan    }
9856132718Skan}
9857132718Skan
9858132718Skan/* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9859132718Skan
9860132718Skanstatic HOST_WIDE_INT
9861132718Skanextract_int (const unsigned char *src, unsigned int size)
9862132718Skan{
9863132718Skan  HOST_WIDE_INT val = 0;
9864132718Skan
9865132718Skan  src += size;
9866132718Skan  while (size != 0)
9867132718Skan    {
9868132718Skan      val <<= 8;
9869132718Skan      val |= *--src & 0xff;
9870132718Skan      --size;
9871132718Skan    }
9872132718Skan  return val;
9873132718Skan}
9874132718Skan
9875132718Skan/* Writes floating point values to dw_vec_const array.  */
9876132718Skan
9877132718Skanstatic void
9878132718Skaninsert_float (rtx rtl, unsigned char *array)
9879132718Skan{
9880132718Skan  REAL_VALUE_TYPE rv;
9881132718Skan  long val[4];
9882132718Skan  int i;
9883132718Skan
9884132718Skan  REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9885132718Skan  real_to_target (val, &rv, GET_MODE (rtl));
9886132718Skan
9887132718Skan  /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9888132718Skan  for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9889132718Skan    {
9890132718Skan      insert_int (val[i], 4, array);
9891132718Skan      array += 4;
9892132718Skan    }
9893132718Skan}
9894132718Skan
9895132718Skan/* Attach a DW_AT_const_value attribute for a variable or a parameter which
989650397Sobrien   does not have a "location" either in memory or in a register.  These
989750397Sobrien   things can arise in GNU C when a constant is passed as an actual parameter
989850397Sobrien   to an inlined function.  They can also arise in C++ where declared
989950397Sobrien   constants do not necessarily get memory "homes".  */
990050397Sobrien
990150397Sobrienstatic void
9902132718Skanadd_const_value_attribute (dw_die_ref die, rtx rtl)
990350397Sobrien{
990450397Sobrien  switch (GET_CODE (rtl))
990550397Sobrien    {
990650397Sobrien    case CONST_INT:
990790075Sobrien      {
990890075Sobrien	HOST_WIDE_INT val = INTVAL (rtl);
9909117395Skan
9910132718Skan	if (val < 0)
9911132718Skan	  add_AT_int (die, DW_AT_const_value, val);
9912169689Skan	else
9913132718Skan	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
991490075Sobrien      }
991550397Sobrien      break;
991650397Sobrien
991750397Sobrien    case CONST_DOUBLE:
991850397Sobrien      /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9919132718Skan	 floating-point constant.  A CONST_DOUBLE is used whenever the
9920132718Skan	 constant requires more than one word in order to be adequately
9921132718Skan	 represented.  We output CONST_DOUBLEs as blocks.  */
992250397Sobrien      {
992390075Sobrien	enum machine_mode mode = GET_MODE (rtl);
992450397Sobrien
9925169689Skan	if (SCALAR_FLOAT_MODE_P (mode))
992650397Sobrien	  {
9927132718Skan	    unsigned int length = GET_MODE_SIZE (mode);
9928132718Skan	    unsigned char *array = ggc_alloc (length);
992950397Sobrien
9930132718Skan	    insert_float (rtl, array);
9931132718Skan	    add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
993250397Sobrien	  }
993350397Sobrien	else
993490075Sobrien	  {
993590075Sobrien	    /* ??? We really should be using HOST_WIDE_INT throughout.  */
9936169689Skan	    gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
993790075Sobrien
993890075Sobrien	    add_AT_long_long (die, DW_AT_const_value,
993990075Sobrien			      CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
994090075Sobrien	  }
994150397Sobrien      }
994250397Sobrien      break;
994350397Sobrien
9944132718Skan    case CONST_VECTOR:
9945132718Skan      {
9946132718Skan	enum machine_mode mode = GET_MODE (rtl);
9947132718Skan	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9948132718Skan	unsigned int length = CONST_VECTOR_NUNITS (rtl);
9949132718Skan	unsigned char *array = ggc_alloc (length * elt_size);
9950132718Skan	unsigned int i;
9951132718Skan	unsigned char *p;
9952132718Skan
9953169689Skan	switch (GET_MODE_CLASS (mode))
9954132718Skan	  {
9955169689Skan	  case MODE_VECTOR_INT:
9956132718Skan	    for (i = 0, p = array; i < length; i++, p += elt_size)
9957132718Skan	      {
9958132718Skan		rtx elt = CONST_VECTOR_ELT (rtl, i);
9959132718Skan		HOST_WIDE_INT lo, hi;
9960169689Skan
9961169689Skan		switch (GET_CODE (elt))
9962132718Skan		  {
9963169689Skan		  case CONST_INT:
9964132718Skan		    lo = INTVAL (elt);
9965132718Skan		    hi = -(lo < 0);
9966169689Skan		    break;
9967169689Skan
9968169689Skan		  case CONST_DOUBLE:
9969132718Skan		    lo = CONST_DOUBLE_LOW (elt);
9970132718Skan		    hi = CONST_DOUBLE_HIGH (elt);
9971169689Skan		    break;
9972169689Skan
9973169689Skan		  default:
9974169689Skan		    gcc_unreachable ();
9975132718Skan		  }
9976132718Skan
9977132718Skan		if (elt_size <= sizeof (HOST_WIDE_INT))
9978132718Skan		  insert_int (lo, elt_size, p);
9979169689Skan		else
9980132718Skan		  {
9981132718Skan		    unsigned char *p0 = p;
9982132718Skan		    unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9983132718Skan
9984169689Skan		    gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9985132718Skan		    if (WORDS_BIG_ENDIAN)
9986132718Skan		      {
9987132718Skan			p0 = p1;
9988132718Skan			p1 = p;
9989132718Skan		      }
9990132718Skan		    insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9991132718Skan		    insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9992132718Skan		  }
9993132718Skan	      }
9994169689Skan	    break;
9995169689Skan
9996169689Skan	  case MODE_VECTOR_FLOAT:
9997132718Skan	    for (i = 0, p = array; i < length; i++, p += elt_size)
9998132718Skan	      {
9999132718Skan		rtx elt = CONST_VECTOR_ELT (rtl, i);
10000132718Skan		insert_float (elt, p);
10001132718Skan	      }
10002169689Skan	    break;
10003169689Skan
10004169689Skan	  default:
10005169689Skan	    gcc_unreachable ();
10006132718Skan	  }
10007132718Skan
10008132718Skan	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10009132718Skan      }
10010132718Skan      break;
10011132718Skan
1001250397Sobrien    case CONST_STRING:
1001350397Sobrien      add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
1001450397Sobrien      break;
1001550397Sobrien
1001650397Sobrien    case SYMBOL_REF:
1001750397Sobrien    case LABEL_REF:
1001850397Sobrien    case CONST:
1001990075Sobrien      add_AT_addr (die, DW_AT_const_value, rtl);
10020169689Skan      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
1002150397Sobrien      break;
1002250397Sobrien
1002350397Sobrien    case PLUS:
1002450397Sobrien      /* In cases where an inlined instance of an inline function is passed
10025132718Skan	 the address of an `auto' variable (which is local to the caller) we
10026132718Skan	 can get a situation where the DECL_RTL of the artificial local
10027132718Skan	 variable (for the inlining) which acts as a stand-in for the
10028132718Skan	 corresponding formal parameter (of the inline function) will look
10029132718Skan	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10030132718Skan	 exactly a compile-time constant expression, but it isn't the address
10031132718Skan	 of the (artificial) local variable either.  Rather, it represents the
10032132718Skan	 *value* which the artificial local variable always has during its
10033132718Skan	 lifetime.  We currently have no way to represent such quasi-constant
10034132718Skan	 values in Dwarf, so for now we just punt and generate nothing.  */
1003550397Sobrien      break;
1003650397Sobrien
1003750397Sobrien    default:
1003850397Sobrien      /* No other kinds of rtx should be possible here.  */
10039169689Skan      gcc_unreachable ();
1004050397Sobrien    }
1004150397Sobrien
1004250397Sobrien}
1004350397Sobrien
10044169689Skan/* Determine whether the evaluation of EXPR references any variables
10045169689Skan   or functions which aren't otherwise used (and therefore may not be
10046169689Skan   output).  */
10047169689Skanstatic tree
10048169689Skanreference_to_unused (tree * tp, int * walk_subtrees,
10049169689Skan		     void * data ATTRIBUTE_UNUSED)
10050169689Skan{
10051169689Skan  if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10052169689Skan    *walk_subtrees = 0;
10053169689Skan
10054169689Skan  if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10055169689Skan      && ! TREE_ASM_WRITTEN (*tp))
10056169689Skan    return *tp;
10057169689Skan  else if (!flag_unit_at_a_time)
10058169689Skan    return NULL_TREE;
10059169689Skan  else if (!cgraph_global_info_ready
10060169689Skan	   && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10061195815Sbms    return *tp;
10062169689Skan  else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10063169689Skan    {
10064169689Skan      struct cgraph_varpool_node *node = cgraph_varpool_node (*tp);
10065169689Skan      if (!node->needed)
10066169689Skan	return *tp;
10067169689Skan    }
10068169689Skan   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10069169689Skan	    && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10070169689Skan    {
10071169689Skan      struct cgraph_node *node = cgraph_node (*tp);
10072169689Skan      if (!node->output)
10073169689Skan        return *tp;
10074169689Skan    }
10075169689Skan
10076169689Skan  return NULL_TREE;
10077169689Skan}
10078169689Skan
10079169689Skan/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10080169689Skan   for use in a later add_const_value_attribute call.  */
10081169689Skan
1008290075Sobrienstatic rtx
10083169689Skanrtl_for_decl_init (tree init, tree type)
10084169689Skan{
10085169689Skan  rtx rtl = NULL_RTX;
10086169689Skan
10087169689Skan  /* If a variable is initialized with a string constant without embedded
10088169689Skan     zeros, build CONST_STRING.  */
10089169689Skan  if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10090169689Skan    {
10091169689Skan      tree enttype = TREE_TYPE (type);
10092169689Skan      tree domain = TYPE_DOMAIN (type);
10093169689Skan      enum machine_mode mode = TYPE_MODE (enttype);
10094169689Skan
10095169689Skan      if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10096169689Skan	  && domain
10097169689Skan	  && integer_zerop (TYPE_MIN_VALUE (domain))
10098169689Skan	  && compare_tree_int (TYPE_MAX_VALUE (domain),
10099169689Skan			       TREE_STRING_LENGTH (init) - 1) == 0
10100169689Skan	  && ((size_t) TREE_STRING_LENGTH (init)
10101169689Skan	      == strlen (TREE_STRING_POINTER (init)) + 1))
10102169689Skan	rtl = gen_rtx_CONST_STRING (VOIDmode,
10103169689Skan				    ggc_strdup (TREE_STRING_POINTER (init)));
10104169689Skan    }
10105169689Skan  /* Other aggregates, and complex values, could be represented using
10106169689Skan     CONCAT: FIXME!  */
10107169689Skan  else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10108169689Skan    ;
10109169689Skan  /* Vectors only work if their mode is supported by the target.
10110169689Skan     FIXME: generic vectors ought to work too.  */
10111169689Skan  else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10112169689Skan    ;
10113169689Skan  /* If the initializer is something that we know will expand into an
10114169689Skan     immediate RTL constant, expand it now.  We must be careful not to
10115169689Skan     reference variables which won't be output.  */
10116169689Skan  else if (initializer_constant_valid_p (init, type)
10117169689Skan	   && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10118169689Skan    {
10119220150Smm      /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10120220150Smm	 possible.  */
10121220150Smm      if (TREE_CODE (type) == VECTOR_TYPE)
10122220150Smm	switch (TREE_CODE (init))
10123220150Smm	  {
10124220150Smm	  case VECTOR_CST:
10125220150Smm	    break;
10126220150Smm	  case CONSTRUCTOR:
10127220150Smm	    if (TREE_CONSTANT (init))
10128220150Smm	      {
10129220150Smm		VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10130220150Smm		bool constant_p = true;
10131220150Smm		tree value;
10132220150Smm		unsigned HOST_WIDE_INT ix;
10133220150Smm
10134220150Smm		/* Even when ctor is constant, it might contain non-*_CST
10135220150Smm		   elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10136220150Smm		   belong into VECTOR_CST nodes.  */
10137220150Smm		FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10138220150Smm		  if (!CONSTANT_CLASS_P (value))
10139220150Smm		    {
10140220150Smm		      constant_p = false;
10141220150Smm		      break;
10142220150Smm		    }
10143220150Smm
10144220150Smm		if (constant_p)
10145220150Smm		  {
10146220150Smm		    init = build_vector_from_ctor (type, elts);
10147220150Smm		    break;
10148220150Smm		  }
10149220150Smm	      }
10150220150Smm	    /* FALLTHRU */
10151220150Smm
10152220150Smm	  default:
10153220150Smm	    return NULL;
10154220150Smm	  }
10155220150Smm
10156169689Skan      rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10157169689Skan
10158169689Skan      /* If expand_expr returns a MEM, it wasn't immediate.  */
10159169689Skan      gcc_assert (!rtl || !MEM_P (rtl));
10160169689Skan    }
10161169689Skan
10162169689Skan  return rtl;
10163169689Skan}
10164169689Skan
10165169689Skan/* Generate RTL for the variable DECL to represent its location.  */
10166169689Skan
10167169689Skanstatic rtx
10168132718Skanrtl_for_decl_location (tree decl)
1016950397Sobrien{
1017090075Sobrien  rtx rtl;
1017150397Sobrien
1017250397Sobrien  /* Here we have to decide where we are going to say the parameter "lives"
1017350397Sobrien     (as far as the debugger is concerned).  We only have a couple of
1017450397Sobrien     choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
1017550397Sobrien
1017690075Sobrien     DECL_RTL normally indicates where the parameter lives during most of the
1017750397Sobrien     activation of the function.  If optimization is enabled however, this
1017890075Sobrien     could be either NULL or else a pseudo-reg.  Both of those cases indicate
1017950397Sobrien     that the parameter doesn't really live anywhere (as far as the code
1018050397Sobrien     generation parts of GCC are concerned) during most of the function's
1018150397Sobrien     activation.  That will happen (for example) if the parameter is never
1018250397Sobrien     referenced within the function.
1018350397Sobrien
1018450397Sobrien     We could just generate a location descriptor here for all non-NULL
1018550397Sobrien     non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
1018650397Sobrien     a little nicer than that if we also consider DECL_INCOMING_RTL in cases
1018750397Sobrien     where DECL_RTL is NULL or is a pseudo-reg.
1018850397Sobrien
1018950397Sobrien     Note however that we can only get away with using DECL_INCOMING_RTL as
1019050397Sobrien     a backup substitute for DECL_RTL in certain limited cases.  In cases
1019150397Sobrien     where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
1019250397Sobrien     we can be sure that the parameter was passed using the same type as it is
1019350397Sobrien     declared to have within the function, and that its DECL_INCOMING_RTL
1019450397Sobrien     points us to a place where a value of that type is passed.
1019550397Sobrien
1019650397Sobrien     In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
1019750397Sobrien     we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
1019850397Sobrien     because in these cases DECL_INCOMING_RTL points us to a value of some
1019950397Sobrien     type which is *different* from the type of the parameter itself.  Thus,
1020050397Sobrien     if we tried to use DECL_INCOMING_RTL to generate a location attribute in
1020150397Sobrien     such cases, the debugger would end up (for example) trying to fetch a
1020250397Sobrien     `float' from a place which actually contains the first part of a
1020350397Sobrien     `double'.  That would lead to really incorrect and confusing
1020450397Sobrien     output at debug-time.
1020550397Sobrien
1020650397Sobrien     So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
1020750397Sobrien     in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
1020850397Sobrien     are a couple of exceptions however.  On little-endian machines we can
1020950397Sobrien     get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
1021050397Sobrien     not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
1021150397Sobrien     an integral type that is smaller than TREE_TYPE (decl). These cases arise
1021250397Sobrien     when (on a little-endian machine) a non-prototyped function has a
1021350397Sobrien     parameter declared to be of type `short' or `char'.  In such cases,
1021450397Sobrien     TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
1021550397Sobrien     be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
1021650397Sobrien     passed `int' value.  If the debugger then uses that address to fetch
1021750397Sobrien     a `short' or a `char' (on a little-endian machine) the result will be
1021850397Sobrien     the correct data, so we allow for such exceptional cases below.
1021950397Sobrien
1022050397Sobrien     Note that our goal here is to describe the place where the given formal
1022190075Sobrien     parameter lives during most of the function's activation (i.e. between the
1022290075Sobrien     end of the prologue and the start of the epilogue).  We'll do that as best
1022390075Sobrien     as we can. Note however that if the given formal parameter is modified
1022490075Sobrien     sometime during the execution of the function, then a stack backtrace (at
1022590075Sobrien     debug-time) will show the function as having been called with the *new*
1022690075Sobrien     value rather than the value which was originally passed in.  This happens
1022790075Sobrien     rarely enough that it is not a major problem, but it *is* a problem, and
1022890075Sobrien     I'd like to fix it.
1022950397Sobrien
1023090075Sobrien     A future version of dwarf2out.c may generate two additional attributes for
1023190075Sobrien     any given DW_TAG_formal_parameter DIE which will describe the "passed
1023290075Sobrien     type" and the "passed location" for the given formal parameter in addition
1023390075Sobrien     to the attributes we now generate to indicate the "declared type" and the
1023490075Sobrien     "active location" for each parameter.  This additional set of attributes
1023590075Sobrien     could be used by debuggers for stack backtraces. Separately, note that
1023690075Sobrien     sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
1023790075Sobrien     This happens (for example) for inlined-instances of inline function formal
1023890075Sobrien     parameters which are never referenced.  This really shouldn't be
1023990075Sobrien     happening.  All PARM_DECL nodes should get valid non-NULL
10240169689Skan     DECL_INCOMING_RTL values.  FIXME.  */
1024150397Sobrien
1024250397Sobrien  /* Use DECL_RTL as the "location" unless we find something better.  */
1024390075Sobrien  rtl = DECL_RTL_IF_SET (decl);
1024450397Sobrien
1024590075Sobrien  /* When generating abstract instances, ignore everything except
10246117395Skan     constants, symbols living in memory, and symbols living in
10247117395Skan     fixed registers.  */
1024890075Sobrien  if (! reload_completed)
1024950397Sobrien    {
1025090075Sobrien      if (rtl
1025190075Sobrien	  && (CONSTANT_P (rtl)
10252169689Skan	      || (MEM_P (rtl)
10253117395Skan	          && CONSTANT_P (XEXP (rtl, 0)))
10254169689Skan	      || (REG_P (rtl)
10255117395Skan	          && TREE_CODE (decl) == VAR_DECL
10256117395Skan		  && TREE_STATIC (decl))))
1025796263Sobrien	{
10258169689Skan	  rtl = targetm.delegitimize_address (rtl);
1025996263Sobrien	  return rtl;
1026096263Sobrien	}
1026190075Sobrien      rtl = NULL_RTX;
1026290075Sobrien    }
1026390075Sobrien  else if (TREE_CODE (decl) == PARM_DECL)
1026490075Sobrien    {
1026550397Sobrien      if (rtl == NULL_RTX || is_pseudo_reg (rtl))
1026650397Sobrien	{
10267146895Skan	  tree declared_type = TREE_TYPE (decl);
10268146895Skan	  tree passed_type = DECL_ARG_TYPE (decl);
10269146895Skan	  enum machine_mode dmode = TYPE_MODE (declared_type);
10270146895Skan	  enum machine_mode pmode = TYPE_MODE (passed_type);
1027150397Sobrien
1027250397Sobrien	  /* This decl represents a formal parameter which was optimized out.
1027350397Sobrien	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
1027490075Sobrien	     all cases where (rtl == NULL_RTX) just below.  */
10275146895Skan	  if (dmode == pmode)
1027650397Sobrien	    rtl = DECL_INCOMING_RTL (decl);
10277146895Skan	  else if (SCALAR_INT_MODE_P (dmode)
10278146895Skan		   && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10279146895Skan		   && DECL_INCOMING_RTL (decl))
10280146895Skan	    {
10281146895Skan	      rtx inc = DECL_INCOMING_RTL (decl);
10282146895Skan	      if (REG_P (inc))
10283146895Skan		rtl = inc;
10284169689Skan	      else if (MEM_P (inc))
10285146895Skan		{
10286146895Skan		  if (BYTES_BIG_ENDIAN)
10287146895Skan		    rtl = adjust_address_nv (inc, dmode,
10288146895Skan					     GET_MODE_SIZE (pmode)
10289146895Skan					     - GET_MODE_SIZE (dmode));
10290146895Skan		  else
10291146895Skan		    rtl = inc;
10292146895Skan		}
10293146895Skan	    }
1029450397Sobrien	}
1029552284Sobrien
1029652284Sobrien      /* If the parm was passed in registers, but lives on the stack, then
1029752284Sobrien	 make a big endian correction if the mode of the type of the
1029852284Sobrien	 parameter is not the same as the mode of the rtl.  */
1029952284Sobrien      /* ??? This is the same series of checks that are made in dbxout.c before
1030052284Sobrien	 we reach the big endian correction code there.  It isn't clear if all
1030152284Sobrien	 of these checks are necessary here, but keeping them all is the safe
1030252284Sobrien	 thing to do.  */
10303169689Skan      else if (MEM_P (rtl)
1030452284Sobrien	       && XEXP (rtl, 0) != const0_rtx
1030552284Sobrien	       && ! CONSTANT_P (XEXP (rtl, 0))
1030652284Sobrien	       /* Not passed in memory.  */
10307169689Skan	       && !MEM_P (DECL_INCOMING_RTL (decl))
1030852284Sobrien	       /* Not passed by invisible reference.  */
10309169689Skan	       && (!REG_P (XEXP (rtl, 0))
1031052284Sobrien		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
1031152284Sobrien		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
1031252284Sobrien#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1031352284Sobrien		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
1031452284Sobrien#endif
1031552284Sobrien		     )
1031652284Sobrien	       /* Big endian correction check.  */
1031752284Sobrien	       && BYTES_BIG_ENDIAN
1031852284Sobrien	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
1031952284Sobrien	       && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
1032052284Sobrien		   < UNITS_PER_WORD))
1032152284Sobrien	{
1032252284Sobrien	  int offset = (UNITS_PER_WORD
1032352284Sobrien			- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
1032490075Sobrien
1032552284Sobrien	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
1032652284Sobrien			     plus_constant (XEXP (rtl, 0), offset));
1032752284Sobrien	}
1032850397Sobrien    }
10329169689Skan  else if (TREE_CODE (decl) == VAR_DECL
10330169689Skan	   && rtl
10331169689Skan	   && MEM_P (rtl)
10332169689Skan	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10333169689Skan	   && BYTES_BIG_ENDIAN)
10334169689Skan    {
10335169689Skan      int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10336169689Skan      int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
1033750397Sobrien
10338169689Skan      /* If a variable is declared "register" yet is smaller than
10339169689Skan	 a register, then if we store the variable to memory, it
10340169689Skan	 looks like we're storing a register-sized value, when in
10341169689Skan	 fact we are not.  We need to adjust the offset of the
10342169689Skan	 storage location to reflect the actual value's bytes,
10343169689Skan	 else gdb will not be able to display it.  */
10344169689Skan      if (rsize > dsize)
10345169689Skan	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10346169689Skan			   plus_constant (XEXP (rtl, 0), rsize-dsize));
1034790075Sobrien    }
1034890075Sobrien
1034990075Sobrien  /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
1035090075Sobrien     and will have been substituted directly into all expressions that use it.
1035190075Sobrien     C does not have such a concept, but C++ and other languages do.  */
10352169689Skan  if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10353169689Skan    rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
1035490075Sobrien
1035596263Sobrien  if (rtl)
10356169689Skan    rtl = targetm.delegitimize_address (rtl);
10357117395Skan
10358117395Skan  /* If we don't look past the constant pool, we risk emitting a
10359117395Skan     reference to a constant pool entry that isn't referenced from
10360117395Skan     code, and thus is not emitted.  */
10361117395Skan  if (rtl)
10362117395Skan    rtl = avoid_constant_pool_reference (rtl);
10363117395Skan
1036490075Sobrien  return rtl;
1036590075Sobrien}
1036690075Sobrien
10367169689Skan/* We need to figure out what section we should use as the base for the
10368169689Skan   address ranges where a given location is valid.
10369169689Skan   1. If this particular DECL has a section associated with it, use that.
10370169689Skan   2. If this function has a section associated with it, use that.
10371169689Skan   3. Otherwise, use the text section.
10372169689Skan   XXX: If you split a variable across multiple sections, we won't notice.  */
10373169689Skan
10374169689Skanstatic const char *
10375169689Skansecname_for_decl (tree decl)
10376169689Skan{
10377169689Skan  const char *secname;
10378169689Skan
10379169689Skan  if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10380169689Skan    {
10381169689Skan      tree sectree = DECL_SECTION_NAME (decl);
10382169689Skan      secname = TREE_STRING_POINTER (sectree);
10383169689Skan    }
10384169689Skan  else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10385169689Skan    {
10386169689Skan      tree sectree = DECL_SECTION_NAME (current_function_decl);
10387169689Skan      secname = TREE_STRING_POINTER (sectree);
10388169689Skan    }
10389169689Skan  else if (cfun && in_cold_section_p)
10390169689Skan    secname = cfun->cold_section_label;
10391169689Skan  else
10392169689Skan    secname = text_section_label;
10393169689Skan
10394169689Skan  return secname;
10395169689Skan}
10396169689Skan
10397132718Skan/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
1039890075Sobrien   data attribute for a variable or a parameter.  We generate the
1039990075Sobrien   DW_AT_const_value attribute only in those cases where the given variable
1040090075Sobrien   or parameter does not have a true "location" either in memory or in a
1040190075Sobrien   register.  This can happen (for example) when a constant is passed as an
1040290075Sobrien   actual argument in a call to an inline function.  (It's possible that
1040390075Sobrien   these things can crop up in other ways also.)  Note that one type of
1040490075Sobrien   constant value which can be passed into an inlined function is a constant
1040590075Sobrien   pointer.  This can happen for example if an actual argument in an inlined
1040690075Sobrien   function call evaluates to a compile-time constant address.  */
1040790075Sobrien
1040890075Sobrienstatic void
10409169689Skanadd_location_or_const_value_attribute (dw_die_ref die, tree decl,
10410169689Skan				       enum dwarf_attribute attr)
1041190075Sobrien{
1041290075Sobrien  rtx rtl;
10413117395Skan  dw_loc_descr_ref descr;
10414169689Skan  var_loc_list *loc_list;
10415169689Skan  struct var_loc_node *node;
1041690075Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
1041790075Sobrien    return;
1041890075Sobrien
10419169689Skan  gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10420169689Skan	      || TREE_CODE (decl) == RESULT_DECL);
10421169689Skan
10422169689Skan  /* See if we possibly have multiple locations for this variable.  */
10423169689Skan  loc_list = lookup_decl_loc (decl);
1042450397Sobrien
10425169689Skan  /* If it truly has multiple locations, the first and last node will
10426169689Skan     differ.  */
10427169689Skan  if (loc_list && loc_list->first != loc_list->last)
1042850397Sobrien    {
10429169689Skan      const char *endname, *secname;
10430169689Skan      dw_loc_list_ref list;
10431169689Skan      rtx varloc;
1043250397Sobrien
10433169689Skan      /* Now that we know what section we are using for a base,
10434169689Skan         actually construct the list of locations.
10435169689Skan	 The first location information is what is passed to the
10436169689Skan	 function that creates the location list, and the remaining
10437169689Skan	 locations just get added on to that list.
10438169689Skan	 Note that we only know the start address for a location
10439169689Skan	 (IE location changes), so to build the range, we use
10440169689Skan	 the range [current location start, next location start].
10441169689Skan	 This means we have to special case the last node, and generate
10442169689Skan	 a range of [last location start, end of function label].  */
1044350397Sobrien
10444169689Skan      node = loc_list->first;
10445169689Skan      varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10446169689Skan      secname = secname_for_decl (decl);
10447132718Skan
10448169689Skan      list = new_loc_list (loc_descriptor (varloc),
10449169689Skan			   node->label, node->next->label, secname, 1);
10450169689Skan      node = node->next;
10451132718Skan
10452169689Skan      for (; node->next; node = node->next)
10453169689Skan	if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10454132718Skan	  {
10455169689Skan	    /* The variable has a location between NODE->LABEL and
10456169689Skan	       NODE->NEXT->LABEL.  */
10457169689Skan	    varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10458169689Skan	    add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10459169689Skan				       node->label, node->next->label, secname);
10460132718Skan	  }
10461132718Skan
10462169689Skan      /* If the variable has a location at the last label
10463169689Skan	 it keeps its location until the end of function.  */
10464169689Skan      if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10465169689Skan	{
10466169689Skan	  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10467169689Skan
10468169689Skan	  varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10469169689Skan	  if (!current_function_decl)
10470169689Skan	    endname = text_end_label;
10471169689Skan	  else
10472169689Skan	    {
10473169689Skan	      ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10474169689Skan					   current_function_funcdef_no);
10475169689Skan	      endname = ggc_strdup (label_id);
10476169689Skan	    }
10477169689Skan	  add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10478169689Skan				     node->label, endname, secname);
10479169689Skan	}
10480169689Skan
10481169689Skan      /* Finally, add the location list to the DIE, and we are done.  */
10482169689Skan      add_AT_loc_list (die, attr, list);
10483169689Skan      return;
1048450397Sobrien    }
10485169689Skan
10486169689Skan  /* Try to get some constant RTL for this decl, and use that as the value of
10487169689Skan     the location.  */
10488169689Skan
10489169689Skan  rtl = rtl_for_decl_location (decl);
10490169689Skan  if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10491169689Skan    {
10492169689Skan      add_const_value_attribute (die, rtl);
10493169689Skan      return;
10494169689Skan    }
10495169689Skan
10496169689Skan  /* If we have tried to generate the location otherwise, and it
10497169689Skan     didn't work out (we wouldn't be here if we did), and we have a one entry
10498169689Skan     location list, try generating a location from that.  */
10499169689Skan  if (loc_list && loc_list->first)
10500169689Skan    {
10501169689Skan      node = loc_list->first;
10502169689Skan      descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10503169689Skan      if (descr)
10504169689Skan	{
10505169689Skan	  add_AT_location_description (die, attr, descr);
10506169689Skan	  return;
10507169689Skan	}
10508169689Skan    }
10509169689Skan
10510169689Skan  /* We couldn't get any rtl, so try directly generating the location
10511169689Skan     description from the tree.  */
10512169689Skan  descr = loc_descriptor_from_tree (decl);
10513169689Skan  if (descr)
10514169689Skan    {
10515169689Skan      add_AT_location_description (die, attr, descr);
10516169689Skan      return;
10517169689Skan    }
10518169689Skan  /* None of that worked, so it must not really have a location;
10519169689Skan     try adding a constant value attribute from the DECL_INITIAL.  */
10520169689Skan  tree_add_const_value_attribute (die, decl);
1052150397Sobrien}
1052250397Sobrien
1052390075Sobrien/* If we don't have a copy of this variable in memory for some reason (such
1052490075Sobrien   as a C++ member constant that doesn't have an out-of-line definition),
1052590075Sobrien   we should tell the debugger about the constant value.  */
1052690075Sobrien
1052790075Sobrienstatic void
10528132718Skantree_add_const_value_attribute (dw_die_ref var_die, tree decl)
1052990075Sobrien{
1053090075Sobrien  tree init = DECL_INITIAL (decl);
1053190075Sobrien  tree type = TREE_TYPE (decl);
10532169689Skan  rtx rtl;
1053390075Sobrien
10534169689Skan  if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
1053590075Sobrien    /* OK */;
1053690075Sobrien  else
1053790075Sobrien    return;
1053890075Sobrien
10539169689Skan  rtl = rtl_for_decl_init (init, type);
10540169689Skan  if (rtl)
10541169689Skan    add_const_value_attribute (var_die, rtl);
10542169689Skan}
10543169689Skan
10544169689Skan/* Convert the CFI instructions for the current function into a
10545169689Skan   location list.  This is used for DW_AT_frame_base when we targeting
10546169689Skan   a dwarf2 consumer that does not support the dwarf3
10547169689Skan   DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10548169689Skan   expressions.  */
10549169689Skan
10550169689Skanstatic dw_loc_list_ref
10551169689Skanconvert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10552169689Skan{
10553169689Skan  dw_fde_ref fde;
10554169689Skan  dw_loc_list_ref list, *list_tail;
10555169689Skan  dw_cfi_ref cfi;
10556169689Skan  dw_cfa_location last_cfa, next_cfa;
10557169689Skan  const char *start_label, *last_label, *section;
10558169689Skan
10559169689Skan  fde = &fde_table[fde_table_in_use - 1];
10560169689Skan
10561169689Skan  section = secname_for_decl (current_function_decl);
10562169689Skan  list_tail = &list;
10563169689Skan  list = NULL;
10564169689Skan
10565169689Skan  next_cfa.reg = INVALID_REGNUM;
10566169689Skan  next_cfa.offset = 0;
10567169689Skan  next_cfa.indirect = 0;
10568169689Skan  next_cfa.base_offset = 0;
10569169689Skan
10570169689Skan  start_label = fde->dw_fde_begin;
10571169689Skan
10572169689Skan  /* ??? Bald assumption that the CIE opcode list does not contain
10573169689Skan     advance opcodes.  */
10574169689Skan  for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10575169689Skan    lookup_cfa_1 (cfi, &next_cfa);
10576169689Skan
10577169689Skan  last_cfa = next_cfa;
10578169689Skan  last_label = start_label;
10579169689Skan
10580169689Skan  for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10581169689Skan    switch (cfi->dw_cfi_opc)
10582169689Skan      {
10583169689Skan      case DW_CFA_set_loc:
10584169689Skan      case DW_CFA_advance_loc1:
10585169689Skan      case DW_CFA_advance_loc2:
10586169689Skan      case DW_CFA_advance_loc4:
10587169689Skan	if (!cfa_equal_p (&last_cfa, &next_cfa))
10588169689Skan	  {
10589169689Skan	    *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10590169689Skan				       start_label, last_label, section,
10591169689Skan				       list == NULL);
10592169689Skan
10593169689Skan	    list_tail = &(*list_tail)->dw_loc_next;
10594169689Skan	    last_cfa = next_cfa;
10595169689Skan	    start_label = last_label;
10596169689Skan	  }
10597169689Skan	last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10598169689Skan	break;
10599169689Skan
10600169689Skan      case DW_CFA_advance_loc:
10601169689Skan	/* The encoding is complex enough that we should never emit this.  */
10602169689Skan      case DW_CFA_remember_state:
10603169689Skan      case DW_CFA_restore_state:
10604169689Skan	/* We don't handle these two in this function.  It would be possible
10605169689Skan	   if it were to be required.  */
10606169689Skan	gcc_unreachable ();
10607169689Skan
10608169689Skan      default:
10609169689Skan	lookup_cfa_1 (cfi, &next_cfa);
10610169689Skan	break;
10611169689Skan      }
10612169689Skan
10613169689Skan  if (!cfa_equal_p (&last_cfa, &next_cfa))
1061490075Sobrien    {
10615169689Skan      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10616169689Skan				 start_label, last_label, section,
10617169689Skan				 list == NULL);
10618169689Skan      list_tail = &(*list_tail)->dw_loc_next;
10619169689Skan      start_label = last_label;
10620169689Skan    }
10621169689Skan  *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10622169689Skan			     start_label, fde->dw_fde_end, section,
10623169689Skan			     list == NULL);
1062490075Sobrien
10625169689Skan  return list;
10626169689Skan}
10627169689Skan
10628169689Skan/* Compute a displacement from the "steady-state frame pointer" to the
10629169689Skan   frame base (often the same as the CFA), and store it in
10630169689Skan   frame_pointer_fb_offset.  OFFSET is added to the displacement
10631169689Skan   before the latter is negated.  */
10632169689Skan
10633169689Skanstatic void
10634169689Skancompute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10635169689Skan{
10636169689Skan  rtx reg, elim;
10637169689Skan
10638169689Skan#ifdef FRAME_POINTER_CFA_OFFSET
10639169689Skan  reg = frame_pointer_rtx;
10640169689Skan  offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10641169689Skan#else
10642169689Skan  reg = arg_pointer_rtx;
10643169689Skan  offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10644169689Skan#endif
10645169689Skan
10646169689Skan  elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10647169689Skan  if (GET_CODE (elim) == PLUS)
10648169689Skan    {
10649169689Skan      offset += INTVAL (XEXP (elim, 1));
10650169689Skan      elim = XEXP (elim, 0);
1065190075Sobrien    }
10652169689Skan  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10653169689Skan		       : stack_pointer_rtx));
10654169689Skan
10655169689Skan  frame_pointer_fb_offset = -offset;
1065690075Sobrien}
1065790075Sobrien
10658132718Skan/* Generate a DW_AT_name attribute given some string value to be included as
1065950397Sobrien   the value of the attribute.  */
1066050397Sobrien
10661132718Skanstatic void
10662132718Skanadd_name_attribute (dw_die_ref die, const char *name_string)
1066350397Sobrien{
1066450397Sobrien  if (name_string != NULL && *name_string != 0)
1066590075Sobrien    {
1066690075Sobrien      if (demangle_name_func)
1066790075Sobrien	name_string = (*demangle_name_func) (name_string);
1066890075Sobrien
1066990075Sobrien      add_AT_string (die, DW_AT_name, name_string);
1067090075Sobrien    }
1067150397Sobrien}
1067250397Sobrien
10673132718Skan/* Generate a DW_AT_comp_dir attribute for DIE.  */
10674132718Skan
10675132718Skanstatic void
10676132718Skanadd_comp_dir_attribute (dw_die_ref die)
10677132718Skan{
10678132718Skan  const char *wd = get_src_pwd ();
10679132718Skan  if (wd != NULL)
10680132718Skan    add_AT_string (die, DW_AT_comp_dir, wd);
10681132718Skan}
10682132718Skan
1068350397Sobrien/* Given a tree node describing an array bound (either lower or upper) output
1068450397Sobrien   a representation for that bound.  */
1068550397Sobrien
1068650397Sobrienstatic void
10687132718Skanadd_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
1068850397Sobrien{
1068950397Sobrien  switch (TREE_CODE (bound))
1069050397Sobrien    {
1069150397Sobrien    case ERROR_MARK:
1069250397Sobrien      return;
1069350397Sobrien
1069490075Sobrien    /* All fixed-bounds are represented by INTEGER_CST nodes.  */
1069550397Sobrien    case INTEGER_CST:
1069690075Sobrien      if (! host_integerp (bound, 0)
1069790075Sobrien	  || (bound_attr == DW_AT_lower_bound
1069890075Sobrien	      && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
1069990075Sobrien		  || (is_fortran () && integer_onep (bound)))))
10700169689Skan	/* Use the default.  */
1070190075Sobrien	;
1070250397Sobrien      else
1070390075Sobrien	add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
1070450397Sobrien      break;
1070550397Sobrien
1070650397Sobrien    case CONVERT_EXPR:
1070750397Sobrien    case NOP_EXPR:
1070850397Sobrien    case NON_LVALUE_EXPR:
1070990075Sobrien    case VIEW_CONVERT_EXPR:
1071050397Sobrien      add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
1071150397Sobrien      break;
1071290075Sobrien
1071350397Sobrien    case SAVE_EXPR:
1071450397Sobrien      break;
1071550397Sobrien
1071650397Sobrien    case VAR_DECL:
1071790075Sobrien    case PARM_DECL:
10718169689Skan    case RESULT_DECL:
1071990075Sobrien      {
1072090075Sobrien	dw_die_ref decl_die = lookup_decl_die (bound);
1072150397Sobrien
1072290075Sobrien	/* ??? Can this happen, or should the variable have been bound
1072390075Sobrien	   first?  Probably it can, since I imagine that we try to create
1072490075Sobrien	   the types of parameters in the order in which they exist in
1072590075Sobrien	   the list, and won't have created a forward reference to a
1072690075Sobrien	   later parameter.  */
1072790075Sobrien	if (decl_die != NULL)
1072890075Sobrien	  add_AT_die_ref (subrange_die, bound_attr, decl_die);
1072990075Sobrien	break;
1073090075Sobrien      }
1073190075Sobrien
1073250397Sobrien    default:
1073390075Sobrien      {
1073490075Sobrien	/* Otherwise try to create a stack operation procedure to
1073590075Sobrien	   evaluate the value of the array bound.  */
1073690075Sobrien
1073790075Sobrien	dw_die_ref ctx, decl_die;
1073890075Sobrien	dw_loc_descr_ref loc;
1073990075Sobrien
10740169689Skan	loc = loc_descriptor_from_tree (bound);
1074190075Sobrien	if (loc == NULL)
1074290075Sobrien	  break;
1074390075Sobrien
1074490075Sobrien	if (current_function_decl == 0)
1074590075Sobrien	  ctx = comp_unit_die;
1074690075Sobrien	else
1074790075Sobrien	  ctx = lookup_decl_die (current_function_decl);
1074890075Sobrien
1074990075Sobrien	decl_die = new_die (DW_TAG_variable, ctx, bound);
1075090075Sobrien	add_AT_flag (decl_die, DW_AT_artificial, 1);
1075190075Sobrien	add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
1075290075Sobrien	add_AT_loc (decl_die, DW_AT_location, loc);
1075390075Sobrien
1075490075Sobrien	add_AT_die_ref (subrange_die, bound_attr, decl_die);
1075590075Sobrien	break;
1075690075Sobrien      }
1075750397Sobrien    }
1075850397Sobrien}
1075950397Sobrien
1076050397Sobrien/* Note that the block of subscript information for an array type also
1076150397Sobrien   includes information about the element type of type given array type.  */
1076250397Sobrien
1076350397Sobrienstatic void
10764132718Skanadd_subscript_info (dw_die_ref type_die, tree type)
1076550397Sobrien{
1076650397Sobrien#ifndef MIPS_DEBUGGING_INFO
1076790075Sobrien  unsigned dimension_number;
1076850397Sobrien#endif
1076990075Sobrien  tree lower, upper;
1077090075Sobrien  dw_die_ref subrange_die;
1077150397Sobrien
1077290075Sobrien  /* The GNU compilers represent multidimensional array types as sequences of
1077350397Sobrien     one dimensional array types whose element types are themselves array
1077450397Sobrien     types.  Here we squish that down, so that each multidimensional array
1077590075Sobrien     type gets only one array_type DIE in the Dwarf debugging info. The draft
1077650397Sobrien     Dwarf specification say that we are allowed to do this kind of
1077750397Sobrien     compression in C (because there is no difference between an array or
1077890075Sobrien     arrays and a multidimensional array in C) but for other source languages
1077950397Sobrien     (e.g. Ada) we probably shouldn't do this.  */
1078050397Sobrien
1078150397Sobrien  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
1078250397Sobrien     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
1078350397Sobrien     We work around this by disabling this feature.  See also
1078450397Sobrien     gen_array_type_die.  */
1078550397Sobrien#ifndef MIPS_DEBUGGING_INFO
1078650397Sobrien  for (dimension_number = 0;
1078750397Sobrien       TREE_CODE (type) == ARRAY_TYPE;
1078850397Sobrien       type = TREE_TYPE (type), dimension_number++)
1078990075Sobrien#endif
1079050397Sobrien    {
1079190075Sobrien      tree domain = TYPE_DOMAIN (type);
1079250397Sobrien
1079350397Sobrien      /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
1079490075Sobrien	 and (in GNU C only) variable bounds.  Handle all three forms
10795132718Skan	 here.  */
1079690075Sobrien      subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
1079750397Sobrien      if (domain)
1079850397Sobrien	{
1079950397Sobrien	  /* We have an array type with specified bounds.  */
1080050397Sobrien	  lower = TYPE_MIN_VALUE (domain);
1080150397Sobrien	  upper = TYPE_MAX_VALUE (domain);
1080250397Sobrien
10803132718Skan	  /* Define the index type.  */
1080450397Sobrien	  if (TREE_TYPE (domain))
1080550397Sobrien	    {
1080650397Sobrien	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
1080750397Sobrien		 TREE_TYPE field.  We can't emit debug info for this
1080850397Sobrien		 because it is an unnamed integral type.  */
1080950397Sobrien	      if (TREE_CODE (domain) == INTEGER_TYPE
1081050397Sobrien		  && TYPE_NAME (domain) == NULL_TREE
1081150397Sobrien		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
1081250397Sobrien		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
1081390075Sobrien		;
1081450397Sobrien	      else
1081550397Sobrien		add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
1081650397Sobrien				    type_die);
1081750397Sobrien	    }
1081850397Sobrien
1081950397Sobrien	  /* ??? If upper is NULL, the array has unspecified length,
1082050397Sobrien	     but it does have a lower bound.  This happens with Fortran
1082150397Sobrien	       dimension arr(N:*)
10822132718Skan	     Since the debugger is definitely going to need to know N
1082350397Sobrien	     to produce useful results, go ahead and output the lower
1082450397Sobrien	     bound solo, and hope the debugger can cope.  */
1082550397Sobrien
1082650397Sobrien	  add_bound_info (subrange_die, DW_AT_lower_bound, lower);
1082750397Sobrien	  if (upper)
1082850397Sobrien	    add_bound_info (subrange_die, DW_AT_upper_bound, upper);
1082950397Sobrien	}
1083050397Sobrien
1083190075Sobrien      /* Otherwise we have an array type with an unspecified length.  The
1083290075Sobrien	 DWARF-2 spec does not say how to handle this; let's just leave out the
1083390075Sobrien	 bounds.  */
1083450397Sobrien    }
1083550397Sobrien}
1083650397Sobrien
1083750397Sobrienstatic void
10838132718Skanadd_byte_size_attribute (dw_die_ref die, tree tree_node)
1083950397Sobrien{
1084090075Sobrien  unsigned size;
1084150397Sobrien
1084250397Sobrien  switch (TREE_CODE (tree_node))
1084350397Sobrien    {
1084450397Sobrien    case ERROR_MARK:
1084550397Sobrien      size = 0;
1084650397Sobrien      break;
1084750397Sobrien    case ENUMERAL_TYPE:
1084850397Sobrien    case RECORD_TYPE:
1084950397Sobrien    case UNION_TYPE:
1085050397Sobrien    case QUAL_UNION_TYPE:
1085150397Sobrien      size = int_size_in_bytes (tree_node);
1085250397Sobrien      break;
1085350397Sobrien    case FIELD_DECL:
1085450397Sobrien      /* For a data member of a struct or union, the DW_AT_byte_size is
10855132718Skan	 generally given as the number of bytes normally allocated for an
10856132718Skan	 object of the *declared* type of the member itself.  This is true
10857132718Skan	 even for bit-fields.  */
1085850397Sobrien      size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
1085950397Sobrien      break;
1086050397Sobrien    default:
10861169689Skan      gcc_unreachable ();
1086250397Sobrien    }
1086350397Sobrien
1086450397Sobrien  /* Note that `size' might be -1 when we get to this point.  If it is, that
1086550397Sobrien     indicates that the byte size of the entity in question is variable.  We
10866244776Spfg     have no good way of expressing this fact in Dwarf at the present time.
10867244776Spfg     GCC/35998: Avoid passing negative sizes to Dtrace and gdb.  */
10868244792Spfg  add_AT_unsigned (die, DW_AT_byte_size, (size != (unsigned)-1 ? size : 0));
1086950397Sobrien}
1087050397Sobrien
1087150397Sobrien/* For a FIELD_DECL node which represents a bit-field, output an attribute
1087250397Sobrien   which specifies the distance in bits from the highest order bit of the
1087350397Sobrien   "containing object" for the bit-field to the highest order bit of the
1087450397Sobrien   bit-field itself.
1087550397Sobrien
1087690075Sobrien   For any given bit-field, the "containing object" is a hypothetical object
1087790075Sobrien   (of some integral or enum type) within which the given bit-field lives.  The
1087890075Sobrien   type of this hypothetical "containing object" is always the same as the
1087990075Sobrien   declared type of the individual bit-field itself.  The determination of the
1088090075Sobrien   exact location of the "containing object" for a bit-field is rather
1088190075Sobrien   complicated.  It's handled by the `field_byte_offset' function (above).
1088250397Sobrien
1088350397Sobrien   Note that it is the size (in bytes) of the hypothetical "containing object"
1088450397Sobrien   which will be given in the DW_AT_byte_size attribute for this bit-field.
1088550397Sobrien   (See `byte_size_attribute' above).  */
1088650397Sobrien
1088750397Sobrienstatic inline void
10888132718Skanadd_bit_offset_attribute (dw_die_ref die, tree decl)
1088950397Sobrien{
1089090075Sobrien  HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
1089190075Sobrien  tree type = DECL_BIT_FIELD_TYPE (decl);
1089290075Sobrien  HOST_WIDE_INT bitpos_int;
1089390075Sobrien  HOST_WIDE_INT highest_order_object_bit_offset;
1089490075Sobrien  HOST_WIDE_INT highest_order_field_bit_offset;
1089590075Sobrien  HOST_WIDE_INT unsigned bit_offset;
1089650397Sobrien
1089750397Sobrien  /* Must be a field and a bit field.  */
10898169689Skan  gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
1089950397Sobrien
1090050397Sobrien  /* We can't yet handle bit-fields whose offsets are variable, so if we
1090150397Sobrien     encounter such things, just return without generating any attribute
1090290075Sobrien     whatsoever.  Likewise for variable or too large size.  */
1090390075Sobrien  if (! host_integerp (bit_position (decl), 0)
1090490075Sobrien      || ! host_integerp (DECL_SIZE (decl), 1))
1090550397Sobrien    return;
1090650397Sobrien
1090790075Sobrien  bitpos_int = int_bit_position (decl);
1090850397Sobrien
1090950397Sobrien  /* Note that the bit offset is always the distance (in bits) from the
1091090075Sobrien     highest-order bit of the "containing object" to the highest-order bit of
1091190075Sobrien     the bit-field itself.  Since the "high-order end" of any object or field
1091250397Sobrien     is different on big-endian and little-endian machines, the computation
1091350397Sobrien     below must take account of these differences.  */
1091450397Sobrien  highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
1091550397Sobrien  highest_order_field_bit_offset = bitpos_int;
1091650397Sobrien
1091750397Sobrien  if (! BYTES_BIG_ENDIAN)
1091850397Sobrien    {
1091990075Sobrien      highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
1092050397Sobrien      highest_order_object_bit_offset += simple_type_size_in_bits (type);
1092150397Sobrien    }
1092250397Sobrien
1092350397Sobrien  bit_offset
1092450397Sobrien    = (! BYTES_BIG_ENDIAN
1092550397Sobrien       ? highest_order_object_bit_offset - highest_order_field_bit_offset
1092650397Sobrien       : highest_order_field_bit_offset - highest_order_object_bit_offset);
1092750397Sobrien
1092850397Sobrien  add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
1092950397Sobrien}
1093050397Sobrien
1093150397Sobrien/* For a FIELD_DECL node which represents a bit field, output an attribute
1093250397Sobrien   which specifies the length in bits of the given field.  */
1093350397Sobrien
1093450397Sobrienstatic inline void
10935132718Skanadd_bit_size_attribute (dw_die_ref die, tree decl)
1093650397Sobrien{
1093750397Sobrien  /* Must be a field and a bit field.  */
10938169689Skan  gcc_assert (TREE_CODE (decl) == FIELD_DECL
10939169689Skan	      && DECL_BIT_FIELD_TYPE (decl));
1094090075Sobrien
1094190075Sobrien  if (host_integerp (DECL_SIZE (decl), 1))
1094290075Sobrien    add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
1094350397Sobrien}
1094450397Sobrien
1094550397Sobrien/* If the compiled language is ANSI C, then add a 'prototyped'
1094650397Sobrien   attribute, if arg types are given for the parameters of a function.  */
1094750397Sobrien
1094850397Sobrienstatic inline void
10949132718Skanadd_prototyped_attribute (dw_die_ref die, tree func_type)
1095050397Sobrien{
1095150397Sobrien  if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
1095250397Sobrien      && TYPE_ARG_TYPES (func_type) != NULL)
1095350397Sobrien    add_AT_flag (die, DW_AT_prototyped, 1);
1095450397Sobrien}
1095550397Sobrien
1095650397Sobrien/* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
1095750397Sobrien   by looking in either the type declaration or object declaration
1095850397Sobrien   equate table.  */
1095950397Sobrien
1096050397Sobrienstatic inline void
10961132718Skanadd_abstract_origin_attribute (dw_die_ref die, tree origin)
1096250397Sobrien{
1096350397Sobrien  dw_die_ref origin_die = NULL;
1096490075Sobrien
1096590075Sobrien  if (TREE_CODE (origin) != FUNCTION_DECL)
1096690075Sobrien    {
1096790075Sobrien      /* We may have gotten separated from the block for the inlined
1096890075Sobrien	 function, if we're in an exception handler or some such; make
1096990075Sobrien	 sure that the abstract function has been written out.
1097090075Sobrien
10971132718Skan	 Doing this for nested functions is wrong, however; functions are
1097290075Sobrien	 distinct units, and our context might not even be inline.  */
1097390075Sobrien      tree fn = origin;
1097490075Sobrien
1097590075Sobrien      if (TYPE_P (fn))
1097690075Sobrien	fn = TYPE_STUB_DECL (fn);
10977169689Skan
1097890075Sobrien      fn = decl_function_context (fn);
1097990075Sobrien      if (fn)
1098090075Sobrien	dwarf2out_abstract_function (fn);
1098190075Sobrien    }
1098290075Sobrien
1098390075Sobrien  if (DECL_P (origin))
1098450397Sobrien    origin_die = lookup_decl_die (origin);
1098590075Sobrien  else if (TYPE_P (origin))
1098650397Sobrien    origin_die = lookup_type_die (origin);
1098750397Sobrien
10988169689Skan  /* XXX: Functions that are never lowered don't always have correct block
10989169689Skan     trees (in the case of java, they simply have no block tree, in some other
10990169689Skan     languages).  For these functions, there is nothing we can really do to
10991169689Skan     output correct debug info for inlined functions in all cases.  Rather
10992169689Skan     than die, we'll just produce deficient debug info now, in that we will
10993169689Skan     have variables without a proper abstract origin.  In the future, when all
10994169689Skan     functions are lowered, we should re-add a gcc_assert (origin_die)
10995169689Skan     here.  */
1099690075Sobrien
10997169689Skan  if (origin_die)
10998169689Skan      add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
1099950397Sobrien}
1100050397Sobrien
1100150397Sobrien/* We do not currently support the pure_virtual attribute.  */
1100250397Sobrien
1100350397Sobrienstatic inline void
11004132718Skanadd_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
1100550397Sobrien{
1100650397Sobrien  if (DECL_VINDEX (func_decl))
1100750397Sobrien    {
1100850397Sobrien      add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
1100950397Sobrien
1101090075Sobrien      if (host_integerp (DECL_VINDEX (func_decl), 0))
1101190075Sobrien	add_AT_loc (die, DW_AT_vtable_elem_location,
1101290075Sobrien		    new_loc_descr (DW_OP_constu,
1101390075Sobrien				   tree_low_cst (DECL_VINDEX (func_decl), 0),
1101490075Sobrien				   0));
1101590075Sobrien
1101650397Sobrien      /* GNU extension: Record what type this method came from originally.  */
1101750397Sobrien      if (debug_info_level > DINFO_LEVEL_TERSE)
1101850397Sobrien	add_AT_die_ref (die, DW_AT_containing_type,
1101950397Sobrien			lookup_type_die (DECL_CONTEXT (func_decl)));
1102050397Sobrien    }
1102150397Sobrien}
1102250397Sobrien
1102350397Sobrien/* Add source coordinate attributes for the given decl.  */
1102450397Sobrien
1102550397Sobrienstatic void
11026132718Skanadd_src_coords_attributes (dw_die_ref die, tree decl)
1102750397Sobrien{
11028169689Skan  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
1102950397Sobrien
11030169689Skan  add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11031169689Skan  add_AT_unsigned (die, DW_AT_decl_line, s.line);
1103250397Sobrien}
1103350397Sobrien
11034132718Skan/* Add a DW_AT_name attribute and source coordinate attribute for the
1103550397Sobrien   given decl, but only if it actually has a name.  */
1103650397Sobrien
1103750397Sobrienstatic void
11038132718Skanadd_name_and_src_coords_attributes (dw_die_ref die, tree decl)
1103950397Sobrien{
1104090075Sobrien  tree decl_name;
1104150397Sobrien
1104290075Sobrien  decl_name = DECL_NAME (decl);
1104350397Sobrien  if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
1104450397Sobrien    {
1104550397Sobrien      add_name_attribute (die, dwarf2_name (decl, 0));
1104690075Sobrien      if (! DECL_ARTIFICIAL (decl))
1104790075Sobrien	add_src_coords_attributes (die, decl);
1104890075Sobrien
1104950397Sobrien      if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
1105090075Sobrien	  && TREE_PUBLIC (decl)
1105190075Sobrien	  && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11052169689Skan	  && !DECL_ABSTRACT (decl)
11053169689Skan	  && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
1105450397Sobrien	add_AT_string (die, DW_AT_MIPS_linkage_name,
1105550397Sobrien		       IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1105650397Sobrien    }
1105790075Sobrien
1105890075Sobrien#ifdef VMS_DEBUGGING_INFO
1105990075Sobrien  /* Get the function's name, as described by its RTL.  This may be different
1106090075Sobrien     from the DECL_NAME name used in the source file.  */
1106190075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
1106290075Sobrien    {
1106390075Sobrien      add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
1106490075Sobrien		   XEXP (DECL_RTL (decl), 0));
11065169689Skan      VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
1106690075Sobrien    }
1106790075Sobrien#endif
1106850397Sobrien}
1106950397Sobrien
1107090075Sobrien/* Push a new declaration scope.  */
1107150397Sobrien
1107250397Sobrienstatic void
11073132718Skanpush_decl_scope (tree scope)
1107450397Sobrien{
11075169689Skan  VEC_safe_push (tree, gc, decl_scope_table, scope);
1107690075Sobrien}
1107750397Sobrien
1107890075Sobrien/* Pop a declaration scope.  */
1107950397Sobrien
1108090075Sobrienstatic inline void
11081132718Skanpop_decl_scope (void)
1108290075Sobrien{
11083169689Skan  VEC_pop (tree, decl_scope_table);
1108450397Sobrien}
1108550397Sobrien
1108690075Sobrien/* Return the DIE for the scope that immediately contains this type.
1108790075Sobrien   Non-named types get global scope.  Named types nested in other
1108890075Sobrien   types get their containing scope if it's open, or global scope
1108990075Sobrien   otherwise.  All other types (i.e. function-local named types) get
1109090075Sobrien   the current active scope.  */
1109150397Sobrien
1109250397Sobrienstatic dw_die_ref
11093132718Skanscope_die_for (tree t, dw_die_ref context_die)
1109450397Sobrien{
1109590075Sobrien  dw_die_ref scope_die = NULL;
1109690075Sobrien  tree containing_scope;
1109790075Sobrien  int i;
1109850397Sobrien
1109990075Sobrien  /* Non-types always go in the current scope.  */
11100169689Skan  gcc_assert (TYPE_P (t));
1110150397Sobrien
1110290075Sobrien  containing_scope = TYPE_CONTEXT (t);
1110390075Sobrien
11104132718Skan  /* Use the containing namespace if it was passed in (for a declaration).  */
1110550397Sobrien  if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11106132718Skan    {
11107132718Skan      if (context_die == lookup_decl_die (containing_scope))
11108132718Skan	/* OK */;
11109132718Skan      else
11110132718Skan	containing_scope = NULL_TREE;
11111132718Skan    }
1111250397Sobrien
1111352284Sobrien  /* Ignore function type "scopes" from the C frontend.  They mean that
1111452284Sobrien     a tagged type is local to a parmlist of a function declarator, but
1111552284Sobrien     that isn't useful to DWARF.  */
1111652284Sobrien  if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
1111752284Sobrien    containing_scope = NULL_TREE;
1111852284Sobrien
11119169689Skan  if (containing_scope == NULL_TREE)
1112050397Sobrien    scope_die = comp_unit_die;
1112190075Sobrien  else if (TYPE_P (containing_scope))
1112250397Sobrien    {
1112390075Sobrien      /* For types, we can just look up the appropriate DIE.  But
1112490075Sobrien	 first we check to see if we're in the middle of emitting it
1112590075Sobrien	 so we know where the new DIE should go.  */
11126169689Skan      for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11127169689Skan	if (VEC_index (tree, decl_scope_table, i) == containing_scope)
1112890075Sobrien	  break;
1112950397Sobrien
1113050397Sobrien      if (i < 0)
1113150397Sobrien	{
11132169689Skan	  gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11133169689Skan		      || TREE_ASM_WRITTEN (containing_scope));
1113452284Sobrien
1113552284Sobrien	  /* If none of the current dies are suitable, we get file scope.  */
1113652284Sobrien	  scope_die = comp_unit_die;
1113750397Sobrien	}
1113890075Sobrien      else
1113990075Sobrien	scope_die = lookup_type_die (containing_scope);
1114050397Sobrien    }
1114190075Sobrien  else
1114290075Sobrien    scope_die = context_die;
1114350397Sobrien
1114450397Sobrien  return scope_die;
1114550397Sobrien}
1114650397Sobrien
1114790075Sobrien/* Returns nonzero if CONTEXT_DIE is internal to a function.  */
1114890075Sobrien
1114990075Sobrienstatic inline int
11150132718Skanlocal_scope_p (dw_die_ref context_die)
1115150397Sobrien{
1115290075Sobrien  for (; context_die; context_die = context_die->die_parent)
1115390075Sobrien    if (context_die->die_tag == DW_TAG_inlined_subroutine
1115490075Sobrien	|| context_die->die_tag == DW_TAG_subprogram)
1115590075Sobrien      return 1;
1115690075Sobrien
1115790075Sobrien  return 0;
1115850397Sobrien}
1115950397Sobrien
11160132718Skan/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11161132718Skan   whether or not to treat a DIE in this context as a declaration.  */
1116290075Sobrien
1116390075Sobrienstatic inline int
11164132718Skanclass_or_namespace_scope_p (dw_die_ref context_die)
1116590075Sobrien{
1116690075Sobrien  return (context_die
1116790075Sobrien	  && (context_die->die_tag == DW_TAG_structure_type
11168132718Skan	      || context_die->die_tag == DW_TAG_union_type
11169132718Skan	      || context_die->die_tag == DW_TAG_namespace));
1117090075Sobrien}
1117190075Sobrien
1117250397Sobrien/* Many forms of DIEs require a "type description" attribute.  This
1117350397Sobrien   routine locates the proper "type descriptor" die for the type given
11174132718Skan   by 'type', and adds a DW_AT_type attribute below the given die.  */
1117550397Sobrien
1117650397Sobrienstatic void
11177132718Skanadd_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11178132718Skan		    int decl_volatile, dw_die_ref context_die)
1117950397Sobrien{
1118090075Sobrien  enum tree_code code  = TREE_CODE (type);
1118190075Sobrien  dw_die_ref type_die  = NULL;
1118250397Sobrien
11183261188Spfg/* APPLE LOCAL begin radar 5847213 */
11184261188Spfg  /* APPLE LOCAL begin radar 5811943 - Fix type of pointers to blocks  */
11185261188Spfg  /* APPLE LOCAL - radar 6113240 */
11186261188Spfg  /* APPLE LOCAL begin radar 6300081  */
11187261188Spfg  if (code == BLOCK_POINTER_TYPE && generic_block_literal_struct_type)
11188261188Spfg    {
11189261188Spfg      type = build_pointer_type (generic_block_literal_struct_type);
11190261188Spfg      code = TREE_CODE (type);
11191261188Spfg    }
11192261188Spfg  /* APPLE LOCAL end radar 6300081  */
11193261188Spfg  /* APPLE LOCAL end radar 5811943 - Fix type of pointers to Blocks  */
11194261188Spfg/* APPLE LOCAL end radar 5847213 */
11195261188Spfg
1119650397Sobrien  /* ??? If this type is an unnamed subrange type of an integral or
1119750397Sobrien     floating-point type, use the inner type.  This is because we have no
1119850397Sobrien     support for unnamed types in base_type_die.  This can happen if this is
1119950397Sobrien     an Ada subrange type.  Correct solution is emit a subrange type die.  */
1120050397Sobrien  if ((code == INTEGER_TYPE || code == REAL_TYPE)
1120150397Sobrien      && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
1120250397Sobrien    type = TREE_TYPE (type), code = TREE_CODE (type);
1120350397Sobrien
1120490075Sobrien  if (code == ERROR_MARK
1120590075Sobrien      /* Handle a special case.  For functions whose return type is void, we
1120690075Sobrien	 generate *no* type attribute.  (Note that no object may have type
1120790075Sobrien	 `void', so this only applies to function return types).  */
1120890075Sobrien      || code == VOID_TYPE)
1120950397Sobrien    return;
1121050397Sobrien
1121150397Sobrien  type_die = modified_type_die (type,
1121250397Sobrien				decl_const || TYPE_READONLY (type),
1121350397Sobrien				decl_volatile || TYPE_VOLATILE (type),
1121450397Sobrien				context_die);
1121590075Sobrien
1121650397Sobrien  if (type_die != NULL)
1121750397Sobrien    add_AT_die_ref (object_die, DW_AT_type, type_die);
1121850397Sobrien}
1121950397Sobrien
11220169689Skan/* Given an object die, add the calling convention attribute for the
11221169689Skan   function call type.  */
11222169689Skanstatic void
11223169689Skanadd_calling_convention_attribute (dw_die_ref subr_die, tree type)
11224169689Skan{
11225169689Skan  enum dwarf_calling_convention value = DW_CC_normal;
11226169689Skan
11227169689Skan  value = targetm.dwarf_calling_convention (type);
11228169689Skan
11229169689Skan  /* Only add the attribute if the backend requests it, and
11230169689Skan     is not DW_CC_normal.  */
11231169689Skan  if (value && (value != DW_CC_normal))
11232169689Skan    add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11233169689Skan}
11234169689Skan
1123550397Sobrien/* Given a tree pointer to a struct, class, union, or enum type node, return
1123650397Sobrien   a pointer to the (string) tag name for the given type, or zero if the type
1123750397Sobrien   was declared without a tag.  */
1123850397Sobrien
1123990075Sobrienstatic const char *
11240132718Skantype_tag (tree type)
1124150397Sobrien{
1124290075Sobrien  const char *name = 0;
1124350397Sobrien
1124450397Sobrien  if (TYPE_NAME (type) != 0)
1124550397Sobrien    {
1124690075Sobrien      tree t = 0;
1124750397Sobrien
1124850397Sobrien      /* Find the IDENTIFIER_NODE for the type name.  */
1124950397Sobrien      if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1125050397Sobrien	t = TYPE_NAME (type);
1125150397Sobrien
1125290075Sobrien      /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11253132718Skan	 a TYPE_DECL node, regardless of whether or not a `typedef' was
11254132718Skan	 involved.  */
1125550397Sobrien      else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1125650397Sobrien	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
1125750397Sobrien	t = DECL_NAME (TYPE_NAME (type));
1125850397Sobrien
1125950397Sobrien      /* Now get the name as a string, or invent one.  */
1126050397Sobrien      if (t != 0)
1126150397Sobrien	name = IDENTIFIER_POINTER (t);
1126250397Sobrien    }
1126350397Sobrien
1126450397Sobrien  return (name == 0 || *name == '\0') ? 0 : name;
1126550397Sobrien}
1126650397Sobrien
1126750397Sobrien/* Return the type associated with a data member, make a special check
1126850397Sobrien   for bit field types.  */
1126950397Sobrien
1127050397Sobrienstatic inline tree
11271132718Skanmember_declared_type (tree member)
1127250397Sobrien{
1127350397Sobrien  return (DECL_BIT_FIELD_TYPE (member)
1127490075Sobrien	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
1127550397Sobrien}
1127650397Sobrien
1127750397Sobrien/* Get the decl's label, as described by its RTL. This may be different
1127850397Sobrien   from the DECL_NAME name used in the source file.  */
1127950397Sobrien
1128050397Sobrien#if 0
1128190075Sobrienstatic const char *
11282132718Skandecl_start_label (tree decl)
1128350397Sobrien{
1128450397Sobrien  rtx x;
1128590075Sobrien  const char *fnname;
1128690075Sobrien
1128750397Sobrien  x = DECL_RTL (decl);
11288169689Skan  gcc_assert (MEM_P (x));
1128950397Sobrien
1129050397Sobrien  x = XEXP (x, 0);
11291169689Skan  gcc_assert (GET_CODE (x) == SYMBOL_REF);
1129250397Sobrien
1129350397Sobrien  fnname = XSTR (x, 0);
1129450397Sobrien  return fnname;
1129550397Sobrien}
1129650397Sobrien#endif
1129750397Sobrien
1129850397Sobrien/* These routines generate the internal representation of the DIE's for
1129950397Sobrien   the compilation unit.  Debugging information is collected by walking
1130050397Sobrien   the declaration trees passed in from dwarf2out_decl().  */
1130150397Sobrien
1130250397Sobrienstatic void
11303132718Skangen_array_type_die (tree type, dw_die_ref context_die)
1130450397Sobrien{
1130590075Sobrien  dw_die_ref scope_die = scope_die_for (type, context_die);
1130690075Sobrien  dw_die_ref array_die;
1130790075Sobrien  tree element_type;
1130850397Sobrien
1130950397Sobrien  /* ??? The SGI dwarf reader fails for array of array of enum types unless
1131050397Sobrien     the inner array type comes before the outer array type.  Thus we must
1131150397Sobrien     call gen_type_die before we call new_die.  See below also.  */
1131250397Sobrien#ifdef MIPS_DEBUGGING_INFO
1131350397Sobrien  gen_type_die (TREE_TYPE (type), context_die);
1131450397Sobrien#endif
1131550397Sobrien
1131690075Sobrien  array_die = new_die (DW_TAG_array_type, scope_die, type);
1131796263Sobrien  add_name_attribute (array_die, type_tag (type));
1131896263Sobrien  equate_type_number_to_die (type, array_die);
1131950397Sobrien
1132096263Sobrien  if (TREE_CODE (type) == VECTOR_TYPE)
1132196263Sobrien    {
1132296263Sobrien      /* The frontend feeds us a representation for the vector as a struct
1132396263Sobrien	 containing an array.  Pull out the array type.  */
1132496263Sobrien      type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
1132596263Sobrien      add_AT_flag (array_die, DW_AT_GNU_vector, 1);
1132696263Sobrien    }
1132796263Sobrien
1132850397Sobrien#if 0
1132950397Sobrien  /* We default the array ordering.  SDB will probably do
1133050397Sobrien     the right things even if DW_AT_ordering is not present.  It's not even
1133150397Sobrien     an issue until we start to get into multidimensional arrays anyway.  If
1133250397Sobrien     SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
1133350397Sobrien     then we'll have to put the DW_AT_ordering attribute back in.  (But if
1133450397Sobrien     and when we find out that we need to put these in, we will only do so
1133550397Sobrien     for multidimensional arrays.  */
1133650397Sobrien  add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
1133750397Sobrien#endif
1133850397Sobrien
1133950397Sobrien#ifdef MIPS_DEBUGGING_INFO
1134050397Sobrien  /* The SGI compilers handle arrays of unknown bound by setting
1134150397Sobrien     AT_declaration and not emitting any subrange DIEs.  */
1134250397Sobrien  if (! TYPE_DOMAIN (type))
11343132718Skan    add_AT_flag (array_die, DW_AT_declaration, 1);
1134450397Sobrien  else
1134550397Sobrien#endif
1134650397Sobrien    add_subscript_info (array_die, type);
1134750397Sobrien
1134850397Sobrien  /* Add representation of the type of the elements of this array type.  */
1134950397Sobrien  element_type = TREE_TYPE (type);
1135050397Sobrien
1135150397Sobrien  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
1135250397Sobrien     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
1135350397Sobrien     We work around this by disabling this feature.  See also
1135450397Sobrien     add_subscript_info.  */
1135550397Sobrien#ifndef MIPS_DEBUGGING_INFO
1135650397Sobrien  while (TREE_CODE (element_type) == ARRAY_TYPE)
1135750397Sobrien    element_type = TREE_TYPE (element_type);
1135850397Sobrien
1135950397Sobrien  gen_type_die (element_type, context_die);
1136050397Sobrien#endif
1136150397Sobrien
1136250397Sobrien  add_type_attribute (array_die, element_type, 0, 0, context_die);
11363260395Spfg
11364260395Spfg  if (get_AT (array_die, DW_AT_name))
11365260395Spfg    add_pubtype (type, array_die);
1136650397Sobrien}
1136750397Sobrien
1136850397Sobrien#if 0
1136950397Sobrienstatic void
11370132718Skangen_entry_point_die (tree decl, dw_die_ref context_die)
1137150397Sobrien{
1137290075Sobrien  tree origin = decl_ultimate_origin (decl);
1137390075Sobrien  dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
1137490075Sobrien
1137550397Sobrien  if (origin != NULL)
1137650397Sobrien    add_abstract_origin_attribute (decl_die, origin);
1137750397Sobrien  else
1137850397Sobrien    {
1137950397Sobrien      add_name_and_src_coords_attributes (decl_die, decl);
1138050397Sobrien      add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
1138150397Sobrien			  0, 0, context_die);
1138250397Sobrien    }
1138350397Sobrien
1138450397Sobrien  if (DECL_ABSTRACT (decl))
1138550397Sobrien    equate_decl_number_to_die (decl, decl_die);
1138650397Sobrien  else
1138750397Sobrien    add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
1138850397Sobrien}
1138950397Sobrien#endif
1139050397Sobrien
1139152284Sobrien/* Walk through the list of incomplete types again, trying once more to
1139252284Sobrien   emit full debugging info for them.  */
1139352284Sobrien
1139452284Sobrienstatic void
11395132718Skanretry_incomplete_types (void)
1139652284Sobrien{
1139790075Sobrien  int i;
1139852284Sobrien
11399169689Skan  for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11400169689Skan    gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
1140152284Sobrien}
1140252284Sobrien
1140350397Sobrien/* Generate a DIE to represent an inlined instance of an enumeration type.  */
1140450397Sobrien
1140550397Sobrienstatic void
11406132718Skangen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
1140750397Sobrien{
1140890075Sobrien  dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
1140950397Sobrien
1141090075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1141190075Sobrien     be incomplete and such types are not marked.  */
1141250397Sobrien  add_abstract_origin_attribute (type_die, type);
1141350397Sobrien}
1141450397Sobrien
1141550397Sobrien/* Generate a DIE to represent an inlined instance of a structure type.  */
1141650397Sobrien
1141750397Sobrienstatic void
11418132718Skangen_inlined_structure_type_die (tree type, dw_die_ref context_die)
1141950397Sobrien{
1142090075Sobrien  dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
1142150397Sobrien
1142290075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1142390075Sobrien     be incomplete and such types are not marked.  */
1142450397Sobrien  add_abstract_origin_attribute (type_die, type);
1142550397Sobrien}
1142650397Sobrien
1142750397Sobrien/* Generate a DIE to represent an inlined instance of a union type.  */
1142850397Sobrien
1142950397Sobrienstatic void
11430132718Skangen_inlined_union_type_die (tree type, dw_die_ref context_die)
1143150397Sobrien{
1143290075Sobrien  dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
1143350397Sobrien
1143490075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1143590075Sobrien     be incomplete and such types are not marked.  */
1143650397Sobrien  add_abstract_origin_attribute (type_die, type);
1143750397Sobrien}
1143850397Sobrien
1143950397Sobrien/* Generate a DIE to represent an enumeration type.  Note that these DIEs
1144050397Sobrien   include all of the information about the enumeration values also. Each
1144150397Sobrien   enumerated type name/value is listed as a child of the enumerated type
1144250397Sobrien   DIE.  */
1144350397Sobrien
11444132718Skanstatic dw_die_ref
11445132718Skangen_enumeration_type_die (tree type, dw_die_ref context_die)
1144650397Sobrien{
1144790075Sobrien  dw_die_ref type_die = lookup_type_die (type);
1144850397Sobrien
1144950397Sobrien  if (type_die == NULL)
1145050397Sobrien    {
1145150397Sobrien      type_die = new_die (DW_TAG_enumeration_type,
1145290075Sobrien			  scope_die_for (type, context_die), type);
1145350397Sobrien      equate_type_number_to_die (type, type_die);
1145450397Sobrien      add_name_attribute (type_die, type_tag (type));
1145550397Sobrien    }
1145650397Sobrien  else if (! TYPE_SIZE (type))
11457132718Skan    return type_die;
1145850397Sobrien  else
1145950397Sobrien    remove_AT (type_die, DW_AT_declaration);
1146050397Sobrien
1146150397Sobrien  /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
1146250397Sobrien     given enum type is incomplete, do not generate the DW_AT_byte_size
1146350397Sobrien     attribute or the DW_AT_element_list attribute.  */
1146450397Sobrien  if (TYPE_SIZE (type))
1146550397Sobrien    {
1146690075Sobrien      tree link;
1146750397Sobrien
1146850397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1146950397Sobrien      add_byte_size_attribute (type_die, type);
1147050397Sobrien      if (TYPE_STUB_DECL (type) != NULL_TREE)
1147150397Sobrien	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
1147250397Sobrien
1147350397Sobrien      /* If the first reference to this type was as the return type of an
1147450397Sobrien	 inline function, then it may not have a parent.  Fix this now.  */
1147550397Sobrien      if (type_die->die_parent == NULL)
1147650397Sobrien	add_child_die (scope_die_for (type, context_die), type_die);
1147750397Sobrien
11478169689Skan      for (link = TYPE_VALUES (type);
1147950397Sobrien	   link != NULL; link = TREE_CHAIN (link))
1148050397Sobrien	{
1148190075Sobrien	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11482132718Skan	  tree value = TREE_VALUE (link);
1148350397Sobrien
1148450397Sobrien	  add_name_attribute (enum_die,
1148550397Sobrien			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
1148690075Sobrien
11487169689Skan	  if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11488132718Skan	    /* DWARF2 does not provide a way of indicating whether or
11489132718Skan	       not enumeration constants are signed or unsigned.  GDB
11490132718Skan	       always assumes the values are signed, so we output all
11491132718Skan	       values as if they were signed.  That means that
11492132718Skan	       enumeration constants with very large unsigned values
11493132718Skan	       will appear to have negative values in the debugger.  */
11494132718Skan	    add_AT_int (enum_die, DW_AT_const_value,
11495132718Skan			tree_low_cst (value, tree_int_cst_sgn (value) > 0));
1149650397Sobrien	}
1149750397Sobrien    }
1149850397Sobrien  else
1149950397Sobrien    add_AT_flag (type_die, DW_AT_declaration, 1);
11500132718Skan
11501260395Spfg  if (get_AT (type_die, DW_AT_name))
11502260395Spfg    add_pubtype (type, type_die);
11503260395Spfg
11504132718Skan  return type_die;
1150550397Sobrien}
1150650397Sobrien
1150750397Sobrien/* Generate a DIE to represent either a real live formal parameter decl or to
1150850397Sobrien   represent just the type of some formal parameter position in some function
1150950397Sobrien   type.
1151050397Sobrien
1151150397Sobrien   Note that this routine is a bit unusual because its argument may be a
1151250397Sobrien   ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
1151350397Sobrien   represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
1151450397Sobrien   node.  If it's the former then this function is being called to output a
1151550397Sobrien   DIE to represent a formal parameter object (or some inlining thereof).  If
1151650397Sobrien   it's the latter, then this function is only being called to output a
1151750397Sobrien   DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
1151850397Sobrien   argument type of some subprogram type.  */
1151950397Sobrien
1152050397Sobrienstatic dw_die_ref
11521132718Skangen_formal_parameter_die (tree node, dw_die_ref context_die)
1152250397Sobrien{
1152390075Sobrien  dw_die_ref parm_die
1152490075Sobrien    = new_die (DW_TAG_formal_parameter, context_die, node);
1152590075Sobrien  tree origin;
1152650397Sobrien
1152750397Sobrien  switch (TREE_CODE_CLASS (TREE_CODE (node)))
1152850397Sobrien    {
11529169689Skan    case tcc_declaration:
1153050397Sobrien      origin = decl_ultimate_origin (node);
1153150397Sobrien      if (origin != NULL)
1153250397Sobrien	add_abstract_origin_attribute (parm_die, origin);
1153350397Sobrien      else
1153450397Sobrien	{
1153550397Sobrien	  add_name_and_src_coords_attributes (parm_die, node);
1153650397Sobrien	  add_type_attribute (parm_die, TREE_TYPE (node),
1153750397Sobrien			      TREE_READONLY (node),
1153850397Sobrien			      TREE_THIS_VOLATILE (node),
1153950397Sobrien			      context_die);
1154050397Sobrien	  if (DECL_ARTIFICIAL (node))
1154150397Sobrien	    add_AT_flag (parm_die, DW_AT_artificial, 1);
1154250397Sobrien	}
1154350397Sobrien
1154450397Sobrien      equate_decl_number_to_die (node, parm_die);
1154550397Sobrien      if (! DECL_ABSTRACT (node))
11546169689Skan	add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
1154750397Sobrien
1154850397Sobrien      break;
1154950397Sobrien
11550169689Skan    case tcc_type:
1155150397Sobrien      /* We were called with some kind of a ..._TYPE node.  */
1155250397Sobrien      add_type_attribute (parm_die, node, 0, 0, context_die);
1155350397Sobrien      break;
1155450397Sobrien
1155550397Sobrien    default:
11556169689Skan      gcc_unreachable ();
1155750397Sobrien    }
1155850397Sobrien
1155950397Sobrien  return parm_die;
1156050397Sobrien}
1156150397Sobrien
1156250397Sobrien/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
1156350397Sobrien   at the end of an (ANSI prototyped) formal parameters list.  */
1156450397Sobrien
1156550397Sobrienstatic void
11566132718Skangen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
1156750397Sobrien{
1156890075Sobrien  new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
1156950397Sobrien}
1157050397Sobrien
1157150397Sobrien/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
1157250397Sobrien   DW_TAG_unspecified_parameters DIE) to represent the types of the formal
1157350397Sobrien   parameters as specified in some function type specification (except for
1157490075Sobrien   those which appear as part of a function *definition*).  */
1157550397Sobrien
1157650397Sobrienstatic void
11577132718Skangen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
1157850397Sobrien{
1157990075Sobrien  tree link;
1158090075Sobrien  tree formal_type = NULL;
1158190075Sobrien  tree first_parm_type;
1158290075Sobrien  tree arg;
1158350397Sobrien
1158490075Sobrien  if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
1158590075Sobrien    {
1158690075Sobrien      arg = DECL_ARGUMENTS (function_or_method_type);
1158790075Sobrien      function_or_method_type = TREE_TYPE (function_or_method_type);
1158890075Sobrien    }
1158990075Sobrien  else
1159090075Sobrien    arg = NULL_TREE;
11591117395Skan
1159290075Sobrien  first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
1159350397Sobrien
1159490075Sobrien  /* Make our first pass over the list of formal parameter types and output a
1159550397Sobrien     DW_TAG_formal_parameter DIE for each one.  */
1159690075Sobrien  for (link = first_parm_type; link; )
1159750397Sobrien    {
1159890075Sobrien      dw_die_ref parm_die;
1159990075Sobrien
1160050397Sobrien      formal_type = TREE_VALUE (link);
1160150397Sobrien      if (formal_type == void_type_node)
1160250397Sobrien	break;
1160350397Sobrien
1160450397Sobrien      /* Output a (nameless) DIE to represent the formal parameter itself.  */
1160550397Sobrien      parm_die = gen_formal_parameter_die (formal_type, context_die);
1160690075Sobrien      if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
1160790075Sobrien	   && link == first_parm_type)
1160890075Sobrien	  || (arg && DECL_ARTIFICIAL (arg)))
1160950397Sobrien	add_AT_flag (parm_die, DW_AT_artificial, 1);
1161090075Sobrien
1161190075Sobrien      link = TREE_CHAIN (link);
1161290075Sobrien      if (arg)
1161390075Sobrien	arg = TREE_CHAIN (arg);
1161450397Sobrien    }
1161550397Sobrien
1161650397Sobrien  /* If this function type has an ellipsis, add a
1161750397Sobrien     DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
1161850397Sobrien  if (formal_type != void_type_node)
1161950397Sobrien    gen_unspecified_parameters_die (function_or_method_type, context_die);
1162050397Sobrien
1162190075Sobrien  /* Make our second (and final) pass over the list of formal parameter types
1162250397Sobrien     and output DIEs to represent those types (as necessary).  */
1162350397Sobrien  for (link = TYPE_ARG_TYPES (function_or_method_type);
1162490075Sobrien       link && TREE_VALUE (link);
1162550397Sobrien       link = TREE_CHAIN (link))
1162690075Sobrien    gen_type_die (TREE_VALUE (link), context_die);
1162790075Sobrien}
1162890075Sobrien
1162990075Sobrien/* We want to generate the DIE for TYPE so that we can generate the
1163090075Sobrien   die for MEMBER, which has been defined; we will need to refer back
1163190075Sobrien   to the member declaration nested within TYPE.  If we're trying to
1163290075Sobrien   generate minimal debug info for TYPE, processing TYPE won't do the
1163390075Sobrien   trick; we need to attach the member declaration by hand.  */
1163490075Sobrien
1163590075Sobrienstatic void
11636132718Skangen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
1163790075Sobrien{
1163890075Sobrien  gen_type_die (type, context_die);
1163990075Sobrien
1164090075Sobrien  /* If we're trying to avoid duplicate debug info, we may not have
1164190075Sobrien     emitted the member decl for this function.  Emit it now.  */
1164290075Sobrien  if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
1164390075Sobrien      && ! lookup_decl_die (member))
1164450397Sobrien    {
11645169689Skan      dw_die_ref type_die;
11646169689Skan      gcc_assert (!decl_ultimate_origin (member));
1164750397Sobrien
1164890075Sobrien      push_decl_scope (type);
11649169689Skan      type_die = lookup_type_die (type);
1165090075Sobrien      if (TREE_CODE (member) == FUNCTION_DECL)
11651169689Skan	gen_subprogram_die (member, type_die);
11652169689Skan      else if (TREE_CODE (member) == FIELD_DECL)
11653169689Skan	{
11654169689Skan	  /* Ignore the nameless fields that are used to skip bits but handle
11655169689Skan	     C++ anonymous unions and structs.  */
11656169689Skan	  if (DECL_NAME (member) != NULL_TREE
11657169689Skan	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11658169689Skan	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11659169689Skan	    {
11660169689Skan	      gen_type_die (member_declared_type (member), type_die);
11661169689Skan	      gen_field_die (member, type_die);
11662169689Skan	    }
11663169689Skan	}
1166490075Sobrien      else
11665169689Skan	gen_variable_die (member, type_die);
1166690075Sobrien
1166790075Sobrien      pop_decl_scope ();
1166850397Sobrien    }
1166950397Sobrien}
1167050397Sobrien
1167190075Sobrien/* Generate the DWARF2 info for the "abstract" instance of a function which we
1167290075Sobrien   may later generate inlined and/or out-of-line instances of.  */
1167390075Sobrien
1167490075Sobrienstatic void
11675132718Skandwarf2out_abstract_function (tree decl)
1167690075Sobrien{
1167790075Sobrien  dw_die_ref old_die;
1167890075Sobrien  tree save_fn;
11679169689Skan  struct function *save_cfun;
1168090075Sobrien  tree context;
1168190075Sobrien  int was_abstract = DECL_ABSTRACT (decl);
1168290075Sobrien
1168390075Sobrien  /* Make sure we have the actual abstract inline, not a clone.  */
1168490075Sobrien  decl = DECL_ORIGIN (decl);
1168590075Sobrien
11686117395Skan  old_die = lookup_decl_die (decl);
11687132718Skan  if (old_die && get_AT (old_die, DW_AT_inline))
1168890075Sobrien    /* We've already generated the abstract instance.  */
1168990075Sobrien    return;
1169090075Sobrien
1169190075Sobrien  /* Be sure we've emitted the in-class declaration DIE (if any) first, so
1169290075Sobrien     we don't get confused by DECL_ABSTRACT.  */
1169390075Sobrien  if (debug_info_level > DINFO_LEVEL_TERSE)
1169490075Sobrien    {
1169590075Sobrien      context = decl_class_context (decl);
1169690075Sobrien      if (context)
1169790075Sobrien	gen_type_die_for_member
1169890075Sobrien	  (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
1169990075Sobrien    }
11700117395Skan
1170190075Sobrien  /* Pretend we've just finished compiling this function.  */
1170290075Sobrien  save_fn = current_function_decl;
11703169689Skan  save_cfun = cfun;
1170490075Sobrien  current_function_decl = decl;
11705169689Skan  cfun = DECL_STRUCT_FUNCTION (decl);
1170690075Sobrien
1170790075Sobrien  set_decl_abstract_flags (decl, 1);
1170890075Sobrien  dwarf2out_decl (decl);
1170990075Sobrien  if (! was_abstract)
1171090075Sobrien    set_decl_abstract_flags (decl, 0);
1171190075Sobrien
1171290075Sobrien  current_function_decl = save_fn;
11713169689Skan  cfun = save_cfun;
1171490075Sobrien}
1171590075Sobrien
11716169689Skan/* Helper function of premark_used_types() which gets called through
11717169689Skan   htab_traverse_resize().
11718169689Skan
11719169689Skan   Marks the DIE of a given type in *SLOT as perennial, so it never gets
11720169689Skan   marked as unused by prune_unused_types.  */
11721169689Skanstatic int
11722169689Skanpremark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11723169689Skan{
11724169689Skan  tree type;
11725169689Skan  dw_die_ref die;
11726169689Skan
11727169689Skan  type = *slot;
11728169689Skan  die = lookup_type_die (type);
11729169689Skan  if (die != NULL)
11730169689Skan    die->die_perennial_p = 1;
11731169689Skan  return 1;
11732169689Skan}
11733169689Skan
11734169689Skan/* Mark all members of used_types_hash as perennial.  */
11735169689Skanstatic void
11736169689Skanpremark_used_types (void)
11737169689Skan{
11738169689Skan  if (cfun && cfun->used_types_hash)
11739169689Skan    htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11740169689Skan}
11741169689Skan
1174250397Sobrien/* Generate a DIE to represent a declared function (either file-scope or
1174350397Sobrien   block-local).  */
1174450397Sobrien
1174550397Sobrienstatic void
11746132718Skangen_subprogram_die (tree decl, dw_die_ref context_die)
1174750397Sobrien{
1174850397Sobrien  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
1174990075Sobrien  tree origin = decl_ultimate_origin (decl);
1175090075Sobrien  dw_die_ref subr_die;
1175190075Sobrien  tree fn_arg_types;
1175290075Sobrien  tree outer_scope;
1175390075Sobrien  dw_die_ref old_die = lookup_decl_die (decl);
1175490075Sobrien  int declaration = (current_function_decl != decl
11755132718Skan		     || class_or_namespace_scope_p (context_die));
1175650397Sobrien
11757169689Skan  premark_used_types ();
11758169689Skan
1175990075Sobrien  /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
1176090075Sobrien     started to generate the abstract instance of an inline, decided to output
1176190075Sobrien     its containing class, and proceeded to emit the declaration of the inline
1176290075Sobrien     from the member list for the class.  If so, DECLARATION takes priority;
1176390075Sobrien     we'll get back to the abstract instance when done with the class.  */
1176490075Sobrien
1176590075Sobrien  /* The class-scope declaration DIE must be the primary DIE.  */
11766132718Skan  if (origin && declaration && class_or_namespace_scope_p (context_die))
1176750397Sobrien    {
1176890075Sobrien      origin = NULL;
11769169689Skan      gcc_assert (!old_die);
1177050397Sobrien    }
1177190075Sobrien
11772169689Skan  /* Now that the C++ front end lazily declares artificial member fns, we
11773169689Skan     might need to retrofit the declaration into its class.  */
11774169689Skan  if (!declaration && !origin && !old_die
11775169689Skan      && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11776169689Skan      && !class_or_namespace_scope_p (context_die)
11777169689Skan      && debug_info_level > DINFO_LEVEL_TERSE)
11778169689Skan    old_die = force_decl_die (decl);
11779169689Skan
1178090075Sobrien  if (origin != NULL)
1178150397Sobrien    {
11782169689Skan      gcc_assert (!declaration || local_scope_p (context_die));
1178350397Sobrien
1178490075Sobrien      /* Fixup die_parent for the abstract instance of a nested
1178590075Sobrien	 inline function.  */
1178690075Sobrien      if (old_die && old_die->die_parent == NULL)
1178790075Sobrien	add_child_die (context_die, old_die);
1178890075Sobrien
1178990075Sobrien      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
1179090075Sobrien      add_abstract_origin_attribute (subr_die, origin);
1179150397Sobrien    }
1179250397Sobrien  else if (old_die)
1179350397Sobrien    {
11794169689Skan      expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11795169689Skan      struct dwarf_file_data * file_index = lookup_filename (s.file);
1179650397Sobrien
1179790075Sobrien      if (!get_AT_flag (old_die, DW_AT_declaration)
1179890075Sobrien	  /* We can have a normal definition following an inline one in the
1179990075Sobrien	     case of redefinition of GNU C extern inlines.
1180090075Sobrien	     It seems reasonable to use AT_specification in this case.  */
11801132718Skan	  && !get_AT (old_die, DW_AT_inline))
1180252284Sobrien	{
11803169689Skan	  /* Detect and ignore this case, where we are trying to output
11804169689Skan	     something we have already output.  */
11805169689Skan	  return;
1180652284Sobrien	}
1180750397Sobrien
1180850397Sobrien      /* If the definition comes from the same place as the declaration,
1180950397Sobrien	 maybe use the old DIE.  We always want the DIE for this function
1181050397Sobrien	 that has the *_pc attributes to be under comp_unit_die so the
1181190075Sobrien	 debugger can find it.  We also need to do this for abstract
1181290075Sobrien	 instances of inlines, since the spec requires the out-of-line copy
1181390075Sobrien	 to have the same parent.  For local class methods, this doesn't
1181490075Sobrien	 apply; we just use the old DIE.  */
1181590075Sobrien      if ((old_die->die_parent == comp_unit_die || context_die == NULL)
1181690075Sobrien	  && (DECL_ARTIFICIAL (decl)
11817169689Skan	      || (get_AT_file (old_die, DW_AT_decl_file) == file_index
1181890075Sobrien		  && (get_AT_unsigned (old_die, DW_AT_decl_line)
11819169689Skan		      == (unsigned) s.line))))
1182050397Sobrien	{
1182150397Sobrien	  subr_die = old_die;
1182250397Sobrien
11823169689Skan	  /* Clear out the declaration attribute and the formal parameters.
11824169689Skan	     Do not remove all children, because it is possible that this
11825169689Skan	     declaration die was forced using force_decl_die(). In such
11826169689Skan	     cases die that forced declaration die (e.g. TAG_imported_module)
11827169689Skan	     is one of the children that we do not want to remove.  */
1182850397Sobrien	  remove_AT (subr_die, DW_AT_declaration);
11829146895Skan	  remove_child_TAG (subr_die, DW_TAG_formal_parameter);
1183050397Sobrien	}
1183150397Sobrien      else
1183250397Sobrien	{
1183390075Sobrien	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11834132718Skan	  add_AT_specification (subr_die, old_die);
11835169689Skan	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11836169689Skan	    add_AT_file (subr_die, DW_AT_decl_file, file_index);
11837169689Skan	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11838169689Skan	    add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
1183950397Sobrien	}
1184050397Sobrien    }
1184150397Sobrien  else
1184250397Sobrien    {
1184390075Sobrien      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
1184450397Sobrien
1184550397Sobrien      if (TREE_PUBLIC (decl))
1184650397Sobrien	add_AT_flag (subr_die, DW_AT_external, 1);
1184750397Sobrien
1184850397Sobrien      add_name_and_src_coords_attributes (subr_die, decl);
1184950397Sobrien      if (debug_info_level > DINFO_LEVEL_TERSE)
1185050397Sobrien	{
1185190075Sobrien	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
1185290075Sobrien	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
1185390075Sobrien			      0, 0, context_die);
1185450397Sobrien	}
1185550397Sobrien
1185650397Sobrien      add_pure_or_virtual_attribute (subr_die, decl);
1185750397Sobrien      if (DECL_ARTIFICIAL (decl))
1185850397Sobrien	add_AT_flag (subr_die, DW_AT_artificial, 1);
1185990075Sobrien
1186050397Sobrien      if (TREE_PROTECTED (decl))
1186150397Sobrien	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
1186250397Sobrien      else if (TREE_PRIVATE (decl))
1186350397Sobrien	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
1186450397Sobrien    }
1186550397Sobrien
1186650397Sobrien  if (declaration)
1186750397Sobrien    {
11868132718Skan      if (!old_die || !get_AT (old_die, DW_AT_inline))
1186990075Sobrien	{
1187090075Sobrien	  add_AT_flag (subr_die, DW_AT_declaration, 1);
1187150397Sobrien
1187290075Sobrien	  /* The first time we see a member function, it is in the context of
1187390075Sobrien	     the class to which it belongs.  We make sure of this by emitting
1187490075Sobrien	     the class first.  The next time is the definition, which is
11875169689Skan	     handled above.  The two may come from the same source text.
11876169689Skan
11877169689Skan	     Note that force_decl_die() forces function declaration die. It is
11878169689Skan	     later reused to represent definition.  */
11879169689Skan	  equate_decl_number_to_die (decl, subr_die);
1188090075Sobrien	}
1188150397Sobrien    }
1188250397Sobrien  else if (DECL_ABSTRACT (decl))
1188350397Sobrien    {
11884132718Skan      if (DECL_DECLARED_INLINE_P (decl))
1188550397Sobrien	{
11886132718Skan          if (cgraph_function_possibly_inlined_p (decl))
1188750397Sobrien	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
1188850397Sobrien	  else
11889132718Skan	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
1189050397Sobrien	}
1189150397Sobrien      else
11892132718Skan	{
11893132718Skan	  if (cgraph_function_possibly_inlined_p (decl))
11894132718Skan            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11895132718Skan	  else
11896132718Skan            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11897132718Skan	}
1189850397Sobrien
1189950397Sobrien      equate_decl_number_to_die (decl, subr_die);
1190050397Sobrien    }
1190150397Sobrien  else if (!DECL_EXTERNAL (decl))
1190250397Sobrien    {
11903169689Skan      HOST_WIDE_INT cfa_fb_offset;
11904169689Skan
11905132718Skan      if (!old_die || !get_AT (old_die, DW_AT_inline))
1190650397Sobrien	equate_decl_number_to_die (decl, subr_die);
1190750397Sobrien
11908169689Skan      if (!flag_reorder_blocks_and_partition)
11909169689Skan	{
11910169689Skan	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11911169689Skan				       current_function_funcdef_no);
11912169689Skan	  add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11913169689Skan	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11914169689Skan				       current_function_funcdef_no);
11915169689Skan	  add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11916169689Skan
11917169689Skan	  add_pubname (decl, subr_die);
11918169689Skan	  add_arange (decl, subr_die);
11919169689Skan	}
11920169689Skan      else
11921169689Skan	{  /* Do nothing for now; maybe need to duplicate die, one for
11922169689Skan	      hot section and ond for cold section, then use the hot/cold
11923169689Skan	      section begin/end labels to generate the aranges...  */
11924169689Skan	  /*
11925169689Skan	    add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11926169689Skan	    add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11927169689Skan	    add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11928169689Skan	    add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
1192950397Sobrien
11930169689Skan	    add_pubname (decl, subr_die);
11931169689Skan	    add_arange (decl, subr_die);
11932169689Skan	    add_arange (decl, subr_die);
11933169689Skan	   */
11934169689Skan	}
1193550397Sobrien
1193650397Sobrien#ifdef MIPS_DEBUGGING_INFO
1193750397Sobrien      /* Add a reference to the FDE for this routine.  */
1193850397Sobrien      add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
1193950397Sobrien#endif
1194050397Sobrien
11941169689Skan      cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
1194250397Sobrien
11943169689Skan      /* We define the "frame base" as the function's CFA.  This is more
11944169689Skan	 convenient for several reasons: (1) It's stable across the prologue
11945169689Skan	 and epilogue, which makes it better than just a frame pointer,
11946169689Skan	 (2) With dwarf3, there exists a one-byte encoding that allows us
11947169689Skan	 to reference the .debug_frame data by proxy, but failing that,
11948169689Skan	 (3) We can at least reuse the code inspection and interpretation
11949169689Skan	 code that determines the CFA position at various points in the
11950169689Skan	 function.  */
11951169689Skan      /* ??? Use some command-line or configury switch to enable the use
11952169689Skan	 of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11953169689Skan	 consumers that understand it; fall back to "pure" dwarf2 and
11954169689Skan	 convert the CFA data into a location list.  */
11955169689Skan      {
11956169689Skan	dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11957169689Skan	if (list->dw_loc_next)
11958169689Skan	  add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11959169689Skan	else
11960169689Skan	  add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11961169689Skan      }
11962169689Skan
11963169689Skan      /* Compute a displacement from the "steady-state frame pointer" to
11964169689Skan	 the CFA.  The former is what all stack slots and argument slots
11965169689Skan	 will reference in the rtl; the later is what we've told the
11966169689Skan	 debugger about.  We'll need to adjust all frame_base references
11967169689Skan	 by this displacement.  */
11968169689Skan      compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11969169689Skan
11970169689Skan      if (cfun->static_chain_decl)
1197150397Sobrien	add_AT_location_description (subr_die, DW_AT_static_link,
11972169689Skan		 loc_descriptor_from_tree (cfun->static_chain_decl));
1197350397Sobrien    }
1197450397Sobrien
1197550397Sobrien  /* Now output descriptions of the arguments for this function. This gets
1197690075Sobrien     (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
1197750397Sobrien     for a FUNCTION_DECL doesn't indicate cases where there was a trailing
1197850397Sobrien     `...' at the end of the formal parameter list.  In order to find out if
1197950397Sobrien     there was a trailing ellipsis or not, we must instead look at the type
1198050397Sobrien     associated with the FUNCTION_DECL.  This will be a node of type
1198150397Sobrien     FUNCTION_TYPE. If the chain of type nodes hanging off of this
1198290075Sobrien     FUNCTION_TYPE node ends with a void_type_node then there should *not* be
1198350397Sobrien     an ellipsis at the end.  */
1198450397Sobrien
1198550397Sobrien  /* In the case where we are describing a mere function declaration, all we
1198690075Sobrien     need to do here (and all we *can* do here) is to describe the *types* of
1198750397Sobrien     its formal parameters.  */
1198850397Sobrien  if (debug_info_level <= DINFO_LEVEL_TERSE)
1198950397Sobrien    ;
1199050397Sobrien  else if (declaration)
1199190075Sobrien    gen_formal_types_die (decl, subr_die);
1199250397Sobrien  else
1199350397Sobrien    {
11994132718Skan      /* Generate DIEs to represent all known formal parameters.  */
1199590075Sobrien      tree arg_decls = DECL_ARGUMENTS (decl);
1199690075Sobrien      tree parm;
1199750397Sobrien
1199850397Sobrien      /* When generating DIEs, generate the unspecified_parameters DIE
11999132718Skan	 instead if we come across the arg "__builtin_va_alist" */
1200050397Sobrien      for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
1200150397Sobrien	if (TREE_CODE (parm) == PARM_DECL)
1200250397Sobrien	  {
1200350397Sobrien	    if (DECL_NAME (parm)
1200450397Sobrien		&& !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
1200550397Sobrien			    "__builtin_va_alist"))
1200650397Sobrien	      gen_unspecified_parameters_die (parm, subr_die);
1200750397Sobrien	    else
1200850397Sobrien	      gen_decl_die (parm, subr_die);
1200950397Sobrien	  }
1201050397Sobrien
1201190075Sobrien      /* Decide whether we need an unspecified_parameters DIE at the end.
12012132718Skan	 There are 2 more cases to do this for: 1) the ansi ... declaration -
12013132718Skan	 this is detectable when the end of the arg list is not a
12014132718Skan	 void_type_node 2) an unprototyped function declaration (not a
12015132718Skan	 definition).  This just means that we have no info about the
12016132718Skan	 parameters at all.  */
1201750397Sobrien      fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1201850397Sobrien      if (fn_arg_types != NULL)
1201950397Sobrien	{
12020132718Skan	  /* This is the prototyped case, check for....  */
1202150397Sobrien	  if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
1202250397Sobrien	    gen_unspecified_parameters_die (decl, subr_die);
1202350397Sobrien	}
1202450397Sobrien      else if (DECL_INITIAL (decl) == NULL_TREE)
1202550397Sobrien	gen_unspecified_parameters_die (decl, subr_die);
1202650397Sobrien    }
1202750397Sobrien
1202850397Sobrien  /* Output Dwarf info for all of the stuff within the body of the function
1202950397Sobrien     (if it has one - it may be just a declaration).  */
1203050397Sobrien  outer_scope = DECL_INITIAL (decl);
1203150397Sobrien
1203290075Sobrien  /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
1203390075Sobrien     a function.  This BLOCK actually represents the outermost binding contour
1203490075Sobrien     for the function, i.e. the contour in which the function's formal
1203590075Sobrien     parameters and labels get declared. Curiously, it appears that the front
1203690075Sobrien     end doesn't actually put the PARM_DECL nodes for the current function onto
1203790075Sobrien     the BLOCK_VARS list for this outer scope, but are strung off of the
1203890075Sobrien     DECL_ARGUMENTS list for the function instead.
1203990075Sobrien
1204090075Sobrien     The BLOCK_VARS list for the `outer_scope' does provide us with a list of
1204190075Sobrien     the LABEL_DECL nodes for the function however, and we output DWARF info
1204290075Sobrien     for those in decls_for_scope.  Just within the `outer_scope' there will be
1204390075Sobrien     a BLOCK node representing the function's outermost pair of curly braces,
1204490075Sobrien     and any blocks used for the base and member initializers of a C++
1204550397Sobrien     constructor function.  */
1204650397Sobrien  if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
1204750397Sobrien    {
12048169689Skan      /* Emit a DW_TAG_variable DIE for a named return value.  */
12049169689Skan      if (DECL_NAME (DECL_RESULT (decl)))
12050169689Skan	gen_decl_die (DECL_RESULT (decl), subr_die);
12051169689Skan
1205250397Sobrien      current_function_has_inlines = 0;
1205350397Sobrien      decls_for_scope (outer_scope, subr_die, 0);
1205450397Sobrien
1205550397Sobrien#if 0 && defined (MIPS_DEBUGGING_INFO)
1205650397Sobrien      if (current_function_has_inlines)
1205750397Sobrien	{
1205850397Sobrien	  add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
1205950397Sobrien	  if (! comp_unit_has_inlines)
1206050397Sobrien	    {
1206150397Sobrien	      add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
1206250397Sobrien	      comp_unit_has_inlines = 1;
1206350397Sobrien	    }
1206450397Sobrien	}
1206550397Sobrien#endif
1206650397Sobrien    }
12067169689Skan  /* Add the calling convention attribute if requested.  */
12068169689Skan  add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12069169689Skan
1207050397Sobrien}
1207150397Sobrien
1207250397Sobrien/* Generate a DIE to represent a declared data object.  */
1207350397Sobrien
1207450397Sobrienstatic void
12075132718Skangen_variable_die (tree decl, dw_die_ref context_die)
1207650397Sobrien{
1207790075Sobrien  tree origin = decl_ultimate_origin (decl);
1207890075Sobrien  dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
1207950397Sobrien
1208050397Sobrien  dw_die_ref old_die = lookup_decl_die (decl);
1208190075Sobrien  int declaration = (DECL_EXTERNAL (decl)
12082169689Skan		     /* If DECL is COMDAT and has not actually been
12083169689Skan			emitted, we cannot take its address; there
12084169689Skan			might end up being no definition anywhere in
12085169689Skan			the program.  For example, consider the C++
12086169689Skan			test case:
12087169689Skan
12088169689Skan                          template <class T>
12089169689Skan                          struct S { static const int i = 7; };
12090169689Skan
12091169689Skan                          template <class T>
12092169689Skan                          const int S<T>::i;
12093169689Skan
12094169689Skan                          int f() { return S<int>::i; }
12095169689Skan
12096169689Skan			Here, S<int>::i is not DECL_EXTERNAL, but no
12097169689Skan			definition is required, so the compiler will
12098169689Skan			not emit a definition.  */
12099169689Skan		     || (TREE_CODE (decl) == VAR_DECL
12100169689Skan			 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12101132718Skan		     || class_or_namespace_scope_p (context_die));
1210250397Sobrien
1210350397Sobrien  if (origin != NULL)
1210450397Sobrien    add_abstract_origin_attribute (var_die, origin);
1210590075Sobrien
1210650397Sobrien  /* Loop unrolling can create multiple blocks that refer to the same
1210790075Sobrien     static variable, so we must test for the DW_AT_declaration flag.
1210890075Sobrien
1210990075Sobrien     ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
1211050397Sobrien     copy decls and set the DECL_ABSTRACT flag on them instead of
1211190075Sobrien     sharing them.
1211290075Sobrien
12113169689Skan     ??? Duplicated blocks have been rewritten to use .debug_ranges.
12114169689Skan
12115169689Skan     ??? The declare_in_namespace support causes us to get two DIEs for one
12116169689Skan     variable, both of which are declarations.  We want to avoid considering
12117169689Skan     one to be a specification, so we must test that this DIE is not a
12118169689Skan     declaration.  */
12119169689Skan  else if (old_die && TREE_STATIC (decl) && ! declaration
12120117395Skan	   && get_AT_flag (old_die, DW_AT_declaration) == 1)
1212150397Sobrien    {
1212290075Sobrien      /* This is a definition of a C++ class level static.  */
12123132718Skan      add_AT_specification (var_die, old_die);
1212450397Sobrien      if (DECL_NAME (decl))
1212550397Sobrien	{
12126169689Skan	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12127169689Skan	  struct dwarf_file_data * file_index = lookup_filename (s.file);
1212850397Sobrien
12129169689Skan	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12130169689Skan	    add_AT_file (var_die, DW_AT_decl_file, file_index);
1213150397Sobrien
12132169689Skan	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
1213350397Sobrien
12134169689Skan	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
1213550397Sobrien	}
1213650397Sobrien    }
1213750397Sobrien  else
1213850397Sobrien    {
1213950397Sobrien      add_name_and_src_coords_attributes (var_die, decl);
1214090075Sobrien      add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
1214150397Sobrien			  TREE_THIS_VOLATILE (decl), context_die);
1214250397Sobrien
1214350397Sobrien      if (TREE_PUBLIC (decl))
1214450397Sobrien	add_AT_flag (var_die, DW_AT_external, 1);
1214550397Sobrien
1214650397Sobrien      if (DECL_ARTIFICIAL (decl))
1214750397Sobrien	add_AT_flag (var_die, DW_AT_artificial, 1);
1214850397Sobrien
1214950397Sobrien      if (TREE_PROTECTED (decl))
1215050397Sobrien	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
1215150397Sobrien      else if (TREE_PRIVATE (decl))
1215250397Sobrien	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
1215350397Sobrien    }
1215450397Sobrien
1215550397Sobrien  if (declaration)
1215650397Sobrien    add_AT_flag (var_die, DW_AT_declaration, 1);
1215790075Sobrien
12158169689Skan  if (DECL_ABSTRACT (decl) || declaration)
1215950397Sobrien    equate_decl_number_to_die (decl, var_die);
1216050397Sobrien
1216150397Sobrien  if (! declaration && ! DECL_ABSTRACT (decl))
1216250397Sobrien    {
12163169689Skan      add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
1216450397Sobrien      add_pubname (decl, var_die);
1216550397Sobrien    }
1216690075Sobrien  else
1216790075Sobrien    tree_add_const_value_attribute (var_die, decl);
1216850397Sobrien}
1216950397Sobrien
1217050397Sobrien/* Generate a DIE to represent a label identifier.  */
1217150397Sobrien
1217250397Sobrienstatic void
12173132718Skangen_label_die (tree decl, dw_die_ref context_die)
1217450397Sobrien{
1217590075Sobrien  tree origin = decl_ultimate_origin (decl);
1217690075Sobrien  dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
1217790075Sobrien  rtx insn;
1217850397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
1217950397Sobrien
1218050397Sobrien  if (origin != NULL)
1218150397Sobrien    add_abstract_origin_attribute (lbl_die, origin);
1218250397Sobrien  else
1218350397Sobrien    add_name_and_src_coords_attributes (lbl_die, decl);
1218450397Sobrien
1218550397Sobrien  if (DECL_ABSTRACT (decl))
1218650397Sobrien    equate_decl_number_to_die (decl, lbl_die);
1218750397Sobrien  else
1218850397Sobrien    {
12189132718Skan      insn = DECL_RTL_IF_SET (decl);
1219052284Sobrien
1219152284Sobrien      /* Deleted labels are programmer specified labels which have been
12192132718Skan	 eliminated because of various optimizations.  We still emit them
1219352284Sobrien	 here so that it is possible to put breakpoints on them.  */
12194132718Skan      if (insn
12195169689Skan	  && (LABEL_P (insn)
12196169689Skan	      || ((NOTE_P (insn)
12197132718Skan	           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
1219850397Sobrien	{
1219990075Sobrien	  /* When optimization is enabled (via -O) some parts of the compiler
1220090075Sobrien	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
1220150397Sobrien	     represent source-level labels which were explicitly declared by
1220250397Sobrien	     the user.  This really shouldn't be happening though, so catch
1220350397Sobrien	     it if it ever does happen.  */
12204169689Skan	  gcc_assert (!INSN_DELETED_P (insn));
1220550397Sobrien
1220690075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
1220750397Sobrien	  add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
1220850397Sobrien	}
1220950397Sobrien    }
1221050397Sobrien}
1221150397Sobrien
12212169689Skan/* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12213169689Skan   attributes to the DIE for a block STMT, to describe where the inlined
12214169689Skan   function was called from.  This is similar to add_src_coords_attributes.  */
1221550397Sobrien
12216169689Skanstatic inline void
12217169689Skanadd_call_src_coords_attributes (tree stmt, dw_die_ref die)
1221850397Sobrien{
12219169689Skan  expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12220169689Skan
12221169689Skan  add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12222169689Skan  add_AT_unsigned (die, DW_AT_call_line, s.line);
12223169689Skan}
12224169689Skan
12225169689Skan/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12226169689Skan   Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12227169689Skan
12228169689Skanstatic inline void
12229169689Skanadd_high_low_attributes (tree stmt, dw_die_ref die)
12230169689Skan{
1223150397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
1223250397Sobrien
12233169689Skan  if (BLOCK_FRAGMENT_CHAIN (stmt))
1223450397Sobrien    {
12235169689Skan      tree chain;
1223690075Sobrien
12237169689Skan      add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
1223890075Sobrien
12239169689Skan      chain = BLOCK_FRAGMENT_CHAIN (stmt);
12240169689Skan      do
1224190075Sobrien	{
12242169689Skan	  add_ranges (chain);
12243169689Skan	  chain = BLOCK_FRAGMENT_CHAIN (chain);
1224490075Sobrien	}
12245169689Skan      while (chain);
12246169689Skan      add_ranges (NULL);
1224750397Sobrien    }
12248169689Skan  else
12249169689Skan    {
12250169689Skan      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12251169689Skan				   BLOCK_NUMBER (stmt));
12252169689Skan      add_AT_lbl_id (die, DW_AT_low_pc, label);
12253169689Skan      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12254169689Skan				   BLOCK_NUMBER (stmt));
12255169689Skan      add_AT_lbl_id (die, DW_AT_high_pc, label);
12256169689Skan    }
12257169689Skan}
1225850397Sobrien
12259169689Skan/* Generate a DIE for a lexical block.  */
12260169689Skan
12261169689Skanstatic void
12262169689Skangen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12263169689Skan{
12264169689Skan  dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12265169689Skan
12266169689Skan  if (! BLOCK_ABSTRACT (stmt))
12267169689Skan    add_high_low_attributes (stmt, stmt_die);
12268169689Skan
1226950397Sobrien  decls_for_scope (stmt, stmt_die, depth);
1227050397Sobrien}
1227150397Sobrien
1227250397Sobrien/* Generate a DIE for an inlined subprogram.  */
1227350397Sobrien
1227450397Sobrienstatic void
12275132718Skangen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
1227650397Sobrien{
12277122180Skan  tree decl = block_ultimate_origin (stmt);
12278122180Skan
12279122180Skan  /* Emit info for the abstract instance first, if we haven't yet.  We
12280122180Skan     must emit this even if the block is abstract, otherwise when we
12281122180Skan     emit the block below (or elsewhere), we may end up trying to emit
12282122180Skan     a die whose origin die hasn't been emitted, and crashing.  */
12283122180Skan  dwarf2out_abstract_function (decl);
12284122180Skan
1228550397Sobrien  if (! BLOCK_ABSTRACT (stmt))
1228650397Sobrien    {
1228790075Sobrien      dw_die_ref subr_die
1228890075Sobrien	= new_die (DW_TAG_inlined_subroutine, context_die, stmt);
1228950397Sobrien
1229050397Sobrien      add_abstract_origin_attribute (subr_die, decl);
12291169689Skan      add_high_low_attributes (stmt, subr_die);
12292169689Skan      add_call_src_coords_attributes (stmt, subr_die);
12293169689Skan
1229450397Sobrien      decls_for_scope (stmt, subr_die, depth);
1229550397Sobrien      current_function_has_inlines = 1;
1229650397Sobrien    }
1229796263Sobrien  else
1229896263Sobrien    /* We may get here if we're the outer block of function A that was
1229996263Sobrien       inlined into function B that was inlined into function C.  When
1230096263Sobrien       generating debugging info for C, dwarf2out_abstract_function(B)
1230196263Sobrien       would mark all inlined blocks as abstract, including this one.
1230296263Sobrien       So, we wouldn't (and shouldn't) expect labels to be generated
1230396263Sobrien       for this one.  Instead, just emit debugging info for
1230496263Sobrien       declarations within the block.  This is particularly important
1230596263Sobrien       in the case of initializers of arguments passed from B to us:
1230696263Sobrien       if they're statement expressions containing declarations, we
1230796263Sobrien       wouldn't generate dies for their abstract variables, and then,
1230896263Sobrien       when generating dies for the real variables, we'd die (pun
1230996263Sobrien       intended :-)  */
1231096263Sobrien    gen_lexical_block_die (stmt, context_die, depth);
1231150397Sobrien}
1231250397Sobrien
1231350397Sobrien/* Generate a DIE for a field in a record, or structure.  */
1231450397Sobrien
1231550397Sobrienstatic void
12316132718Skangen_field_die (tree decl, dw_die_ref context_die)
1231750397Sobrien{
12318117395Skan  dw_die_ref decl_die;
1231950397Sobrien
12320117395Skan  if (TREE_TYPE (decl) == error_mark_node)
12321117395Skan    return;
12322132718Skan
12323117395Skan  decl_die = new_die (DW_TAG_member, context_die, decl);
1232450397Sobrien  add_name_and_src_coords_attributes (decl_die, decl);
1232550397Sobrien  add_type_attribute (decl_die, member_declared_type (decl),
1232650397Sobrien		      TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
1232750397Sobrien		      context_die);
1232850397Sobrien
1232950397Sobrien  if (DECL_BIT_FIELD_TYPE (decl))
1233050397Sobrien    {
1233150397Sobrien      add_byte_size_attribute (decl_die, decl);
1233250397Sobrien      add_bit_size_attribute (decl_die, decl);
1233350397Sobrien      add_bit_offset_attribute (decl_die, decl);
1233450397Sobrien    }
1233550397Sobrien
1233650397Sobrien  if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
1233750397Sobrien    add_data_member_location_attribute (decl_die, decl);
1233850397Sobrien
1233950397Sobrien  if (DECL_ARTIFICIAL (decl))
1234050397Sobrien    add_AT_flag (decl_die, DW_AT_artificial, 1);
1234150397Sobrien
1234250397Sobrien  if (TREE_PROTECTED (decl))
1234350397Sobrien    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
1234450397Sobrien  else if (TREE_PRIVATE (decl))
1234550397Sobrien    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12346169689Skan
12347169689Skan  /* Equate decl number to die, so that we can look up this decl later on.  */
12348169689Skan  equate_decl_number_to_die (decl, decl_die);
1234950397Sobrien}
1235050397Sobrien
1235150397Sobrien#if 0
1235250397Sobrien/* Don't generate either pointer_type DIEs or reference_type DIEs here.
1235350397Sobrien   Use modified_type_die instead.
1235450397Sobrien   We keep this code here just in case these types of DIEs may be needed to
1235550397Sobrien   represent certain things in other languages (e.g. Pascal) someday.  */
1235690075Sobrien
1235750397Sobrienstatic void
12358132718Skangen_pointer_type_die (tree type, dw_die_ref context_die)
1235950397Sobrien{
1236090075Sobrien  dw_die_ref ptr_die
1236190075Sobrien    = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
1236250397Sobrien
1236350397Sobrien  equate_type_number_to_die (type, ptr_die);
1236450397Sobrien  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
1236550397Sobrien  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
1236650397Sobrien}
1236750397Sobrien
1236850397Sobrien/* Don't generate either pointer_type DIEs or reference_type DIEs here.
1236950397Sobrien   Use modified_type_die instead.
1237050397Sobrien   We keep this code here just in case these types of DIEs may be needed to
1237150397Sobrien   represent certain things in other languages (e.g. Pascal) someday.  */
1237290075Sobrien
1237350397Sobrienstatic void
12374132718Skangen_reference_type_die (tree type, dw_die_ref context_die)
1237550397Sobrien{
1237690075Sobrien  dw_die_ref ref_die
1237790075Sobrien    = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
1237850397Sobrien
1237950397Sobrien  equate_type_number_to_die (type, ref_die);
1238050397Sobrien  add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
1238150397Sobrien  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
1238250397Sobrien}
1238350397Sobrien#endif
1238450397Sobrien
1238550397Sobrien/* Generate a DIE for a pointer to a member type.  */
1238690075Sobrien
1238750397Sobrienstatic void
12388132718Skangen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
1238950397Sobrien{
1239090075Sobrien  dw_die_ref ptr_die
1239190075Sobrien    = new_die (DW_TAG_ptr_to_member_type,
1239290075Sobrien	       scope_die_for (type, context_die), type);
1239350397Sobrien
1239450397Sobrien  equate_type_number_to_die (type, ptr_die);
1239550397Sobrien  add_AT_die_ref (ptr_die, DW_AT_containing_type,
1239650397Sobrien		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
1239750397Sobrien  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
1239850397Sobrien}
1239950397Sobrien
1240050397Sobrien/* Generate the DIE for the compilation unit.  */
1240150397Sobrien
1240290075Sobrienstatic dw_die_ref
12403132718Skangen_compile_unit_die (const char *filename)
1240450397Sobrien{
1240590075Sobrien  dw_die_ref die;
1240650397Sobrien  char producer[250];
1240790075Sobrien  const char *language_string = lang_hooks.name;
1240890075Sobrien  int language;
1240950397Sobrien
1241090075Sobrien  die = new_die (DW_TAG_compile_unit, NULL, NULL);
1241150397Sobrien
12412132718Skan  if (filename)
12413132718Skan    {
12414132718Skan      add_name_attribute (die, filename);
12415132718Skan      /* Don't add cwd for <built-in>.  */
12416132718Skan      if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12417132718Skan	add_comp_dir_attribute (die);
12418132718Skan    }
1241950397Sobrien
1242050397Sobrien  sprintf (producer, "%s %s", language_string, version_string);
1242150397Sobrien
1242250397Sobrien#ifdef MIPS_DEBUGGING_INFO
1242350397Sobrien  /* The MIPS/SGI compilers place the 'cc' command line options in the producer
1242450397Sobrien     string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
1242550397Sobrien     not appear in the producer string, the debugger reaches the conclusion
1242650397Sobrien     that the object file is stripped and has no debugging information.
1242750397Sobrien     To get the MIPS/SGI debugger to believe that there is debugging
1242850397Sobrien     information in the object file, we add a -g to the producer string.  */
1242950397Sobrien  if (debug_info_level > DINFO_LEVEL_TERSE)
1243050397Sobrien    strcat (producer, " -g");
1243150397Sobrien#endif
1243250397Sobrien
1243390075Sobrien  add_AT_string (die, DW_AT_producer, producer);
1243450397Sobrien
1243550397Sobrien  if (strcmp (language_string, "GNU C++") == 0)
1243690075Sobrien    language = DW_LANG_C_plus_plus;
1243750397Sobrien  else if (strcmp (language_string, "GNU Ada") == 0)
12438132718Skan    language = DW_LANG_Ada95;
1243950397Sobrien  else if (strcmp (language_string, "GNU F77") == 0)
1244090075Sobrien    language = DW_LANG_Fortran77;
12441169689Skan  else if (strcmp (language_string, "GNU F95") == 0)
12442169689Skan    language = DW_LANG_Fortran95;
1244350397Sobrien  else if (strcmp (language_string, "GNU Pascal") == 0)
1244490075Sobrien    language = DW_LANG_Pascal83;
1244590075Sobrien  else if (strcmp (language_string, "GNU Java") == 0)
1244690075Sobrien    language = DW_LANG_Java;
12447169689Skan  else if (strcmp (language_string, "GNU Objective-C") == 0)
12448169689Skan    language = DW_LANG_ObjC;
12449169689Skan  else if (strcmp (language_string, "GNU Objective-C++") == 0)
12450169689Skan    language = DW_LANG_ObjC_plus_plus;
1245150397Sobrien  else
1245290075Sobrien    language = DW_LANG_C89;
1245350397Sobrien
1245490075Sobrien  add_AT_unsigned (die, DW_AT_language, language);
1245590075Sobrien  return die;
1245650397Sobrien}
1245750397Sobrien
1245850397Sobrien/* Generate the DIE for a base class.  */
1245950397Sobrien
1246050397Sobrienstatic void
12461132718Skangen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
1246250397Sobrien{
1246390075Sobrien  dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
1246450397Sobrien
1246550397Sobrien  add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
1246650397Sobrien  add_data_member_location_attribute (die, binfo);
1246750397Sobrien
12468169689Skan  if (BINFO_VIRTUAL_P (binfo))
1246950397Sobrien    add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
1247090075Sobrien
12471132718Skan  if (access == access_public_node)
1247250397Sobrien    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12473132718Skan  else if (access == access_protected_node)
1247450397Sobrien    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
1247550397Sobrien}
1247650397Sobrien
1247750397Sobrien/* Generate a DIE for a class member.  */
1247850397Sobrien
1247950397Sobrienstatic void
12480132718Skangen_member_die (tree type, dw_die_ref context_die)
1248150397Sobrien{
1248290075Sobrien  tree member;
12483132718Skan  tree binfo = TYPE_BINFO (type);
1248490075Sobrien  dw_die_ref child;
1248550397Sobrien
1248650397Sobrien  /* If this is not an incomplete type, output descriptions of each of its
1248750397Sobrien     members. Note that as we output the DIEs necessary to represent the
1248850397Sobrien     members of this record or union type, we will also be trying to output
1248950397Sobrien     DIEs to represent the *types* of those members. However the `type'
1249090075Sobrien     function (above) will specifically avoid generating type DIEs for member
1249190075Sobrien     types *within* the list of member DIEs for this (containing) type except
1249250397Sobrien     for those types (of members) which are explicitly marked as also being
1249350397Sobrien     members of this (containing) type themselves.  The g++ front- end can
1249490075Sobrien     force any given type to be treated as a member of some other (containing)
1249590075Sobrien     type by setting the TYPE_CONTEXT of the given (member) type to point to
1249690075Sobrien     the TREE node representing the appropriate (containing) type.  */
1249750397Sobrien
1249850397Sobrien  /* First output info about the base classes.  */
12499169689Skan  if (binfo)
1250050397Sobrien    {
12501169689Skan      VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
1250290075Sobrien      int i;
12503169689Skan      tree base;
1250450397Sobrien
12505169689Skan      for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12506169689Skan	gen_inheritance_die (base,
12507169689Skan			     (accesses ? VEC_index (tree, accesses, i)
12508132718Skan			      : access_public_node), context_die);
1250950397Sobrien    }
1251050397Sobrien
1251150397Sobrien  /* Now output info about the data members and type members.  */
1251250397Sobrien  for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
1251390075Sobrien    {
1251490075Sobrien      /* If we thought we were generating minimal debug info for TYPE
1251590075Sobrien	 and then changed our minds, some of the member declarations
1251690075Sobrien	 may have already been defined.  Don't define them again, but
1251790075Sobrien	 do put them in the right order.  */
1251850397Sobrien
1251990075Sobrien      child = lookup_decl_die (member);
1252090075Sobrien      if (child)
1252190075Sobrien	splice_child_die (context_die, child);
1252290075Sobrien      else
1252390075Sobrien	gen_decl_die (member, context_die);
1252490075Sobrien    }
1252590075Sobrien
1252650397Sobrien  /* Now output info about the function members (if any).  */
1252750397Sobrien  for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
1252890075Sobrien    {
1252990075Sobrien      /* Don't include clones in the member list.  */
1253090075Sobrien      if (DECL_ABSTRACT_ORIGIN (member))
1253190075Sobrien	continue;
1253290075Sobrien
1253390075Sobrien      child = lookup_decl_die (member);
1253490075Sobrien      if (child)
1253590075Sobrien	splice_child_die (context_die, child);
1253690075Sobrien      else
1253790075Sobrien	gen_decl_die (member, context_die);
1253890075Sobrien    }
1253950397Sobrien}
1254050397Sobrien
1254190075Sobrien/* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
1254290075Sobrien   is set, we pretend that the type was never defined, so we only get the
1254390075Sobrien   member DIEs needed by later specification DIEs.  */
1254450397Sobrien
1254550397Sobrienstatic void
12546259268Spfggen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12547259268Spfg				enum debug_info_usage usage)
1254850397Sobrien{
1254990075Sobrien  dw_die_ref type_die = lookup_type_die (type);
1255090075Sobrien  dw_die_ref scope_die = 0;
1255190075Sobrien  int nested = 0;
1255290075Sobrien  int complete = (TYPE_SIZE (type)
1255390075Sobrien		  && (! TYPE_STUB_DECL (type)
1255490075Sobrien		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12555132718Skan  int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12556259268Spfg  complete = complete && should_emit_struct_debug (type, usage);
1255750397Sobrien
1255890075Sobrien  if (type_die && ! complete)
1255950397Sobrien    return;
1256050397Sobrien
1256150397Sobrien  if (TYPE_CONTEXT (type) != NULL_TREE
12562132718Skan      && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12563132718Skan	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
1256450397Sobrien    nested = 1;
1256550397Sobrien
1256650397Sobrien  scope_die = scope_die_for (type, context_die);
1256750397Sobrien
1256850397Sobrien  if (! type_die || (nested && scope_die == comp_unit_die))
1256950397Sobrien    /* First occurrence of type or toplevel definition of nested class.  */
1257050397Sobrien    {
1257190075Sobrien      dw_die_ref old_die = type_die;
1257250397Sobrien
1257350397Sobrien      type_die = new_die (TREE_CODE (type) == RECORD_TYPE
1257450397Sobrien			  ? DW_TAG_structure_type : DW_TAG_union_type,
1257590075Sobrien			  scope_die, type);
1257650397Sobrien      equate_type_number_to_die (type, type_die);
1257750397Sobrien      if (old_die)
12578132718Skan	add_AT_specification (type_die, old_die);
1257990075Sobrien      else
1258090075Sobrien	add_name_attribute (type_die, type_tag (type));
12581261188Spfg      /* APPLE LOCAL begin radar 5811943 - Fix type of pointers to Blocks  */
12582261188Spfg      if (TYPE_BLOCK_IMPL_STRUCT (type))
12583261188Spfg	add_AT_flag (type_die, DW_AT_APPLE_block, 1);
12584261188Spfg      /* APPLE LOCAL end radar 5811943 - Fix type of pointers to Blocks  */
12585261188Spfg     }
1258650397Sobrien  else
1258750397Sobrien    remove_AT (type_die, DW_AT_declaration);
1258850397Sobrien
1258950397Sobrien  /* If this type has been completed, then give it a byte_size attribute and
1259050397Sobrien     then give a list of members.  */
12591132718Skan  if (complete && !ns_decl)
1259250397Sobrien    {
1259390075Sobrien      /* Prevent infinite recursion in cases where the type of some member of
12594132718Skan	 this type is expressed in terms of this type itself.  */
1259550397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1259650397Sobrien      add_byte_size_attribute (type_die, type);
1259750397Sobrien      if (TYPE_STUB_DECL (type) != NULL_TREE)
1259850397Sobrien	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
1259950397Sobrien
1260050397Sobrien      /* If the first reference to this type was as the return type of an
1260150397Sobrien	 inline function, then it may not have a parent.  Fix this now.  */
1260250397Sobrien      if (type_die->die_parent == NULL)
1260350397Sobrien	add_child_die (scope_die, type_die);
1260450397Sobrien
1260550397Sobrien      push_decl_scope (type);
1260650397Sobrien      gen_member_die (type, type_die);
1260750397Sobrien      pop_decl_scope ();
1260850397Sobrien
1260950397Sobrien      /* GNU extension: Record what type our vtable lives in.  */
1261050397Sobrien      if (TYPE_VFIELD (type))
1261150397Sobrien	{
1261250397Sobrien	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
1261350397Sobrien
1261450397Sobrien	  gen_type_die (vtype, context_die);
1261550397Sobrien	  add_AT_die_ref (type_die, DW_AT_containing_type,
1261650397Sobrien			  lookup_type_die (vtype));
1261750397Sobrien	}
1261850397Sobrien    }
1261950397Sobrien  else
1262052284Sobrien    {
1262152284Sobrien      add_AT_flag (type_die, DW_AT_declaration, 1);
1262252284Sobrien
1262390075Sobrien      /* We don't need to do this for function-local types.  */
1262490075Sobrien      if (TYPE_STUB_DECL (type)
1262590075Sobrien	  && ! decl_function_context (TYPE_STUB_DECL (type)))
12626169689Skan	VEC_safe_push (tree, gc, incomplete_types, type);
1262752284Sobrien    }
12628260395Spfg
12629260395Spfg  if (get_AT (type_die, DW_AT_name))
12630260395Spfg    add_pubtype (type, type_die);
1263150397Sobrien}
1263250397Sobrien
1263350397Sobrien/* Generate a DIE for a subroutine _type_.  */
1263450397Sobrien
1263550397Sobrienstatic void
12636132718Skangen_subroutine_type_die (tree type, dw_die_ref context_die)
1263750397Sobrien{
1263890075Sobrien  tree return_type = TREE_TYPE (type);
1263990075Sobrien  dw_die_ref subr_die
1264090075Sobrien    = new_die (DW_TAG_subroutine_type,
1264190075Sobrien	       scope_die_for (type, context_die), type);
1264250397Sobrien
1264350397Sobrien  equate_type_number_to_die (type, subr_die);
1264450397Sobrien  add_prototyped_attribute (subr_die, type);
1264550397Sobrien  add_type_attribute (subr_die, return_type, 0, 0, context_die);
1264650397Sobrien  gen_formal_types_die (type, subr_die);
12647260395Spfg
12648260395Spfg  if (get_AT (subr_die, DW_AT_name))
12649260395Spfg    add_pubtype (type, subr_die);
1265050397Sobrien}
1265150397Sobrien
12652132718Skan/* Generate a DIE for a type definition.  */
1265350397Sobrien
1265450397Sobrienstatic void
12655132718Skangen_typedef_die (tree decl, dw_die_ref context_die)
1265650397Sobrien{
1265790075Sobrien  dw_die_ref type_die;
1265890075Sobrien  tree origin;
1265950397Sobrien
1266050397Sobrien  if (TREE_ASM_WRITTEN (decl))
1266150397Sobrien    return;
1266290075Sobrien
1266350397Sobrien  TREE_ASM_WRITTEN (decl) = 1;
1266490075Sobrien  type_die = new_die (DW_TAG_typedef, context_die, decl);
1266550397Sobrien  origin = decl_ultimate_origin (decl);
1266650397Sobrien  if (origin != NULL)
1266750397Sobrien    add_abstract_origin_attribute (type_die, origin);
1266850397Sobrien  else
1266950397Sobrien    {
1267090075Sobrien      tree type;
1267190075Sobrien
1267250397Sobrien      add_name_and_src_coords_attributes (type_die, decl);
1267350397Sobrien      if (DECL_ORIGINAL_TYPE (decl))
1267450397Sobrien	{
1267550397Sobrien	  type = DECL_ORIGINAL_TYPE (decl);
1267690075Sobrien
12677169689Skan	  gcc_assert (type != TREE_TYPE (decl));
12678169689Skan	  equate_type_number_to_die (TREE_TYPE (decl), type_die);
1267950397Sobrien	}
1268050397Sobrien      else
1268150397Sobrien	type = TREE_TYPE (decl);
1268290075Sobrien
1268350397Sobrien      add_type_attribute (type_die, type, TREE_READONLY (decl),
1268450397Sobrien			  TREE_THIS_VOLATILE (decl), context_die);
1268550397Sobrien    }
1268650397Sobrien
1268750397Sobrien  if (DECL_ABSTRACT (decl))
1268850397Sobrien    equate_decl_number_to_die (decl, type_die);
12689260395Spfg
12690260395Spfg  if (get_AT (type_die, DW_AT_name))
12691260395Spfg    add_pubtype (decl, type_die);
1269250397Sobrien}
1269350397Sobrien
1269450397Sobrien/* Generate a type description DIE.  */
1269550397Sobrien
1269650397Sobrienstatic void
12697259268Spfggen_type_die_with_usage (tree type, dw_die_ref context_die,
12698259268Spfg				enum debug_info_usage usage)
1269950397Sobrien{
1270090075Sobrien  int need_pop;
1270190075Sobrien
1270250397Sobrien  if (type == NULL_TREE || type == error_mark_node)
1270350397Sobrien    return;
1270450397Sobrien
1270550397Sobrien  if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1270650397Sobrien      && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1270790075Sobrien    {
12708117395Skan      if (TREE_ASM_WRITTEN (type))
12709117395Skan	return;
12710117395Skan
12711117395Skan      /* Prevent broken recursion; we can't hand off to the same type.  */
12712169689Skan      gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12713117395Skan
1271450397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1271550397Sobrien      gen_decl_die (TYPE_NAME (type), context_die);
1271650397Sobrien      return;
1271750397Sobrien    }
1271850397Sobrien
12719117395Skan  /* We are going to output a DIE to represent the unqualified version
12720117395Skan     of this type (i.e. without any const or volatile qualifiers) so
12721117395Skan     get the main variant (i.e. the unqualified version) of this type
12722117395Skan     now.  (Vectors are special because the debugging info is in the
12723117395Skan     cloned type itself).  */
12724117395Skan  if (TREE_CODE (type) != VECTOR_TYPE)
12725117395Skan    type = type_main_variant (type);
12726117395Skan
12727117395Skan  if (TREE_ASM_WRITTEN (type))
12728117395Skan    return;
12729117395Skan
1273050397Sobrien  switch (TREE_CODE (type))
1273150397Sobrien    {
1273250397Sobrien    case ERROR_MARK:
1273350397Sobrien      break;
12734261188Spfg	/* APPLE LOCAL radar 5732232 - blocks */
12735261188Spfg    case BLOCK_POINTER_TYPE:
1273650397Sobrien    case POINTER_TYPE:
1273750397Sobrien    case REFERENCE_TYPE:
1273850397Sobrien      /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
1273950397Sobrien	 ensures that the gen_type_die recursion will terminate even if the
1274050397Sobrien	 type is recursive.  Recursive types are possible in Ada.  */
1274150397Sobrien      /* ??? We could perhaps do this for all types before the switch
1274250397Sobrien	 statement.  */
1274350397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1274450397Sobrien
1274550397Sobrien      /* For these types, all that is required is that we output a DIE (or a
12746132718Skan	 set of DIEs) to represent the "basis" type.  */
12747259268Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12748259268Spfg				DINFO_USAGE_IND_USE);
1274950397Sobrien      break;
1275050397Sobrien
1275150397Sobrien    case OFFSET_TYPE:
1275290075Sobrien      /* This code is used for C++ pointer-to-data-member types.
1275350397Sobrien	 Output a description of the relevant class type.  */
12754259268Spfg      gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
12755259268Spfg					DINFO_USAGE_IND_USE);
1275650397Sobrien
1275750397Sobrien      /* Output a description of the type of the object pointed to.  */
12758259268Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12759259268Spfg					DINFO_USAGE_IND_USE);
1276050397Sobrien
1276150397Sobrien      /* Now output a DIE to represent this pointer-to-data-member type
12762132718Skan	 itself.  */
1276350397Sobrien      gen_ptr_to_mbr_type_die (type, context_die);
1276450397Sobrien      break;
1276550397Sobrien
1276650397Sobrien    case FUNCTION_TYPE:
1276750397Sobrien      /* Force out return type (in case it wasn't forced out already).  */
12768259268Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12769259268Spfg					DINFO_USAGE_DIR_USE);
1277050397Sobrien      gen_subroutine_type_die (type, context_die);
1277150397Sobrien      break;
1277250397Sobrien
1277350397Sobrien    case METHOD_TYPE:
1277450397Sobrien      /* Force out return type (in case it wasn't forced out already).  */
12775259268Spfg      gen_type_die_with_usage (TREE_TYPE (type), context_die,
12776259268Spfg					DINFO_USAGE_DIR_USE);
1277750397Sobrien      gen_subroutine_type_die (type, context_die);
1277850397Sobrien      break;
1277950397Sobrien
1278050397Sobrien    case ARRAY_TYPE:
12781169689Skan      gen_array_type_die (type, context_die);
1278250397Sobrien      break;
1278350397Sobrien
1278490075Sobrien    case VECTOR_TYPE:
1278596263Sobrien      gen_array_type_die (type, context_die);
1278690075Sobrien      break;
1278790075Sobrien
1278850397Sobrien    case ENUMERAL_TYPE:
1278950397Sobrien    case RECORD_TYPE:
1279050397Sobrien    case UNION_TYPE:
1279150397Sobrien    case QUAL_UNION_TYPE:
1279290075Sobrien      /* If this is a nested type whose containing class hasn't been written
12793132718Skan	 out yet, writing it out will cover this one, too.  This does not apply
12794132718Skan	 to instantiations of member class templates; they need to be added to
12795132718Skan	 the containing class as they are generated.  FIXME: This hurts the
12796132718Skan	 idea of combining type decls from multiple TUs, since we can't predict
12797132718Skan	 what set of template instantiations we'll get.  */
1279850397Sobrien      if (TYPE_CONTEXT (type)
1279952284Sobrien	  && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
1280050397Sobrien	  && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
1280150397Sobrien	{
12802259268Spfg	  gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
1280350397Sobrien
1280490075Sobrien	  if (TREE_ASM_WRITTEN (type))
1280550397Sobrien	    return;
1280650397Sobrien
1280750397Sobrien	  /* If that failed, attach ourselves to the stub.  */
1280850397Sobrien	  push_decl_scope (TYPE_CONTEXT (type));
1280950397Sobrien	  context_die = lookup_type_die (TYPE_CONTEXT (type));
1281090075Sobrien	  need_pop = 1;
1281150397Sobrien	}
1281290075Sobrien      else
12813132718Skan	{
12814132718Skan	  declare_in_namespace (type, context_die);
12815132718Skan	  need_pop = 0;
12816132718Skan	}
1281750397Sobrien
1281850397Sobrien      if (TREE_CODE (type) == ENUMERAL_TYPE)
12819169689Skan	{
12820169689Skan	  /* This might have been written out by the call to
12821169689Skan	     declare_in_namespace.  */
12822169689Skan	  if (!TREE_ASM_WRITTEN (type))
12823169689Skan	    gen_enumeration_type_die (type, context_die);
12824169689Skan	}
1282550397Sobrien      else
12826259268Spfg	gen_struct_or_union_type_die (type, context_die, usage);
1282750397Sobrien
1282890075Sobrien      if (need_pop)
1282950397Sobrien	pop_decl_scope ();
1283050397Sobrien
1283150397Sobrien      /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
1283250397Sobrien	 it up if it is ever completed.  gen_*_type_die will set it for us
1283350397Sobrien	 when appropriate.  */
1283450397Sobrien      return;
1283550397Sobrien
1283650397Sobrien    case VOID_TYPE:
1283750397Sobrien    case INTEGER_TYPE:
1283850397Sobrien    case REAL_TYPE:
1283950397Sobrien    case COMPLEX_TYPE:
1284050397Sobrien    case BOOLEAN_TYPE:
1284150397Sobrien      /* No DIEs needed for fundamental types.  */
1284250397Sobrien      break;
1284350397Sobrien
1284450397Sobrien    case LANG_TYPE:
1284550397Sobrien      /* No Dwarf representation currently defined.  */
1284650397Sobrien      break;
1284750397Sobrien
1284850397Sobrien    default:
12849169689Skan      gcc_unreachable ();
1285050397Sobrien    }
1285150397Sobrien
1285250397Sobrien  TREE_ASM_WRITTEN (type) = 1;
1285350397Sobrien}
1285450397Sobrien
12855259268Spfgstatic void
12856259268Spfggen_type_die (tree type, dw_die_ref context_die)
12857259268Spfg{
12858259268Spfg  gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
12859259268Spfg}
12860259268Spfg
1286150397Sobrien/* Generate a DIE for a tagged type instantiation.  */
1286250397Sobrien
1286350397Sobrienstatic void
12864132718Skangen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
1286550397Sobrien{
1286650397Sobrien  if (type == NULL_TREE || type == error_mark_node)
1286750397Sobrien    return;
1286850397Sobrien
1286950397Sobrien  /* We are going to output a DIE to represent the unqualified version of
1287050397Sobrien     this type (i.e. without any const or volatile qualifiers) so make sure
1287150397Sobrien     that we have the main variant (i.e. the unqualified version) of this
1287250397Sobrien     type now.  */
12873169689Skan  gcc_assert (type == type_main_variant (type));
1287450397Sobrien
1287590075Sobrien  /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
1287690075Sobrien     an instance of an unresolved type.  */
1287790075Sobrien
1287850397Sobrien  switch (TREE_CODE (type))
1287950397Sobrien    {
1288050397Sobrien    case ERROR_MARK:
1288150397Sobrien      break;
1288250397Sobrien
1288350397Sobrien    case ENUMERAL_TYPE:
1288450397Sobrien      gen_inlined_enumeration_type_die (type, context_die);
1288550397Sobrien      break;
1288650397Sobrien
1288750397Sobrien    case RECORD_TYPE:
1288850397Sobrien      gen_inlined_structure_type_die (type, context_die);
1288950397Sobrien      break;
1289050397Sobrien
1289150397Sobrien    case UNION_TYPE:
1289250397Sobrien    case QUAL_UNION_TYPE:
1289350397Sobrien      gen_inlined_union_type_die (type, context_die);
1289450397Sobrien      break;
1289550397Sobrien
1289650397Sobrien    default:
12897169689Skan      gcc_unreachable ();
1289850397Sobrien    }
1289950397Sobrien}
1290050397Sobrien
1290150397Sobrien/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
1290250397Sobrien   things which are local to the given block.  */
1290350397Sobrien
1290450397Sobrienstatic void
12905132718Skangen_block_die (tree stmt, dw_die_ref context_die, int depth)
1290650397Sobrien{
1290790075Sobrien  int must_output_die = 0;
1290890075Sobrien  tree origin;
1290990075Sobrien  tree decl;
1291090075Sobrien  enum tree_code origin_code;
1291150397Sobrien
12912169689Skan  /* Ignore blocks that are NULL.  */
12913169689Skan  if (stmt == NULL_TREE)
1291450397Sobrien    return;
1291550397Sobrien
1291690075Sobrien  /* If the block is one fragment of a non-contiguous block, do not
1291790075Sobrien     process the variables, since they will have been done by the
1291890075Sobrien     origin block.  Do process subblocks.  */
1291990075Sobrien  if (BLOCK_FRAGMENT_ORIGIN (stmt))
1292090075Sobrien    {
1292190075Sobrien      tree sub;
1292290075Sobrien
1292390075Sobrien      for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
1292490075Sobrien	gen_block_die (sub, context_die, depth + 1);
1292590075Sobrien
1292690075Sobrien      return;
1292790075Sobrien    }
1292890075Sobrien
1292950397Sobrien  /* Determine the "ultimate origin" of this block.  This block may be an
1293050397Sobrien     inlined instance of an inlined instance of inline function, so we have
1293150397Sobrien     to trace all of the way back through the origin chain to find out what
1293250397Sobrien     sort of node actually served as the original seed for the creation of
1293350397Sobrien     the current block.  */
1293450397Sobrien  origin = block_ultimate_origin (stmt);
1293550397Sobrien  origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
1293650397Sobrien
1293750397Sobrien  /* Determine if we need to output any Dwarf DIEs at all to represent this
1293850397Sobrien     block.  */
1293950397Sobrien  if (origin_code == FUNCTION_DECL)
1294050397Sobrien    /* The outer scopes for inlinings *must* always be represented.  We
1294150397Sobrien       generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
1294250397Sobrien    must_output_die = 1;
1294350397Sobrien  else
1294450397Sobrien    {
1294550397Sobrien      /* In the case where the current block represents an inlining of the
12946132718Skan	 "body block" of an inline function, we must *NOT* output any DIE for
12947132718Skan	 this block because we have already output a DIE to represent the whole
12948132718Skan	 inlined function scope and the "body block" of any function doesn't
12949132718Skan	 really represent a different scope according to ANSI C rules.  So we
12950132718Skan	 check here to make sure that this block does not represent a "body
12951132718Skan	 block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
1295250397Sobrien      if (! is_body_block (origin ? origin : stmt))
1295350397Sobrien	{
1295450397Sobrien	  /* Determine if this block directly contains any "significant"
1295550397Sobrien	     local declarations which we will need to output DIEs for.  */
1295650397Sobrien	  if (debug_info_level > DINFO_LEVEL_TERSE)
1295750397Sobrien	    /* We are not in terse mode so *any* local declaration counts
1295850397Sobrien	       as being a "significant" one.  */
12959169689Skan	    must_output_die = (BLOCK_VARS (stmt) != NULL
12960169689Skan			       && (TREE_USED (stmt)
12961169689Skan				   || TREE_ASM_WRITTEN (stmt)
12962169689Skan				   || BLOCK_ABSTRACT (stmt)));
1296350397Sobrien	  else
1296450397Sobrien	    /* We are in terse mode, so only local (nested) function
1296550397Sobrien	       definitions count as "significant" local declarations.  */
1296650397Sobrien	    for (decl = BLOCK_VARS (stmt);
1296750397Sobrien		 decl != NULL; decl = TREE_CHAIN (decl))
1296850397Sobrien	      if (TREE_CODE (decl) == FUNCTION_DECL
1296950397Sobrien		  && DECL_INITIAL (decl))
1297050397Sobrien		{
1297150397Sobrien		  must_output_die = 1;
1297250397Sobrien		  break;
1297350397Sobrien		}
1297450397Sobrien	}
1297550397Sobrien    }
1297650397Sobrien
1297750397Sobrien  /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
1297850397Sobrien     DIE for any block which contains no significant local declarations at
1297950397Sobrien     all.  Rather, in such cases we just call `decls_for_scope' so that any
1298050397Sobrien     needed Dwarf info for any sub-blocks will get properly generated. Note
1298150397Sobrien     that in terse mode, our definition of what constitutes a "significant"
1298250397Sobrien     local declaration gets restricted to include only inlined function
1298350397Sobrien     instances and local (nested) function definitions.  */
1298450397Sobrien  if (must_output_die)
1298550397Sobrien    {
1298650397Sobrien      if (origin_code == FUNCTION_DECL)
1298750397Sobrien	gen_inlined_subroutine_die (stmt, context_die, depth);
1298850397Sobrien      else
1298950397Sobrien	gen_lexical_block_die (stmt, context_die, depth);
1299050397Sobrien    }
1299150397Sobrien  else
1299250397Sobrien    decls_for_scope (stmt, context_die, depth);
1299350397Sobrien}
1299450397Sobrien
1299550397Sobrien/* Generate all of the decls declared within a given scope and (recursively)
1299650397Sobrien   all of its sub-blocks.  */
1299750397Sobrien
1299850397Sobrienstatic void
12999132718Skandecls_for_scope (tree stmt, dw_die_ref context_die, int depth)
1300050397Sobrien{
1300190075Sobrien  tree decl;
1300290075Sobrien  tree subblocks;
1300350397Sobrien
13004169689Skan  /* Ignore NULL blocks.  */
13005169689Skan  if (stmt == NULL_TREE)
1300650397Sobrien    return;
1300750397Sobrien
13008169689Skan  if (TREE_USED (stmt))
1300950397Sobrien    {
13010169689Skan      /* Output the DIEs to represent all of the data objects and typedefs
13011169689Skan	 declared directly within this block but not within any nested
13012169689Skan	 sub-blocks.  Also, nested function and tag DIEs have been
13013169689Skan	 generated with a parent of NULL; fix that up now.  */
13014169689Skan      for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13015169689Skan	{
13016169689Skan	  dw_die_ref die;
13017169689Skan
13018169689Skan	  if (TREE_CODE (decl) == FUNCTION_DECL)
13019169689Skan	    die = lookup_decl_die (decl);
13020169689Skan	  else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13021169689Skan	    die = lookup_type_die (TREE_TYPE (decl));
13022169689Skan	  else
13023169689Skan	    die = NULL;
13024169689Skan
13025169689Skan	  if (die != NULL && die->die_parent == NULL)
13026169689Skan	    add_child_die (context_die, die);
13027169689Skan	  /* Do not produce debug information for static variables since
13028169689Skan	     these might be optimized out.  We are called for these later
13029169689Skan	     in cgraph_varpool_analyze_pending_decls. */
13030169689Skan	  if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13031169689Skan	    ;
13032169689Skan	  else
13033169689Skan	    gen_decl_die (decl, context_die);
13034169689Skan	}
1303550397Sobrien    }
1303650397Sobrien
13037117395Skan  /* If we're at -g1, we're not interested in subblocks.  */
13038117395Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13039117395Skan    return;
13040117395Skan
1304150397Sobrien  /* Output the DIEs to represent all sub-blocks (and the items declared
1304250397Sobrien     therein) of this block.  */
1304350397Sobrien  for (subblocks = BLOCK_SUBBLOCKS (stmt);
1304450397Sobrien       subblocks != NULL;
1304550397Sobrien       subblocks = BLOCK_CHAIN (subblocks))
1304650397Sobrien    gen_block_die (subblocks, context_die, depth + 1);
1304750397Sobrien}
1304850397Sobrien
1304950397Sobrien/* Is this a typedef we can avoid emitting?  */
1305050397Sobrien
1305150397Sobrienstatic inline int
13052132718Skanis_redundant_typedef (tree decl)
1305350397Sobrien{
1305450397Sobrien  if (TYPE_DECL_IS_STUB (decl))
1305550397Sobrien    return 1;
1305650397Sobrien
1305750397Sobrien  if (DECL_ARTIFICIAL (decl)
1305850397Sobrien      && DECL_CONTEXT (decl)
1305950397Sobrien      && is_tagged_type (DECL_CONTEXT (decl))
1306050397Sobrien      && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
1306150397Sobrien      && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
1306250397Sobrien    /* Also ignore the artificial member typedef for the class name.  */
1306350397Sobrien    return 1;
1306450397Sobrien
1306550397Sobrien  return 0;
1306650397Sobrien}
1306750397Sobrien
13068169689Skan/* Returns the DIE for decl.  A DIE will always be returned.  */
13069132718Skan
13070169689Skanstatic dw_die_ref
13071169689Skanforce_decl_die (tree decl)
13072169689Skan{
13073169689Skan  dw_die_ref decl_die;
13074169689Skan  unsigned saved_external_flag;
13075169689Skan  tree save_fn = NULL_TREE;
13076169689Skan  decl_die = lookup_decl_die (decl);
13077169689Skan  if (!decl_die)
13078169689Skan    {
13079169689Skan      dw_die_ref context_die;
13080169689Skan      tree decl_context = DECL_CONTEXT (decl);
13081169689Skan      if (decl_context)
13082169689Skan	{
13083169689Skan	  /* Find die that represents this context.  */
13084169689Skan	  if (TYPE_P (decl_context))
13085169689Skan	    context_die = force_type_die (decl_context);
13086169689Skan	  else
13087169689Skan	    context_die = force_decl_die (decl_context);
13088169689Skan	}
13089169689Skan      else
13090169689Skan	context_die = comp_unit_die;
13091132718Skan
13092169689Skan      decl_die = lookup_decl_die (decl);
13093169689Skan      if (decl_die)
13094169689Skan	return decl_die;
13095169689Skan
13096169689Skan      switch (TREE_CODE (decl))
13097169689Skan	{
13098169689Skan	case FUNCTION_DECL:
13099169689Skan	  /* Clear current_function_decl, so that gen_subprogram_die thinks
13100169689Skan	     that this is a declaration. At this point, we just want to force
13101169689Skan	     declaration die.  */
13102169689Skan	  save_fn = current_function_decl;
13103169689Skan	  current_function_decl = NULL_TREE;
13104169689Skan	  gen_subprogram_die (decl, context_die);
13105169689Skan	  current_function_decl = save_fn;
13106169689Skan	  break;
13107169689Skan
13108169689Skan	case VAR_DECL:
13109169689Skan	  /* Set external flag to force declaration die. Restore it after
13110169689Skan	   gen_decl_die() call.  */
13111169689Skan	  saved_external_flag = DECL_EXTERNAL (decl);
13112169689Skan	  DECL_EXTERNAL (decl) = 1;
13113169689Skan	  gen_decl_die (decl, context_die);
13114169689Skan	  DECL_EXTERNAL (decl) = saved_external_flag;
13115169689Skan	  break;
13116169689Skan
13117169689Skan	case NAMESPACE_DECL:
13118169689Skan	  dwarf2out_decl (decl);
13119169689Skan	  break;
13120169689Skan
13121169689Skan	default:
13122169689Skan	  gcc_unreachable ();
13123169689Skan	}
13124169689Skan
13125169689Skan      /* We should be able to find the DIE now.  */
13126169689Skan      if (!decl_die)
13127169689Skan	decl_die = lookup_decl_die (decl);
13128169689Skan      gcc_assert (decl_die);
13129169689Skan    }
13130169689Skan
13131169689Skan  return decl_die;
13132169689Skan}
13133169689Skan
13134169689Skan/* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13135169689Skan   always returned.  */
13136169689Skan
13137132718Skanstatic dw_die_ref
13138169689Skanforce_type_die (tree type)
13139132718Skan{
13140169689Skan  dw_die_ref type_die;
13141132718Skan
13142169689Skan  type_die = lookup_type_die (type);
13143169689Skan  if (!type_die)
13144169689Skan    {
13145169689Skan      dw_die_ref context_die;
13146169689Skan      if (TYPE_CONTEXT (type))
13147169689Skan	{
13148169689Skan	  if (TYPE_P (TYPE_CONTEXT (type)))
13149169689Skan	    context_die = force_type_die (TYPE_CONTEXT (type));
13150169689Skan	  else
13151169689Skan	    context_die = force_decl_die (TYPE_CONTEXT (type));
13152169689Skan	}
13153169689Skan      else
13154169689Skan	context_die = comp_unit_die;
13155132718Skan
13156169689Skan      type_die = lookup_type_die (type);
13157169689Skan      if (type_die)
13158169689Skan	return type_die;
13159169689Skan      gen_type_die (type, context_die);
13160169689Skan      type_die = lookup_type_die (type);
13161169689Skan      gcc_assert (type_die);
13162169689Skan    }
13163169689Skan  return type_die;
13164132718Skan}
13165132718Skan
13166132718Skan/* Force out any required namespaces to be able to output DECL,
13167132718Skan   and return the new context_die for it, if it's changed.  */
13168132718Skan
13169132718Skanstatic dw_die_ref
13170132718Skansetup_namespace_context (tree thing, dw_die_ref context_die)
13171132718Skan{
13172169689Skan  tree context = (DECL_P (thing)
13173169689Skan		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13174132718Skan  if (context && TREE_CODE (context) == NAMESPACE_DECL)
13175132718Skan    /* Force out the namespace.  */
13176169689Skan    context_die = force_decl_die (context);
13177132718Skan
13178132718Skan  return context_die;
13179132718Skan}
13180132718Skan
13181132718Skan/* Emit a declaration DIE for THING (which is either a DECL or a tagged
13182132718Skan   type) within its namespace, if appropriate.
13183132718Skan
13184132718Skan   For compatibility with older debuggers, namespace DIEs only contain
13185132718Skan   declarations; all definitions are emitted at CU scope.  */
13186132718Skan
13187132718Skanstatic void
13188132718Skandeclare_in_namespace (tree thing, dw_die_ref context_die)
13189132718Skan{
13190132718Skan  dw_die_ref ns_context;
13191132718Skan
13192132718Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13193132718Skan    return;
13194132718Skan
13195169689Skan  /* If this decl is from an inlined function, then don't try to emit it in its
13196169689Skan     namespace, as we will get confused.  It would have already been emitted
13197169689Skan     when the abstract instance of the inline function was emitted anyways.  */
13198169689Skan  if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13199169689Skan    return;
13200169689Skan
13201132718Skan  ns_context = setup_namespace_context (thing, context_die);
13202132718Skan
13203132718Skan  if (ns_context != context_die)
13204132718Skan    {
13205132718Skan      if (DECL_P (thing))
13206132718Skan	gen_decl_die (thing, ns_context);
13207132718Skan      else
13208132718Skan	gen_type_die (thing, ns_context);
13209132718Skan    }
13210132718Skan}
13211132718Skan
13212132718Skan/* Generate a DIE for a namespace or namespace alias.  */
13213132718Skan
13214132718Skanstatic void
13215132718Skangen_namespace_die (tree decl)
13216132718Skan{
13217132718Skan  dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13218132718Skan
13219132718Skan  /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13220132718Skan     they are an alias of.  */
13221132718Skan  if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13222132718Skan    {
13223132718Skan      /* Output a real namespace.  */
13224132718Skan      dw_die_ref namespace_die
13225132718Skan	= new_die (DW_TAG_namespace, context_die, decl);
13226132718Skan      add_name_and_src_coords_attributes (namespace_die, decl);
13227132718Skan      equate_decl_number_to_die (decl, namespace_die);
13228132718Skan    }
13229132718Skan  else
13230132718Skan    {
13231132718Skan      /* Output a namespace alias.  */
13232132718Skan
13233132718Skan      /* Force out the namespace we are an alias of, if necessary.  */
13234132718Skan      dw_die_ref origin_die
13235169689Skan	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13236132718Skan
13237132718Skan      /* Now create the namespace alias DIE.  */
13238132718Skan      dw_die_ref namespace_die
13239132718Skan	= new_die (DW_TAG_imported_declaration, context_die, decl);
13240132718Skan      add_name_and_src_coords_attributes (namespace_die, decl);
13241132718Skan      add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13242132718Skan      equate_decl_number_to_die (decl, namespace_die);
13243132718Skan    }
13244132718Skan}
13245132718Skan
1324650397Sobrien/* Generate Dwarf debug information for a decl described by DECL.  */
1324750397Sobrien
1324850397Sobrienstatic void
13249132718Skangen_decl_die (tree decl, dw_die_ref context_die)
1325050397Sobrien{
1325190075Sobrien  tree origin;
1325250397Sobrien
1325390075Sobrien  if (DECL_P (decl) && DECL_IGNORED_P (decl))
1325450397Sobrien    return;
1325550397Sobrien
1325650397Sobrien  switch (TREE_CODE (decl))
1325750397Sobrien    {
1325890075Sobrien    case ERROR_MARK:
1325990075Sobrien      break;
1326090075Sobrien
1326150397Sobrien    case CONST_DECL:
1326290075Sobrien      /* The individual enumerators of an enum type get output when we output
13263132718Skan	 the Dwarf representation of the relevant enum type itself.  */
1326450397Sobrien      break;
1326550397Sobrien
1326650397Sobrien    case FUNCTION_DECL:
1326750397Sobrien      /* Don't output any DIEs to represent mere function declarations,
1326850397Sobrien	 unless they are class members or explicit block externs.  */
1326950397Sobrien      if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
1327090075Sobrien	  && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
1327150397Sobrien	break;
1327250397Sobrien
13273169689Skan#if 0
13274169689Skan      /* FIXME */
13275169689Skan      /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13276169689Skan	 on local redeclarations of global functions.  That seems broken.  */
13277169689Skan      if (current_function_decl != decl)
13278169689Skan	/* This is only a declaration.  */;
13279169689Skan#endif
13280169689Skan
1328190075Sobrien      /* If we're emitting a clone, emit info for the abstract instance.  */
1328290075Sobrien      if (DECL_ORIGIN (decl) != decl)
1328390075Sobrien	dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
1328490075Sobrien
1328590075Sobrien      /* If we're emitting an out-of-line copy of an inline function,
1328690075Sobrien	 emit info for the abstract instance and set up to refer to it.  */
13287132718Skan      else if (cgraph_function_possibly_inlined_p (decl)
13288132718Skan	       && ! DECL_ABSTRACT (decl)
13289132718Skan	       && ! class_or_namespace_scope_p (context_die)
1329090075Sobrien	       /* dwarf2out_abstract_function won't emit a die if this is just
1329190075Sobrien		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
1329290075Sobrien		  that case, because that works only if we have a die.  */
1329390075Sobrien	       && DECL_INITIAL (decl) != NULL_TREE)
1329450397Sobrien	{
1329590075Sobrien	  dwarf2out_abstract_function (decl);
1329690075Sobrien	  set_decl_origin_self (decl);
1329790075Sobrien	}
1329890075Sobrien
1329990075Sobrien      /* Otherwise we're emitting the primary DIE for this decl.  */
1330090075Sobrien      else if (debug_info_level > DINFO_LEVEL_TERSE)
1330190075Sobrien	{
1330250397Sobrien	  /* Before we describe the FUNCTION_DECL itself, make sure that we
1330350397Sobrien	     have described its return type.  */
1330450397Sobrien	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
1330550397Sobrien
1330690075Sobrien	  /* And its virtual context.  */
1330790075Sobrien	  if (DECL_VINDEX (decl) != NULL_TREE)
1330890075Sobrien	    gen_type_die (DECL_CONTEXT (decl), context_die);
1330990075Sobrien
1331050397Sobrien	  /* And its containing type.  */
1331150397Sobrien	  origin = decl_class_context (decl);
1331250397Sobrien	  if (origin != NULL_TREE)
1331390075Sobrien	    gen_type_die_for_member (origin, decl, context_die);
13314132718Skan
13315132718Skan	  /* And its containing namespace.  */
13316132718Skan	  declare_in_namespace (decl, context_die);
1331750397Sobrien	}
1331850397Sobrien
1331950397Sobrien      /* Now output a DIE to represent the function itself.  */
1332050397Sobrien      gen_subprogram_die (decl, context_die);
1332150397Sobrien      break;
1332250397Sobrien
1332350397Sobrien    case TYPE_DECL:
1332450397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13325132718Skan	 actual typedefs.  */
1332650397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1332750397Sobrien	break;
1332850397Sobrien
1332990075Sobrien      /* In the special case of a TYPE_DECL node representing the declaration
13330132718Skan	 of some type tag, if the given TYPE_DECL is marked as having been
13331132718Skan	 instantiated from some other (original) TYPE_DECL node (e.g. one which
13332132718Skan	 was generated within the original definition of an inline function) we
13333132718Skan	 have to generate a special (abbreviated) DW_TAG_structure_type,
13334132718Skan	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13335220150Smm      if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13336220150Smm	  && is_tagged_type (TREE_TYPE (decl)))
1333750397Sobrien	{
1333850397Sobrien	  gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
1333950397Sobrien	  break;
1334050397Sobrien	}
1334150397Sobrien
1334250397Sobrien      if (is_redundant_typedef (decl))
1334350397Sobrien	gen_type_die (TREE_TYPE (decl), context_die);
1334450397Sobrien      else
1334550397Sobrien	/* Output a DIE to represent the typedef itself.  */
1334650397Sobrien	gen_typedef_die (decl, context_die);
1334750397Sobrien      break;
1334850397Sobrien
1334950397Sobrien    case LABEL_DECL:
1335050397Sobrien      if (debug_info_level >= DINFO_LEVEL_NORMAL)
1335150397Sobrien	gen_label_die (decl, context_die);
1335250397Sobrien      break;
1335350397Sobrien
1335450397Sobrien    case VAR_DECL:
13355169689Skan    case RESULT_DECL:
1335650397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13357132718Skan	 variable declarations or definitions.  */
1335850397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1335950397Sobrien	break;
1336050397Sobrien
1336150397Sobrien      /* Output any DIEs that are needed to specify the type of this data
13362132718Skan	 object.  */
1336350397Sobrien      gen_type_die (TREE_TYPE (decl), context_die);
1336450397Sobrien
1336550397Sobrien      /* And its containing type.  */
1336650397Sobrien      origin = decl_class_context (decl);
1336750397Sobrien      if (origin != NULL_TREE)
1336890075Sobrien	gen_type_die_for_member (origin, decl, context_die);
1336950397Sobrien
13370132718Skan      /* And its containing namespace.  */
13371132718Skan      declare_in_namespace (decl, context_die);
13372132718Skan
1337350397Sobrien      /* Now output the DIE to represent the data object itself.  This gets
13374132718Skan	 complicated because of the possibility that the VAR_DECL really
13375132718Skan	 represents an inlined instance of a formal parameter for an inline
13376132718Skan	 function.  */
1337750397Sobrien      origin = decl_ultimate_origin (decl);
1337850397Sobrien      if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
1337950397Sobrien	gen_formal_parameter_die (decl, context_die);
1338050397Sobrien      else
1338150397Sobrien	gen_variable_die (decl, context_die);
1338250397Sobrien      break;
1338350397Sobrien
1338450397Sobrien    case FIELD_DECL:
1338590075Sobrien      /* Ignore the nameless fields that are used to skip bits but handle C++
13386169689Skan	 anonymous unions and structs.  */
1338750397Sobrien      if (DECL_NAME (decl) != NULL_TREE
13388169689Skan	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13389169689Skan	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
1339050397Sobrien	{
1339150397Sobrien	  gen_type_die (member_declared_type (decl), context_die);
1339250397Sobrien	  gen_field_die (decl, context_die);
1339350397Sobrien	}
1339450397Sobrien      break;
1339550397Sobrien
1339650397Sobrien    case PARM_DECL:
1339750397Sobrien      gen_type_die (TREE_TYPE (decl), context_die);
1339850397Sobrien      gen_formal_parameter_die (decl, context_die);
1339950397Sobrien      break;
1340050397Sobrien
1340190075Sobrien    case NAMESPACE_DECL:
13402132718Skan      gen_namespace_die (decl);
1340390075Sobrien      break;
1340490075Sobrien
1340550397Sobrien    default:
13406169689Skan      /* Probably some frontend-internal decl.  Assume we don't care.  */
13407169689Skan      gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13408169689Skan      break;
1340950397Sobrien    }
1341050397Sobrien}
1341150397Sobrien
1341290075Sobrien/* Output debug information for global decl DECL.  Called from toplev.c after
1341390075Sobrien   compilation proper has finished.  */
1341490075Sobrien
1341590075Sobrienstatic void
13416132718Skandwarf2out_global_decl (tree decl)
1341790075Sobrien{
1341890075Sobrien  /* Output DWARF2 information for file-scope tentative data object
1341990075Sobrien     declarations, file-scope (extern) function declarations (which had no
1342090075Sobrien     corresponding body) and file-scope tagged type declarations and
1342190075Sobrien     definitions which have not yet been forced out.  */
1342290075Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
1342390075Sobrien    dwarf2out_decl (decl);
1342490075Sobrien}
1342590075Sobrien
13426169689Skan/* Output debug information for type decl DECL.  Called from toplev.c
13427169689Skan   and from language front ends (to record built-in types).  */
13428169689Skanstatic void
13429169689Skandwarf2out_type_decl (tree decl, int local)
13430169689Skan{
13431169689Skan  if (!local)
13432169689Skan    dwarf2out_decl (decl);
13433169689Skan}
13434169689Skan
13435169689Skan/* Output debug information for imported module or decl.  */
13436169689Skan
13437169689Skanstatic void
13438169689Skandwarf2out_imported_module_or_decl (tree decl, tree context)
13439169689Skan{
13440169689Skan  dw_die_ref imported_die, at_import_die;
13441169689Skan  dw_die_ref scope_die;
13442169689Skan  expanded_location xloc;
13443169689Skan
13444169689Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13445169689Skan    return;
13446169689Skan
13447169689Skan  gcc_assert (decl);
13448169689Skan
13449169689Skan  /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13450169689Skan     We need decl DIE for reference and scope die. First, get DIE for the decl
13451169689Skan     itself.  */
13452169689Skan
13453169689Skan  /* Get the scope die for decl context. Use comp_unit_die for global module
13454169689Skan     or decl. If die is not found for non globals, force new die.  */
13455169689Skan  if (!context)
13456169689Skan    scope_die = comp_unit_die;
13457169689Skan  else if (TYPE_P (context))
13458259268Spfg    {
13459259268Spfg      if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13460259268Spfg	return;
13461169689Skan    scope_die = force_type_die (context);
13462259268Spfg    }
13463169689Skan  else
13464169689Skan    scope_die = force_decl_die (context);
13465169689Skan
13466169689Skan  /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13467169689Skan  if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13468169689Skan    {
13469169689Skan      if (is_base_type (TREE_TYPE (decl)))
13470169689Skan	at_import_die = base_type_die (TREE_TYPE (decl));
13471169689Skan      else
13472169689Skan	at_import_die = force_type_die (TREE_TYPE (decl));
13473169689Skan    }
13474169689Skan  else
13475169689Skan    {
13476169689Skan      at_import_die = lookup_decl_die (decl);
13477169689Skan      if (!at_import_die)
13478169689Skan	{
13479169689Skan	  /* If we're trying to avoid duplicate debug info, we may not have
13480169689Skan	     emitted the member decl for this field.  Emit it now.  */
13481169689Skan	  if (TREE_CODE (decl) == FIELD_DECL)
13482169689Skan	    {
13483169689Skan	      tree type = DECL_CONTEXT (decl);
13484169689Skan	      dw_die_ref type_context_die;
13485169689Skan
13486169689Skan	      if (TYPE_CONTEXT (type))
13487169689Skan		if (TYPE_P (TYPE_CONTEXT (type)))
13488259268Spfg		  {
13489259268Spfg		    if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13490259268Spfg						   DINFO_USAGE_DIR_USE))
13491259268Spfg		      return;
13492169689Skan		  type_context_die = force_type_die (TYPE_CONTEXT (type));
13493259268Spfg		  }
13494169689Skan	      else
13495169689Skan		type_context_die = force_decl_die (TYPE_CONTEXT (type));
13496169689Skan	      else
13497169689Skan		type_context_die = comp_unit_die;
13498169689Skan	      gen_type_die_for_member (type, decl, type_context_die);
13499169689Skan	    }
13500169689Skan	  at_import_die = force_decl_die (decl);
13501169689Skan	}
13502169689Skan    }
13503169689Skan
13504169689Skan  /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13505169689Skan  if (TREE_CODE (decl) == NAMESPACE_DECL)
13506169689Skan    imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13507169689Skan  else
13508169689Skan    imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13509169689Skan
13510169689Skan  xloc = expand_location (input_location);
13511169689Skan  add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13512169689Skan  add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13513169689Skan  add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13514169689Skan}
13515169689Skan
1351690075Sobrien/* Write the debugging output for DECL.  */
1351790075Sobrien
1351890075Sobrienvoid
13519132718Skandwarf2out_decl (tree decl)
1352090075Sobrien{
1352190075Sobrien  dw_die_ref context_die = comp_unit_die;
1352290075Sobrien
1352350397Sobrien  switch (TREE_CODE (decl))
1352450397Sobrien    {
1352590075Sobrien    case ERROR_MARK:
1352690075Sobrien      return;
1352790075Sobrien
1352850397Sobrien    case FUNCTION_DECL:
1352950397Sobrien      /* What we would really like to do here is to filter out all mere
13530132718Skan	 file-scope declarations of file-scope functions which are never
13531132718Skan	 referenced later within this translation unit (and keep all of ones
13532132718Skan	 that *are* referenced later on) but we aren't clairvoyant, so we have
13533132718Skan	 no idea which functions will be referenced in the future (i.e. later
13534132718Skan	 on within the current translation unit). So here we just ignore all
13535132718Skan	 file-scope function declarations which are not also definitions.  If
13536132718Skan	 and when the debugger needs to know something about these functions,
13537132718Skan	 it will have to hunt around and find the DWARF information associated
13538132718Skan	 with the definition of the function.
1353990075Sobrien
1354090075Sobrien	 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13541132718Skan	 nodes represent definitions and which ones represent mere
13542132718Skan	 declarations.  We have to check DECL_INITIAL instead. That's because
13543132718Skan	 the C front-end supports some weird semantics for "extern inline"
13544132718Skan	 function definitions.  These can get inlined within the current
13545169689Skan	 translation unit (and thus, we need to generate Dwarf info for their
13546132718Skan	 abstract instances so that the Dwarf info for the concrete inlined
13547132718Skan	 instances can have something to refer to) but the compiler never
13548132718Skan	 generates any out-of-lines instances of such things (despite the fact
13549132718Skan	 that they *are* definitions).
1355090075Sobrien
1355190075Sobrien	 The important point is that the C front-end marks these "extern
1355290075Sobrien	 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
1355390075Sobrien	 them anyway. Note that the C++ front-end also plays some similar games
1355490075Sobrien	 for inline function definitions appearing within include files which
1355590075Sobrien	 also contain `#pragma interface' pragmas.  */
1355650397Sobrien      if (DECL_INITIAL (decl) == NULL_TREE)
1355750397Sobrien	return;
1355850397Sobrien
1355950397Sobrien      /* If we're a nested function, initially use a parent of NULL; if we're
1356050397Sobrien	 a plain function, this will be fixed up in decls_for_scope.  If
1356150397Sobrien	 we're a method, it will be ignored, since we already have a DIE.  */
13562117395Skan      if (decl_function_context (decl)
13563117395Skan	  /* But if we're in terse mode, we don't care about scope.  */
13564117395Skan	  && debug_info_level > DINFO_LEVEL_TERSE)
1356550397Sobrien	context_die = NULL;
1356650397Sobrien      break;
1356750397Sobrien
1356850397Sobrien    case VAR_DECL:
1356990075Sobrien      /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13570132718Skan	 declaration and if the declaration was never even referenced from
13571132718Skan	 within this entire compilation unit.  We suppress these DIEs in
13572132718Skan	 order to save space in the .debug section (by eliminating entries
13573132718Skan	 which are probably useless).  Note that we must not suppress
13574132718Skan	 block-local extern declarations (whether used or not) because that
13575132718Skan	 would screw-up the debugger's name lookup mechanism and cause it to
13576132718Skan	 miss things which really ought to be in scope at a given point.  */
1357750397Sobrien      if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
1357850397Sobrien	return;
1357950397Sobrien
13580169689Skan      /* For local statics lookup proper context die.  */
13581169689Skan      if (TREE_STATIC (decl) && decl_function_context (decl))
13582169689Skan	context_die = lookup_decl_die (DECL_CONTEXT (decl));
13583169689Skan
1358450397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13585132718Skan	 variable declarations or definitions.  */
1358650397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1358750397Sobrien	return;
1358850397Sobrien      break;
1358950397Sobrien
13590132718Skan    case NAMESPACE_DECL:
13591132718Skan      if (debug_info_level <= DINFO_LEVEL_TERSE)
13592132718Skan	return;
13593132718Skan      if (lookup_decl_die (decl) != NULL)
13594132718Skan        return;
13595132718Skan      break;
13596132718Skan
1359750397Sobrien    case TYPE_DECL:
1359890075Sobrien      /* Don't emit stubs for types unless they are needed by other DIEs.  */
1359990075Sobrien      if (TYPE_DECL_SUPPRESS_DEBUG (decl))
1360090075Sobrien	return;
1360190075Sobrien
1360250397Sobrien      /* Don't bother trying to generate any DIEs to represent any of the
13603132718Skan	 normal built-in types for the language we are compiling.  */
13604169689Skan      if (DECL_IS_BUILTIN (decl))
1360550397Sobrien	{
1360650397Sobrien	  /* OK, we need to generate one for `bool' so GDB knows what type
13607132718Skan	     comparisons have.  */
13608169689Skan	  if (is_cxx ()
1360990075Sobrien	      && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
1361090075Sobrien	      && ! DECL_IGNORED_P (decl))
1361150397Sobrien	    modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
1361250397Sobrien
1361350397Sobrien	  return;
1361450397Sobrien	}
1361550397Sobrien
1361650397Sobrien      /* If we are in terse mode, don't generate any DIEs for types.  */
1361750397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1361850397Sobrien	return;
1361950397Sobrien
1362050397Sobrien      /* If we're a function-scope tag, initially use a parent of NULL;
1362150397Sobrien	 this will be fixed up in decls_for_scope.  */
1362250397Sobrien      if (decl_function_context (decl))
1362350397Sobrien	context_die = NULL;
1362450397Sobrien
1362550397Sobrien      break;
1362650397Sobrien
1362750397Sobrien    default:
1362850397Sobrien      return;
1362950397Sobrien    }
1363050397Sobrien
1363150397Sobrien  gen_decl_die (decl, context_die);
1363250397Sobrien}
1363350397Sobrien
1363450397Sobrien/* Output a marker (i.e. a label) for the beginning of the generated code for
1363550397Sobrien   a lexical block.  */
1363650397Sobrien
1363790075Sobrienstatic void
13638132718Skandwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13639132718Skan		       unsigned int blocknum)
1364050397Sobrien{
13641169689Skan  switch_to_section (current_function_section ());
1364290075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
1364350397Sobrien}
1364450397Sobrien
1364550397Sobrien/* Output a marker (i.e. a label) for the end of the generated code for a
1364650397Sobrien   lexical block.  */
1364750397Sobrien
1364890075Sobrienstatic void
13649132718Skandwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
1365050397Sobrien{
13651169689Skan  switch_to_section (current_function_section ());
1365290075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
1365350397Sobrien}
1365450397Sobrien
1365590075Sobrien/* Returns nonzero if it is appropriate not to emit any debugging
1365690075Sobrien   information for BLOCK, because it doesn't contain any instructions.
1365750397Sobrien
1365890075Sobrien   Don't allow this for blocks with nested functions or local classes
1365990075Sobrien   as we would end up with orphans, and in the presence of scheduling
1366090075Sobrien   we may end up calling them anyway.  */
1366190075Sobrien
1366290075Sobrienstatic bool
13663132718Skandwarf2out_ignore_block (tree block)
1366450397Sobrien{
1366590075Sobrien  tree decl;
1366650397Sobrien
1366790075Sobrien  for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
1366890075Sobrien    if (TREE_CODE (decl) == FUNCTION_DECL
1366990075Sobrien	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
1367090075Sobrien      return 0;
1367190075Sobrien
1367290075Sobrien  return 1;
1367350397Sobrien}
1367450397Sobrien
13675169689Skan/* Hash table routines for file_hash.  */
13676169689Skan
13677169689Skanstatic int
13678169689Skanfile_table_eq (const void *p1_p, const void *p2_p)
13679169689Skan{
13680169689Skan  const struct dwarf_file_data * p1 = p1_p;
13681169689Skan  const char * p2 = p2_p;
13682169689Skan  return strcmp (p1->filename, p2) == 0;
13683169689Skan}
13684169689Skan
13685169689Skanstatic hashval_t
13686169689Skanfile_table_hash (const void *p_p)
13687169689Skan{
13688169689Skan  const struct dwarf_file_data * p = p_p;
13689169689Skan  return htab_hash_string (p->filename);
13690169689Skan}
13691169689Skan
1369290075Sobrien/* Lookup FILE_NAME (in the list of filenames that we know about here in
1369350397Sobrien   dwarf2out.c) and return its "index".  The index of each (known) filename is
1369490075Sobrien   just a unique number which is associated with only that one filename.  We
1369590075Sobrien   need such numbers for the sake of generating labels (in the .debug_sfnames
1369690075Sobrien   section) and references to those files numbers (in the .debug_srcinfo
1369790075Sobrien   and.debug_macinfo sections).  If the filename given as an argument is not
1369890075Sobrien   found in our current list, add it to the list and assign it the next
1369990075Sobrien   available unique index number.  In order to speed up searches, we remember
1370090075Sobrien   the index of the filename was looked up last.  This handles the majority of
1370190075Sobrien   all searches.  */
1370250397Sobrien
13703169689Skanstatic struct dwarf_file_data *
13704132718Skanlookup_filename (const char *file_name)
1370550397Sobrien{
13706169689Skan  void ** slot;
13707169689Skan  struct dwarf_file_data * created;
1370850397Sobrien
1370990075Sobrien  /* Check to see if the file name that was searched on the previous
1371090075Sobrien     call matches this file name.  If so, return the index.  */
13711169689Skan  if (file_table_last_lookup
13712169689Skan      && (file_name == file_table_last_lookup->filename
13713169689Skan	  || strcmp (file_table_last_lookup->filename, file_name) == 0))
13714169689Skan    return file_table_last_lookup;
1371590075Sobrien
13716169689Skan  /* Didn't match the previous lookup, search the table.  */
13717169689Skan  slot = htab_find_slot_with_hash (file_table, file_name,
13718169689Skan				   htab_hash_string (file_name), INSERT);
13719169689Skan  if (*slot)
13720169689Skan    return *slot;
1372150397Sobrien
13722169689Skan  created = ggc_alloc (sizeof (struct dwarf_file_data));
13723169689Skan  created->filename = file_name;
13724169689Skan  created->emitted_number = 0;
13725169689Skan  *slot = created;
13726169689Skan  return created;
13727132718Skan}
13728132718Skan
13729169689Skan/* If the assembler will construct the file table, then translate the compiler
13730169689Skan   internal file table number into the assembler file table number, and emit
13731169689Skan   a .file directive if we haven't already emitted one yet.  The file table
13732169689Skan   numbers are different because we prune debug info for unused variables and
13733169689Skan   types, which may include filenames.  */
13734169689Skan
13735132718Skanstatic int
13736169689Skanmaybe_emit_file (struct dwarf_file_data * fd)
13737132718Skan{
13738169689Skan  if (! fd->emitted_number)
13739117395Skan    {
13740169689Skan      if (last_emitted_file)
13741169689Skan	fd->emitted_number = last_emitted_file->emitted_number + 1;
13742169689Skan      else
13743169689Skan	fd->emitted_number = 1;
13744169689Skan      last_emitted_file = fd;
13745169689Skan
13746169689Skan      if (DWARF2_ASM_LINE_DEBUG_INFO)
13747132718Skan	{
13748169689Skan	  fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13749169689Skan	  output_quoted_string (asm_out_file, fd->filename);
13750132718Skan	  fputc ('\n', asm_out_file);
13751132718Skan	}
13752117395Skan    }
13753169689Skan
13754169689Skan  return fd->emitted_number;
1375550397Sobrien}
1375650397Sobrien
13757169689Skan/* Called by the final INSN scan whenever we see a var location.  We
13758169689Skan   use it to drop labels in the right places, and throw the location in
13759169689Skan   our lookup table.  */
13760169689Skan
1376190075Sobrienstatic void
13762169689Skandwarf2out_var_location (rtx loc_note)
1376390075Sobrien{
13764169689Skan  char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13765169689Skan  struct var_loc_node *newloc;
13766169689Skan  rtx prev_insn;
13767169689Skan  static rtx last_insn;
13768169689Skan  static const char *last_label;
13769169689Skan  tree decl;
1377090075Sobrien
13771169689Skan  if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13772169689Skan    return;
13773169689Skan  prev_insn = PREV_INSN (loc_note);
13774169689Skan
13775169689Skan  newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13776169689Skan  /* If the insn we processed last time is the previous insn
13777169689Skan     and it is also a var location note, use the label we emitted
13778169689Skan     last time.  */
13779169689Skan  if (last_insn != NULL_RTX
13780169689Skan      && last_insn == prev_insn
13781169689Skan      && NOTE_P (prev_insn)
13782169689Skan      && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13783169689Skan    {
13784169689Skan      newloc->label = last_label;
13785169689Skan    }
13786169689Skan  else
13787169689Skan    {
13788169689Skan      ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13789169689Skan      ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13790169689Skan      loclabel_num++;
13791169689Skan      newloc->label = ggc_strdup (loclabel);
13792169689Skan    }
13793169689Skan  newloc->var_loc_note = loc_note;
13794169689Skan  newloc->next = NULL;
13795169689Skan
13796169689Skan  if (cfun && in_cold_section_p)
13797169689Skan    newloc->section_label = cfun->cold_section_label;
13798169689Skan  else
13799169689Skan    newloc->section_label = text_section_label;
13800169689Skan
13801169689Skan  last_insn = loc_note;
13802169689Skan  last_label = newloc->label;
13803169689Skan  decl = NOTE_VAR_LOCATION_DECL (loc_note);
13804169689Skan  add_var_loc_to_decl (decl, newloc);
1380590075Sobrien}
1380690075Sobrien
13807169689Skan/* We need to reset the locations at the beginning of each
13808169689Skan   function. We can't do this in the end_function hook, because the
13809169689Skan   declarations that use the locations won't have been output when
13810169689Skan   that hook is called.  Also compute have_multiple_function_sections here.  */
13811169689Skan
13812169689Skanstatic void
13813169689Skandwarf2out_begin_function (tree fun)
13814169689Skan{
13815169689Skan  htab_empty (decl_loc_table);
13816169689Skan
13817169689Skan  if (function_section (fun) != text_section)
13818169689Skan    have_multiple_function_sections = true;
13819169689Skan}
13820169689Skan
1382150397Sobrien/* Output a label to mark the beginning of a source code line entry
1382250397Sobrien   and record information relating to this source line, in
1382350397Sobrien   'line_info_table' for later output of the .debug_line section.  */
1382450397Sobrien
1382590075Sobrienstatic void
13826132718Skandwarf2out_source_line (unsigned int line, const char *filename)
1382750397Sobrien{
13828132718Skan  if (debug_info_level >= DINFO_LEVEL_NORMAL
13829132718Skan      && line != 0)
1383050397Sobrien    {
13831169689Skan      int file_num = maybe_emit_file (lookup_filename (filename));
13832169689Skan
13833169689Skan      switch_to_section (current_function_section ());
1383450397Sobrien
1383590075Sobrien      /* If requested, emit something human-readable.  */
1383690075Sobrien      if (flag_debug_asm)
1383790075Sobrien	fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
1383890075Sobrien		 filename, line);
1383990075Sobrien
1384090075Sobrien      if (DWARF2_ASM_LINE_DEBUG_INFO)
1384150397Sobrien	{
1384290075Sobrien	  /* Emit the .loc directive understood by GNU as.  */
1384390075Sobrien	  fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
1384490075Sobrien
1384590075Sobrien	  /* Indicate that line number info exists.  */
1384690075Sobrien	  line_info_table_in_use++;
1384790075Sobrien	}
13848169689Skan      else if (function_section (current_function_decl) != text_section)
1384990075Sobrien	{
1385090075Sobrien	  dw_separate_line_info_ref line_info;
13851169689Skan	  targetm.asm_out.internal_label (asm_out_file,
13852169689Skan					  SEPARATE_LINE_CODE_LABEL,
13853169689Skan					  separate_line_info_table_in_use);
1385450397Sobrien
13855169689Skan	  /* Expand the line info table if necessary.  */
1385650397Sobrien	  if (separate_line_info_table_in_use
1385750397Sobrien	      == separate_line_info_table_allocated)
1385850397Sobrien	    {
1385950397Sobrien	      separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1386050397Sobrien	      separate_line_info_table
13861132718Skan		= ggc_realloc (separate_line_info_table,
13862132718Skan			       separate_line_info_table_allocated
13863132718Skan			       * sizeof (dw_separate_line_info_entry));
13864132718Skan	      memset (separate_line_info_table
13865132718Skan		       + separate_line_info_table_in_use,
13866132718Skan		      0,
13867132718Skan		      (LINE_INFO_TABLE_INCREMENT
13868132718Skan		       * sizeof (dw_separate_line_info_entry)));
1386950397Sobrien	    }
1387050397Sobrien
1387150397Sobrien	  /* Add the new entry at the end of the line_info_table.  */
1387250397Sobrien	  line_info
1387350397Sobrien	    = &separate_line_info_table[separate_line_info_table_in_use++];
13874169689Skan	  line_info->dw_file_num = file_num;
1387550397Sobrien	  line_info->dw_line_num = line;
13876117395Skan	  line_info->function = current_function_funcdef_no;
1387750397Sobrien	}
1387850397Sobrien      else
1387950397Sobrien	{
1388090075Sobrien	  dw_line_info_ref line_info;
1388150397Sobrien
13882169689Skan	  targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
1388350397Sobrien				     line_info_table_in_use);
1388450397Sobrien
1388550397Sobrien	  /* Expand the line info table if necessary.  */
1388650397Sobrien	  if (line_info_table_in_use == line_info_table_allocated)
1388750397Sobrien	    {
1388850397Sobrien	      line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1388950397Sobrien	      line_info_table
13890132718Skan		= ggc_realloc (line_info_table,
13891132718Skan			       (line_info_table_allocated
13892132718Skan				* sizeof (dw_line_info_entry)));
13893132718Skan	      memset (line_info_table + line_info_table_in_use, 0,
13894132718Skan		      LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
1389550397Sobrien	    }
1389650397Sobrien
1389750397Sobrien	  /* Add the new entry at the end of the line_info_table.  */
1389850397Sobrien	  line_info = &line_info_table[line_info_table_in_use++];
13899169689Skan	  line_info->dw_file_num = file_num;
1390050397Sobrien	  line_info->dw_line_num = line;
1390150397Sobrien	}
1390250397Sobrien    }
1390350397Sobrien}
1390450397Sobrien
1390590075Sobrien/* Record the beginning of a new source file.  */
1390650397Sobrien
1390790075Sobrienstatic void
13908132718Skandwarf2out_start_source_file (unsigned int lineno, const char *filename)
1390950397Sobrien{
13910132718Skan  if (flag_eliminate_dwarf2_dups)
1391190075Sobrien    {
1391290075Sobrien      /* Record the beginning of the file for break_out_includes.  */
13913117395Skan      dw_die_ref bincl_die;
13914117395Skan
13915117395Skan      bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
1391690075Sobrien      add_AT_string (bincl_die, DW_AT_name, filename);
1391790075Sobrien    }
1391890075Sobrien
1391990075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1392090075Sobrien    {
13921169689Skan      int file_num = maybe_emit_file (lookup_filename (filename));
13922169689Skan
13923169689Skan      switch_to_section (debug_macinfo_section);
1392490075Sobrien      dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
1392590075Sobrien      dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
1392690075Sobrien				   lineno);
13927169689Skan
13928169689Skan      dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
1392990075Sobrien    }
1393050397Sobrien}
1393150397Sobrien
1393290075Sobrien/* Record the end of a source file.  */
1393350397Sobrien
1393490075Sobrienstatic void
13935132718Skandwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
1393650397Sobrien{
1393790075Sobrien  if (flag_eliminate_dwarf2_dups)
1393890075Sobrien    /* Record the end of the file for break_out_includes.  */
1393990075Sobrien    new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
1394090075Sobrien
1394190075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1394290075Sobrien    {
13943169689Skan      switch_to_section (debug_macinfo_section);
1394490075Sobrien      dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
1394590075Sobrien    }
1394650397Sobrien}
1394750397Sobrien
1394890075Sobrien/* Called from debug_define in toplev.c.  The `buffer' parameter contains
1394950397Sobrien   the tail part of the directive line, i.e. the part which is past the
1395050397Sobrien   initial whitespace, #, whitespace, directive-name, whitespace part.  */
1395150397Sobrien
1395290075Sobrienstatic void
13953132718Skandwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13954132718Skan		  const char *buffer ATTRIBUTE_UNUSED)
1395550397Sobrien{
1395690075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1395790075Sobrien    {
13958169689Skan      switch_to_section (debug_macinfo_section);
1395990075Sobrien      dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
1396090075Sobrien      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
1396190075Sobrien      dw2_asm_output_nstring (buffer, -1, "The macro");
1396290075Sobrien    }
1396350397Sobrien}
1396450397Sobrien
1396590075Sobrien/* Called from debug_undef in toplev.c.  The `buffer' parameter contains
1396650397Sobrien   the tail part of the directive line, i.e. the part which is past the
1396750397Sobrien   initial whitespace, #, whitespace, directive-name, whitespace part.  */
1396850397Sobrien
1396990075Sobrienstatic void
13970132718Skandwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13971132718Skan		 const char *buffer ATTRIBUTE_UNUSED)
1397250397Sobrien{
1397390075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1397490075Sobrien    {
13975169689Skan      switch_to_section (debug_macinfo_section);
1397690075Sobrien      dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
1397790075Sobrien      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
1397890075Sobrien      dw2_asm_output_nstring (buffer, -1, "The macro");
1397990075Sobrien    }
1398050397Sobrien}
1398150397Sobrien
1398250397Sobrien/* Set up for Dwarf output at the start of compilation.  */
1398350397Sobrien
1398490075Sobrienstatic void
13985132718Skandwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
1398650397Sobrien{
13987169689Skan  /* Allocate the file_table.  */
13988169689Skan  file_table = htab_create_ggc (50, file_table_hash,
13989169689Skan				file_table_eq, NULL);
1399090075Sobrien
13991169689Skan  /* Allocate the decl_die_table.  */
13992169689Skan  decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13993169689Skan				    decl_die_table_eq, NULL);
1399450397Sobrien
13995169689Skan  /* Allocate the decl_loc_table.  */
13996169689Skan  decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13997169689Skan				    decl_loc_table_eq, NULL);
13998169689Skan
1399950397Sobrien  /* Allocate the initial hunk of the decl_scope_table.  */
14000169689Skan  decl_scope_table = VEC_alloc (tree, gc, 256);
1400150397Sobrien
1400250397Sobrien  /* Allocate the initial hunk of the abbrev_die_table.  */
14003132718Skan  abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14004132718Skan					* sizeof (dw_die_ref));
1400550397Sobrien  abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14006169689Skan  /* Zero-th entry is allocated, but unused.  */
1400750397Sobrien  abbrev_die_table_in_use = 1;
1400850397Sobrien
1400950397Sobrien  /* Allocate the initial hunk of the line_info_table.  */
14010132718Skan  line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14011132718Skan				       * sizeof (dw_line_info_entry));
1401250397Sobrien  line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
1401390075Sobrien
14014169689Skan  /* Zero-th entry is allocated, but unused.  */
1401550397Sobrien  line_info_table_in_use = 1;
1401650397Sobrien
14017260395Spfg  /* Allocate the pubtypes and pubnames vectors.  */
14018260395Spfg  pubname_table = VEC_alloc (pubname_entry, gc, 32);
14019260395Spfg  pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14020260395Spfg
1402190075Sobrien  /* Generate the initial DIE for the .debug section.  Note that the (string)
1402250397Sobrien     value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
1402390075Sobrien     will (typically) be a relative pathname and that this pathname should be
1402450397Sobrien     taken as being relative to the directory from which the compiler was
14025132718Skan     invoked when the given (base) source file was compiled.  We will fill
14026132718Skan     in this value in dwarf2out_finish.  */
14027132718Skan  comp_unit_die = gen_compile_unit_die (NULL);
1402850397Sobrien
14029169689Skan  incomplete_types = VEC_alloc (tree, gc, 64);
1403090075Sobrien
14031169689Skan  used_rtx_array = VEC_alloc (rtx, gc, 32);
1403290075Sobrien
14033169689Skan  debug_info_section = get_section (DEBUG_INFO_SECTION,
14034169689Skan				    SECTION_DEBUG, NULL);
14035169689Skan  debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14036169689Skan				      SECTION_DEBUG, NULL);
14037169689Skan  debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14038169689Skan				       SECTION_DEBUG, NULL);
14039169689Skan  debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14040169689Skan				       SECTION_DEBUG, NULL);
14041169689Skan  debug_line_section = get_section (DEBUG_LINE_SECTION,
14042169689Skan				    SECTION_DEBUG, NULL);
14043169689Skan  debug_loc_section = get_section (DEBUG_LOC_SECTION,
14044169689Skan				   SECTION_DEBUG, NULL);
14045169689Skan  debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14046169689Skan					SECTION_DEBUG, NULL);
14047260395Spfg#ifdef DEBUG_PUBTYPES_SECTION
14048260395Spfg  debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14049260395Spfg					SECTION_DEBUG, NULL);
14050260395Spfg#endif
14051169689Skan  debug_str_section = get_section (DEBUG_STR_SECTION,
14052169689Skan				   DEBUG_STR_SECTION_FLAGS, NULL);
14053169689Skan  debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14054169689Skan				      SECTION_DEBUG, NULL);
14055169689Skan  debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14056169689Skan				     SECTION_DEBUG, NULL);
14057169689Skan
1405850397Sobrien  ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
1405990075Sobrien  ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
1406090075Sobrien			       DEBUG_ABBREV_SECTION_LABEL, 0);
14061169689Skan  ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14062169689Skan  ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14063169689Skan			       COLD_TEXT_SECTION_LABEL, 0);
14064169689Skan  ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
1406590075Sobrien
1406690075Sobrien  ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
1406752284Sobrien			       DEBUG_INFO_SECTION_LABEL, 0);
1406890075Sobrien  ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
1406952284Sobrien			       DEBUG_LINE_SECTION_LABEL, 0);
1407090075Sobrien  ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
1407190075Sobrien			       DEBUG_RANGES_SECTION_LABEL, 0);
14072169689Skan  switch_to_section (debug_abbrev_section);
1407352284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14074169689Skan  switch_to_section (debug_info_section);
1407552284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14076169689Skan  switch_to_section (debug_line_section);
1407752284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
1407890075Sobrien
1407990075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1408090075Sobrien    {
14081169689Skan      switch_to_section (debug_macinfo_section);
1408290075Sobrien      ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
1408390075Sobrien				   DEBUG_MACINFO_SECTION_LABEL, 0);
1408490075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
1408590075Sobrien    }
1408690075Sobrien
14087169689Skan  switch_to_section (text_section);
14088169689Skan  ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14089169689Skan  if (flag_reorder_blocks_and_partition)
1409090075Sobrien    {
14091169689Skan      switch_to_section (unlikely_text_section ());
14092169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
1409390075Sobrien    }
1409450397Sobrien}
1409550397Sobrien
1409690075Sobrien/* A helper function for dwarf2out_finish called through
1409790075Sobrien   ht_forall.  Emit one queued .debug_str string.  */
1409890075Sobrien
1409990075Sobrienstatic int
14100132718Skanoutput_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
1410190075Sobrien{
14102132718Skan  struct indirect_string_node *node = (struct indirect_string_node *) *h;
1410390075Sobrien
1410490075Sobrien  if (node->form == DW_FORM_strp)
1410590075Sobrien    {
14106169689Skan      switch_to_section (debug_str_section);
1410790075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, node->label);
14108132718Skan      assemble_string (node->str, strlen (node->str) + 1);
1410990075Sobrien    }
1411090075Sobrien
1411190075Sobrien  return 1;
1411290075Sobrien}
1411390075Sobrien
14114169689Skan#if ENABLE_ASSERT_CHECKING
14115169689Skan/* Verify that all marks are clear.  */
14116132718Skan
14117169689Skanstatic void
14118169689Skanverify_marks_clear (dw_die_ref die)
14119169689Skan{
14120169689Skan  dw_die_ref c;
14121169689Skan
14122169689Skan  gcc_assert (! die->die_mark);
14123169689Skan  FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14124169689Skan}
14125169689Skan#endif /* ENABLE_ASSERT_CHECKING */
14126132718Skan
14127132718Skan/* Clear the marks for a die and its children.
14128132718Skan   Be cool if the mark isn't set.  */
14129132718Skan
14130132718Skanstatic void
14131132718Skanprune_unmark_dies (dw_die_ref die)
14132132718Skan{
14133132718Skan  dw_die_ref c;
14134169689Skan
14135169689Skan  if (die->die_mark)
14136169689Skan    die->die_mark = 0;
14137169689Skan  FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14138132718Skan}
14139132718Skan
14140132718Skan/* Given DIE that we're marking as used, find any other dies
14141132718Skan   it references as attributes and mark them as used.  */
14142132718Skan
14143132718Skanstatic void
14144132718Skanprune_unused_types_walk_attribs (dw_die_ref die)
14145132718Skan{
14146132718Skan  dw_attr_ref a;
14147169689Skan  unsigned ix;
14148132718Skan
14149169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14150132718Skan    {
14151132718Skan      if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14152132718Skan	{
14153132718Skan	  /* A reference to another DIE.
14154132718Skan	     Make sure that it will get emitted.  */
14155132718Skan	  prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14156132718Skan	}
14157169689Skan      /* Set the string's refcount to 0 so that prune_unused_types_mark
14158169689Skan	 accounts properly for it.  */
14159169689Skan      if (AT_class (a) == dw_val_class_str)
14160169689Skan	a->dw_attr_val.v.val_str->refcount = 0;
14161132718Skan    }
14162132718Skan}
14163132718Skan
14164132718Skan
14165132718Skan/* Mark DIE as being used.  If DOKIDS is true, then walk down
14166132718Skan   to DIE's children.  */
14167132718Skan
14168132718Skanstatic void
14169132718Skanprune_unused_types_mark (dw_die_ref die, int dokids)
14170132718Skan{
14171132718Skan  dw_die_ref c;
14172132718Skan
14173132718Skan  if (die->die_mark == 0)
14174132718Skan    {
14175132718Skan      /* We haven't done this node yet.  Mark it as used.  */
14176132718Skan      die->die_mark = 1;
14177132718Skan
14178132718Skan      /* We also have to mark its parents as used.
14179132718Skan	 (But we don't want to mark our parents' kids due to this.)  */
14180132718Skan      if (die->die_parent)
14181132718Skan	prune_unused_types_mark (die->die_parent, 0);
14182132718Skan
14183132718Skan      /* Mark any referenced nodes.  */
14184132718Skan      prune_unused_types_walk_attribs (die);
14185132718Skan
14186132718Skan      /* If this node is a specification,
14187132718Skan         also mark the definition, if it exists.  */
14188132718Skan      if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14189132718Skan        prune_unused_types_mark (die->die_definition, 1);
14190132718Skan    }
14191132718Skan
14192132718Skan  if (dokids && die->die_mark != 2)
14193132718Skan    {
14194132718Skan      /* We need to walk the children, but haven't done so yet.
14195132718Skan	 Remember that we've walked the kids.  */
14196132718Skan      die->die_mark = 2;
14197132718Skan
14198169689Skan      /* If this is an array type, we need to make sure our
14199169689Skan	 kids get marked, even if they're types.  */
14200169689Skan      if (die->die_tag == DW_TAG_array_type)
14201169689Skan	FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14202169689Skan      else
14203169689Skan	FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14204132718Skan    }
14205132718Skan}
14206132718Skan
14207132718Skan
14208132718Skan/* Walk the tree DIE and mark types that we actually use.  */
14209132718Skan
14210132718Skanstatic void
14211132718Skanprune_unused_types_walk (dw_die_ref die)
14212132718Skan{
14213132718Skan  dw_die_ref c;
14214132718Skan
14215132718Skan  /* Don't do anything if this node is already marked.  */
14216132718Skan  if (die->die_mark)
14217132718Skan    return;
14218132718Skan
14219132718Skan  switch (die->die_tag) {
14220132718Skan  case DW_TAG_const_type:
14221132718Skan  case DW_TAG_packed_type:
14222132718Skan  case DW_TAG_pointer_type:
14223132718Skan  case DW_TAG_reference_type:
14224132718Skan  case DW_TAG_volatile_type:
14225132718Skan  case DW_TAG_typedef:
14226132718Skan  case DW_TAG_array_type:
14227132718Skan  case DW_TAG_structure_type:
14228132718Skan  case DW_TAG_union_type:
14229132718Skan  case DW_TAG_class_type:
14230132718Skan  case DW_TAG_friend:
14231132718Skan  case DW_TAG_variant_part:
14232132718Skan  case DW_TAG_enumeration_type:
14233132718Skan  case DW_TAG_subroutine_type:
14234132718Skan  case DW_TAG_string_type:
14235132718Skan  case DW_TAG_set_type:
14236132718Skan  case DW_TAG_subrange_type:
14237132718Skan  case DW_TAG_ptr_to_member_type:
14238132718Skan  case DW_TAG_file_type:
14239169689Skan    if (die->die_perennial_p)
14240169689Skan      break;
14241169689Skan
14242132718Skan    /* It's a type node --- don't mark it.  */
14243132718Skan    return;
14244132718Skan
14245132718Skan  default:
14246132718Skan    /* Mark everything else.  */
14247132718Skan    break;
14248132718Skan  }
14249132718Skan
14250132718Skan  die->die_mark = 1;
14251132718Skan
14252132718Skan  /* Now, mark any dies referenced from here.  */
14253132718Skan  prune_unused_types_walk_attribs (die);
14254132718Skan
14255132718Skan  /* Mark children.  */
14256169689Skan  FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14257132718Skan}
14258132718Skan
14259169689Skan/* Increment the string counts on strings referred to from DIE's
14260169689Skan   attributes.  */
14261132718Skan
14262169689Skanstatic void
14263169689Skanprune_unused_types_update_strings (dw_die_ref die)
14264169689Skan{
14265169689Skan  dw_attr_ref a;
14266169689Skan  unsigned ix;
14267169689Skan
14268169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14269169689Skan    if (AT_class (a) == dw_val_class_str)
14270169689Skan      {
14271169689Skan	struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14272169689Skan	s->refcount++;
14273169689Skan	/* Avoid unnecessarily putting strings that are used less than
14274169689Skan	   twice in the hash table.  */
14275169689Skan	if (s->refcount
14276169689Skan	    == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14277169689Skan	  {
14278169689Skan	    void ** slot;
14279169689Skan	    slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14280169689Skan					     htab_hash_string (s->str),
14281169689Skan					     INSERT);
14282169689Skan	    gcc_assert (*slot == NULL);
14283169689Skan	    *slot = s;
14284169689Skan	  }
14285169689Skan      }
14286169689Skan}
14287169689Skan
14288132718Skan/* Remove from the tree DIE any dies that aren't marked.  */
14289132718Skan
14290132718Skanstatic void
14291132718Skanprune_unused_types_prune (dw_die_ref die)
14292132718Skan{
14293169689Skan  dw_die_ref c;
14294132718Skan
14295169689Skan  gcc_assert (die->die_mark);
14296169689Skan  prune_unused_types_update_strings (die);
14297169689Skan
14298169689Skan  if (! die->die_child)
14299169689Skan    return;
14300169689Skan
14301169689Skan  c = die->die_child;
14302169689Skan  do {
14303169689Skan    dw_die_ref prev = c;
14304169689Skan    for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14305169689Skan      if (c == die->die_child)
14306132718Skan	{
14307169689Skan	  /* No marked children between 'prev' and the end of the list.  */
14308169689Skan	  if (prev == c)
14309169689Skan	    /* No marked children at all.  */
14310169689Skan	    die->die_child = NULL;
14311132718Skan	  else
14312169689Skan	    {
14313169689Skan	      prev->die_sib = c->die_sib;
14314169689Skan	      die->die_child = prev;
14315169689Skan	    }
14316169689Skan	  return;
14317132718Skan	}
14318169689Skan
14319169689Skan    if (c != prev->die_sib)
14320169689Skan      prev->die_sib = c;
14321169689Skan    prune_unused_types_prune (c);
14322169689Skan  } while (c != die->die_child);
14323132718Skan}
14324132718Skan
14325132718Skan
14326132718Skan/* Remove dies representing declarations that we never use.  */
14327132718Skan
14328132718Skanstatic void
14329132718Skanprune_unused_types (void)
14330132718Skan{
14331132718Skan  unsigned int i;
14332132718Skan  limbo_die_node *node;
14333260395Spfg  pubname_ref pub;
14334132718Skan
14335169689Skan#if ENABLE_ASSERT_CHECKING
14336169689Skan  /* All the marks should already be clear.  */
14337169689Skan  verify_marks_clear (comp_unit_die);
14338132718Skan  for (node = limbo_die_list; node; node = node->next)
14339169689Skan    verify_marks_clear (node->die);
14340169689Skan#endif /* ENABLE_ASSERT_CHECKING */
14341132718Skan
14342132718Skan  /* Set the mark on nodes that are actually used.  */
14343132718Skan  prune_unused_types_walk (comp_unit_die);
14344132718Skan  for (node = limbo_die_list; node; node = node->next)
14345132718Skan    prune_unused_types_walk (node->die);
14346132718Skan
14347132718Skan  /* Also set the mark on nodes referenced from the
14348132718Skan     pubname_table or arange_table.  */
14349260395Spfg  for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14350260395Spfg    prune_unused_types_mark (pub->die, 1);
14351132718Skan  for (i = 0; i < arange_table_in_use; i++)
14352132718Skan    prune_unused_types_mark (arange_table[i], 1);
14353132718Skan
14354169689Skan  /* Get rid of nodes that aren't marked; and update the string counts.  */
14355169689Skan  if (debug_str_hash)
14356169689Skan    htab_empty (debug_str_hash);
14357132718Skan  prune_unused_types_prune (comp_unit_die);
14358132718Skan  for (node = limbo_die_list; node; node = node->next)
14359132718Skan    prune_unused_types_prune (node->die);
14360132718Skan
14361132718Skan  /* Leave the marks clear.  */
14362132718Skan  prune_unmark_dies (comp_unit_die);
14363132718Skan  for (node = limbo_die_list; node; node = node->next)
14364132718Skan    prune_unmark_dies (node->die);
14365132718Skan}
14366132718Skan
14367169689Skan/* Set the parameter to true if there are any relative pathnames in
14368169689Skan   the file table.  */
14369169689Skanstatic int
14370169689Skanfile_table_relative_p (void ** slot, void *param)
14371169689Skan{
14372169689Skan  bool *p = param;
14373169689Skan  struct dwarf_file_data *d = *slot;
14374169689Skan  if (d->emitted_number && d->filename[0] != DIR_SEPARATOR)
14375169689Skan    {
14376169689Skan      *p = true;
14377169689Skan      return 0;
14378169689Skan    }
14379169689Skan  return 1;
14380169689Skan}
14381169689Skan
1438250397Sobrien/* Output stuff that dwarf requires at the end of every file,
1438350397Sobrien   and generate the DWARF-2 debugging info.  */
1438450397Sobrien
1438590075Sobrienstatic void
14386132718Skandwarf2out_finish (const char *filename)
1438750397Sobrien{
1438850397Sobrien  limbo_die_node *node, *next_node;
1438990075Sobrien  dw_die_ref die = 0;
1439050397Sobrien
14391132718Skan  /* Add the name for the main input file now.  We delayed this from
14392132718Skan     dwarf2out_init to avoid complications with PCH.  */
14393132718Skan  add_name_attribute (comp_unit_die, filename);
14394132718Skan  if (filename[0] != DIR_SEPARATOR)
14395132718Skan    add_comp_dir_attribute (comp_unit_die);
14396132718Skan  else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14397132718Skan    {
14398169689Skan      bool p = false;
14399169689Skan      htab_traverse (file_table, file_table_relative_p, &p);
14400169689Skan      if (p)
14401169689Skan	add_comp_dir_attribute (comp_unit_die);
14402132718Skan    }
14403132718Skan
1440450397Sobrien  /* Traverse the limbo die list, and add parent/child links.  The only
1440550397Sobrien     dies without parents that should be here are concrete instances of
1440650397Sobrien     inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
1440750397Sobrien     For concrete instances, we can get the parent die from the abstract
1440850397Sobrien     instance.  */
1440950397Sobrien  for (node = limbo_die_list; node; node = next_node)
1441050397Sobrien    {
1441150397Sobrien      next_node = node->next;
1441250397Sobrien      die = node->die;
1441350397Sobrien
1441450397Sobrien      if (die->die_parent == NULL)
1441550397Sobrien	{
1441690075Sobrien	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
1441790075Sobrien
1441890075Sobrien	  if (origin)
1441990075Sobrien	    add_child_die (origin->die_parent, die);
1442050397Sobrien	  else if (die == comp_unit_die)
1442190075Sobrien	    ;
1442290075Sobrien	  else if (errorcount > 0 || sorrycount > 0)
1442390075Sobrien	    /* It's OK to be confused by errors in the input.  */
1442490075Sobrien	    add_child_die (comp_unit_die, die);
14425169689Skan	  else
1442690075Sobrien	    {
1442790075Sobrien	      /* In certain situations, the lexical block containing a
1442890075Sobrien		 nested function can be optimized away, which results
1442990075Sobrien		 in the nested function die being orphaned.  Likewise
1443090075Sobrien		 with the return type of that nested function.  Force
14431169689Skan		 this to be a child of the containing function.
14432169689Skan
14433169689Skan		 It may happen that even the containing function got fully
14434169689Skan		 inlined and optimized out.  In that case we are lost and
14435169689Skan		 assign the empty child.  This should not be big issue as
14436169689Skan		 the function is likely unreachable too.  */
14437169689Skan	      tree context = NULL_TREE;
14438169689Skan
14439169689Skan	      gcc_assert (node->created_for);
14440169689Skan
14441169689Skan	      if (DECL_P (node->created_for))
14442169689Skan		context = DECL_CONTEXT (node->created_for);
14443169689Skan	      else if (TYPE_P (node->created_for))
14444169689Skan		context = TYPE_CONTEXT (node->created_for);
14445169689Skan
14446171825Skan	      gcc_assert (context
14447171825Skan			  && (TREE_CODE (context) == FUNCTION_DECL
14448171825Skan			      || TREE_CODE (context) == NAMESPACE_DECL));
14449169689Skan
1445090075Sobrien	      origin = lookup_decl_die (context);
14451169689Skan	      if (origin)
14452169689Skan	        add_child_die (origin, die);
14453169689Skan	      else
14454169689Skan	        add_child_die (comp_unit_die, die);
1445590075Sobrien	    }
1445650397Sobrien	}
1445750397Sobrien    }
1445850397Sobrien
1445990075Sobrien  limbo_die_list = NULL;
1446090075Sobrien
1446152284Sobrien  /* Walk through the list of incomplete types again, trying once more to
1446252284Sobrien     emit full debugging info for them.  */
1446352284Sobrien  retry_incomplete_types ();
1446452284Sobrien
14465132718Skan  if (flag_eliminate_unused_debug_types)
14466132718Skan    prune_unused_types ();
14467132718Skan
1446890075Sobrien  /* Generate separate CUs for each of the include files we've seen.
1446990075Sobrien     They will go into limbo_die_list.  */
1447090075Sobrien  if (flag_eliminate_dwarf2_dups)
1447190075Sobrien    break_out_includes (comp_unit_die);
1447290075Sobrien
1447390075Sobrien  /* Traverse the DIE's and add add sibling attributes to those DIE's
1447450397Sobrien     that have children.  */
1447550397Sobrien  add_sibling_attributes (comp_unit_die);
1447690075Sobrien  for (node = limbo_die_list; node; node = node->next)
1447790075Sobrien    add_sibling_attributes (node->die);
1447850397Sobrien
1447950397Sobrien  /* Output a terminator label for the .text section.  */
14480169689Skan  switch_to_section (text_section);
14481169689Skan  targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14482169689Skan  if (flag_reorder_blocks_and_partition)
1448350397Sobrien    {
14484169689Skan      switch_to_section (unlikely_text_section ());
14485169689Skan      targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
1448690075Sobrien    }
1448750397Sobrien
1448890075Sobrien  /* We can only use the low/high_pc attributes if all of the code was
1448990075Sobrien     in .text.  */
14490169689Skan  if (!have_multiple_function_sections)
1449190075Sobrien    {
1449290075Sobrien      add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
1449390075Sobrien      add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
1449450397Sobrien    }
1449550397Sobrien
1449690075Sobrien  /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
1449790075Sobrien     "base address".  Use zero so that these addresses become absolute.  */
1449890075Sobrien  else if (have_location_lists || ranges_table_in_use)
1449990075Sobrien    add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
1450090075Sobrien
14501169689Skan  /* Output location list section if necessary.  */
14502169689Skan  if (have_location_lists)
14503169689Skan    {
14504169689Skan      /* Output the location lists info.  */
14505169689Skan      switch_to_section (debug_loc_section);
14506169689Skan      ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14507169689Skan				   DEBUG_LOC_SECTION_LABEL, 0);
14508169689Skan      ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14509169689Skan      output_location_lists (die);
14510169689Skan    }
14511169689Skan
1451290075Sobrien  if (debug_info_level >= DINFO_LEVEL_NORMAL)
14513169689Skan    add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14514169689Skan		    debug_line_section_label);
1451590075Sobrien
1451690075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14517169689Skan    add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
1451890075Sobrien
1451990075Sobrien  /* Output all of the compilation units.  We put the main one last so that
1452090075Sobrien     the offsets are available to output_pubnames.  */
1452190075Sobrien  for (node = limbo_die_list; node; node = node->next)
14522117395Skan    output_comp_unit (node->die, 0);
1452390075Sobrien
14524117395Skan  output_comp_unit (comp_unit_die, 0);
1452590075Sobrien
1452650397Sobrien  /* Output the abbreviation table.  */
14527169689Skan  switch_to_section (debug_abbrev_section);
1452850397Sobrien  output_abbrev_section ();
1452950397Sobrien
1453090075Sobrien  /* Output public names table if necessary.  */
14531260395Spfg  if (!VEC_empty (pubname_entry, pubname_table))
1453250397Sobrien    {
14533169689Skan      switch_to_section (debug_pubnames_section);
14534260395Spfg      output_pubnames (pubname_table);
1453550397Sobrien    }
1453650397Sobrien
14537260395Spfg#ifdef DEBUG_PUBTYPES_SECTION
14538260395Spfg  /* Output public types table if necessary.  */
14539260395Spfg  if (!VEC_empty (pubname_entry, pubtype_table))
14540260395Spfg    {
14541260395Spfg      switch_to_section (debug_pubtypes_section);
14542260395Spfg      output_pubnames (pubtype_table);
14543260395Spfg    }
14544260395Spfg#endif
14545260395Spfg
1454690075Sobrien  /* Output the address range information.  We only put functions in the arange
1454790075Sobrien     table, so don't write it out if we don't have any.  */
1454850397Sobrien  if (fde_table_in_use)
1454950397Sobrien    {
14550169689Skan      switch_to_section (debug_aranges_section);
1455150397Sobrien      output_aranges ();
1455250397Sobrien    }
1455390075Sobrien
1455490075Sobrien  /* Output ranges section if necessary.  */
1455590075Sobrien  if (ranges_table_in_use)
1455690075Sobrien    {
14557169689Skan      switch_to_section (debug_ranges_section);
1455890075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
1455990075Sobrien      output_ranges ();
1456090075Sobrien    }
1456190075Sobrien
14562169689Skan  /* Output the source line correspondence table.  We must do this
14563169689Skan     even if there is no line information.  Otherwise, on an empty
14564169689Skan     translation unit, we will generate a present, but empty,
14565169689Skan     .debug_info section.  IRIX 6.5 `nm' will then complain when
14566169689Skan     examining the file.  This is done late so that any filenames
14567169689Skan     used by the debug_info section are marked as 'used'.  */
14568169689Skan  if (! DWARF2_ASM_LINE_DEBUG_INFO)
14569169689Skan    {
14570169689Skan      switch_to_section (debug_line_section);
14571169689Skan      output_line_info ();
14572169689Skan    }
14573169689Skan
14574169689Skan  /* Have to end the macro section.  */
1457590075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14576117395Skan    {
14577169689Skan      switch_to_section (debug_macinfo_section);
14578117395Skan      dw2_asm_output_data (1, 0, "End compilation unit");
1457990075Sobrien    }
1458090075Sobrien
1458190075Sobrien  /* If we emitted any DW_FORM_strp form attribute, output the string
1458290075Sobrien     table too.  */
1458390075Sobrien  if (debug_str_hash)
14584132718Skan    htab_traverse (debug_str_hash, output_indirect_string, NULL);
1458550397Sobrien}
14586117395Skan#else
14587117395Skan
14588117395Skan/* This should never be used, but its address is needed for comparisons.  */
14589117395Skanconst struct gcc_debug_hooks dwarf2_debug_hooks;
14590117395Skan
14591117395Skan#endif /* DWARF2_DEBUGGING_INFO */
14592117395Skan
14593117395Skan#include "gt-dwarf2out.h"
14594