dwarf2out.c revision 245023
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;
159169689Skanstatic GTY(()) section *debug_str_section;
160169689Skanstatic GTY(()) section *debug_ranges_section;
161169689Skanstatic GTY(()) section *debug_frame_section;
162169689Skan
16350397Sobrien/* How to start an assembler comment.  */
16450397Sobrien#ifndef ASM_COMMENT_START
16550397Sobrien#define ASM_COMMENT_START ";#"
16650397Sobrien#endif
16750397Sobrien
16850397Sobrientypedef struct dw_cfi_struct *dw_cfi_ref;
16950397Sobrientypedef struct dw_fde_struct *dw_fde_ref;
17050397Sobrientypedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
17150397Sobrien
17250397Sobrien/* Call frames are described using a sequence of Call Frame
17350397Sobrien   Information instructions.  The register number, offset
17450397Sobrien   and address fields are provided as possible operands;
17550397Sobrien   their use is selected by the opcode field.  */
17650397Sobrien
177132718Skanenum dw_cfi_oprnd_type {
178132718Skan  dw_cfi_oprnd_unused,
179132718Skan  dw_cfi_oprnd_reg_num,
180132718Skan  dw_cfi_oprnd_offset,
181132718Skan  dw_cfi_oprnd_addr,
182132718Skan  dw_cfi_oprnd_loc
183132718Skan};
184132718Skan
185132718Skantypedef union dw_cfi_oprnd_struct GTY(())
18650397Sobrien{
187169689Skan  unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
188132718Skan  HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
189132718Skan  const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
190132718Skan  struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
19150397Sobrien}
19250397Sobriendw_cfi_oprnd;
19350397Sobrien
194132718Skantypedef struct dw_cfi_struct GTY(())
19550397Sobrien{
19650397Sobrien  dw_cfi_ref dw_cfi_next;
19750397Sobrien  enum dwarf_call_frame_info dw_cfi_opc;
198132718Skan  dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
199132718Skan    dw_cfi_oprnd1;
200132718Skan  dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
201132718Skan    dw_cfi_oprnd2;
20250397Sobrien}
20350397Sobriendw_cfi_node;
20450397Sobrien
20590075Sobrien/* This is how we define the location of the CFA. We use to handle it
20690075Sobrien   as REG + OFFSET all the time,  but now it can be more complex.
20790075Sobrien   It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
20890075Sobrien   Instead of passing around REG and OFFSET, we pass a copy
20990075Sobrien   of this structure.  */
210132718Skantypedef struct cfa_loc GTY(())
21190075Sobrien{
212132718Skan  HOST_WIDE_INT offset;
213132718Skan  HOST_WIDE_INT base_offset;
214169689Skan  unsigned int reg;
21590075Sobrien  int indirect;            /* 1 if CFA is accessed via a dereference.  */
21690075Sobrien} dw_cfa_location;
21790075Sobrien
21850397Sobrien/* All call frame descriptions (FDE's) in the GCC generated DWARF
21950397Sobrien   refer to a single Common Information Entry (CIE), defined at
22090075Sobrien   the beginning of the .debug_frame section.  This use of a single
22150397Sobrien   CIE obviates the need to keep track of multiple CIE's
22250397Sobrien   in the DWARF generation routines below.  */
22350397Sobrien
224132718Skantypedef struct dw_fde_struct GTY(())
22550397Sobrien{
226169689Skan  tree decl;
22790075Sobrien  const char *dw_fde_begin;
22890075Sobrien  const char *dw_fde_current_label;
22990075Sobrien  const char *dw_fde_end;
230169689Skan  const char *dw_fde_hot_section_label;
231169689Skan  const char *dw_fde_hot_section_end_label;
232169689Skan  const char *dw_fde_unlikely_section_label;
233169689Skan  const char *dw_fde_unlikely_section_end_label;
234169689Skan  bool dw_fde_switched_sections;
23550397Sobrien  dw_cfi_ref dw_fde_cfi;
23690075Sobrien  unsigned funcdef_number;
237117395Skan  unsigned all_throwers_are_sibcalls : 1;
23890075Sobrien  unsigned nothrow : 1;
23990075Sobrien  unsigned uses_eh_lsda : 1;
24050397Sobrien}
24150397Sobriendw_fde_node;
24250397Sobrien
24390075Sobrien/* Maximum size (in bytes) of an artificially generated label.  */
24450397Sobrien#define MAX_ARTIFICIAL_LABEL_BYTES	30
24550397Sobrien
24690075Sobrien/* The size of addresses as they appear in the Dwarf 2 data.
24790075Sobrien   Some architectures use word addresses to refer to code locations,
24890075Sobrien   but Dwarf 2 info always uses byte addresses.  On such machines,
24990075Sobrien   Dwarf 2 addresses need to be larger than the architecture's
25090075Sobrien   pointers.  */
25190075Sobrien#ifndef DWARF2_ADDR_SIZE
25290075Sobrien#define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
25350397Sobrien#endif
25450397Sobrien
25550397Sobrien/* The size in bytes of a DWARF field indicating an offset or length
25690075Sobrien   relative to a debug info section, specified to be 4 bytes in the
25790075Sobrien   DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
25890075Sobrien   as PTR_SIZE.  */
25950397Sobrien
26050397Sobrien#ifndef DWARF_OFFSET_SIZE
26150397Sobrien#define DWARF_OFFSET_SIZE 4
26250397Sobrien#endif
26350397Sobrien
264132718Skan/* According to the (draft) DWARF 3 specification, the initial length
265132718Skan   should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
266132718Skan   bytes are 0xffffffff, followed by the length stored in the next 8
267132718Skan   bytes.
268132718Skan
269132718Skan   However, the SGI/MIPS ABI uses an initial length which is equal to
270132718Skan   DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
271132718Skan
272132718Skan#ifndef DWARF_INITIAL_LENGTH_SIZE
273132718Skan#define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
274132718Skan#endif
275132718Skan
27650397Sobrien#define DWARF_VERSION 2
27750397Sobrien
27850397Sobrien/* Round SIZE up to the nearest BOUNDARY.  */
27950397Sobrien#define DWARF_ROUND(SIZE,BOUNDARY) \
28090075Sobrien  ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
28150397Sobrien
28250397Sobrien/* Offsets recorded in opcodes are a multiple of this alignment factor.  */
28390075Sobrien#ifndef DWARF_CIE_DATA_ALIGNMENT
28450397Sobrien#ifdef STACK_GROWS_DOWNWARD
28590075Sobrien#define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
28650397Sobrien#else
28790075Sobrien#define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
28850397Sobrien#endif
28990075Sobrien#endif
29050397Sobrien
291169689Skan/* CIE identifier.  */
292169689Skan#if HOST_BITS_PER_WIDE_INT >= 64
293169689Skan#define DWARF_CIE_ID \
294169689Skan  (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
295169689Skan#else
296169689Skan#define DWARF_CIE_ID DW_CIE_ID
297169689Skan#endif
298169689Skan
29950397Sobrien/* A pointer to the base of a table that contains frame description
30050397Sobrien   information for each routine.  */
301132718Skanstatic GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
30250397Sobrien
30350397Sobrien/* Number of elements currently allocated for fde_table.  */
304132718Skanstatic GTY(()) unsigned fde_table_allocated;
30550397Sobrien
30650397Sobrien/* Number of elements in fde_table currently in use.  */
307132718Skanstatic GTY(()) unsigned fde_table_in_use;
30850397Sobrien
30950397Sobrien/* Size (in elements) of increments by which we may expand the
31050397Sobrien   fde_table.  */
31150397Sobrien#define FDE_TABLE_INCREMENT 256
31250397Sobrien
31350397Sobrien/* A list of call frame insns for the CIE.  */
314132718Skanstatic GTY(()) dw_cfi_ref cie_cfi_head;
31550397Sobrien
316132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
31750397Sobrien/* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
31850397Sobrien   attribute that accelerates the lookup of the FDE associated
31990075Sobrien   with the subprogram.  This variable holds the table index of the FDE
32050397Sobrien   associated with the current function (body) definition.  */
32150397Sobrienstatic unsigned current_funcdef_fde;
322132718Skan#endif
32350397Sobrien
324132718Skanstruct indirect_string_node GTY(())
32590075Sobrien{
326132718Skan  const char *str;
32790075Sobrien  unsigned int refcount;
32890075Sobrien  unsigned int form;
32990075Sobrien  char *label;
33090075Sobrien};
33190075Sobrien
332132718Skanstatic GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
333132718Skan
334132718Skanstatic GTY(()) int dw2_string_counter;
335132718Skanstatic GTY(()) unsigned long dwarf2out_cfi_label_num;
336132718Skan
337132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
338132718Skan
33950397Sobrien/* Forward declarations for functions defined in this file.  */
34050397Sobrien
341132718Skanstatic char *stripattributes (const char *);
342132718Skanstatic const char *dwarf_cfi_name (unsigned);
343132718Skanstatic dw_cfi_ref new_cfi (void);
344132718Skanstatic void add_cfi (dw_cfi_ref *, dw_cfi_ref);
345132718Skanstatic void add_fde_cfi (const char *, dw_cfi_ref);
346132718Skanstatic void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
347132718Skanstatic void lookup_cfa (dw_cfa_location *);
348132718Skanstatic void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
349132718Skanstatic void initial_return_save (rtx);
350132718Skanstatic HOST_WIDE_INT stack_adjust_offset (rtx);
351132718Skanstatic void output_cfi (dw_cfi_ref, dw_fde_ref, int);
352132718Skanstatic void output_call_frame_info (int);
353169689Skanstatic void dwarf2out_stack_adjust (rtx, bool);
354132718Skanstatic void flush_queued_reg_saves (void);
355132718Skanstatic bool clobbers_queued_reg_save (rtx);
356132718Skanstatic void dwarf2out_frame_debug_expr (rtx, const char *);
35750397Sobrien
35890075Sobrien/* Support for complex CFA locations.  */
359132718Skanstatic void output_cfa_loc (dw_cfi_ref);
360132718Skanstatic void get_cfa_from_loc_descr (dw_cfa_location *,
361132718Skan				    struct dw_loc_descr_struct *);
36290075Sobrienstatic struct dw_loc_descr_struct *build_cfa_loc
363169689Skan  (dw_cfa_location *, HOST_WIDE_INT);
364132718Skanstatic void def_cfa_1 (const char *, dw_cfa_location *);
36550397Sobrien
36690075Sobrien/* How to start an assembler comment.  */
36790075Sobrien#ifndef ASM_COMMENT_START
36890075Sobrien#define ASM_COMMENT_START ";#"
36950397Sobrien#endif
37050397Sobrien
37150397Sobrien/* Data and reference forms for relocatable data.  */
37250397Sobrien#define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
37350397Sobrien#define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
37450397Sobrien
37590075Sobrien#ifndef DEBUG_FRAME_SECTION
37690075Sobrien#define DEBUG_FRAME_SECTION	".debug_frame"
37750397Sobrien#endif
37850397Sobrien
37950397Sobrien#ifndef FUNC_BEGIN_LABEL
38050397Sobrien#define FUNC_BEGIN_LABEL	"LFB"
38150397Sobrien#endif
38290075Sobrien
38350397Sobrien#ifndef FUNC_END_LABEL
38450397Sobrien#define FUNC_END_LABEL		"LFE"
38550397Sobrien#endif
38690075Sobrien
387169689Skan#ifndef FRAME_BEGIN_LABEL
38890075Sobrien#define FRAME_BEGIN_LABEL	"Lframe"
389169689Skan#endif
39050397Sobrien#define CIE_AFTER_SIZE_LABEL	"LSCIE"
39150397Sobrien#define CIE_END_LABEL		"LECIE"
39290075Sobrien#define FDE_LABEL		"LSFDE"
39390075Sobrien#define FDE_AFTER_SIZE_LABEL	"LASFDE"
39450397Sobrien#define FDE_END_LABEL		"LEFDE"
39590075Sobrien#define LINE_NUMBER_BEGIN_LABEL	"LSLT"
39690075Sobrien#define LINE_NUMBER_END_LABEL	"LELT"
39790075Sobrien#define LN_PROLOG_AS_LABEL	"LASLTP"
39890075Sobrien#define LN_PROLOG_END_LABEL	"LELTP"
39990075Sobrien#define DIE_LABEL_PREFIX	"DW"
40050397Sobrien
40150397Sobrien/* The DWARF 2 CFA column which tracks the return address.  Normally this
40250397Sobrien   is the column for PC, or the first column after all of the hard
40350397Sobrien   registers.  */
40450397Sobrien#ifndef DWARF_FRAME_RETURN_COLUMN
40550397Sobrien#ifdef PC_REGNUM
406132718Skan#define DWARF_FRAME_RETURN_COLUMN	DWARF_FRAME_REGNUM (PC_REGNUM)
40750397Sobrien#else
408132718Skan#define DWARF_FRAME_RETURN_COLUMN	DWARF_FRAME_REGISTERS
40950397Sobrien#endif
41050397Sobrien#endif
41150397Sobrien
41250397Sobrien/* The mapping from gcc register number to DWARF 2 CFA column number.  By
41350397Sobrien   default, we just provide columns for all registers.  */
41450397Sobrien#ifndef DWARF_FRAME_REGNUM
41550397Sobrien#define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
41650397Sobrien#endif
41790075Sobrien
41850397Sobrien/* Hook used by __throw.  */
41950397Sobrien
42050397Sobrienrtx
421132718Skanexpand_builtin_dwarf_sp_column (void)
42250397Sobrien{
423169689Skan  unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
424169689Skan  return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
42550397Sobrien}
42650397Sobrien
42750397Sobrien/* Return a pointer to a copy of the section string name S with all
42850397Sobrien   attributes stripped off, and an asterisk prepended (for assemble_name).  */
42950397Sobrien
43050397Sobrienstatic inline char *
431132718Skanstripattributes (const char *s)
43250397Sobrien{
433169689Skan  char *stripped = XNEWVEC (char, strlen (s) + 2);
43450397Sobrien  char *p = stripped;
43550397Sobrien
43650397Sobrien  *p++ = '*';
43750397Sobrien
43850397Sobrien  while (*s && *s != ',')
43950397Sobrien    *p++ = *s++;
44050397Sobrien
44150397Sobrien  *p = '\0';
44250397Sobrien  return stripped;
44350397Sobrien}
44450397Sobrien
44590075Sobrien/* Generate code to initialize the register size table.  */
44650397Sobrien
44790075Sobrienvoid
448132718Skanexpand_builtin_init_dwarf_reg_sizes (tree address)
44950397Sobrien{
450169689Skan  unsigned int i;
45190075Sobrien  enum machine_mode mode = TYPE_MODE (char_type_node);
452169689Skan  rtx addr = expand_normal (address);
45390075Sobrien  rtx mem = gen_rtx_MEM (BLKmode, addr);
454132718Skan  bool wrote_return_column = false;
45550397Sobrien
456102780Skan  for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
457169689Skan    {
458169689Skan      int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
459169689Skan
460169689Skan      if (rnum < DWARF_FRAME_REGISTERS)
461169689Skan	{
462169689Skan	  HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
463169689Skan	  enum machine_mode save_mode = reg_raw_mode[i];
464169689Skan	  HOST_WIDE_INT size;
465169689Skan
466169689Skan	  if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
467169689Skan	    save_mode = choose_hard_reg_mode (i, 1, true);
468169689Skan	  if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
469169689Skan	    {
470169689Skan	      if (save_mode == VOIDmode)
471169689Skan		continue;
472169689Skan	      wrote_return_column = true;
473169689Skan	    }
474169689Skan	  size = GET_MODE_SIZE (save_mode);
475169689Skan	  if (offset < 0)
476169689Skan	    continue;
477169689Skan
478169689Skan	  emit_move_insn (adjust_address (mem, mode, offset),
479169689Skan			  gen_int_mode (size, mode));
480169689Skan	}
481169689Skan    }
48250397Sobrien
483122180Skan#ifdef DWARF_ALT_FRAME_RETURN_COLUMN
484169689Skan  gcc_assert (wrote_return_column);
485132718Skan  i = DWARF_ALT_FRAME_RETURN_COLUMN;
486132718Skan  wrote_return_column = false;
487132718Skan#else
488132718Skan  i = DWARF_FRAME_RETURN_COLUMN;
489122180Skan#endif
490132718Skan
491132718Skan  if (! wrote_return_column)
492132718Skan    {
493132718Skan      enum machine_mode save_mode = Pmode;
494132718Skan      HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
495132718Skan      HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
496132718Skan      emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
497132718Skan    }
49850397Sobrien}
49950397Sobrien
50050397Sobrien/* Convert a DWARF call frame info. operation to its string name */
50150397Sobrien
50290075Sobrienstatic const char *
503132718Skandwarf_cfi_name (unsigned int cfi_opc)
50450397Sobrien{
50550397Sobrien  switch (cfi_opc)
50650397Sobrien    {
50750397Sobrien    case DW_CFA_advance_loc:
50850397Sobrien      return "DW_CFA_advance_loc";
50950397Sobrien    case DW_CFA_offset:
51050397Sobrien      return "DW_CFA_offset";
51150397Sobrien    case DW_CFA_restore:
51250397Sobrien      return "DW_CFA_restore";
51350397Sobrien    case DW_CFA_nop:
51450397Sobrien      return "DW_CFA_nop";
51550397Sobrien    case DW_CFA_set_loc:
51650397Sobrien      return "DW_CFA_set_loc";
51750397Sobrien    case DW_CFA_advance_loc1:
51850397Sobrien      return "DW_CFA_advance_loc1";
51950397Sobrien    case DW_CFA_advance_loc2:
52050397Sobrien      return "DW_CFA_advance_loc2";
52150397Sobrien    case DW_CFA_advance_loc4:
52250397Sobrien      return "DW_CFA_advance_loc4";
52350397Sobrien    case DW_CFA_offset_extended:
52450397Sobrien      return "DW_CFA_offset_extended";
52550397Sobrien    case DW_CFA_restore_extended:
52650397Sobrien      return "DW_CFA_restore_extended";
52750397Sobrien    case DW_CFA_undefined:
52850397Sobrien      return "DW_CFA_undefined";
52950397Sobrien    case DW_CFA_same_value:
53050397Sobrien      return "DW_CFA_same_value";
53150397Sobrien    case DW_CFA_register:
53250397Sobrien      return "DW_CFA_register";
53350397Sobrien    case DW_CFA_remember_state:
53450397Sobrien      return "DW_CFA_remember_state";
53550397Sobrien    case DW_CFA_restore_state:
53650397Sobrien      return "DW_CFA_restore_state";
53750397Sobrien    case DW_CFA_def_cfa:
53850397Sobrien      return "DW_CFA_def_cfa";
53950397Sobrien    case DW_CFA_def_cfa_register:
54050397Sobrien      return "DW_CFA_def_cfa_register";
54150397Sobrien    case DW_CFA_def_cfa_offset:
54250397Sobrien      return "DW_CFA_def_cfa_offset";
54350397Sobrien
54490075Sobrien    /* DWARF 3 */
54590075Sobrien    case DW_CFA_def_cfa_expression:
54690075Sobrien      return "DW_CFA_def_cfa_expression";
54790075Sobrien    case DW_CFA_expression:
54890075Sobrien      return "DW_CFA_expression";
54990075Sobrien    case DW_CFA_offset_extended_sf:
55090075Sobrien      return "DW_CFA_offset_extended_sf";
55190075Sobrien    case DW_CFA_def_cfa_sf:
55290075Sobrien      return "DW_CFA_def_cfa_sf";
55390075Sobrien    case DW_CFA_def_cfa_offset_sf:
55490075Sobrien      return "DW_CFA_def_cfa_offset_sf";
55590075Sobrien
55650397Sobrien    /* SGI/MIPS specific */
55750397Sobrien    case DW_CFA_MIPS_advance_loc8:
55850397Sobrien      return "DW_CFA_MIPS_advance_loc8";
55950397Sobrien
56050397Sobrien    /* GNU extensions */
56150397Sobrien    case DW_CFA_GNU_window_save:
56250397Sobrien      return "DW_CFA_GNU_window_save";
56350397Sobrien    case DW_CFA_GNU_args_size:
56450397Sobrien      return "DW_CFA_GNU_args_size";
56570635Sobrien    case DW_CFA_GNU_negative_offset_extended:
56670635Sobrien      return "DW_CFA_GNU_negative_offset_extended";
56750397Sobrien
56850397Sobrien    default:
56950397Sobrien      return "DW_CFA_<unknown>";
57050397Sobrien    }
57150397Sobrien}
57250397Sobrien
57350397Sobrien/* Return a pointer to a newly allocated Call Frame Instruction.  */
57450397Sobrien
57550397Sobrienstatic inline dw_cfi_ref
576132718Skannew_cfi (void)
57750397Sobrien{
578132718Skan  dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
57950397Sobrien
58050397Sobrien  cfi->dw_cfi_next = NULL;
58150397Sobrien  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
58250397Sobrien  cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
58350397Sobrien
58450397Sobrien  return cfi;
58550397Sobrien}
58650397Sobrien
58750397Sobrien/* Add a Call Frame Instruction to list of instructions.  */
58850397Sobrien
58950397Sobrienstatic inline void
590132718Skanadd_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
59150397Sobrien{
59290075Sobrien  dw_cfi_ref *p;
59350397Sobrien
59450397Sobrien  /* Find the end of the chain.  */
59550397Sobrien  for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
59650397Sobrien    ;
59750397Sobrien
59850397Sobrien  *p = cfi;
59950397Sobrien}
60050397Sobrien
60150397Sobrien/* Generate a new label for the CFI info to refer to.  */
60250397Sobrien
60350397Sobrienchar *
604132718Skandwarf2out_cfi_label (void)
60550397Sobrien{
60650397Sobrien  static char label[20];
60790075Sobrien
608132718Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
60950397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, label);
61050397Sobrien  return label;
61150397Sobrien}
61250397Sobrien
61350397Sobrien/* Add CFI to the current fde at the PC value indicated by LABEL if specified,
61450397Sobrien   or to the CIE if LABEL is NULL.  */
61550397Sobrien
61650397Sobrienstatic void
617132718Skanadd_fde_cfi (const char *label, dw_cfi_ref cfi)
61850397Sobrien{
61950397Sobrien  if (label)
62050397Sobrien    {
62190075Sobrien      dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
62250397Sobrien
62350397Sobrien      if (*label == 0)
62450397Sobrien	label = dwarf2out_cfi_label ();
62550397Sobrien
62650397Sobrien      if (fde->dw_fde_current_label == NULL
62750397Sobrien	  || strcmp (label, fde->dw_fde_current_label) != 0)
62850397Sobrien	{
62990075Sobrien	  dw_cfi_ref xcfi;
63050397Sobrien
631169689Skan	  label = xstrdup (label);
63250397Sobrien
63350397Sobrien	  /* Set the location counter to the new label.  */
63450397Sobrien	  xcfi = new_cfi ();
635169689Skan	  /* If we have a current label, advance from there, otherwise
636169689Skan	     set the location directly using set_loc.  */
637169689Skan	  xcfi->dw_cfi_opc = fde->dw_fde_current_label
638169689Skan			     ? DW_CFA_advance_loc4
639169689Skan			     : DW_CFA_set_loc;
64050397Sobrien	  xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
64150397Sobrien	  add_cfi (&fde->dw_fde_cfi, xcfi);
642169689Skan
643169689Skan	  fde->dw_fde_current_label = label;
64450397Sobrien	}
64550397Sobrien
64650397Sobrien      add_cfi (&fde->dw_fde_cfi, cfi);
64750397Sobrien    }
64850397Sobrien
64950397Sobrien  else
65050397Sobrien    add_cfi (&cie_cfi_head, cfi);
65150397Sobrien}
65250397Sobrien
65350397Sobrien/* Subroutine of lookup_cfa.  */
65450397Sobrien
655169689Skanstatic void
656132718Skanlookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
65750397Sobrien{
65850397Sobrien  switch (cfi->dw_cfi_opc)
65950397Sobrien    {
66050397Sobrien    case DW_CFA_def_cfa_offset:
66190075Sobrien      loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
66250397Sobrien      break;
663169689Skan    case DW_CFA_def_cfa_offset_sf:
664169689Skan      loc->offset
665169689Skan	= cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
666169689Skan      break;
66750397Sobrien    case DW_CFA_def_cfa_register:
66890075Sobrien      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
66950397Sobrien      break;
67050397Sobrien    case DW_CFA_def_cfa:
67190075Sobrien      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
67290075Sobrien      loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
67350397Sobrien      break;
674169689Skan    case DW_CFA_def_cfa_sf:
675169689Skan      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
676169689Skan      loc->offset
677169689Skan	= cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
678169689Skan      break;
67990075Sobrien    case DW_CFA_def_cfa_expression:
68090075Sobrien      get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
68190075Sobrien      break;
68250397Sobrien    default:
68350397Sobrien      break;
68450397Sobrien    }
68550397Sobrien}
68650397Sobrien
68750397Sobrien/* Find the previous value for the CFA.  */
68850397Sobrien
68950397Sobrienstatic void
690132718Skanlookup_cfa (dw_cfa_location *loc)
69150397Sobrien{
69290075Sobrien  dw_cfi_ref cfi;
69350397Sobrien
694169689Skan  loc->reg = INVALID_REGNUM;
69590075Sobrien  loc->offset = 0;
69690075Sobrien  loc->indirect = 0;
69790075Sobrien  loc->base_offset = 0;
69850397Sobrien
69950397Sobrien  for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
70090075Sobrien    lookup_cfa_1 (cfi, loc);
70150397Sobrien
70250397Sobrien  if (fde_table_in_use)
70350397Sobrien    {
70490075Sobrien      dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
70550397Sobrien      for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
70690075Sobrien	lookup_cfa_1 (cfi, loc);
70750397Sobrien    }
70850397Sobrien}
70950397Sobrien
71050397Sobrien/* The current rule for calculating the DWARF2 canonical frame address.  */
71190075Sobrienstatic dw_cfa_location cfa;
71250397Sobrien
71350397Sobrien/* The register used for saving registers to the stack, and its offset
71450397Sobrien   from the CFA.  */
71590075Sobrienstatic dw_cfa_location cfa_store;
71650397Sobrien
71750397Sobrien/* The running total of the size of arguments pushed onto the stack.  */
718132718Skanstatic HOST_WIDE_INT args_size;
71950397Sobrien
72050397Sobrien/* The last args_size we actually output.  */
721132718Skanstatic HOST_WIDE_INT old_args_size;
72250397Sobrien
72350397Sobrien/* Entry point to update the canonical frame address (CFA).
72450397Sobrien   LABEL is passed to add_fde_cfi.  The value of CFA is now to be
72550397Sobrien   calculated from REG+OFFSET.  */
72650397Sobrien
72750397Sobrienvoid
728132718Skandwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
72950397Sobrien{
73090075Sobrien  dw_cfa_location loc;
73190075Sobrien  loc.indirect = 0;
73290075Sobrien  loc.base_offset = 0;
73390075Sobrien  loc.reg = reg;
73490075Sobrien  loc.offset = offset;
73590075Sobrien  def_cfa_1 (label, &loc);
73690075Sobrien}
73750397Sobrien
738169689Skan/* Determine if two dw_cfa_location structures define the same data.  */
739169689Skan
740169689Skanstatic bool
741169689Skancfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
742169689Skan{
743169689Skan  return (loc1->reg == loc2->reg
744169689Skan	  && loc1->offset == loc2->offset
745169689Skan	  && loc1->indirect == loc2->indirect
746169689Skan	  && (loc1->indirect == 0
747169689Skan	      || loc1->base_offset == loc2->base_offset));
748169689Skan}
749169689Skan
75090075Sobrien/* This routine does the actual work.  The CFA is now calculated from
75190075Sobrien   the dw_cfa_location structure.  */
75250397Sobrien
75390075Sobrienstatic void
754132718Skandef_cfa_1 (const char *label, dw_cfa_location *loc_p)
75590075Sobrien{
75690075Sobrien  dw_cfi_ref cfi;
75790075Sobrien  dw_cfa_location old_cfa, loc;
75850397Sobrien
75990075Sobrien  cfa = *loc_p;
76090075Sobrien  loc = *loc_p;
76190075Sobrien
76290075Sobrien  if (cfa_store.reg == loc.reg && loc.indirect == 0)
76390075Sobrien    cfa_store.offset = loc.offset;
76490075Sobrien
76590075Sobrien  loc.reg = DWARF_FRAME_REGNUM (loc.reg);
76690075Sobrien  lookup_cfa (&old_cfa);
76790075Sobrien
76890075Sobrien  /* If nothing changed, no need to issue any call frame instructions.  */
769169689Skan  if (cfa_equal_p (&loc, &old_cfa))
77050397Sobrien    return;
77150397Sobrien
77250397Sobrien  cfi = new_cfi ();
77350397Sobrien
77490075Sobrien  if (loc.reg == old_cfa.reg && !loc.indirect)
77550397Sobrien    {
776169689Skan      /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
777169689Skan	 the CFA register did not change but the offset did.  */
778169689Skan      if (loc.offset < 0)
779169689Skan	{
780169689Skan	  HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
781169689Skan	  gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
782169689Skan
783169689Skan	  cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
784169689Skan	  cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
785169689Skan	}
786169689Skan      else
787169689Skan	{
788169689Skan	  cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
789169689Skan	  cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
790169689Skan	}
79150397Sobrien    }
79250397Sobrien
79350397Sobrien#ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
794169689Skan  else if (loc.offset == old_cfa.offset
795169689Skan	   && old_cfa.reg != INVALID_REGNUM
79690075Sobrien	   && !loc.indirect)
79750397Sobrien    {
79890075Sobrien      /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
79990075Sobrien	 indicating the CFA register has changed to <register> but the
80090075Sobrien	 offset has not changed.  */
80150397Sobrien      cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
80290075Sobrien      cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
80350397Sobrien    }
80450397Sobrien#endif
80550397Sobrien
80690075Sobrien  else if (loc.indirect == 0)
80750397Sobrien    {
80890075Sobrien      /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
80990075Sobrien	 indicating the CFA register has changed to <register> with
81090075Sobrien	 the specified offset.  */
811169689Skan      if (loc.offset < 0)
812169689Skan	{
813169689Skan	  HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
814169689Skan	  gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
815169689Skan
816169689Skan	  cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
817169689Skan	  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
818169689Skan	  cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
819169689Skan	}
820169689Skan      else
821169689Skan	{
822169689Skan	  cfi->dw_cfi_opc = DW_CFA_def_cfa;
823169689Skan	  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
824169689Skan	  cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
825169689Skan	}
82650397Sobrien    }
82790075Sobrien  else
82890075Sobrien    {
82990075Sobrien      /* Construct a DW_CFA_def_cfa_expression instruction to
83090075Sobrien	 calculate the CFA using a full location expression since no
83190075Sobrien	 register-offset pair is available.  */
83290075Sobrien      struct dw_loc_descr_struct *loc_list;
83350397Sobrien
83490075Sobrien      cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
835169689Skan      loc_list = build_cfa_loc (&loc, 0);
83690075Sobrien      cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
83790075Sobrien    }
83890075Sobrien
83950397Sobrien  add_fde_cfi (label, cfi);
84050397Sobrien}
84150397Sobrien
84250397Sobrien/* Add the CFI for saving a register.  REG is the CFA column number.
84350397Sobrien   LABEL is passed to add_fde_cfi.
84450397Sobrien   If SREG is -1, the register is saved at OFFSET from the CFA;
84550397Sobrien   otherwise it is saved in SREG.  */
84650397Sobrien
84750397Sobrienstatic void
848132718Skanreg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
84950397Sobrien{
85090075Sobrien  dw_cfi_ref cfi = new_cfi ();
85150397Sobrien
85250397Sobrien  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
85350397Sobrien
854169689Skan  if (sreg == INVALID_REGNUM)
85550397Sobrien    {
85650397Sobrien      if (reg & ~0x3f)
85750397Sobrien	/* The register number won't fit in 6 bits, so we have to use
85850397Sobrien	   the long form.  */
85950397Sobrien	cfi->dw_cfi_opc = DW_CFA_offset_extended;
86050397Sobrien      else
86150397Sobrien	cfi->dw_cfi_opc = DW_CFA_offset;
86250397Sobrien
86390075Sobrien#ifdef ENABLE_CHECKING
86490075Sobrien      {
86590075Sobrien	/* If we get an offset that is not a multiple of
86690075Sobrien	   DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
86790075Sobrien	   definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
86890075Sobrien	   description.  */
869132718Skan	HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
87090075Sobrien
871169689Skan	gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
87290075Sobrien      }
87390075Sobrien#endif
87450397Sobrien      offset /= DWARF_CIE_DATA_ALIGNMENT;
87550397Sobrien      if (offset < 0)
87690075Sobrien	cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
87790075Sobrien
87850397Sobrien      cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
87950397Sobrien    }
88090075Sobrien  else if (sreg == reg)
881169689Skan    cfi->dw_cfi_opc = DW_CFA_same_value;
88250397Sobrien  else
88350397Sobrien    {
88450397Sobrien      cfi->dw_cfi_opc = DW_CFA_register;
88550397Sobrien      cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
88650397Sobrien    }
88750397Sobrien
88850397Sobrien  add_fde_cfi (label, cfi);
88950397Sobrien}
89050397Sobrien
89150397Sobrien/* Add the CFI for saving a register window.  LABEL is passed to reg_save.
89250397Sobrien   This CFI tells the unwinder that it needs to restore the window registers
89350397Sobrien   from the previous frame's window save area.
89490075Sobrien
89550397Sobrien   ??? Perhaps we should note in the CIE where windows are saved (instead of
89650397Sobrien   assuming 0(cfa)) and what registers are in the window.  */
89750397Sobrien
89850397Sobrienvoid
899132718Skandwarf2out_window_save (const char *label)
90050397Sobrien{
90190075Sobrien  dw_cfi_ref cfi = new_cfi ();
90290075Sobrien
90350397Sobrien  cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
90450397Sobrien  add_fde_cfi (label, cfi);
90550397Sobrien}
90650397Sobrien
90750397Sobrien/* Add a CFI to update the running total of the size of arguments
90850397Sobrien   pushed onto the stack.  */
90950397Sobrien
91050397Sobrienvoid
911132718Skandwarf2out_args_size (const char *label, HOST_WIDE_INT size)
91250397Sobrien{
91390075Sobrien  dw_cfi_ref cfi;
91450397Sobrien
91550397Sobrien  if (size == old_args_size)
91650397Sobrien    return;
91790075Sobrien
91850397Sobrien  old_args_size = size;
91950397Sobrien
92050397Sobrien  cfi = new_cfi ();
92150397Sobrien  cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
92250397Sobrien  cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
92350397Sobrien  add_fde_cfi (label, cfi);
92450397Sobrien}
92550397Sobrien
92650397Sobrien/* Entry point for saving a register to the stack.  REG is the GCC register
92750397Sobrien   number.  LABEL and OFFSET are passed to reg_save.  */
92850397Sobrien
92950397Sobrienvoid
930132718Skandwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
93150397Sobrien{
932169689Skan  reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
93350397Sobrien}
93450397Sobrien
93550397Sobrien/* Entry point for saving the return address in the stack.
93650397Sobrien   LABEL and OFFSET are passed to reg_save.  */
93750397Sobrien
93850397Sobrienvoid
939132718Skandwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
94050397Sobrien{
941169689Skan  reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
94250397Sobrien}
94350397Sobrien
94450397Sobrien/* Entry point for saving the return address in a register.
94550397Sobrien   LABEL and SREG are passed to reg_save.  */
94650397Sobrien
94750397Sobrienvoid
948132718Skandwarf2out_return_reg (const char *label, unsigned int sreg)
94950397Sobrien{
950169689Skan  reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
95150397Sobrien}
95250397Sobrien
95350397Sobrien/* Record the initial position of the return address.  RTL is
95450397Sobrien   INCOMING_RETURN_ADDR_RTX.  */
95550397Sobrien
95650397Sobrienstatic void
957132718Skaninitial_return_save (rtx rtl)
95850397Sobrien{
959169689Skan  unsigned int reg = INVALID_REGNUM;
96090075Sobrien  HOST_WIDE_INT offset = 0;
96150397Sobrien
96250397Sobrien  switch (GET_CODE (rtl))
96350397Sobrien    {
96450397Sobrien    case REG:
96550397Sobrien      /* RA is in a register.  */
96690075Sobrien      reg = DWARF_FRAME_REGNUM (REGNO (rtl));
96750397Sobrien      break;
96890075Sobrien
96950397Sobrien    case MEM:
97050397Sobrien      /* RA is on the stack.  */
97150397Sobrien      rtl = XEXP (rtl, 0);
97250397Sobrien      switch (GET_CODE (rtl))
97350397Sobrien	{
97450397Sobrien	case REG:
975169689Skan	  gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
97650397Sobrien	  offset = 0;
97750397Sobrien	  break;
97890075Sobrien
97950397Sobrien	case PLUS:
980169689Skan	  gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
98150397Sobrien	  offset = INTVAL (XEXP (rtl, 1));
98250397Sobrien	  break;
98390075Sobrien
98450397Sobrien	case MINUS:
985169689Skan	  gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
98650397Sobrien	  offset = -INTVAL (XEXP (rtl, 1));
98750397Sobrien	  break;
98890075Sobrien
98950397Sobrien	default:
990169689Skan	  gcc_unreachable ();
99150397Sobrien	}
99290075Sobrien
99350397Sobrien      break;
99490075Sobrien
99550397Sobrien    case PLUS:
99650397Sobrien      /* The return address is at some offset from any value we can
99750397Sobrien	 actually load.  For instance, on the SPARC it is in %i7+8. Just
99850397Sobrien	 ignore the offset for now; it doesn't matter for unwinding frames.  */
999169689Skan      gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
100050397Sobrien      initial_return_save (XEXP (rtl, 0));
100150397Sobrien      return;
100290075Sobrien
100350397Sobrien    default:
1004169689Skan      gcc_unreachable ();
100550397Sobrien    }
100650397Sobrien
1007169689Skan  if (reg != DWARF_FRAME_RETURN_COLUMN)
1008169689Skan    reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
100950397Sobrien}
101050397Sobrien
101190075Sobrien/* Given a SET, calculate the amount of stack adjustment it
101290075Sobrien   contains.  */
101390075Sobrien
1014132718Skanstatic HOST_WIDE_INT
1015132718Skanstack_adjust_offset (rtx pattern)
101690075Sobrien{
101790075Sobrien  rtx src = SET_SRC (pattern);
101890075Sobrien  rtx dest = SET_DEST (pattern);
101990075Sobrien  HOST_WIDE_INT offset = 0;
102090075Sobrien  enum rtx_code code;
102190075Sobrien
102290075Sobrien  if (dest == stack_pointer_rtx)
102390075Sobrien    {
102490075Sobrien      /* (set (reg sp) (plus (reg sp) (const_int))) */
102590075Sobrien      code = GET_CODE (src);
102690075Sobrien      if (! (code == PLUS || code == MINUS)
102790075Sobrien	  || XEXP (src, 0) != stack_pointer_rtx
102890075Sobrien	  || GET_CODE (XEXP (src, 1)) != CONST_INT)
102990075Sobrien	return 0;
103090075Sobrien
103190075Sobrien      offset = INTVAL (XEXP (src, 1));
1032117395Skan      if (code == PLUS)
1033117395Skan	offset = -offset;
103490075Sobrien    }
1035169689Skan  else if (MEM_P (dest))
103690075Sobrien    {
103790075Sobrien      /* (set (mem (pre_dec (reg sp))) (foo)) */
103890075Sobrien      src = XEXP (dest, 0);
103990075Sobrien      code = GET_CODE (src);
104090075Sobrien
1041117395Skan      switch (code)
104290075Sobrien	{
1043117395Skan	case PRE_MODIFY:
1044117395Skan	case POST_MODIFY:
1045117395Skan	  if (XEXP (src, 0) == stack_pointer_rtx)
1046117395Skan	    {
1047117395Skan	      rtx val = XEXP (XEXP (src, 1), 1);
1048117395Skan	      /* We handle only adjustments by constant amount.  */
1049169689Skan	      gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1050169689Skan			  && GET_CODE (val) == CONST_INT);
1051117395Skan	      offset = -INTVAL (val);
1052117395Skan	      break;
1053117395Skan	    }
1054117395Skan	  return 0;
105590075Sobrien
1056117395Skan	case PRE_DEC:
1057117395Skan	case POST_DEC:
1058117395Skan	  if (XEXP (src, 0) == stack_pointer_rtx)
1059117395Skan	    {
1060117395Skan	      offset = GET_MODE_SIZE (GET_MODE (dest));
1061117395Skan	      break;
1062117395Skan	    }
1063117395Skan	  return 0;
106490075Sobrien
1065117395Skan	case PRE_INC:
1066117395Skan	case POST_INC:
1067117395Skan	  if (XEXP (src, 0) == stack_pointer_rtx)
1068117395Skan	    {
1069117395Skan	      offset = -GET_MODE_SIZE (GET_MODE (dest));
1070117395Skan	      break;
1071117395Skan	    }
1072117395Skan	  return 0;
1073117395Skan
1074117395Skan	default:
1075117395Skan	  return 0;
107690075Sobrien	}
107790075Sobrien    }
107890075Sobrien  else
107990075Sobrien    return 0;
108090075Sobrien
108190075Sobrien  return offset;
108290075Sobrien}
108390075Sobrien
108450397Sobrien/* Check INSN to see if it looks like a push or a stack adjustment, and
108550397Sobrien   make a note of it if it does.  EH uses this information to find out how
108650397Sobrien   much extra space it needs to pop off the stack.  */
108750397Sobrien
108850397Sobrienstatic void
1089169689Skandwarf2out_stack_adjust (rtx insn, bool after_p)
109050397Sobrien{
109190075Sobrien  HOST_WIDE_INT offset;
109290075Sobrien  const char *label;
109390075Sobrien  int i;
109450397Sobrien
1095132718Skan  /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1096132718Skan     with this function.  Proper support would require all frame-related
1097132718Skan     insns to be marked, and to be able to handle saving state around
1098132718Skan     epilogues textually in the middle of the function.  */
1099132718Skan  if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1100132718Skan    return;
1101132718Skan
1102169689Skan  /* If only calls can throw, and we have a frame pointer,
1103169689Skan     save up adjustments until we see the CALL_INSN.  */
1104169689Skan  if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
110550397Sobrien    {
1106169689Skan      if (CALL_P (insn) && !after_p)
1107169689Skan	{
1108169689Skan	  /* Extract the size of the args from the CALL rtx itself.  */
1109169689Skan	  insn = PATTERN (insn);
1110169689Skan	  if (GET_CODE (insn) == PARALLEL)
1111169689Skan	    insn = XVECEXP (insn, 0, 0);
1112169689Skan	  if (GET_CODE (insn) == SET)
1113169689Skan	    insn = SET_SRC (insn);
1114169689Skan	  gcc_assert (GET_CODE (insn) == CALL);
1115169689Skan	  dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1116169689Skan	}
111750397Sobrien      return;
111850397Sobrien    }
111950397Sobrien
1120169689Skan  if (CALL_P (insn) && !after_p)
112150397Sobrien    {
1122169689Skan      if (!flag_asynchronous_unwind_tables)
1123169689Skan	dwarf2out_args_size ("", args_size);
1124169689Skan      return;
1125169689Skan    }
1126169689Skan  else if (BARRIER_P (insn))
1127169689Skan    {
112850397Sobrien      /* When we see a BARRIER, we know to reset args_size to 0.  Usually
112950397Sobrien	 the compiler will have already emitted a stack adjustment, but
113050397Sobrien	 doesn't bother for calls to noreturn functions.  */
113150397Sobrien#ifdef STACK_GROWS_DOWNWARD
113250397Sobrien      offset = -args_size;
113350397Sobrien#else
113450397Sobrien      offset = args_size;
113550397Sobrien#endif
113650397Sobrien    }
113750397Sobrien  else if (GET_CODE (PATTERN (insn)) == SET)
113890075Sobrien    offset = stack_adjust_offset (PATTERN (insn));
113990075Sobrien  else if (GET_CODE (PATTERN (insn)) == PARALLEL
114090075Sobrien	   || GET_CODE (PATTERN (insn)) == SEQUENCE)
114150397Sobrien    {
114290075Sobrien      /* There may be stack adjustments inside compound insns.  Search
114390075Sobrien	 for them.  */
114490075Sobrien      for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
114590075Sobrien	if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
114690075Sobrien	  offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
114750397Sobrien    }
114850397Sobrien  else
114950397Sobrien    return;
115050397Sobrien
115150397Sobrien  if (offset == 0)
115250397Sobrien    return;
115350397Sobrien
115490075Sobrien  if (cfa.reg == STACK_POINTER_REGNUM)
115590075Sobrien    cfa.offset += offset;
115650397Sobrien
115750397Sobrien#ifndef STACK_GROWS_DOWNWARD
115850397Sobrien  offset = -offset;
115950397Sobrien#endif
116090075Sobrien
116150397Sobrien  args_size += offset;
116250397Sobrien  if (args_size < 0)
116350397Sobrien    args_size = 0;
116450397Sobrien
116550397Sobrien  label = dwarf2out_cfi_label ();
116690075Sobrien  def_cfa_1 (label, &cfa);
1167169689Skan  if (flag_asynchronous_unwind_tables)
1168169689Skan    dwarf2out_args_size (label, args_size);
116950397Sobrien}
117050397Sobrien
1171132718Skan#endif
1172132718Skan
117390075Sobrien/* We delay emitting a register save until either (a) we reach the end
117490075Sobrien   of the prologue or (b) the register is clobbered.  This clusters
117590075Sobrien   register saves so that there are fewer pc advances.  */
117652284Sobrien
1177132718Skanstruct queued_reg_save GTY(())
117890075Sobrien{
117990075Sobrien  struct queued_reg_save *next;
118090075Sobrien  rtx reg;
1181132718Skan  HOST_WIDE_INT cfa_offset;
1182169689Skan  rtx saved_reg;
118390075Sobrien};
118452284Sobrien
1185132718Skanstatic GTY(()) struct queued_reg_save *queued_reg_saves;
1186132718Skan
1187169689Skan/* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1188169689Skanstruct reg_saved_in_data GTY(()) {
1189169689Skan  rtx orig_reg;
1190169689Skan  rtx saved_in_reg;
1191169689Skan};
1192169689Skan
1193169689Skan/* A list of registers saved in other registers.
1194169689Skan   The list intentionally has a small maximum capacity of 4; if your
1195169689Skan   port needs more than that, you might consider implementing a
1196169689Skan   more efficient data structure.  */
1197169689Skanstatic GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1198169689Skanstatic GTY(()) size_t num_regs_saved_in_regs;
1199169689Skan
1200132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
120190075Sobrienstatic const char *last_reg_save_label;
120250397Sobrien
1203169689Skan/* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1204169689Skan   SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1205169689Skan
120652284Sobrienstatic void
1207169689Skanqueue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
120890075Sobrien{
1209169689Skan  struct queued_reg_save *q;
121090075Sobrien
1211169689Skan  /* Duplicates waste space, but it's also necessary to remove them
1212169689Skan     for correctness, since the queue gets output in reverse
1213169689Skan     order.  */
1214169689Skan  for (q = queued_reg_saves; q != NULL; q = q->next)
1215169689Skan    if (REGNO (q->reg) == REGNO (reg))
1216169689Skan      break;
1217169689Skan
1218169689Skan  if (q == NULL)
1219169689Skan    {
1220169689Skan      q = ggc_alloc (sizeof (*q));
1221169689Skan      q->next = queued_reg_saves;
1222169689Skan      queued_reg_saves = q;
1223169689Skan    }
1224169689Skan
122590075Sobrien  q->reg = reg;
122690075Sobrien  q->cfa_offset = offset;
1227169689Skan  q->saved_reg = sreg;
122890075Sobrien
122990075Sobrien  last_reg_save_label = label;
123090075Sobrien}
123190075Sobrien
1232169689Skan/* Output all the entries in QUEUED_REG_SAVES.  */
1233169689Skan
123490075Sobrienstatic void
1235132718Skanflush_queued_reg_saves (void)
123690075Sobrien{
1237169689Skan  struct queued_reg_save *q;
123890075Sobrien
1239169689Skan  for (q = queued_reg_saves; q; q = q->next)
124090075Sobrien    {
1241169689Skan      size_t i;
1242169689Skan      unsigned int reg, sreg;
1243169689Skan
1244169689Skan      for (i = 0; i < num_regs_saved_in_regs; i++)
1245169689Skan	if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1246169689Skan	  break;
1247169689Skan      if (q->saved_reg && i == num_regs_saved_in_regs)
1248169689Skan	{
1249169689Skan	  gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1250169689Skan	  num_regs_saved_in_regs++;
1251169689Skan	}
1252169689Skan      if (i != num_regs_saved_in_regs)
1253169689Skan	{
1254169689Skan	  regs_saved_in_regs[i].orig_reg = q->reg;
1255169689Skan	  regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1256169689Skan	}
1257169689Skan
1258169689Skan      reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1259169689Skan      if (q->saved_reg)
1260169689Skan	sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1261169689Skan      else
1262169689Skan	sreg = INVALID_REGNUM;
1263169689Skan      reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
126490075Sobrien    }
126590075Sobrien
126690075Sobrien  queued_reg_saves = NULL;
126790075Sobrien  last_reg_save_label = NULL;
126890075Sobrien}
126990075Sobrien
1270169689Skan/* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1271169689Skan   location for?  Or, does it clobber a register which we've previously
1272169689Skan   said that some other register is saved in, and for which we now
1273169689Skan   have a new location for?  */
1274169689Skan
127590075Sobrienstatic bool
1276132718Skanclobbers_queued_reg_save (rtx insn)
127790075Sobrien{
127890075Sobrien  struct queued_reg_save *q;
127990075Sobrien
1280117395Skan  for (q = queued_reg_saves; q; q = q->next)
1281169689Skan    {
1282169689Skan      size_t i;
1283169689Skan      if (modified_in_p (q->reg, insn))
1284169689Skan	return true;
1285169689Skan      for (i = 0; i < num_regs_saved_in_regs; i++)
1286169689Skan	if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1287169689Skan	    && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1288169689Skan	  return true;
1289169689Skan    }
129090075Sobrien
129190075Sobrien  return false;
129290075Sobrien}
129390075Sobrien
1294169689Skan/* Entry point for saving the first register into the second.  */
1295117395Skan
1296169689Skanvoid
1297169689Skandwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1298169689Skan{
1299169689Skan  size_t i;
1300169689Skan  unsigned int regno, sregno;
1301169689Skan
1302169689Skan  for (i = 0; i < num_regs_saved_in_regs; i++)
1303169689Skan    if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1304169689Skan      break;
1305169689Skan  if (i == num_regs_saved_in_regs)
1306169689Skan    {
1307169689Skan      gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1308169689Skan      num_regs_saved_in_regs++;
1309169689Skan    }
1310169689Skan  regs_saved_in_regs[i].orig_reg = reg;
1311169689Skan  regs_saved_in_regs[i].saved_in_reg = sreg;
1312169689Skan
1313169689Skan  regno = DWARF_FRAME_REGNUM (REGNO (reg));
1314169689Skan  sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1315169689Skan  reg_save (label, regno, sregno, 0);
1316169689Skan}
1317169689Skan
1318169689Skan/* What register, if any, is currently saved in REG?  */
1319169689Skan
1320169689Skanstatic rtx
1321169689Skanreg_saved_in (rtx reg)
1322169689Skan{
1323169689Skan  unsigned int regn = REGNO (reg);
1324169689Skan  size_t i;
1325169689Skan  struct queued_reg_save *q;
1326169689Skan
1327169689Skan  for (q = queued_reg_saves; q; q = q->next)
1328169689Skan    if (q->saved_reg && regn == REGNO (q->saved_reg))
1329169689Skan      return q->reg;
1330169689Skan
1331169689Skan  for (i = 0; i < num_regs_saved_in_regs; i++)
1332169689Skan    if (regs_saved_in_regs[i].saved_in_reg
1333169689Skan	&& regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1334169689Skan      return regs_saved_in_regs[i].orig_reg;
1335169689Skan
1336169689Skan  return NULL_RTX;
1337169689Skan}
1338169689Skan
1339169689Skan
134090075Sobrien/* A temporary register holding an integral value used in adjusting SP
134190075Sobrien   or setting up the store_reg.  The "offset" field holds the integer
134290075Sobrien   value, not an offset.  */
134390075Sobrienstatic dw_cfa_location cfa_temp;
134490075Sobrien
134590075Sobrien/* Record call frame debugging information for an expression EXPR,
134690075Sobrien   which either sets SP or FP (adjusting how we calculate the frame
1347169689Skan   address) or saves a register to the stack or another register.
1348169689Skan   LABEL indicates the address of EXPR.
134990075Sobrien
135090075Sobrien   This function encodes a state machine mapping rtxes to actions on
135190075Sobrien   cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
135290075Sobrien   users need not read the source code.
135390075Sobrien
135490075Sobrien  The High-Level Picture
135590075Sobrien
135690075Sobrien  Changes in the register we use to calculate the CFA: Currently we
135790075Sobrien  assume that if you copy the CFA register into another register, we
135890075Sobrien  should take the other one as the new CFA register; this seems to
135990075Sobrien  work pretty well.  If it's wrong for some target, it's simple
136090075Sobrien  enough not to set RTX_FRAME_RELATED_P on the insn in question.
136190075Sobrien
136290075Sobrien  Changes in the register we use for saving registers to the stack:
136390075Sobrien  This is usually SP, but not always.  Again, we deduce that if you
136490075Sobrien  copy SP into another register (and SP is not the CFA register),
136590075Sobrien  then the new register is the one we will be using for register
136690075Sobrien  saves.  This also seems to work.
136790075Sobrien
136890075Sobrien  Register saves: There's not much guesswork about this one; if
136990075Sobrien  RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
137090075Sobrien  register save, and the register used to calculate the destination
137190075Sobrien  had better be the one we think we're using for this purpose.
1372169689Skan  It's also assumed that a copy from a call-saved register to another
1373169689Skan  register is saving that register if RTX_FRAME_RELATED_P is set on
1374169689Skan  that instruction.  If the copy is from a call-saved register to
1375169689Skan  the *same* register, that means that the register is now the same
1376169689Skan  value as in the caller.
137790075Sobrien
137890075Sobrien  Except: If the register being saved is the CFA register, and the
1379117395Skan  offset is nonzero, we are saving the CFA, so we assume we have to
138090075Sobrien  use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
138190075Sobrien  the intent is to save the value of SP from the previous frame.
138290075Sobrien
1383169689Skan  In addition, if a register has previously been saved to a different
1384169689Skan  register,
1385169689Skan
138690075Sobrien  Invariants / Summaries of Rules
138790075Sobrien
138890075Sobrien  cfa	       current rule for calculating the CFA.  It usually
138990075Sobrien	       consists of a register and an offset.
139090075Sobrien  cfa_store    register used by prologue code to save things to the stack
139190075Sobrien	       cfa_store.offset is the offset from the value of
139290075Sobrien	       cfa_store.reg to the actual CFA
139390075Sobrien  cfa_temp     register holding an integral value.  cfa_temp.offset
139490075Sobrien	       stores the value, which will be used to adjust the
139590075Sobrien	       stack pointer.  cfa_temp is also used like cfa_store,
139690075Sobrien	       to track stores to the stack via fp or a temp reg.
1397117395Skan
139890075Sobrien  Rules  1- 4: Setting a register's value to cfa.reg or an expression
1399132718Skan	       with cfa.reg as the first operand changes the cfa.reg and its
140090075Sobrien	       cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
140190075Sobrien	       cfa_temp.offset.
140290075Sobrien
140390075Sobrien  Rules  6- 9: Set a non-cfa.reg register value to a constant or an
140490075Sobrien	       expression yielding a constant.  This sets cfa_temp.reg
140590075Sobrien	       and cfa_temp.offset.
140690075Sobrien
140790075Sobrien  Rule 5:      Create a new register cfa_store used to save items to the
140890075Sobrien	       stack.
140990075Sobrien
141090075Sobrien  Rules 10-14: Save a register to the stack.  Define offset as the
141190075Sobrien	       difference of the original location and cfa_store's
141290075Sobrien	       location (or cfa_temp's location if cfa_temp is used).
141390075Sobrien
141490075Sobrien  The Rules
141590075Sobrien
141690075Sobrien  "{a,b}" indicates a choice of a xor b.
141790075Sobrien  "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
141890075Sobrien
141990075Sobrien  Rule 1:
142090075Sobrien  (set <reg1> <reg2>:cfa.reg)
142190075Sobrien  effects: cfa.reg = <reg1>
1422132718Skan	   cfa.offset unchanged
142390075Sobrien	   cfa_temp.reg = <reg1>
142490075Sobrien	   cfa_temp.offset = cfa.offset
142590075Sobrien
142690075Sobrien  Rule 2:
142790075Sobrien  (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
142890075Sobrien			      {<const_int>,<reg>:cfa_temp.reg}))
142990075Sobrien  effects: cfa.reg = sp if fp used
1430132718Skan	   cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
143190075Sobrien	   cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
143290075Sobrien	     if cfa_store.reg==sp
143390075Sobrien
143490075Sobrien  Rule 3:
143590075Sobrien  (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
143690075Sobrien  effects: cfa.reg = fp
1437132718Skan	   cfa_offset += +/- <const_int>
143890075Sobrien
143990075Sobrien  Rule 4:
144090075Sobrien  (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
144190075Sobrien  constraints: <reg1> != fp
1442132718Skan	       <reg1> != sp
144390075Sobrien  effects: cfa.reg = <reg1>
144490075Sobrien	   cfa_temp.reg = <reg1>
144590075Sobrien	   cfa_temp.offset = cfa.offset
144690075Sobrien
144790075Sobrien  Rule 5:
144890075Sobrien  (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
144990075Sobrien  constraints: <reg1> != fp
1450132718Skan	       <reg1> != sp
145190075Sobrien  effects: cfa_store.reg = <reg1>
1452132718Skan	   cfa_store.offset = cfa.offset - cfa_temp.offset
145390075Sobrien
145490075Sobrien  Rule 6:
145590075Sobrien  (set <reg> <const_int>)
145690075Sobrien  effects: cfa_temp.reg = <reg>
1457132718Skan	   cfa_temp.offset = <const_int>
145890075Sobrien
145990075Sobrien  Rule 7:
146090075Sobrien  (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
146190075Sobrien  effects: cfa_temp.reg = <reg1>
146290075Sobrien	   cfa_temp.offset |= <const_int>
146390075Sobrien
146490075Sobrien  Rule 8:
146590075Sobrien  (set <reg> (high <exp>))
146690075Sobrien  effects: none
146790075Sobrien
146890075Sobrien  Rule 9:
146990075Sobrien  (set <reg> (lo_sum <exp> <const_int>))
147090075Sobrien  effects: cfa_temp.reg = <reg>
1471132718Skan	   cfa_temp.offset = <const_int>
147290075Sobrien
147390075Sobrien  Rule 10:
147490075Sobrien  (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
147590075Sobrien  effects: cfa_store.offset -= <const_int>
147690075Sobrien	   cfa.offset = cfa_store.offset if cfa.reg == sp
147790075Sobrien	   cfa.reg = sp
147890075Sobrien	   cfa.base_offset = -cfa_store.offset
147990075Sobrien
148090075Sobrien  Rule 11:
148190075Sobrien  (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
148290075Sobrien  effects: cfa_store.offset += -/+ mode_size(mem)
148390075Sobrien	   cfa.offset = cfa_store.offset if cfa.reg == sp
148490075Sobrien	   cfa.reg = sp
148590075Sobrien	   cfa.base_offset = -cfa_store.offset
148690075Sobrien
148790075Sobrien  Rule 12:
148890075Sobrien  (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
148990075Sobrien
149090075Sobrien       <reg2>)
149190075Sobrien  effects: cfa.reg = <reg1>
149290075Sobrien	   cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
149390075Sobrien
149490075Sobrien  Rule 13:
149590075Sobrien  (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
149690075Sobrien  effects: cfa.reg = <reg1>
149790075Sobrien	   cfa.base_offset = -{cfa_store,cfa_temp}.offset
149890075Sobrien
149990075Sobrien  Rule 14:
150090075Sobrien  (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
150190075Sobrien  effects: cfa.reg = <reg1>
150290075Sobrien	   cfa.base_offset = -cfa_temp.offset
1503169689Skan	   cfa_temp.offset -= mode_size(mem)
150490075Sobrien
1505169689Skan  Rule 15:
1506169689Skan  (set <reg> {unspec, unspec_volatile})
1507169689Skan  effects: target-dependent  */
1508169689Skan
150990075Sobrienstatic void
1510132718Skandwarf2out_frame_debug_expr (rtx expr, const char *label)
151150397Sobrien{
151250397Sobrien  rtx src, dest;
151390075Sobrien  HOST_WIDE_INT offset;
151450397Sobrien
151590075Sobrien  /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
151690075Sobrien     the PARALLEL independently. The first element is always processed if
151790075Sobrien     it is a SET. This is for backward compatibility.   Other elements
151890075Sobrien     are processed only if they are SETs and the RTX_FRAME_RELATED_P
151990075Sobrien     flag is set in them.  */
152090075Sobrien  if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
152190075Sobrien    {
152252284Sobrien      int par_index;
152352284Sobrien      int limit = XVECLEN (expr, 0);
152450397Sobrien
152552284Sobrien      for (par_index = 0; par_index < limit; par_index++)
152690075Sobrien	if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
152790075Sobrien	    && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
152890075Sobrien		|| par_index == 0))
152990075Sobrien	  dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
153090075Sobrien
153150397Sobrien      return;
153250397Sobrien    }
153390075Sobrien
1534169689Skan  gcc_assert (GET_CODE (expr) == SET);
153550397Sobrien
153652284Sobrien  src = SET_SRC (expr);
153752284Sobrien  dest = SET_DEST (expr);
153850397Sobrien
1539169689Skan  if (REG_P (src))
1540169689Skan    {
1541169689Skan      rtx rsi = reg_saved_in (src);
1542169689Skan      if (rsi)
1543169689Skan	src = rsi;
1544169689Skan    }
1545169689Skan
154650397Sobrien  switch (GET_CODE (dest))
154750397Sobrien    {
154850397Sobrien    case REG:
154950397Sobrien      switch (GET_CODE (src))
155090075Sobrien	{
155190075Sobrien	  /* Setting FP from SP.  */
155290075Sobrien	case REG:
155390075Sobrien	  if (cfa.reg == (unsigned) REGNO (src))
1554169689Skan	    {
1555169689Skan	      /* Rule 1 */
1556169689Skan	      /* Update the CFA rule wrt SP or FP.  Make sure src is
1557169689Skan		 relative to the current CFA register.
1558169689Skan
1559169689Skan		 We used to require that dest be either SP or FP, but the
1560169689Skan		 ARM copies SP to a temporary register, and from there to
1561169689Skan		 FP.  So we just rely on the backends to only set
1562169689Skan		 RTX_FRAME_RELATED_P on appropriate insns.  */
1563169689Skan	      cfa.reg = REGNO (dest);
1564169689Skan	      cfa_temp.reg = cfa.reg;
1565169689Skan	      cfa_temp.offset = cfa.offset;
1566169689Skan	    }
156790075Sobrien	  else
1568169689Skan	    {
1569169689Skan	      /* Saving a register in a register.  */
1570169689Skan	      gcc_assert (!fixed_regs [REGNO (dest)]
1571169689Skan			  /* For the SPARC and its register window.  */
1572169689Skan			  || (DWARF_FRAME_REGNUM (REGNO (src))
1573169689Skan			      == DWARF_FRAME_RETURN_COLUMN));
1574169689Skan	      queue_reg_save (label, src, dest, 0);
1575169689Skan	    }
157690075Sobrien	  break;
157750397Sobrien
157890075Sobrien	case PLUS:
157990075Sobrien	case MINUS:
158090075Sobrien	case LO_SUM:
158190075Sobrien	  if (dest == stack_pointer_rtx)
158290075Sobrien	    {
158390075Sobrien	      /* Rule 2 */
158490075Sobrien	      /* Adjusting SP.  */
158590075Sobrien	      switch (GET_CODE (XEXP (src, 1)))
158690075Sobrien		{
158790075Sobrien		case CONST_INT:
158890075Sobrien		  offset = INTVAL (XEXP (src, 1));
158990075Sobrien		  break;
159090075Sobrien		case REG:
1591169689Skan		  gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1592169689Skan			      == cfa_temp.reg);
159390075Sobrien		  offset = cfa_temp.offset;
159490075Sobrien		  break;
159590075Sobrien		default:
1596169689Skan		  gcc_unreachable ();
159790075Sobrien		}
159850397Sobrien
159990075Sobrien	      if (XEXP (src, 0) == hard_frame_pointer_rtx)
160090075Sobrien		{
160190075Sobrien		  /* Restoring SP from FP in the epilogue.  */
1602169689Skan		  gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
160390075Sobrien		  cfa.reg = STACK_POINTER_REGNUM;
160490075Sobrien		}
160590075Sobrien	      else if (GET_CODE (src) == LO_SUM)
160690075Sobrien		/* Assume we've set the source reg of the LO_SUM from sp.  */
160790075Sobrien		;
1608169689Skan	      else
1609169689Skan		gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
161050397Sobrien
161190075Sobrien	      if (GET_CODE (src) != MINUS)
161290075Sobrien		offset = -offset;
161390075Sobrien	      if (cfa.reg == STACK_POINTER_REGNUM)
161490075Sobrien		cfa.offset += offset;
161590075Sobrien	      if (cfa_store.reg == STACK_POINTER_REGNUM)
161690075Sobrien		cfa_store.offset += offset;
161790075Sobrien	    }
161890075Sobrien	  else if (dest == hard_frame_pointer_rtx)
161990075Sobrien	    {
162090075Sobrien	      /* Rule 3 */
162190075Sobrien	      /* Either setting the FP from an offset of the SP,
162290075Sobrien		 or adjusting the FP */
1623169689Skan	      gcc_assert (frame_pointer_needed);
162450397Sobrien
1625169689Skan	      gcc_assert (REG_P (XEXP (src, 0))
1626169689Skan			  && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1627169689Skan			  && GET_CODE (XEXP (src, 1)) == CONST_INT);
1628169689Skan	      offset = INTVAL (XEXP (src, 1));
1629169689Skan	      if (GET_CODE (src) != MINUS)
1630169689Skan		offset = -offset;
1631169689Skan	      cfa.offset += offset;
1632169689Skan	      cfa.reg = HARD_FRAME_POINTER_REGNUM;
163390075Sobrien	    }
163490075Sobrien	  else
163590075Sobrien	    {
1636169689Skan	      gcc_assert (GET_CODE (src) != MINUS);
163750397Sobrien
163890075Sobrien	      /* Rule 4 */
1639169689Skan	      if (REG_P (XEXP (src, 0))
164090075Sobrien		  && REGNO (XEXP (src, 0)) == cfa.reg
164190075Sobrien		  && GET_CODE (XEXP (src, 1)) == CONST_INT)
164290075Sobrien		{
164390075Sobrien		  /* Setting a temporary CFA register that will be copied
164490075Sobrien		     into the FP later on.  */
164590075Sobrien		  offset = - INTVAL (XEXP (src, 1));
164690075Sobrien		  cfa.offset += offset;
164790075Sobrien		  cfa.reg = REGNO (dest);
164890075Sobrien		  /* Or used to save regs to the stack.  */
164990075Sobrien		  cfa_temp.reg = cfa.reg;
165090075Sobrien		  cfa_temp.offset = cfa.offset;
165190075Sobrien		}
165250397Sobrien
165390075Sobrien	      /* Rule 5 */
1654169689Skan	      else if (REG_P (XEXP (src, 0))
165590075Sobrien		       && REGNO (XEXP (src, 0)) == cfa_temp.reg
165690075Sobrien		       && XEXP (src, 1) == stack_pointer_rtx)
165790075Sobrien		{
165890075Sobrien		  /* Setting a scratch register that we will use instead
165990075Sobrien		     of SP for saving registers to the stack.  */
1660169689Skan		  gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
166190075Sobrien		  cfa_store.reg = REGNO (dest);
166290075Sobrien		  cfa_store.offset = cfa.offset - cfa_temp.offset;
166390075Sobrien		}
166450397Sobrien
166590075Sobrien	      /* Rule 9 */
166690075Sobrien	      else if (GET_CODE (src) == LO_SUM
166790075Sobrien		       && GET_CODE (XEXP (src, 1)) == CONST_INT)
166890075Sobrien		{
166990075Sobrien		  cfa_temp.reg = REGNO (dest);
167090075Sobrien		  cfa_temp.offset = INTVAL (XEXP (src, 1));
167190075Sobrien		}
167290075Sobrien	      else
1673169689Skan		gcc_unreachable ();
167490075Sobrien	    }
167590075Sobrien	  break;
167650397Sobrien
167790075Sobrien	  /* Rule 6 */
167890075Sobrien	case CONST_INT:
167990075Sobrien	  cfa_temp.reg = REGNO (dest);
168090075Sobrien	  cfa_temp.offset = INTVAL (src);
168190075Sobrien	  break;
168250397Sobrien
168390075Sobrien	  /* Rule 7 */
168490075Sobrien	case IOR:
1685169689Skan	  gcc_assert (REG_P (XEXP (src, 0))
1686169689Skan		      && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1687169689Skan		      && GET_CODE (XEXP (src, 1)) == CONST_INT);
168850397Sobrien
168990075Sobrien	  if ((unsigned) REGNO (dest) != cfa_temp.reg)
169090075Sobrien	    cfa_temp.reg = REGNO (dest);
169190075Sobrien	  cfa_temp.offset |= INTVAL (XEXP (src, 1));
169290075Sobrien	  break;
169350397Sobrien
169490075Sobrien	  /* Skip over HIGH, assuming it will be followed by a LO_SUM,
169590075Sobrien	     which will fill in all of the bits.  */
169690075Sobrien	  /* Rule 8 */
169790075Sobrien	case HIGH:
169890075Sobrien	  break;
169950397Sobrien
1700169689Skan	  /* Rule 15 */
1701169689Skan	case UNSPEC:
1702169689Skan	case UNSPEC_VOLATILE:
1703169689Skan	  gcc_assert (targetm.dwarf_handle_frame_unspec);
1704169689Skan	  targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1705169689Skan	  return;
1706169689Skan
170790075Sobrien	default:
1708169689Skan	  gcc_unreachable ();
170990075Sobrien	}
171050397Sobrien
171190075Sobrien      def_cfa_1 (label, &cfa);
171290075Sobrien      break;
171350397Sobrien
171490075Sobrien    case MEM:
1715169689Skan      gcc_assert (REG_P (src));
171652284Sobrien
171790075Sobrien      /* Saving a register to the stack.  Make sure dest is relative to the
171890075Sobrien	 CFA register.  */
171990075Sobrien      switch (GET_CODE (XEXP (dest, 0)))
172090075Sobrien	{
172190075Sobrien	  /* Rule 10 */
172290075Sobrien	  /* With a push.  */
172390075Sobrien	case PRE_MODIFY:
172490075Sobrien	  /* We can't handle variable size modifications.  */
1725169689Skan	  gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1726169689Skan		      == CONST_INT);
172790075Sobrien	  offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
172890075Sobrien
1729169689Skan	  gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1730169689Skan		      && cfa_store.reg == STACK_POINTER_REGNUM);
173190075Sobrien
173290075Sobrien	  cfa_store.offset += offset;
173390075Sobrien	  if (cfa.reg == STACK_POINTER_REGNUM)
173490075Sobrien	    cfa.offset = cfa_store.offset;
173590075Sobrien
173690075Sobrien	  offset = -cfa_store.offset;
173790075Sobrien	  break;
173890075Sobrien
173990075Sobrien	  /* Rule 11 */
174090075Sobrien	case PRE_INC:
174190075Sobrien	case PRE_DEC:
174290075Sobrien	  offset = GET_MODE_SIZE (GET_MODE (dest));
174390075Sobrien	  if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
174490075Sobrien	    offset = -offset;
174590075Sobrien
1746169689Skan	  gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1747169689Skan		      && cfa_store.reg == STACK_POINTER_REGNUM);
174890075Sobrien
174990075Sobrien	  cfa_store.offset += offset;
175090075Sobrien	  if (cfa.reg == STACK_POINTER_REGNUM)
175190075Sobrien	    cfa.offset = cfa_store.offset;
175290075Sobrien
175390075Sobrien	  offset = -cfa_store.offset;
175490075Sobrien	  break;
175590075Sobrien
175690075Sobrien	  /* Rule 12 */
175790075Sobrien	  /* With an offset.  */
175890075Sobrien	case PLUS:
175990075Sobrien	case MINUS:
176090075Sobrien	case LO_SUM:
1761169689Skan	  {
1762169689Skan	    int regno;
176390075Sobrien
1764169689Skan	    gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1765169689Skan			&& REG_P (XEXP (XEXP (dest, 0), 0)));
1766169689Skan	    offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1767169689Skan	    if (GET_CODE (XEXP (dest, 0)) == MINUS)
1768169689Skan	      offset = -offset;
1769169689Skan
1770169689Skan	    regno = REGNO (XEXP (XEXP (dest, 0), 0));
1771169689Skan
1772169689Skan	    if (cfa_store.reg == (unsigned) regno)
1773169689Skan	      offset -= cfa_store.offset;
1774169689Skan	    else
1775169689Skan	      {
1776169689Skan		gcc_assert (cfa_temp.reg == (unsigned) regno);
1777169689Skan		offset -= cfa_temp.offset;
1778169689Skan	      }
1779169689Skan	  }
178090075Sobrien	  break;
178190075Sobrien
178290075Sobrien	  /* Rule 13 */
178390075Sobrien	  /* Without an offset.  */
178490075Sobrien	case REG:
1785169689Skan	  {
1786169689Skan	    int regno = REGNO (XEXP (dest, 0));
1787169689Skan
1788169689Skan	    if (cfa_store.reg == (unsigned) regno)
1789169689Skan	      offset = -cfa_store.offset;
1790169689Skan	    else
1791169689Skan	      {
1792169689Skan		gcc_assert (cfa_temp.reg == (unsigned) regno);
1793169689Skan		offset = -cfa_temp.offset;
1794169689Skan	      }
1795169689Skan	  }
179690075Sobrien	  break;
179790075Sobrien
179890075Sobrien	  /* Rule 14 */
179990075Sobrien	case POST_INC:
1800169689Skan	  gcc_assert (cfa_temp.reg
1801169689Skan		      == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
180290075Sobrien	  offset = -cfa_temp.offset;
180390075Sobrien	  cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
180490075Sobrien	  break;
180590075Sobrien
180690075Sobrien	default:
1807169689Skan	  gcc_unreachable ();
180890075Sobrien	}
180990075Sobrien
181090075Sobrien      if (REGNO (src) != STACK_POINTER_REGNUM
181190075Sobrien	  && REGNO (src) != HARD_FRAME_POINTER_REGNUM
181290075Sobrien	  && (unsigned) REGNO (src) == cfa.reg)
181390075Sobrien	{
181490075Sobrien	  /* We're storing the current CFA reg into the stack.  */
181590075Sobrien
181690075Sobrien	  if (cfa.offset == 0)
181790075Sobrien	    {
181890075Sobrien	      /* If the source register is exactly the CFA, assume
181990075Sobrien		 we're saving SP like any other register; this happens
182090075Sobrien		 on the ARM.  */
182190075Sobrien	      def_cfa_1 (label, &cfa);
1822169689Skan	      queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
182390075Sobrien	      break;
182490075Sobrien	    }
182590075Sobrien	  else
182690075Sobrien	    {
182790075Sobrien	      /* Otherwise, we'll need to look in the stack to
1828132718Skan		 calculate the CFA.  */
182990075Sobrien	      rtx x = XEXP (dest, 0);
183090075Sobrien
1831169689Skan	      if (!REG_P (x))
183290075Sobrien		x = XEXP (x, 0);
1833169689Skan	      gcc_assert (REG_P (x));
183490075Sobrien
183590075Sobrien	      cfa.reg = REGNO (x);
183690075Sobrien	      cfa.base_offset = offset;
183790075Sobrien	      cfa.indirect = 1;
183890075Sobrien	      def_cfa_1 (label, &cfa);
183990075Sobrien	      break;
184090075Sobrien	    }
184190075Sobrien	}
184290075Sobrien
184390075Sobrien      def_cfa_1 (label, &cfa);
1844169689Skan      queue_reg_save (label, src, NULL_RTX, offset);
184590075Sobrien      break;
184690075Sobrien
184790075Sobrien    default:
1848169689Skan      gcc_unreachable ();
184990075Sobrien    }
185052284Sobrien}
185152284Sobrien
185252284Sobrien/* Record call frame debugging information for INSN, which either
185352284Sobrien   sets SP or FP (adjusting how we calculate the frame address) or saves a
1854169689Skan   register to the stack.  If INSN is NULL_RTX, initialize our state.
185552284Sobrien
1856169689Skan   If AFTER_P is false, we're being called before the insn is emitted,
1857169689Skan   otherwise after.  Call instructions get invoked twice.  */
1858169689Skan
185952284Sobrienvoid
1860169689Skandwarf2out_frame_debug (rtx insn, bool after_p)
186152284Sobrien{
186290075Sobrien  const char *label;
186352284Sobrien  rtx src;
186452284Sobrien
186552284Sobrien  if (insn == NULL_RTX)
186652284Sobrien    {
1867169689Skan      size_t i;
1868169689Skan
186990075Sobrien      /* Flush any queued register saves.  */
187090075Sobrien      flush_queued_reg_saves ();
187190075Sobrien
187252284Sobrien      /* Set up state for generating call frame debug info.  */
187390075Sobrien      lookup_cfa (&cfa);
1874169689Skan      gcc_assert (cfa.reg
1875169689Skan		  == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
187690075Sobrien
187790075Sobrien      cfa.reg = STACK_POINTER_REGNUM;
187890075Sobrien      cfa_store = cfa;
187990075Sobrien      cfa_temp.reg = -1;
188090075Sobrien      cfa_temp.offset = 0;
1881169689Skan
1882169689Skan      for (i = 0; i < num_regs_saved_in_regs; i++)
1883169689Skan	{
1884169689Skan	  regs_saved_in_regs[i].orig_reg = NULL_RTX;
1885169689Skan	  regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1886169689Skan	}
1887169689Skan      num_regs_saved_in_regs = 0;
188852284Sobrien      return;
188950397Sobrien    }
189052284Sobrien
1891169689Skan  if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
189290075Sobrien    flush_queued_reg_saves ();
189390075Sobrien
189452284Sobrien  if (! RTX_FRAME_RELATED_P (insn))
189552284Sobrien    {
189690075Sobrien      if (!ACCUMULATE_OUTGOING_ARGS)
1897169689Skan	dwarf2out_stack_adjust (insn, after_p);
189852284Sobrien      return;
189952284Sobrien    }
190052284Sobrien
190152284Sobrien  label = dwarf2out_cfi_label ();
190252284Sobrien  src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
190352284Sobrien  if (src)
190452284Sobrien    insn = XEXP (src, 0);
190590075Sobrien  else
190652284Sobrien    insn = PATTERN (insn);
190752284Sobrien
190852284Sobrien  dwarf2out_frame_debug_expr (insn, label);
190950397Sobrien}
191050397Sobrien
1911132718Skan#endif
1912132718Skan
1913132718Skan/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1914132718Skanstatic enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1915132718Skan (enum dwarf_call_frame_info cfi);
1916132718Skan
1917132718Skanstatic enum dw_cfi_oprnd_type
1918132718Skandw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1919132718Skan{
1920132718Skan  switch (cfi)
1921132718Skan    {
1922132718Skan    case DW_CFA_nop:
1923132718Skan    case DW_CFA_GNU_window_save:
1924132718Skan      return dw_cfi_oprnd_unused;
1925132718Skan
1926132718Skan    case DW_CFA_set_loc:
1927132718Skan    case DW_CFA_advance_loc1:
1928132718Skan    case DW_CFA_advance_loc2:
1929132718Skan    case DW_CFA_advance_loc4:
1930132718Skan    case DW_CFA_MIPS_advance_loc8:
1931132718Skan      return dw_cfi_oprnd_addr;
1932132718Skan
1933132718Skan    case DW_CFA_offset:
1934132718Skan    case DW_CFA_offset_extended:
1935132718Skan    case DW_CFA_def_cfa:
1936132718Skan    case DW_CFA_offset_extended_sf:
1937132718Skan    case DW_CFA_def_cfa_sf:
1938132718Skan    case DW_CFA_restore_extended:
1939132718Skan    case DW_CFA_undefined:
1940132718Skan    case DW_CFA_same_value:
1941132718Skan    case DW_CFA_def_cfa_register:
1942132718Skan    case DW_CFA_register:
1943132718Skan      return dw_cfi_oprnd_reg_num;
1944132718Skan
1945132718Skan    case DW_CFA_def_cfa_offset:
1946132718Skan    case DW_CFA_GNU_args_size:
1947132718Skan    case DW_CFA_def_cfa_offset_sf:
1948132718Skan      return dw_cfi_oprnd_offset;
1949132718Skan
1950132718Skan    case DW_CFA_def_cfa_expression:
1951132718Skan    case DW_CFA_expression:
1952132718Skan      return dw_cfi_oprnd_loc;
1953132718Skan
1954132718Skan    default:
1955169689Skan      gcc_unreachable ();
1956132718Skan    }
1957132718Skan}
1958132718Skan
1959132718Skan/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1960132718Skanstatic enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1961132718Skan (enum dwarf_call_frame_info cfi);
1962132718Skan
1963132718Skanstatic enum dw_cfi_oprnd_type
1964132718Skandw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1965132718Skan{
1966132718Skan  switch (cfi)
1967132718Skan    {
1968132718Skan    case DW_CFA_def_cfa:
1969132718Skan    case DW_CFA_def_cfa_sf:
1970132718Skan    case DW_CFA_offset:
1971132718Skan    case DW_CFA_offset_extended_sf:
1972132718Skan    case DW_CFA_offset_extended:
1973132718Skan      return dw_cfi_oprnd_offset;
1974132718Skan
1975132718Skan    case DW_CFA_register:
1976132718Skan      return dw_cfi_oprnd_reg_num;
1977132718Skan
1978132718Skan    default:
1979132718Skan      return dw_cfi_oprnd_unused;
1980132718Skan    }
1981132718Skan}
1982132718Skan
1983132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1984132718Skan
1985169689Skan/* Switch to eh_frame_section.  If we don't have an eh_frame_section,
1986169689Skan   switch to the data section instead, and write out a synthetic label
1987169689Skan   for collect2.  */
1988169689Skan
1989169689Skanstatic void
1990169689Skanswitch_to_eh_frame_section (void)
1991169689Skan{
1992169689Skan  tree label;
1993169689Skan
1994169689Skan#ifdef EH_FRAME_SECTION_NAME
1995169689Skan  if (eh_frame_section == 0)
1996169689Skan    {
1997169689Skan      int flags;
1998169689Skan
1999169689Skan      if (EH_TABLES_CAN_BE_READ_ONLY)
2000169689Skan	{
2001169689Skan	  int fde_encoding;
2002169689Skan	  int per_encoding;
2003169689Skan	  int lsda_encoding;
2004169689Skan
2005169689Skan	  fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2006169689Skan						       /*global=*/0);
2007169689Skan	  per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2008169689Skan						       /*global=*/1);
2009169689Skan	  lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2010169689Skan							/*global=*/0);
2011169689Skan	  flags = ((! flag_pic
2012169689Skan		    || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2013169689Skan			&& (fde_encoding & 0x70) != DW_EH_PE_aligned
2014169689Skan			&& (per_encoding & 0x70) != DW_EH_PE_absptr
2015169689Skan			&& (per_encoding & 0x70) != DW_EH_PE_aligned
2016169689Skan			&& (lsda_encoding & 0x70) != DW_EH_PE_absptr
2017169689Skan			&& (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2018169689Skan		   ? 0 : SECTION_WRITE);
2019169689Skan	}
2020169689Skan      else
2021169689Skan	flags = SECTION_WRITE;
2022169689Skan      eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2023169689Skan    }
2024132718Skan#endif
2025132718Skan
2026169689Skan  if (eh_frame_section)
2027169689Skan    switch_to_section (eh_frame_section);
2028169689Skan  else
2029169689Skan    {
2030169689Skan      /* We have no special eh_frame section.  Put the information in
2031169689Skan	 the data section and emit special labels to guide collect2.  */
2032169689Skan      switch_to_section (data_section);
2033169689Skan      label = get_file_function_name ('F');
2034169689Skan      ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2035169689Skan      targetm.asm_out.globalize_label (asm_out_file,
2036169689Skan				       IDENTIFIER_POINTER (label));
2037169689Skan      ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2038169689Skan    }
2039169689Skan}
2040169689Skan
204150397Sobrien/* Output a Call Frame Information opcode and its operand(s).  */
204250397Sobrien
204350397Sobrienstatic void
2044132718Skanoutput_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
204550397Sobrien{
2046132718Skan  unsigned long r;
204750397Sobrien  if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
204890075Sobrien    dw2_asm_output_data (1, (cfi->dw_cfi_opc
204990075Sobrien			     | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2050132718Skan			 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
205190075Sobrien			 cfi->dw_cfi_oprnd1.dw_cfi_offset);
205250397Sobrien  else if (cfi->dw_cfi_opc == DW_CFA_offset)
205350397Sobrien    {
2054132718Skan      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2055132718Skan      dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2056132718Skan			   "DW_CFA_offset, column 0x%lx", r);
205790075Sobrien      dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
205850397Sobrien    }
205950397Sobrien  else if (cfi->dw_cfi_opc == DW_CFA_restore)
2060132718Skan    {
2061132718Skan      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2062132718Skan      dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2063132718Skan			   "DW_CFA_restore, column 0x%lx", r);
2064132718Skan    }
206550397Sobrien  else
206650397Sobrien    {
206790075Sobrien      dw2_asm_output_data (1, cfi->dw_cfi_opc,
206890075Sobrien			   "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
206950397Sobrien
207050397Sobrien      switch (cfi->dw_cfi_opc)
207150397Sobrien	{
207250397Sobrien	case DW_CFA_set_loc:
207390075Sobrien	  if (for_eh)
207490075Sobrien	    dw2_asm_output_encoded_addr_rtx (
207590075Sobrien		ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
207690075Sobrien		gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2077169689Skan		false, NULL);
207890075Sobrien	  else
207990075Sobrien	    dw2_asm_output_addr (DWARF2_ADDR_SIZE,
208090075Sobrien				 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2081169689Skan	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
208250397Sobrien	  break;
208390075Sobrien
208450397Sobrien	case DW_CFA_advance_loc1:
208590075Sobrien	  dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
208690075Sobrien				fde->dw_fde_current_label, NULL);
208750397Sobrien	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
208850397Sobrien	  break;
208990075Sobrien
209050397Sobrien	case DW_CFA_advance_loc2:
209190075Sobrien	  dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
209290075Sobrien				fde->dw_fde_current_label, NULL);
209350397Sobrien	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
209450397Sobrien	  break;
209590075Sobrien
209650397Sobrien	case DW_CFA_advance_loc4:
209790075Sobrien	  dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
209890075Sobrien				fde->dw_fde_current_label, NULL);
209950397Sobrien	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
210050397Sobrien	  break;
210190075Sobrien
210250397Sobrien	case DW_CFA_MIPS_advance_loc8:
210390075Sobrien	  dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
210490075Sobrien				fde->dw_fde_current_label, NULL);
210590075Sobrien	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
210650397Sobrien	  break;
210790075Sobrien
210850397Sobrien	case DW_CFA_offset_extended:
210950397Sobrien	case DW_CFA_def_cfa:
2110132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2111132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
211290075Sobrien	  dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
211350397Sobrien	  break;
211490075Sobrien
211590075Sobrien	case DW_CFA_offset_extended_sf:
211690075Sobrien	case DW_CFA_def_cfa_sf:
2117132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2118132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
211990075Sobrien	  dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
212090075Sobrien	  break;
212190075Sobrien
212250397Sobrien	case DW_CFA_restore_extended:
212350397Sobrien	case DW_CFA_undefined:
212450397Sobrien	case DW_CFA_same_value:
212550397Sobrien	case DW_CFA_def_cfa_register:
2126132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2127132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
212850397Sobrien	  break;
212990075Sobrien
213050397Sobrien	case DW_CFA_register:
2131132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2132132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
2133132718Skan	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2134132718Skan	  dw2_asm_output_data_uleb128 (r, NULL);
213550397Sobrien	  break;
213690075Sobrien
213750397Sobrien	case DW_CFA_def_cfa_offset:
213890075Sobrien	case DW_CFA_GNU_args_size:
213990075Sobrien	  dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
214050397Sobrien	  break;
214190075Sobrien
214290075Sobrien	case DW_CFA_def_cfa_offset_sf:
214390075Sobrien	  dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
214490075Sobrien	  break;
214590075Sobrien
214650397Sobrien	case DW_CFA_GNU_window_save:
214750397Sobrien	  break;
214890075Sobrien
214990075Sobrien	case DW_CFA_def_cfa_expression:
215090075Sobrien	case DW_CFA_expression:
215190075Sobrien	  output_cfa_loc (cfi);
215250397Sobrien	  break;
215390075Sobrien
215490075Sobrien	case DW_CFA_GNU_negative_offset_extended:
215590075Sobrien	  /* Obsoleted by DW_CFA_offset_extended_sf.  */
2156169689Skan	  gcc_unreachable ();
215790075Sobrien
215850397Sobrien	default:
215950397Sobrien	  break;
216050397Sobrien	}
216190075Sobrien    }
216250397Sobrien}
216350397Sobrien
2164132718Skan/* Output the call frame information used to record information
216550397Sobrien   that relates to calculating the frame pointer, and records the
216650397Sobrien   location of saved registers.  */
216750397Sobrien
216850397Sobrienstatic void
2169132718Skanoutput_call_frame_info (int for_eh)
217050397Sobrien{
217190075Sobrien  unsigned int i;
217290075Sobrien  dw_fde_ref fde;
217390075Sobrien  dw_cfi_ref cfi;
217490075Sobrien  char l1[20], l2[20], section_start_label[20];
2175117395Skan  bool any_lsda_needed = false;
217690075Sobrien  char augmentation[6];
217790075Sobrien  int augmentation_size;
217890075Sobrien  int fde_encoding = DW_EH_PE_absptr;
217990075Sobrien  int per_encoding = DW_EH_PE_absptr;
218090075Sobrien  int lsda_encoding = DW_EH_PE_absptr;
2181169689Skan  int return_reg;
218250397Sobrien
2183117395Skan  /* Don't emit a CIE if there won't be any FDEs.  */
2184117395Skan  if (fde_table_in_use == 0)
2185117395Skan    return;
2186117395Skan
2187169689Skan  /* If we make FDEs linkonce, we may have to emit an empty label for
2188169689Skan     an FDE that wouldn't otherwise be emitted.  We want to avoid
2189169689Skan     having an FDE kept around when the function it refers to is
2190169689Skan     discarded.  Example where this matters: a primary function
2191169689Skan     template in C++ requires EH information, but an explicit
2192169689Skan     specialization doesn't.  */
2193169689Skan  if (TARGET_USES_WEAK_UNWIND_INFO
2194169689Skan      && ! flag_asynchronous_unwind_tables
2195169689Skan      && for_eh)
2196169689Skan    for (i = 0; i < fde_table_in_use; i++)
2197169689Skan      if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2198169689Skan          && !fde_table[i].uses_eh_lsda
2199169689Skan	  && ! DECL_WEAK (fde_table[i].decl))
2200169689Skan	targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2201169689Skan				      for_eh, /* empty */ 1);
2202169689Skan
2203117395Skan  /* If we don't have any functions we'll want to unwind out of, don't
2204117395Skan     emit any EH unwind information.  Note that if exceptions aren't
2205117395Skan     enabled, we won't have collected nothrow information, and if we
2206117395Skan     asked for asynchronous tables, we always want this info.  */
220790075Sobrien  if (for_eh)
220890075Sobrien    {
2209117395Skan      bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
221050397Sobrien
221190075Sobrien      for (i = 0; i < fde_table_in_use; i++)
221290075Sobrien	if (fde_table[i].uses_eh_lsda)
2213117395Skan	  any_eh_needed = any_lsda_needed = true;
2214169689Skan        else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2215169689Skan	  any_eh_needed = true;
2216132718Skan	else if (! fde_table[i].nothrow
2217132718Skan		 && ! fde_table[i].all_throwers_are_sibcalls)
2218117395Skan	  any_eh_needed = true;
221950397Sobrien
222090075Sobrien      if (! any_eh_needed)
222190075Sobrien	return;
222290075Sobrien    }
222390075Sobrien
222450397Sobrien  /* We're going to be generating comments, so turn on app.  */
222550397Sobrien  if (flag_debug_asm)
222650397Sobrien    app_enable ();
222750397Sobrien
222850397Sobrien  if (for_eh)
2229169689Skan    switch_to_eh_frame_section ();
223050397Sobrien  else
2231169689Skan    {
2232169689Skan      if (!debug_frame_section)
2233169689Skan	debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2234169689Skan					   SECTION_DEBUG, NULL);
2235169689Skan      switch_to_section (debug_frame_section);
2236169689Skan    }
223750397Sobrien
223890075Sobrien  ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
223990075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
224090075Sobrien
224190075Sobrien  /* Output the CIE.  */
224250397Sobrien  ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
224350397Sobrien  ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2244169689Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2245169689Skan    dw2_asm_output_data (4, 0xffffffff,
2246169689Skan      "Initial length escape value indicating 64-bit DWARF extension");
224790075Sobrien  dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
224890075Sobrien			"Length of Common Information Entry");
224950397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l1);
225050397Sobrien
225190075Sobrien  /* Now that the CIE pointer is PC-relative for EH,
225290075Sobrien     use 0 to identify the CIE.  */
225390075Sobrien  dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2254169689Skan		       (for_eh ? 0 : DWARF_CIE_ID),
225590075Sobrien		       "CIE Identifier Tag");
225650397Sobrien
225790075Sobrien  dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
225850397Sobrien
225990075Sobrien  augmentation[0] = 0;
226090075Sobrien  augmentation_size = 0;
226190075Sobrien  if (for_eh)
226250397Sobrien    {
226390075Sobrien      char *p;
226450397Sobrien
226590075Sobrien      /* Augmentation:
226690075Sobrien	 z	Indicates that a uleb128 is present to size the
2267132718Skan		augmentation section.
226890075Sobrien	 L	Indicates the encoding (and thus presence) of
226990075Sobrien		an LSDA pointer in the FDE augmentation.
227090075Sobrien	 R	Indicates a non-default pointer encoding for
227190075Sobrien		FDE code pointers.
227290075Sobrien	 P	Indicates the presence of an encoding + language
227390075Sobrien		personality routine in the CIE augmentation.  */
227450397Sobrien
227590075Sobrien      fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
227690075Sobrien      per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
227790075Sobrien      lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
227890075Sobrien
227990075Sobrien      p = augmentation + 1;
228090075Sobrien      if (eh_personality_libfunc)
228150397Sobrien	{
228290075Sobrien	  *p++ = 'P';
228390075Sobrien	  augmentation_size += 1 + size_of_encoded_value (per_encoding);
228450397Sobrien	}
228590075Sobrien      if (any_lsda_needed)
228650397Sobrien	{
228790075Sobrien	  *p++ = 'L';
228890075Sobrien	  augmentation_size += 1;
228950397Sobrien	}
229090075Sobrien      if (fde_encoding != DW_EH_PE_absptr)
229190075Sobrien	{
229290075Sobrien	  *p++ = 'R';
229390075Sobrien	  augmentation_size += 1;
229490075Sobrien	}
229590075Sobrien      if (p > augmentation + 1)
229690075Sobrien	{
229790075Sobrien	  augmentation[0] = 'z';
2298117395Skan	  *p = '\0';
229990075Sobrien	}
230050397Sobrien
230190075Sobrien      /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
230290075Sobrien      if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
230390075Sobrien	{
230490075Sobrien	  int offset = (  4		/* Length */
230590075Sobrien			+ 4		/* CIE Id */
230690075Sobrien			+ 1		/* CIE version */
230790075Sobrien			+ strlen (augmentation) + 1	/* Augmentation */
230890075Sobrien			+ size_of_uleb128 (1)		/* Code alignment */
230990075Sobrien			+ size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
231090075Sobrien			+ 1		/* RA column */
231190075Sobrien			+ 1		/* Augmentation size */
231290075Sobrien			+ 1		/* Personality encoding */ );
231390075Sobrien	  int pad = -offset & (PTR_SIZE - 1);
231490075Sobrien
231590075Sobrien	  augmentation_size += pad;
231690075Sobrien
231790075Sobrien	  /* Augmentations should be small, so there's scarce need to
231890075Sobrien	     iterate for a solution.  Die if we exceed one uleb128 byte.  */
2319169689Skan	  gcc_assert (size_of_uleb128 (augmentation_size) == 1);
232090075Sobrien	}
232150397Sobrien    }
232250397Sobrien
232390075Sobrien  dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
232490075Sobrien  dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
232590075Sobrien  dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
232690075Sobrien			       "CIE Data Alignment Factor");
232750397Sobrien
2328169689Skan  return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2329169689Skan  if (DW_CIE_VERSION == 1)
2330169689Skan    dw2_asm_output_data (1, return_reg, "CIE RA Column");
2331169689Skan  else
2332169689Skan    dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2333169689Skan
233490075Sobrien  if (augmentation[0])
233590075Sobrien    {
233690075Sobrien      dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
233790075Sobrien      if (eh_personality_libfunc)
233890075Sobrien	{
233990075Sobrien	  dw2_asm_output_data (1, per_encoding, "Personality (%s)",
234090075Sobrien			       eh_data_format_name (per_encoding));
234190075Sobrien	  dw2_asm_output_encoded_addr_rtx (per_encoding,
2342169689Skan					   eh_personality_libfunc,
2343169689Skan					   true, NULL);
234490075Sobrien	}
234550397Sobrien
234690075Sobrien      if (any_lsda_needed)
234790075Sobrien	dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
234890075Sobrien			     eh_data_format_name (lsda_encoding));
234950397Sobrien
235090075Sobrien      if (fde_encoding != DW_EH_PE_absptr)
235190075Sobrien	dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
235290075Sobrien			     eh_data_format_name (fde_encoding));
235390075Sobrien    }
235450397Sobrien
235550397Sobrien  for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
235690075Sobrien    output_cfi (cfi, NULL, for_eh);
235750397Sobrien
235850397Sobrien  /* Pad the CIE out to an address sized boundary.  */
2359117395Skan  ASM_OUTPUT_ALIGN (asm_out_file,
236090075Sobrien		    floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
236150397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l2);
236250397Sobrien
236350397Sobrien  /* Loop through all of the FDE's.  */
236490075Sobrien  for (i = 0; i < fde_table_in_use; i++)
236550397Sobrien    {
236650397Sobrien      fde = &fde_table[i];
236750397Sobrien
236890075Sobrien      /* Don't emit EH unwind info for leaf functions that don't need it.  */
2369117395Skan      if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2370117395Skan	  && (fde->nothrow || fde->all_throwers_are_sibcalls)
2371169689Skan	  && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2372117395Skan	  && !fde->uses_eh_lsda)
237390075Sobrien	continue;
237490075Sobrien
2375169689Skan      targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2376169689Skan      targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
237790075Sobrien      ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
237890075Sobrien      ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2379169689Skan      if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2380169689Skan	dw2_asm_output_data (4, 0xffffffff,
2381169689Skan			     "Initial length escape value indicating 64-bit DWARF extension");
238290075Sobrien      dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
238390075Sobrien			    "FDE Length");
238490075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, l1);
238590075Sobrien
238650397Sobrien      if (for_eh)
238790075Sobrien	dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
238850397Sobrien      else
238990075Sobrien	dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2390169689Skan			       debug_frame_section, "FDE CIE offset");
239150397Sobrien
239250397Sobrien      if (for_eh)
239390075Sobrien	{
2394169689Skan	  rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2395169689Skan	  SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
239690075Sobrien	  dw2_asm_output_encoded_addr_rtx (fde_encoding,
2397169689Skan					   sym_ref,
2398169689Skan					   false,
2399169689Skan					   "FDE initial location");
2400169689Skan	  if (fde->dw_fde_switched_sections)
2401169689Skan	    {
2402169689Skan	      rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2403169689Skan				      fde->dw_fde_unlikely_section_label);
2404169689Skan	      rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2405169689Skan				      fde->dw_fde_hot_section_label);
2406169689Skan	      SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2407169689Skan	      SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2408169689Skan	      dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2409169689Skan					       "FDE initial location");
2410169689Skan	      dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2411169689Skan				    fde->dw_fde_hot_section_end_label,
2412169689Skan				    fde->dw_fde_hot_section_label,
2413169689Skan				    "FDE address range");
2414169689Skan	      dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2415169689Skan					       "FDE initial location");
2416169689Skan	      dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2417169689Skan				    fde->dw_fde_unlikely_section_end_label,
2418169689Skan				    fde->dw_fde_unlikely_section_label,
2419169689Skan				    "FDE address range");
2420169689Skan	    }
2421169689Skan	  else
2422169689Skan	    dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2423169689Skan				  fde->dw_fde_end, fde->dw_fde_begin,
2424169689Skan				  "FDE address range");
242590075Sobrien	}
242650397Sobrien      else
242790075Sobrien	{
242890075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
242990075Sobrien			       "FDE initial location");
2430169689Skan	  if (fde->dw_fde_switched_sections)
2431169689Skan	    {
2432169689Skan	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2433169689Skan				   fde->dw_fde_hot_section_label,
2434169689Skan				   "FDE initial location");
2435169689Skan	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2436169689Skan				    fde->dw_fde_hot_section_end_label,
2437169689Skan				    fde->dw_fde_hot_section_label,
2438169689Skan				    "FDE address range");
2439169689Skan	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2440169689Skan				   fde->dw_fde_unlikely_section_label,
2441169689Skan				   "FDE initial location");
2442169689Skan	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2443169689Skan				    fde->dw_fde_unlikely_section_end_label,
2444169689Skan				    fde->dw_fde_unlikely_section_label,
2445169689Skan				    "FDE address range");
2446169689Skan	    }
2447169689Skan	  else
2448169689Skan	    dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2449169689Skan				  fde->dw_fde_end, fde->dw_fde_begin,
2450169689Skan				  "FDE address range");
245190075Sobrien	}
245250397Sobrien
245390075Sobrien      if (augmentation[0])
245490075Sobrien	{
245590075Sobrien	  if (any_lsda_needed)
245690075Sobrien	    {
245790075Sobrien	      int size = size_of_encoded_value (lsda_encoding);
245850397Sobrien
245990075Sobrien	      if (lsda_encoding == DW_EH_PE_aligned)
246090075Sobrien		{
246190075Sobrien		  int offset = (  4		/* Length */
246290075Sobrien				+ 4		/* CIE offset */
246390075Sobrien				+ 2 * size_of_encoded_value (fde_encoding)
246490075Sobrien				+ 1		/* Augmentation size */ );
246590075Sobrien		  int pad = -offset & (PTR_SIZE - 1);
246650397Sobrien
246790075Sobrien		  size += pad;
2468169689Skan		  gcc_assert (size_of_uleb128 (size) == 1);
246990075Sobrien		}
247050397Sobrien
247190075Sobrien	      dw2_asm_output_data_uleb128 (size, "Augmentation size");
247290075Sobrien
247390075Sobrien	      if (fde->uses_eh_lsda)
2474132718Skan		{
2475132718Skan		  ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
247690075Sobrien					       fde->funcdef_number);
2477132718Skan		  dw2_asm_output_encoded_addr_rtx (
247890075Sobrien			lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2479169689Skan			false, "Language Specific Data Area");
2480132718Skan		}
248190075Sobrien	      else
248290075Sobrien		{
248390075Sobrien		  if (lsda_encoding == DW_EH_PE_aligned)
248490075Sobrien		    ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
248590075Sobrien		  dw2_asm_output_data
248690075Sobrien		    (size_of_encoded_value (lsda_encoding), 0,
248790075Sobrien		     "Language Specific Data Area (none)");
248890075Sobrien		}
248990075Sobrien	    }
249090075Sobrien	  else
249190075Sobrien	    dw2_asm_output_data_uleb128 (0, "Augmentation size");
249290075Sobrien	}
249390075Sobrien
249450397Sobrien      /* Loop through the Call Frame Instructions associated with
249550397Sobrien	 this FDE.  */
249650397Sobrien      fde->dw_fde_current_label = fde->dw_fde_begin;
249750397Sobrien      for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
249890075Sobrien	output_cfi (cfi, fde, for_eh);
249950397Sobrien
250050397Sobrien      /* Pad the FDE out to an address sized boundary.  */
2501117395Skan      ASM_OUTPUT_ALIGN (asm_out_file,
2502132718Skan			floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
250350397Sobrien      ASM_OUTPUT_LABEL (asm_out_file, l2);
250450397Sobrien    }
250590075Sobrien
2506117395Skan  if (for_eh && targetm.terminate_dw2_eh_frame_info)
250790075Sobrien    dw2_asm_output_data (4, 0, "End of Table");
250850397Sobrien#ifdef MIPS_DEBUGGING_INFO
250950397Sobrien  /* Work around Irix 6 assembler bug whereby labels at the end of a section
251050397Sobrien     get a value of 0.  Putting .align 0 after the label fixes it.  */
251150397Sobrien  ASM_OUTPUT_ALIGN (asm_out_file, 0);
251250397Sobrien#endif
251350397Sobrien
251450397Sobrien  /* Turn off app to make assembly quicker.  */
251550397Sobrien  if (flag_debug_asm)
251650397Sobrien    app_disable ();
251750397Sobrien}
251850397Sobrien
251950397Sobrien/* Output a marker (i.e. a label) for the beginning of a function, before
252050397Sobrien   the prologue.  */
252150397Sobrien
252250397Sobrienvoid
2523132718Skandwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2524132718Skan			  const char *file ATTRIBUTE_UNUSED)
252550397Sobrien{
252650397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
2527169689Skan  char * dup_label;
252890075Sobrien  dw_fde_ref fde;
252950397Sobrien
2530169689Skan  current_function_func_begin_label = NULL;
253150397Sobrien
2532169689Skan#ifdef TARGET_UNWIND_INFO
253390075Sobrien  /* ??? current_function_func_begin_label is also used by except.c
253490075Sobrien     for call-site information.  We must emit this label if it might
253590075Sobrien     be used.  */
253690075Sobrien  if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
253790075Sobrien      && ! dwarf2out_do_frame ())
253890075Sobrien    return;
253990075Sobrien#else
254090075Sobrien  if (! dwarf2out_do_frame ())
254190075Sobrien    return;
254290075Sobrien#endif
254390075Sobrien
2544169689Skan  switch_to_section (function_section (current_function_decl));
254550397Sobrien  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2546117395Skan			       current_function_funcdef_no);
254790075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2548117395Skan			  current_function_funcdef_no);
2549169689Skan  dup_label = xstrdup (label);
2550169689Skan  current_function_func_begin_label = dup_label;
255150397Sobrien
2552169689Skan#ifdef TARGET_UNWIND_INFO
255390075Sobrien  /* We can elide the fde allocation if we're not emitting debug info.  */
255490075Sobrien  if (! dwarf2out_do_frame ())
255590075Sobrien    return;
255690075Sobrien#endif
255790075Sobrien
255850397Sobrien  /* Expand the fde table if necessary.  */
255950397Sobrien  if (fde_table_in_use == fde_table_allocated)
256050397Sobrien    {
256150397Sobrien      fde_table_allocated += FDE_TABLE_INCREMENT;
2562132718Skan      fde_table = ggc_realloc (fde_table,
2563132718Skan			       fde_table_allocated * sizeof (dw_fde_node));
2564132718Skan      memset (fde_table + fde_table_in_use, 0,
2565132718Skan	      FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
256650397Sobrien    }
256750397Sobrien
256850397Sobrien  /* Record the FDE associated with this function.  */
256950397Sobrien  current_funcdef_fde = fde_table_in_use;
257050397Sobrien
257150397Sobrien  /* Add the new FDE at the end of the fde_table.  */
257250397Sobrien  fde = &fde_table[fde_table_in_use++];
2573169689Skan  fde->decl = current_function_decl;
2574169689Skan  fde->dw_fde_begin = dup_label;
2575169689Skan  fde->dw_fde_current_label = dup_label;
2576169689Skan  fde->dw_fde_hot_section_label = NULL;
2577169689Skan  fde->dw_fde_hot_section_end_label = NULL;
2578169689Skan  fde->dw_fde_unlikely_section_label = NULL;
2579169689Skan  fde->dw_fde_unlikely_section_end_label = NULL;
2580169689Skan  fde->dw_fde_switched_sections = false;
258150397Sobrien  fde->dw_fde_end = NULL;
258250397Sobrien  fde->dw_fde_cfi = NULL;
2583117395Skan  fde->funcdef_number = current_function_funcdef_no;
2584169689Skan  fde->nothrow = TREE_NOTHROW (current_function_decl);
258590075Sobrien  fde->uses_eh_lsda = cfun->uses_eh_lsda;
2586117395Skan  fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
258750397Sobrien
258850397Sobrien  args_size = old_args_size = 0;
258990075Sobrien
259090075Sobrien  /* We only want to output line number information for the genuine dwarf2
259190075Sobrien     prologue case, not the eh frame case.  */
259290075Sobrien#ifdef DWARF2_DEBUGGING_INFO
259390075Sobrien  if (file)
259490075Sobrien    dwarf2out_source_line (line, file);
259590075Sobrien#endif
259650397Sobrien}
259750397Sobrien
259850397Sobrien/* Output a marker (i.e. a label) for the absolute end of the generated code
259950397Sobrien   for a function definition.  This gets called *after* the epilogue code has
260050397Sobrien   been generated.  */
260150397Sobrien
260250397Sobrienvoid
2603132718Skandwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2604132718Skan			const char *file ATTRIBUTE_UNUSED)
260550397Sobrien{
260650397Sobrien  dw_fde_ref fde;
260750397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
260850397Sobrien
260950397Sobrien  /* Output a label to mark the endpoint of the code generated for this
261090075Sobrien     function.  */
2611117395Skan  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2612117395Skan			       current_function_funcdef_no);
261350397Sobrien  ASM_OUTPUT_LABEL (asm_out_file, label);
261450397Sobrien  fde = &fde_table[fde_table_in_use - 1];
261550397Sobrien  fde->dw_fde_end = xstrdup (label);
261650397Sobrien}
261750397Sobrien
261850397Sobrienvoid
2619132718Skandwarf2out_frame_init (void)
262050397Sobrien{
262150397Sobrien  /* Allocate the initial hunk of the fde_table.  */
2622132718Skan  fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
262350397Sobrien  fde_table_allocated = FDE_TABLE_INCREMENT;
262450397Sobrien  fde_table_in_use = 0;
262550397Sobrien
262650397Sobrien  /* Generate the CFA instructions common to all FDE's.  Do it now for the
262750397Sobrien     sake of lookup_cfa.  */
262850397Sobrien
262950397Sobrien  /* On entry, the Canonical Frame Address is at SP.  */
263050397Sobrien  dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2631169689Skan
2632169689Skan#ifdef DWARF2_UNWIND_INFO
2633169689Skan  if (DWARF2_UNWIND_INFO)
2634169689Skan    initial_return_save (INCOMING_RETURN_ADDR_RTX);
263550397Sobrien#endif
263650397Sobrien}
263750397Sobrien
263850397Sobrienvoid
2639132718Skandwarf2out_frame_finish (void)
264050397Sobrien{
264150397Sobrien  /* Output call frame information.  */
2642169689Skan  if (DWARF2_FRAME_INFO)
264350397Sobrien    output_call_frame_info (0);
264490075Sobrien
2645169689Skan#ifndef TARGET_UNWIND_INFO
2646169689Skan  /* Output another copy for the unwinder.  */
264790075Sobrien  if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
264850397Sobrien    output_call_frame_info (1);
2649169689Skan#endif
265090075Sobrien}
2651132718Skan#endif
265290075Sobrien
265390075Sobrien/* And now, the subset of the debugging information support code necessary
265490075Sobrien   for emitting location expressions.  */
265550397Sobrien
2656169689Skan/* Data about a single source file.  */
2657169689Skanstruct dwarf_file_data GTY(())
2658169689Skan{
2659169689Skan  const char * filename;
2660169689Skan  int emitted_number;
2661169689Skan};
2662169689Skan
2663117395Skan/* We need some way to distinguish DW_OP_addr with a direct symbol
2664117395Skan   relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2665117395Skan#define INTERNAL_DW_OP_tls_addr		(0x100 + DW_OP_addr)
2666117395Skan
2667117395Skan
266890075Sobrientypedef struct dw_val_struct *dw_val_ref;
266990075Sobrientypedef struct die_struct *dw_die_ref;
267090075Sobrientypedef struct dw_loc_descr_struct *dw_loc_descr_ref;
267190075Sobrientypedef struct dw_loc_list_struct *dw_loc_list_ref;
267250397Sobrien
267350397Sobrien/* Each DIE may have a series of attribute/value pairs.  Values
267450397Sobrien   can take on several forms.  The forms that are used in this
267550397Sobrien   implementation are listed below.  */
267650397Sobrien
2677132718Skanenum dw_val_class
267850397Sobrien{
267950397Sobrien  dw_val_class_addr,
268090075Sobrien  dw_val_class_offset,
268150397Sobrien  dw_val_class_loc,
268290075Sobrien  dw_val_class_loc_list,
268390075Sobrien  dw_val_class_range_list,
268450397Sobrien  dw_val_class_const,
268550397Sobrien  dw_val_class_unsigned_const,
268650397Sobrien  dw_val_class_long_long,
2687132718Skan  dw_val_class_vec,
268850397Sobrien  dw_val_class_flag,
268950397Sobrien  dw_val_class_die_ref,
269050397Sobrien  dw_val_class_fde_ref,
269150397Sobrien  dw_val_class_lbl_id,
2692169689Skan  dw_val_class_lineptr,
2693169689Skan  dw_val_class_str,
2694169689Skan  dw_val_class_macptr,
2695169689Skan  dw_val_class_file
2696132718Skan};
269750397Sobrien
269850397Sobrien/* Describe a double word constant value.  */
269990075Sobrien/* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
270050397Sobrien
2701132718Skantypedef struct dw_long_long_struct GTY(())
270250397Sobrien{
270350397Sobrien  unsigned long hi;
270450397Sobrien  unsigned long low;
270550397Sobrien}
270650397Sobriendw_long_long_const;
270750397Sobrien
2708132718Skan/* Describe a floating point constant value, or a vector constant value.  */
270950397Sobrien
2710132718Skantypedef struct dw_vec_struct GTY(())
271150397Sobrien{
2712132718Skan  unsigned char * GTY((length ("%h.length"))) array;
271350397Sobrien  unsigned length;
2714132718Skan  unsigned elt_size;
271550397Sobrien}
2716132718Skandw_vec_const;
271750397Sobrien
271850397Sobrien/* The dw_val_node describes an attribute's value, as it is
271950397Sobrien   represented internally.  */
272050397Sobrien
2721132718Skantypedef struct dw_val_struct GTY(())
272250397Sobrien{
2723132718Skan  enum dw_val_class val_class;
2724132718Skan  union dw_val_struct_union
272550397Sobrien    {
2726132718Skan      rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2727132718Skan      unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2728132718Skan      dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2729132718Skan      dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2730169689Skan      HOST_WIDE_INT GTY ((default)) val_int;
2731132718Skan      unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2732132718Skan      dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2733132718Skan      dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2734132718Skan      struct dw_val_die_union
273590075Sobrien	{
273690075Sobrien	  dw_die_ref die;
273790075Sobrien	  int external;
2738132718Skan	} GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2739132718Skan      unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2740132718Skan      struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2741132718Skan      char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2742132718Skan      unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2743169689Skan      struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
274450397Sobrien    }
2745132718Skan  GTY ((desc ("%1.val_class"))) v;
274650397Sobrien}
274750397Sobriendw_val_node;
274850397Sobrien
274950397Sobrien/* Locations in memory are described using a sequence of stack machine
275050397Sobrien   operations.  */
275150397Sobrien
2752132718Skantypedef struct dw_loc_descr_struct GTY(())
275350397Sobrien{
275450397Sobrien  dw_loc_descr_ref dw_loc_next;
275550397Sobrien  enum dwarf_location_atom dw_loc_opc;
275650397Sobrien  dw_val_node dw_loc_oprnd1;
275750397Sobrien  dw_val_node dw_loc_oprnd2;
275890075Sobrien  int dw_loc_addr;
275950397Sobrien}
276050397Sobriendw_loc_descr_node;
276150397Sobrien
276290075Sobrien/* Location lists are ranges + location descriptions for that range,
276390075Sobrien   so you can track variables that are in different places over
276490075Sobrien   their entire life.  */
2765132718Skantypedef struct dw_loc_list_struct GTY(())
276690075Sobrien{
276790075Sobrien  dw_loc_list_ref dw_loc_next;
276890075Sobrien  const char *begin; /* Label for begin address of range */
276990075Sobrien  const char *end;  /* Label for end address of range */
277090075Sobrien  char *ll_symbol; /* Label for beginning of location list.
277190075Sobrien		      Only on head of list */
277290075Sobrien  const char *section; /* Section this loclist is relative to */
277390075Sobrien  dw_loc_descr_ref expr;
277490075Sobrien} dw_loc_list_node;
277590075Sobrien
2776132718Skan#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
277790075Sobrien
2778132718Skanstatic const char *dwarf_stack_op_name (unsigned);
2779132718Skanstatic dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2780132718Skan				       unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2781132718Skanstatic void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2782132718Skanstatic unsigned long size_of_loc_descr (dw_loc_descr_ref);
2783132718Skanstatic unsigned long size_of_locs (dw_loc_descr_ref);
2784132718Skanstatic void output_loc_operands (dw_loc_descr_ref);
2785132718Skanstatic void output_loc_sequence (dw_loc_descr_ref);
2786132718Skan
278790075Sobrien/* Convert a DWARF stack opcode into its string name.  */
278890075Sobrien
278990075Sobrienstatic const char *
2790132718Skandwarf_stack_op_name (unsigned int op)
279190075Sobrien{
279290075Sobrien  switch (op)
279390075Sobrien    {
279490075Sobrien    case DW_OP_addr:
2795117395Skan    case INTERNAL_DW_OP_tls_addr:
279690075Sobrien      return "DW_OP_addr";
279790075Sobrien    case DW_OP_deref:
279890075Sobrien      return "DW_OP_deref";
279990075Sobrien    case DW_OP_const1u:
280090075Sobrien      return "DW_OP_const1u";
280190075Sobrien    case DW_OP_const1s:
280290075Sobrien      return "DW_OP_const1s";
280390075Sobrien    case DW_OP_const2u:
280490075Sobrien      return "DW_OP_const2u";
280590075Sobrien    case DW_OP_const2s:
280690075Sobrien      return "DW_OP_const2s";
280790075Sobrien    case DW_OP_const4u:
280890075Sobrien      return "DW_OP_const4u";
280990075Sobrien    case DW_OP_const4s:
281090075Sobrien      return "DW_OP_const4s";
281190075Sobrien    case DW_OP_const8u:
281290075Sobrien      return "DW_OP_const8u";
281390075Sobrien    case DW_OP_const8s:
281490075Sobrien      return "DW_OP_const8s";
281590075Sobrien    case DW_OP_constu:
281690075Sobrien      return "DW_OP_constu";
281790075Sobrien    case DW_OP_consts:
281890075Sobrien      return "DW_OP_consts";
281990075Sobrien    case DW_OP_dup:
282090075Sobrien      return "DW_OP_dup";
282190075Sobrien    case DW_OP_drop:
282290075Sobrien      return "DW_OP_drop";
282390075Sobrien    case DW_OP_over:
282490075Sobrien      return "DW_OP_over";
282590075Sobrien    case DW_OP_pick:
282690075Sobrien      return "DW_OP_pick";
282790075Sobrien    case DW_OP_swap:
282890075Sobrien      return "DW_OP_swap";
282990075Sobrien    case DW_OP_rot:
283090075Sobrien      return "DW_OP_rot";
283190075Sobrien    case DW_OP_xderef:
283290075Sobrien      return "DW_OP_xderef";
283390075Sobrien    case DW_OP_abs:
283490075Sobrien      return "DW_OP_abs";
283590075Sobrien    case DW_OP_and:
283690075Sobrien      return "DW_OP_and";
283790075Sobrien    case DW_OP_div:
283890075Sobrien      return "DW_OP_div";
283990075Sobrien    case DW_OP_minus:
284090075Sobrien      return "DW_OP_minus";
284190075Sobrien    case DW_OP_mod:
284290075Sobrien      return "DW_OP_mod";
284390075Sobrien    case DW_OP_mul:
284490075Sobrien      return "DW_OP_mul";
284590075Sobrien    case DW_OP_neg:
284690075Sobrien      return "DW_OP_neg";
284790075Sobrien    case DW_OP_not:
284890075Sobrien      return "DW_OP_not";
284990075Sobrien    case DW_OP_or:
285090075Sobrien      return "DW_OP_or";
285190075Sobrien    case DW_OP_plus:
285290075Sobrien      return "DW_OP_plus";
285390075Sobrien    case DW_OP_plus_uconst:
285490075Sobrien      return "DW_OP_plus_uconst";
285590075Sobrien    case DW_OP_shl:
285690075Sobrien      return "DW_OP_shl";
285790075Sobrien    case DW_OP_shr:
285890075Sobrien      return "DW_OP_shr";
285990075Sobrien    case DW_OP_shra:
286090075Sobrien      return "DW_OP_shra";
286190075Sobrien    case DW_OP_xor:
286290075Sobrien      return "DW_OP_xor";
286390075Sobrien    case DW_OP_bra:
286490075Sobrien      return "DW_OP_bra";
286590075Sobrien    case DW_OP_eq:
286690075Sobrien      return "DW_OP_eq";
286790075Sobrien    case DW_OP_ge:
286890075Sobrien      return "DW_OP_ge";
286990075Sobrien    case DW_OP_gt:
287090075Sobrien      return "DW_OP_gt";
287190075Sobrien    case DW_OP_le:
287290075Sobrien      return "DW_OP_le";
287390075Sobrien    case DW_OP_lt:
287490075Sobrien      return "DW_OP_lt";
287590075Sobrien    case DW_OP_ne:
287690075Sobrien      return "DW_OP_ne";
287790075Sobrien    case DW_OP_skip:
287890075Sobrien      return "DW_OP_skip";
287990075Sobrien    case DW_OP_lit0:
288090075Sobrien      return "DW_OP_lit0";
288190075Sobrien    case DW_OP_lit1:
288290075Sobrien      return "DW_OP_lit1";
288390075Sobrien    case DW_OP_lit2:
288490075Sobrien      return "DW_OP_lit2";
288590075Sobrien    case DW_OP_lit3:
288690075Sobrien      return "DW_OP_lit3";
288790075Sobrien    case DW_OP_lit4:
288890075Sobrien      return "DW_OP_lit4";
288990075Sobrien    case DW_OP_lit5:
289090075Sobrien      return "DW_OP_lit5";
289190075Sobrien    case DW_OP_lit6:
289290075Sobrien      return "DW_OP_lit6";
289390075Sobrien    case DW_OP_lit7:
289490075Sobrien      return "DW_OP_lit7";
289590075Sobrien    case DW_OP_lit8:
289690075Sobrien      return "DW_OP_lit8";
289790075Sobrien    case DW_OP_lit9:
289890075Sobrien      return "DW_OP_lit9";
289990075Sobrien    case DW_OP_lit10:
290090075Sobrien      return "DW_OP_lit10";
290190075Sobrien    case DW_OP_lit11:
290290075Sobrien      return "DW_OP_lit11";
290390075Sobrien    case DW_OP_lit12:
290490075Sobrien      return "DW_OP_lit12";
290590075Sobrien    case DW_OP_lit13:
290690075Sobrien      return "DW_OP_lit13";
290790075Sobrien    case DW_OP_lit14:
290890075Sobrien      return "DW_OP_lit14";
290990075Sobrien    case DW_OP_lit15:
291090075Sobrien      return "DW_OP_lit15";
291190075Sobrien    case DW_OP_lit16:
291290075Sobrien      return "DW_OP_lit16";
291390075Sobrien    case DW_OP_lit17:
291490075Sobrien      return "DW_OP_lit17";
291590075Sobrien    case DW_OP_lit18:
291690075Sobrien      return "DW_OP_lit18";
291790075Sobrien    case DW_OP_lit19:
291890075Sobrien      return "DW_OP_lit19";
291990075Sobrien    case DW_OP_lit20:
292090075Sobrien      return "DW_OP_lit20";
292190075Sobrien    case DW_OP_lit21:
292290075Sobrien      return "DW_OP_lit21";
292390075Sobrien    case DW_OP_lit22:
292490075Sobrien      return "DW_OP_lit22";
292590075Sobrien    case DW_OP_lit23:
292690075Sobrien      return "DW_OP_lit23";
292790075Sobrien    case DW_OP_lit24:
292890075Sobrien      return "DW_OP_lit24";
292990075Sobrien    case DW_OP_lit25:
293090075Sobrien      return "DW_OP_lit25";
293190075Sobrien    case DW_OP_lit26:
293290075Sobrien      return "DW_OP_lit26";
293390075Sobrien    case DW_OP_lit27:
293490075Sobrien      return "DW_OP_lit27";
293590075Sobrien    case DW_OP_lit28:
293690075Sobrien      return "DW_OP_lit28";
293790075Sobrien    case DW_OP_lit29:
293890075Sobrien      return "DW_OP_lit29";
293990075Sobrien    case DW_OP_lit30:
294090075Sobrien      return "DW_OP_lit30";
294190075Sobrien    case DW_OP_lit31:
294290075Sobrien      return "DW_OP_lit31";
294390075Sobrien    case DW_OP_reg0:
294490075Sobrien      return "DW_OP_reg0";
294590075Sobrien    case DW_OP_reg1:
294690075Sobrien      return "DW_OP_reg1";
294790075Sobrien    case DW_OP_reg2:
294890075Sobrien      return "DW_OP_reg2";
294990075Sobrien    case DW_OP_reg3:
295090075Sobrien      return "DW_OP_reg3";
295190075Sobrien    case DW_OP_reg4:
295290075Sobrien      return "DW_OP_reg4";
295390075Sobrien    case DW_OP_reg5:
295490075Sobrien      return "DW_OP_reg5";
295590075Sobrien    case DW_OP_reg6:
295690075Sobrien      return "DW_OP_reg6";
295790075Sobrien    case DW_OP_reg7:
295890075Sobrien      return "DW_OP_reg7";
295990075Sobrien    case DW_OP_reg8:
296090075Sobrien      return "DW_OP_reg8";
296190075Sobrien    case DW_OP_reg9:
296290075Sobrien      return "DW_OP_reg9";
296390075Sobrien    case DW_OP_reg10:
296490075Sobrien      return "DW_OP_reg10";
296590075Sobrien    case DW_OP_reg11:
296690075Sobrien      return "DW_OP_reg11";
296790075Sobrien    case DW_OP_reg12:
296890075Sobrien      return "DW_OP_reg12";
296990075Sobrien    case DW_OP_reg13:
297090075Sobrien      return "DW_OP_reg13";
297190075Sobrien    case DW_OP_reg14:
297290075Sobrien      return "DW_OP_reg14";
297390075Sobrien    case DW_OP_reg15:
297490075Sobrien      return "DW_OP_reg15";
297590075Sobrien    case DW_OP_reg16:
297690075Sobrien      return "DW_OP_reg16";
297790075Sobrien    case DW_OP_reg17:
297890075Sobrien      return "DW_OP_reg17";
297990075Sobrien    case DW_OP_reg18:
298090075Sobrien      return "DW_OP_reg18";
298190075Sobrien    case DW_OP_reg19:
298290075Sobrien      return "DW_OP_reg19";
298390075Sobrien    case DW_OP_reg20:
298490075Sobrien      return "DW_OP_reg20";
298590075Sobrien    case DW_OP_reg21:
298690075Sobrien      return "DW_OP_reg21";
298790075Sobrien    case DW_OP_reg22:
298890075Sobrien      return "DW_OP_reg22";
298990075Sobrien    case DW_OP_reg23:
299090075Sobrien      return "DW_OP_reg23";
299190075Sobrien    case DW_OP_reg24:
299290075Sobrien      return "DW_OP_reg24";
299390075Sobrien    case DW_OP_reg25:
299490075Sobrien      return "DW_OP_reg25";
299590075Sobrien    case DW_OP_reg26:
299690075Sobrien      return "DW_OP_reg26";
299790075Sobrien    case DW_OP_reg27:
299890075Sobrien      return "DW_OP_reg27";
299990075Sobrien    case DW_OP_reg28:
300090075Sobrien      return "DW_OP_reg28";
300190075Sobrien    case DW_OP_reg29:
300290075Sobrien      return "DW_OP_reg29";
300390075Sobrien    case DW_OP_reg30:
300490075Sobrien      return "DW_OP_reg30";
300590075Sobrien    case DW_OP_reg31:
300690075Sobrien      return "DW_OP_reg31";
300790075Sobrien    case DW_OP_breg0:
300890075Sobrien      return "DW_OP_breg0";
300990075Sobrien    case DW_OP_breg1:
301090075Sobrien      return "DW_OP_breg1";
301190075Sobrien    case DW_OP_breg2:
301290075Sobrien      return "DW_OP_breg2";
301390075Sobrien    case DW_OP_breg3:
301490075Sobrien      return "DW_OP_breg3";
301590075Sobrien    case DW_OP_breg4:
301690075Sobrien      return "DW_OP_breg4";
301790075Sobrien    case DW_OP_breg5:
301890075Sobrien      return "DW_OP_breg5";
301990075Sobrien    case DW_OP_breg6:
302090075Sobrien      return "DW_OP_breg6";
302190075Sobrien    case DW_OP_breg7:
302290075Sobrien      return "DW_OP_breg7";
302390075Sobrien    case DW_OP_breg8:
302490075Sobrien      return "DW_OP_breg8";
302590075Sobrien    case DW_OP_breg9:
302690075Sobrien      return "DW_OP_breg9";
302790075Sobrien    case DW_OP_breg10:
302890075Sobrien      return "DW_OP_breg10";
302990075Sobrien    case DW_OP_breg11:
303090075Sobrien      return "DW_OP_breg11";
303190075Sobrien    case DW_OP_breg12:
303290075Sobrien      return "DW_OP_breg12";
303390075Sobrien    case DW_OP_breg13:
303490075Sobrien      return "DW_OP_breg13";
303590075Sobrien    case DW_OP_breg14:
303690075Sobrien      return "DW_OP_breg14";
303790075Sobrien    case DW_OP_breg15:
303890075Sobrien      return "DW_OP_breg15";
303990075Sobrien    case DW_OP_breg16:
304090075Sobrien      return "DW_OP_breg16";
304190075Sobrien    case DW_OP_breg17:
304290075Sobrien      return "DW_OP_breg17";
304390075Sobrien    case DW_OP_breg18:
304490075Sobrien      return "DW_OP_breg18";
304590075Sobrien    case DW_OP_breg19:
304690075Sobrien      return "DW_OP_breg19";
304790075Sobrien    case DW_OP_breg20:
304890075Sobrien      return "DW_OP_breg20";
304990075Sobrien    case DW_OP_breg21:
305090075Sobrien      return "DW_OP_breg21";
305190075Sobrien    case DW_OP_breg22:
305290075Sobrien      return "DW_OP_breg22";
305390075Sobrien    case DW_OP_breg23:
305490075Sobrien      return "DW_OP_breg23";
305590075Sobrien    case DW_OP_breg24:
305690075Sobrien      return "DW_OP_breg24";
305790075Sobrien    case DW_OP_breg25:
305890075Sobrien      return "DW_OP_breg25";
305990075Sobrien    case DW_OP_breg26:
306090075Sobrien      return "DW_OP_breg26";
306190075Sobrien    case DW_OP_breg27:
306290075Sobrien      return "DW_OP_breg27";
306390075Sobrien    case DW_OP_breg28:
306490075Sobrien      return "DW_OP_breg28";
306590075Sobrien    case DW_OP_breg29:
306690075Sobrien      return "DW_OP_breg29";
306790075Sobrien    case DW_OP_breg30:
306890075Sobrien      return "DW_OP_breg30";
306990075Sobrien    case DW_OP_breg31:
307090075Sobrien      return "DW_OP_breg31";
307190075Sobrien    case DW_OP_regx:
307290075Sobrien      return "DW_OP_regx";
307390075Sobrien    case DW_OP_fbreg:
307490075Sobrien      return "DW_OP_fbreg";
307590075Sobrien    case DW_OP_bregx:
307690075Sobrien      return "DW_OP_bregx";
307790075Sobrien    case DW_OP_piece:
307890075Sobrien      return "DW_OP_piece";
307990075Sobrien    case DW_OP_deref_size:
308090075Sobrien      return "DW_OP_deref_size";
308190075Sobrien    case DW_OP_xderef_size:
308290075Sobrien      return "DW_OP_xderef_size";
308390075Sobrien    case DW_OP_nop:
308490075Sobrien      return "DW_OP_nop";
3085117395Skan    case DW_OP_push_object_address:
3086117395Skan      return "DW_OP_push_object_address";
3087117395Skan    case DW_OP_call2:
3088117395Skan      return "DW_OP_call2";
3089117395Skan    case DW_OP_call4:
3090117395Skan      return "DW_OP_call4";
3091117395Skan    case DW_OP_call_ref:
3092117395Skan      return "DW_OP_call_ref";
3093117395Skan    case DW_OP_GNU_push_tls_address:
3094117395Skan      return "DW_OP_GNU_push_tls_address";
309590075Sobrien    default:
309690075Sobrien      return "OP_<unknown>";
309790075Sobrien    }
309890075Sobrien}
309990075Sobrien
310090075Sobrien/* Return a pointer to a newly allocated location description.  Location
310190075Sobrien   descriptions are simple expression terms that can be strung
310290075Sobrien   together to form more complicated location (address) descriptions.  */
310390075Sobrien
310490075Sobrienstatic inline dw_loc_descr_ref
3105132718Skannew_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3106132718Skan	       unsigned HOST_WIDE_INT oprnd2)
310790075Sobrien{
3108132718Skan  dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
310990075Sobrien
311090075Sobrien  descr->dw_loc_opc = op;
311190075Sobrien  descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
311290075Sobrien  descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
311390075Sobrien  descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
311490075Sobrien  descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
311590075Sobrien
311690075Sobrien  return descr;
311790075Sobrien}
311890075Sobrien
311990075Sobrien/* Add a location description term to a location description expression.  */
312090075Sobrien
312190075Sobrienstatic inline void
3122132718Skanadd_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
312390075Sobrien{
312490075Sobrien  dw_loc_descr_ref *d;
312590075Sobrien
312690075Sobrien  /* Find the end of the chain.  */
312790075Sobrien  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
312890075Sobrien    ;
312990075Sobrien
313090075Sobrien  *d = descr;
313190075Sobrien}
313290075Sobrien
313390075Sobrien/* Return the size of a location descriptor.  */
313490075Sobrien
313590075Sobrienstatic unsigned long
3136132718Skansize_of_loc_descr (dw_loc_descr_ref loc)
313790075Sobrien{
313890075Sobrien  unsigned long size = 1;
313990075Sobrien
314090075Sobrien  switch (loc->dw_loc_opc)
314190075Sobrien    {
314290075Sobrien    case DW_OP_addr:
3143117395Skan    case INTERNAL_DW_OP_tls_addr:
314490075Sobrien      size += DWARF2_ADDR_SIZE;
314590075Sobrien      break;
314690075Sobrien    case DW_OP_const1u:
314790075Sobrien    case DW_OP_const1s:
314890075Sobrien      size += 1;
314990075Sobrien      break;
315090075Sobrien    case DW_OP_const2u:
315190075Sobrien    case DW_OP_const2s:
315290075Sobrien      size += 2;
315390075Sobrien      break;
315490075Sobrien    case DW_OP_const4u:
315590075Sobrien    case DW_OP_const4s:
315690075Sobrien      size += 4;
315790075Sobrien      break;
315890075Sobrien    case DW_OP_const8u:
315990075Sobrien    case DW_OP_const8s:
316090075Sobrien      size += 8;
316190075Sobrien      break;
316290075Sobrien    case DW_OP_constu:
316390075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
316490075Sobrien      break;
316590075Sobrien    case DW_OP_consts:
316690075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
316790075Sobrien      break;
316890075Sobrien    case DW_OP_pick:
316990075Sobrien      size += 1;
317090075Sobrien      break;
317190075Sobrien    case DW_OP_plus_uconst:
317290075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
317390075Sobrien      break;
317490075Sobrien    case DW_OP_skip:
317590075Sobrien    case DW_OP_bra:
317690075Sobrien      size += 2;
317790075Sobrien      break;
317890075Sobrien    case DW_OP_breg0:
317990075Sobrien    case DW_OP_breg1:
318090075Sobrien    case DW_OP_breg2:
318190075Sobrien    case DW_OP_breg3:
318290075Sobrien    case DW_OP_breg4:
318390075Sobrien    case DW_OP_breg5:
318490075Sobrien    case DW_OP_breg6:
318590075Sobrien    case DW_OP_breg7:
318690075Sobrien    case DW_OP_breg8:
318790075Sobrien    case DW_OP_breg9:
318890075Sobrien    case DW_OP_breg10:
318990075Sobrien    case DW_OP_breg11:
319090075Sobrien    case DW_OP_breg12:
319190075Sobrien    case DW_OP_breg13:
319290075Sobrien    case DW_OP_breg14:
319390075Sobrien    case DW_OP_breg15:
319490075Sobrien    case DW_OP_breg16:
319590075Sobrien    case DW_OP_breg17:
319690075Sobrien    case DW_OP_breg18:
319790075Sobrien    case DW_OP_breg19:
319890075Sobrien    case DW_OP_breg20:
319990075Sobrien    case DW_OP_breg21:
320090075Sobrien    case DW_OP_breg22:
320190075Sobrien    case DW_OP_breg23:
320290075Sobrien    case DW_OP_breg24:
320390075Sobrien    case DW_OP_breg25:
320490075Sobrien    case DW_OP_breg26:
320590075Sobrien    case DW_OP_breg27:
320690075Sobrien    case DW_OP_breg28:
320790075Sobrien    case DW_OP_breg29:
320890075Sobrien    case DW_OP_breg30:
320990075Sobrien    case DW_OP_breg31:
321090075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
321190075Sobrien      break;
321290075Sobrien    case DW_OP_regx:
321390075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
321490075Sobrien      break;
321590075Sobrien    case DW_OP_fbreg:
321690075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
321790075Sobrien      break;
321890075Sobrien    case DW_OP_bregx:
321990075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
322090075Sobrien      size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
322190075Sobrien      break;
322290075Sobrien    case DW_OP_piece:
322390075Sobrien      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
322490075Sobrien      break;
322590075Sobrien    case DW_OP_deref_size:
322690075Sobrien    case DW_OP_xderef_size:
322790075Sobrien      size += 1;
322890075Sobrien      break;
3229117395Skan    case DW_OP_call2:
3230117395Skan      size += 2;
3231117395Skan      break;
3232117395Skan    case DW_OP_call4:
3233117395Skan      size += 4;
3234117395Skan      break;
3235117395Skan    case DW_OP_call_ref:
3236117395Skan      size += DWARF2_ADDR_SIZE;
3237117395Skan      break;
323890075Sobrien    default:
323990075Sobrien      break;
324090075Sobrien    }
324190075Sobrien
324290075Sobrien  return size;
324390075Sobrien}
324490075Sobrien
324590075Sobrien/* Return the size of a series of location descriptors.  */
324690075Sobrien
324790075Sobrienstatic unsigned long
3248132718Skansize_of_locs (dw_loc_descr_ref loc)
324990075Sobrien{
3250169689Skan  dw_loc_descr_ref l;
325190075Sobrien  unsigned long size;
325290075Sobrien
3253169689Skan  /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3254169689Skan     field, to avoid writing to a PCH file.  */
3255169689Skan  for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
325690075Sobrien    {
3257169689Skan      if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3258169689Skan	break;
3259169689Skan      size += size_of_loc_descr (l);
326090075Sobrien    }
3261169689Skan  if (! l)
3262169689Skan    return size;
326390075Sobrien
3264169689Skan  for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3265169689Skan    {
3266169689Skan      l->dw_loc_addr = size;
3267169689Skan      size += size_of_loc_descr (l);
3268169689Skan    }
3269169689Skan
327090075Sobrien  return size;
327190075Sobrien}
327290075Sobrien
327390075Sobrien/* Output location description stack opcode's operands (if any).  */
327490075Sobrien
327590075Sobrienstatic void
3276132718Skanoutput_loc_operands (dw_loc_descr_ref loc)
327790075Sobrien{
327890075Sobrien  dw_val_ref val1 = &loc->dw_loc_oprnd1;
327990075Sobrien  dw_val_ref val2 = &loc->dw_loc_oprnd2;
328090075Sobrien
328190075Sobrien  switch (loc->dw_loc_opc)
328290075Sobrien    {
328390075Sobrien#ifdef DWARF2_DEBUGGING_INFO
328490075Sobrien    case DW_OP_addr:
328590075Sobrien      dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
328690075Sobrien      break;
328790075Sobrien    case DW_OP_const2u:
328890075Sobrien    case DW_OP_const2s:
328990075Sobrien      dw2_asm_output_data (2, val1->v.val_int, NULL);
329090075Sobrien      break;
329190075Sobrien    case DW_OP_const4u:
329290075Sobrien    case DW_OP_const4s:
329390075Sobrien      dw2_asm_output_data (4, val1->v.val_int, NULL);
329490075Sobrien      break;
329590075Sobrien    case DW_OP_const8u:
329690075Sobrien    case DW_OP_const8s:
3297169689Skan      gcc_assert (HOST_BITS_PER_LONG >= 64);
329890075Sobrien      dw2_asm_output_data (8, val1->v.val_int, NULL);
329990075Sobrien      break;
330090075Sobrien    case DW_OP_skip:
330190075Sobrien    case DW_OP_bra:
330290075Sobrien      {
330390075Sobrien	int offset;
330490075Sobrien
3305169689Skan	gcc_assert (val1->val_class == dw_val_class_loc);
3306169689Skan	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
330790075Sobrien
330890075Sobrien	dw2_asm_output_data (2, offset, NULL);
330990075Sobrien      }
331090075Sobrien      break;
331190075Sobrien#else
331290075Sobrien    case DW_OP_addr:
331390075Sobrien    case DW_OP_const2u:
331490075Sobrien    case DW_OP_const2s:
331590075Sobrien    case DW_OP_const4u:
331690075Sobrien    case DW_OP_const4s:
331790075Sobrien    case DW_OP_const8u:
331890075Sobrien    case DW_OP_const8s:
331990075Sobrien    case DW_OP_skip:
332090075Sobrien    case DW_OP_bra:
332190075Sobrien      /* We currently don't make any attempt to make sure these are
3322132718Skan	 aligned properly like we do for the main unwind info, so
3323132718Skan	 don't support emitting things larger than a byte if we're
3324132718Skan	 only doing unwinding.  */
3325169689Skan      gcc_unreachable ();
332690075Sobrien#endif
332790075Sobrien    case DW_OP_const1u:
332890075Sobrien    case DW_OP_const1s:
332990075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
333090075Sobrien      break;
333190075Sobrien    case DW_OP_constu:
333290075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
333390075Sobrien      break;
333490075Sobrien    case DW_OP_consts:
333590075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
333690075Sobrien      break;
333790075Sobrien    case DW_OP_pick:
333890075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
333990075Sobrien      break;
334090075Sobrien    case DW_OP_plus_uconst:
334190075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
334290075Sobrien      break;
334390075Sobrien    case DW_OP_breg0:
334490075Sobrien    case DW_OP_breg1:
334590075Sobrien    case DW_OP_breg2:
334690075Sobrien    case DW_OP_breg3:
334790075Sobrien    case DW_OP_breg4:
334890075Sobrien    case DW_OP_breg5:
334990075Sobrien    case DW_OP_breg6:
335090075Sobrien    case DW_OP_breg7:
335190075Sobrien    case DW_OP_breg8:
335290075Sobrien    case DW_OP_breg9:
335390075Sobrien    case DW_OP_breg10:
335490075Sobrien    case DW_OP_breg11:
335590075Sobrien    case DW_OP_breg12:
335690075Sobrien    case DW_OP_breg13:
335790075Sobrien    case DW_OP_breg14:
335890075Sobrien    case DW_OP_breg15:
335990075Sobrien    case DW_OP_breg16:
336090075Sobrien    case DW_OP_breg17:
336190075Sobrien    case DW_OP_breg18:
336290075Sobrien    case DW_OP_breg19:
336390075Sobrien    case DW_OP_breg20:
336490075Sobrien    case DW_OP_breg21:
336590075Sobrien    case DW_OP_breg22:
336690075Sobrien    case DW_OP_breg23:
336790075Sobrien    case DW_OP_breg24:
336890075Sobrien    case DW_OP_breg25:
336990075Sobrien    case DW_OP_breg26:
337090075Sobrien    case DW_OP_breg27:
337190075Sobrien    case DW_OP_breg28:
337290075Sobrien    case DW_OP_breg29:
337390075Sobrien    case DW_OP_breg30:
337490075Sobrien    case DW_OP_breg31:
337590075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
337690075Sobrien      break;
337790075Sobrien    case DW_OP_regx:
337890075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
337990075Sobrien      break;
338090075Sobrien    case DW_OP_fbreg:
338190075Sobrien      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
338290075Sobrien      break;
338390075Sobrien    case DW_OP_bregx:
338490075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
338590075Sobrien      dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
338690075Sobrien      break;
338790075Sobrien    case DW_OP_piece:
338890075Sobrien      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
338990075Sobrien      break;
339090075Sobrien    case DW_OP_deref_size:
339190075Sobrien    case DW_OP_xderef_size:
339290075Sobrien      dw2_asm_output_data (1, val1->v.val_int, NULL);
339390075Sobrien      break;
3394117395Skan
3395117395Skan    case INTERNAL_DW_OP_tls_addr:
3396169689Skan      if (targetm.asm_out.output_dwarf_dtprel)
3397169689Skan	{
3398169689Skan	  targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3399169689Skan					       DWARF2_ADDR_SIZE,
3400169689Skan					       val1->v.val_addr);
3401169689Skan	  fputc ('\n', asm_out_file);
3402169689Skan	}
3403169689Skan      else
3404169689Skan	gcc_unreachable ();
3405117395Skan      break;
3406117395Skan
340790075Sobrien    default:
340890075Sobrien      /* Other codes have no operands.  */
340990075Sobrien      break;
341090075Sobrien    }
341190075Sobrien}
341290075Sobrien
341390075Sobrien/* Output a sequence of location operations.  */
341490075Sobrien
341590075Sobrienstatic void
3416132718Skanoutput_loc_sequence (dw_loc_descr_ref loc)
341790075Sobrien{
341890075Sobrien  for (; loc != NULL; loc = loc->dw_loc_next)
341990075Sobrien    {
342090075Sobrien      /* Output the opcode.  */
342190075Sobrien      dw2_asm_output_data (1, loc->dw_loc_opc,
342290075Sobrien			   "%s", dwarf_stack_op_name (loc->dw_loc_opc));
342390075Sobrien
342490075Sobrien      /* Output the operand(s) (if any).  */
342590075Sobrien      output_loc_operands (loc);
342690075Sobrien    }
342790075Sobrien}
342890075Sobrien
342990075Sobrien/* This routine will generate the correct assembly data for a location
343090075Sobrien   description based on a cfi entry with a complex address.  */
343190075Sobrien
343290075Sobrienstatic void
3433132718Skanoutput_cfa_loc (dw_cfi_ref cfi)
343490075Sobrien{
343590075Sobrien  dw_loc_descr_ref loc;
343690075Sobrien  unsigned long size;
343790075Sobrien
343890075Sobrien  /* Output the size of the block.  */
343990075Sobrien  loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
344090075Sobrien  size = size_of_locs (loc);
344190075Sobrien  dw2_asm_output_data_uleb128 (size, NULL);
344290075Sobrien
344390075Sobrien  /* Now output the operations themselves.  */
344490075Sobrien  output_loc_sequence (loc);
344590075Sobrien}
344690075Sobrien
3447169689Skan/* This function builds a dwarf location descriptor sequence from a
3448169689Skan   dw_cfa_location, adding the given OFFSET to the result of the
3449169689Skan   expression.  */
345090075Sobrien
345190075Sobrienstatic struct dw_loc_descr_struct *
3452169689Skanbuild_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
345390075Sobrien{
345490075Sobrien  struct dw_loc_descr_struct *head, *tmp;
345590075Sobrien
3456169689Skan  offset += cfa->offset;
345790075Sobrien
3458169689Skan  if (cfa->indirect)
345990075Sobrien    {
3460169689Skan      if (cfa->base_offset)
3461169689Skan	{
3462169689Skan	  if (cfa->reg <= 31)
3463169689Skan	    head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3464169689Skan	  else
3465169689Skan	    head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3466169689Skan	}
3467169689Skan      else if (cfa->reg <= 31)
3468169689Skan	head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
346990075Sobrien      else
3470169689Skan	head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3471169689Skan
3472169689Skan      head->dw_loc_oprnd1.val_class = dw_val_class_const;
3473169689Skan      tmp = new_loc_descr (DW_OP_deref, 0, 0);
3474169689Skan      add_loc_descr (&head, tmp);
3475169689Skan      if (offset != 0)
3476169689Skan	{
3477169689Skan	  tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3478169689Skan	  add_loc_descr (&head, tmp);
3479169689Skan	}
348090075Sobrien    }
348190075Sobrien  else
348290075Sobrien    {
3483169689Skan      if (offset == 0)
3484169689Skan	if (cfa->reg <= 31)
3485169689Skan	  head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3486169689Skan	else
3487169689Skan	  head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3488169689Skan      else if (cfa->reg <= 31)
3489169689Skan	head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3490169689Skan      else
3491169689Skan	head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
349290075Sobrien    }
349390075Sobrien
349490075Sobrien  return head;
349590075Sobrien}
349690075Sobrien
349790075Sobrien/* This function fills in aa dw_cfa_location structure from a dwarf location
349890075Sobrien   descriptor sequence.  */
349990075Sobrien
350090075Sobrienstatic void
3501132718Skanget_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
350290075Sobrien{
350390075Sobrien  struct dw_loc_descr_struct *ptr;
350490075Sobrien  cfa->offset = 0;
350590075Sobrien  cfa->base_offset = 0;
350690075Sobrien  cfa->indirect = 0;
350790075Sobrien  cfa->reg = -1;
350890075Sobrien
350990075Sobrien  for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
351090075Sobrien    {
351190075Sobrien      enum dwarf_location_atom op = ptr->dw_loc_opc;
351290075Sobrien
351390075Sobrien      switch (op)
351490075Sobrien	{
351590075Sobrien	case DW_OP_reg0:
351690075Sobrien	case DW_OP_reg1:
351790075Sobrien	case DW_OP_reg2:
351890075Sobrien	case DW_OP_reg3:
351990075Sobrien	case DW_OP_reg4:
352090075Sobrien	case DW_OP_reg5:
352190075Sobrien	case DW_OP_reg6:
352290075Sobrien	case DW_OP_reg7:
352390075Sobrien	case DW_OP_reg8:
352490075Sobrien	case DW_OP_reg9:
352590075Sobrien	case DW_OP_reg10:
352690075Sobrien	case DW_OP_reg11:
352790075Sobrien	case DW_OP_reg12:
352890075Sobrien	case DW_OP_reg13:
352990075Sobrien	case DW_OP_reg14:
353090075Sobrien	case DW_OP_reg15:
353190075Sobrien	case DW_OP_reg16:
353290075Sobrien	case DW_OP_reg17:
353390075Sobrien	case DW_OP_reg18:
353490075Sobrien	case DW_OP_reg19:
353590075Sobrien	case DW_OP_reg20:
353690075Sobrien	case DW_OP_reg21:
353790075Sobrien	case DW_OP_reg22:
353890075Sobrien	case DW_OP_reg23:
353990075Sobrien	case DW_OP_reg24:
354090075Sobrien	case DW_OP_reg25:
354190075Sobrien	case DW_OP_reg26:
354290075Sobrien	case DW_OP_reg27:
354390075Sobrien	case DW_OP_reg28:
354490075Sobrien	case DW_OP_reg29:
354590075Sobrien	case DW_OP_reg30:
354690075Sobrien	case DW_OP_reg31:
354790075Sobrien	  cfa->reg = op - DW_OP_reg0;
354890075Sobrien	  break;
354990075Sobrien	case DW_OP_regx:
355090075Sobrien	  cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
355190075Sobrien	  break;
355290075Sobrien	case DW_OP_breg0:
355390075Sobrien	case DW_OP_breg1:
355490075Sobrien	case DW_OP_breg2:
355590075Sobrien	case DW_OP_breg3:
355690075Sobrien	case DW_OP_breg4:
355790075Sobrien	case DW_OP_breg5:
355890075Sobrien	case DW_OP_breg6:
355990075Sobrien	case DW_OP_breg7:
356090075Sobrien	case DW_OP_breg8:
356190075Sobrien	case DW_OP_breg9:
356290075Sobrien	case DW_OP_breg10:
356390075Sobrien	case DW_OP_breg11:
356490075Sobrien	case DW_OP_breg12:
356590075Sobrien	case DW_OP_breg13:
356690075Sobrien	case DW_OP_breg14:
356790075Sobrien	case DW_OP_breg15:
356890075Sobrien	case DW_OP_breg16:
356990075Sobrien	case DW_OP_breg17:
357090075Sobrien	case DW_OP_breg18:
357190075Sobrien	case DW_OP_breg19:
357290075Sobrien	case DW_OP_breg20:
357390075Sobrien	case DW_OP_breg21:
357490075Sobrien	case DW_OP_breg22:
357590075Sobrien	case DW_OP_breg23:
357690075Sobrien	case DW_OP_breg24:
357790075Sobrien	case DW_OP_breg25:
357890075Sobrien	case DW_OP_breg26:
357990075Sobrien	case DW_OP_breg27:
358090075Sobrien	case DW_OP_breg28:
358190075Sobrien	case DW_OP_breg29:
358290075Sobrien	case DW_OP_breg30:
358390075Sobrien	case DW_OP_breg31:
358490075Sobrien	  cfa->reg = op - DW_OP_breg0;
358590075Sobrien	  cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
358690075Sobrien	  break;
358790075Sobrien	case DW_OP_bregx:
358890075Sobrien	  cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
358990075Sobrien	  cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
359090075Sobrien	  break;
359190075Sobrien	case DW_OP_deref:
359290075Sobrien	  cfa->indirect = 1;
359390075Sobrien	  break;
359490075Sobrien	case DW_OP_plus_uconst:
359590075Sobrien	  cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
359690075Sobrien	  break;
359790075Sobrien	default:
3598169689Skan	  internal_error ("DW_LOC_OP %s not implemented",
359990075Sobrien			  dwarf_stack_op_name (ptr->dw_loc_opc));
360090075Sobrien	}
360190075Sobrien    }
360290075Sobrien}
360390075Sobrien#endif /* .debug_frame support */
360490075Sobrien
360590075Sobrien/* And now, the support for symbolic debugging information.  */
360690075Sobrien#ifdef DWARF2_DEBUGGING_INFO
360790075Sobrien
360890075Sobrien/* .debug_str support.  */
3609132718Skanstatic int output_indirect_string (void **, void *);
361090075Sobrien
3611132718Skanstatic void dwarf2out_init (const char *);
3612132718Skanstatic void dwarf2out_finish (const char *);
3613132718Skanstatic void dwarf2out_define (unsigned int, const char *);
3614132718Skanstatic void dwarf2out_undef (unsigned int, const char *);
3615132718Skanstatic void dwarf2out_start_source_file (unsigned, const char *);
3616132718Skanstatic void dwarf2out_end_source_file (unsigned);
3617132718Skanstatic void dwarf2out_begin_block (unsigned, unsigned);
3618132718Skanstatic void dwarf2out_end_block (unsigned, unsigned);
3619132718Skanstatic bool dwarf2out_ignore_block (tree);
3620132718Skanstatic void dwarf2out_global_decl (tree);
3621169689Skanstatic void dwarf2out_type_decl (tree, int);
3622169689Skanstatic void dwarf2out_imported_module_or_decl (tree, tree);
3623132718Skanstatic void dwarf2out_abstract_function (tree);
3624169689Skanstatic void dwarf2out_var_location (rtx);
3625169689Skanstatic void dwarf2out_begin_function (tree);
3626169689Skanstatic void dwarf2out_switch_text_section (void);
362790075Sobrien
362890075Sobrien/* The debug hooks structure.  */
362990075Sobrien
3630117395Skanconst struct gcc_debug_hooks dwarf2_debug_hooks =
363190075Sobrien{
363290075Sobrien  dwarf2out_init,
363390075Sobrien  dwarf2out_finish,
363490075Sobrien  dwarf2out_define,
363590075Sobrien  dwarf2out_undef,
363690075Sobrien  dwarf2out_start_source_file,
363790075Sobrien  dwarf2out_end_source_file,
363890075Sobrien  dwarf2out_begin_block,
363990075Sobrien  dwarf2out_end_block,
364090075Sobrien  dwarf2out_ignore_block,
364190075Sobrien  dwarf2out_source_line,
364290075Sobrien  dwarf2out_begin_prologue,
3643117395Skan  debug_nothing_int_charstar,	/* end_prologue */
364490075Sobrien  dwarf2out_end_epilogue,
3645169689Skan  dwarf2out_begin_function,
364690075Sobrien  debug_nothing_int,		/* end_function */
364790075Sobrien  dwarf2out_decl,		/* function_decl */
364890075Sobrien  dwarf2out_global_decl,
3649169689Skan  dwarf2out_type_decl,		/* type_decl */
3650169689Skan  dwarf2out_imported_module_or_decl,
365190075Sobrien  debug_nothing_tree,		/* deferred_inline_function */
365290075Sobrien  /* The DWARF 2 backend tries to reduce debugging bloat by not
365390075Sobrien     emitting the abstract description of inline functions until
365490075Sobrien     something tries to reference them.  */
365590075Sobrien  dwarf2out_abstract_function,	/* outlining_inline_function */
3656132718Skan  debug_nothing_rtx,		/* label */
3657169689Skan  debug_nothing_int,		/* handle_pch */
3658169689Skan  dwarf2out_var_location,
3659169689Skan  dwarf2out_switch_text_section,
3660169689Skan  1                             /* start_end_main_source_file */
366190075Sobrien};
3662132718Skan#endif
366390075Sobrien
366490075Sobrien/* NOTE: In the comments in this file, many references are made to
366590075Sobrien   "Debugging Information Entries".  This term is abbreviated as `DIE'
366690075Sobrien   throughout the remainder of this file.  */
366790075Sobrien
366890075Sobrien/* An internal representation of the DWARF output is built, and then
366990075Sobrien   walked to generate the DWARF debugging info.  The walk of the internal
367090075Sobrien   representation is done after the entire program has been compiled.
367190075Sobrien   The types below are used to describe the internal representation.  */
367290075Sobrien
367390075Sobrien/* Various DIE's use offsets relative to the beginning of the
367490075Sobrien   .debug_info section to refer to each other.  */
367590075Sobrien
367690075Sobrientypedef long int dw_offset;
367790075Sobrien
367890075Sobrien/* Define typedefs here to avoid circular dependencies.  */
367990075Sobrien
368090075Sobrientypedef struct dw_attr_struct *dw_attr_ref;
368190075Sobrientypedef struct dw_line_info_struct *dw_line_info_ref;
368290075Sobrientypedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
368390075Sobrientypedef struct pubname_struct *pubname_ref;
368490075Sobrientypedef struct dw_ranges_struct *dw_ranges_ref;
368590075Sobrien
368690075Sobrien/* Each entry in the line_info_table maintains the file and
368790075Sobrien   line number associated with the label generated for that
368890075Sobrien   entry.  The label gives the PC value associated with
368990075Sobrien   the line number entry.  */
369090075Sobrien
3691132718Skantypedef struct dw_line_info_struct GTY(())
369290075Sobrien{
369390075Sobrien  unsigned long dw_file_num;
369490075Sobrien  unsigned long dw_line_num;
369590075Sobrien}
369690075Sobriendw_line_info_entry;
369790075Sobrien
369890075Sobrien/* Line information for functions in separate sections; each one gets its
369990075Sobrien   own sequence.  */
3700132718Skantypedef struct dw_separate_line_info_struct GTY(())
370190075Sobrien{
370290075Sobrien  unsigned long dw_file_num;
370390075Sobrien  unsigned long dw_line_num;
370490075Sobrien  unsigned long function;
370590075Sobrien}
370690075Sobriendw_separate_line_info_entry;
370790075Sobrien
370850397Sobrien/* Each DIE attribute has a field specifying the attribute kind,
370950397Sobrien   a link to the next attribute in the chain, and an attribute value.
371050397Sobrien   Attributes are typically linked below the DIE they modify.  */
371150397Sobrien
3712132718Skantypedef struct dw_attr_struct GTY(())
371350397Sobrien{
371450397Sobrien  enum dwarf_attribute dw_attr;
371550397Sobrien  dw_val_node dw_attr_val;
371650397Sobrien}
371750397Sobriendw_attr_node;
371850397Sobrien
3719169689SkanDEF_VEC_O(dw_attr_node);
3720169689SkanDEF_VEC_ALLOC_O(dw_attr_node,gc);
372150397Sobrien
3722169689Skan/* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3723169689Skan   The children of each node form a circular list linked by
3724169689Skan   die_sib.  die_child points to the node *before* the "first" child node.  */
3725169689Skan
3726132718Skantypedef struct die_struct GTY(())
372750397Sobrien{
372850397Sobrien  enum dwarf_tag die_tag;
372990075Sobrien  char *die_symbol;
3730169689Skan  VEC(dw_attr_node,gc) * die_attr;
373150397Sobrien  dw_die_ref die_parent;
373250397Sobrien  dw_die_ref die_child;
373350397Sobrien  dw_die_ref die_sib;
3734132718Skan  dw_die_ref die_definition; /* ref from a specification to its definition */
373550397Sobrien  dw_offset die_offset;
373650397Sobrien  unsigned long die_abbrev;
373790075Sobrien  int die_mark;
3738169689Skan  /* Die is used and must not be pruned as unused.  */
3739169689Skan  int die_perennial_p;
3740169689Skan  unsigned int decl_id;
374150397Sobrien}
374250397Sobriendie_node;
374350397Sobrien
3744169689Skan/* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3745169689Skan#define FOR_EACH_CHILD(die, c, expr) do {	\
3746169689Skan  c = die->die_child;				\
3747169689Skan  if (c) do {					\
3748169689Skan    c = c->die_sib;				\
3749169689Skan    expr;					\
3750169689Skan  } while (c != die->die_child);		\
3751169689Skan} while (0)
3752169689Skan
375350397Sobrien/* The pubname structure */
375450397Sobrien
3755132718Skantypedef struct pubname_struct GTY(())
375650397Sobrien{
375750397Sobrien  dw_die_ref die;
375890075Sobrien  char *name;
375950397Sobrien}
376050397Sobrienpubname_entry;
376150397Sobrien
3762132718Skanstruct dw_ranges_struct GTY(())
376390075Sobrien{
376490075Sobrien  int block_num;
376590075Sobrien};
376690075Sobrien
376750397Sobrien/* The limbo die list structure.  */
3768132718Skantypedef struct limbo_die_struct GTY(())
376950397Sobrien{
377050397Sobrien  dw_die_ref die;
377190075Sobrien  tree created_for;
377250397Sobrien  struct limbo_die_struct *next;
377350397Sobrien}
377450397Sobrienlimbo_die_node;
377550397Sobrien
377650397Sobrien/* How to start an assembler comment.  */
377750397Sobrien#ifndef ASM_COMMENT_START
377850397Sobrien#define ASM_COMMENT_START ";#"
377950397Sobrien#endif
378050397Sobrien
3781117395Skan/* Define a macro which returns nonzero for a TYPE_DECL which was
378250397Sobrien   implicitly generated for a tagged type.
378350397Sobrien
378450397Sobrien   Note that unlike the gcc front end (which generates a NULL named
378550397Sobrien   TYPE_DECL node for each complete tagged type, each array type, and
378650397Sobrien   each function type node created) the g++ front end generates a
378750397Sobrien   _named_ TYPE_DECL node for each tagged type node created.
378850397Sobrien   These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
378950397Sobrien   generate a DW_TAG_typedef DIE for them.  */
379050397Sobrien
379150397Sobrien#define TYPE_DECL_IS_STUB(decl)				\
379250397Sobrien  (DECL_NAME (decl) == NULL_TREE			\
379350397Sobrien   || (DECL_ARTIFICIAL (decl)				\
379450397Sobrien       && is_tagged_type (TREE_TYPE (decl))		\
379550397Sobrien       && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))	\
379650397Sobrien	   /* This is necessary for stub decls that	\
379750397Sobrien	      appear in nested inline functions.  */	\
379850397Sobrien	   || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE	\
379950397Sobrien	       && (decl_ultimate_origin (decl)		\
380050397Sobrien		   == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
380150397Sobrien
380250397Sobrien/* Information concerning the compilation unit's programming
380350397Sobrien   language, and compiler version.  */
380450397Sobrien
380550397Sobrien/* Fixed size portion of the DWARF compilation unit header.  */
3806132718Skan#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3807132718Skan  (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
380850397Sobrien
380950397Sobrien/* Fixed size portion of public names info.  */
381050397Sobrien#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
381150397Sobrien
381250397Sobrien/* Fixed size portion of the address range info.  */
381390075Sobrien#define DWARF_ARANGES_HEADER_SIZE					\
3814132718Skan  (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,	\
3815132718Skan                DWARF2_ADDR_SIZE * 2)					\
3816132718Skan   - DWARF_INITIAL_LENGTH_SIZE)
381750397Sobrien
381890075Sobrien/* Size of padding portion in the address range info.  It must be
381990075Sobrien   aligned to twice the pointer size.  */
382090075Sobrien#define DWARF_ARANGES_PAD_SIZE \
3821132718Skan  (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3822132718Skan                DWARF2_ADDR_SIZE * 2) \
3823132718Skan   - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
382490075Sobrien
382590075Sobrien/* Use assembler line directives if available.  */
382690075Sobrien#ifndef DWARF2_ASM_LINE_DEBUG_INFO
382790075Sobrien#ifdef HAVE_AS_DWARF2_DEBUG_LINE
382890075Sobrien#define DWARF2_ASM_LINE_DEBUG_INFO 1
382990075Sobrien#else
383090075Sobrien#define DWARF2_ASM_LINE_DEBUG_INFO 0
383190075Sobrien#endif
383290075Sobrien#endif
383390075Sobrien
383450397Sobrien/* Minimum line offset in a special line info. opcode.
383550397Sobrien   This value was chosen to give a reasonable range of values.  */
383650397Sobrien#define DWARF_LINE_BASE  -10
383750397Sobrien
383890075Sobrien/* First special line opcode - leave room for the standard opcodes.  */
383950397Sobrien#define DWARF_LINE_OPCODE_BASE  10
384050397Sobrien
384150397Sobrien/* Range of line offsets in a special line info. opcode.  */
384250397Sobrien#define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
384350397Sobrien
384450397Sobrien/* Flag that indicates the initial value of the is_stmt_start flag.
384550397Sobrien   In the present implementation, we do not mark any lines as
384650397Sobrien   the beginning of a source statement, because that information
384750397Sobrien   is not made available by the GCC front-end.  */
384850397Sobrien#define	DWARF_LINE_DEFAULT_IS_STMT_START 1
384950397Sobrien
3850132718Skan#ifdef DWARF2_DEBUGGING_INFO
385150397Sobrien/* This location is used by calc_die_sizes() to keep track
385250397Sobrien   the offset of each DIE within the .debug_info section.  */
385350397Sobrienstatic unsigned long next_die_offset;
3854132718Skan#endif
385550397Sobrien
385650397Sobrien/* Record the root of the DIE's built for the current compilation unit.  */
3857132718Skanstatic GTY(()) dw_die_ref comp_unit_die;
385850397Sobrien
385950397Sobrien/* A list of DIEs with a NULL parent waiting to be relocated.  */
3860132718Skanstatic GTY(()) limbo_die_node *limbo_die_list;
386150397Sobrien
386290075Sobrien/* Filenames referenced by this compilation unit.  */
3863169689Skanstatic GTY((param_is (struct dwarf_file_data))) htab_t file_table;
386490075Sobrien
3865169689Skan/* A hash table of references to DIE's that describe declarations.
3866169689Skan   The key is a DECL_UID() which is a unique number identifying each decl.  */
3867169689Skanstatic GTY ((param_is (struct die_struct))) htab_t decl_die_table;
386850397Sobrien
3869169689Skan/* Node of the variable location list.  */
3870169689Skanstruct var_loc_node GTY ((chain_next ("%h.next")))
3871169689Skan{
3872169689Skan  rtx GTY (()) var_loc_note;
3873169689Skan  const char * GTY (()) label;
3874169689Skan  const char * GTY (()) section_label;
3875169689Skan  struct var_loc_node * GTY (()) next;
3876169689Skan};
387750397Sobrien
3878169689Skan/* Variable location list.  */
3879169689Skanstruct var_loc_list_def GTY (())
3880169689Skan{
3881169689Skan  struct var_loc_node * GTY (()) first;
388250397Sobrien
3883169689Skan  /* Do not mark the last element of the chained list because
3884169689Skan     it is marked through the chain.  */
3885169689Skan  struct var_loc_node * GTY ((skip ("%h"))) last;
388650397Sobrien
3887169689Skan  /* DECL_UID of the variable decl.  */
3888169689Skan  unsigned int decl_id;
3889169689Skan};
3890169689Skantypedef struct var_loc_list_def var_loc_list;
3891169689Skan
3892169689Skan
3893169689Skan/* Table of decl location linked lists.  */
3894169689Skanstatic GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3895169689Skan
389650397Sobrien/* A pointer to the base of a list of references to DIE's that
389750397Sobrien   are uniquely identified by their tag, presence/absence of
389850397Sobrien   children DIE's, and list of attribute/value pairs.  */
3899132718Skanstatic GTY((length ("abbrev_die_table_allocated")))
3900132718Skan  dw_die_ref *abbrev_die_table;
390150397Sobrien
390250397Sobrien/* Number of elements currently allocated for abbrev_die_table.  */
3903132718Skanstatic GTY(()) unsigned abbrev_die_table_allocated;
390450397Sobrien
390550397Sobrien/* Number of elements in type_die_table currently in use.  */
3906132718Skanstatic GTY(()) unsigned abbrev_die_table_in_use;
390750397Sobrien
390850397Sobrien/* Size (in elements) of increments by which we may expand the
390950397Sobrien   abbrev_die_table.  */
391050397Sobrien#define ABBREV_DIE_TABLE_INCREMENT 256
391150397Sobrien
391250397Sobrien/* A pointer to the base of a table that contains line information
391350397Sobrien   for each source code line in .text in the compilation unit.  */
3914132718Skanstatic GTY((length ("line_info_table_allocated")))
3915132718Skan     dw_line_info_ref line_info_table;
391650397Sobrien
391750397Sobrien/* Number of elements currently allocated for line_info_table.  */
3918132718Skanstatic GTY(()) unsigned line_info_table_allocated;
391950397Sobrien
3920132718Skan/* Number of elements in line_info_table currently in use.  */
3921132718Skanstatic GTY(()) unsigned line_info_table_in_use;
392250397Sobrien
3923169689Skan/* True if the compilation unit places functions in more than one section.  */
3924169689Skanstatic GTY(()) bool have_multiple_function_sections = false;
3925169689Skan
392650397Sobrien/* A pointer to the base of a table that contains line information
392750397Sobrien   for each source code line outside of .text in the compilation unit.  */
3928132718Skanstatic GTY ((length ("separate_line_info_table_allocated")))
3929132718Skan     dw_separate_line_info_ref separate_line_info_table;
393050397Sobrien
393150397Sobrien/* Number of elements currently allocated for separate_line_info_table.  */
3932132718Skanstatic GTY(()) unsigned separate_line_info_table_allocated;
393350397Sobrien
3934132718Skan/* Number of elements in separate_line_info_table currently in use.  */
3935132718Skanstatic GTY(()) unsigned separate_line_info_table_in_use;
393650397Sobrien
393750397Sobrien/* Size (in elements) of increments by which we may expand the
393850397Sobrien   line_info_table.  */
393950397Sobrien#define LINE_INFO_TABLE_INCREMENT 1024
394050397Sobrien
394150397Sobrien/* A pointer to the base of a table that contains a list of publicly
394250397Sobrien   accessible names.  */
3943132718Skanstatic GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
394450397Sobrien
394550397Sobrien/* Number of elements currently allocated for pubname_table.  */
3946132718Skanstatic GTY(()) unsigned pubname_table_allocated;
394750397Sobrien
394850397Sobrien/* Number of elements in pubname_table currently in use.  */
3949132718Skanstatic GTY(()) unsigned pubname_table_in_use;
395050397Sobrien
395150397Sobrien/* Size (in elements) of increments by which we may expand the
395250397Sobrien   pubname_table.  */
395350397Sobrien#define PUBNAME_TABLE_INCREMENT 64
395450397Sobrien
395590075Sobrien/* Array of dies for which we should generate .debug_arange info.  */
3956132718Skanstatic GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
395750397Sobrien
395850397Sobrien/* Number of elements currently allocated for arange_table.  */
3959132718Skanstatic GTY(()) unsigned arange_table_allocated;
396050397Sobrien
396150397Sobrien/* Number of elements in arange_table currently in use.  */
3962132718Skanstatic GTY(()) unsigned arange_table_in_use;
396350397Sobrien
396450397Sobrien/* Size (in elements) of increments by which we may expand the
396550397Sobrien   arange_table.  */
396650397Sobrien#define ARANGE_TABLE_INCREMENT 64
396750397Sobrien
396890075Sobrien/* Array of dies for which we should generate .debug_ranges info.  */
3969132718Skanstatic GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
397050397Sobrien
397190075Sobrien/* Number of elements currently allocated for ranges_table.  */
3972132718Skanstatic GTY(()) unsigned ranges_table_allocated;
397350397Sobrien
397490075Sobrien/* Number of elements in ranges_table currently in use.  */
3975132718Skanstatic GTY(()) unsigned ranges_table_in_use;
397650397Sobrien
397790075Sobrien/* Size (in elements) of increments by which we may expand the
397890075Sobrien   ranges_table.  */
397990075Sobrien#define RANGES_TABLE_INCREMENT 64
398050397Sobrien
398190075Sobrien/* Whether we have location lists that need outputting */
3982169689Skanstatic GTY(()) bool have_location_lists;
398350397Sobrien
3984169689Skan/* Unique label counter.  */
3985169689Skanstatic GTY(()) unsigned int loclabel_num;
3986169689Skan
3987132718Skan#ifdef DWARF2_DEBUGGING_INFO
398850397Sobrien/* Record whether the function being analyzed contains inlined functions.  */
398950397Sobrienstatic int current_function_has_inlines;
3990132718Skan#endif
399150397Sobrien#if 0 && defined (MIPS_DEBUGGING_INFO)
399250397Sobrienstatic int comp_unit_has_inlines;
399350397Sobrien#endif
399450397Sobrien
3995169689Skan/* The last file entry emitted by maybe_emit_file().  */
3996169689Skanstatic GTY(()) struct dwarf_file_data * last_emitted_file;
3997132718Skan
3998132718Skan/* Number of internal labels generated by gen_internal_sym().  */
3999132718Skanstatic GTY(()) int label_num;
4000132718Skan
4001169689Skan/* Cached result of previous call to lookup_filename.  */
4002169689Skanstatic GTY(()) struct dwarf_file_data * file_table_last_lookup;
4003169689Skan
4004132718Skan#ifdef DWARF2_DEBUGGING_INFO
4005132718Skan
4006169689Skan/* Offset from the "steady-state frame pointer" to the frame base,
4007169689Skan   within the current function.  */
4008169689Skanstatic HOST_WIDE_INT frame_pointer_fb_offset;
4009169689Skan
401050397Sobrien/* Forward declarations for functions defined in this file.  */
401150397Sobrien
4012132718Skanstatic int is_pseudo_reg (rtx);
4013132718Skanstatic tree type_main_variant (tree);
4014132718Skanstatic int is_tagged_type (tree);
4015132718Skanstatic const char *dwarf_tag_name (unsigned);
4016132718Skanstatic const char *dwarf_attr_name (unsigned);
4017132718Skanstatic const char *dwarf_form_name (unsigned);
4018132718Skanstatic tree decl_ultimate_origin (tree);
4019132718Skanstatic tree block_ultimate_origin (tree);
4020132718Skanstatic tree decl_class_context (tree);
4021132718Skanstatic void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4022132718Skanstatic inline enum dw_val_class AT_class (dw_attr_ref);
4023132718Skanstatic void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4024132718Skanstatic inline unsigned AT_flag (dw_attr_ref);
4025132718Skanstatic void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4026132718Skanstatic inline HOST_WIDE_INT AT_int (dw_attr_ref);
4027132718Skanstatic void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4028132718Skanstatic inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4029132718Skanstatic void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4030132718Skan			      unsigned long);
4031132718Skanstatic inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4032132718Skan			       unsigned int, unsigned char *);
4033132718Skanstatic hashval_t debug_str_do_hash (const void *);
4034132718Skanstatic int debug_str_eq (const void *, const void *);
4035132718Skanstatic void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4036132718Skanstatic inline const char *AT_string (dw_attr_ref);
4037132718Skanstatic int AT_string_form (dw_attr_ref);
4038132718Skanstatic void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4039132718Skanstatic void add_AT_specification (dw_die_ref, dw_die_ref);
4040132718Skanstatic inline dw_die_ref AT_ref (dw_attr_ref);
4041132718Skanstatic inline int AT_ref_external (dw_attr_ref);
4042132718Skanstatic inline void set_AT_ref_external (dw_attr_ref, int);
4043132718Skanstatic void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4044132718Skanstatic void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4045132718Skanstatic inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4046132718Skanstatic void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4047132718Skan			     dw_loc_list_ref);
4048132718Skanstatic inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4049132718Skanstatic void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4050132718Skanstatic inline rtx AT_addr (dw_attr_ref);
4051132718Skanstatic void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4052169689Skanstatic void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4053169689Skanstatic void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4054132718Skanstatic void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4055132718Skan			   unsigned HOST_WIDE_INT);
4056132718Skanstatic void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4057132718Skan			       unsigned long);
4058132718Skanstatic inline const char *AT_lbl (dw_attr_ref);
4059132718Skanstatic dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4060132718Skanstatic const char *get_AT_low_pc (dw_die_ref);
4061132718Skanstatic const char *get_AT_hi_pc (dw_die_ref);
4062132718Skanstatic const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4063132718Skanstatic int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4064132718Skanstatic unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4065132718Skanstatic inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4066132718Skanstatic bool is_c_family (void);
4067132718Skanstatic bool is_cxx (void);
4068132718Skanstatic bool is_java (void);
4069132718Skanstatic bool is_fortran (void);
4070132718Skanstatic bool is_ada (void);
4071132718Skanstatic void remove_AT (dw_die_ref, enum dwarf_attribute);
4072146895Skanstatic void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4073132718Skanstatic void add_child_die (dw_die_ref, dw_die_ref);
4074132718Skanstatic dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4075132718Skanstatic dw_die_ref lookup_type_die (tree);
4076132718Skanstatic void equate_type_number_to_die (tree, dw_die_ref);
4077169689Skanstatic hashval_t decl_die_table_hash (const void *);
4078169689Skanstatic int decl_die_table_eq (const void *, const void *);
4079132718Skanstatic dw_die_ref lookup_decl_die (tree);
4080169689Skanstatic hashval_t decl_loc_table_hash (const void *);
4081169689Skanstatic int decl_loc_table_eq (const void *, const void *);
4082169689Skanstatic var_loc_list *lookup_decl_loc (tree);
4083132718Skanstatic void equate_decl_number_to_die (tree, dw_die_ref);
4084169689Skanstatic void add_var_loc_to_decl (tree, struct var_loc_node *);
4085132718Skanstatic void print_spaces (FILE *);
4086132718Skanstatic void print_die (dw_die_ref, FILE *);
4087132718Skanstatic void print_dwarf_line_table (FILE *);
4088132718Skanstatic dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4089132718Skanstatic dw_die_ref pop_compile_unit (dw_die_ref);
4090132718Skanstatic void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4091132718Skanstatic void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4092132718Skanstatic void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4093132718Skanstatic int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4094132718Skanstatic int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4095132718Skanstatic int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4096132718Skanstatic int same_die_p (dw_die_ref, dw_die_ref, int *);
4097132718Skanstatic int same_die_p_wrap (dw_die_ref, dw_die_ref);
4098132718Skanstatic void compute_section_prefix (dw_die_ref);
4099132718Skanstatic int is_type_die (dw_die_ref);
4100132718Skanstatic int is_comdat_die (dw_die_ref);
4101132718Skanstatic int is_symbol_die (dw_die_ref);
4102132718Skanstatic void assign_symbol_names (dw_die_ref);
4103132718Skanstatic void break_out_includes (dw_die_ref);
4104132718Skanstatic hashval_t htab_cu_hash (const void *);
4105132718Skanstatic int htab_cu_eq (const void *, const void *);
4106132718Skanstatic void htab_cu_del (void *);
4107132718Skanstatic int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4108132718Skanstatic void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4109132718Skanstatic void add_sibling_attributes (dw_die_ref);
4110132718Skanstatic void build_abbrev_table (dw_die_ref);
4111132718Skanstatic void output_location_lists (dw_die_ref);
4112132718Skanstatic int constant_size (long unsigned);
4113132718Skanstatic unsigned long size_of_die (dw_die_ref);
4114132718Skanstatic void calc_die_sizes (dw_die_ref);
4115132718Skanstatic void mark_dies (dw_die_ref);
4116132718Skanstatic void unmark_dies (dw_die_ref);
4117132718Skanstatic void unmark_all_dies (dw_die_ref);
4118132718Skanstatic unsigned long size_of_pubnames (void);
4119132718Skanstatic unsigned long size_of_aranges (void);
4120132718Skanstatic enum dwarf_form value_format (dw_attr_ref);
4121132718Skanstatic void output_value_format (dw_attr_ref);
4122132718Skanstatic void output_abbrev_section (void);
4123132718Skanstatic void output_die_symbol (dw_die_ref);
4124132718Skanstatic void output_die (dw_die_ref);
4125132718Skanstatic void output_compilation_unit_header (void);
4126132718Skanstatic void output_comp_unit (dw_die_ref, int);
4127132718Skanstatic const char *dwarf2_name (tree, int);
4128132718Skanstatic void add_pubname (tree, dw_die_ref);
4129132718Skanstatic void output_pubnames (void);
4130132718Skanstatic void add_arange (tree, dw_die_ref);
4131132718Skanstatic void output_aranges (void);
4132132718Skanstatic unsigned int add_ranges (tree);
4133132718Skanstatic void output_ranges (void);
4134132718Skanstatic void output_line_info (void);
4135132718Skanstatic void output_file_names (void);
4136132718Skanstatic dw_die_ref base_type_die (tree);
4137132718Skanstatic tree root_type (tree);
4138132718Skanstatic int is_base_type (tree);
4139132718Skanstatic bool is_subrange_type (tree);
4140132718Skanstatic dw_die_ref subrange_type_die (tree, dw_die_ref);
4141132718Skanstatic dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4142132718Skanstatic int type_is_enum (tree);
4143132718Skanstatic unsigned int dbx_reg_number (rtx);
4144169689Skanstatic void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4145132718Skanstatic dw_loc_descr_ref reg_loc_descriptor (rtx);
4146132718Skanstatic dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4147132718Skanstatic dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4148132718Skanstatic dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4149169689Skanstatic dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4150132718Skanstatic int is_based_loc (rtx);
4151132718Skanstatic dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4152132718Skanstatic dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4153132718Skanstatic dw_loc_descr_ref loc_descriptor (rtx);
4154169689Skanstatic dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4155169689Skanstatic dw_loc_descr_ref loc_descriptor_from_tree (tree);
4156132718Skanstatic HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4157132718Skanstatic tree field_type (tree);
4158132718Skanstatic unsigned int simple_type_align_in_bits (tree);
4159132718Skanstatic unsigned int simple_decl_align_in_bits (tree);
4160132718Skanstatic unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4161132718Skanstatic HOST_WIDE_INT field_byte_offset (tree);
4162132718Skanstatic void add_AT_location_description	(dw_die_ref, enum dwarf_attribute,
4163132718Skan					 dw_loc_descr_ref);
4164132718Skanstatic void add_data_member_location_attribute (dw_die_ref, tree);
4165132718Skanstatic void add_const_value_attribute (dw_die_ref, rtx);
4166132718Skanstatic void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4167132718Skanstatic HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4168132718Skanstatic void insert_float (rtx, unsigned char *);
4169132718Skanstatic rtx rtl_for_decl_location (tree);
4170169689Skanstatic void add_location_or_const_value_attribute (dw_die_ref, tree,
4171169689Skan						   enum dwarf_attribute);
4172132718Skanstatic void tree_add_const_value_attribute (dw_die_ref, tree);
4173132718Skanstatic void add_name_attribute (dw_die_ref, const char *);
4174132718Skanstatic void add_comp_dir_attribute (dw_die_ref);
4175132718Skanstatic void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4176132718Skanstatic void add_subscript_info (dw_die_ref, tree);
4177132718Skanstatic void add_byte_size_attribute (dw_die_ref, tree);
4178132718Skanstatic void add_bit_offset_attribute (dw_die_ref, tree);
4179132718Skanstatic void add_bit_size_attribute (dw_die_ref, tree);
4180132718Skanstatic void add_prototyped_attribute (dw_die_ref, tree);
4181132718Skanstatic void add_abstract_origin_attribute (dw_die_ref, tree);
4182132718Skanstatic void add_pure_or_virtual_attribute (dw_die_ref, tree);
4183132718Skanstatic void add_src_coords_attributes (dw_die_ref, tree);
4184132718Skanstatic void add_name_and_src_coords_attributes (dw_die_ref, tree);
4185132718Skanstatic void push_decl_scope (tree);
4186132718Skanstatic void pop_decl_scope (void);
4187132718Skanstatic dw_die_ref scope_die_for (tree, dw_die_ref);
4188132718Skanstatic inline int local_scope_p (dw_die_ref);
4189132718Skanstatic inline int class_or_namespace_scope_p (dw_die_ref);
4190132718Skanstatic void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4191169689Skanstatic void add_calling_convention_attribute (dw_die_ref, tree);
4192132718Skanstatic const char *type_tag (tree);
4193132718Skanstatic tree member_declared_type (tree);
419450397Sobrien#if 0
4195132718Skanstatic const char *decl_start_label (tree);
419650397Sobrien#endif
4197132718Skanstatic void gen_array_type_die (tree, dw_die_ref);
419850397Sobrien#if 0
4199132718Skanstatic void gen_entry_point_die (tree, dw_die_ref);
420050397Sobrien#endif
4201132718Skanstatic void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4202132718Skanstatic void gen_inlined_structure_type_die (tree, dw_die_ref);
4203132718Skanstatic void gen_inlined_union_type_die (tree, dw_die_ref);
4204132718Skanstatic dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4205132718Skanstatic dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4206132718Skanstatic void gen_unspecified_parameters_die (tree, dw_die_ref);
4207132718Skanstatic void gen_formal_types_die (tree, dw_die_ref);
4208132718Skanstatic void gen_subprogram_die (tree, dw_die_ref);
4209132718Skanstatic void gen_variable_die (tree, dw_die_ref);
4210132718Skanstatic void gen_label_die (tree, dw_die_ref);
4211132718Skanstatic void gen_lexical_block_die (tree, dw_die_ref, int);
4212132718Skanstatic void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4213132718Skanstatic void gen_field_die (tree, dw_die_ref);
4214132718Skanstatic void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4215132718Skanstatic dw_die_ref gen_compile_unit_die (const char *);
4216132718Skanstatic void gen_inheritance_die (tree, tree, dw_die_ref);
4217132718Skanstatic void gen_member_die (tree, dw_die_ref);
4218132718Skanstatic void gen_struct_or_union_type_die (tree, dw_die_ref);
4219132718Skanstatic void gen_subroutine_type_die (tree, dw_die_ref);
4220132718Skanstatic void gen_typedef_die (tree, dw_die_ref);
4221132718Skanstatic void gen_type_die (tree, dw_die_ref);
4222132718Skanstatic void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4223132718Skanstatic void gen_block_die (tree, dw_die_ref, int);
4224132718Skanstatic void decls_for_scope (tree, dw_die_ref, int);
4225132718Skanstatic int is_redundant_typedef (tree);
4226132718Skanstatic void gen_namespace_die (tree);
4227132718Skanstatic void gen_decl_die (tree, dw_die_ref);
4228169689Skanstatic dw_die_ref force_decl_die (tree);
4229169689Skanstatic dw_die_ref force_type_die (tree);
4230132718Skanstatic dw_die_ref setup_namespace_context (tree, dw_die_ref);
4231132718Skanstatic void declare_in_namespace (tree, dw_die_ref);
4232169689Skanstatic struct dwarf_file_data * lookup_filename (const char *);
4233132718Skanstatic void retry_incomplete_types (void);
4234132718Skanstatic void gen_type_die_for_member (tree, tree, dw_die_ref);
4235132718Skanstatic void splice_child_die (dw_die_ref, dw_die_ref);
4236132718Skanstatic int file_info_cmp (const void *, const void *);
4237132718Skanstatic dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4238132718Skan				     const char *, const char *, unsigned);
4239132718Skanstatic void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4240132718Skan				       const char *, const char *,
4241132718Skan				       const char *);
4242132718Skanstatic void output_loc_list (dw_loc_list_ref);
4243132718Skanstatic char *gen_internal_sym (const char *);
424450397Sobrien
4245132718Skanstatic void prune_unmark_dies (dw_die_ref);
4246132718Skanstatic void prune_unused_types_mark (dw_die_ref, int);
4247132718Skanstatic void prune_unused_types_walk (dw_die_ref);
4248132718Skanstatic void prune_unused_types_walk_attribs (dw_die_ref);
4249132718Skanstatic void prune_unused_types_prune (dw_die_ref);
4250132718Skanstatic void prune_unused_types (void);
4251169689Skanstatic int maybe_emit_file (struct dwarf_file_data *fd);
4252132718Skan
425350397Sobrien/* Section names used to hold DWARF debugging information.  */
425450397Sobrien#ifndef DEBUG_INFO_SECTION
425550397Sobrien#define DEBUG_INFO_SECTION	".debug_info"
425650397Sobrien#endif
425790075Sobrien#ifndef DEBUG_ABBREV_SECTION
425890075Sobrien#define DEBUG_ABBREV_SECTION	".debug_abbrev"
425950397Sobrien#endif
426090075Sobrien#ifndef DEBUG_ARANGES_SECTION
426190075Sobrien#define DEBUG_ARANGES_SECTION	".debug_aranges"
426250397Sobrien#endif
426390075Sobrien#ifndef DEBUG_MACINFO_SECTION
426490075Sobrien#define DEBUG_MACINFO_SECTION	".debug_macinfo"
426550397Sobrien#endif
426650397Sobrien#ifndef DEBUG_LINE_SECTION
426750397Sobrien#define DEBUG_LINE_SECTION	".debug_line"
426850397Sobrien#endif
426990075Sobrien#ifndef DEBUG_LOC_SECTION
427090075Sobrien#define DEBUG_LOC_SECTION	".debug_loc"
427150397Sobrien#endif
427290075Sobrien#ifndef DEBUG_PUBNAMES_SECTION
427390075Sobrien#define DEBUG_PUBNAMES_SECTION	".debug_pubnames"
427450397Sobrien#endif
427590075Sobrien#ifndef DEBUG_STR_SECTION
427690075Sobrien#define DEBUG_STR_SECTION	".debug_str"
427750397Sobrien#endif
427890075Sobrien#ifndef DEBUG_RANGES_SECTION
427990075Sobrien#define DEBUG_RANGES_SECTION	".debug_ranges"
428090075Sobrien#endif
428150397Sobrien
428250397Sobrien/* Standard ELF section names for compiled code and data.  */
428390075Sobrien#ifndef TEXT_SECTION_NAME
428490075Sobrien#define TEXT_SECTION_NAME	".text"
428550397Sobrien#endif
428690075Sobrien
428790075Sobrien/* Section flags for .debug_str section.  */
428890075Sobrien#define DEBUG_STR_SECTION_FLAGS \
4289132718Skan  (HAVE_GAS_SHF_MERGE && flag_merge_constants			\
4290132718Skan   ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1	\
4291132718Skan   : SECTION_DEBUG)
429250397Sobrien
429352284Sobrien/* Labels we insert at beginning sections we can reference instead of
429490075Sobrien   the section names themselves.  */
429550397Sobrien
429652284Sobrien#ifndef TEXT_SECTION_LABEL
429790075Sobrien#define TEXT_SECTION_LABEL		"Ltext"
429852284Sobrien#endif
4299169689Skan#ifndef COLD_TEXT_SECTION_LABEL
4300169689Skan#define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4301169689Skan#endif
430252284Sobrien#ifndef DEBUG_LINE_SECTION_LABEL
430390075Sobrien#define DEBUG_LINE_SECTION_LABEL	"Ldebug_line"
430452284Sobrien#endif
430552284Sobrien#ifndef DEBUG_INFO_SECTION_LABEL
430690075Sobrien#define DEBUG_INFO_SECTION_LABEL	"Ldebug_info"
430752284Sobrien#endif
430890075Sobrien#ifndef DEBUG_ABBREV_SECTION_LABEL
430990075Sobrien#define DEBUG_ABBREV_SECTION_LABEL	"Ldebug_abbrev"
431052284Sobrien#endif
431190075Sobrien#ifndef DEBUG_LOC_SECTION_LABEL
431290075Sobrien#define DEBUG_LOC_SECTION_LABEL		"Ldebug_loc"
431390075Sobrien#endif
431490075Sobrien#ifndef DEBUG_RANGES_SECTION_LABEL
431590075Sobrien#define DEBUG_RANGES_SECTION_LABEL	"Ldebug_ranges"
431690075Sobrien#endif
431790075Sobrien#ifndef DEBUG_MACINFO_SECTION_LABEL
431890075Sobrien#define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
431990075Sobrien#endif
432052284Sobrien
432150397Sobrien/* Definitions of defaults for formats and names of various special
432250397Sobrien   (artificial) labels which may be generated within this file (when the -g
4323132718Skan   options is used and DWARF2_DEBUGGING_INFO is in effect.
432450397Sobrien   If necessary, these may be overridden from within the tm.h file, but
432550397Sobrien   typically, overriding these defaults is unnecessary.  */
432650397Sobrien
432750397Sobrienstatic char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
432852284Sobrienstatic char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4329169689Skanstatic char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4330169689Skanstatic char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
433152284Sobrienstatic char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433252284Sobrienstatic char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433352284Sobrienstatic char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433490075Sobrienstatic char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433590075Sobrienstatic char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
433690075Sobrienstatic char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
433750397Sobrien
433850397Sobrien#ifndef TEXT_END_LABEL
433950397Sobrien#define TEXT_END_LABEL		"Letext"
434050397Sobrien#endif
4341169689Skan#ifndef COLD_END_LABEL
4342169689Skan#define COLD_END_LABEL          "Letext_cold"
4343169689Skan#endif
434450397Sobrien#ifndef BLOCK_BEGIN_LABEL
434550397Sobrien#define BLOCK_BEGIN_LABEL	"LBB"
434650397Sobrien#endif
434750397Sobrien#ifndef BLOCK_END_LABEL
434850397Sobrien#define BLOCK_END_LABEL		"LBE"
434950397Sobrien#endif
435050397Sobrien#ifndef LINE_CODE_LABEL
435150397Sobrien#define LINE_CODE_LABEL		"LM"
435250397Sobrien#endif
435350397Sobrien#ifndef SEPARATE_LINE_CODE_LABEL
435450397Sobrien#define SEPARATE_LINE_CODE_LABEL	"LSM"
435550397Sobrien#endif
435650397Sobrien
435790075Sobrien/* We allow a language front-end to designate a function that is to be
4358169689Skan   called to "demangle" any name before it is put into a DIE.  */
435950397Sobrien
4360132718Skanstatic const char *(*demangle_name_func) (const char *);
436150397Sobrien
436290075Sobrienvoid
4363132718Skandwarf2out_set_demangle_name_func (const char *(*func) (const char *))
436450397Sobrien{
436590075Sobrien  demangle_name_func = func;
436650397Sobrien}
436750397Sobrien
436850397Sobrien/* Test if rtl node points to a pseudo register.  */
436950397Sobrien
437050397Sobrienstatic inline int
4371132718Skanis_pseudo_reg (rtx rtl)
437250397Sobrien{
4373169689Skan  return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
437490075Sobrien	  || (GET_CODE (rtl) == SUBREG
437590075Sobrien	      && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
437650397Sobrien}
437750397Sobrien
437850397Sobrien/* Return a reference to a type, with its const and volatile qualifiers
437950397Sobrien   removed.  */
438050397Sobrien
438150397Sobrienstatic inline tree
4382132718Skantype_main_variant (tree type)
438350397Sobrien{
438450397Sobrien  type = TYPE_MAIN_VARIANT (type);
438550397Sobrien
438690075Sobrien  /* ??? There really should be only one main variant among any group of
438790075Sobrien     variants of a given type (and all of the MAIN_VARIANT values for all
438890075Sobrien     members of the group should point to that one type) but sometimes the C
438990075Sobrien     front-end messes this up for array types, so we work around that bug
439090075Sobrien     here.  */
439150397Sobrien  if (TREE_CODE (type) == ARRAY_TYPE)
439250397Sobrien    while (type != TYPE_MAIN_VARIANT (type))
439350397Sobrien      type = TYPE_MAIN_VARIANT (type);
439450397Sobrien
439550397Sobrien  return type;
439650397Sobrien}
439750397Sobrien
4398117395Skan/* Return nonzero if the given type node represents a tagged type.  */
439950397Sobrien
440050397Sobrienstatic inline int
4401132718Skanis_tagged_type (tree type)
440250397Sobrien{
440390075Sobrien  enum tree_code code = TREE_CODE (type);
440450397Sobrien
440550397Sobrien  return (code == RECORD_TYPE || code == UNION_TYPE
440650397Sobrien	  || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
440750397Sobrien}
440850397Sobrien
440950397Sobrien/* Convert a DIE tag into its string name.  */
441050397Sobrien
441190075Sobrienstatic const char *
4412132718Skandwarf_tag_name (unsigned int tag)
441350397Sobrien{
441450397Sobrien  switch (tag)
441550397Sobrien    {
441650397Sobrien    case DW_TAG_padding:
441750397Sobrien      return "DW_TAG_padding";
441850397Sobrien    case DW_TAG_array_type:
441950397Sobrien      return "DW_TAG_array_type";
442050397Sobrien    case DW_TAG_class_type:
442150397Sobrien      return "DW_TAG_class_type";
442250397Sobrien    case DW_TAG_entry_point:
442350397Sobrien      return "DW_TAG_entry_point";
442450397Sobrien    case DW_TAG_enumeration_type:
442550397Sobrien      return "DW_TAG_enumeration_type";
442650397Sobrien    case DW_TAG_formal_parameter:
442750397Sobrien      return "DW_TAG_formal_parameter";
442850397Sobrien    case DW_TAG_imported_declaration:
442950397Sobrien      return "DW_TAG_imported_declaration";
443050397Sobrien    case DW_TAG_label:
443150397Sobrien      return "DW_TAG_label";
443250397Sobrien    case DW_TAG_lexical_block:
443350397Sobrien      return "DW_TAG_lexical_block";
443450397Sobrien    case DW_TAG_member:
443550397Sobrien      return "DW_TAG_member";
443650397Sobrien    case DW_TAG_pointer_type:
443750397Sobrien      return "DW_TAG_pointer_type";
443850397Sobrien    case DW_TAG_reference_type:
443950397Sobrien      return "DW_TAG_reference_type";
444050397Sobrien    case DW_TAG_compile_unit:
444150397Sobrien      return "DW_TAG_compile_unit";
444250397Sobrien    case DW_TAG_string_type:
444350397Sobrien      return "DW_TAG_string_type";
444450397Sobrien    case DW_TAG_structure_type:
444550397Sobrien      return "DW_TAG_structure_type";
444650397Sobrien    case DW_TAG_subroutine_type:
444750397Sobrien      return "DW_TAG_subroutine_type";
444850397Sobrien    case DW_TAG_typedef:
444950397Sobrien      return "DW_TAG_typedef";
445050397Sobrien    case DW_TAG_union_type:
445150397Sobrien      return "DW_TAG_union_type";
445250397Sobrien    case DW_TAG_unspecified_parameters:
445350397Sobrien      return "DW_TAG_unspecified_parameters";
445450397Sobrien    case DW_TAG_variant:
445550397Sobrien      return "DW_TAG_variant";
445650397Sobrien    case DW_TAG_common_block:
445750397Sobrien      return "DW_TAG_common_block";
445850397Sobrien    case DW_TAG_common_inclusion:
445950397Sobrien      return "DW_TAG_common_inclusion";
446050397Sobrien    case DW_TAG_inheritance:
446150397Sobrien      return "DW_TAG_inheritance";
446250397Sobrien    case DW_TAG_inlined_subroutine:
446350397Sobrien      return "DW_TAG_inlined_subroutine";
446450397Sobrien    case DW_TAG_module:
446550397Sobrien      return "DW_TAG_module";
446650397Sobrien    case DW_TAG_ptr_to_member_type:
446750397Sobrien      return "DW_TAG_ptr_to_member_type";
446850397Sobrien    case DW_TAG_set_type:
446950397Sobrien      return "DW_TAG_set_type";
447050397Sobrien    case DW_TAG_subrange_type:
447150397Sobrien      return "DW_TAG_subrange_type";
447250397Sobrien    case DW_TAG_with_stmt:
447350397Sobrien      return "DW_TAG_with_stmt";
447450397Sobrien    case DW_TAG_access_declaration:
447550397Sobrien      return "DW_TAG_access_declaration";
447650397Sobrien    case DW_TAG_base_type:
447750397Sobrien      return "DW_TAG_base_type";
447850397Sobrien    case DW_TAG_catch_block:
447950397Sobrien      return "DW_TAG_catch_block";
448050397Sobrien    case DW_TAG_const_type:
448150397Sobrien      return "DW_TAG_const_type";
448250397Sobrien    case DW_TAG_constant:
448350397Sobrien      return "DW_TAG_constant";
448450397Sobrien    case DW_TAG_enumerator:
448550397Sobrien      return "DW_TAG_enumerator";
448650397Sobrien    case DW_TAG_file_type:
448750397Sobrien      return "DW_TAG_file_type";
448850397Sobrien    case DW_TAG_friend:
448950397Sobrien      return "DW_TAG_friend";
449050397Sobrien    case DW_TAG_namelist:
449150397Sobrien      return "DW_TAG_namelist";
449250397Sobrien    case DW_TAG_namelist_item:
449350397Sobrien      return "DW_TAG_namelist_item";
4494132718Skan    case DW_TAG_namespace:
4495132718Skan      return "DW_TAG_namespace";
449650397Sobrien    case DW_TAG_packed_type:
449750397Sobrien      return "DW_TAG_packed_type";
449850397Sobrien    case DW_TAG_subprogram:
449950397Sobrien      return "DW_TAG_subprogram";
450050397Sobrien    case DW_TAG_template_type_param:
450150397Sobrien      return "DW_TAG_template_type_param";
450250397Sobrien    case DW_TAG_template_value_param:
450350397Sobrien      return "DW_TAG_template_value_param";
450450397Sobrien    case DW_TAG_thrown_type:
450550397Sobrien      return "DW_TAG_thrown_type";
450650397Sobrien    case DW_TAG_try_block:
450750397Sobrien      return "DW_TAG_try_block";
450850397Sobrien    case DW_TAG_variant_part:
450950397Sobrien      return "DW_TAG_variant_part";
451050397Sobrien    case DW_TAG_variable:
451150397Sobrien      return "DW_TAG_variable";
451250397Sobrien    case DW_TAG_volatile_type:
451350397Sobrien      return "DW_TAG_volatile_type";
4514169689Skan    case DW_TAG_imported_module:
4515169689Skan      return "DW_TAG_imported_module";
451650397Sobrien    case DW_TAG_MIPS_loop:
451750397Sobrien      return "DW_TAG_MIPS_loop";
451850397Sobrien    case DW_TAG_format_label:
451950397Sobrien      return "DW_TAG_format_label";
452050397Sobrien    case DW_TAG_function_template:
452150397Sobrien      return "DW_TAG_function_template";
452250397Sobrien    case DW_TAG_class_template:
452350397Sobrien      return "DW_TAG_class_template";
452490075Sobrien    case DW_TAG_GNU_BINCL:
452590075Sobrien      return "DW_TAG_GNU_BINCL";
452690075Sobrien    case DW_TAG_GNU_EINCL:
452790075Sobrien      return "DW_TAG_GNU_EINCL";
452850397Sobrien    default:
452950397Sobrien      return "DW_TAG_<unknown>";
453050397Sobrien    }
453150397Sobrien}
453250397Sobrien
453350397Sobrien/* Convert a DWARF attribute code into its string name.  */
453450397Sobrien
453590075Sobrienstatic const char *
4536132718Skandwarf_attr_name (unsigned int attr)
453750397Sobrien{
453850397Sobrien  switch (attr)
453950397Sobrien    {
454050397Sobrien    case DW_AT_sibling:
454150397Sobrien      return "DW_AT_sibling";
454250397Sobrien    case DW_AT_location:
454350397Sobrien      return "DW_AT_location";
454450397Sobrien    case DW_AT_name:
454550397Sobrien      return "DW_AT_name";
454650397Sobrien    case DW_AT_ordering:
454750397Sobrien      return "DW_AT_ordering";
454850397Sobrien    case DW_AT_subscr_data:
454950397Sobrien      return "DW_AT_subscr_data";
455050397Sobrien    case DW_AT_byte_size:
455150397Sobrien      return "DW_AT_byte_size";
455250397Sobrien    case DW_AT_bit_offset:
455350397Sobrien      return "DW_AT_bit_offset";
455450397Sobrien    case DW_AT_bit_size:
455550397Sobrien      return "DW_AT_bit_size";
455650397Sobrien    case DW_AT_element_list:
455750397Sobrien      return "DW_AT_element_list";
455850397Sobrien    case DW_AT_stmt_list:
455950397Sobrien      return "DW_AT_stmt_list";
456050397Sobrien    case DW_AT_low_pc:
456150397Sobrien      return "DW_AT_low_pc";
456250397Sobrien    case DW_AT_high_pc:
456350397Sobrien      return "DW_AT_high_pc";
456450397Sobrien    case DW_AT_language:
456550397Sobrien      return "DW_AT_language";
456650397Sobrien    case DW_AT_member:
456750397Sobrien      return "DW_AT_member";
456850397Sobrien    case DW_AT_discr:
456950397Sobrien      return "DW_AT_discr";
457050397Sobrien    case DW_AT_discr_value:
457150397Sobrien      return "DW_AT_discr_value";
457250397Sobrien    case DW_AT_visibility:
457350397Sobrien      return "DW_AT_visibility";
457450397Sobrien    case DW_AT_import:
457550397Sobrien      return "DW_AT_import";
457650397Sobrien    case DW_AT_string_length:
457750397Sobrien      return "DW_AT_string_length";
457850397Sobrien    case DW_AT_common_reference:
457950397Sobrien      return "DW_AT_common_reference";
458050397Sobrien    case DW_AT_comp_dir:
458150397Sobrien      return "DW_AT_comp_dir";
458250397Sobrien    case DW_AT_const_value:
458350397Sobrien      return "DW_AT_const_value";
458450397Sobrien    case DW_AT_containing_type:
458550397Sobrien      return "DW_AT_containing_type";
458650397Sobrien    case DW_AT_default_value:
458750397Sobrien      return "DW_AT_default_value";
458850397Sobrien    case DW_AT_inline:
458950397Sobrien      return "DW_AT_inline";
459050397Sobrien    case DW_AT_is_optional:
459150397Sobrien      return "DW_AT_is_optional";
459250397Sobrien    case DW_AT_lower_bound:
459350397Sobrien      return "DW_AT_lower_bound";
459450397Sobrien    case DW_AT_producer:
459550397Sobrien      return "DW_AT_producer";
459650397Sobrien    case DW_AT_prototyped:
459750397Sobrien      return "DW_AT_prototyped";
459850397Sobrien    case DW_AT_return_addr:
459950397Sobrien      return "DW_AT_return_addr";
460050397Sobrien    case DW_AT_start_scope:
460150397Sobrien      return "DW_AT_start_scope";
460250397Sobrien    case DW_AT_stride_size:
460350397Sobrien      return "DW_AT_stride_size";
460450397Sobrien    case DW_AT_upper_bound:
460550397Sobrien      return "DW_AT_upper_bound";
460650397Sobrien    case DW_AT_abstract_origin:
460750397Sobrien      return "DW_AT_abstract_origin";
460850397Sobrien    case DW_AT_accessibility:
460950397Sobrien      return "DW_AT_accessibility";
461050397Sobrien    case DW_AT_address_class:
461150397Sobrien      return "DW_AT_address_class";
461250397Sobrien    case DW_AT_artificial:
461350397Sobrien      return "DW_AT_artificial";
461450397Sobrien    case DW_AT_base_types:
461550397Sobrien      return "DW_AT_base_types";
461650397Sobrien    case DW_AT_calling_convention:
461750397Sobrien      return "DW_AT_calling_convention";
461850397Sobrien    case DW_AT_count:
461950397Sobrien      return "DW_AT_count";
462050397Sobrien    case DW_AT_data_member_location:
462150397Sobrien      return "DW_AT_data_member_location";
462250397Sobrien    case DW_AT_decl_column:
462350397Sobrien      return "DW_AT_decl_column";
462450397Sobrien    case DW_AT_decl_file:
462550397Sobrien      return "DW_AT_decl_file";
462650397Sobrien    case DW_AT_decl_line:
462750397Sobrien      return "DW_AT_decl_line";
462850397Sobrien    case DW_AT_declaration:
462950397Sobrien      return "DW_AT_declaration";
463050397Sobrien    case DW_AT_discr_list:
463150397Sobrien      return "DW_AT_discr_list";
463250397Sobrien    case DW_AT_encoding:
463350397Sobrien      return "DW_AT_encoding";
463450397Sobrien    case DW_AT_external:
463550397Sobrien      return "DW_AT_external";
463650397Sobrien    case DW_AT_frame_base:
463750397Sobrien      return "DW_AT_frame_base";
463850397Sobrien    case DW_AT_friend:
463950397Sobrien      return "DW_AT_friend";
464050397Sobrien    case DW_AT_identifier_case:
464150397Sobrien      return "DW_AT_identifier_case";
464250397Sobrien    case DW_AT_macro_info:
464350397Sobrien      return "DW_AT_macro_info";
464450397Sobrien    case DW_AT_namelist_items:
464550397Sobrien      return "DW_AT_namelist_items";
464650397Sobrien    case DW_AT_priority:
464750397Sobrien      return "DW_AT_priority";
464850397Sobrien    case DW_AT_segment:
464950397Sobrien      return "DW_AT_segment";
465050397Sobrien    case DW_AT_specification:
465150397Sobrien      return "DW_AT_specification";
465250397Sobrien    case DW_AT_static_link:
465350397Sobrien      return "DW_AT_static_link";
465450397Sobrien    case DW_AT_type:
465550397Sobrien      return "DW_AT_type";
465650397Sobrien    case DW_AT_use_location:
465750397Sobrien      return "DW_AT_use_location";
465850397Sobrien    case DW_AT_variable_parameter:
465950397Sobrien      return "DW_AT_variable_parameter";
466050397Sobrien    case DW_AT_virtuality:
466150397Sobrien      return "DW_AT_virtuality";
466250397Sobrien    case DW_AT_vtable_elem_location:
466350397Sobrien      return "DW_AT_vtable_elem_location";
466450397Sobrien
466590075Sobrien    case DW_AT_allocated:
466690075Sobrien      return "DW_AT_allocated";
466790075Sobrien    case DW_AT_associated:
466890075Sobrien      return "DW_AT_associated";
466990075Sobrien    case DW_AT_data_location:
467090075Sobrien      return "DW_AT_data_location";
467190075Sobrien    case DW_AT_stride:
467290075Sobrien      return "DW_AT_stride";
467390075Sobrien    case DW_AT_entry_pc:
467490075Sobrien      return "DW_AT_entry_pc";
467590075Sobrien    case DW_AT_use_UTF8:
467690075Sobrien      return "DW_AT_use_UTF8";
467790075Sobrien    case DW_AT_extension:
467890075Sobrien      return "DW_AT_extension";
467990075Sobrien    case DW_AT_ranges:
468090075Sobrien      return "DW_AT_ranges";
468190075Sobrien    case DW_AT_trampoline:
468290075Sobrien      return "DW_AT_trampoline";
468390075Sobrien    case DW_AT_call_column:
468490075Sobrien      return "DW_AT_call_column";
468590075Sobrien    case DW_AT_call_file:
468690075Sobrien      return "DW_AT_call_file";
468790075Sobrien    case DW_AT_call_line:
468890075Sobrien      return "DW_AT_call_line";
468990075Sobrien
469050397Sobrien    case DW_AT_MIPS_fde:
469150397Sobrien      return "DW_AT_MIPS_fde";
469250397Sobrien    case DW_AT_MIPS_loop_begin:
469350397Sobrien      return "DW_AT_MIPS_loop_begin";
469450397Sobrien    case DW_AT_MIPS_tail_loop_begin:
469550397Sobrien      return "DW_AT_MIPS_tail_loop_begin";
469650397Sobrien    case DW_AT_MIPS_epilog_begin:
469750397Sobrien      return "DW_AT_MIPS_epilog_begin";
469850397Sobrien    case DW_AT_MIPS_loop_unroll_factor:
469950397Sobrien      return "DW_AT_MIPS_loop_unroll_factor";
470050397Sobrien    case DW_AT_MIPS_software_pipeline_depth:
470150397Sobrien      return "DW_AT_MIPS_software_pipeline_depth";
470250397Sobrien    case DW_AT_MIPS_linkage_name:
470350397Sobrien      return "DW_AT_MIPS_linkage_name";
470450397Sobrien    case DW_AT_MIPS_stride:
470550397Sobrien      return "DW_AT_MIPS_stride";
470650397Sobrien    case DW_AT_MIPS_abstract_name:
470750397Sobrien      return "DW_AT_MIPS_abstract_name";
470850397Sobrien    case DW_AT_MIPS_clone_origin:
470950397Sobrien      return "DW_AT_MIPS_clone_origin";
471050397Sobrien    case DW_AT_MIPS_has_inlines:
471150397Sobrien      return "DW_AT_MIPS_has_inlines";
471250397Sobrien
471350397Sobrien    case DW_AT_sf_names:
471450397Sobrien      return "DW_AT_sf_names";
471550397Sobrien    case DW_AT_src_info:
471650397Sobrien      return "DW_AT_src_info";
471750397Sobrien    case DW_AT_mac_info:
471850397Sobrien      return "DW_AT_mac_info";
471950397Sobrien    case DW_AT_src_coords:
472050397Sobrien      return "DW_AT_src_coords";
472150397Sobrien    case DW_AT_body_begin:
472250397Sobrien      return "DW_AT_body_begin";
472350397Sobrien    case DW_AT_body_end:
472450397Sobrien      return "DW_AT_body_end";
472596263Sobrien    case DW_AT_GNU_vector:
472696263Sobrien      return "DW_AT_GNU_vector";
472796263Sobrien
472890075Sobrien    case DW_AT_VMS_rtnbeg_pd_address:
472990075Sobrien      return "DW_AT_VMS_rtnbeg_pd_address";
473090075Sobrien
473150397Sobrien    default:
473250397Sobrien      return "DW_AT_<unknown>";
473350397Sobrien    }
473450397Sobrien}
473550397Sobrien
473650397Sobrien/* Convert a DWARF value form code into its string name.  */
473750397Sobrien
473890075Sobrienstatic const char *
4739132718Skandwarf_form_name (unsigned int form)
474050397Sobrien{
474150397Sobrien  switch (form)
474250397Sobrien    {
474350397Sobrien    case DW_FORM_addr:
474450397Sobrien      return "DW_FORM_addr";
474550397Sobrien    case DW_FORM_block2:
474650397Sobrien      return "DW_FORM_block2";
474750397Sobrien    case DW_FORM_block4:
474850397Sobrien      return "DW_FORM_block4";
474950397Sobrien    case DW_FORM_data2:
475050397Sobrien      return "DW_FORM_data2";
475150397Sobrien    case DW_FORM_data4:
475250397Sobrien      return "DW_FORM_data4";
475350397Sobrien    case DW_FORM_data8:
475450397Sobrien      return "DW_FORM_data8";
475550397Sobrien    case DW_FORM_string:
475650397Sobrien      return "DW_FORM_string";
475750397Sobrien    case DW_FORM_block:
475850397Sobrien      return "DW_FORM_block";
475950397Sobrien    case DW_FORM_block1:
476050397Sobrien      return "DW_FORM_block1";
476150397Sobrien    case DW_FORM_data1:
476250397Sobrien      return "DW_FORM_data1";
476350397Sobrien    case DW_FORM_flag:
476450397Sobrien      return "DW_FORM_flag";
476550397Sobrien    case DW_FORM_sdata:
476650397Sobrien      return "DW_FORM_sdata";
476750397Sobrien    case DW_FORM_strp:
476850397Sobrien      return "DW_FORM_strp";
476950397Sobrien    case DW_FORM_udata:
477050397Sobrien      return "DW_FORM_udata";
477150397Sobrien    case DW_FORM_ref_addr:
477250397Sobrien      return "DW_FORM_ref_addr";
477350397Sobrien    case DW_FORM_ref1:
477450397Sobrien      return "DW_FORM_ref1";
477550397Sobrien    case DW_FORM_ref2:
477650397Sobrien      return "DW_FORM_ref2";
477750397Sobrien    case DW_FORM_ref4:
477850397Sobrien      return "DW_FORM_ref4";
477950397Sobrien    case DW_FORM_ref8:
478050397Sobrien      return "DW_FORM_ref8";
478150397Sobrien    case DW_FORM_ref_udata:
478250397Sobrien      return "DW_FORM_ref_udata";
478350397Sobrien    case DW_FORM_indirect:
478450397Sobrien      return "DW_FORM_indirect";
478550397Sobrien    default:
478650397Sobrien      return "DW_FORM_<unknown>";
478750397Sobrien    }
478850397Sobrien}
478950397Sobrien
479050397Sobrien/* Determine the "ultimate origin" of a decl.  The decl may be an inlined
479150397Sobrien   instance of an inlined instance of a decl which is local to an inline
479250397Sobrien   function, so we have to trace all of the way back through the origin chain
479350397Sobrien   to find out what sort of node actually served as the original seed for the
479450397Sobrien   given block.  */
479550397Sobrien
479650397Sobrienstatic tree
4797132718Skandecl_ultimate_origin (tree decl)
479850397Sobrien{
4799169689Skan  if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4800169689Skan    return NULL_TREE;
4801169689Skan
480290075Sobrien  /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
480390075Sobrien     nodes in the function to point to themselves; ignore that if
480490075Sobrien     we're trying to output the abstract instance of this function.  */
480590075Sobrien  if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
480690075Sobrien    return NULL_TREE;
480790075Sobrien
4808169689Skan  /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4809169689Skan     most distant ancestor, this should never happen.  */
4810169689Skan  gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
481150397Sobrien
481252284Sobrien  return DECL_ABSTRACT_ORIGIN (decl);
481350397Sobrien}
481450397Sobrien
481550397Sobrien/* Determine the "ultimate origin" of a block.  The block may be an inlined
481650397Sobrien   instance of an inlined instance of a block which is local to an inline
481750397Sobrien   function, so we have to trace all of the way back through the origin chain
481850397Sobrien   to find out what sort of node actually served as the original seed for the
481950397Sobrien   given block.  */
482050397Sobrien
482150397Sobrienstatic tree
4822132718Skanblock_ultimate_origin (tree block)
482350397Sobrien{
482490075Sobrien  tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
482550397Sobrien
482690075Sobrien  /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
482790075Sobrien     nodes in the function to point to themselves; ignore that if
482890075Sobrien     we're trying to output the abstract instance of this function.  */
482990075Sobrien  if (BLOCK_ABSTRACT (block) && immediate_origin == block)
483090075Sobrien    return NULL_TREE;
483190075Sobrien
483250397Sobrien  if (immediate_origin == NULL_TREE)
483350397Sobrien    return NULL_TREE;
483450397Sobrien  else
483550397Sobrien    {
483690075Sobrien      tree ret_val;
483790075Sobrien      tree lookahead = immediate_origin;
483850397Sobrien
483950397Sobrien      do
484050397Sobrien	{
484150397Sobrien	  ret_val = lookahead;
484290075Sobrien	  lookahead = (TREE_CODE (ret_val) == BLOCK
484390075Sobrien		       ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
484450397Sobrien	}
484550397Sobrien      while (lookahead != NULL && lookahead != ret_val);
4846169689Skan
4847169689Skan      /* The block's abstract origin chain may not be the *ultimate* origin of
4848169689Skan	 the block. It could lead to a DECL that has an abstract origin set.
4849169689Skan	 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4850169689Skan	 will give us if it has one).  Note that DECL's abstract origins are
4851169689Skan	 supposed to be the most distant ancestor (or so decl_ultimate_origin
4852169689Skan	 claims), so we don't need to loop following the DECL origins.  */
4853169689Skan      if (DECL_P (ret_val))
4854169689Skan	return DECL_ORIGIN (ret_val);
485550397Sobrien
485650397Sobrien      return ret_val;
485750397Sobrien    }
485850397Sobrien}
485950397Sobrien
486050397Sobrien/* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
486150397Sobrien   of a virtual function may refer to a base class, so we check the 'this'
486250397Sobrien   parameter.  */
486350397Sobrien
486450397Sobrienstatic tree
4865132718Skandecl_class_context (tree decl)
486650397Sobrien{
486750397Sobrien  tree context = NULL_TREE;
486850397Sobrien
486950397Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
487050397Sobrien    context = DECL_CONTEXT (decl);
487150397Sobrien  else
487250397Sobrien    context = TYPE_MAIN_VARIANT
487350397Sobrien      (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
487450397Sobrien
487590075Sobrien  if (context && !TYPE_P (context))
487650397Sobrien    context = NULL_TREE;
487750397Sobrien
487850397Sobrien  return context;
487950397Sobrien}
488050397Sobrien
4881169689Skan/* Add an attribute/value pair to a DIE.  */
488250397Sobrien
488350397Sobrienstatic inline void
4884132718Skanadd_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
488550397Sobrien{
4886169689Skan  /* Maybe this should be an assert?  */
4887169689Skan  if (die == NULL)
4888169689Skan    return;
4889169689Skan
4890169689Skan  if (die->die_attr == NULL)
4891169689Skan    die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4892169689Skan  VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
489350397Sobrien}
489450397Sobrien
4895132718Skanstatic inline enum dw_val_class
4896132718SkanAT_class (dw_attr_ref a)
489790075Sobrien{
489890075Sobrien  return a->dw_attr_val.val_class;
489990075Sobrien}
490090075Sobrien
490150397Sobrien/* Add a flag value attribute to a DIE.  */
490250397Sobrien
490350397Sobrienstatic inline void
4904132718Skanadd_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
490550397Sobrien{
4906169689Skan  dw_attr_node attr;
490750397Sobrien
4908169689Skan  attr.dw_attr = attr_kind;
4909169689Skan  attr.dw_attr_val.val_class = dw_val_class_flag;
4910169689Skan  attr.dw_attr_val.v.val_flag = flag;
4911169689Skan  add_dwarf_attr (die, &attr);
491250397Sobrien}
491350397Sobrien
491490075Sobrienstatic inline unsigned
4915132718SkanAT_flag (dw_attr_ref a)
491690075Sobrien{
4917169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_flag);
4918169689Skan  return a->dw_attr_val.v.val_flag;
491990075Sobrien}
492090075Sobrien
492150397Sobrien/* Add a signed integer attribute value to a DIE.  */
492250397Sobrien
492350397Sobrienstatic inline void
4924132718Skanadd_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
492550397Sobrien{
4926169689Skan  dw_attr_node attr;
492750397Sobrien
4928169689Skan  attr.dw_attr = attr_kind;
4929169689Skan  attr.dw_attr_val.val_class = dw_val_class_const;
4930169689Skan  attr.dw_attr_val.v.val_int = int_val;
4931169689Skan  add_dwarf_attr (die, &attr);
493250397Sobrien}
493350397Sobrien
4934132718Skanstatic inline HOST_WIDE_INT
4935132718SkanAT_int (dw_attr_ref a)
493690075Sobrien{
4937169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_const);
4938169689Skan  return a->dw_attr_val.v.val_int;
493990075Sobrien}
494090075Sobrien
494150397Sobrien/* Add an unsigned integer attribute value to a DIE.  */
494250397Sobrien
494350397Sobrienstatic inline void
4944132718Skanadd_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4945132718Skan		 unsigned HOST_WIDE_INT unsigned_val)
494650397Sobrien{
4947169689Skan  dw_attr_node attr;
494850397Sobrien
4949169689Skan  attr.dw_attr = attr_kind;
4950169689Skan  attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4951169689Skan  attr.dw_attr_val.v.val_unsigned = unsigned_val;
4952169689Skan  add_dwarf_attr (die, &attr);
495350397Sobrien}
495450397Sobrien
4955132718Skanstatic inline unsigned HOST_WIDE_INT
4956132718SkanAT_unsigned (dw_attr_ref a)
495790075Sobrien{
4958169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4959169689Skan  return a->dw_attr_val.v.val_unsigned;
496090075Sobrien}
496190075Sobrien
496250397Sobrien/* Add an unsigned double integer attribute value to a DIE.  */
496350397Sobrien
496450397Sobrienstatic inline void
4965132718Skanadd_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4966132718Skan		  long unsigned int val_hi, long unsigned int val_low)
496750397Sobrien{
4968169689Skan  dw_attr_node attr;
496950397Sobrien
4970169689Skan  attr.dw_attr = attr_kind;
4971169689Skan  attr.dw_attr_val.val_class = dw_val_class_long_long;
4972169689Skan  attr.dw_attr_val.v.val_long_long.hi = val_hi;
4973169689Skan  attr.dw_attr_val.v.val_long_long.low = val_low;
4974169689Skan  add_dwarf_attr (die, &attr);
497550397Sobrien}
497650397Sobrien
497750397Sobrien/* Add a floating point attribute value to a DIE and return it.  */
497850397Sobrien
497950397Sobrienstatic inline void
4980132718Skanadd_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4981132718Skan	    unsigned int length, unsigned int elt_size, unsigned char *array)
498250397Sobrien{
4983169689Skan  dw_attr_node attr;
498450397Sobrien
4985169689Skan  attr.dw_attr = attr_kind;
4986169689Skan  attr.dw_attr_val.val_class = dw_val_class_vec;
4987169689Skan  attr.dw_attr_val.v.val_vec.length = length;
4988169689Skan  attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4989169689Skan  attr.dw_attr_val.v.val_vec.array = array;
4990169689Skan  add_dwarf_attr (die, &attr);
499150397Sobrien}
499250397Sobrien
4993132718Skan/* Hash and equality functions for debug_str_hash.  */
4994132718Skan
4995132718Skanstatic hashval_t
4996132718Skandebug_str_do_hash (const void *x)
4997132718Skan{
4998132718Skan  return htab_hash_string (((const struct indirect_string_node *)x)->str);
4999132718Skan}
5000132718Skan
5001132718Skanstatic int
5002132718Skandebug_str_eq (const void *x1, const void *x2)
5003132718Skan{
5004132718Skan  return strcmp ((((const struct indirect_string_node *)x1)->str),
5005132718Skan		 (const char *)x2) == 0;
5006132718Skan}
5007132718Skan
500850397Sobrien/* Add a string attribute value to a DIE.  */
500950397Sobrien
501050397Sobrienstatic inline void
5011132718Skanadd_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
501250397Sobrien{
5013169689Skan  dw_attr_node attr;
501490075Sobrien  struct indirect_string_node *node;
5015132718Skan  void **slot;
5016117395Skan
501790075Sobrien  if (! debug_str_hash)
5018132718Skan    debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5019132718Skan				      debug_str_eq, NULL);
502050397Sobrien
5021132718Skan  slot = htab_find_slot_with_hash (debug_str_hash, str,
5022132718Skan				   htab_hash_string (str), INSERT);
5023132718Skan  if (*slot == NULL)
5024132718Skan    *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
5025132718Skan  node = (struct indirect_string_node *) *slot;
5026132718Skan  node->str = ggc_strdup (str);
502790075Sobrien  node->refcount++;
502890075Sobrien
5029169689Skan  attr.dw_attr = attr_kind;
5030169689Skan  attr.dw_attr_val.val_class = dw_val_class_str;
5031169689Skan  attr.dw_attr_val.v.val_str = node;
5032169689Skan  add_dwarf_attr (die, &attr);
503350397Sobrien}
503450397Sobrien
503590075Sobrienstatic inline const char *
5036132718SkanAT_string (dw_attr_ref a)
503790075Sobrien{
5038169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_str);
5039169689Skan  return a->dw_attr_val.v.val_str->str;
504090075Sobrien}
504190075Sobrien
504290075Sobrien/* Find out whether a string should be output inline in DIE
504390075Sobrien   or out-of-line in .debug_str section.  */
504490075Sobrien
504590075Sobrienstatic int
5046132718SkanAT_string_form (dw_attr_ref a)
504790075Sobrien{
5048169689Skan  struct indirect_string_node *node;
5049169689Skan  unsigned int len;
5050169689Skan  char label[32];
505190075Sobrien
5052169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_str);
505390075Sobrien
5054169689Skan  node = a->dw_attr_val.v.val_str;
5055169689Skan  if (node->form)
5056169689Skan    return node->form;
505790075Sobrien
5058169689Skan  len = strlen (node->str) + 1;
505990075Sobrien
5060169689Skan  /* If the string is shorter or equal to the size of the reference, it is
5061169689Skan     always better to put it inline.  */
5062169689Skan  if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5063169689Skan    return node->form = DW_FORM_string;
506490075Sobrien
5065169689Skan  /* If we cannot expect the linker to merge strings in .debug_str
5066169689Skan     section, only put it into .debug_str if it is worth even in this
5067169689Skan     single module.  */
5068169689Skan  if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5069169689Skan      && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5070169689Skan    return node->form = DW_FORM_string;
507190075Sobrien
5072169689Skan  ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5073169689Skan  ++dw2_string_counter;
5074169689Skan  node->label = xstrdup (label);
507590075Sobrien
5076169689Skan  return node->form = DW_FORM_strp;
507790075Sobrien}
507890075Sobrien
507950397Sobrien/* Add a DIE reference attribute value to a DIE.  */
508050397Sobrien
508150397Sobrienstatic inline void
5082132718Skanadd_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
508350397Sobrien{
5084169689Skan  dw_attr_node attr;
508550397Sobrien
5086169689Skan  attr.dw_attr = attr_kind;
5087169689Skan  attr.dw_attr_val.val_class = dw_val_class_die_ref;
5088169689Skan  attr.dw_attr_val.v.val_die_ref.die = targ_die;
5089169689Skan  attr.dw_attr_val.v.val_die_ref.external = 0;
5090169689Skan  add_dwarf_attr (die, &attr);
509150397Sobrien}
509250397Sobrien
5093132718Skan/* Add an AT_specification attribute to a DIE, and also make the back
5094132718Skan   pointer from the specification to the definition.  */
5095132718Skan
5096132718Skanstatic inline void
5097132718Skanadd_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5098132718Skan{
5099132718Skan  add_AT_die_ref (die, DW_AT_specification, targ_die);
5100169689Skan  gcc_assert (!targ_die->die_definition);
5101132718Skan  targ_die->die_definition = die;
5102132718Skan}
5103132718Skan
510490075Sobrienstatic inline dw_die_ref
5105132718SkanAT_ref (dw_attr_ref a)
510690075Sobrien{
5107169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5108169689Skan  return a->dw_attr_val.v.val_die_ref.die;
510990075Sobrien}
511090075Sobrien
511190075Sobrienstatic inline int
5112132718SkanAT_ref_external (dw_attr_ref a)
511390075Sobrien{
511490075Sobrien  if (a && AT_class (a) == dw_val_class_die_ref)
511590075Sobrien    return a->dw_attr_val.v.val_die_ref.external;
511690075Sobrien
511790075Sobrien  return 0;
511890075Sobrien}
511990075Sobrien
512090075Sobrienstatic inline void
5121132718Skanset_AT_ref_external (dw_attr_ref a, int i)
512290075Sobrien{
5123169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5124169689Skan  a->dw_attr_val.v.val_die_ref.external = i;
512590075Sobrien}
512690075Sobrien
512750397Sobrien/* Add an FDE reference attribute value to a DIE.  */
512850397Sobrien
512950397Sobrienstatic inline void
5130132718Skanadd_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
513150397Sobrien{
5132169689Skan  dw_attr_node attr;
513350397Sobrien
5134169689Skan  attr.dw_attr = attr_kind;
5135169689Skan  attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5136169689Skan  attr.dw_attr_val.v.val_fde_index = targ_fde;
5137169689Skan  add_dwarf_attr (die, &attr);
513850397Sobrien}
513950397Sobrien
514050397Sobrien/* Add a location description attribute value to a DIE.  */
514150397Sobrien
514250397Sobrienstatic inline void
5143132718Skanadd_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
514450397Sobrien{
5145169689Skan  dw_attr_node attr;
514650397Sobrien
5147169689Skan  attr.dw_attr = attr_kind;
5148169689Skan  attr.dw_attr_val.val_class = dw_val_class_loc;
5149169689Skan  attr.dw_attr_val.v.val_loc = loc;
5150169689Skan  add_dwarf_attr (die, &attr);
515150397Sobrien}
515250397Sobrien
515390075Sobrienstatic inline dw_loc_descr_ref
5154132718SkanAT_loc (dw_attr_ref a)
515590075Sobrien{
5156169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_loc);
5157169689Skan  return a->dw_attr_val.v.val_loc;
515890075Sobrien}
515990075Sobrien
516090075Sobrienstatic inline void
5161132718Skanadd_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
516290075Sobrien{
5163169689Skan  dw_attr_node attr;
516490075Sobrien
5165169689Skan  attr.dw_attr = attr_kind;
5166169689Skan  attr.dw_attr_val.val_class = dw_val_class_loc_list;
5167169689Skan  attr.dw_attr_val.v.val_loc_list = loc_list;
5168169689Skan  add_dwarf_attr (die, &attr);
5169169689Skan  have_location_lists = true;
517090075Sobrien}
517190075Sobrien
517290075Sobrienstatic inline dw_loc_list_ref
5173132718SkanAT_loc_list (dw_attr_ref a)
517490075Sobrien{
5175169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5176169689Skan  return a->dw_attr_val.v.val_loc_list;
517790075Sobrien}
517890075Sobrien
517950397Sobrien/* Add an address constant attribute value to a DIE.  */
518050397Sobrien
518150397Sobrienstatic inline void
5182132718Skanadd_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
518350397Sobrien{
5184169689Skan  dw_attr_node attr;
518550397Sobrien
5186169689Skan  attr.dw_attr = attr_kind;
5187169689Skan  attr.dw_attr_val.val_class = dw_val_class_addr;
5188169689Skan  attr.dw_attr_val.v.val_addr = addr;
5189169689Skan  add_dwarf_attr (die, &attr);
519050397Sobrien}
519150397Sobrien
5192169689Skan/* Get the RTX from to an address DIE attribute.  */
5193169689Skan
519490075Sobrienstatic inline rtx
5195132718SkanAT_addr (dw_attr_ref a)
519690075Sobrien{
5197169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_addr);
5198169689Skan  return a->dw_attr_val.v.val_addr;
5199169689Skan}
520090075Sobrien
5201169689Skan/* Add a file attribute value to a DIE.  */
5202169689Skan
5203169689Skanstatic inline void
5204169689Skanadd_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5205169689Skan	     struct dwarf_file_data *fd)
5206169689Skan{
5207169689Skan  dw_attr_node attr;
5208169689Skan
5209169689Skan  attr.dw_attr = attr_kind;
5210169689Skan  attr.dw_attr_val.val_class = dw_val_class_file;
5211169689Skan  attr.dw_attr_val.v.val_file = fd;
5212169689Skan  add_dwarf_attr (die, &attr);
521390075Sobrien}
521490075Sobrien
5215169689Skan/* Get the dwarf_file_data from a file DIE attribute.  */
5216169689Skan
5217169689Skanstatic inline struct dwarf_file_data *
5218169689SkanAT_file (dw_attr_ref a)
5219169689Skan{
5220169689Skan  gcc_assert (a && AT_class (a) == dw_val_class_file);
5221169689Skan  return a->dw_attr_val.v.val_file;
5222169689Skan}
5223169689Skan
522450397Sobrien/* Add a label identifier attribute value to a DIE.  */
522550397Sobrien
522650397Sobrienstatic inline void
5227132718Skanadd_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
522850397Sobrien{
5229169689Skan  dw_attr_node attr;
523050397Sobrien
5231169689Skan  attr.dw_attr = attr_kind;
5232169689Skan  attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5233169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5234169689Skan  add_dwarf_attr (die, &attr);
523550397Sobrien}
523650397Sobrien
5237169689Skan/* Add a section offset attribute value to a DIE, an offset into the
5238169689Skan   debug_line section.  */
523950397Sobrien
524050397Sobrienstatic inline void
5241169689Skanadd_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5242169689Skan		const char *label)
524350397Sobrien{
5244169689Skan  dw_attr_node attr;
524550397Sobrien
5246169689Skan  attr.dw_attr = attr_kind;
5247169689Skan  attr.dw_attr_val.val_class = dw_val_class_lineptr;
5248169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5249169689Skan  add_dwarf_attr (die, &attr);
525050397Sobrien}
525150397Sobrien
5252169689Skan/* Add a section offset attribute value to a DIE, an offset into the
5253169689Skan   debug_macinfo section.  */
5254169689Skan
5255169689Skanstatic inline void
5256169689Skanadd_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5257169689Skan	       const char *label)
5258169689Skan{
5259169689Skan  dw_attr_node attr;
5260169689Skan
5261169689Skan  attr.dw_attr = attr_kind;
5262169689Skan  attr.dw_attr_val.val_class = dw_val_class_macptr;
5263169689Skan  attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5264169689Skan  add_dwarf_attr (die, &attr);
5265169689Skan}
5266169689Skan
526790075Sobrien/* Add an offset attribute value to a DIE.  */
526850397Sobrien
526990075Sobrienstatic inline void
5270132718Skanadd_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5271132718Skan	       unsigned HOST_WIDE_INT offset)
527250397Sobrien{
5273169689Skan  dw_attr_node attr;
527450397Sobrien
5275169689Skan  attr.dw_attr = attr_kind;
5276169689Skan  attr.dw_attr_val.val_class = dw_val_class_offset;
5277169689Skan  attr.dw_attr_val.v.val_offset = offset;
5278169689Skan  add_dwarf_attr (die, &attr);
527990075Sobrien}
528050397Sobrien
528190075Sobrien/* Add an range_list attribute value to a DIE.  */
528290075Sobrien
528390075Sobrienstatic void
5284132718Skanadd_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5285132718Skan		   long unsigned int offset)
528690075Sobrien{
5287169689Skan  dw_attr_node attr;
528890075Sobrien
5289169689Skan  attr.dw_attr = attr_kind;
5290169689Skan  attr.dw_attr_val.val_class = dw_val_class_range_list;
5291169689Skan  attr.dw_attr_val.v.val_offset = offset;
5292169689Skan  add_dwarf_attr (die, &attr);
529350397Sobrien}
529450397Sobrien
529590075Sobrienstatic inline const char *
5296132718SkanAT_lbl (dw_attr_ref a)
529790075Sobrien{
5298169689Skan  gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5299169689Skan		    || AT_class (a) == dw_val_class_lineptr
5300169689Skan		    || AT_class (a) == dw_val_class_macptr));
5301169689Skan  return a->dw_attr_val.v.val_lbl_id;
530290075Sobrien}
530390075Sobrien
530450397Sobrien/* Get the attribute of type attr_kind.  */
530550397Sobrien
5306132718Skanstatic dw_attr_ref
5307132718Skanget_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
530850397Sobrien{
530990075Sobrien  dw_attr_ref a;
5310169689Skan  unsigned ix;
531190075Sobrien  dw_die_ref spec = NULL;
531290075Sobrien
5313169689Skan  if (! die)
5314169689Skan    return NULL;
531550397Sobrien
5316169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5317169689Skan    if (a->dw_attr == attr_kind)
5318169689Skan      return a;
5319169689Skan    else if (a->dw_attr == DW_AT_specification
5320169689Skan	     || a->dw_attr == DW_AT_abstract_origin)
5321169689Skan      spec = AT_ref (a);
5322169689Skan
5323169689Skan  if (spec)
5324169689Skan    return get_AT (spec, attr_kind);
532550397Sobrien
532650397Sobrien  return NULL;
532750397Sobrien}
532850397Sobrien
532990075Sobrien/* Return the "low pc" attribute value, typically associated with a subprogram
533090075Sobrien   DIE.  Return null if the "low pc" attribute is either not present, or if it
533190075Sobrien   cannot be represented as an assembler label identifier.  */
533250397Sobrien
533390075Sobrienstatic inline const char *
5334132718Skanget_AT_low_pc (dw_die_ref die)
533550397Sobrien{
533690075Sobrien  dw_attr_ref a = get_AT (die, DW_AT_low_pc);
533750397Sobrien
533890075Sobrien  return a ? AT_lbl (a) : NULL;
533950397Sobrien}
534050397Sobrien
534190075Sobrien/* Return the "high pc" attribute value, typically associated with a subprogram
534290075Sobrien   DIE.  Return null if the "high pc" attribute is either not present, or if it
534390075Sobrien   cannot be represented as an assembler label identifier.  */
534450397Sobrien
534590075Sobrienstatic inline const char *
5346132718Skanget_AT_hi_pc (dw_die_ref die)
534750397Sobrien{
534890075Sobrien  dw_attr_ref a = get_AT (die, DW_AT_high_pc);
534950397Sobrien
535090075Sobrien  return a ? AT_lbl (a) : NULL;
535150397Sobrien}
535250397Sobrien
535350397Sobrien/* Return the value of the string attribute designated by ATTR_KIND, or
535450397Sobrien   NULL if it is not present.  */
535550397Sobrien
535690075Sobrienstatic inline const char *
5357132718Skanget_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
535850397Sobrien{
535990075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
536050397Sobrien
536190075Sobrien  return a ? AT_string (a) : NULL;
536250397Sobrien}
536350397Sobrien
536450397Sobrien/* Return the value of the flag attribute designated by ATTR_KIND, or -1
536550397Sobrien   if it is not present.  */
536650397Sobrien
536750397Sobrienstatic inline int
5368132718Skanget_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
536950397Sobrien{
537090075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
537150397Sobrien
537290075Sobrien  return a ? AT_flag (a) : 0;
537350397Sobrien}
537450397Sobrien
537550397Sobrien/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
537650397Sobrien   if it is not present.  */
537750397Sobrien
537850397Sobrienstatic inline unsigned
5379132718Skanget_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
538050397Sobrien{
538190075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
538250397Sobrien
538390075Sobrien  return a ? AT_unsigned (a) : 0;
538490075Sobrien}
538550397Sobrien
538690075Sobrienstatic inline dw_die_ref
5387132718Skanget_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
538890075Sobrien{
538990075Sobrien  dw_attr_ref a = get_AT (die, attr_kind);
539090075Sobrien
539190075Sobrien  return a ? AT_ref (a) : NULL;
539250397Sobrien}
539350397Sobrien
5394169689Skanstatic inline struct dwarf_file_data *
5395169689Skanget_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5396169689Skan{
5397169689Skan  dw_attr_ref a = get_AT (die, attr_kind);
5398169689Skan
5399169689Skan  return a ? AT_file (a) : NULL;
5400169689Skan}
5401169689Skan
5402132718Skan/* Return TRUE if the language is C or C++.  */
5403132718Skan
5404132718Skanstatic inline bool
5405132718Skanis_c_family (void)
540650397Sobrien{
5407132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
540850397Sobrien
5409169689Skan  return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5410169689Skan	  || lang == DW_LANG_C99
5411169689Skan	  || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
541290075Sobrien}
541350397Sobrien
5414132718Skan/* Return TRUE if the language is C++.  */
5415132718Skan
5416132718Skanstatic inline bool
5417132718Skanis_cxx (void)
541890075Sobrien{
5419169689Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5420169689Skan
5421169689Skan  return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5422117395Skan}
542390075Sobrien
5424132718Skan/* Return TRUE if the language is Fortran.  */
5425132718Skan
5426132718Skanstatic inline bool
5427132718Skanis_fortran (void)
542850397Sobrien{
5429132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
543050397Sobrien
5431169689Skan  return (lang == DW_LANG_Fortran77
5432169689Skan	  || lang == DW_LANG_Fortran90
5433169689Skan	  || lang == DW_LANG_Fortran95);
543490075Sobrien}
543550397Sobrien
5436132718Skan/* Return TRUE if the language is Java.  */
5437132718Skan
5438132718Skanstatic inline bool
5439132718Skanis_java (void)
544090075Sobrien{
5441132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
544290075Sobrien
5443132718Skan  return lang == DW_LANG_Java;
544490075Sobrien}
544590075Sobrien
5446132718Skan/* Return TRUE if the language is Ada.  */
544790075Sobrien
5448132718Skanstatic inline bool
5449132718Skanis_ada (void)
545090075Sobrien{
5451132718Skan  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
545290075Sobrien
5453132718Skan  return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5454132718Skan}
545590075Sobrien
545650397Sobrien/* Remove the specified attribute if present.  */
545750397Sobrien
545890075Sobrienstatic void
5459132718Skanremove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
546050397Sobrien{
5461169689Skan  dw_attr_ref a;
5462169689Skan  unsigned ix;
546350397Sobrien
5464169689Skan  if (! die)
5465169689Skan    return;
546650397Sobrien
5467169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5468169689Skan    if (a->dw_attr == attr_kind)
5469169689Skan      {
5470169689Skan	if (AT_class (a) == dw_val_class_str)
5471169689Skan	  if (a->dw_attr_val.v.val_str->refcount)
5472169689Skan	    a->dw_attr_val.v.val_str->refcount--;
5473169689Skan
5474169689Skan	/* VEC_ordered_remove should help reduce the number of abbrevs
5475169689Skan	   that are needed.  */
5476169689Skan	VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5477169689Skan	return;
5478169689Skan      }
547950397Sobrien}
548050397Sobrien
5481169689Skan/* Remove CHILD from its parent.  PREV must have the property that
5482169689Skan   PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5483146895Skan
5484146895Skanstatic void
5485169689Skanremove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5486146895Skan{
5487169689Skan  gcc_assert (child->die_parent == prev->die_parent);
5488169689Skan  gcc_assert (prev->die_sib == child);
5489169689Skan  if (prev == child)
5490146895Skan    {
5491169689Skan      gcc_assert (child->die_parent->die_child == child);
5492169689Skan      prev = NULL;
5493146895Skan    }
5494169689Skan  else
5495169689Skan    prev->die_sib = child->die_sib;
5496169689Skan  if (child->die_parent->die_child == child)
5497169689Skan    child->die_parent->die_child = prev;
5498146895Skan}
5499146895Skan
5500169689Skan/* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5501169689Skan   matches TAG.  */
550290075Sobrien
5503169689Skanstatic void
5504169689Skanremove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
550590075Sobrien{
5506169689Skan  dw_die_ref c;
5507169689Skan
5508169689Skan  c = die->die_child;
5509169689Skan  if (c) do {
5510169689Skan    dw_die_ref prev = c;
5511169689Skan    c = c->die_sib;
5512169689Skan    while (c->die_tag == tag)
5513169689Skan      {
5514169689Skan	remove_child_with_prev (c, prev);
5515169689Skan	/* Might have removed every child.  */
5516169689Skan	if (c == c->die_sib)
5517169689Skan	  return;
5518169689Skan	c = c->die_sib;
5519169689Skan      }
5520169689Skan  } while (c != die->die_child);
552190075Sobrien}
552290075Sobrien
5523169689Skan/* Add a CHILD_DIE as the last child of DIE.  */
552450397Sobrien
552590075Sobrienstatic void
5526169689Skanadd_child_die (dw_die_ref die, dw_die_ref child_die)
552750397Sobrien{
5528169689Skan  /* FIXME this should probably be an assert.  */
5529169689Skan  if (! die || ! child_die)
5530169689Skan    return;
5531169689Skan  gcc_assert (die != child_die);
553250397Sobrien
5533169689Skan  child_die->die_parent = die;
5534169689Skan  if (die->die_child)
553550397Sobrien    {
5536169689Skan      child_die->die_sib = die->die_child->die_sib;
5537169689Skan      die->die_child->die_sib = child_die;
553850397Sobrien    }
5539169689Skan  else
5540169689Skan    child_die->die_sib = child_die;
5541169689Skan  die->die_child = child_die;
554250397Sobrien}
554350397Sobrien
554490075Sobrien/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5545169689Skan   is the specification, to the end of PARENT's list of children.
5546169689Skan   This is done by removing and re-adding it.  */
554790075Sobrien
554890075Sobrienstatic void
5549132718Skansplice_child_die (dw_die_ref parent, dw_die_ref child)
555090075Sobrien{
5551169689Skan  dw_die_ref p;
555290075Sobrien
555390075Sobrien  /* We want the declaration DIE from inside the class, not the
555490075Sobrien     specification DIE at toplevel.  */
555590075Sobrien  if (child->die_parent != parent)
555690075Sobrien    {
555790075Sobrien      dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
555890075Sobrien
555990075Sobrien      if (tmp)
556090075Sobrien	child = tmp;
556150397Sobrien    }
556290075Sobrien
5563169689Skan  gcc_assert (child->die_parent == parent
5564169689Skan	      || (child->die_parent
5565169689Skan		  == get_AT_ref (parent, DW_AT_specification)));
5566169689Skan
5567169689Skan  for (p = child->die_parent->die_child; ; p = p->die_sib)
5568169689Skan    if (p->die_sib == child)
556990075Sobrien      {
5570169689Skan	remove_child_with_prev (child, p);
557190075Sobrien	break;
557290075Sobrien      }
557390075Sobrien
5574169689Skan  add_child_die (parent, child);
557550397Sobrien}
557650397Sobrien
557750397Sobrien/* Return a pointer to a newly created DIE node.  */
557850397Sobrien
557950397Sobrienstatic inline dw_die_ref
5580132718Skannew_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
558150397Sobrien{
5582132718Skan  dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
558350397Sobrien
558450397Sobrien  die->die_tag = tag_value;
558550397Sobrien
558650397Sobrien  if (parent_die != NULL)
558750397Sobrien    add_child_die (parent_die, die);
558850397Sobrien  else
558950397Sobrien    {
559050397Sobrien      limbo_die_node *limbo_node;
559150397Sobrien
5592132718Skan      limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
559350397Sobrien      limbo_node->die = die;
559490075Sobrien      limbo_node->created_for = t;
559550397Sobrien      limbo_node->next = limbo_die_list;
559650397Sobrien      limbo_die_list = limbo_node;
559750397Sobrien    }
559850397Sobrien
559950397Sobrien  return die;
560050397Sobrien}
560150397Sobrien
560250397Sobrien/* Return the DIE associated with the given type specifier.  */
560350397Sobrien
560450397Sobrienstatic inline dw_die_ref
5605132718Skanlookup_type_die (tree type)
560650397Sobrien{
5607117395Skan  return TYPE_SYMTAB_DIE (type);
560850397Sobrien}
560950397Sobrien
561050397Sobrien/* Equate a DIE to a given type specifier.  */
561150397Sobrien
561290075Sobrienstatic inline void
5613132718Skanequate_type_number_to_die (tree type, dw_die_ref type_die)
561450397Sobrien{
5615117395Skan  TYPE_SYMTAB_DIE (type) = type_die;
561650397Sobrien}
561750397Sobrien
5618169689Skan/* Returns a hash value for X (which really is a die_struct).  */
5619169689Skan
5620169689Skanstatic hashval_t
5621169689Skandecl_die_table_hash (const void *x)
5622169689Skan{
5623169689Skan  return (hashval_t) ((const dw_die_ref) x)->decl_id;
5624169689Skan}
5625169689Skan
5626169689Skan/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5627169689Skan
5628169689Skanstatic int
5629169689Skandecl_die_table_eq (const void *x, const void *y)
5630169689Skan{
5631169689Skan  return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5632169689Skan}
5633169689Skan
563450397Sobrien/* Return the DIE associated with a given declaration.  */
563550397Sobrien
563650397Sobrienstatic inline dw_die_ref
5637132718Skanlookup_decl_die (tree decl)
563850397Sobrien{
5639169689Skan  return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5640169689Skan}
564150397Sobrien
5642169689Skan/* Returns a hash value for X (which really is a var_loc_list).  */
5643169689Skan
5644169689Skanstatic hashval_t
5645169689Skandecl_loc_table_hash (const void *x)
5646169689Skan{
5647169689Skan  return (hashval_t) ((const var_loc_list *) x)->decl_id;
564850397Sobrien}
564950397Sobrien
5650169689Skan/* Return nonzero if decl_id of var_loc_list X is the same as
5651169689Skan   UID of decl *Y.  */
5652169689Skan
5653169689Skanstatic int
5654169689Skandecl_loc_table_eq (const void *x, const void *y)
5655169689Skan{
5656169689Skan  return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5657169689Skan}
5658169689Skan
5659169689Skan/* Return the var_loc list associated with a given declaration.  */
5660169689Skan
5661169689Skanstatic inline var_loc_list *
5662169689Skanlookup_decl_loc (tree decl)
5663169689Skan{
5664169689Skan  return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5665169689Skan}
5666169689Skan
566750397Sobrien/* Equate a DIE to a particular declaration.  */
566850397Sobrien
566950397Sobrienstatic void
5670132718Skanequate_decl_number_to_die (tree decl, dw_die_ref decl_die)
567150397Sobrien{
567290075Sobrien  unsigned int decl_id = DECL_UID (decl);
5673169689Skan  void **slot;
567450397Sobrien
5675169689Skan  slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5676169689Skan  *slot = decl_die;
5677169689Skan  decl_die->decl_id = decl_id;
5678169689Skan}
567950397Sobrien
5680169689Skan/* Add a variable location node to the linked list for DECL.  */
568150397Sobrien
5682169689Skanstatic void
5683169689Skanadd_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5684169689Skan{
5685169689Skan  unsigned int decl_id = DECL_UID (decl);
5686169689Skan  var_loc_list *temp;
5687169689Skan  void **slot;
5688169689Skan
5689169689Skan  slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5690169689Skan  if (*slot == NULL)
5691169689Skan    {
5692169689Skan      temp = ggc_alloc_cleared (sizeof (var_loc_list));
5693169689Skan      temp->decl_id = decl_id;
5694169689Skan      *slot = temp;
569550397Sobrien    }
5696169689Skan  else
5697169689Skan    temp = *slot;
569850397Sobrien
5699169689Skan  if (temp->last)
5700169689Skan    {
5701169689Skan      /* If the current location is the same as the end of the list,
5702169689Skan	 we have nothing to do.  */
5703169689Skan      if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5704169689Skan			NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5705169689Skan	{
5706169689Skan	  /* Add LOC to the end of list and update LAST.  */
5707169689Skan	  temp->last->next = loc;
5708169689Skan	  temp->last = loc;
5709169689Skan	}
5710169689Skan    }
5711169689Skan  /* Do not add empty location to the beginning of the list.  */
5712169689Skan  else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5713169689Skan    {
5714169689Skan      temp->first = loc;
5715169689Skan      temp->last = loc;
5716169689Skan    }
571750397Sobrien}
571850397Sobrien
571950397Sobrien/* Keep track of the number of spaces used to indent the
572050397Sobrien   output of the debugging routines that print the structure of
572150397Sobrien   the DIE internal representation.  */
572250397Sobrienstatic int print_indent;
572350397Sobrien
572450397Sobrien/* Indent the line the number of spaces given by print_indent.  */
572550397Sobrien
572650397Sobrienstatic inline void
5727132718Skanprint_spaces (FILE *outfile)
572850397Sobrien{
572950397Sobrien  fprintf (outfile, "%*s", print_indent, "");
573050397Sobrien}
573150397Sobrien
573250397Sobrien/* Print the information associated with a given DIE, and its children.
573350397Sobrien   This routine is a debugging aid only.  */
573450397Sobrien
573550397Sobrienstatic void
5736132718Skanprint_die (dw_die_ref die, FILE *outfile)
573750397Sobrien{
573890075Sobrien  dw_attr_ref a;
573990075Sobrien  dw_die_ref c;
5740169689Skan  unsigned ix;
574150397Sobrien
574250397Sobrien  print_spaces (outfile);
574350397Sobrien  fprintf (outfile, "DIE %4lu: %s\n",
574450397Sobrien	   die->die_offset, dwarf_tag_name (die->die_tag));
574550397Sobrien  print_spaces (outfile);
574650397Sobrien  fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
574750397Sobrien  fprintf (outfile, " offset: %lu\n", die->die_offset);
574850397Sobrien
5749169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
575050397Sobrien    {
575150397Sobrien      print_spaces (outfile);
575250397Sobrien      fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
575350397Sobrien
575490075Sobrien      switch (AT_class (a))
575550397Sobrien	{
575650397Sobrien	case dw_val_class_addr:
575750397Sobrien	  fprintf (outfile, "address");
575850397Sobrien	  break;
575990075Sobrien	case dw_val_class_offset:
576090075Sobrien	  fprintf (outfile, "offset");
576190075Sobrien	  break;
576250397Sobrien	case dw_val_class_loc:
576350397Sobrien	  fprintf (outfile, "location descriptor");
576450397Sobrien	  break;
576590075Sobrien	case dw_val_class_loc_list:
576690075Sobrien	  fprintf (outfile, "location list -> label:%s",
576790075Sobrien		   AT_loc_list (a)->ll_symbol);
576890075Sobrien	  break;
576990075Sobrien	case dw_val_class_range_list:
577090075Sobrien	  fprintf (outfile, "range list");
577190075Sobrien	  break;
577250397Sobrien	case dw_val_class_const:
5773132718Skan	  fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
577450397Sobrien	  break;
577550397Sobrien	case dw_val_class_unsigned_const:
5776132718Skan	  fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
577750397Sobrien	  break;
577850397Sobrien	case dw_val_class_long_long:
577950397Sobrien	  fprintf (outfile, "constant (%lu,%lu)",
578090075Sobrien		   a->dw_attr_val.v.val_long_long.hi,
578190075Sobrien		   a->dw_attr_val.v.val_long_long.low);
578250397Sobrien	  break;
5783132718Skan	case dw_val_class_vec:
5784132718Skan	  fprintf (outfile, "floating-point or vector constant");
578550397Sobrien	  break;
578650397Sobrien	case dw_val_class_flag:
578790075Sobrien	  fprintf (outfile, "%u", AT_flag (a));
578850397Sobrien	  break;
578950397Sobrien	case dw_val_class_die_ref:
579090075Sobrien	  if (AT_ref (a) != NULL)
579190075Sobrien	    {
579290075Sobrien	      if (AT_ref (a)->die_symbol)
579390075Sobrien		fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
579490075Sobrien	      else
579590075Sobrien		fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
579690075Sobrien	    }
579750397Sobrien	  else
579850397Sobrien	    fprintf (outfile, "die -> <null>");
579950397Sobrien	  break;
580050397Sobrien	case dw_val_class_lbl_id:
5801169689Skan	case dw_val_class_lineptr:
5802169689Skan	case dw_val_class_macptr:
580390075Sobrien	  fprintf (outfile, "label: %s", AT_lbl (a));
580450397Sobrien	  break;
580550397Sobrien	case dw_val_class_str:
580690075Sobrien	  if (AT_string (a) != NULL)
580790075Sobrien	    fprintf (outfile, "\"%s\"", AT_string (a));
580850397Sobrien	  else
580950397Sobrien	    fprintf (outfile, "<null>");
581050397Sobrien	  break;
5811169689Skan	case dw_val_class_file:
5812169689Skan	  fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5813169689Skan		   AT_file (a)->emitted_number);
5814169689Skan	  break;
581550397Sobrien	default:
581650397Sobrien	  break;
581750397Sobrien	}
581850397Sobrien
581950397Sobrien      fprintf (outfile, "\n");
582050397Sobrien    }
582150397Sobrien
582250397Sobrien  if (die->die_child != NULL)
582350397Sobrien    {
582450397Sobrien      print_indent += 4;
5825169689Skan      FOR_EACH_CHILD (die, c, print_die (c, outfile));
582650397Sobrien      print_indent -= 4;
582750397Sobrien    }
582890075Sobrien  if (print_indent == 0)
582990075Sobrien    fprintf (outfile, "\n");
583050397Sobrien}
583150397Sobrien
583250397Sobrien/* Print the contents of the source code line number correspondence table.
583350397Sobrien   This routine is a debugging aid only.  */
583450397Sobrien
583550397Sobrienstatic void
5836132718Skanprint_dwarf_line_table (FILE *outfile)
583750397Sobrien{
583890075Sobrien  unsigned i;
583990075Sobrien  dw_line_info_ref line_info;
584050397Sobrien
584150397Sobrien  fprintf (outfile, "\n\nDWARF source line information\n");
584290075Sobrien  for (i = 1; i < line_info_table_in_use; i++)
584350397Sobrien    {
584450397Sobrien      line_info = &line_info_table[i];
5845169689Skan      fprintf (outfile, "%5d: %4ld %6ld\n", i,
5846169689Skan	       line_info->dw_file_num,
5847169689Skan	       line_info->dw_line_num);
584850397Sobrien    }
584950397Sobrien
585050397Sobrien  fprintf (outfile, "\n\n");
585150397Sobrien}
585250397Sobrien
585350397Sobrien/* Print the information collected for a given DIE.  */
585450397Sobrien
585550397Sobrienvoid
5856132718Skandebug_dwarf_die (dw_die_ref die)
585750397Sobrien{
585850397Sobrien  print_die (die, stderr);
585950397Sobrien}
586050397Sobrien
586150397Sobrien/* Print all DWARF information collected for the compilation unit.
586250397Sobrien   This routine is a debugging aid only.  */
586350397Sobrien
586450397Sobrienvoid
5865132718Skandebug_dwarf (void)
586650397Sobrien{
586750397Sobrien  print_indent = 0;
586850397Sobrien  print_die (comp_unit_die, stderr);
586990075Sobrien  if (! DWARF2_ASM_LINE_DEBUG_INFO)
587090075Sobrien    print_dwarf_line_table (stderr);
587150397Sobrien}
587250397Sobrien
587390075Sobrien/* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
587490075Sobrien   for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
587590075Sobrien   DIE that marks the start of the DIEs for this include file.  */
587690075Sobrien
587790075Sobrienstatic dw_die_ref
5878132718Skanpush_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
587990075Sobrien{
588090075Sobrien  const char *filename = get_AT_string (bincl_die, DW_AT_name);
588190075Sobrien  dw_die_ref new_unit = gen_compile_unit_die (filename);
588290075Sobrien
588390075Sobrien  new_unit->die_sib = old_unit;
588490075Sobrien  return new_unit;
588590075Sobrien}
588690075Sobrien
588790075Sobrien/* Close an include-file CU and reopen the enclosing one.  */
588890075Sobrien
588990075Sobrienstatic dw_die_ref
5890132718Skanpop_compile_unit (dw_die_ref old_unit)
589190075Sobrien{
589290075Sobrien  dw_die_ref new_unit = old_unit->die_sib;
589390075Sobrien
589490075Sobrien  old_unit->die_sib = NULL;
589590075Sobrien  return new_unit;
589690075Sobrien}
589790075Sobrien
589890075Sobrien#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
589990075Sobrien#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
590090075Sobrien
590190075Sobrien/* Calculate the checksum of a location expression.  */
590290075Sobrien
590390075Sobrienstatic inline void
5904132718Skanloc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
590590075Sobrien{
590690075Sobrien  CHECKSUM (loc->dw_loc_opc);
590790075Sobrien  CHECKSUM (loc->dw_loc_oprnd1);
590890075Sobrien  CHECKSUM (loc->dw_loc_oprnd2);
590990075Sobrien}
591090075Sobrien
591190075Sobrien/* Calculate the checksum of an attribute.  */
591290075Sobrien
591390075Sobrienstatic void
5914132718Skanattr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
591590075Sobrien{
591690075Sobrien  dw_loc_descr_ref loc;
591790075Sobrien  rtx r;
591890075Sobrien
591990075Sobrien  CHECKSUM (at->dw_attr);
592090075Sobrien
5921169689Skan  /* We don't care that this was compiled with a different compiler
5922169689Skan     snapshot; if the output is the same, that's what matters.  */
5923169689Skan  if (at->dw_attr == DW_AT_producer)
592490075Sobrien    return;
592590075Sobrien
592690075Sobrien  switch (AT_class (at))
592790075Sobrien    {
592890075Sobrien    case dw_val_class_const:
592990075Sobrien      CHECKSUM (at->dw_attr_val.v.val_int);
593090075Sobrien      break;
593190075Sobrien    case dw_val_class_unsigned_const:
593290075Sobrien      CHECKSUM (at->dw_attr_val.v.val_unsigned);
593390075Sobrien      break;
593490075Sobrien    case dw_val_class_long_long:
593590075Sobrien      CHECKSUM (at->dw_attr_val.v.val_long_long);
593690075Sobrien      break;
5937132718Skan    case dw_val_class_vec:
5938132718Skan      CHECKSUM (at->dw_attr_val.v.val_vec);
593990075Sobrien      break;
594090075Sobrien    case dw_val_class_flag:
594190075Sobrien      CHECKSUM (at->dw_attr_val.v.val_flag);
594290075Sobrien      break;
594390075Sobrien    case dw_val_class_str:
594490075Sobrien      CHECKSUM_STRING (AT_string (at));
594590075Sobrien      break;
594690075Sobrien
594790075Sobrien    case dw_val_class_addr:
594890075Sobrien      r = AT_addr (at);
5949169689Skan      gcc_assert (GET_CODE (r) == SYMBOL_REF);
5950169689Skan      CHECKSUM_STRING (XSTR (r, 0));
595190075Sobrien      break;
595290075Sobrien
595390075Sobrien    case dw_val_class_offset:
595490075Sobrien      CHECKSUM (at->dw_attr_val.v.val_offset);
595590075Sobrien      break;
595690075Sobrien
595790075Sobrien    case dw_val_class_loc:
595890075Sobrien      for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
595990075Sobrien	loc_checksum (loc, ctx);
596090075Sobrien      break;
596190075Sobrien
596290075Sobrien    case dw_val_class_die_ref:
5963117395Skan      die_checksum (AT_ref (at), ctx, mark);
5964117395Skan      break;
596590075Sobrien
596690075Sobrien    case dw_val_class_fde_ref:
596790075Sobrien    case dw_val_class_lbl_id:
5968169689Skan    case dw_val_class_lineptr:
5969169689Skan    case dw_val_class_macptr:
597090075Sobrien      break;
597190075Sobrien
5972169689Skan    case dw_val_class_file:
5973169689Skan      CHECKSUM_STRING (AT_file (at)->filename);
5974169689Skan      break;
5975169689Skan
597690075Sobrien    default:
597790075Sobrien      break;
597890075Sobrien    }
597990075Sobrien}
598090075Sobrien
598190075Sobrien/* Calculate the checksum of a DIE.  */
598290075Sobrien
598390075Sobrienstatic void
5984132718Skandie_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
598590075Sobrien{
598690075Sobrien  dw_die_ref c;
598790075Sobrien  dw_attr_ref a;
5988169689Skan  unsigned ix;
598990075Sobrien
5990117395Skan  /* To avoid infinite recursion.  */
5991117395Skan  if (die->die_mark)
5992117395Skan    {
5993117395Skan      CHECKSUM (die->die_mark);
5994117395Skan      return;
5995117395Skan    }
5996117395Skan  die->die_mark = ++(*mark);
5997117395Skan
599890075Sobrien  CHECKSUM (die->die_tag);
599990075Sobrien
6000169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6001117395Skan    attr_checksum (a, ctx, mark);
600290075Sobrien
6003169689Skan  FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
600490075Sobrien}
600590075Sobrien
600690075Sobrien#undef CHECKSUM
600790075Sobrien#undef CHECKSUM_STRING
600890075Sobrien
6009117395Skan/* Do the location expressions look same?  */
6010117395Skanstatic inline int
6011132718Skansame_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6012117395Skan{
6013117395Skan  return loc1->dw_loc_opc == loc2->dw_loc_opc
6014117395Skan	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6015117395Skan	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6016117395Skan}
6017117395Skan
6018117395Skan/* Do the values look the same?  */
6019117395Skanstatic int
6020132718Skansame_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6021117395Skan{
6022117395Skan  dw_loc_descr_ref loc1, loc2;
6023117395Skan  rtx r1, r2;
6024117395Skan
6025117395Skan  if (v1->val_class != v2->val_class)
6026117395Skan    return 0;
6027117395Skan
6028117395Skan  switch (v1->val_class)
6029117395Skan    {
6030117395Skan    case dw_val_class_const:
6031117395Skan      return v1->v.val_int == v2->v.val_int;
6032117395Skan    case dw_val_class_unsigned_const:
6033117395Skan      return v1->v.val_unsigned == v2->v.val_unsigned;
6034117395Skan    case dw_val_class_long_long:
6035117395Skan      return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6036132718Skan	     && v1->v.val_long_long.low == v2->v.val_long_long.low;
6037132718Skan    case dw_val_class_vec:
6038132718Skan      if (v1->v.val_vec.length != v2->v.val_vec.length
6039132718Skan	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6040117395Skan	return 0;
6041132718Skan      if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6042132718Skan		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
6043132718Skan	return 0;
6044117395Skan      return 1;
6045117395Skan    case dw_val_class_flag:
6046117395Skan      return v1->v.val_flag == v2->v.val_flag;
6047117395Skan    case dw_val_class_str:
6048132718Skan      return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6049117395Skan
6050117395Skan    case dw_val_class_addr:
6051117395Skan      r1 = v1->v.val_addr;
6052117395Skan      r2 = v2->v.val_addr;
6053117395Skan      if (GET_CODE (r1) != GET_CODE (r2))
6054117395Skan	return 0;
6055169689Skan      gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6056169689Skan      return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6057117395Skan
6058117395Skan    case dw_val_class_offset:
6059117395Skan      return v1->v.val_offset == v2->v.val_offset;
6060117395Skan
6061117395Skan    case dw_val_class_loc:
6062117395Skan      for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6063117395Skan	   loc1 && loc2;
6064117395Skan	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6065117395Skan	if (!same_loc_p (loc1, loc2, mark))
6066117395Skan	  return 0;
6067117395Skan      return !loc1 && !loc2;
6068117395Skan
6069117395Skan    case dw_val_class_die_ref:
6070117395Skan      return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6071117395Skan
6072117395Skan    case dw_val_class_fde_ref:
6073117395Skan    case dw_val_class_lbl_id:
6074169689Skan    case dw_val_class_lineptr:
6075169689Skan    case dw_val_class_macptr:
6076117395Skan      return 1;
6077117395Skan
6078169689Skan    case dw_val_class_file:
6079169689Skan      return v1->v.val_file == v2->v.val_file;
6080169689Skan
6081117395Skan    default:
6082117395Skan      return 1;
6083117395Skan    }
6084117395Skan}
6085117395Skan
6086117395Skan/* Do the attributes look the same?  */
6087117395Skan
6088117395Skanstatic int
6089132718Skansame_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6090117395Skan{
6091117395Skan  if (at1->dw_attr != at2->dw_attr)
6092117395Skan    return 0;
6093117395Skan
6094169689Skan  /* We don't care that this was compiled with a different compiler
6095169689Skan     snapshot; if the output is the same, that's what matters. */
6096169689Skan  if (at1->dw_attr == DW_AT_producer)
6097117395Skan    return 1;
6098117395Skan
6099117395Skan  return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6100117395Skan}
6101117395Skan
6102117395Skan/* Do the dies look the same?  */
6103117395Skan
6104117395Skanstatic int
6105132718Skansame_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6106117395Skan{
6107117395Skan  dw_die_ref c1, c2;
6108169689Skan  dw_attr_ref a1;
6109169689Skan  unsigned ix;
6110117395Skan
6111117395Skan  /* To avoid infinite recursion.  */
6112117395Skan  if (die1->die_mark)
6113117395Skan    return die1->die_mark == die2->die_mark;
6114117395Skan  die1->die_mark = die2->die_mark = ++(*mark);
6115117395Skan
6116117395Skan  if (die1->die_tag != die2->die_tag)
6117117395Skan    return 0;
6118117395Skan
6119169689Skan  if (VEC_length (dw_attr_node, die1->die_attr)
6120169689Skan      != VEC_length (dw_attr_node, die2->die_attr))
6121117395Skan    return 0;
6122169689Skan
6123169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6124169689Skan    if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6125117395Skan      return 0;
6126117395Skan
6127169689Skan  c1 = die1->die_child;
6128169689Skan  c2 = die2->die_child;
6129169689Skan  if (! c1)
6130169689Skan    {
6131169689Skan      if (c2)
6132169689Skan	return 0;
6133169689Skan    }
6134169689Skan  else
6135169689Skan    for (;;)
6136169689Skan      {
6137169689Skan	if (!same_die_p (c1, c2, mark))
6138169689Skan	  return 0;
6139169689Skan	c1 = c1->die_sib;
6140169689Skan	c2 = c2->die_sib;
6141169689Skan	if (c1 == die1->die_child)
6142169689Skan	  {
6143169689Skan	    if (c2 == die2->die_child)
6144169689Skan	      break;
6145169689Skan	    else
6146169689Skan	      return 0;
6147169689Skan	  }
6148169689Skan    }
6149169689Skan
6150117395Skan  return 1;
6151117395Skan}
6152117395Skan
6153117395Skan/* Do the dies look the same?  Wrapper around same_die_p.  */
6154117395Skan
6155117395Skanstatic int
6156132718Skansame_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6157117395Skan{
6158117395Skan  int mark = 0;
6159117395Skan  int ret = same_die_p (die1, die2, &mark);
6160117395Skan
6161117395Skan  unmark_all_dies (die1);
6162117395Skan  unmark_all_dies (die2);
6163117395Skan
6164117395Skan  return ret;
6165117395Skan}
6166117395Skan
616790075Sobrien/* The prefix to attach to symbols on DIEs in the current comdat debug
616890075Sobrien   info section.  */
616990075Sobrienstatic char *comdat_symbol_id;
617090075Sobrien
617190075Sobrien/* The index of the current symbol within the current comdat CU.  */
617290075Sobrienstatic unsigned int comdat_symbol_number;
617390075Sobrien
617490075Sobrien/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
617590075Sobrien   children, and set comdat_symbol_id accordingly.  */
617690075Sobrien
617790075Sobrienstatic void
6178132718Skancompute_section_prefix (dw_die_ref unit_die)
617990075Sobrien{
6180117395Skan  const char *die_name = get_AT_string (unit_die, DW_AT_name);
6181117395Skan  const char *base = die_name ? lbasename (die_name) : "anonymous";
6182132718Skan  char *name = alloca (strlen (base) + 64);
618390075Sobrien  char *p;
6184117395Skan  int i, mark;
618590075Sobrien  unsigned char checksum[16];
618690075Sobrien  struct md5_ctx ctx;
618790075Sobrien
618890075Sobrien  /* Compute the checksum of the DIE, then append part of it as hex digits to
618990075Sobrien     the name filename of the unit.  */
619090075Sobrien
619190075Sobrien  md5_init_ctx (&ctx);
6192117395Skan  mark = 0;
6193117395Skan  die_checksum (unit_die, &ctx, &mark);
6194117395Skan  unmark_all_dies (unit_die);
619590075Sobrien  md5_finish_ctx (&ctx, checksum);
619690075Sobrien
619790075Sobrien  sprintf (name, "%s.", base);
619890075Sobrien  clean_symbol_name (name);
619990075Sobrien
620090075Sobrien  p = name + strlen (name);
620190075Sobrien  for (i = 0; i < 4; i++)
620290075Sobrien    {
620390075Sobrien      sprintf (p, "%.2x", checksum[i]);
620490075Sobrien      p += 2;
620590075Sobrien    }
620690075Sobrien
620790075Sobrien  comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
620890075Sobrien  comdat_symbol_number = 0;
620990075Sobrien}
621090075Sobrien
621190075Sobrien/* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
621290075Sobrien
621390075Sobrienstatic int
6214132718Skanis_type_die (dw_die_ref die)
621590075Sobrien{
621690075Sobrien  switch (die->die_tag)
621790075Sobrien    {
621890075Sobrien    case DW_TAG_array_type:
621990075Sobrien    case DW_TAG_class_type:
622090075Sobrien    case DW_TAG_enumeration_type:
622190075Sobrien    case DW_TAG_pointer_type:
622290075Sobrien    case DW_TAG_reference_type:
622390075Sobrien    case DW_TAG_string_type:
622490075Sobrien    case DW_TAG_structure_type:
622590075Sobrien    case DW_TAG_subroutine_type:
622690075Sobrien    case DW_TAG_union_type:
622790075Sobrien    case DW_TAG_ptr_to_member_type:
622890075Sobrien    case DW_TAG_set_type:
622990075Sobrien    case DW_TAG_subrange_type:
623090075Sobrien    case DW_TAG_base_type:
623190075Sobrien    case DW_TAG_const_type:
623290075Sobrien    case DW_TAG_file_type:
623390075Sobrien    case DW_TAG_packed_type:
623490075Sobrien    case DW_TAG_volatile_type:
6235117395Skan    case DW_TAG_typedef:
623690075Sobrien      return 1;
623790075Sobrien    default:
623890075Sobrien      return 0;
623990075Sobrien    }
624090075Sobrien}
624190075Sobrien
624290075Sobrien/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
624390075Sobrien   Basically, we want to choose the bits that are likely to be shared between
624490075Sobrien   compilations (types) and leave out the bits that are specific to individual
624590075Sobrien   compilations (functions).  */
624690075Sobrien
624790075Sobrienstatic int
6248132718Skanis_comdat_die (dw_die_ref c)
624990075Sobrien{
625090075Sobrien  /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
625190075Sobrien     we do for stabs.  The advantage is a greater likelihood of sharing between
625290075Sobrien     objects that don't include headers in the same order (and therefore would
625390075Sobrien     put the base types in a different comdat).  jason 8/28/00 */
625490075Sobrien
625590075Sobrien  if (c->die_tag == DW_TAG_base_type)
625690075Sobrien    return 0;
625790075Sobrien
625890075Sobrien  if (c->die_tag == DW_TAG_pointer_type
625990075Sobrien      || c->die_tag == DW_TAG_reference_type
626090075Sobrien      || c->die_tag == DW_TAG_const_type
626190075Sobrien      || c->die_tag == DW_TAG_volatile_type)
626290075Sobrien    {
626390075Sobrien      dw_die_ref t = get_AT_ref (c, DW_AT_type);
626490075Sobrien
626590075Sobrien      return t ? is_comdat_die (t) : 0;
626690075Sobrien    }
626790075Sobrien
626890075Sobrien  return is_type_die (c);
626990075Sobrien}
627090075Sobrien
627190075Sobrien/* Returns 1 iff C is the sort of DIE that might be referred to from another
627290075Sobrien   compilation unit.  */
627390075Sobrien
627490075Sobrienstatic int
6275132718Skanis_symbol_die (dw_die_ref c)
627690075Sobrien{
627790075Sobrien  return (is_type_die (c)
6278117395Skan	  || (get_AT (c, DW_AT_declaration)
6279169689Skan	      && !get_AT (c, DW_AT_specification))
6280169689Skan	  || c->die_tag == DW_TAG_namespace);
628190075Sobrien}
628290075Sobrien
628390075Sobrienstatic char *
6284132718Skangen_internal_sym (const char *prefix)
628590075Sobrien{
628690075Sobrien  char buf[256];
628790075Sobrien
628890075Sobrien  ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
628990075Sobrien  return xstrdup (buf);
629090075Sobrien}
629190075Sobrien
629290075Sobrien/* Assign symbols to all worthy DIEs under DIE.  */
629390075Sobrien
629490075Sobrienstatic void
6295132718Skanassign_symbol_names (dw_die_ref die)
629690075Sobrien{
629790075Sobrien  dw_die_ref c;
629890075Sobrien
629990075Sobrien  if (is_symbol_die (die))
630090075Sobrien    {
630190075Sobrien      if (comdat_symbol_id)
630290075Sobrien	{
630390075Sobrien	  char *p = alloca (strlen (comdat_symbol_id) + 64);
630490075Sobrien
630590075Sobrien	  sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
630690075Sobrien		   comdat_symbol_id, comdat_symbol_number++);
630790075Sobrien	  die->die_symbol = xstrdup (p);
630890075Sobrien	}
630990075Sobrien      else
631090075Sobrien	die->die_symbol = gen_internal_sym ("LDIE");
631190075Sobrien    }
631290075Sobrien
6313169689Skan  FOR_EACH_CHILD (die, c, assign_symbol_names (c));
631490075Sobrien}
631590075Sobrien
6316117395Skanstruct cu_hash_table_entry
6317117395Skan{
6318117395Skan  dw_die_ref cu;
6319117395Skan  unsigned min_comdat_num, max_comdat_num;
6320117395Skan  struct cu_hash_table_entry *next;
6321117395Skan};
6322117395Skan
6323117395Skan/* Routines to manipulate hash table of CUs.  */
6324117395Skanstatic hashval_t
6325132718Skanhtab_cu_hash (const void *of)
6326117395Skan{
6327117395Skan  const struct cu_hash_table_entry *entry = of;
6328117395Skan
6329117395Skan  return htab_hash_string (entry->cu->die_symbol);
6330117395Skan}
6331117395Skan
6332117395Skanstatic int
6333132718Skanhtab_cu_eq (const void *of1, const void *of2)
6334117395Skan{
6335117395Skan  const struct cu_hash_table_entry *entry1 = of1;
6336117395Skan  const struct die_struct *entry2 = of2;
6337117395Skan
6338117395Skan  return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6339117395Skan}
6340117395Skan
6341117395Skanstatic void
6342132718Skanhtab_cu_del (void *what)
6343117395Skan{
6344117395Skan  struct cu_hash_table_entry *next, *entry = what;
6345117395Skan
6346117395Skan  while (entry)
6347117395Skan    {
6348117395Skan      next = entry->next;
6349117395Skan      free (entry);
6350117395Skan      entry = next;
6351117395Skan    }
6352117395Skan}
6353117395Skan
6354117395Skan/* Check whether we have already seen this CU and set up SYM_NUM
6355117395Skan   accordingly.  */
6356117395Skanstatic int
6357132718Skancheck_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6358117395Skan{
6359117395Skan  struct cu_hash_table_entry dummy;
6360117395Skan  struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6361117395Skan
6362117395Skan  dummy.max_comdat_num = 0;
6363117395Skan
6364117395Skan  slot = (struct cu_hash_table_entry **)
6365117395Skan    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6366117395Skan	INSERT);
6367117395Skan  entry = *slot;
6368117395Skan
6369117395Skan  for (; entry; last = entry, entry = entry->next)
6370117395Skan    {
6371117395Skan      if (same_die_p_wrap (cu, entry->cu))
6372117395Skan	break;
6373117395Skan    }
6374117395Skan
6375117395Skan  if (entry)
6376117395Skan    {
6377117395Skan      *sym_num = entry->min_comdat_num;
6378117395Skan      return 1;
6379117395Skan    }
6380117395Skan
6381169689Skan  entry = XCNEW (struct cu_hash_table_entry);
6382117395Skan  entry->cu = cu;
6383117395Skan  entry->min_comdat_num = *sym_num = last->max_comdat_num;
6384117395Skan  entry->next = *slot;
6385117395Skan  *slot = entry;
6386117395Skan
6387117395Skan  return 0;
6388117395Skan}
6389117395Skan
6390117395Skan/* Record SYM_NUM to record of CU in HTABLE.  */
6391117395Skanstatic void
6392132718Skanrecord_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6393117395Skan{
6394117395Skan  struct cu_hash_table_entry **slot, *entry;
6395117395Skan
6396117395Skan  slot = (struct cu_hash_table_entry **)
6397117395Skan    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6398117395Skan	NO_INSERT);
6399117395Skan  entry = *slot;
6400117395Skan
6401117395Skan  entry->max_comdat_num = sym_num;
6402117395Skan}
6403117395Skan
640490075Sobrien/* Traverse the DIE (which is always comp_unit_die), and set up
640590075Sobrien   additional compilation units for each of the include files we see
640690075Sobrien   bracketed by BINCL/EINCL.  */
640790075Sobrien
640890075Sobrienstatic void
6409132718Skanbreak_out_includes (dw_die_ref die)
641090075Sobrien{
6411169689Skan  dw_die_ref c;
641290075Sobrien  dw_die_ref unit = NULL;
6413117395Skan  limbo_die_node *node, **pnode;
6414117395Skan  htab_t cu_hash_table;
641590075Sobrien
6416169689Skan  c = die->die_child;
6417169689Skan  if (c) do {
6418169689Skan    dw_die_ref prev = c;
6419169689Skan    c = c->die_sib;
6420169689Skan    while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6421169689Skan	   || (unit && is_comdat_die (c)))
6422169689Skan      {
6423169689Skan	dw_die_ref next = c->die_sib;
642490075Sobrien
6425169689Skan	/* This DIE is for a secondary CU; remove it from the main one.  */
6426169689Skan	remove_child_with_prev (c, prev);
6427169689Skan
6428169689Skan	if (c->die_tag == DW_TAG_GNU_BINCL)
6429169689Skan	  unit = push_new_compile_unit (unit, c);
6430169689Skan	else if (c->die_tag == DW_TAG_GNU_EINCL)
6431169689Skan	  unit = pop_compile_unit (unit);
6432169689Skan	else
6433169689Skan	  add_child_die (unit, c);
6434169689Skan	c = next;
6435169689Skan	if (c == die->die_child)
6436169689Skan	  break;
6437169689Skan      }
6438169689Skan  } while (c != die->die_child);
643990075Sobrien
644090075Sobrien#if 0
644190075Sobrien  /* We can only use this in debugging, since the frontend doesn't check
644290075Sobrien     to make sure that we leave every include file we enter.  */
6443169689Skan  gcc_assert (!unit);
644490075Sobrien#endif
644590075Sobrien
644690075Sobrien  assign_symbol_names (die);
6447117395Skan  cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6448117395Skan  for (node = limbo_die_list, pnode = &limbo_die_list;
6449117395Skan       node;
6450117395Skan       node = node->next)
645190075Sobrien    {
6452117395Skan      int is_dupl;
6453117395Skan
645490075Sobrien      compute_section_prefix (node->die);
6455117395Skan      is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6456117395Skan			&comdat_symbol_number);
645790075Sobrien      assign_symbol_names (node->die);
6458117395Skan      if (is_dupl)
6459117395Skan	*pnode = node->next;
6460117395Skan      else
6461132718Skan	{
6462117395Skan	  pnode = &node->next;
6463117395Skan	  record_comdat_symbol_number (node->die, cu_hash_table,
6464117395Skan		comdat_symbol_number);
6465117395Skan	}
646690075Sobrien    }
6467117395Skan  htab_delete (cu_hash_table);
646890075Sobrien}
646990075Sobrien
647090075Sobrien/* Traverse the DIE and add a sibling attribute if it may have the
647190075Sobrien   effect of speeding up access to siblings.  To save some space,
647290075Sobrien   avoid generating sibling attributes for DIE's without children.  */
647390075Sobrien
647490075Sobrienstatic void
6475132718Skanadd_sibling_attributes (dw_die_ref die)
647690075Sobrien{
647790075Sobrien  dw_die_ref c;
647890075Sobrien
6479169689Skan  if (! die->die_child)
6480169689Skan    return;
6481169689Skan
6482169689Skan  if (die->die_parent && die != die->die_parent->die_child)
648390075Sobrien    add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
648490075Sobrien
6485169689Skan  FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
648650397Sobrien}
648750397Sobrien
648890075Sobrien/* Output all location lists for the DIE and its children.  */
648950397Sobrien
649050397Sobrienstatic void
6491132718Skanoutput_location_lists (dw_die_ref die)
649290075Sobrien{
649390075Sobrien  dw_die_ref c;
6494169689Skan  dw_attr_ref a;
6495169689Skan  unsigned ix;
649690075Sobrien
6497169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6498169689Skan    if (AT_class (a) == dw_val_class_loc_list)
6499169689Skan      output_loc_list (AT_loc_list (a));
650090075Sobrien
6501169689Skan  FOR_EACH_CHILD (die, c, output_location_lists (c));
650290075Sobrien}
6503117395Skan
650490075Sobrien/* The format of each DIE (and its attribute value pairs) is encoded in an
650590075Sobrien   abbreviation table.  This routine builds the abbreviation table and assigns
650690075Sobrien   a unique abbreviation id for each abbreviation entry.  The children of each
650790075Sobrien   die are visited recursively.  */
650890075Sobrien
650990075Sobrienstatic void
6510132718Skanbuild_abbrev_table (dw_die_ref die)
651150397Sobrien{
651290075Sobrien  unsigned long abbrev_id;
651390075Sobrien  unsigned int n_alloc;
651490075Sobrien  dw_die_ref c;
6515169689Skan  dw_attr_ref a;
6516169689Skan  unsigned ix;
651790075Sobrien
651890075Sobrien  /* Scan the DIE references, and mark as external any that refer to
651990075Sobrien     DIEs from other CUs (i.e. those which are not marked).  */
6520169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6521169689Skan    if (AT_class (a) == dw_val_class_die_ref
6522169689Skan	&& AT_ref (a)->die_mark == 0)
652390075Sobrien      {
6524169689Skan	gcc_assert (AT_ref (a)->die_symbol);
652590075Sobrien
6526169689Skan	set_AT_ref_external (a, 1);
652790075Sobrien      }
652890075Sobrien
652950397Sobrien  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
653050397Sobrien    {
653190075Sobrien      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6532169689Skan      dw_attr_ref die_a, abbrev_a;
6533169689Skan      unsigned ix;
6534169689Skan      bool ok = true;
6535169689Skan
6536169689Skan      if (abbrev->die_tag != die->die_tag)
6537169689Skan	continue;
6538169689Skan      if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6539169689Skan	continue;
6540169689Skan
6541169689Skan      if (VEC_length (dw_attr_node, abbrev->die_attr)
6542169689Skan	  != VEC_length (dw_attr_node, die->die_attr))
6543169689Skan	continue;
6544169689Skan
6545169689Skan      for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
654650397Sobrien	{
6547169689Skan	  abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6548169689Skan	  if ((abbrev_a->dw_attr != die_a->dw_attr)
6549169689Skan	      || (value_format (abbrev_a) != value_format (die_a)))
655050397Sobrien	    {
6551169689Skan	      ok = false;
6552169689Skan	      break;
655350397Sobrien	    }
655450397Sobrien	}
6555169689Skan      if (ok)
6556169689Skan	break;
655750397Sobrien    }
655850397Sobrien
655950397Sobrien  if (abbrev_id >= abbrev_die_table_in_use)
656050397Sobrien    {
656150397Sobrien      if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
656250397Sobrien	{
656350397Sobrien	  n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6564132718Skan	  abbrev_die_table = ggc_realloc (abbrev_die_table,
6565132718Skan					  sizeof (dw_die_ref) * n_alloc);
656650397Sobrien
6567132718Skan	  memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
656850397Sobrien		 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
656950397Sobrien	  abbrev_die_table_allocated = n_alloc;
657050397Sobrien	}
657150397Sobrien
657250397Sobrien      ++abbrev_die_table_in_use;
657350397Sobrien      abbrev_die_table[abbrev_id] = die;
657450397Sobrien    }
657550397Sobrien
657650397Sobrien  die->die_abbrev = abbrev_id;
6577169689Skan  FOR_EACH_CHILD (die, c, build_abbrev_table (c));
657850397Sobrien}
657950397Sobrien
658050397Sobrien/* Return the power-of-two number of bytes necessary to represent VALUE.  */
658150397Sobrien
658250397Sobrienstatic int
6583132718Skanconstant_size (long unsigned int value)
658450397Sobrien{
658550397Sobrien  int log;
658650397Sobrien
658750397Sobrien  if (value == 0)
658850397Sobrien    log = 0;
658950397Sobrien  else
659050397Sobrien    log = floor_log2 (value);
659150397Sobrien
659250397Sobrien  log = log / 8;
659350397Sobrien  log = 1 << (floor_log2 (log) + 1);
659450397Sobrien
659550397Sobrien  return log;
659650397Sobrien}
659750397Sobrien
659890075Sobrien/* Return the size of a DIE as it is represented in the
659950397Sobrien   .debug_info section.  */
660050397Sobrien
660150397Sobrienstatic unsigned long
6602132718Skansize_of_die (dw_die_ref die)
660350397Sobrien{
660490075Sobrien  unsigned long size = 0;
660590075Sobrien  dw_attr_ref a;
6606169689Skan  unsigned ix;
660750397Sobrien
660850397Sobrien  size += size_of_uleb128 (die->die_abbrev);
6609169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
661050397Sobrien    {
661190075Sobrien      switch (AT_class (a))
661250397Sobrien	{
661350397Sobrien	case dw_val_class_addr:
661490075Sobrien	  size += DWARF2_ADDR_SIZE;
661550397Sobrien	  break;
661690075Sobrien	case dw_val_class_offset:
661790075Sobrien	  size += DWARF_OFFSET_SIZE;
661890075Sobrien	  break;
661950397Sobrien	case dw_val_class_loc:
662050397Sobrien	  {
662190075Sobrien	    unsigned long lsize = size_of_locs (AT_loc (a));
662250397Sobrien
662350397Sobrien	    /* Block length.  */
662450397Sobrien	    size += constant_size (lsize);
662550397Sobrien	    size += lsize;
662650397Sobrien	  }
662750397Sobrien	  break;
662890075Sobrien	case dw_val_class_loc_list:
662990075Sobrien	  size += DWARF_OFFSET_SIZE;
663090075Sobrien	  break;
663190075Sobrien	case dw_val_class_range_list:
663290075Sobrien	  size += DWARF_OFFSET_SIZE;
663390075Sobrien	  break;
663450397Sobrien	case dw_val_class_const:
663590075Sobrien	  size += size_of_sleb128 (AT_int (a));
663650397Sobrien	  break;
663750397Sobrien	case dw_val_class_unsigned_const:
663890075Sobrien	  size += constant_size (AT_unsigned (a));
663950397Sobrien	  break;
664050397Sobrien	case dw_val_class_long_long:
664190075Sobrien	  size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
664250397Sobrien	  break;
6643132718Skan	case dw_val_class_vec:
6644132718Skan	  size += 1 + (a->dw_attr_val.v.val_vec.length
6645132718Skan		       * a->dw_attr_val.v.val_vec.elt_size); /* block */
664650397Sobrien	  break;
664750397Sobrien	case dw_val_class_flag:
664850397Sobrien	  size += 1;
664950397Sobrien	  break;
665050397Sobrien	case dw_val_class_die_ref:
6651132718Skan	  if (AT_ref_external (a))
6652132718Skan	    size += DWARF2_ADDR_SIZE;
6653132718Skan	  else
6654132718Skan	    size += DWARF_OFFSET_SIZE;
665550397Sobrien	  break;
665650397Sobrien	case dw_val_class_fde_ref:
665750397Sobrien	  size += DWARF_OFFSET_SIZE;
665850397Sobrien	  break;
665950397Sobrien	case dw_val_class_lbl_id:
666090075Sobrien	  size += DWARF2_ADDR_SIZE;
666150397Sobrien	  break;
6662169689Skan	case dw_val_class_lineptr:
6663169689Skan	case dw_val_class_macptr:
666450397Sobrien	  size += DWARF_OFFSET_SIZE;
666550397Sobrien	  break;
666650397Sobrien	case dw_val_class_str:
666790075Sobrien	  if (AT_string_form (a) == DW_FORM_strp)
666890075Sobrien	    size += DWARF_OFFSET_SIZE;
666990075Sobrien	  else
6670132718Skan	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
667150397Sobrien	  break;
6672169689Skan	case dw_val_class_file:
6673169689Skan	  size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6674169689Skan	  break;
667550397Sobrien	default:
6676169689Skan	  gcc_unreachable ();
667750397Sobrien	}
667850397Sobrien    }
667950397Sobrien
668050397Sobrien  return size;
668150397Sobrien}
668250397Sobrien
668390075Sobrien/* Size the debugging information associated with a given DIE.  Visits the
668490075Sobrien   DIE's children recursively.  Updates the global variable next_die_offset, on
668590075Sobrien   each time through.  Uses the current value of next_die_offset to update the
668690075Sobrien   die_offset field in each DIE.  */
668750397Sobrien
668850397Sobrienstatic void
6689132718Skancalc_die_sizes (dw_die_ref die)
669050397Sobrien{
669190075Sobrien  dw_die_ref c;
669290075Sobrien
669350397Sobrien  die->die_offset = next_die_offset;
669450397Sobrien  next_die_offset += size_of_die (die);
669550397Sobrien
6696169689Skan  FOR_EACH_CHILD (die, c, calc_die_sizes (c));
669750397Sobrien
669850397Sobrien  if (die->die_child != NULL)
669950397Sobrien    /* Count the null byte used to terminate sibling lists.  */
670050397Sobrien    next_die_offset += 1;
670150397Sobrien}
670250397Sobrien
670390075Sobrien/* Set the marks for a die and its children.  We do this so
670490075Sobrien   that we know whether or not a reference needs to use FORM_ref_addr; only
670590075Sobrien   DIEs in the same CU will be marked.  We used to clear out the offset
670690075Sobrien   and use that as the flag, but ran into ordering problems.  */
670750397Sobrien
670890075Sobrienstatic void
6709132718Skanmark_dies (dw_die_ref die)
671050397Sobrien{
671190075Sobrien  dw_die_ref c;
671250397Sobrien
6713169689Skan  gcc_assert (!die->die_mark);
6714132718Skan
671590075Sobrien  die->die_mark = 1;
6716169689Skan  FOR_EACH_CHILD (die, c, mark_dies (c));
671750397Sobrien}
671850397Sobrien
671990075Sobrien/* Clear the marks for a die and its children.  */
672050397Sobrien
672190075Sobrienstatic void
6722132718Skanunmark_dies (dw_die_ref die)
672350397Sobrien{
672490075Sobrien  dw_die_ref c;
672550397Sobrien
6726169689Skan  gcc_assert (die->die_mark);
6727132718Skan
672890075Sobrien  die->die_mark = 0;
6729169689Skan  FOR_EACH_CHILD (die, c, unmark_dies (c));
673050397Sobrien}
673150397Sobrien
6732117395Skan/* Clear the marks for a die, its children and referred dies.  */
6733117395Skan
6734117395Skanstatic void
6735132718Skanunmark_all_dies (dw_die_ref die)
6736117395Skan{
6737117395Skan  dw_die_ref c;
6738117395Skan  dw_attr_ref a;
6739169689Skan  unsigned ix;
6740117395Skan
6741117395Skan  if (!die->die_mark)
6742117395Skan    return;
6743117395Skan  die->die_mark = 0;
6744117395Skan
6745169689Skan  FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6746117395Skan
6747169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6748117395Skan    if (AT_class (a) == dw_val_class_die_ref)
6749117395Skan      unmark_all_dies (AT_ref (a));
6750117395Skan}
6751117395Skan
675250397Sobrien/* Return the size of the .debug_pubnames table  generated for the
675350397Sobrien   compilation unit.  */
675450397Sobrien
675550397Sobrienstatic unsigned long
6756132718Skansize_of_pubnames (void)
675750397Sobrien{
675890075Sobrien  unsigned long size;
675990075Sobrien  unsigned i;
676050397Sobrien
676150397Sobrien  size = DWARF_PUBNAMES_HEADER_SIZE;
676290075Sobrien  for (i = 0; i < pubname_table_in_use; i++)
676350397Sobrien    {
676490075Sobrien      pubname_ref p = &pubname_table[i];
676590075Sobrien      size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
676650397Sobrien    }
676750397Sobrien
676850397Sobrien  size += DWARF_OFFSET_SIZE;
676950397Sobrien  return size;
677050397Sobrien}
677150397Sobrien
677250397Sobrien/* Return the size of the information in the .debug_aranges section.  */
677350397Sobrien
677450397Sobrienstatic unsigned long
6775132718Skansize_of_aranges (void)
677650397Sobrien{
677790075Sobrien  unsigned long size;
677850397Sobrien
677950397Sobrien  size = DWARF_ARANGES_HEADER_SIZE;
678050397Sobrien
678150397Sobrien  /* Count the address/length pair for this compilation unit.  */
678290075Sobrien  size += 2 * DWARF2_ADDR_SIZE;
678390075Sobrien  size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
678450397Sobrien
678550397Sobrien  /* Count the two zero words used to terminated the address range table.  */
678690075Sobrien  size += 2 * DWARF2_ADDR_SIZE;
678750397Sobrien  return size;
678850397Sobrien}
678950397Sobrien
679050397Sobrien/* Select the encoding of an attribute value.  */
679150397Sobrien
679250397Sobrienstatic enum dwarf_form
6793132718Skanvalue_format (dw_attr_ref a)
679450397Sobrien{
679590075Sobrien  switch (a->dw_attr_val.val_class)
679650397Sobrien    {
679750397Sobrien    case dw_val_class_addr:
679850397Sobrien      return DW_FORM_addr;
679990075Sobrien    case dw_val_class_range_list:
680090075Sobrien    case dw_val_class_offset:
680190075Sobrien    case dw_val_class_loc_list:
6802169689Skan      switch (DWARF_OFFSET_SIZE)
6803169689Skan	{
6804169689Skan	case 4:
6805169689Skan	  return DW_FORM_data4;
6806169689Skan	case 8:
6807169689Skan	  return DW_FORM_data8;
6808169689Skan	default:
6809169689Skan	  gcc_unreachable ();
6810169689Skan	}
681150397Sobrien    case dw_val_class_loc:
681290075Sobrien      switch (constant_size (size_of_locs (AT_loc (a))))
681350397Sobrien	{
681450397Sobrien	case 1:
681550397Sobrien	  return DW_FORM_block1;
681650397Sobrien	case 2:
681750397Sobrien	  return DW_FORM_block2;
681850397Sobrien	default:
6819169689Skan	  gcc_unreachable ();
682050397Sobrien	}
682150397Sobrien    case dw_val_class_const:
682290075Sobrien      return DW_FORM_sdata;
682350397Sobrien    case dw_val_class_unsigned_const:
682490075Sobrien      switch (constant_size (AT_unsigned (a)))
682550397Sobrien	{
682650397Sobrien	case 1:
682750397Sobrien	  return DW_FORM_data1;
682850397Sobrien	case 2:
682950397Sobrien	  return DW_FORM_data2;
683050397Sobrien	case 4:
683150397Sobrien	  return DW_FORM_data4;
683250397Sobrien	case 8:
683350397Sobrien	  return DW_FORM_data8;
683450397Sobrien	default:
6835169689Skan	  gcc_unreachable ();
683650397Sobrien	}
683750397Sobrien    case dw_val_class_long_long:
683850397Sobrien      return DW_FORM_block1;
6839132718Skan    case dw_val_class_vec:
684050397Sobrien      return DW_FORM_block1;
684150397Sobrien    case dw_val_class_flag:
684250397Sobrien      return DW_FORM_flag;
684350397Sobrien    case dw_val_class_die_ref:
684490075Sobrien      if (AT_ref_external (a))
684590075Sobrien	return DW_FORM_ref_addr;
684690075Sobrien      else
684790075Sobrien	return DW_FORM_ref;
684850397Sobrien    case dw_val_class_fde_ref:
684950397Sobrien      return DW_FORM_data;
685050397Sobrien    case dw_val_class_lbl_id:
685150397Sobrien      return DW_FORM_addr;
6852169689Skan    case dw_val_class_lineptr:
6853169689Skan    case dw_val_class_macptr:
685450397Sobrien      return DW_FORM_data;
685550397Sobrien    case dw_val_class_str:
685690075Sobrien      return AT_string_form (a);
6857169689Skan    case dw_val_class_file:
6858169689Skan      switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6859169689Skan	{
6860169689Skan	case 1:
6861169689Skan	  return DW_FORM_data1;
6862169689Skan	case 2:
6863169689Skan	  return DW_FORM_data2;
6864169689Skan	case 4:
6865169689Skan	  return DW_FORM_data4;
6866169689Skan	default:
6867169689Skan	  gcc_unreachable ();
6868169689Skan	}
686990075Sobrien
687050397Sobrien    default:
6871169689Skan      gcc_unreachable ();
687250397Sobrien    }
687350397Sobrien}
687450397Sobrien
687550397Sobrien/* Output the encoding of an attribute value.  */
687650397Sobrien
687750397Sobrienstatic void
6878132718Skanoutput_value_format (dw_attr_ref a)
687950397Sobrien{
688090075Sobrien  enum dwarf_form form = value_format (a);
688150397Sobrien
688290075Sobrien  dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
688350397Sobrien}
688450397Sobrien
688550397Sobrien/* Output the .debug_abbrev section which defines the DIE abbreviation
688650397Sobrien   table.  */
688750397Sobrien
688850397Sobrienstatic void
6889132718Skanoutput_abbrev_section (void)
689050397Sobrien{
689150397Sobrien  unsigned long abbrev_id;
689250397Sobrien
689350397Sobrien  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
689450397Sobrien    {
689590075Sobrien      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6896169689Skan      unsigned ix;
6897169689Skan      dw_attr_ref a_attr;
689850397Sobrien
689990075Sobrien      dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
690090075Sobrien      dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
690190075Sobrien				   dwarf_tag_name (abbrev->die_tag));
690250397Sobrien
690390075Sobrien      if (abbrev->die_child != NULL)
690490075Sobrien	dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
690590075Sobrien      else
690690075Sobrien	dw2_asm_output_data (1, DW_children_no, "DW_children_no");
690750397Sobrien
6908169689Skan      for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6909169689Skan	   ix++)
691050397Sobrien	{
691190075Sobrien	  dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
691290075Sobrien				       dwarf_attr_name (a_attr->dw_attr));
691390075Sobrien	  output_value_format (a_attr);
691450397Sobrien	}
691550397Sobrien
691690075Sobrien      dw2_asm_output_data (1, 0, NULL);
691790075Sobrien      dw2_asm_output_data (1, 0, NULL);
691850397Sobrien    }
691952284Sobrien
692090075Sobrien  /* Terminate the table.  */
692190075Sobrien  dw2_asm_output_data (1, 0, NULL);
692250397Sobrien}
692350397Sobrien
692490075Sobrien/* Output a symbol we can use to refer to this DIE from another CU.  */
692550397Sobrien
692690075Sobrienstatic inline void
6927132718Skanoutput_die_symbol (dw_die_ref die)
692890075Sobrien{
692990075Sobrien  char *sym = die->die_symbol;
693090075Sobrien
693190075Sobrien  if (sym == 0)
693290075Sobrien    return;
693390075Sobrien
693490075Sobrien  if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
693590075Sobrien    /* We make these global, not weak; if the target doesn't support
693690075Sobrien       .linkonce, it doesn't support combining the sections, so debugging
693790075Sobrien       will break.  */
6938169689Skan    targetm.asm_out.globalize_label (asm_out_file, sym);
693990075Sobrien
694090075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, sym);
694190075Sobrien}
694290075Sobrien
694390075Sobrien/* Return a new location list, given the begin and end range, and the
694490075Sobrien   expression. gensym tells us whether to generate a new internal symbol for
694590075Sobrien   this location list node, which is done for the head of the list only.  */
694690075Sobrien
694790075Sobrienstatic inline dw_loc_list_ref
6948132718Skannew_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6949132718Skan	      const char *section, unsigned int gensym)
695090075Sobrien{
6951132718Skan  dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
695290075Sobrien
695390075Sobrien  retlist->begin = begin;
695490075Sobrien  retlist->end = end;
695590075Sobrien  retlist->expr = expr;
695690075Sobrien  retlist->section = section;
6957117395Skan  if (gensym)
695890075Sobrien    retlist->ll_symbol = gen_internal_sym ("LLST");
695990075Sobrien
696090075Sobrien  return retlist;
696190075Sobrien}
696290075Sobrien
6963132718Skan/* Add a location description expression to a location list.  */
696490075Sobrien
696590075Sobrienstatic inline void
6966132718Skanadd_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6967132718Skan			   const char *begin, const char *end,
6968132718Skan			   const char *section)
696990075Sobrien{
697090075Sobrien  dw_loc_list_ref *d;
6971117395Skan
697290075Sobrien  /* Find the end of the chain.  */
697390075Sobrien  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
697490075Sobrien    ;
697590075Sobrien
6976132718Skan  /* Add a new location list node to the list.  */
697790075Sobrien  *d = new_loc_list (descr, begin, end, section, 0);
697890075Sobrien}
697990075Sobrien
6980169689Skanstatic void
6981169689Skandwarf2out_switch_text_section (void)
6982169689Skan{
6983169689Skan  dw_fde_ref fde;
6984169689Skan
6985169689Skan  gcc_assert (cfun);
6986169689Skan
6987169689Skan  fde = &fde_table[fde_table_in_use - 1];
6988169689Skan  fde->dw_fde_switched_sections = true;
6989169689Skan  fde->dw_fde_hot_section_label = cfun->hot_section_label;
6990169689Skan  fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6991169689Skan  fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6992169689Skan  fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6993169689Skan  have_multiple_function_sections = true;
6994169689Skan
6995169689Skan  /* Reset the current label on switching text sections, so that we
6996169689Skan     don't attempt to advance_loc4 between labels in different sections.  */
6997169689Skan  fde->dw_fde_current_label = NULL;
6998169689Skan}
6999169689Skan
7000132718Skan/* Output the location list given to us.  */
700190075Sobrien
700250397Sobrienstatic void
7003132718Skanoutput_loc_list (dw_loc_list_ref list_head)
700450397Sobrien{
700590075Sobrien  dw_loc_list_ref curr = list_head;
700650397Sobrien
700790075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
700890075Sobrien
7009169689Skan  /* Walk the location list, and output each range + expression.  */
7010117395Skan  for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
701190075Sobrien    {
701290075Sobrien      unsigned long size;
7013169689Skan      if (!have_multiple_function_sections)
7014169689Skan	{
7015169689Skan	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7016169689Skan				"Location list begin address (%s)",
7017169689Skan				list_head->ll_symbol);
7018169689Skan	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7019169689Skan				"Location list end address (%s)",
7020169689Skan				list_head->ll_symbol);
7021169689Skan	}
7022169689Skan      else
7023169689Skan	{
7024169689Skan	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7025169689Skan			       "Location list begin address (%s)",
7026169689Skan			       list_head->ll_symbol);
7027169689Skan	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7028169689Skan			       "Location list end address (%s)",
7029169689Skan			       list_head->ll_symbol);
7030169689Skan	}
703190075Sobrien      size = size_of_locs (curr->expr);
7032117395Skan
703390075Sobrien      /* Output the block length for this list of location operations.  */
7034169689Skan      gcc_assert (size <= 0xffff);
703590075Sobrien      dw2_asm_output_data (2, size, "%s", "Location expression size");
703650397Sobrien
703790075Sobrien      output_loc_sequence (curr->expr);
703890075Sobrien    }
703950397Sobrien
7040169689Skan  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
704190075Sobrien		       "Location list terminator begin (%s)",
704290075Sobrien		       list_head->ll_symbol);
7043169689Skan  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
704490075Sobrien		       "Location list terminator end (%s)",
704590075Sobrien		       list_head->ll_symbol);
704650397Sobrien}
704750397Sobrien
704850397Sobrien/* Output the DIE and its attributes.  Called recursively to generate
704950397Sobrien   the definitions of each child DIE.  */
705050397Sobrien
705150397Sobrienstatic void
7052132718Skanoutput_die (dw_die_ref die)
705350397Sobrien{
705490075Sobrien  dw_attr_ref a;
705590075Sobrien  dw_die_ref c;
705690075Sobrien  unsigned long size;
7057169689Skan  unsigned ix;
705850397Sobrien
705990075Sobrien  /* If someone in another CU might refer to us, set up a symbol for
706090075Sobrien     them to point to.  */
706190075Sobrien  if (die->die_symbol)
706290075Sobrien    output_die_symbol (die);
706350397Sobrien
706490075Sobrien  dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
706590075Sobrien			       die->die_offset, dwarf_tag_name (die->die_tag));
706650397Sobrien
7067169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
706850397Sobrien    {
706990075Sobrien      const char *name = dwarf_attr_name (a->dw_attr);
707090075Sobrien
707190075Sobrien      switch (AT_class (a))
707250397Sobrien	{
707350397Sobrien	case dw_val_class_addr:
707490075Sobrien	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
707550397Sobrien	  break;
707650397Sobrien
707790075Sobrien	case dw_val_class_offset:
707890075Sobrien	  dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
707990075Sobrien			       "%s", name);
708090075Sobrien	  break;
708150397Sobrien
708290075Sobrien	case dw_val_class_range_list:
708390075Sobrien	  {
708490075Sobrien	    char *p = strchr (ranges_section_label, '\0');
708550397Sobrien
7086132718Skan	    sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7087132718Skan		     a->dw_attr_val.v.val_offset);
708890075Sobrien	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7089169689Skan				   debug_ranges_section, "%s", name);
709090075Sobrien	    *p = '\0';
709190075Sobrien	  }
709290075Sobrien	  break;
709350397Sobrien
709490075Sobrien	case dw_val_class_loc:
709590075Sobrien	  size = size_of_locs (AT_loc (a));
709650397Sobrien
709790075Sobrien	  /* Output the block length for this list of location operations.  */
709890075Sobrien	  dw2_asm_output_data (constant_size (size), size, "%s", name);
709950397Sobrien
710090075Sobrien	  output_loc_sequence (AT_loc (a));
710150397Sobrien	  break;
710250397Sobrien
710350397Sobrien	case dw_val_class_const:
710490075Sobrien	  /* ??? It would be slightly more efficient to use a scheme like is
710590075Sobrien	     used for unsigned constants below, but gdb 4.x does not sign
710690075Sobrien	     extend.  Gdb 5.x does sign extend.  */
710790075Sobrien	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
710850397Sobrien	  break;
710950397Sobrien
711050397Sobrien	case dw_val_class_unsigned_const:
711190075Sobrien	  dw2_asm_output_data (constant_size (AT_unsigned (a)),
711290075Sobrien			       AT_unsigned (a), "%s", name);
711350397Sobrien	  break;
711450397Sobrien
711550397Sobrien	case dw_val_class_long_long:
711690075Sobrien	  {
711790075Sobrien	    unsigned HOST_WIDE_INT first, second;
711850397Sobrien
711990075Sobrien	    dw2_asm_output_data (1,
712090075Sobrien				 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7121117395Skan				 "%s", name);
712250397Sobrien
712390075Sobrien	    if (WORDS_BIG_ENDIAN)
712490075Sobrien	      {
712590075Sobrien		first = a->dw_attr_val.v.val_long_long.hi;
712690075Sobrien		second = a->dw_attr_val.v.val_long_long.low;
712790075Sobrien	      }
712890075Sobrien	    else
712990075Sobrien	      {
713090075Sobrien		first = a->dw_attr_val.v.val_long_long.low;
713190075Sobrien		second = a->dw_attr_val.v.val_long_long.hi;
713290075Sobrien	      }
713390075Sobrien
713490075Sobrien	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
713590075Sobrien				 first, "long long constant");
713690075Sobrien	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
713790075Sobrien				 second, NULL);
713890075Sobrien	  }
713950397Sobrien	  break;
714050397Sobrien
7141132718Skan	case dw_val_class_vec:
714252284Sobrien	  {
7143132718Skan	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7144132718Skan	    unsigned int len = a->dw_attr_val.v.val_vec.length;
714590075Sobrien	    unsigned int i;
7146132718Skan	    unsigned char *p;
714750397Sobrien
7148132718Skan	    dw2_asm_output_data (1, len * elt_size, "%s", name);
7149132718Skan	    if (elt_size > sizeof (HOST_WIDE_INT))
7150132718Skan	      {
7151132718Skan		elt_size /= 2;
7152132718Skan		len *= 2;
7153132718Skan	      }
7154132718Skan	    for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7155132718Skan		 i < len;
7156132718Skan		 i++, p += elt_size)
7157132718Skan	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7158132718Skan				   "fp or vector constant word %u", i);
715990075Sobrien	    break;
716052284Sobrien	  }
716150397Sobrien
716250397Sobrien	case dw_val_class_flag:
716390075Sobrien	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
716450397Sobrien	  break;
716550397Sobrien
7166117395Skan	case dw_val_class_loc_list:
716790075Sobrien	  {
716890075Sobrien	    char *sym = AT_loc_list (a)->ll_symbol;
716990075Sobrien
7170169689Skan	    gcc_assert (sym);
7171169689Skan	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7172169689Skan				   "%s", name);
717390075Sobrien	  }
717490075Sobrien	  break;
717590075Sobrien
717650397Sobrien	case dw_val_class_die_ref:
717790075Sobrien	  if (AT_ref_external (a))
717890075Sobrien	    {
717990075Sobrien	      char *sym = AT_ref (a)->die_symbol;
718090075Sobrien
7181169689Skan	      gcc_assert (sym);
7182169689Skan	      dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7183169689Skan				     "%s", name);
718490075Sobrien	    }
718550397Sobrien	  else
7186169689Skan	    {
7187169689Skan	      gcc_assert (AT_ref (a)->die_offset);
7188169689Skan	      dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7189169689Skan				   "%s", name);
7190169689Skan	    }
719150397Sobrien	  break;
719250397Sobrien
719350397Sobrien	case dw_val_class_fde_ref:
719450397Sobrien	  {
719550397Sobrien	    char l1[20];
719690075Sobrien
719790075Sobrien	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
719890075Sobrien					 a->dw_attr_val.v.val_fde_index * 2);
7199169689Skan	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7200169689Skan				   "%s", name);
720150397Sobrien	  }
720250397Sobrien	  break;
720350397Sobrien
720450397Sobrien	case dw_val_class_lbl_id:
720590075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
720650397Sobrien	  break;
720750397Sobrien
7208169689Skan	case dw_val_class_lineptr:
7209169689Skan	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7210169689Skan				 debug_line_section, "%s", name);
721150397Sobrien	  break;
721250397Sobrien
7213169689Skan	case dw_val_class_macptr:
7214169689Skan	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7215169689Skan				 debug_macinfo_section, "%s", name);
7216169689Skan	  break;
7217169689Skan
721850397Sobrien	case dw_val_class_str:
721990075Sobrien	  if (AT_string_form (a) == DW_FORM_strp)
722090075Sobrien	    dw2_asm_output_offset (DWARF_OFFSET_SIZE,
722190075Sobrien				   a->dw_attr_val.v.val_str->label,
7222169689Skan				   debug_str_section,
722390075Sobrien				   "%s: \"%s\"", name, AT_string (a));
722450397Sobrien	  else
722590075Sobrien	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
722650397Sobrien	  break;
722750397Sobrien
7228169689Skan	case dw_val_class_file:
7229169689Skan	  {
7230169689Skan	    int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7231169689Skan
7232169689Skan	    dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7233169689Skan				 a->dw_attr_val.v.val_file->filename);
7234169689Skan	    break;
7235169689Skan	  }
7236169689Skan
723750397Sobrien	default:
7238169689Skan	  gcc_unreachable ();
723950397Sobrien	}
724050397Sobrien    }
724150397Sobrien
7242169689Skan  FOR_EACH_CHILD (die, c, output_die (c));
724350397Sobrien
724490075Sobrien  /* Add null byte to terminate sibling list.  */
724550397Sobrien  if (die->die_child != NULL)
724690075Sobrien    dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
724790075Sobrien			 die->die_offset);
724850397Sobrien}
724950397Sobrien
725050397Sobrien/* Output the compilation unit that appears at the beginning of the
725150397Sobrien   .debug_info section, and precedes the DIE descriptions.  */
725250397Sobrien
725350397Sobrienstatic void
7254132718Skanoutput_compilation_unit_header (void)
725550397Sobrien{
7256132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7257132718Skan    dw2_asm_output_data (4, 0xffffffff,
7258132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
7259132718Skan  dw2_asm_output_data (DWARF_OFFSET_SIZE,
7260132718Skan                       next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
726190075Sobrien		       "Length of Compilation Unit Info");
726290075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
726390075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7264169689Skan			 debug_abbrev_section,
726590075Sobrien			 "Offset Into Abbrev. Section");
726690075Sobrien  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
726790075Sobrien}
726850397Sobrien
726990075Sobrien/* Output the compilation unit DIE and its children.  */
727050397Sobrien
727190075Sobrienstatic void
7272132718Skanoutput_comp_unit (dw_die_ref die, int output_if_empty)
727390075Sobrien{
727490075Sobrien  const char *secname;
7275117395Skan  char *oldsym, *tmp;
727650397Sobrien
7277117395Skan  /* Unless we are outputting main CU, we may throw away empty ones.  */
7278117395Skan  if (!output_if_empty && die->die_child == NULL)
7279117395Skan    return;
7280117395Skan
728190075Sobrien  /* Even if there are no children of this DIE, we must output the information
728290075Sobrien     about the compilation unit.  Otherwise, on an empty translation unit, we
728390075Sobrien     will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
728490075Sobrien     will then complain when examining the file.  First mark all the DIEs in
728590075Sobrien     this CU so we know which get local refs.  */
728690075Sobrien  mark_dies (die);
728750397Sobrien
728890075Sobrien  build_abbrev_table (die);
728990075Sobrien
729090075Sobrien  /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
729190075Sobrien  next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
729290075Sobrien  calc_die_sizes (die);
729390075Sobrien
7294117395Skan  oldsym = die->die_symbol;
7295117395Skan  if (oldsym)
729690075Sobrien    {
7297132718Skan      tmp = alloca (strlen (oldsym) + 24);
729890075Sobrien
7299117395Skan      sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
730090075Sobrien      secname = tmp;
730190075Sobrien      die->die_symbol = NULL;
7302169689Skan      switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
730390075Sobrien    }
730490075Sobrien  else
7305169689Skan    switch_to_section (debug_info_section);
730690075Sobrien
730790075Sobrien  /* Output debugging information.  */
730890075Sobrien  output_compilation_unit_header ();
730990075Sobrien  output_die (die);
731090075Sobrien
731190075Sobrien  /* Leave the marks on the main CU, so we can check them in
731290075Sobrien     output_pubnames.  */
7313117395Skan  if (oldsym)
7314117395Skan    {
7315117395Skan      unmark_dies (die);
7316117395Skan      die->die_symbol = oldsym;
7317117395Skan    }
731850397Sobrien}
731950397Sobrien
7320169689Skan/* Return the DWARF2/3 pubname associated with a decl.  */
732150397Sobrien
732290075Sobrienstatic const char *
7323132718Skandwarf2_name (tree decl, int scope)
732450397Sobrien{
7325169689Skan  return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
732650397Sobrien}
732750397Sobrien
732850397Sobrien/* Add a new entry to .debug_pubnames if appropriate.  */
732950397Sobrien
733050397Sobrienstatic void
7331132718Skanadd_pubname (tree decl, dw_die_ref die)
733250397Sobrien{
733350397Sobrien  pubname_ref p;
733450397Sobrien
733550397Sobrien  if (! TREE_PUBLIC (decl))
733650397Sobrien    return;
733750397Sobrien
733850397Sobrien  if (pubname_table_in_use == pubname_table_allocated)
733950397Sobrien    {
734050397Sobrien      pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
734190075Sobrien      pubname_table
7342132718Skan	= ggc_realloc (pubname_table,
7343132718Skan		       (pubname_table_allocated * sizeof (pubname_entry)));
7344132718Skan      memset (pubname_table + pubname_table_in_use, 0,
7345132718Skan	      PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
734650397Sobrien    }
734750397Sobrien
734850397Sobrien  p = &pubname_table[pubname_table_in_use++];
734950397Sobrien  p->die = die;
735050397Sobrien  p->name = xstrdup (dwarf2_name (decl, 1));
735150397Sobrien}
735250397Sobrien
735350397Sobrien/* Output the public names table used to speed up access to externally
735450397Sobrien   visible names.  For now, only generate entries for externally
735550397Sobrien   visible procedures.  */
735650397Sobrien
735750397Sobrienstatic void
7358132718Skanoutput_pubnames (void)
735950397Sobrien{
736090075Sobrien  unsigned i;
736190075Sobrien  unsigned long pubnames_length = size_of_pubnames ();
736250397Sobrien
7363132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7364132718Skan    dw2_asm_output_data (4, 0xffffffff,
7365132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
736690075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
736790075Sobrien		       "Length of Public Names Info");
736890075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
736990075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7370169689Skan			 debug_info_section,
737190075Sobrien			 "Offset of Compilation Unit Info");
737290075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
737390075Sobrien		       "Compilation Unit Length");
737450397Sobrien
737590075Sobrien  for (i = 0; i < pubname_table_in_use; i++)
737650397Sobrien    {
737790075Sobrien      pubname_ref pub = &pubname_table[i];
737850397Sobrien
737990075Sobrien      /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7380169689Skan      gcc_assert (pub->die->die_mark);
738150397Sobrien
738290075Sobrien      dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
738390075Sobrien			   "DIE offset");
738450397Sobrien
738590075Sobrien      dw2_asm_output_nstring (pub->name, -1, "external name");
738650397Sobrien    }
738750397Sobrien
738890075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
738950397Sobrien}
739050397Sobrien
739150397Sobrien/* Add a new entry to .debug_aranges if appropriate.  */
739250397Sobrien
739350397Sobrienstatic void
7394132718Skanadd_arange (tree decl, dw_die_ref die)
739550397Sobrien{
739650397Sobrien  if (! DECL_SECTION_NAME (decl))
739750397Sobrien    return;
739850397Sobrien
739950397Sobrien  if (arange_table_in_use == arange_table_allocated)
740050397Sobrien    {
740150397Sobrien      arange_table_allocated += ARANGE_TABLE_INCREMENT;
7402132718Skan      arange_table = ggc_realloc (arange_table,
7403132718Skan				  (arange_table_allocated
7404132718Skan				   * sizeof (dw_die_ref)));
7405132718Skan      memset (arange_table + arange_table_in_use, 0,
7406132718Skan	      ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
740750397Sobrien    }
740850397Sobrien
740950397Sobrien  arange_table[arange_table_in_use++] = die;
741050397Sobrien}
741150397Sobrien
741250397Sobrien/* Output the information that goes into the .debug_aranges table.
741350397Sobrien   Namely, define the beginning and ending address range of the
741450397Sobrien   text section generated for this compilation unit.  */
741550397Sobrien
741650397Sobrienstatic void
7417132718Skanoutput_aranges (void)
741850397Sobrien{
741990075Sobrien  unsigned i;
742090075Sobrien  unsigned long aranges_length = size_of_aranges ();
742150397Sobrien
7422132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7423132718Skan    dw2_asm_output_data (4, 0xffffffff,
7424132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
742590075Sobrien  dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
742690075Sobrien		       "Length of Address Ranges Info");
742790075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
742890075Sobrien  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7429169689Skan			 debug_info_section,
743090075Sobrien			 "Offset of Compilation Unit Info");
743190075Sobrien  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
743290075Sobrien  dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
743350397Sobrien
743490075Sobrien  /* We need to align to twice the pointer size here.  */
743590075Sobrien  if (DWARF_ARANGES_PAD_SIZE)
743690075Sobrien    {
743790075Sobrien      /* Pad using a 2 byte words so that padding is correct for any
7438132718Skan	 pointer size.  */
743990075Sobrien      dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
744090075Sobrien			   2 * DWARF2_ADDR_SIZE);
744190075Sobrien      for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
744290075Sobrien	dw2_asm_output_data (2, 0, NULL);
744390075Sobrien    }
744450397Sobrien
744590075Sobrien  dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
744690075Sobrien  dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
744790075Sobrien			text_section_label, "Length");
7448169689Skan  if (flag_reorder_blocks_and_partition)
7449169689Skan    {
7450169689Skan      dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7451169689Skan			   "Address");
7452169689Skan      dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7453169689Skan			    cold_text_section_label, "Length");
7454169689Skan    }
745550397Sobrien
745690075Sobrien  for (i = 0; i < arange_table_in_use; i++)
745790075Sobrien    {
745890075Sobrien      dw_die_ref die = arange_table[i];
745950397Sobrien
746090075Sobrien      /* We shouldn't see aranges for DIEs outside of the main CU.  */
7461169689Skan      gcc_assert (die->die_mark);
746250397Sobrien
746390075Sobrien      if (die->die_tag == DW_TAG_subprogram)
746490075Sobrien	{
746590075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
746690075Sobrien			       "Address");
746790075Sobrien	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
746890075Sobrien				get_AT_low_pc (die), "Length");
746990075Sobrien	}
747090075Sobrien      else
747190075Sobrien	{
747290075Sobrien	  /* A static variable; extract the symbol from DW_AT_location.
747390075Sobrien	     Note that this code isn't currently hit, as we only emit
747490075Sobrien	     aranges for functions (jason 9/23/99).  */
747590075Sobrien	  dw_attr_ref a = get_AT (die, DW_AT_location);
747690075Sobrien	  dw_loc_descr_ref loc;
747750397Sobrien
7478169689Skan	  gcc_assert (a && AT_class (a) == dw_val_class_loc);
747950397Sobrien
748090075Sobrien	  loc = AT_loc (a);
7481169689Skan	  gcc_assert (loc->dw_loc_opc == DW_OP_addr);
748250397Sobrien
748390075Sobrien	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
748490075Sobrien				   loc->dw_loc_oprnd1.v.val_addr, "Address");
748590075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE,
748690075Sobrien			       get_AT_unsigned (die, DW_AT_byte_size),
748790075Sobrien			       "Length");
748890075Sobrien	}
748990075Sobrien    }
749050397Sobrien
749190075Sobrien  /* Output the terminator words.  */
749290075Sobrien  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
749390075Sobrien  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
749490075Sobrien}
749590075Sobrien
749690075Sobrien/* Add a new entry to .debug_ranges.  Return the offset at which it
749790075Sobrien   was placed.  */
749890075Sobrien
749990075Sobrienstatic unsigned int
7500132718Skanadd_ranges (tree block)
750190075Sobrien{
750290075Sobrien  unsigned int in_use = ranges_table_in_use;
750390075Sobrien
750490075Sobrien  if (in_use == ranges_table_allocated)
750550397Sobrien    {
750690075Sobrien      ranges_table_allocated += RANGES_TABLE_INCREMENT;
7507132718Skan      ranges_table
7508132718Skan	= ggc_realloc (ranges_table, (ranges_table_allocated
7509132718Skan				      * sizeof (struct dw_ranges_struct)));
7510132718Skan      memset (ranges_table + ranges_table_in_use, 0,
7511132718Skan	      RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
751290075Sobrien    }
751350397Sobrien
751490075Sobrien  ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
751590075Sobrien  ranges_table_in_use = in_use + 1;
751690075Sobrien
751790075Sobrien  return in_use * 2 * DWARF2_ADDR_SIZE;
751890075Sobrien}
751990075Sobrien
752090075Sobrienstatic void
7521132718Skanoutput_ranges (void)
752290075Sobrien{
752390075Sobrien  unsigned i;
752490075Sobrien  static const char *const start_fmt = "Offset 0x%x";
752590075Sobrien  const char *fmt = start_fmt;
752690075Sobrien
752790075Sobrien  for (i = 0; i < ranges_table_in_use; i++)
752890075Sobrien    {
752990075Sobrien      int block_num = ranges_table[i].block_num;
753090075Sobrien
753190075Sobrien      if (block_num)
753290075Sobrien	{
753390075Sobrien	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
753490075Sobrien	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
753590075Sobrien
753690075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
753790075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
753890075Sobrien
753990075Sobrien	  /* If all code is in the text section, then the compilation
754090075Sobrien	     unit base address defaults to DW_AT_low_pc, which is the
754190075Sobrien	     base of the text section.  */
7542169689Skan	  if (!have_multiple_function_sections)
754390075Sobrien	    {
754490075Sobrien	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
754590075Sobrien				    text_section_label,
754690075Sobrien				    fmt, i * 2 * DWARF2_ADDR_SIZE);
754790075Sobrien	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
754890075Sobrien				    text_section_label, NULL);
754990075Sobrien	    }
755090075Sobrien
755190075Sobrien	  /* Otherwise, we add a DW_AT_entry_pc attribute to force the
755290075Sobrien	     compilation unit base address to zero, which allows us to
755390075Sobrien	     use absolute addresses, and not worry about whether the
755490075Sobrien	     target supports cross-section arithmetic.  */
755590075Sobrien	  else
755690075Sobrien	    {
755790075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
755890075Sobrien				   fmt, i * 2 * DWARF2_ADDR_SIZE);
755990075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
756090075Sobrien	    }
756190075Sobrien
756290075Sobrien	  fmt = NULL;
756390075Sobrien	}
756450397Sobrien      else
756550397Sobrien	{
756690075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
756790075Sobrien	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
756890075Sobrien	  fmt = start_fmt;
756990075Sobrien	}
757090075Sobrien    }
757190075Sobrien}
757250397Sobrien
757390075Sobrien/* Data structure containing information about input files.  */
757490075Sobrienstruct file_info
757590075Sobrien{
7576169689Skan  const char *path;	/* Complete file name.  */
7577169689Skan  const char *fname;	/* File name part.  */
757890075Sobrien  int length;		/* Length of entire string.  */
7579169689Skan  struct dwarf_file_data * file_idx;	/* Index in input file table.  */
758090075Sobrien  int dir_idx;		/* Index in directory table.  */
758190075Sobrien};
758290075Sobrien
758390075Sobrien/* Data structure containing information about directories with source
758490075Sobrien   files.  */
758590075Sobrienstruct dir_info
758690075Sobrien{
7587169689Skan  const char *path;	/* Path including directory name.  */
758890075Sobrien  int length;		/* Path length.  */
758990075Sobrien  int prefix;		/* Index of directory entry which is a prefix.  */
759090075Sobrien  int count;		/* Number of files in this directory.  */
759190075Sobrien  int dir_idx;		/* Index of directory used as base.  */
759290075Sobrien};
759390075Sobrien
759490075Sobrien/* Callback function for file_info comparison.  We sort by looking at
759590075Sobrien   the directories in the path.  */
759690075Sobrien
759790075Sobrienstatic int
7598132718Skanfile_info_cmp (const void *p1, const void *p2)
759990075Sobrien{
760090075Sobrien  const struct file_info *s1 = p1;
760190075Sobrien  const struct file_info *s2 = p2;
760290075Sobrien  unsigned char *cp1;
760390075Sobrien  unsigned char *cp2;
760490075Sobrien
760590075Sobrien  /* Take care of file names without directories.  We need to make sure that
760690075Sobrien     we return consistent values to qsort since some will get confused if
760790075Sobrien     we return the same value when identical operands are passed in opposite
760890075Sobrien     orders.  So if neither has a directory, return 0 and otherwise return
760990075Sobrien     1 or -1 depending on which one has the directory.  */
761090075Sobrien  if ((s1->path == s1->fname || s2->path == s2->fname))
761190075Sobrien    return (s2->path == s2->fname) - (s1->path == s1->fname);
761290075Sobrien
761390075Sobrien  cp1 = (unsigned char *) s1->path;
761490075Sobrien  cp2 = (unsigned char *) s2->path;
761590075Sobrien
761690075Sobrien  while (1)
761790075Sobrien    {
761890075Sobrien      ++cp1;
761990075Sobrien      ++cp2;
762090075Sobrien      /* Reached the end of the first path?  If so, handle like above.  */
762190075Sobrien      if ((cp1 == (unsigned char *) s1->fname)
762290075Sobrien	  || (cp2 == (unsigned char *) s2->fname))
762390075Sobrien	return ((cp2 == (unsigned char *) s2->fname)
762490075Sobrien		- (cp1 == (unsigned char *) s1->fname));
762590075Sobrien
762690075Sobrien      /* Character of current path component the same?  */
762790075Sobrien      else if (*cp1 != *cp2)
762890075Sobrien	return *cp1 - *cp2;
762990075Sobrien    }
763090075Sobrien}
763190075Sobrien
7632169689Skanstruct file_name_acquire_data
7633169689Skan{
7634169689Skan  struct file_info *files;
7635169689Skan  int used_files;
7636169689Skan  int max_files;
7637169689Skan};
7638169689Skan
7639169689Skan/* Traversal function for the hash table.  */
7640169689Skan
7641169689Skanstatic int
7642169689Skanfile_name_acquire (void ** slot, void *data)
7643169689Skan{
7644169689Skan  struct file_name_acquire_data *fnad = data;
7645169689Skan  struct dwarf_file_data *d = *slot;
7646169689Skan  struct file_info *fi;
7647169689Skan  const char *f;
7648169689Skan
7649169689Skan  gcc_assert (fnad->max_files >= d->emitted_number);
7650169689Skan
7651169689Skan  if (! d->emitted_number)
7652169689Skan    return 1;
7653169689Skan
7654169689Skan  gcc_assert (fnad->max_files != fnad->used_files);
7655169689Skan
7656169689Skan  fi = fnad->files + fnad->used_files++;
7657169689Skan
7658169689Skan  /* Skip all leading "./".  */
7659169689Skan  f = d->filename;
7660169689Skan  while (f[0] == '.' && f[1] == '/')
7661169689Skan    f += 2;
7662169689Skan
7663169689Skan  /* Create a new array entry.  */
7664169689Skan  fi->path = f;
7665169689Skan  fi->length = strlen (f);
7666169689Skan  fi->file_idx = d;
7667169689Skan
7668169689Skan  /* Search for the file name part.  */
7669169689Skan  f = strrchr (f, '/');
7670169689Skan  fi->fname = f == NULL ? fi->path : f + 1;
7671169689Skan  return 1;
7672169689Skan}
7673169689Skan
767490075Sobrien/* Output the directory table and the file name table.  We try to minimize
767590075Sobrien   the total amount of memory needed.  A heuristic is used to avoid large
767690075Sobrien   slowdowns with many input files.  */
767790075Sobrien
767890075Sobrienstatic void
7679132718Skanoutput_file_names (void)
768090075Sobrien{
7681169689Skan  struct file_name_acquire_data fnad;
7682169689Skan  int numfiles;
768390075Sobrien  struct file_info *files;
768490075Sobrien  struct dir_info *dirs;
768590075Sobrien  int *saved;
768690075Sobrien  int *savehere;
768790075Sobrien  int *backmap;
7688169689Skan  int ndirs;
768990075Sobrien  int idx_offset;
7690169689Skan  int i;
769190075Sobrien  int idx;
769290075Sobrien
7693169689Skan  if (!last_emitted_file)
7694132718Skan    {
7695132718Skan      dw2_asm_output_data (1, 0, "End directory table");
7696132718Skan      dw2_asm_output_data (1, 0, "End file name table");
7697132718Skan      return;
7698132718Skan    }
7699132718Skan
7700169689Skan  numfiles = last_emitted_file->emitted_number;
7701169689Skan
770290075Sobrien  /* Allocate the various arrays we need.  */
7703169689Skan  files = alloca (numfiles * sizeof (struct file_info));
7704169689Skan  dirs = alloca (numfiles * sizeof (struct dir_info));
770590075Sobrien
7706169689Skan  fnad.files = files;
7707169689Skan  fnad.used_files = 0;
7708169689Skan  fnad.max_files = numfiles;
7709169689Skan  htab_traverse (file_table, file_name_acquire, &fnad);
7710169689Skan  gcc_assert (fnad.used_files == fnad.max_files);
771190075Sobrien
7712169689Skan  qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
771390075Sobrien
771490075Sobrien  /* Find all the different directories used.  */
7715169689Skan  dirs[0].path = files[0].path;
7716169689Skan  dirs[0].length = files[0].fname - files[0].path;
771790075Sobrien  dirs[0].prefix = -1;
771890075Sobrien  dirs[0].count = 1;
771990075Sobrien  dirs[0].dir_idx = 0;
7720169689Skan  files[0].dir_idx = 0;
772190075Sobrien  ndirs = 1;
772290075Sobrien
7723169689Skan  for (i = 1; i < numfiles; i++)
772490075Sobrien    if (files[i].fname - files[i].path == dirs[ndirs - 1].length
772590075Sobrien	&& memcmp (dirs[ndirs - 1].path, files[i].path,
772690075Sobrien		   dirs[ndirs - 1].length) == 0)
772790075Sobrien      {
772890075Sobrien	/* Same directory as last entry.  */
772990075Sobrien	files[i].dir_idx = ndirs - 1;
773090075Sobrien	++dirs[ndirs - 1].count;
773190075Sobrien      }
773290075Sobrien    else
773390075Sobrien      {
7734169689Skan	int j;
773590075Sobrien
773690075Sobrien	/* This is a new directory.  */
773790075Sobrien	dirs[ndirs].path = files[i].path;
773890075Sobrien	dirs[ndirs].length = files[i].fname - files[i].path;
773990075Sobrien	dirs[ndirs].count = 1;
774090075Sobrien	dirs[ndirs].dir_idx = ndirs;
774190075Sobrien	files[i].dir_idx = ndirs;
774290075Sobrien
774390075Sobrien	/* Search for a prefix.  */
774490075Sobrien	dirs[ndirs].prefix = -1;
774590075Sobrien	for (j = 0; j < ndirs; j++)
774690075Sobrien	  if (dirs[j].length < dirs[ndirs].length
774790075Sobrien	      && dirs[j].length > 1
774890075Sobrien	      && (dirs[ndirs].prefix == -1
774990075Sobrien		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
775090075Sobrien	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
775190075Sobrien	    dirs[ndirs].prefix = j;
775290075Sobrien
775390075Sobrien	++ndirs;
775490075Sobrien      }
775590075Sobrien
775690075Sobrien  /* Now to the actual work.  We have to find a subset of the directories which
775790075Sobrien     allow expressing the file name using references to the directory table
775890075Sobrien     with the least amount of characters.  We do not do an exhaustive search
775990075Sobrien     where we would have to check out every combination of every single
776090075Sobrien     possible prefix.  Instead we use a heuristic which provides nearly optimal
776190075Sobrien     results in most cases and never is much off.  */
7762132718Skan  saved = alloca (ndirs * sizeof (int));
7763132718Skan  savehere = alloca (ndirs * sizeof (int));
776490075Sobrien
776590075Sobrien  memset (saved, '\0', ndirs * sizeof (saved[0]));
776690075Sobrien  for (i = 0; i < ndirs; i++)
776790075Sobrien    {
7768169689Skan      int j;
776990075Sobrien      int total;
777090075Sobrien
777190075Sobrien      /* We can always save some space for the current directory.  But this
777290075Sobrien	 does not mean it will be enough to justify adding the directory.  */
777390075Sobrien      savehere[i] = dirs[i].length;
777490075Sobrien      total = (savehere[i] - saved[i]) * dirs[i].count;
777590075Sobrien
777690075Sobrien      for (j = i + 1; j < ndirs; j++)
777790075Sobrien	{
777890075Sobrien	  savehere[j] = 0;
777990075Sobrien	  if (saved[j] < dirs[i].length)
778090075Sobrien	    {
778190075Sobrien	      /* Determine whether the dirs[i] path is a prefix of the
778290075Sobrien		 dirs[j] path.  */
778390075Sobrien	      int k;
778490075Sobrien
778590075Sobrien	      k = dirs[j].prefix;
7786132718Skan	      while (k != -1 && k != (int) i)
778790075Sobrien		k = dirs[k].prefix;
778890075Sobrien
7789132718Skan	      if (k == (int) i)
779090075Sobrien		{
7791169689Skan		  /* Yes it is.  We can possibly save some memory by
779290075Sobrien		     writing the filenames in dirs[j] relative to
779390075Sobrien		     dirs[i].  */
779490075Sobrien		  savehere[j] = dirs[i].length;
779590075Sobrien		  total += (savehere[j] - saved[j]) * dirs[j].count;
779690075Sobrien		}
779790075Sobrien	    }
779850397Sobrien	}
779950397Sobrien
7800169689Skan      /* Check whether we can save enough to justify adding the dirs[i]
780190075Sobrien	 directory.  */
780290075Sobrien      if (total > dirs[i].length + 1)
780390075Sobrien	{
780490075Sobrien	  /* It's worthwhile adding.  */
7805117395Skan	  for (j = i; j < ndirs; j++)
780690075Sobrien	    if (savehere[j] > 0)
780790075Sobrien	      {
780890075Sobrien		/* Remember how much we saved for this directory so far.  */
780990075Sobrien		saved[j] = savehere[j];
781050397Sobrien
781190075Sobrien		/* Remember the prefix directory.  */
781290075Sobrien		dirs[j].dir_idx = i;
781390075Sobrien	      }
781490075Sobrien	}
781590075Sobrien    }
781650397Sobrien
7817169689Skan  /* Emit the directory name table.  */
781890075Sobrien  idx = 1;
781990075Sobrien  idx_offset = dirs[0].length > 0 ? 1 : 0;
782090075Sobrien  for (i = 1 - idx_offset; i < ndirs; i++)
7821169689Skan    dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7822169689Skan			    "Directory Entry: 0x%x", i + idx_offset);
782390075Sobrien
782490075Sobrien  dw2_asm_output_data (1, 0, "End directory table");
782590075Sobrien
7826169689Skan  /* We have to emit them in the order of emitted_number since that's
7827169689Skan     used in the debug info generation.  To do this efficiently we
7828169689Skan     generate a back-mapping of the indices first.  */
7829169689Skan  backmap = alloca (numfiles * sizeof (int));
7830169689Skan  for (i = 0; i < numfiles; i++)
7831169689Skan    backmap[files[i].file_idx->emitted_number - 1] = i;
783290075Sobrien
783390075Sobrien  /* Now write all the file names.  */
7834169689Skan  for (i = 0; i < numfiles; i++)
783590075Sobrien    {
783690075Sobrien      int file_idx = backmap[i];
783790075Sobrien      int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
783890075Sobrien
783990075Sobrien      dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7840169689Skan			      "File Entry: 0x%x", (unsigned) i + 1);
784190075Sobrien
784290075Sobrien      /* Include directory index.  */
7843169689Skan      dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
784490075Sobrien
784590075Sobrien      /* Modification time.  */
784690075Sobrien      dw2_asm_output_data_uleb128 (0, NULL);
784790075Sobrien
784890075Sobrien      /* File length in bytes.  */
784990075Sobrien      dw2_asm_output_data_uleb128 (0, NULL);
785090075Sobrien    }
785190075Sobrien
785290075Sobrien  dw2_asm_output_data (1, 0, "End file name table");
785350397Sobrien}
785450397Sobrien
785590075Sobrien
785650397Sobrien/* Output the source line number correspondence information.  This
785790075Sobrien   information goes into the .debug_line section.  */
785850397Sobrien
785950397Sobrienstatic void
7860132718Skanoutput_line_info (void)
786150397Sobrien{
786290075Sobrien  char l1[20], l2[20], p1[20], p2[20];
786350397Sobrien  char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
786450397Sobrien  char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
786590075Sobrien  unsigned opc;
786690075Sobrien  unsigned n_op_args;
786790075Sobrien  unsigned long lt_index;
786890075Sobrien  unsigned long current_line;
786990075Sobrien  long line_offset;
787090075Sobrien  long line_delta;
787190075Sobrien  unsigned long current_file;
787290075Sobrien  unsigned long function;
787350397Sobrien
787490075Sobrien  ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
787590075Sobrien  ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
787690075Sobrien  ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
787790075Sobrien  ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
787850397Sobrien
7879132718Skan  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7880132718Skan    dw2_asm_output_data (4, 0xffffffff,
7881132718Skan      "Initial length escape value indicating 64-bit DWARF extension");
788290075Sobrien  dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
788390075Sobrien			"Length of Source Line Info");
788490075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l1);
788550397Sobrien
788690075Sobrien  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
788790075Sobrien  dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
788890075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, p1);
788950397Sobrien
789096263Sobrien  /* Define the architecture-dependent minimum instruction length (in
789196263Sobrien   bytes).  In this implementation of DWARF, this field is used for
789296263Sobrien   information purposes only.  Since GCC generates assembly language,
789396263Sobrien   we have no a priori knowledge of how many instruction bytes are
789496263Sobrien   generated for each source line, and therefore can use only the
789596263Sobrien   DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
789696263Sobrien   commands.  Accordingly, we fix this as `1', which is "correct
789796263Sobrien   enough" for all architectures, and don't let the target override.  */
789896263Sobrien  dw2_asm_output_data (1, 1,
789990075Sobrien		       "Minimum Instruction Length");
790096263Sobrien
790190075Sobrien  dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
790290075Sobrien		       "Default is_stmt_start flag");
790390075Sobrien  dw2_asm_output_data (1, DWARF_LINE_BASE,
790490075Sobrien		       "Line Base Value (Special Opcodes)");
790590075Sobrien  dw2_asm_output_data (1, DWARF_LINE_RANGE,
790690075Sobrien		       "Line Range Value (Special Opcodes)");
790790075Sobrien  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
790890075Sobrien		       "Special Opcode Base");
790950397Sobrien
791090075Sobrien  for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
791150397Sobrien    {
791250397Sobrien      switch (opc)
791350397Sobrien	{
791450397Sobrien	case DW_LNS_advance_pc:
791550397Sobrien	case DW_LNS_advance_line:
791650397Sobrien	case DW_LNS_set_file:
791750397Sobrien	case DW_LNS_set_column:
791850397Sobrien	case DW_LNS_fixed_advance_pc:
791950397Sobrien	  n_op_args = 1;
792050397Sobrien	  break;
792150397Sobrien	default:
792250397Sobrien	  n_op_args = 0;
792350397Sobrien	  break;
792450397Sobrien	}
792550397Sobrien
792690075Sobrien      dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
792790075Sobrien			   opc, n_op_args);
792850397Sobrien    }
792950397Sobrien
793090075Sobrien  /* Write out the information about the files we use.  */
793190075Sobrien  output_file_names ();
793290075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, p2);
793350397Sobrien
793452284Sobrien  /* We used to set the address register to the first location in the text
793552284Sobrien     section here, but that didn't accomplish anything since we already
793652284Sobrien     have a line note for the opening brace of the first function.  */
793750397Sobrien
793850397Sobrien  /* Generate the line number to PC correspondence table, encoded as
793950397Sobrien     a series of state machine operations.  */
794050397Sobrien  current_file = 1;
794150397Sobrien  current_line = 1;
7942169689Skan
7943169689Skan  if (cfun && in_cold_section_p)
7944169689Skan    strcpy (prev_line_label, cfun->cold_section_label);
7945169689Skan  else
7946169689Skan    strcpy (prev_line_label, text_section_label);
794750397Sobrien  for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
794850397Sobrien    {
794990075Sobrien      dw_line_info_ref line_info = &line_info_table[lt_index];
795050397Sobrien
795190075Sobrien#if 0
795290075Sobrien      /* Disable this optimization for now; GDB wants to see two line notes
795390075Sobrien	 at the beginning of a function so it can find the end of the
795490075Sobrien	 prologue.  */
795590075Sobrien
795652284Sobrien      /* Don't emit anything for redundant notes.  Just updating the
7957132718Skan	 address doesn't accomplish anything, because we already assume
7958132718Skan	 that anything after the last address is this line.  */
795952284Sobrien      if (line_info->dw_line_num == current_line
796052284Sobrien	  && line_info->dw_file_num == current_file)
796152284Sobrien	continue;
796290075Sobrien#endif
796352284Sobrien
796490075Sobrien      /* Emit debug info for the address of the current line.
796590075Sobrien
796690075Sobrien	 Unfortunately, we have little choice here currently, and must always
796790075Sobrien	 use the most general form.  GCC does not know the address delta
796890075Sobrien	 itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
796990075Sobrien	 attributes which will give an upper bound on the address range.  We
797090075Sobrien	 could perhaps use length attributes to determine when it is safe to
797190075Sobrien	 use DW_LNS_fixed_advance_pc.  */
797290075Sobrien
797350397Sobrien      ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
797450397Sobrien      if (0)
797550397Sobrien	{
797650397Sobrien	  /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
797790075Sobrien	  dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
797890075Sobrien			       "DW_LNS_fixed_advance_pc");
797990075Sobrien	  dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
798050397Sobrien	}
798150397Sobrien      else
798250397Sobrien	{
798390075Sobrien	  /* This can handle any delta.  This takes
7984132718Skan	     4+DWARF2_ADDR_SIZE bytes.  */
798590075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
798690075Sobrien	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
798790075Sobrien	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
798890075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
798950397Sobrien	}
799090075Sobrien
799150397Sobrien      strcpy (prev_line_label, line_label);
799250397Sobrien
799350397Sobrien      /* Emit debug info for the source file of the current line, if
799450397Sobrien	 different from the previous line.  */
799550397Sobrien      if (line_info->dw_file_num != current_file)
799650397Sobrien	{
799750397Sobrien	  current_file = line_info->dw_file_num;
799890075Sobrien	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7999169689Skan	  dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
800050397Sobrien	}
800150397Sobrien
800250397Sobrien      /* Emit debug info for the current line number, choosing the encoding
800350397Sobrien	 that uses the least amount of space.  */
800452284Sobrien      if (line_info->dw_line_num != current_line)
800550397Sobrien	{
800652284Sobrien	  line_offset = line_info->dw_line_num - current_line;
800752284Sobrien	  line_delta = line_offset - DWARF_LINE_BASE;
800852284Sobrien	  current_line = line_info->dw_line_num;
800952284Sobrien	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
801090075Sobrien	    /* This can handle deltas from -10 to 234, using the current
801190075Sobrien	       definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
801290075Sobrien	       takes 1 byte.  */
801390075Sobrien	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
801490075Sobrien				 "line %lu", current_line);
801552284Sobrien	  else
801652284Sobrien	    {
801752284Sobrien	      /* This can handle any delta.  This takes at least 4 bytes,
801852284Sobrien		 depending on the value being encoded.  */
801990075Sobrien	      dw2_asm_output_data (1, DW_LNS_advance_line,
802090075Sobrien				   "advance to line %lu", current_line);
802190075Sobrien	      dw2_asm_output_data_sleb128 (line_offset, NULL);
802290075Sobrien	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
802352284Sobrien	    }
802450397Sobrien	}
802550397Sobrien      else
802690075Sobrien	/* We still need to start a new row, so output a copy insn.  */
802790075Sobrien	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
802850397Sobrien    }
802950397Sobrien
803050397Sobrien  /* Emit debug info for the address of the end of the function.  */
803150397Sobrien  if (0)
803250397Sobrien    {
803390075Sobrien      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
803490075Sobrien			   "DW_LNS_fixed_advance_pc");
803590075Sobrien      dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
803650397Sobrien    }
803750397Sobrien  else
803850397Sobrien    {
803990075Sobrien      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
804090075Sobrien      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
804190075Sobrien      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
804290075Sobrien      dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
804350397Sobrien    }
804450397Sobrien
804590075Sobrien  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
804690075Sobrien  dw2_asm_output_data_uleb128 (1, NULL);
804790075Sobrien  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
804850397Sobrien
804950397Sobrien  function = 0;
805050397Sobrien  current_file = 1;
805150397Sobrien  current_line = 1;
805290075Sobrien  for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
805350397Sobrien    {
805490075Sobrien      dw_separate_line_info_ref line_info
805550397Sobrien	= &separate_line_info_table[lt_index];
805650397Sobrien
805790075Sobrien#if 0
805852284Sobrien      /* Don't emit anything for redundant notes.  */
805952284Sobrien      if (line_info->dw_line_num == current_line
806052284Sobrien	  && line_info->dw_file_num == current_file
806152284Sobrien	  && line_info->function == function)
806252284Sobrien	goto cont;
806390075Sobrien#endif
806452284Sobrien
806550397Sobrien      /* Emit debug info for the address of the current line.  If this is
806650397Sobrien	 a new function, or the first line of a function, then we need
806750397Sobrien	 to handle it differently.  */
806850397Sobrien      ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
806950397Sobrien				   lt_index);
807050397Sobrien      if (function != line_info->function)
807150397Sobrien	{
807250397Sobrien	  function = line_info->function;
807350397Sobrien
8074132718Skan	  /* Set the address register to the first line in the function.  */
807590075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
807690075Sobrien	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
807790075Sobrien	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
807890075Sobrien	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
807950397Sobrien	}
808050397Sobrien      else
808150397Sobrien	{
808250397Sobrien	  /* ??? See the DW_LNS_advance_pc comment above.  */
808350397Sobrien	  if (0)
808450397Sobrien	    {
808590075Sobrien	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
808690075Sobrien				   "DW_LNS_fixed_advance_pc");
808790075Sobrien	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
808850397Sobrien	    }
808950397Sobrien	  else
809050397Sobrien	    {
809190075Sobrien	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
809290075Sobrien	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
809390075Sobrien	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
809490075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
809550397Sobrien	    }
809650397Sobrien	}
809790075Sobrien
809850397Sobrien      strcpy (prev_line_label, line_label);
809950397Sobrien
810050397Sobrien      /* Emit debug info for the source file of the current line, if
810150397Sobrien	 different from the previous line.  */
810250397Sobrien      if (line_info->dw_file_num != current_file)
810350397Sobrien	{
810450397Sobrien	  current_file = line_info->dw_file_num;
810590075Sobrien	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8106169689Skan	  dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
810750397Sobrien	}
810850397Sobrien
810950397Sobrien      /* Emit debug info for the current line number, choosing the encoding
811050397Sobrien	 that uses the least amount of space.  */
811150397Sobrien      if (line_info->dw_line_num != current_line)
811250397Sobrien	{
811350397Sobrien	  line_offset = line_info->dw_line_num - current_line;
811450397Sobrien	  line_delta = line_offset - DWARF_LINE_BASE;
811550397Sobrien	  current_line = line_info->dw_line_num;
811650397Sobrien	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
811790075Sobrien	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
811890075Sobrien				 "line %lu", current_line);
811950397Sobrien	  else
812050397Sobrien	    {
812190075Sobrien	      dw2_asm_output_data (1, DW_LNS_advance_line,
812290075Sobrien				   "advance to line %lu", current_line);
812390075Sobrien	      dw2_asm_output_data_sleb128 (line_offset, NULL);
812490075Sobrien	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
812550397Sobrien	    }
812650397Sobrien	}
812752284Sobrien      else
812890075Sobrien	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
812950397Sobrien
813090075Sobrien#if 0
813152284Sobrien    cont:
813290075Sobrien#endif
813350397Sobrien
813490075Sobrien      lt_index++;
813590075Sobrien
813650397Sobrien      /* If we're done with a function, end its sequence.  */
813750397Sobrien      if (lt_index == separate_line_info_table_in_use
813850397Sobrien	  || separate_line_info_table[lt_index].function != function)
813950397Sobrien	{
814050397Sobrien	  current_file = 1;
814150397Sobrien	  current_line = 1;
814250397Sobrien
814350397Sobrien	  /* Emit debug info for the address of the end of the function.  */
814450397Sobrien	  ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
814550397Sobrien	  if (0)
814650397Sobrien	    {
814790075Sobrien	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
814890075Sobrien				   "DW_LNS_fixed_advance_pc");
814990075Sobrien	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
815050397Sobrien	    }
815150397Sobrien	  else
815250397Sobrien	    {
815390075Sobrien	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
815490075Sobrien	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
815590075Sobrien	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
815690075Sobrien	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
815750397Sobrien	    }
815850397Sobrien
815950397Sobrien	  /* Output the marker for the end of this sequence.  */
816090075Sobrien	  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
816190075Sobrien	  dw2_asm_output_data_uleb128 (1, NULL);
816290075Sobrien	  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
816350397Sobrien	}
816450397Sobrien    }
816590075Sobrien
816690075Sobrien  /* Output the marker for the end of the line number info.  */
816790075Sobrien  ASM_OUTPUT_LABEL (asm_out_file, l2);
816850397Sobrien}
816950397Sobrien
817050397Sobrien/* Given a pointer to a tree node for some base type, return a pointer to
817150397Sobrien   a DIE that describes the given type.
817250397Sobrien
817350397Sobrien   This routine must only be called for GCC type nodes that correspond to
817450397Sobrien   Dwarf base (fundamental) types.  */
817550397Sobrien
817650397Sobrienstatic dw_die_ref
8177132718Skanbase_type_die (tree type)
817850397Sobrien{
817990075Sobrien  dw_die_ref base_type_result;
818090075Sobrien  enum dwarf_type encoding;
818150397Sobrien
818290075Sobrien  if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
818350397Sobrien    return 0;
818450397Sobrien
818550397Sobrien  switch (TREE_CODE (type))
818650397Sobrien    {
818750397Sobrien    case INTEGER_TYPE:
8188169689Skan      if (TYPE_STRING_FLAG (type))
818950397Sobrien	{
8190169689Skan	  if (TYPE_UNSIGNED (type))
8191169689Skan	    encoding = DW_ATE_unsigned_char;
819250397Sobrien	  else
8193169689Skan	    encoding = DW_ATE_signed_char;
819450397Sobrien	}
8195169689Skan      else if (TYPE_UNSIGNED (type))
8196169689Skan	encoding = DW_ATE_unsigned;
819750397Sobrien      else
8198169689Skan	encoding = DW_ATE_signed;
819950397Sobrien      break;
820050397Sobrien
820150397Sobrien    case REAL_TYPE:
8202169689Skan      if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8203169689Skan	encoding = DW_ATE_decimal_float;
8204169689Skan      else
8205169689Skan	encoding = DW_ATE_float;
820650397Sobrien      break;
820750397Sobrien
820890075Sobrien      /* Dwarf2 doesn't know anything about complex ints, so use
820990075Sobrien	 a user defined type for it.  */
821050397Sobrien    case COMPLEX_TYPE:
821190075Sobrien      if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
821290075Sobrien	encoding = DW_ATE_complex_float;
821390075Sobrien      else
821490075Sobrien	encoding = DW_ATE_lo_user;
821550397Sobrien      break;
821650397Sobrien
821750397Sobrien    case BOOLEAN_TYPE:
821850397Sobrien      /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
821950397Sobrien      encoding = DW_ATE_boolean;
822050397Sobrien      break;
822150397Sobrien
822250397Sobrien    default:
822390075Sobrien      /* No other TREE_CODEs are Dwarf fundamental types.  */
8224169689Skan      gcc_unreachable ();
822550397Sobrien    }
822650397Sobrien
822790075Sobrien  base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
822890075Sobrien
8229169689Skan  /* This probably indicates a bug.  */
8230169689Skan  if (! TYPE_NAME (type))
8231169689Skan    add_name_attribute (base_type_result, "__unknown__");
8232169689Skan
823350397Sobrien  add_AT_unsigned (base_type_result, DW_AT_byte_size,
823450397Sobrien		   int_size_in_bytes (type));
823550397Sobrien  add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
823650397Sobrien
823750397Sobrien  return base_type_result;
823850397Sobrien}
823950397Sobrien
824050397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
824150397Sobrien   the Dwarf "root" type for the given input type.  The Dwarf "root" type of
824250397Sobrien   a given type is generally the same as the given type, except that if the
824350397Sobrien   given type is a pointer or reference type, then the root type of the given
824450397Sobrien   type is the root type of the "basis" type for the pointer or reference
824550397Sobrien   type.  (This definition of the "root" type is recursive.) Also, the root
824650397Sobrien   type of a `const' qualified type or a `volatile' qualified type is the
824750397Sobrien   root type of the given type without the qualifiers.  */
824850397Sobrien
824950397Sobrienstatic tree
8250132718Skanroot_type (tree type)
825150397Sobrien{
825250397Sobrien  if (TREE_CODE (type) == ERROR_MARK)
825350397Sobrien    return error_mark_node;
825450397Sobrien
825550397Sobrien  switch (TREE_CODE (type))
825650397Sobrien    {
825750397Sobrien    case ERROR_MARK:
825850397Sobrien      return error_mark_node;
825950397Sobrien
826050397Sobrien    case POINTER_TYPE:
826150397Sobrien    case REFERENCE_TYPE:
826250397Sobrien      return type_main_variant (root_type (TREE_TYPE (type)));
826350397Sobrien
826450397Sobrien    default:
826550397Sobrien      return type_main_variant (type);
826650397Sobrien    }
826750397Sobrien}
826850397Sobrien
8269117395Skan/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
827050397Sobrien   given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
827150397Sobrien
827250397Sobrienstatic inline int
8273132718Skanis_base_type (tree type)
827450397Sobrien{
827550397Sobrien  switch (TREE_CODE (type))
827650397Sobrien    {
827750397Sobrien    case ERROR_MARK:
827850397Sobrien    case VOID_TYPE:
827950397Sobrien    case INTEGER_TYPE:
828050397Sobrien    case REAL_TYPE:
828150397Sobrien    case COMPLEX_TYPE:
828250397Sobrien    case BOOLEAN_TYPE:
828350397Sobrien      return 1;
828450397Sobrien
828550397Sobrien    case ARRAY_TYPE:
828650397Sobrien    case RECORD_TYPE:
828750397Sobrien    case UNION_TYPE:
828850397Sobrien    case QUAL_UNION_TYPE:
828950397Sobrien    case ENUMERAL_TYPE:
829050397Sobrien    case FUNCTION_TYPE:
829150397Sobrien    case METHOD_TYPE:
829250397Sobrien    case POINTER_TYPE:
829350397Sobrien    case REFERENCE_TYPE:
829450397Sobrien    case OFFSET_TYPE:
829550397Sobrien    case LANG_TYPE:
829690075Sobrien    case VECTOR_TYPE:
829750397Sobrien      return 0;
829850397Sobrien
829950397Sobrien    default:
8300169689Skan      gcc_unreachable ();
830150397Sobrien    }
830250397Sobrien
830350397Sobrien  return 0;
830450397Sobrien}
830550397Sobrien
8306132718Skan/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8307132718Skan   node, return the size in bits for the type if it is a constant, or else
8308132718Skan   return the alignment for the type if the type's size is not constant, or
8309132718Skan   else return BITS_PER_WORD if the type actually turns out to be an
8310132718Skan   ERROR_MARK node.  */
8311132718Skan
8312132718Skanstatic inline unsigned HOST_WIDE_INT
8313132718Skansimple_type_size_in_bits (tree type)
8314132718Skan{
8315132718Skan  if (TREE_CODE (type) == ERROR_MARK)
8316132718Skan    return BITS_PER_WORD;
8317132718Skan  else if (TYPE_SIZE (type) == NULL_TREE)
8318132718Skan    return 0;
8319132718Skan  else if (host_integerp (TYPE_SIZE (type), 1))
8320132718Skan    return tree_low_cst (TYPE_SIZE (type), 1);
8321132718Skan  else
8322132718Skan    return TYPE_ALIGN (type);
8323132718Skan}
8324132718Skan
8325132718Skan/* Return true if the debug information for the given type should be
8326132718Skan   emitted as a subrange type.  */
8327132718Skan
8328132718Skanstatic inline bool
8329132718Skanis_subrange_type (tree type)
8330132718Skan{
8331132718Skan  tree subtype = TREE_TYPE (type);
8332132718Skan
8333169689Skan  /* Subrange types are identified by the fact that they are integer
8334169689Skan     types, and that they have a subtype which is either an integer type
8335169689Skan     or an enumeral type.  */
8336169689Skan
8337169689Skan  if (TREE_CODE (type) != INTEGER_TYPE
8338169689Skan      || subtype == NULL_TREE)
8339169689Skan    return false;
8340169689Skan
8341169689Skan  if (TREE_CODE (subtype) != INTEGER_TYPE
8342169689Skan      && TREE_CODE (subtype) != ENUMERAL_TYPE)
8343169689Skan    return false;
8344169689Skan
8345169689Skan  if (TREE_CODE (type) == TREE_CODE (subtype)
8346169689Skan      && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8347169689Skan      && TYPE_MIN_VALUE (type) != NULL
8348169689Skan      && TYPE_MIN_VALUE (subtype) != NULL
8349169689Skan      && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8350169689Skan      && TYPE_MAX_VALUE (type) != NULL
8351169689Skan      && TYPE_MAX_VALUE (subtype) != NULL
8352169689Skan      && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8353132718Skan    {
8354169689Skan      /* The type and its subtype have the same representation.  If in
8355169689Skan         addition the two types also have the same name, then the given
8356169689Skan         type is not a subrange type, but rather a plain base type.  */
8357169689Skan      /* FIXME: brobecker/2004-03-22:
8358169689Skan         Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8359169689Skan         therefore be sufficient to check the TYPE_SIZE node pointers
8360169689Skan         rather than checking the actual size.  Unfortunately, we have
8361169689Skan         found some cases, such as in the Ada "integer" type, where
8362169689Skan         this is not the case.  Until this problem is solved, we need to
8363169689Skan         keep checking the actual size.  */
8364169689Skan      tree type_name = TYPE_NAME (type);
8365169689Skan      tree subtype_name = TYPE_NAME (subtype);
8366169689Skan
8367169689Skan      if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8368169689Skan        type_name = DECL_NAME (type_name);
8369169689Skan
8370169689Skan      if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8371169689Skan        subtype_name = DECL_NAME (subtype_name);
8372169689Skan
8373169689Skan      if (type_name == subtype_name)
8374169689Skan        return false;
8375132718Skan    }
8376169689Skan
8377169689Skan  return true;
8378132718Skan}
8379132718Skan
8380132718Skan/*  Given a pointer to a tree node for a subrange type, return a pointer
8381132718Skan    to a DIE that describes the given type.  */
8382132718Skan
8383132718Skanstatic dw_die_ref
8384132718Skansubrange_type_die (tree type, dw_die_ref context_die)
8385132718Skan{
8386132718Skan  dw_die_ref subrange_die;
8387132718Skan  const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8388132718Skan
8389132718Skan  if (context_die == NULL)
8390132718Skan    context_die = comp_unit_die;
8391132718Skan
8392132718Skan  subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8393132718Skan
8394132718Skan  if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8395132718Skan    {
8396132718Skan      /* The size of the subrange type and its base type do not match,
8397132718Skan         so we need to generate a size attribute for the subrange type.  */
8398132718Skan      add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8399132718Skan    }
8400132718Skan
8401132718Skan  if (TYPE_MIN_VALUE (type) != NULL)
8402132718Skan    add_bound_info (subrange_die, DW_AT_lower_bound,
8403132718Skan                    TYPE_MIN_VALUE (type));
8404132718Skan  if (TYPE_MAX_VALUE (type) != NULL)
8405132718Skan    add_bound_info (subrange_die, DW_AT_upper_bound,
8406132718Skan                    TYPE_MAX_VALUE (type));
8407132718Skan
8408132718Skan  return subrange_die;
8409132718Skan}
8410132718Skan
841150397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
841250397Sobrien   entry that chains various modifiers in front of the given type.  */
841350397Sobrien
841450397Sobrienstatic dw_die_ref
8415132718Skanmodified_type_die (tree type, int is_const_type, int is_volatile_type,
8416132718Skan		   dw_die_ref context_die)
841750397Sobrien{
841890075Sobrien  enum tree_code code = TREE_CODE (type);
8419169689Skan  dw_die_ref mod_type_die;
842090075Sobrien  dw_die_ref sub_die = NULL;
842190075Sobrien  tree item_type = NULL;
8422169689Skan  tree qualified_type;
8423169689Skan  tree name;
842450397Sobrien
8425169689Skan  if (code == ERROR_MARK)
8426169689Skan    return NULL;
8427169689Skan
8428169689Skan  /* See if we already have the appropriately qualified variant of
8429169689Skan     this type.  */
8430169689Skan  qualified_type
8431169689Skan    = get_qualified_type (type,
8432169689Skan			  ((is_const_type ? TYPE_QUAL_CONST : 0)
8433169689Skan			   | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8434169689Skan
8435169689Skan  /* If we do, then we can just use its DIE, if it exists.  */
8436169689Skan  if (qualified_type)
843750397Sobrien    {
8438169689Skan      mod_type_die = lookup_type_die (qualified_type);
843950397Sobrien      if (mod_type_die)
8440169689Skan	return mod_type_die;
8441169689Skan    }
8442169689Skan
8443169689Skan  name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8444169689Skan
8445169689Skan  /* Handle C typedef types.  */
8446169689Skan  if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8447169689Skan    {
8448169689Skan      tree dtype = TREE_TYPE (name);
8449169689Skan
8450169689Skan      if (qualified_type == dtype)
845150397Sobrien	{
8452169689Skan	  /* For a named type, use the typedef.  */
8453169689Skan	  gen_type_die (qualified_type, context_die);
8454169689Skan	  return lookup_type_die (qualified_type);
845550397Sobrien	}
8456169689Skan      else if (is_const_type < TYPE_READONLY (dtype)
8457169689Skan	       || is_volatile_type < TYPE_VOLATILE (dtype)
8458169689Skan	       || (is_const_type <= TYPE_READONLY (dtype)
8459169689Skan		   && is_volatile_type <= TYPE_VOLATILE (dtype)
8460169689Skan		   && DECL_ORIGINAL_TYPE (name) != type))
8461169689Skan	/* cv-unqualified version of named type.  Just use the unnamed
8462169689Skan	   type to which it refers.  */
8463169689Skan	return modified_type_die (DECL_ORIGINAL_TYPE (name),
8464169689Skan				  is_const_type, is_volatile_type,
8465169689Skan				  context_die);
8466169689Skan      /* Else cv-qualified version of named type; fall through.  */
8467169689Skan    }
8468169689Skan
8469169689Skan  if (is_const_type)
8470169689Skan    {
8471169689Skan      mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8472169689Skan      sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8473169689Skan    }
8474169689Skan  else if (is_volatile_type)
8475169689Skan    {
8476169689Skan      mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8477169689Skan      sub_die = modified_type_die (type, 0, 0, context_die);
8478169689Skan    }
8479169689Skan  else if (code == POINTER_TYPE)
8480169689Skan    {
8481169689Skan      mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8482169689Skan      add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8483169689Skan		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
8484169689Skan      item_type = TREE_TYPE (type);
8485169689Skan    }
8486169689Skan  else if (code == REFERENCE_TYPE)
8487169689Skan    {
8488169689Skan      mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8489169689Skan      add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8490169689Skan		       simple_type_size_in_bits (type) / BITS_PER_UNIT);
8491169689Skan      item_type = TREE_TYPE (type);
8492169689Skan    }
8493169689Skan  else if (is_subrange_type (type))
8494169689Skan    {
8495169689Skan      mod_type_die = subrange_type_die (type, context_die);
8496169689Skan      item_type = TREE_TYPE (type);
8497169689Skan    }
8498169689Skan  else if (is_base_type (type))
8499169689Skan    mod_type_die = base_type_die (type);
8500169689Skan  else
8501169689Skan    {
8502169689Skan      gen_type_die (type, context_die);
8503169689Skan
8504169689Skan      /* We have to get the type_main_variant here (and pass that to the
8505169689Skan	 `lookup_type_die' routine) because the ..._TYPE node we have
8506169689Skan	 might simply be a *copy* of some original type node (where the
8507169689Skan	 copy was created to help us keep track of typedef names) and
8508169689Skan	 that copy might have a different TYPE_UID from the original
8509169689Skan	 ..._TYPE node.  */
8510169689Skan      if (TREE_CODE (type) != VECTOR_TYPE)
8511169689Skan	return lookup_type_die (type_main_variant (type));
851250397Sobrien      else
8513169689Skan	/* Vectors have the debugging information in the type,
8514169689Skan	   not the main variant.  */
8515169689Skan	return lookup_type_die (type);
851650397Sobrien    }
8517169689Skan
8518169689Skan  /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8519169689Skan     don't output a DW_TAG_typedef, since there isn't one in the
8520169689Skan     user's program; just attach a DW_AT_name to the type.  */
8521169689Skan  if (name
8522169689Skan      && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8523169689Skan    {
8524169689Skan      if (TREE_CODE (name) == TYPE_DECL)
8525169689Skan	/* Could just call add_name_and_src_coords_attributes here,
8526169689Skan	   but since this is a builtin type it doesn't have any
8527169689Skan	   useful source coordinates anyway.  */
8528169689Skan	name = DECL_NAME (name);
8529169689Skan      add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8530169689Skan    }
8531169689Skan
8532169689Skan  if (qualified_type)
8533169689Skan    equate_type_number_to_die (qualified_type, mod_type_die);
853450397Sobrien
853550397Sobrien  if (item_type)
853650397Sobrien    /* We must do this after the equate_type_number_to_die call, in case
853750397Sobrien       this is a recursive type.  This ensures that the modified_type_die
853850397Sobrien       recursion will terminate even if the type is recursive.  Recursive
853950397Sobrien       types are possible in Ada.  */
854050397Sobrien    sub_die = modified_type_die (item_type,
854150397Sobrien				 TYPE_READONLY (item_type),
854250397Sobrien				 TYPE_VOLATILE (item_type),
854350397Sobrien				 context_die);
854450397Sobrien
854550397Sobrien  if (sub_die != NULL)
854650397Sobrien    add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
854750397Sobrien
854850397Sobrien  return mod_type_die;
854950397Sobrien}
855050397Sobrien
855150397Sobrien/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
855290075Sobrien   an enumerated type.  */
855350397Sobrien
855450397Sobrienstatic inline int
8555132718Skantype_is_enum (tree type)
855650397Sobrien{
855750397Sobrien  return TREE_CODE (type) == ENUMERAL_TYPE;
855850397Sobrien}
855950397Sobrien
8560132718Skan/* Return the DBX register number described by a given RTL node.  */
856150397Sobrien
856290075Sobrienstatic unsigned int
8563132718Skandbx_reg_number (rtx rtl)
856490075Sobrien{
856590075Sobrien  unsigned regno = REGNO (rtl);
856690075Sobrien
8567169689Skan  gcc_assert (regno < FIRST_PSEUDO_REGISTER);
856890075Sobrien
8569169689Skan#ifdef LEAF_REG_REMAP
8570169689Skan  if (current_function_uses_only_leaf_regs)
8571169689Skan    {
8572169689Skan      int leaf_reg = LEAF_REG_REMAP (regno);
8573169689Skan      if (leaf_reg != -1)
8574169689Skan	regno = (unsigned) leaf_reg;
8575169689Skan    }
8576169689Skan#endif
8577169689Skan
857890075Sobrien  return DBX_REGISTER_NUMBER (regno);
857990075Sobrien}
858090075Sobrien
8581169689Skan/* Optionally add a DW_OP_piece term to a location description expression.
8582169689Skan   DW_OP_piece is only added if the location description expression already
8583169689Skan   doesn't end with DW_OP_piece.  */
8584169689Skan
8585169689Skanstatic void
8586169689Skanadd_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8587169689Skan{
8588169689Skan  dw_loc_descr_ref loc;
8589169689Skan
8590169689Skan  if (*list_head != NULL)
8591169689Skan    {
8592169689Skan      /* Find the end of the chain.  */
8593169689Skan      for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8594169689Skan	;
8595169689Skan
8596169689Skan      if (loc->dw_loc_opc != DW_OP_piece)
8597169689Skan	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8598169689Skan    }
8599169689Skan}
8600169689Skan
860190075Sobrien/* Return a location descriptor that designates a machine register or
8602132718Skan   zero if there is none.  */
860390075Sobrien
860450397Sobrienstatic dw_loc_descr_ref
8605132718Skanreg_loc_descriptor (rtx rtl)
860650397Sobrien{
8607132718Skan  rtx regs;
860850397Sobrien
860990075Sobrien  if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
861090075Sobrien    return 0;
861190075Sobrien
8612169689Skan  regs = targetm.dwarf_register_span (rtl);
8613132718Skan
8614169689Skan  if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8615132718Skan    return multiple_reg_loc_descriptor (rtl, regs);
861650397Sobrien  else
8617169689Skan    return one_reg_loc_descriptor (dbx_reg_number (rtl));
8618132718Skan}
861950397Sobrien
8620132718Skan/* Return a location descriptor that designates a machine register for
8621132718Skan   a given hard register number.  */
8622132718Skan
8623132718Skanstatic dw_loc_descr_ref
8624132718Skanone_reg_loc_descriptor (unsigned int regno)
8625132718Skan{
8626132718Skan  if (regno <= 31)
8627132718Skan    return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8628132718Skan  else
8629132718Skan    return new_loc_descr (DW_OP_regx, regno, 0);
8630132718Skan}
8631132718Skan
8632132718Skan/* Given an RTL of a register, return a location descriptor that
8633132718Skan   designates a value that spans more than one register.  */
8634132718Skan
8635132718Skanstatic dw_loc_descr_ref
8636132718Skanmultiple_reg_loc_descriptor (rtx rtl, rtx regs)
8637132718Skan{
8638132718Skan  int nregs, size, i;
8639132718Skan  unsigned reg;
8640132718Skan  dw_loc_descr_ref loc_result = NULL;
8641132718Skan
8642169689Skan  reg = REGNO (rtl);
8643169689Skan#ifdef LEAF_REG_REMAP
8644169689Skan  if (current_function_uses_only_leaf_regs)
8645169689Skan    {
8646169689Skan      int leaf_reg = LEAF_REG_REMAP (reg);
8647169689Skan      if (leaf_reg != -1)
8648169689Skan	reg = (unsigned) leaf_reg;
8649169689Skan    }
8650169689Skan#endif
8651169689Skan  gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8652169689Skan  nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8653132718Skan
8654132718Skan  /* Simple, contiguous registers.  */
8655132718Skan  if (regs == NULL_RTX)
8656132718Skan    {
8657132718Skan      size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8658132718Skan
8659132718Skan      loc_result = NULL;
8660132718Skan      while (nregs--)
8661132718Skan	{
8662132718Skan	  dw_loc_descr_ref t;
8663132718Skan
8664169689Skan	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8665132718Skan	  add_loc_descr (&loc_result, t);
8666169689Skan	  add_loc_descr_op_piece (&loc_result, size);
8667132718Skan	  ++reg;
8668132718Skan	}
8669132718Skan      return loc_result;
8670132718Skan    }
8671132718Skan
8672132718Skan  /* Now onto stupid register sets in non contiguous locations.  */
8673132718Skan
8674169689Skan  gcc_assert (GET_CODE (regs) == PARALLEL);
8675132718Skan
8676132718Skan  size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8677132718Skan  loc_result = NULL;
8678132718Skan
8679132718Skan  for (i = 0; i < XVECLEN (regs, 0); ++i)
8680132718Skan    {
8681132718Skan      dw_loc_descr_ref t;
8682132718Skan
8683132718Skan      t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8684132718Skan      add_loc_descr (&loc_result, t);
8685132718Skan      size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8686169689Skan      add_loc_descr_op_piece (&loc_result, size);
8687132718Skan    }
868850397Sobrien  return loc_result;
868950397Sobrien}
869050397Sobrien
869190075Sobrien/* Return a location descriptor that designates a constant.  */
869290075Sobrien
869390075Sobrienstatic dw_loc_descr_ref
8694132718Skanint_loc_descriptor (HOST_WIDE_INT i)
869590075Sobrien{
869690075Sobrien  enum dwarf_location_atom op;
869790075Sobrien
869890075Sobrien  /* Pick the smallest representation of a constant, rather than just
869990075Sobrien     defaulting to the LEB encoding.  */
870090075Sobrien  if (i >= 0)
870190075Sobrien    {
870290075Sobrien      if (i <= 31)
870390075Sobrien	op = DW_OP_lit0 + i;
870490075Sobrien      else if (i <= 0xff)
870590075Sobrien	op = DW_OP_const1u;
870690075Sobrien      else if (i <= 0xffff)
870790075Sobrien	op = DW_OP_const2u;
870890075Sobrien      else if (HOST_BITS_PER_WIDE_INT == 32
870990075Sobrien	       || i <= 0xffffffff)
871090075Sobrien	op = DW_OP_const4u;
871190075Sobrien      else
871290075Sobrien	op = DW_OP_constu;
871390075Sobrien    }
871490075Sobrien  else
871590075Sobrien    {
871690075Sobrien      if (i >= -0x80)
871790075Sobrien	op = DW_OP_const1s;
871890075Sobrien      else if (i >= -0x8000)
871990075Sobrien	op = DW_OP_const2s;
872090075Sobrien      else if (HOST_BITS_PER_WIDE_INT == 32
872190075Sobrien	       || i >= -0x80000000)
872290075Sobrien	op = DW_OP_const4s;
872390075Sobrien      else
872490075Sobrien	op = DW_OP_consts;
872590075Sobrien    }
872690075Sobrien
872790075Sobrien  return new_loc_descr (op, i, 0);
872890075Sobrien}
872990075Sobrien
873050397Sobrien/* Return a location descriptor that designates a base+offset location.  */
873150397Sobrien
873250397Sobrienstatic dw_loc_descr_ref
8733169689Skanbased_loc_descr (rtx reg, HOST_WIDE_INT offset)
873450397Sobrien{
8735169689Skan  unsigned int regno;
873650397Sobrien
8737169689Skan  /* We only use "frame base" when we're sure we're talking about the
8738169689Skan     post-prologue local stack frame.  We do this by *not* running
8739169689Skan     register elimination until this point, and recognizing the special
8740169689Skan     argument pointer and soft frame pointer rtx's.  */
8741169689Skan  if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8742169689Skan    {
8743169689Skan      rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8744169689Skan
8745169689Skan      if (elim != reg)
8746169689Skan	{
8747169689Skan	  if (GET_CODE (elim) == PLUS)
8748169689Skan	    {
8749169689Skan	      offset += INTVAL (XEXP (elim, 1));
8750169689Skan	      elim = XEXP (elim, 0);
8751169689Skan	    }
8752169689Skan	  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8753169689Skan		      : stack_pointer_rtx));
8754169689Skan          offset += frame_pointer_fb_offset;
8755169689Skan
8756169689Skan          return new_loc_descr (DW_OP_fbreg, offset, 0);
8757169689Skan	}
8758169689Skan    }
8759169689Skan
8760169689Skan  regno = dbx_reg_number (reg);
8761169689Skan  if (regno <= 31)
8762169689Skan    return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
876350397Sobrien  else
8764169689Skan    return new_loc_descr (DW_OP_bregx, regno, offset);
876550397Sobrien}
876650397Sobrien
876750397Sobrien/* Return true if this RTL expression describes a base+offset calculation.  */
876850397Sobrien
876950397Sobrienstatic inline int
8770132718Skanis_based_loc (rtx rtl)
877150397Sobrien{
877290075Sobrien  return (GET_CODE (rtl) == PLUS
8773169689Skan	  && ((REG_P (XEXP (rtl, 0))
877490075Sobrien	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
877590075Sobrien	       && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
877650397Sobrien}
877750397Sobrien
877850397Sobrien/* The following routine converts the RTL for a variable or parameter
877950397Sobrien   (resident in memory) into an equivalent Dwarf representation of a
878050397Sobrien   mechanism for getting the address of that same variable onto the top of a
878150397Sobrien   hypothetical "address evaluation" stack.
878250397Sobrien
878350397Sobrien   When creating memory location descriptors, we are effectively transforming
878450397Sobrien   the RTL for a memory-resident object into its Dwarf postfix expression
878550397Sobrien   equivalent.  This routine recursively descends an RTL tree, turning
878690075Sobrien   it into Dwarf postfix code as it goes.
878750397Sobrien
878890075Sobrien   MODE is the mode of the memory reference, needed to handle some
878990075Sobrien   autoincrement addressing modes.
879090075Sobrien
8791169689Skan   CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8792169689Skan   location list for RTL.
8793169689Skan
879490075Sobrien   Return 0 if we can't represent the location.  */
879590075Sobrien
879650397Sobrienstatic dw_loc_descr_ref
8797132718Skanmem_loc_descriptor (rtx rtl, enum machine_mode mode)
879850397Sobrien{
879950397Sobrien  dw_loc_descr_ref mem_loc_result = NULL;
8800169689Skan  enum dwarf_location_atom op;
880190075Sobrien
880290075Sobrien  /* Note that for a dynamically sized array, the location we will generate a
880350397Sobrien     description of here will be the lowest numbered location which is
880450397Sobrien     actually within the array.  That's *not* necessarily the same as the
880550397Sobrien     zeroth element of the array.  */
880650397Sobrien
8807169689Skan  rtl = targetm.delegitimize_address (rtl);
880890075Sobrien
880950397Sobrien  switch (GET_CODE (rtl))
881050397Sobrien    {
881190075Sobrien    case POST_INC:
881290075Sobrien    case POST_DEC:
881390075Sobrien    case POST_MODIFY:
881490075Sobrien      /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
881590075Sobrien	 just fall into the SUBREG code.  */
881690075Sobrien
881790075Sobrien      /* ... fall through ...  */
881890075Sobrien
881950397Sobrien    case SUBREG:
882050397Sobrien      /* The case of a subreg may arise when we have a local (register)
8821132718Skan	 variable or a formal (register) parameter which doesn't quite fill
8822132718Skan	 up an entire register.  For now, just assume that it is
8823132718Skan	 legitimate to make the Dwarf info refer to the whole register which
8824132718Skan	 contains the given subreg.  */
8825169689Skan      rtl = XEXP (rtl, 0);
882650397Sobrien
882790075Sobrien      /* ... fall through ...  */
882850397Sobrien
882950397Sobrien    case REG:
883050397Sobrien      /* Whenever a register number forms a part of the description of the
8831132718Skan	 method for calculating the (dynamic) address of a memory resident
8832132718Skan	 object, DWARF rules require the register number be referred to as
8833132718Skan	 a "base register".  This distinction is not based in any way upon
8834132718Skan	 what category of register the hardware believes the given register
8835132718Skan	 belongs to.  This is strictly DWARF terminology we're dealing with
8836132718Skan	 here. Note that in cases where the location of a memory-resident
8837132718Skan	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8838132718Skan	 OP_CONST (0)) the actual DWARF location descriptor that we generate
8839132718Skan	 may just be OP_BASEREG (basereg).  This may look deceptively like
8840132718Skan	 the object in question was allocated to a register (rather than in
8841132718Skan	 memory) so DWARF consumers need to be aware of the subtle
8842132718Skan	 distinction between OP_REG and OP_BASEREG.  */
884390075Sobrien      if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8844169689Skan	mem_loc_result = based_loc_descr (rtl, 0);
884550397Sobrien      break;
884650397Sobrien
884750397Sobrien    case MEM:
884890075Sobrien      mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
884990075Sobrien      if (mem_loc_result != 0)
885090075Sobrien	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
885150397Sobrien      break;
885250397Sobrien
8853117395Skan    case LO_SUM:
8854117395Skan	 rtl = XEXP (rtl, 1);
8855117395Skan
8856117395Skan      /* ... fall through ...  */
8857117395Skan
885890075Sobrien    case LABEL_REF:
885990075Sobrien      /* Some ports can transform a symbol ref into a label ref, because
8860132718Skan	 the symbol ref is too far away and has to be dumped into a constant
8861132718Skan	 pool.  */
886250397Sobrien    case CONST:
886350397Sobrien    case SYMBOL_REF:
886490075Sobrien      /* Alternatively, the symbol in the constant pool might be referenced
886590075Sobrien	 by a different symbol.  */
886690075Sobrien      if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
886790075Sobrien	{
886896263Sobrien	  bool marked;
886996263Sobrien	  rtx tmp = get_pool_constant_mark (rtl, &marked);
887090075Sobrien
887190075Sobrien	  if (GET_CODE (tmp) == SYMBOL_REF)
887296263Sobrien	    {
887396263Sobrien	      rtl = tmp;
887496263Sobrien	      if (CONSTANT_POOL_ADDRESS_P (tmp))
887596263Sobrien		get_pool_constant_mark (tmp, &marked);
887696263Sobrien	      else
887796263Sobrien		marked = true;
887896263Sobrien	    }
887996263Sobrien
888096263Sobrien	  /* If all references to this pool constant were optimized away,
888196263Sobrien	     it was not output and thus we can't represent it.
888296263Sobrien	     FIXME: might try to use DW_OP_const_value here, though
888396263Sobrien	     DW_OP_piece complicates it.  */
888496263Sobrien	  if (!marked)
888596263Sobrien	    return 0;
888690075Sobrien	}
888790075Sobrien
888850397Sobrien      mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
888950397Sobrien      mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
889090075Sobrien      mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8891169689Skan      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
889250397Sobrien      break;
889350397Sobrien
889490075Sobrien    case PRE_MODIFY:
889590075Sobrien      /* Extract the PLUS expression nested inside and fall into
8896132718Skan	 PLUS code below.  */
889790075Sobrien      rtl = XEXP (rtl, 1);
889890075Sobrien      goto plus;
889990075Sobrien
890090075Sobrien    case PRE_INC:
890190075Sobrien    case PRE_DEC:
890290075Sobrien      /* Turn these into a PLUS expression and fall into the PLUS code
890390075Sobrien	 below.  */
890490075Sobrien      rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
890590075Sobrien			  GEN_INT (GET_CODE (rtl) == PRE_INC
890690075Sobrien				   ? GET_MODE_UNIT_SIZE (mode)
890790075Sobrien				   : -GET_MODE_UNIT_SIZE (mode)));
890890075Sobrien
890990075Sobrien      /* ... fall through ...  */
891090075Sobrien
891150397Sobrien    case PLUS:
891290075Sobrien    plus:
891350397Sobrien      if (is_based_loc (rtl))
8914169689Skan	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
891550397Sobrien					  INTVAL (XEXP (rtl, 1)));
891650397Sobrien      else
891750397Sobrien	{
891890075Sobrien	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
891990075Sobrien	  if (mem_loc_result == 0)
892090075Sobrien	    break;
892190075Sobrien
892290075Sobrien	  if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
892390075Sobrien	      && INTVAL (XEXP (rtl, 1)) >= 0)
892490075Sobrien	    add_loc_descr (&mem_loc_result,
892590075Sobrien			   new_loc_descr (DW_OP_plus_uconst,
892690075Sobrien					  INTVAL (XEXP (rtl, 1)), 0));
892790075Sobrien	  else
892890075Sobrien	    {
892990075Sobrien	      add_loc_descr (&mem_loc_result,
893090075Sobrien			     mem_loc_descriptor (XEXP (rtl, 1), mode));
893190075Sobrien	      add_loc_descr (&mem_loc_result,
893290075Sobrien			     new_loc_descr (DW_OP_plus, 0, 0));
893390075Sobrien	    }
893450397Sobrien	}
893550397Sobrien      break;
893650397Sobrien
8937169689Skan    /* If a pseudo-reg is optimized away, it is possible for it to
8938169689Skan       be replaced with a MEM containing a multiply or shift.  */
893950397Sobrien    case MULT:
8940169689Skan      op = DW_OP_mul;
8941169689Skan      goto do_binop;
8942169689Skan
8943169689Skan    case ASHIFT:
8944169689Skan      op = DW_OP_shl;
8945169689Skan      goto do_binop;
8946169689Skan
8947169689Skan    case ASHIFTRT:
8948169689Skan      op = DW_OP_shra;
8949169689Skan      goto do_binop;
8950169689Skan
8951169689Skan    case LSHIFTRT:
8952169689Skan      op = DW_OP_shr;
8953169689Skan      goto do_binop;
8954169689Skan
8955169689Skan    do_binop:
895690075Sobrien      {
895790075Sobrien	dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
895890075Sobrien	dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
895950397Sobrien
896090075Sobrien	if (op0 == 0 || op1 == 0)
896190075Sobrien	  break;
896290075Sobrien
896390075Sobrien	mem_loc_result = op0;
896490075Sobrien	add_loc_descr (&mem_loc_result, op1);
8965169689Skan	add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
896690075Sobrien	break;
896790075Sobrien      }
896890075Sobrien
896950397Sobrien    case CONST_INT:
897090075Sobrien      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
897150397Sobrien      break;
897250397Sobrien
897350397Sobrien    default:
8974169689Skan      gcc_unreachable ();
897550397Sobrien    }
897650397Sobrien
897750397Sobrien  return mem_loc_result;
897850397Sobrien}
897950397Sobrien
898050397Sobrien/* Return a descriptor that describes the concatenation of two locations.
898150397Sobrien   This is typically a complex variable.  */
898250397Sobrien
898350397Sobrienstatic dw_loc_descr_ref
8984132718Skanconcat_loc_descriptor (rtx x0, rtx x1)
898550397Sobrien{
898650397Sobrien  dw_loc_descr_ref cc_loc_result = NULL;
898790075Sobrien  dw_loc_descr_ref x0_ref = loc_descriptor (x0);
898890075Sobrien  dw_loc_descr_ref x1_ref = loc_descriptor (x1);
898950397Sobrien
899090075Sobrien  if (x0_ref == 0 || x1_ref == 0)
899190075Sobrien    return 0;
899290075Sobrien
899390075Sobrien  cc_loc_result = x0_ref;
8994169689Skan  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
899550397Sobrien
899690075Sobrien  add_loc_descr (&cc_loc_result, x1_ref);
8997169689Skan  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
899850397Sobrien
899950397Sobrien  return cc_loc_result;
900050397Sobrien}
900150397Sobrien
900250397Sobrien/* Output a proper Dwarf location descriptor for a variable or parameter
900350397Sobrien   which is either allocated in a register or in a memory location.  For a
900450397Sobrien   register, we just generate an OP_REG and the register number.  For a
900550397Sobrien   memory location we provide a Dwarf postfix expression describing how to
900690075Sobrien   generate the (dynamic) address of the object onto the address stack.
900750397Sobrien
900890075Sobrien   If we don't know how to describe it, return 0.  */
900990075Sobrien
901050397Sobrienstatic dw_loc_descr_ref
9011132718Skanloc_descriptor (rtx rtl)
901250397Sobrien{
901350397Sobrien  dw_loc_descr_ref loc_result = NULL;
901490075Sobrien
901550397Sobrien  switch (GET_CODE (rtl))
901650397Sobrien    {
901750397Sobrien    case SUBREG:
901850397Sobrien      /* The case of a subreg may arise when we have a local (register)
9019132718Skan	 variable or a formal (register) parameter which doesn't quite fill
9020132718Skan	 up an entire register.  For now, just assume that it is
9021132718Skan	 legitimate to make the Dwarf info refer to the whole register which
9022132718Skan	 contains the given subreg.  */
902390075Sobrien      rtl = SUBREG_REG (rtl);
902450397Sobrien
902590075Sobrien      /* ... fall through ...  */
902650397Sobrien
902750397Sobrien    case REG:
902850397Sobrien      loc_result = reg_loc_descriptor (rtl);
902950397Sobrien      break;
903050397Sobrien
903150397Sobrien    case MEM:
903290075Sobrien      loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
903350397Sobrien      break;
903450397Sobrien
903550397Sobrien    case CONCAT:
903650397Sobrien      loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
903750397Sobrien      break;
903850397Sobrien
9039169689Skan    case VAR_LOCATION:
9040169689Skan      /* Single part.  */
9041169689Skan      if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9042169689Skan	{
9043169689Skan	  loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9044169689Skan	  break;
9045169689Skan	}
9046169689Skan
9047169689Skan      rtl = XEXP (rtl, 1);
9048169689Skan      /* FALLTHRU */
9049169689Skan
9050169689Skan    case PARALLEL:
9051169689Skan      {
9052169689Skan	rtvec par_elems = XVEC (rtl, 0);
9053169689Skan	int num_elem = GET_NUM_ELEM (par_elems);
9054169689Skan	enum machine_mode mode;
9055169689Skan	int i;
9056169689Skan
9057169689Skan	/* Create the first one, so we have something to add to.  */
9058169689Skan	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9059169689Skan	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9060169689Skan	add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9061169689Skan	for (i = 1; i < num_elem; i++)
9062169689Skan	  {
9063169689Skan	    dw_loc_descr_ref temp;
9064169689Skan
9065169689Skan	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9066169689Skan	    add_loc_descr (&loc_result, temp);
9067169689Skan	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9068169689Skan	    add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9069169689Skan	  }
9070169689Skan      }
9071169689Skan      break;
9072169689Skan
907350397Sobrien    default:
9074169689Skan      gcc_unreachable ();
907550397Sobrien    }
907650397Sobrien
907750397Sobrien  return loc_result;
907850397Sobrien}
907950397Sobrien
908090075Sobrien/* Similar, but generate the descriptor from trees instead of rtl.  This comes
9081169689Skan   up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9082169689Skan   a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9083169689Skan   top-level invocation, and we require the address of LOC; is 0 if we require
9084169689Skan   the value of LOC.  */
908590075Sobrien
908690075Sobrienstatic dw_loc_descr_ref
9087169689Skanloc_descriptor_from_tree_1 (tree loc, int want_address)
908890075Sobrien{
908990075Sobrien  dw_loc_descr_ref ret, ret1;
9090169689Skan  int have_address = 0;
909190075Sobrien  enum dwarf_location_atom op;
909290075Sobrien
909390075Sobrien  /* ??? Most of the time we do not take proper care for sign/zero
909490075Sobrien     extending the values properly.  Hopefully this won't be a real
909590075Sobrien     problem...  */
909690075Sobrien
909790075Sobrien  switch (TREE_CODE (loc))
909890075Sobrien    {
909990075Sobrien    case ERROR_MARK:
910090075Sobrien      return 0;
910190075Sobrien
910290075Sobrien    case PLACEHOLDER_EXPR:
910390075Sobrien      /* This case involves extracting fields from an object to determine the
910490075Sobrien	 position of other fields.  We don't try to encode this here.  The
910590075Sobrien	 only user of this is Ada, which encodes the needed information using
910690075Sobrien	 the names of types.  */
910790075Sobrien      return 0;
910890075Sobrien
910990075Sobrien    case CALL_EXPR:
911090075Sobrien      return 0;
911190075Sobrien
9112132718Skan    case PREINCREMENT_EXPR:
9113132718Skan    case PREDECREMENT_EXPR:
9114132718Skan    case POSTINCREMENT_EXPR:
9115132718Skan    case POSTDECREMENT_EXPR:
9116132718Skan      /* There are no opcodes for these operations.  */
9117132718Skan      return 0;
9118132718Skan
911990075Sobrien    case ADDR_EXPR:
9120169689Skan      /* If we already want an address, there's nothing we can do.  */
9121169689Skan      if (want_address)
9122169689Skan	return 0;
912390075Sobrien
9124169689Skan      /* Otherwise, process the argument and look for the address.  */
9125169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
912690075Sobrien
912790075Sobrien    case VAR_DECL:
9128169689Skan      if (DECL_THREAD_LOCAL_P (loc))
9129117395Skan	{
9130117395Skan	  rtx rtl;
9131117395Skan
9132117395Skan	  /* If this is not defined, we have no way to emit the data.  */
9133169689Skan	  if (!targetm.asm_out.output_dwarf_dtprel)
9134169689Skan	    return 0;
9135117395Skan
9136117395Skan	  /* The way DW_OP_GNU_push_tls_address is specified, we can only
9137117395Skan	     look up addresses of objects in the current module.  */
9138117395Skan	  if (DECL_EXTERNAL (loc))
9139117395Skan	    return 0;
9140117395Skan
9141117395Skan	  rtl = rtl_for_decl_location (loc);
9142117395Skan	  if (rtl == NULL_RTX)
9143117395Skan	    return 0;
9144117395Skan
9145169689Skan	  if (!MEM_P (rtl))
9146117395Skan	    return 0;
9147117395Skan	  rtl = XEXP (rtl, 0);
9148117395Skan	  if (! CONSTANT_P (rtl))
9149117395Skan	    return 0;
9150117395Skan
9151117395Skan	  ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9152117395Skan	  ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9153117395Skan	  ret->dw_loc_oprnd1.v.val_addr = rtl;
9154117395Skan
9155117395Skan	  ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9156117395Skan	  add_loc_descr (&ret, ret1);
9157117395Skan
9158169689Skan	  have_address = 1;
9159117395Skan	  break;
9160117395Skan	}
9161169689Skan      /* FALLTHRU */
9162117395Skan
916390075Sobrien    case PARM_DECL:
9164169689Skan      if (DECL_HAS_VALUE_EXPR_P (loc))
9165169689Skan	return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9166169689Skan					   want_address);
9167169689Skan      /* FALLTHRU */
9168169689Skan
9169169689Skan    case RESULT_DECL:
9170169689Skan    case FUNCTION_DECL:
917190075Sobrien      {
917290075Sobrien	rtx rtl = rtl_for_decl_location (loc);
917390075Sobrien
917490075Sobrien	if (rtl == NULL_RTX)
917590075Sobrien	  return 0;
9176169689Skan        else if (GET_CODE (rtl) == CONST_INT)
9177169689Skan	  {
9178169689Skan	    HOST_WIDE_INT val = INTVAL (rtl);
9179169689Skan	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9180169689Skan	      val &= GET_MODE_MASK (DECL_MODE (loc));
9181169689Skan	    ret = int_loc_descriptor (val);
9182169689Skan	  }
9183169689Skan	else if (GET_CODE (rtl) == CONST_STRING)
9184169689Skan	  return 0;
918590075Sobrien	else if (CONSTANT_P (rtl))
918690075Sobrien	  {
918790075Sobrien	    ret = new_loc_descr (DW_OP_addr, 0, 0);
918890075Sobrien	    ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
918990075Sobrien	    ret->dw_loc_oprnd1.v.val_addr = rtl;
919090075Sobrien	  }
919190075Sobrien	else
919290075Sobrien	  {
9193169689Skan	    enum machine_mode mode;
919490075Sobrien
9195169689Skan	    /* Certain constructs can only be represented at top-level.  */
9196169689Skan	    if (want_address == 2)
9197169689Skan	      return loc_descriptor (rtl);
9198169689Skan
9199169689Skan	    mode = GET_MODE (rtl);
9200169689Skan	    if (MEM_P (rtl))
920190075Sobrien	      {
920290075Sobrien		rtl = XEXP (rtl, 0);
9203169689Skan		have_address = 1;
920490075Sobrien	      }
920590075Sobrien	    ret = mem_loc_descriptor (rtl, mode);
920690075Sobrien	  }
920790075Sobrien      }
920890075Sobrien      break;
920990075Sobrien
921090075Sobrien    case INDIRECT_REF:
9211169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9212169689Skan      have_address = 1;
921390075Sobrien      break;
921490075Sobrien
921590075Sobrien    case COMPOUND_EXPR:
9216169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
921790075Sobrien
921890075Sobrien    case NOP_EXPR:
921990075Sobrien    case CONVERT_EXPR:
922090075Sobrien    case NON_LVALUE_EXPR:
922190075Sobrien    case VIEW_CONVERT_EXPR:
922290075Sobrien    case SAVE_EXPR:
9223132718Skan    case MODIFY_EXPR:
9224169689Skan      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
922590075Sobrien
922690075Sobrien    case COMPONENT_REF:
922790075Sobrien    case BIT_FIELD_REF:
922890075Sobrien    case ARRAY_REF:
922990075Sobrien    case ARRAY_RANGE_REF:
923090075Sobrien      {
923190075Sobrien	tree obj, offset;
923290075Sobrien	HOST_WIDE_INT bitsize, bitpos, bytepos;
923390075Sobrien	enum machine_mode mode;
923490075Sobrien	int volatilep;
9235169689Skan	int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
923690075Sobrien
923790075Sobrien	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9238169689Skan				   &unsignedp, &volatilep, false);
923990075Sobrien
924090075Sobrien	if (obj == loc)
924190075Sobrien	  return 0;
924290075Sobrien
9243169689Skan	ret = loc_descriptor_from_tree_1 (obj, 1);
924490075Sobrien	if (ret == 0
924590075Sobrien	    || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
924690075Sobrien	  return 0;
924790075Sobrien
924890075Sobrien	if (offset != NULL_TREE)
924990075Sobrien	  {
925090075Sobrien	    /* Variable offset.  */
9251169689Skan	    add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
925290075Sobrien	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
925390075Sobrien	  }
925490075Sobrien
925590075Sobrien	bytepos = bitpos / BITS_PER_UNIT;
925690075Sobrien	if (bytepos > 0)
925790075Sobrien	  add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
925890075Sobrien	else if (bytepos < 0)
925990075Sobrien	  {
926090075Sobrien	    add_loc_descr (&ret, int_loc_descriptor (bytepos));
926190075Sobrien	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
926290075Sobrien	  }
9263169689Skan
9264169689Skan	have_address = 1;
926590075Sobrien	break;
926690075Sobrien      }
926790075Sobrien
926890075Sobrien    case INTEGER_CST:
926990075Sobrien      if (host_integerp (loc, 0))
927090075Sobrien	ret = int_loc_descriptor (tree_low_cst (loc, 0));
927190075Sobrien      else
927290075Sobrien	return 0;
927390075Sobrien      break;
927490075Sobrien
9275132718Skan    case CONSTRUCTOR:
9276132718Skan      {
9277132718Skan	/* Get an RTL for this, if something has been emitted.  */
9278132718Skan	rtx rtl = lookup_constant_def (loc);
9279132718Skan	enum machine_mode mode;
9280132718Skan
9281169689Skan	if (!rtl || !MEM_P (rtl))
9282132718Skan	  return 0;
9283132718Skan	mode = GET_MODE (rtl);
9284132718Skan	rtl = XEXP (rtl, 0);
9285132718Skan	ret = mem_loc_descriptor (rtl, mode);
9286169689Skan	have_address = 1;
9287132718Skan	break;
9288132718Skan      }
9289132718Skan
9290117395Skan    case TRUTH_AND_EXPR:
929190075Sobrien    case TRUTH_ANDIF_EXPR:
929290075Sobrien    case BIT_AND_EXPR:
929390075Sobrien      op = DW_OP_and;
929490075Sobrien      goto do_binop;
929590075Sobrien
929690075Sobrien    case TRUTH_XOR_EXPR:
929790075Sobrien    case BIT_XOR_EXPR:
929890075Sobrien      op = DW_OP_xor;
929990075Sobrien      goto do_binop;
930090075Sobrien
930190075Sobrien    case TRUTH_OR_EXPR:
930290075Sobrien    case TRUTH_ORIF_EXPR:
930390075Sobrien    case BIT_IOR_EXPR:
930490075Sobrien      op = DW_OP_or;
930590075Sobrien      goto do_binop;
930690075Sobrien
9307132718Skan    case FLOOR_DIV_EXPR:
9308132718Skan    case CEIL_DIV_EXPR:
9309132718Skan    case ROUND_DIV_EXPR:
931090075Sobrien    case TRUNC_DIV_EXPR:
931190075Sobrien      op = DW_OP_div;
931290075Sobrien      goto do_binop;
931390075Sobrien
931490075Sobrien    case MINUS_EXPR:
931590075Sobrien      op = DW_OP_minus;
931690075Sobrien      goto do_binop;
931790075Sobrien
9318132718Skan    case FLOOR_MOD_EXPR:
9319132718Skan    case CEIL_MOD_EXPR:
9320132718Skan    case ROUND_MOD_EXPR:
932190075Sobrien    case TRUNC_MOD_EXPR:
932290075Sobrien      op = DW_OP_mod;
932390075Sobrien      goto do_binop;
932490075Sobrien
932590075Sobrien    case MULT_EXPR:
932690075Sobrien      op = DW_OP_mul;
932790075Sobrien      goto do_binop;
932890075Sobrien
932990075Sobrien    case LSHIFT_EXPR:
933090075Sobrien      op = DW_OP_shl;
933190075Sobrien      goto do_binop;
933290075Sobrien
933390075Sobrien    case RSHIFT_EXPR:
9334169689Skan      op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
933590075Sobrien      goto do_binop;
933690075Sobrien
933790075Sobrien    case PLUS_EXPR:
933890075Sobrien      if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
933990075Sobrien	  && host_integerp (TREE_OPERAND (loc, 1), 0))
934090075Sobrien	{
9341169689Skan	  ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
934290075Sobrien	  if (ret == 0)
934390075Sobrien	    return 0;
934490075Sobrien
934590075Sobrien	  add_loc_descr (&ret,
934690075Sobrien			 new_loc_descr (DW_OP_plus_uconst,
934790075Sobrien					tree_low_cst (TREE_OPERAND (loc, 1),
934890075Sobrien						      0),
934990075Sobrien					0));
935090075Sobrien	  break;
935190075Sobrien	}
935290075Sobrien
935390075Sobrien      op = DW_OP_plus;
935490075Sobrien      goto do_binop;
935590075Sobrien
935690075Sobrien    case LE_EXPR:
9357169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
935890075Sobrien	return 0;
935990075Sobrien
936090075Sobrien      op = DW_OP_le;
936190075Sobrien      goto do_binop;
936290075Sobrien
936390075Sobrien    case GE_EXPR:
9364169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
936590075Sobrien	return 0;
936690075Sobrien
936790075Sobrien      op = DW_OP_ge;
936890075Sobrien      goto do_binop;
936990075Sobrien
937090075Sobrien    case LT_EXPR:
9371169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
937290075Sobrien	return 0;
937390075Sobrien
937490075Sobrien      op = DW_OP_lt;
937590075Sobrien      goto do_binop;
937690075Sobrien
937790075Sobrien    case GT_EXPR:
9378169689Skan      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
937990075Sobrien	return 0;
938090075Sobrien
938190075Sobrien      op = DW_OP_gt;
938290075Sobrien      goto do_binop;
938390075Sobrien
938490075Sobrien    case EQ_EXPR:
938590075Sobrien      op = DW_OP_eq;
938690075Sobrien      goto do_binop;
938790075Sobrien
938890075Sobrien    case NE_EXPR:
938990075Sobrien      op = DW_OP_ne;
939090075Sobrien      goto do_binop;
939190075Sobrien
939290075Sobrien    do_binop:
9393169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9394169689Skan      ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
939590075Sobrien      if (ret == 0 || ret1 == 0)
939690075Sobrien	return 0;
939790075Sobrien
939890075Sobrien      add_loc_descr (&ret, ret1);
939990075Sobrien      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
940090075Sobrien      break;
940190075Sobrien
940290075Sobrien    case TRUTH_NOT_EXPR:
940390075Sobrien    case BIT_NOT_EXPR:
940490075Sobrien      op = DW_OP_not;
940590075Sobrien      goto do_unop;
940690075Sobrien
940790075Sobrien    case ABS_EXPR:
940890075Sobrien      op = DW_OP_abs;
940990075Sobrien      goto do_unop;
941090075Sobrien
941190075Sobrien    case NEGATE_EXPR:
941290075Sobrien      op = DW_OP_neg;
941390075Sobrien      goto do_unop;
941490075Sobrien
941590075Sobrien    do_unop:
9416169689Skan      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
941790075Sobrien      if (ret == 0)
941890075Sobrien	return 0;
941990075Sobrien
942090075Sobrien      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
942190075Sobrien      break;
942290075Sobrien
9423169689Skan    case MIN_EXPR:
942490075Sobrien    case MAX_EXPR:
9425169689Skan      {
9426169689Skan        const enum tree_code code =
9427169689Skan          TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
942890075Sobrien
9429169689Skan        loc = build3 (COND_EXPR, TREE_TYPE (loc),
9430169689Skan		      build2 (code, integer_type_node,
9431169689Skan			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9432169689Skan                      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9433169689Skan      }
9434169689Skan
943590075Sobrien      /* ... fall through ...  */
943690075Sobrien
943790075Sobrien    case COND_EXPR:
943890075Sobrien      {
943990075Sobrien	dw_loc_descr_ref lhs
9440169689Skan	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
944190075Sobrien	dw_loc_descr_ref rhs
9442169689Skan	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
944390075Sobrien	dw_loc_descr_ref bra_node, jump_node, tmp;
944490075Sobrien
9445169689Skan	ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
944690075Sobrien	if (ret == 0 || lhs == 0 || rhs == 0)
944790075Sobrien	  return 0;
944890075Sobrien
944990075Sobrien	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
945090075Sobrien	add_loc_descr (&ret, bra_node);
945190075Sobrien
945290075Sobrien	add_loc_descr (&ret, rhs);
945390075Sobrien	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
945490075Sobrien	add_loc_descr (&ret, jump_node);
945590075Sobrien
945690075Sobrien	add_loc_descr (&ret, lhs);
945790075Sobrien	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
945890075Sobrien	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
945990075Sobrien
946090075Sobrien	/* ??? Need a node to point the skip at.  Use a nop.  */
946190075Sobrien	tmp = new_loc_descr (DW_OP_nop, 0, 0);
946290075Sobrien	add_loc_descr (&ret, tmp);
946390075Sobrien	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
946490075Sobrien	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
946590075Sobrien      }
946690075Sobrien      break;
946790075Sobrien
9468146895Skan    case FIX_TRUNC_EXPR:
9469146895Skan    case FIX_CEIL_EXPR:
9470146895Skan    case FIX_FLOOR_EXPR:
9471146895Skan    case FIX_ROUND_EXPR:
9472146895Skan      return 0;
9473146895Skan
947490075Sobrien    default:
9475132718Skan      /* Leave front-end specific codes as simply unknown.  This comes
9476132718Skan	 up, for instance, with the C STMT_EXPR.  */
9477132718Skan      if ((unsigned int) TREE_CODE (loc)
9478132718Skan          >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9479132718Skan	return 0;
9480132718Skan
9481146895Skan#ifdef ENABLE_CHECKING
9482132718Skan      /* Otherwise this is a generic code; we should just lists all of
9483169689Skan	 these explicitly.  We forgot one.  */
9484169689Skan      gcc_unreachable ();
9485146895Skan#else
9486146895Skan      /* In a release build, we want to degrade gracefully: better to
9487146895Skan	 generate incomplete debugging information than to crash.  */
9488146895Skan      return NULL;
9489146895Skan#endif
949090075Sobrien    }
949190075Sobrien
949290075Sobrien  /* Show if we can't fill the request for an address.  */
9493169689Skan  if (want_address && !have_address)
949490075Sobrien    return 0;
949590075Sobrien
949690075Sobrien  /* If we've got an address and don't want one, dereference.  */
9497169689Skan  if (!want_address && have_address && ret)
949890075Sobrien    {
949990075Sobrien      HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
950090075Sobrien
950190075Sobrien      if (size > DWARF2_ADDR_SIZE || size == -1)
950290075Sobrien	return 0;
950390075Sobrien      else if (size == DWARF2_ADDR_SIZE)
950490075Sobrien	op = DW_OP_deref;
950590075Sobrien      else
950690075Sobrien	op = DW_OP_deref_size;
950790075Sobrien
950890075Sobrien      add_loc_descr (&ret, new_loc_descr (op, size, 0));
950990075Sobrien    }
951090075Sobrien
951190075Sobrien  return ret;
951290075Sobrien}
951390075Sobrien
9514169689Skanstatic inline dw_loc_descr_ref
9515169689Skanloc_descriptor_from_tree (tree loc)
9516169689Skan{
9517169689Skan  return loc_descriptor_from_tree_1 (loc, 2);
9518169689Skan}
9519169689Skan
952090075Sobrien/* Given a value, round it up to the lowest multiple of `boundary'
952150397Sobrien   which is not less than the value itself.  */
952250397Sobrien
952390075Sobrienstatic inline HOST_WIDE_INT
9524132718Skanceiling (HOST_WIDE_INT value, unsigned int boundary)
952550397Sobrien{
952650397Sobrien  return (((value + boundary - 1) / boundary) * boundary);
952750397Sobrien}
952850397Sobrien
952950397Sobrien/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
953050397Sobrien   pointer to the declared type for the relevant field variable, or return
953150397Sobrien   `integer_type_node' if the given node turns out to be an
953250397Sobrien   ERROR_MARK node.  */
953350397Sobrien
953450397Sobrienstatic inline tree
9535132718Skanfield_type (tree decl)
953650397Sobrien{
953790075Sobrien  tree type;
953850397Sobrien
953950397Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
954050397Sobrien    return integer_type_node;
954150397Sobrien
954250397Sobrien  type = DECL_BIT_FIELD_TYPE (decl);
954350397Sobrien  if (type == NULL_TREE)
954450397Sobrien    type = TREE_TYPE (decl);
954550397Sobrien
954650397Sobrien  return type;
954750397Sobrien}
954850397Sobrien
954990075Sobrien/* Given a pointer to a tree node, return the alignment in bits for
955090075Sobrien   it, or else return BITS_PER_WORD if the node actually turns out to
955190075Sobrien   be an ERROR_MARK node.  */
955250397Sobrien
955350397Sobrienstatic inline unsigned
9554132718Skansimple_type_align_in_bits (tree type)
955550397Sobrien{
955650397Sobrien  return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
955750397Sobrien}
955850397Sobrien
955990075Sobrienstatic inline unsigned
9560132718Skansimple_decl_align_in_bits (tree decl)
956190075Sobrien{
9562117395Skan  return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
956390075Sobrien}
956490075Sobrien
956590075Sobrien/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
956690075Sobrien   lowest addressed byte of the "containing object" for the given FIELD_DECL,
956790075Sobrien   or return 0 if we are unable to determine what that offset is, either
956890075Sobrien   because the argument turns out to be a pointer to an ERROR_MARK node, or
956990075Sobrien   because the offset is actually variable.  (We can't handle the latter case
957090075Sobrien   just yet).  */
957150397Sobrien
957290075Sobrienstatic HOST_WIDE_INT
9573132718Skanfield_byte_offset (tree decl)
957450397Sobrien{
957590075Sobrien  unsigned int type_align_in_bits;
957690075Sobrien  unsigned int decl_align_in_bits;
957790075Sobrien  unsigned HOST_WIDE_INT type_size_in_bits;
957890075Sobrien  HOST_WIDE_INT object_offset_in_bits;
957990075Sobrien  tree type;
958090075Sobrien  tree field_size_tree;
958190075Sobrien  HOST_WIDE_INT bitpos_int;
958290075Sobrien  HOST_WIDE_INT deepest_bitpos;
958390075Sobrien  unsigned HOST_WIDE_INT field_size_in_bits;
958450397Sobrien
958550397Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
958650397Sobrien    return 0;
958750397Sobrien
9588169689Skan  gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9589169689Skan
959050397Sobrien  type = field_type (decl);
959150397Sobrien  field_size_tree = DECL_SIZE (decl);
959250397Sobrien
959390075Sobrien  /* The size could be unspecified if there was an error, or for
959490075Sobrien     a flexible array member.  */
959590075Sobrien  if (! field_size_tree)
959690075Sobrien    field_size_tree = bitsize_zero_node;
959790075Sobrien
959890075Sobrien  /* We cannot yet cope with fields whose positions are variable, so
959950397Sobrien     for now, when we see such things, we simply return 0.  Someday, we may
960050397Sobrien     be able to handle such cases, but it will be damn difficult.  */
960190075Sobrien  if (! host_integerp (bit_position (decl), 0))
960250397Sobrien    return 0;
960350397Sobrien
960490075Sobrien  bitpos_int = int_bit_position (decl);
960550397Sobrien
960690075Sobrien  /* If we don't know the size of the field, pretend it's a full word.  */
960790075Sobrien  if (host_integerp (field_size_tree, 1))
960890075Sobrien    field_size_in_bits = tree_low_cst (field_size_tree, 1);
960990075Sobrien  else
961090075Sobrien    field_size_in_bits = BITS_PER_WORD;
961190075Sobrien
961250397Sobrien  type_size_in_bits = simple_type_size_in_bits (type);
961350397Sobrien  type_align_in_bits = simple_type_align_in_bits (type);
9614117395Skan  decl_align_in_bits = simple_decl_align_in_bits (decl);
961550397Sobrien
961690075Sobrien  /* The GCC front-end doesn't make any attempt to keep track of the starting
961790075Sobrien     bit offset (relative to the start of the containing structure type) of the
961890075Sobrien     hypothetical "containing object" for a bit-field.  Thus, when computing
961990075Sobrien     the byte offset value for the start of the "containing object" of a
962090075Sobrien     bit-field, we must deduce this information on our own. This can be rather
962190075Sobrien     tricky to do in some cases.  For example, handling the following structure
962290075Sobrien     type definition when compiling for an i386/i486 target (which only aligns
962390075Sobrien     long long's to 32-bit boundaries) can be very tricky:
962450397Sobrien
962550397Sobrien	 struct S { int field1; long long field2:31; };
962650397Sobrien
962790075Sobrien     Fortunately, there is a simple rule-of-thumb which can be used in such
962890075Sobrien     cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
962990075Sobrien     structure shown above.  It decides to do this based upon one simple rule
963090075Sobrien     for bit-field allocation.  GCC allocates each "containing object" for each
963190075Sobrien     bit-field at the first (i.e. lowest addressed) legitimate alignment
963290075Sobrien     boundary (based upon the required minimum alignment for the declared type
963390075Sobrien     of the field) which it can possibly use, subject to the condition that
963490075Sobrien     there is still enough available space remaining in the containing object
963590075Sobrien     (when allocated at the selected point) to fully accommodate all of the
963690075Sobrien     bits of the bit-field itself.
963750397Sobrien
963890075Sobrien     This simple rule makes it obvious why GCC allocates 8 bytes for each
963990075Sobrien     object of the structure type shown above.  When looking for a place to
964090075Sobrien     allocate the "containing object" for `field2', the compiler simply tries
964190075Sobrien     to allocate a 64-bit "containing object" at each successive 32-bit
964290075Sobrien     boundary (starting at zero) until it finds a place to allocate that 64-
964390075Sobrien     bit field such that at least 31 contiguous (and previously unallocated)
964490075Sobrien     bits remain within that selected 64 bit field.  (As it turns out, for the
964590075Sobrien     example above, the compiler finds it is OK to allocate the "containing
964690075Sobrien     object" 64-bit field at bit-offset zero within the structure type.)
964790075Sobrien
964890075Sobrien     Here we attempt to work backwards from the limited set of facts we're
964990075Sobrien     given, and we try to deduce from those facts, where GCC must have believed
965090075Sobrien     that the containing object started (within the structure type). The value
965190075Sobrien     we deduce is then used (by the callers of this routine) to generate
965290075Sobrien     DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
965390075Sobrien     and, in the case of DW_AT_location, regular fields as well).  */
965490075Sobrien
965550397Sobrien  /* Figure out the bit-distance from the start of the structure to the
965650397Sobrien     "deepest" bit of the bit-field.  */
965750397Sobrien  deepest_bitpos = bitpos_int + field_size_in_bits;
965850397Sobrien
965950397Sobrien  /* This is the tricky part.  Use some fancy footwork to deduce where the
966050397Sobrien     lowest addressed bit of the containing object must be.  */
966190075Sobrien  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
966250397Sobrien
966390075Sobrien  /* Round up to type_align by default.  This works best for bitfields.  */
966490075Sobrien  object_offset_in_bits += type_align_in_bits - 1;
966590075Sobrien  object_offset_in_bits /= type_align_in_bits;
966690075Sobrien  object_offset_in_bits *= type_align_in_bits;
966750397Sobrien
966890075Sobrien  if (object_offset_in_bits > bitpos_int)
966990075Sobrien    {
967090075Sobrien      /* Sigh, the decl must be packed.  */
967190075Sobrien      object_offset_in_bits = deepest_bitpos - type_size_in_bits;
967250397Sobrien
967390075Sobrien      /* Round up to decl_align instead.  */
967490075Sobrien      object_offset_in_bits += decl_align_in_bits - 1;
967590075Sobrien      object_offset_in_bits /= decl_align_in_bits;
967690075Sobrien      object_offset_in_bits *= decl_align_in_bits;
967790075Sobrien    }
967890075Sobrien
967990075Sobrien  return object_offset_in_bits / BITS_PER_UNIT;
968050397Sobrien}
968150397Sobrien
968250397Sobrien/* The following routines define various Dwarf attributes and any data
968350397Sobrien   associated with them.  */
968450397Sobrien
968550397Sobrien/* Add a location description attribute value to a DIE.
968650397Sobrien
968750397Sobrien   This emits location attributes suitable for whole variables and
968850397Sobrien   whole parameters.  Note that the location attributes for struct fields are
968950397Sobrien   generated by the routine `data_member_location_attribute' below.  */
969050397Sobrien
9691117395Skanstatic inline void
9692132718Skanadd_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9693132718Skan			     dw_loc_descr_ref descr)
969450397Sobrien{
969590075Sobrien  if (descr != 0)
969690075Sobrien    add_AT_loc (die, attr_kind, descr);
969750397Sobrien}
969850397Sobrien
969990075Sobrien/* Attach the specialized form of location attribute used for data members of
970090075Sobrien   struct and union types.  In the special case of a FIELD_DECL node which
970190075Sobrien   represents a bit-field, the "offset" part of this special location
970290075Sobrien   descriptor must indicate the distance in bytes from the lowest-addressed
970390075Sobrien   byte of the containing struct or union type to the lowest-addressed byte of
970490075Sobrien   the "containing object" for the bit-field.  (See the `field_byte_offset'
970590075Sobrien   function above).
970650397Sobrien
970790075Sobrien   For any given bit-field, the "containing object" is a hypothetical object
970890075Sobrien   (of some integral or enum type) within which the given bit-field lives.  The
970990075Sobrien   type of this hypothetical "containing object" is always the same as the
971090075Sobrien   declared type of the individual bit-field itself (for GCC anyway... the
971190075Sobrien   DWARF spec doesn't actually mandate this).  Note that it is the size (in
971290075Sobrien   bytes) of the hypothetical "containing object" which will be given in the
971390075Sobrien   DW_AT_byte_size attribute for this bit-field.  (See the
971490075Sobrien   `byte_size_attribute' function below.)  It is also used when calculating the
971590075Sobrien   value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
971690075Sobrien   function below.)  */
971790075Sobrien
971850397Sobrienstatic void
9719132718Skanadd_data_member_location_attribute (dw_die_ref die, tree decl)
972050397Sobrien{
9721132718Skan  HOST_WIDE_INT offset;
972290075Sobrien  dw_loc_descr_ref loc_descr = 0;
972350397Sobrien
9724169689Skan  if (TREE_CODE (decl) == TREE_BINFO)
972590075Sobrien    {
972690075Sobrien      /* We're working on the TAG_inheritance for a base class.  */
9727169689Skan      if (BINFO_VIRTUAL_P (decl) && is_cxx ())
972890075Sobrien	{
972990075Sobrien	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
973090075Sobrien	     aren't at a fixed offset from all (sub)objects of the same
973190075Sobrien	     type.  We need to extract the appropriate offset from our
973290075Sobrien	     vtable.  The following dwarf expression means
973390075Sobrien
973490075Sobrien	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
973590075Sobrien
973690075Sobrien	     This is specific to the V3 ABI, of course.  */
973790075Sobrien
973890075Sobrien	  dw_loc_descr_ref tmp;
973990075Sobrien
974090075Sobrien	  /* Make a copy of the object address.  */
974190075Sobrien	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
974290075Sobrien	  add_loc_descr (&loc_descr, tmp);
974390075Sobrien
974490075Sobrien	  /* Extract the vtable address.  */
974590075Sobrien	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
974690075Sobrien	  add_loc_descr (&loc_descr, tmp);
974790075Sobrien
974890075Sobrien	  /* Calculate the address of the offset.  */
974990075Sobrien	  offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9750169689Skan	  gcc_assert (offset < 0);
975190075Sobrien
975290075Sobrien	  tmp = int_loc_descriptor (-offset);
975390075Sobrien	  add_loc_descr (&loc_descr, tmp);
975490075Sobrien	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
975590075Sobrien	  add_loc_descr (&loc_descr, tmp);
975690075Sobrien
975790075Sobrien	  /* Extract the offset.  */
975890075Sobrien	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
975990075Sobrien	  add_loc_descr (&loc_descr, tmp);
976090075Sobrien
976190075Sobrien	  /* Add it to the object address.  */
976290075Sobrien	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
976390075Sobrien	  add_loc_descr (&loc_descr, tmp);
976490075Sobrien	}
976590075Sobrien      else
976690075Sobrien	offset = tree_low_cst (BINFO_OFFSET (decl), 0);
976790075Sobrien    }
976850397Sobrien  else
976950397Sobrien    offset = field_byte_offset (decl);
977050397Sobrien
977190075Sobrien  if (! loc_descr)
977290075Sobrien    {
977390075Sobrien      enum dwarf_location_atom op;
977450397Sobrien
977590075Sobrien      /* The DWARF2 standard says that we should assume that the structure
977690075Sobrien	 address is already on the stack, so we can specify a structure field
977790075Sobrien	 address by using DW_OP_plus_uconst.  */
977890075Sobrien
977950397Sobrien#ifdef MIPS_DEBUGGING_INFO
978090075Sobrien      /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
978190075Sobrien	 operator correctly.  It works only if we leave the offset on the
978290075Sobrien	 stack.  */
978390075Sobrien      op = DW_OP_constu;
978450397Sobrien#else
978590075Sobrien      op = DW_OP_plus_uconst;
978650397Sobrien#endif
978750397Sobrien
978890075Sobrien      loc_descr = new_loc_descr (op, offset, 0);
978990075Sobrien    }
979090075Sobrien
979150397Sobrien  add_AT_loc (die, DW_AT_data_member_location, loc_descr);
979250397Sobrien}
979350397Sobrien
9794132718Skan/* Writes integer values to dw_vec_const array.  */
9795132718Skan
9796132718Skanstatic void
9797132718Skaninsert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9798132718Skan{
9799132718Skan  while (size != 0)
9800132718Skan    {
9801132718Skan      *dest++ = val & 0xff;
9802132718Skan      val >>= 8;
9803132718Skan      --size;
9804132718Skan    }
9805132718Skan}
9806132718Skan
9807132718Skan/* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9808132718Skan
9809132718Skanstatic HOST_WIDE_INT
9810132718Skanextract_int (const unsigned char *src, unsigned int size)
9811132718Skan{
9812132718Skan  HOST_WIDE_INT val = 0;
9813132718Skan
9814132718Skan  src += size;
9815132718Skan  while (size != 0)
9816132718Skan    {
9817132718Skan      val <<= 8;
9818132718Skan      val |= *--src & 0xff;
9819132718Skan      --size;
9820132718Skan    }
9821132718Skan  return val;
9822132718Skan}
9823132718Skan
9824132718Skan/* Writes floating point values to dw_vec_const array.  */
9825132718Skan
9826132718Skanstatic void
9827132718Skaninsert_float (rtx rtl, unsigned char *array)
9828132718Skan{
9829132718Skan  REAL_VALUE_TYPE rv;
9830132718Skan  long val[4];
9831132718Skan  int i;
9832132718Skan
9833132718Skan  REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9834132718Skan  real_to_target (val, &rv, GET_MODE (rtl));
9835132718Skan
9836132718Skan  /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9837132718Skan  for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9838132718Skan    {
9839132718Skan      insert_int (val[i], 4, array);
9840132718Skan      array += 4;
9841132718Skan    }
9842132718Skan}
9843132718Skan
9844132718Skan/* Attach a DW_AT_const_value attribute for a variable or a parameter which
984550397Sobrien   does not have a "location" either in memory or in a register.  These
984650397Sobrien   things can arise in GNU C when a constant is passed as an actual parameter
984750397Sobrien   to an inlined function.  They can also arise in C++ where declared
984850397Sobrien   constants do not necessarily get memory "homes".  */
984950397Sobrien
985050397Sobrienstatic void
9851132718Skanadd_const_value_attribute (dw_die_ref die, rtx rtl)
985250397Sobrien{
985350397Sobrien  switch (GET_CODE (rtl))
985450397Sobrien    {
985550397Sobrien    case CONST_INT:
985690075Sobrien      {
985790075Sobrien	HOST_WIDE_INT val = INTVAL (rtl);
9858117395Skan
9859132718Skan	if (val < 0)
9860132718Skan	  add_AT_int (die, DW_AT_const_value, val);
9861169689Skan	else
9862132718Skan	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
986390075Sobrien      }
986450397Sobrien      break;
986550397Sobrien
986650397Sobrien    case CONST_DOUBLE:
986750397Sobrien      /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9868132718Skan	 floating-point constant.  A CONST_DOUBLE is used whenever the
9869132718Skan	 constant requires more than one word in order to be adequately
9870132718Skan	 represented.  We output CONST_DOUBLEs as blocks.  */
987150397Sobrien      {
987290075Sobrien	enum machine_mode mode = GET_MODE (rtl);
987350397Sobrien
9874169689Skan	if (SCALAR_FLOAT_MODE_P (mode))
987550397Sobrien	  {
9876132718Skan	    unsigned int length = GET_MODE_SIZE (mode);
9877132718Skan	    unsigned char *array = ggc_alloc (length);
987850397Sobrien
9879132718Skan	    insert_float (rtl, array);
9880132718Skan	    add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
988150397Sobrien	  }
988250397Sobrien	else
988390075Sobrien	  {
988490075Sobrien	    /* ??? We really should be using HOST_WIDE_INT throughout.  */
9885169689Skan	    gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
988690075Sobrien
988790075Sobrien	    add_AT_long_long (die, DW_AT_const_value,
988890075Sobrien			      CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
988990075Sobrien	  }
989050397Sobrien      }
989150397Sobrien      break;
989250397Sobrien
9893132718Skan    case CONST_VECTOR:
9894132718Skan      {
9895132718Skan	enum machine_mode mode = GET_MODE (rtl);
9896132718Skan	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9897132718Skan	unsigned int length = CONST_VECTOR_NUNITS (rtl);
9898132718Skan	unsigned char *array = ggc_alloc (length * elt_size);
9899132718Skan	unsigned int i;
9900132718Skan	unsigned char *p;
9901132718Skan
9902169689Skan	switch (GET_MODE_CLASS (mode))
9903132718Skan	  {
9904169689Skan	  case MODE_VECTOR_INT:
9905132718Skan	    for (i = 0, p = array; i < length; i++, p += elt_size)
9906132718Skan	      {
9907132718Skan		rtx elt = CONST_VECTOR_ELT (rtl, i);
9908132718Skan		HOST_WIDE_INT lo, hi;
9909169689Skan
9910169689Skan		switch (GET_CODE (elt))
9911132718Skan		  {
9912169689Skan		  case CONST_INT:
9913132718Skan		    lo = INTVAL (elt);
9914132718Skan		    hi = -(lo < 0);
9915169689Skan		    break;
9916169689Skan
9917169689Skan		  case CONST_DOUBLE:
9918132718Skan		    lo = CONST_DOUBLE_LOW (elt);
9919132718Skan		    hi = CONST_DOUBLE_HIGH (elt);
9920169689Skan		    break;
9921169689Skan
9922169689Skan		  default:
9923169689Skan		    gcc_unreachable ();
9924132718Skan		  }
9925132718Skan
9926132718Skan		if (elt_size <= sizeof (HOST_WIDE_INT))
9927132718Skan		  insert_int (lo, elt_size, p);
9928169689Skan		else
9929132718Skan		  {
9930132718Skan		    unsigned char *p0 = p;
9931132718Skan		    unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9932132718Skan
9933169689Skan		    gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9934132718Skan		    if (WORDS_BIG_ENDIAN)
9935132718Skan		      {
9936132718Skan			p0 = p1;
9937132718Skan			p1 = p;
9938132718Skan		      }
9939132718Skan		    insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9940132718Skan		    insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9941132718Skan		  }
9942132718Skan	      }
9943169689Skan	    break;
9944169689Skan
9945169689Skan	  case MODE_VECTOR_FLOAT:
9946132718Skan	    for (i = 0, p = array; i < length; i++, p += elt_size)
9947132718Skan	      {
9948132718Skan		rtx elt = CONST_VECTOR_ELT (rtl, i);
9949132718Skan		insert_float (elt, p);
9950132718Skan	      }
9951169689Skan	    break;
9952169689Skan
9953169689Skan	  default:
9954169689Skan	    gcc_unreachable ();
9955132718Skan	  }
9956132718Skan
9957132718Skan	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9958132718Skan      }
9959132718Skan      break;
9960132718Skan
996150397Sobrien    case CONST_STRING:
996250397Sobrien      add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
996350397Sobrien      break;
996450397Sobrien
996550397Sobrien    case SYMBOL_REF:
996650397Sobrien    case LABEL_REF:
996750397Sobrien    case CONST:
996890075Sobrien      add_AT_addr (die, DW_AT_const_value, rtl);
9969169689Skan      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
997050397Sobrien      break;
997150397Sobrien
997250397Sobrien    case PLUS:
997350397Sobrien      /* In cases where an inlined instance of an inline function is passed
9974132718Skan	 the address of an `auto' variable (which is local to the caller) we
9975132718Skan	 can get a situation where the DECL_RTL of the artificial local
9976132718Skan	 variable (for the inlining) which acts as a stand-in for the
9977132718Skan	 corresponding formal parameter (of the inline function) will look
9978132718Skan	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
9979132718Skan	 exactly a compile-time constant expression, but it isn't the address
9980132718Skan	 of the (artificial) local variable either.  Rather, it represents the
9981132718Skan	 *value* which the artificial local variable always has during its
9982132718Skan	 lifetime.  We currently have no way to represent such quasi-constant
9983132718Skan	 values in Dwarf, so for now we just punt and generate nothing.  */
998450397Sobrien      break;
998550397Sobrien
998650397Sobrien    default:
998750397Sobrien      /* No other kinds of rtx should be possible here.  */
9988169689Skan      gcc_unreachable ();
998950397Sobrien    }
999050397Sobrien
999150397Sobrien}
999250397Sobrien
9993169689Skan/* Determine whether the evaluation of EXPR references any variables
9994169689Skan   or functions which aren't otherwise used (and therefore may not be
9995169689Skan   output).  */
9996169689Skanstatic tree
9997169689Skanreference_to_unused (tree * tp, int * walk_subtrees,
9998169689Skan		     void * data ATTRIBUTE_UNUSED)
9999169689Skan{
10000169689Skan  if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10001169689Skan    *walk_subtrees = 0;
10002169689Skan
10003169689Skan  if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10004169689Skan      && ! TREE_ASM_WRITTEN (*tp))
10005169689Skan    return *tp;
10006169689Skan  else if (!flag_unit_at_a_time)
10007169689Skan    return NULL_TREE;
10008169689Skan  else if (!cgraph_global_info_ready
10009169689Skan	   && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10010195815Sbms    return *tp;
10011169689Skan  else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10012169689Skan    {
10013169689Skan      struct cgraph_varpool_node *node = cgraph_varpool_node (*tp);
10014169689Skan      if (!node->needed)
10015169689Skan	return *tp;
10016169689Skan    }
10017169689Skan   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10018169689Skan	    && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10019169689Skan    {
10020169689Skan      struct cgraph_node *node = cgraph_node (*tp);
10021169689Skan      if (!node->output)
10022169689Skan        return *tp;
10023169689Skan    }
10024169689Skan
10025169689Skan  return NULL_TREE;
10026169689Skan}
10027169689Skan
10028169689Skan/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10029169689Skan   for use in a later add_const_value_attribute call.  */
10030169689Skan
1003190075Sobrienstatic rtx
10032169689Skanrtl_for_decl_init (tree init, tree type)
10033169689Skan{
10034169689Skan  rtx rtl = NULL_RTX;
10035169689Skan
10036169689Skan  /* If a variable is initialized with a string constant without embedded
10037169689Skan     zeros, build CONST_STRING.  */
10038169689Skan  if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10039169689Skan    {
10040169689Skan      tree enttype = TREE_TYPE (type);
10041169689Skan      tree domain = TYPE_DOMAIN (type);
10042169689Skan      enum machine_mode mode = TYPE_MODE (enttype);
10043169689Skan
10044169689Skan      if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10045169689Skan	  && domain
10046169689Skan	  && integer_zerop (TYPE_MIN_VALUE (domain))
10047169689Skan	  && compare_tree_int (TYPE_MAX_VALUE (domain),
10048169689Skan			       TREE_STRING_LENGTH (init) - 1) == 0
10049169689Skan	  && ((size_t) TREE_STRING_LENGTH (init)
10050169689Skan	      == strlen (TREE_STRING_POINTER (init)) + 1))
10051169689Skan	rtl = gen_rtx_CONST_STRING (VOIDmode,
10052169689Skan				    ggc_strdup (TREE_STRING_POINTER (init)));
10053169689Skan    }
10054169689Skan  /* Other aggregates, and complex values, could be represented using
10055169689Skan     CONCAT: FIXME!  */
10056169689Skan  else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10057169689Skan    ;
10058169689Skan  /* Vectors only work if their mode is supported by the target.
10059169689Skan     FIXME: generic vectors ought to work too.  */
10060169689Skan  else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10061169689Skan    ;
10062169689Skan  /* If the initializer is something that we know will expand into an
10063169689Skan     immediate RTL constant, expand it now.  We must be careful not to
10064169689Skan     reference variables which won't be output.  */
10065169689Skan  else if (initializer_constant_valid_p (init, type)
10066169689Skan	   && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10067169689Skan    {
10068220150Smm      /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10069220150Smm	 possible.  */
10070220150Smm      if (TREE_CODE (type) == VECTOR_TYPE)
10071220150Smm	switch (TREE_CODE (init))
10072220150Smm	  {
10073220150Smm	  case VECTOR_CST:
10074220150Smm	    break;
10075220150Smm	  case CONSTRUCTOR:
10076220150Smm	    if (TREE_CONSTANT (init))
10077220150Smm	      {
10078220150Smm		VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10079220150Smm		bool constant_p = true;
10080220150Smm		tree value;
10081220150Smm		unsigned HOST_WIDE_INT ix;
10082220150Smm
10083220150Smm		/* Even when ctor is constant, it might contain non-*_CST
10084220150Smm		   elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10085220150Smm		   belong into VECTOR_CST nodes.  */
10086220150Smm		FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10087220150Smm		  if (!CONSTANT_CLASS_P (value))
10088220150Smm		    {
10089220150Smm		      constant_p = false;
10090220150Smm		      break;
10091220150Smm		    }
10092220150Smm
10093220150Smm		if (constant_p)
10094220150Smm		  {
10095220150Smm		    init = build_vector_from_ctor (type, elts);
10096220150Smm		    break;
10097220150Smm		  }
10098220150Smm	      }
10099220150Smm	    /* FALLTHRU */
10100220150Smm
10101220150Smm	  default:
10102220150Smm	    return NULL;
10103220150Smm	  }
10104220150Smm
10105169689Skan      rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10106169689Skan
10107169689Skan      /* If expand_expr returns a MEM, it wasn't immediate.  */
10108169689Skan      gcc_assert (!rtl || !MEM_P (rtl));
10109169689Skan    }
10110169689Skan
10111169689Skan  return rtl;
10112169689Skan}
10113169689Skan
10114169689Skan/* Generate RTL for the variable DECL to represent its location.  */
10115169689Skan
10116169689Skanstatic rtx
10117132718Skanrtl_for_decl_location (tree decl)
1011850397Sobrien{
1011990075Sobrien  rtx rtl;
1012050397Sobrien
1012150397Sobrien  /* Here we have to decide where we are going to say the parameter "lives"
1012250397Sobrien     (as far as the debugger is concerned).  We only have a couple of
1012350397Sobrien     choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
1012450397Sobrien
1012590075Sobrien     DECL_RTL normally indicates where the parameter lives during most of the
1012650397Sobrien     activation of the function.  If optimization is enabled however, this
1012790075Sobrien     could be either NULL or else a pseudo-reg.  Both of those cases indicate
1012850397Sobrien     that the parameter doesn't really live anywhere (as far as the code
1012950397Sobrien     generation parts of GCC are concerned) during most of the function's
1013050397Sobrien     activation.  That will happen (for example) if the parameter is never
1013150397Sobrien     referenced within the function.
1013250397Sobrien
1013350397Sobrien     We could just generate a location descriptor here for all non-NULL
1013450397Sobrien     non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
1013550397Sobrien     a little nicer than that if we also consider DECL_INCOMING_RTL in cases
1013650397Sobrien     where DECL_RTL is NULL or is a pseudo-reg.
1013750397Sobrien
1013850397Sobrien     Note however that we can only get away with using DECL_INCOMING_RTL as
1013950397Sobrien     a backup substitute for DECL_RTL in certain limited cases.  In cases
1014050397Sobrien     where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
1014150397Sobrien     we can be sure that the parameter was passed using the same type as it is
1014250397Sobrien     declared to have within the function, and that its DECL_INCOMING_RTL
1014350397Sobrien     points us to a place where a value of that type is passed.
1014450397Sobrien
1014550397Sobrien     In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
1014650397Sobrien     we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
1014750397Sobrien     because in these cases DECL_INCOMING_RTL points us to a value of some
1014850397Sobrien     type which is *different* from the type of the parameter itself.  Thus,
1014950397Sobrien     if we tried to use DECL_INCOMING_RTL to generate a location attribute in
1015050397Sobrien     such cases, the debugger would end up (for example) trying to fetch a
1015150397Sobrien     `float' from a place which actually contains the first part of a
1015250397Sobrien     `double'.  That would lead to really incorrect and confusing
1015350397Sobrien     output at debug-time.
1015450397Sobrien
1015550397Sobrien     So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
1015650397Sobrien     in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
1015750397Sobrien     are a couple of exceptions however.  On little-endian machines we can
1015850397Sobrien     get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
1015950397Sobrien     not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
1016050397Sobrien     an integral type that is smaller than TREE_TYPE (decl). These cases arise
1016150397Sobrien     when (on a little-endian machine) a non-prototyped function has a
1016250397Sobrien     parameter declared to be of type `short' or `char'.  In such cases,
1016350397Sobrien     TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
1016450397Sobrien     be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
1016550397Sobrien     passed `int' value.  If the debugger then uses that address to fetch
1016650397Sobrien     a `short' or a `char' (on a little-endian machine) the result will be
1016750397Sobrien     the correct data, so we allow for such exceptional cases below.
1016850397Sobrien
1016950397Sobrien     Note that our goal here is to describe the place where the given formal
1017090075Sobrien     parameter lives during most of the function's activation (i.e. between the
1017190075Sobrien     end of the prologue and the start of the epilogue).  We'll do that as best
1017290075Sobrien     as we can. Note however that if the given formal parameter is modified
1017390075Sobrien     sometime during the execution of the function, then a stack backtrace (at
1017490075Sobrien     debug-time) will show the function as having been called with the *new*
1017590075Sobrien     value rather than the value which was originally passed in.  This happens
1017690075Sobrien     rarely enough that it is not a major problem, but it *is* a problem, and
1017790075Sobrien     I'd like to fix it.
1017850397Sobrien
1017990075Sobrien     A future version of dwarf2out.c may generate two additional attributes for
1018090075Sobrien     any given DW_TAG_formal_parameter DIE which will describe the "passed
1018190075Sobrien     type" and the "passed location" for the given formal parameter in addition
1018290075Sobrien     to the attributes we now generate to indicate the "declared type" and the
1018390075Sobrien     "active location" for each parameter.  This additional set of attributes
1018490075Sobrien     could be used by debuggers for stack backtraces. Separately, note that
1018590075Sobrien     sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
1018690075Sobrien     This happens (for example) for inlined-instances of inline function formal
1018790075Sobrien     parameters which are never referenced.  This really shouldn't be
1018890075Sobrien     happening.  All PARM_DECL nodes should get valid non-NULL
10189169689Skan     DECL_INCOMING_RTL values.  FIXME.  */
1019050397Sobrien
1019150397Sobrien  /* Use DECL_RTL as the "location" unless we find something better.  */
1019290075Sobrien  rtl = DECL_RTL_IF_SET (decl);
1019350397Sobrien
1019490075Sobrien  /* When generating abstract instances, ignore everything except
10195117395Skan     constants, symbols living in memory, and symbols living in
10196117395Skan     fixed registers.  */
1019790075Sobrien  if (! reload_completed)
1019850397Sobrien    {
1019990075Sobrien      if (rtl
1020090075Sobrien	  && (CONSTANT_P (rtl)
10201169689Skan	      || (MEM_P (rtl)
10202117395Skan	          && CONSTANT_P (XEXP (rtl, 0)))
10203169689Skan	      || (REG_P (rtl)
10204117395Skan	          && TREE_CODE (decl) == VAR_DECL
10205117395Skan		  && TREE_STATIC (decl))))
1020696263Sobrien	{
10207169689Skan	  rtl = targetm.delegitimize_address (rtl);
1020896263Sobrien	  return rtl;
1020996263Sobrien	}
1021090075Sobrien      rtl = NULL_RTX;
1021190075Sobrien    }
1021290075Sobrien  else if (TREE_CODE (decl) == PARM_DECL)
1021390075Sobrien    {
1021450397Sobrien      if (rtl == NULL_RTX || is_pseudo_reg (rtl))
1021550397Sobrien	{
10216146895Skan	  tree declared_type = TREE_TYPE (decl);
10217146895Skan	  tree passed_type = DECL_ARG_TYPE (decl);
10218146895Skan	  enum machine_mode dmode = TYPE_MODE (declared_type);
10219146895Skan	  enum machine_mode pmode = TYPE_MODE (passed_type);
1022050397Sobrien
1022150397Sobrien	  /* This decl represents a formal parameter which was optimized out.
1022250397Sobrien	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
1022390075Sobrien	     all cases where (rtl == NULL_RTX) just below.  */
10224146895Skan	  if (dmode == pmode)
1022550397Sobrien	    rtl = DECL_INCOMING_RTL (decl);
10226146895Skan	  else if (SCALAR_INT_MODE_P (dmode)
10227146895Skan		   && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10228146895Skan		   && DECL_INCOMING_RTL (decl))
10229146895Skan	    {
10230146895Skan	      rtx inc = DECL_INCOMING_RTL (decl);
10231146895Skan	      if (REG_P (inc))
10232146895Skan		rtl = inc;
10233169689Skan	      else if (MEM_P (inc))
10234146895Skan		{
10235146895Skan		  if (BYTES_BIG_ENDIAN)
10236146895Skan		    rtl = adjust_address_nv (inc, dmode,
10237146895Skan					     GET_MODE_SIZE (pmode)
10238146895Skan					     - GET_MODE_SIZE (dmode));
10239146895Skan		  else
10240146895Skan		    rtl = inc;
10241146895Skan		}
10242146895Skan	    }
1024350397Sobrien	}
1024452284Sobrien
1024552284Sobrien      /* If the parm was passed in registers, but lives on the stack, then
1024652284Sobrien	 make a big endian correction if the mode of the type of the
1024752284Sobrien	 parameter is not the same as the mode of the rtl.  */
1024852284Sobrien      /* ??? This is the same series of checks that are made in dbxout.c before
1024952284Sobrien	 we reach the big endian correction code there.  It isn't clear if all
1025052284Sobrien	 of these checks are necessary here, but keeping them all is the safe
1025152284Sobrien	 thing to do.  */
10252169689Skan      else if (MEM_P (rtl)
1025352284Sobrien	       && XEXP (rtl, 0) != const0_rtx
1025452284Sobrien	       && ! CONSTANT_P (XEXP (rtl, 0))
1025552284Sobrien	       /* Not passed in memory.  */
10256169689Skan	       && !MEM_P (DECL_INCOMING_RTL (decl))
1025752284Sobrien	       /* Not passed by invisible reference.  */
10258169689Skan	       && (!REG_P (XEXP (rtl, 0))
1025952284Sobrien		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
1026052284Sobrien		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
1026152284Sobrien#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1026252284Sobrien		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
1026352284Sobrien#endif
1026452284Sobrien		     )
1026552284Sobrien	       /* Big endian correction check.  */
1026652284Sobrien	       && BYTES_BIG_ENDIAN
1026752284Sobrien	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
1026852284Sobrien	       && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
1026952284Sobrien		   < UNITS_PER_WORD))
1027052284Sobrien	{
1027152284Sobrien	  int offset = (UNITS_PER_WORD
1027252284Sobrien			- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
1027390075Sobrien
1027452284Sobrien	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
1027552284Sobrien			     plus_constant (XEXP (rtl, 0), offset));
1027652284Sobrien	}
1027750397Sobrien    }
10278169689Skan  else if (TREE_CODE (decl) == VAR_DECL
10279169689Skan	   && rtl
10280169689Skan	   && MEM_P (rtl)
10281169689Skan	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10282169689Skan	   && BYTES_BIG_ENDIAN)
10283169689Skan    {
10284169689Skan      int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10285169689Skan      int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
1028650397Sobrien
10287169689Skan      /* If a variable is declared "register" yet is smaller than
10288169689Skan	 a register, then if we store the variable to memory, it
10289169689Skan	 looks like we're storing a register-sized value, when in
10290169689Skan	 fact we are not.  We need to adjust the offset of the
10291169689Skan	 storage location to reflect the actual value's bytes,
10292169689Skan	 else gdb will not be able to display it.  */
10293169689Skan      if (rsize > dsize)
10294169689Skan	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10295169689Skan			   plus_constant (XEXP (rtl, 0), rsize-dsize));
1029690075Sobrien    }
1029790075Sobrien
1029890075Sobrien  /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
1029990075Sobrien     and will have been substituted directly into all expressions that use it.
1030090075Sobrien     C does not have such a concept, but C++ and other languages do.  */
10301169689Skan  if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10302169689Skan    rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
1030390075Sobrien
1030496263Sobrien  if (rtl)
10305169689Skan    rtl = targetm.delegitimize_address (rtl);
10306117395Skan
10307117395Skan  /* If we don't look past the constant pool, we risk emitting a
10308117395Skan     reference to a constant pool entry that isn't referenced from
10309117395Skan     code, and thus is not emitted.  */
10310117395Skan  if (rtl)
10311117395Skan    rtl = avoid_constant_pool_reference (rtl);
10312117395Skan
1031390075Sobrien  return rtl;
1031490075Sobrien}
1031590075Sobrien
10316169689Skan/* We need to figure out what section we should use as the base for the
10317169689Skan   address ranges where a given location is valid.
10318169689Skan   1. If this particular DECL has a section associated with it, use that.
10319169689Skan   2. If this function has a section associated with it, use that.
10320169689Skan   3. Otherwise, use the text section.
10321169689Skan   XXX: If you split a variable across multiple sections, we won't notice.  */
10322169689Skan
10323169689Skanstatic const char *
10324169689Skansecname_for_decl (tree decl)
10325169689Skan{
10326169689Skan  const char *secname;
10327169689Skan
10328169689Skan  if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10329169689Skan    {
10330169689Skan      tree sectree = DECL_SECTION_NAME (decl);
10331169689Skan      secname = TREE_STRING_POINTER (sectree);
10332169689Skan    }
10333169689Skan  else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10334169689Skan    {
10335169689Skan      tree sectree = DECL_SECTION_NAME (current_function_decl);
10336169689Skan      secname = TREE_STRING_POINTER (sectree);
10337169689Skan    }
10338169689Skan  else if (cfun && in_cold_section_p)
10339169689Skan    secname = cfun->cold_section_label;
10340169689Skan  else
10341169689Skan    secname = text_section_label;
10342169689Skan
10343169689Skan  return secname;
10344169689Skan}
10345169689Skan
10346132718Skan/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
1034790075Sobrien   data attribute for a variable or a parameter.  We generate the
1034890075Sobrien   DW_AT_const_value attribute only in those cases where the given variable
1034990075Sobrien   or parameter does not have a true "location" either in memory or in a
1035090075Sobrien   register.  This can happen (for example) when a constant is passed as an
1035190075Sobrien   actual argument in a call to an inline function.  (It's possible that
1035290075Sobrien   these things can crop up in other ways also.)  Note that one type of
1035390075Sobrien   constant value which can be passed into an inlined function is a constant
1035490075Sobrien   pointer.  This can happen for example if an actual argument in an inlined
1035590075Sobrien   function call evaluates to a compile-time constant address.  */
1035690075Sobrien
1035790075Sobrienstatic void
10358169689Skanadd_location_or_const_value_attribute (dw_die_ref die, tree decl,
10359169689Skan				       enum dwarf_attribute attr)
1036090075Sobrien{
1036190075Sobrien  rtx rtl;
10362117395Skan  dw_loc_descr_ref descr;
10363169689Skan  var_loc_list *loc_list;
10364169689Skan  struct var_loc_node *node;
1036590075Sobrien  if (TREE_CODE (decl) == ERROR_MARK)
1036690075Sobrien    return;
1036790075Sobrien
10368169689Skan  gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10369169689Skan	      || TREE_CODE (decl) == RESULT_DECL);
10370169689Skan
10371169689Skan  /* See if we possibly have multiple locations for this variable.  */
10372169689Skan  loc_list = lookup_decl_loc (decl);
1037350397Sobrien
10374169689Skan  /* If it truly has multiple locations, the first and last node will
10375169689Skan     differ.  */
10376169689Skan  if (loc_list && loc_list->first != loc_list->last)
1037750397Sobrien    {
10378169689Skan      const char *endname, *secname;
10379169689Skan      dw_loc_list_ref list;
10380169689Skan      rtx varloc;
1038150397Sobrien
10382169689Skan      /* Now that we know what section we are using for a base,
10383169689Skan         actually construct the list of locations.
10384169689Skan	 The first location information is what is passed to the
10385169689Skan	 function that creates the location list, and the remaining
10386169689Skan	 locations just get added on to that list.
10387169689Skan	 Note that we only know the start address for a location
10388169689Skan	 (IE location changes), so to build the range, we use
10389169689Skan	 the range [current location start, next location start].
10390169689Skan	 This means we have to special case the last node, and generate
10391169689Skan	 a range of [last location start, end of function label].  */
1039250397Sobrien
10393169689Skan      node = loc_list->first;
10394169689Skan      varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10395169689Skan      secname = secname_for_decl (decl);
10396132718Skan
10397169689Skan      list = new_loc_list (loc_descriptor (varloc),
10398169689Skan			   node->label, node->next->label, secname, 1);
10399169689Skan      node = node->next;
10400132718Skan
10401169689Skan      for (; node->next; node = node->next)
10402169689Skan	if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10403132718Skan	  {
10404169689Skan	    /* The variable has a location between NODE->LABEL and
10405169689Skan	       NODE->NEXT->LABEL.  */
10406169689Skan	    varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10407169689Skan	    add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10408169689Skan				       node->label, node->next->label, secname);
10409132718Skan	  }
10410132718Skan
10411169689Skan      /* If the variable has a location at the last label
10412169689Skan	 it keeps its location until the end of function.  */
10413169689Skan      if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10414169689Skan	{
10415169689Skan	  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10416169689Skan
10417169689Skan	  varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10418169689Skan	  if (!current_function_decl)
10419169689Skan	    endname = text_end_label;
10420169689Skan	  else
10421169689Skan	    {
10422169689Skan	      ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10423169689Skan					   current_function_funcdef_no);
10424169689Skan	      endname = ggc_strdup (label_id);
10425169689Skan	    }
10426169689Skan	  add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10427169689Skan				     node->label, endname, secname);
10428169689Skan	}
10429169689Skan
10430169689Skan      /* Finally, add the location list to the DIE, and we are done.  */
10431169689Skan      add_AT_loc_list (die, attr, list);
10432169689Skan      return;
1043350397Sobrien    }
10434169689Skan
10435169689Skan  /* Try to get some constant RTL for this decl, and use that as the value of
10436169689Skan     the location.  */
10437169689Skan
10438169689Skan  rtl = rtl_for_decl_location (decl);
10439169689Skan  if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10440169689Skan    {
10441169689Skan      add_const_value_attribute (die, rtl);
10442169689Skan      return;
10443169689Skan    }
10444169689Skan
10445169689Skan  /* If we have tried to generate the location otherwise, and it
10446169689Skan     didn't work out (we wouldn't be here if we did), and we have a one entry
10447169689Skan     location list, try generating a location from that.  */
10448169689Skan  if (loc_list && loc_list->first)
10449169689Skan    {
10450169689Skan      node = loc_list->first;
10451169689Skan      descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10452169689Skan      if (descr)
10453169689Skan	{
10454169689Skan	  add_AT_location_description (die, attr, descr);
10455169689Skan	  return;
10456169689Skan	}
10457169689Skan    }
10458169689Skan
10459169689Skan  /* We couldn't get any rtl, so try directly generating the location
10460169689Skan     description from the tree.  */
10461169689Skan  descr = loc_descriptor_from_tree (decl);
10462169689Skan  if (descr)
10463169689Skan    {
10464169689Skan      add_AT_location_description (die, attr, descr);
10465169689Skan      return;
10466169689Skan    }
10467169689Skan  /* None of that worked, so it must not really have a location;
10468169689Skan     try adding a constant value attribute from the DECL_INITIAL.  */
10469169689Skan  tree_add_const_value_attribute (die, decl);
1047050397Sobrien}
1047150397Sobrien
1047290075Sobrien/* If we don't have a copy of this variable in memory for some reason (such
1047390075Sobrien   as a C++ member constant that doesn't have an out-of-line definition),
1047490075Sobrien   we should tell the debugger about the constant value.  */
1047590075Sobrien
1047690075Sobrienstatic void
10477132718Skantree_add_const_value_attribute (dw_die_ref var_die, tree decl)
1047890075Sobrien{
1047990075Sobrien  tree init = DECL_INITIAL (decl);
1048090075Sobrien  tree type = TREE_TYPE (decl);
10481169689Skan  rtx rtl;
1048290075Sobrien
10483169689Skan  if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
1048490075Sobrien    /* OK */;
1048590075Sobrien  else
1048690075Sobrien    return;
1048790075Sobrien
10488169689Skan  rtl = rtl_for_decl_init (init, type);
10489169689Skan  if (rtl)
10490169689Skan    add_const_value_attribute (var_die, rtl);
10491169689Skan}
10492169689Skan
10493169689Skan/* Convert the CFI instructions for the current function into a
10494169689Skan   location list.  This is used for DW_AT_frame_base when we targeting
10495169689Skan   a dwarf2 consumer that does not support the dwarf3
10496169689Skan   DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10497169689Skan   expressions.  */
10498169689Skan
10499169689Skanstatic dw_loc_list_ref
10500169689Skanconvert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10501169689Skan{
10502169689Skan  dw_fde_ref fde;
10503169689Skan  dw_loc_list_ref list, *list_tail;
10504169689Skan  dw_cfi_ref cfi;
10505169689Skan  dw_cfa_location last_cfa, next_cfa;
10506169689Skan  const char *start_label, *last_label, *section;
10507169689Skan
10508169689Skan  fde = &fde_table[fde_table_in_use - 1];
10509169689Skan
10510169689Skan  section = secname_for_decl (current_function_decl);
10511169689Skan  list_tail = &list;
10512169689Skan  list = NULL;
10513169689Skan
10514169689Skan  next_cfa.reg = INVALID_REGNUM;
10515169689Skan  next_cfa.offset = 0;
10516169689Skan  next_cfa.indirect = 0;
10517169689Skan  next_cfa.base_offset = 0;
10518169689Skan
10519169689Skan  start_label = fde->dw_fde_begin;
10520169689Skan
10521169689Skan  /* ??? Bald assumption that the CIE opcode list does not contain
10522169689Skan     advance opcodes.  */
10523169689Skan  for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10524169689Skan    lookup_cfa_1 (cfi, &next_cfa);
10525169689Skan
10526169689Skan  last_cfa = next_cfa;
10527169689Skan  last_label = start_label;
10528169689Skan
10529169689Skan  for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10530169689Skan    switch (cfi->dw_cfi_opc)
10531169689Skan      {
10532169689Skan      case DW_CFA_set_loc:
10533169689Skan      case DW_CFA_advance_loc1:
10534169689Skan      case DW_CFA_advance_loc2:
10535169689Skan      case DW_CFA_advance_loc4:
10536169689Skan	if (!cfa_equal_p (&last_cfa, &next_cfa))
10537169689Skan	  {
10538169689Skan	    *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10539169689Skan				       start_label, last_label, section,
10540169689Skan				       list == NULL);
10541169689Skan
10542169689Skan	    list_tail = &(*list_tail)->dw_loc_next;
10543169689Skan	    last_cfa = next_cfa;
10544169689Skan	    start_label = last_label;
10545169689Skan	  }
10546169689Skan	last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10547169689Skan	break;
10548169689Skan
10549169689Skan      case DW_CFA_advance_loc:
10550169689Skan	/* The encoding is complex enough that we should never emit this.  */
10551169689Skan      case DW_CFA_remember_state:
10552169689Skan      case DW_CFA_restore_state:
10553169689Skan	/* We don't handle these two in this function.  It would be possible
10554169689Skan	   if it were to be required.  */
10555169689Skan	gcc_unreachable ();
10556169689Skan
10557169689Skan      default:
10558169689Skan	lookup_cfa_1 (cfi, &next_cfa);
10559169689Skan	break;
10560169689Skan      }
10561169689Skan
10562169689Skan  if (!cfa_equal_p (&last_cfa, &next_cfa))
1056390075Sobrien    {
10564169689Skan      *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10565169689Skan				 start_label, last_label, section,
10566169689Skan				 list == NULL);
10567169689Skan      list_tail = &(*list_tail)->dw_loc_next;
10568169689Skan      start_label = last_label;
10569169689Skan    }
10570169689Skan  *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10571169689Skan			     start_label, fde->dw_fde_end, section,
10572169689Skan			     list == NULL);
1057390075Sobrien
10574169689Skan  return list;
10575169689Skan}
10576169689Skan
10577169689Skan/* Compute a displacement from the "steady-state frame pointer" to the
10578169689Skan   frame base (often the same as the CFA), and store it in
10579169689Skan   frame_pointer_fb_offset.  OFFSET is added to the displacement
10580169689Skan   before the latter is negated.  */
10581169689Skan
10582169689Skanstatic void
10583169689Skancompute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10584169689Skan{
10585169689Skan  rtx reg, elim;
10586169689Skan
10587169689Skan#ifdef FRAME_POINTER_CFA_OFFSET
10588169689Skan  reg = frame_pointer_rtx;
10589169689Skan  offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10590169689Skan#else
10591169689Skan  reg = arg_pointer_rtx;
10592169689Skan  offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10593169689Skan#endif
10594169689Skan
10595169689Skan  elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10596169689Skan  if (GET_CODE (elim) == PLUS)
10597169689Skan    {
10598169689Skan      offset += INTVAL (XEXP (elim, 1));
10599169689Skan      elim = XEXP (elim, 0);
1060090075Sobrien    }
10601169689Skan  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10602169689Skan		       : stack_pointer_rtx));
10603169689Skan
10604169689Skan  frame_pointer_fb_offset = -offset;
1060590075Sobrien}
1060690075Sobrien
10607132718Skan/* Generate a DW_AT_name attribute given some string value to be included as
1060850397Sobrien   the value of the attribute.  */
1060950397Sobrien
10610132718Skanstatic void
10611132718Skanadd_name_attribute (dw_die_ref die, const char *name_string)
1061250397Sobrien{
1061350397Sobrien  if (name_string != NULL && *name_string != 0)
1061490075Sobrien    {
1061590075Sobrien      if (demangle_name_func)
1061690075Sobrien	name_string = (*demangle_name_func) (name_string);
1061790075Sobrien
1061890075Sobrien      add_AT_string (die, DW_AT_name, name_string);
1061990075Sobrien    }
1062050397Sobrien}
1062150397Sobrien
10622132718Skan/* Generate a DW_AT_comp_dir attribute for DIE.  */
10623132718Skan
10624132718Skanstatic void
10625132718Skanadd_comp_dir_attribute (dw_die_ref die)
10626132718Skan{
10627132718Skan  const char *wd = get_src_pwd ();
10628132718Skan  if (wd != NULL)
10629132718Skan    add_AT_string (die, DW_AT_comp_dir, wd);
10630132718Skan}
10631132718Skan
1063250397Sobrien/* Given a tree node describing an array bound (either lower or upper) output
1063350397Sobrien   a representation for that bound.  */
1063450397Sobrien
1063550397Sobrienstatic void
10636132718Skanadd_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
1063750397Sobrien{
1063850397Sobrien  switch (TREE_CODE (bound))
1063950397Sobrien    {
1064050397Sobrien    case ERROR_MARK:
1064150397Sobrien      return;
1064250397Sobrien
1064390075Sobrien    /* All fixed-bounds are represented by INTEGER_CST nodes.  */
1064450397Sobrien    case INTEGER_CST:
1064590075Sobrien      if (! host_integerp (bound, 0)
1064690075Sobrien	  || (bound_attr == DW_AT_lower_bound
1064790075Sobrien	      && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
1064890075Sobrien		  || (is_fortran () && integer_onep (bound)))))
10649169689Skan	/* Use the default.  */
1065090075Sobrien	;
1065150397Sobrien      else
1065290075Sobrien	add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
1065350397Sobrien      break;
1065450397Sobrien
1065550397Sobrien    case CONVERT_EXPR:
1065650397Sobrien    case NOP_EXPR:
1065750397Sobrien    case NON_LVALUE_EXPR:
1065890075Sobrien    case VIEW_CONVERT_EXPR:
1065950397Sobrien      add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
1066050397Sobrien      break;
1066190075Sobrien
1066250397Sobrien    case SAVE_EXPR:
1066350397Sobrien      break;
1066450397Sobrien
1066550397Sobrien    case VAR_DECL:
1066690075Sobrien    case PARM_DECL:
10667169689Skan    case RESULT_DECL:
1066890075Sobrien      {
1066990075Sobrien	dw_die_ref decl_die = lookup_decl_die (bound);
1067050397Sobrien
1067190075Sobrien	/* ??? Can this happen, or should the variable have been bound
1067290075Sobrien	   first?  Probably it can, since I imagine that we try to create
1067390075Sobrien	   the types of parameters in the order in which they exist in
1067490075Sobrien	   the list, and won't have created a forward reference to a
1067590075Sobrien	   later parameter.  */
1067690075Sobrien	if (decl_die != NULL)
1067790075Sobrien	  add_AT_die_ref (subrange_die, bound_attr, decl_die);
1067890075Sobrien	break;
1067990075Sobrien      }
1068090075Sobrien
1068150397Sobrien    default:
1068290075Sobrien      {
1068390075Sobrien	/* Otherwise try to create a stack operation procedure to
1068490075Sobrien	   evaluate the value of the array bound.  */
1068590075Sobrien
1068690075Sobrien	dw_die_ref ctx, decl_die;
1068790075Sobrien	dw_loc_descr_ref loc;
1068890075Sobrien
10689169689Skan	loc = loc_descriptor_from_tree (bound);
1069090075Sobrien	if (loc == NULL)
1069190075Sobrien	  break;
1069290075Sobrien
1069390075Sobrien	if (current_function_decl == 0)
1069490075Sobrien	  ctx = comp_unit_die;
1069590075Sobrien	else
1069690075Sobrien	  ctx = lookup_decl_die (current_function_decl);
1069790075Sobrien
1069890075Sobrien	decl_die = new_die (DW_TAG_variable, ctx, bound);
1069990075Sobrien	add_AT_flag (decl_die, DW_AT_artificial, 1);
1070090075Sobrien	add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
1070190075Sobrien	add_AT_loc (decl_die, DW_AT_location, loc);
1070290075Sobrien
1070390075Sobrien	add_AT_die_ref (subrange_die, bound_attr, decl_die);
1070490075Sobrien	break;
1070590075Sobrien      }
1070650397Sobrien    }
1070750397Sobrien}
1070850397Sobrien
1070950397Sobrien/* Note that the block of subscript information for an array type also
1071050397Sobrien   includes information about the element type of type given array type.  */
1071150397Sobrien
1071250397Sobrienstatic void
10713132718Skanadd_subscript_info (dw_die_ref type_die, tree type)
1071450397Sobrien{
1071550397Sobrien#ifndef MIPS_DEBUGGING_INFO
1071690075Sobrien  unsigned dimension_number;
1071750397Sobrien#endif
1071890075Sobrien  tree lower, upper;
1071990075Sobrien  dw_die_ref subrange_die;
1072050397Sobrien
1072190075Sobrien  /* The GNU compilers represent multidimensional array types as sequences of
1072250397Sobrien     one dimensional array types whose element types are themselves array
1072350397Sobrien     types.  Here we squish that down, so that each multidimensional array
1072490075Sobrien     type gets only one array_type DIE in the Dwarf debugging info. The draft
1072550397Sobrien     Dwarf specification say that we are allowed to do this kind of
1072650397Sobrien     compression in C (because there is no difference between an array or
1072790075Sobrien     arrays and a multidimensional array in C) but for other source languages
1072850397Sobrien     (e.g. Ada) we probably shouldn't do this.  */
1072950397Sobrien
1073050397Sobrien  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
1073150397Sobrien     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
1073250397Sobrien     We work around this by disabling this feature.  See also
1073350397Sobrien     gen_array_type_die.  */
1073450397Sobrien#ifndef MIPS_DEBUGGING_INFO
1073550397Sobrien  for (dimension_number = 0;
1073650397Sobrien       TREE_CODE (type) == ARRAY_TYPE;
1073750397Sobrien       type = TREE_TYPE (type), dimension_number++)
1073890075Sobrien#endif
1073950397Sobrien    {
1074090075Sobrien      tree domain = TYPE_DOMAIN (type);
1074150397Sobrien
1074250397Sobrien      /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
1074390075Sobrien	 and (in GNU C only) variable bounds.  Handle all three forms
10744132718Skan	 here.  */
1074590075Sobrien      subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
1074650397Sobrien      if (domain)
1074750397Sobrien	{
1074850397Sobrien	  /* We have an array type with specified bounds.  */
1074950397Sobrien	  lower = TYPE_MIN_VALUE (domain);
1075050397Sobrien	  upper = TYPE_MAX_VALUE (domain);
1075150397Sobrien
10752132718Skan	  /* Define the index type.  */
1075350397Sobrien	  if (TREE_TYPE (domain))
1075450397Sobrien	    {
1075550397Sobrien	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
1075650397Sobrien		 TREE_TYPE field.  We can't emit debug info for this
1075750397Sobrien		 because it is an unnamed integral type.  */
1075850397Sobrien	      if (TREE_CODE (domain) == INTEGER_TYPE
1075950397Sobrien		  && TYPE_NAME (domain) == NULL_TREE
1076050397Sobrien		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
1076150397Sobrien		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
1076290075Sobrien		;
1076350397Sobrien	      else
1076450397Sobrien		add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
1076550397Sobrien				    type_die);
1076650397Sobrien	    }
1076750397Sobrien
1076850397Sobrien	  /* ??? If upper is NULL, the array has unspecified length,
1076950397Sobrien	     but it does have a lower bound.  This happens with Fortran
1077050397Sobrien	       dimension arr(N:*)
10771132718Skan	     Since the debugger is definitely going to need to know N
1077250397Sobrien	     to produce useful results, go ahead and output the lower
1077350397Sobrien	     bound solo, and hope the debugger can cope.  */
1077450397Sobrien
1077550397Sobrien	  add_bound_info (subrange_die, DW_AT_lower_bound, lower);
1077650397Sobrien	  if (upper)
1077750397Sobrien	    add_bound_info (subrange_die, DW_AT_upper_bound, upper);
1077850397Sobrien	}
1077950397Sobrien
1078090075Sobrien      /* Otherwise we have an array type with an unspecified length.  The
1078190075Sobrien	 DWARF-2 spec does not say how to handle this; let's just leave out the
1078290075Sobrien	 bounds.  */
1078350397Sobrien    }
1078450397Sobrien}
1078550397Sobrien
1078650397Sobrienstatic void
10787132718Skanadd_byte_size_attribute (dw_die_ref die, tree tree_node)
1078850397Sobrien{
1078990075Sobrien  unsigned size;
1079050397Sobrien
1079150397Sobrien  switch (TREE_CODE (tree_node))
1079250397Sobrien    {
1079350397Sobrien    case ERROR_MARK:
1079450397Sobrien      size = 0;
1079550397Sobrien      break;
1079650397Sobrien    case ENUMERAL_TYPE:
1079750397Sobrien    case RECORD_TYPE:
1079850397Sobrien    case UNION_TYPE:
1079950397Sobrien    case QUAL_UNION_TYPE:
1080050397Sobrien      size = int_size_in_bytes (tree_node);
1080150397Sobrien      break;
1080250397Sobrien    case FIELD_DECL:
1080350397Sobrien      /* For a data member of a struct or union, the DW_AT_byte_size is
10804132718Skan	 generally given as the number of bytes normally allocated for an
10805132718Skan	 object of the *declared* type of the member itself.  This is true
10806132718Skan	 even for bit-fields.  */
1080750397Sobrien      size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
1080850397Sobrien      break;
1080950397Sobrien    default:
10810169689Skan      gcc_unreachable ();
1081150397Sobrien    }
1081250397Sobrien
1081350397Sobrien  /* Note that `size' might be -1 when we get to this point.  If it is, that
1081450397Sobrien     indicates that the byte size of the entity in question is variable.  We
10815245023Spfg     have no good way of expressing this fact in Dwarf at the present time.
10816245023Spfg     GCC/35998: Avoid passing negative sizes to Dtrace and gdb.  */
10817245023Spfg  add_AT_unsigned (die, DW_AT_byte_size, (size != (unsigned)-1 ? size : 0));
1081850397Sobrien}
1081950397Sobrien
1082050397Sobrien/* For a FIELD_DECL node which represents a bit-field, output an attribute
1082150397Sobrien   which specifies the distance in bits from the highest order bit of the
1082250397Sobrien   "containing object" for the bit-field to the highest order bit of the
1082350397Sobrien   bit-field itself.
1082450397Sobrien
1082590075Sobrien   For any given bit-field, the "containing object" is a hypothetical object
1082690075Sobrien   (of some integral or enum type) within which the given bit-field lives.  The
1082790075Sobrien   type of this hypothetical "containing object" is always the same as the
1082890075Sobrien   declared type of the individual bit-field itself.  The determination of the
1082990075Sobrien   exact location of the "containing object" for a bit-field is rather
1083090075Sobrien   complicated.  It's handled by the `field_byte_offset' function (above).
1083150397Sobrien
1083250397Sobrien   Note that it is the size (in bytes) of the hypothetical "containing object"
1083350397Sobrien   which will be given in the DW_AT_byte_size attribute for this bit-field.
1083450397Sobrien   (See `byte_size_attribute' above).  */
1083550397Sobrien
1083650397Sobrienstatic inline void
10837132718Skanadd_bit_offset_attribute (dw_die_ref die, tree decl)
1083850397Sobrien{
1083990075Sobrien  HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
1084090075Sobrien  tree type = DECL_BIT_FIELD_TYPE (decl);
1084190075Sobrien  HOST_WIDE_INT bitpos_int;
1084290075Sobrien  HOST_WIDE_INT highest_order_object_bit_offset;
1084390075Sobrien  HOST_WIDE_INT highest_order_field_bit_offset;
1084490075Sobrien  HOST_WIDE_INT unsigned bit_offset;
1084550397Sobrien
1084650397Sobrien  /* Must be a field and a bit field.  */
10847169689Skan  gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
1084850397Sobrien
1084950397Sobrien  /* We can't yet handle bit-fields whose offsets are variable, so if we
1085050397Sobrien     encounter such things, just return without generating any attribute
1085190075Sobrien     whatsoever.  Likewise for variable or too large size.  */
1085290075Sobrien  if (! host_integerp (bit_position (decl), 0)
1085390075Sobrien      || ! host_integerp (DECL_SIZE (decl), 1))
1085450397Sobrien    return;
1085550397Sobrien
1085690075Sobrien  bitpos_int = int_bit_position (decl);
1085750397Sobrien
1085850397Sobrien  /* Note that the bit offset is always the distance (in bits) from the
1085990075Sobrien     highest-order bit of the "containing object" to the highest-order bit of
1086090075Sobrien     the bit-field itself.  Since the "high-order end" of any object or field
1086150397Sobrien     is different on big-endian and little-endian machines, the computation
1086250397Sobrien     below must take account of these differences.  */
1086350397Sobrien  highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
1086450397Sobrien  highest_order_field_bit_offset = bitpos_int;
1086550397Sobrien
1086650397Sobrien  if (! BYTES_BIG_ENDIAN)
1086750397Sobrien    {
1086890075Sobrien      highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
1086950397Sobrien      highest_order_object_bit_offset += simple_type_size_in_bits (type);
1087050397Sobrien    }
1087150397Sobrien
1087250397Sobrien  bit_offset
1087350397Sobrien    = (! BYTES_BIG_ENDIAN
1087450397Sobrien       ? highest_order_object_bit_offset - highest_order_field_bit_offset
1087550397Sobrien       : highest_order_field_bit_offset - highest_order_object_bit_offset);
1087650397Sobrien
1087750397Sobrien  add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
1087850397Sobrien}
1087950397Sobrien
1088050397Sobrien/* For a FIELD_DECL node which represents a bit field, output an attribute
1088150397Sobrien   which specifies the length in bits of the given field.  */
1088250397Sobrien
1088350397Sobrienstatic inline void
10884132718Skanadd_bit_size_attribute (dw_die_ref die, tree decl)
1088550397Sobrien{
1088650397Sobrien  /* Must be a field and a bit field.  */
10887169689Skan  gcc_assert (TREE_CODE (decl) == FIELD_DECL
10888169689Skan	      && DECL_BIT_FIELD_TYPE (decl));
1088990075Sobrien
1089090075Sobrien  if (host_integerp (DECL_SIZE (decl), 1))
1089190075Sobrien    add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
1089250397Sobrien}
1089350397Sobrien
1089450397Sobrien/* If the compiled language is ANSI C, then add a 'prototyped'
1089550397Sobrien   attribute, if arg types are given for the parameters of a function.  */
1089650397Sobrien
1089750397Sobrienstatic inline void
10898132718Skanadd_prototyped_attribute (dw_die_ref die, tree func_type)
1089950397Sobrien{
1090050397Sobrien  if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
1090150397Sobrien      && TYPE_ARG_TYPES (func_type) != NULL)
1090250397Sobrien    add_AT_flag (die, DW_AT_prototyped, 1);
1090350397Sobrien}
1090450397Sobrien
1090550397Sobrien/* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
1090650397Sobrien   by looking in either the type declaration or object declaration
1090750397Sobrien   equate table.  */
1090850397Sobrien
1090950397Sobrienstatic inline void
10910132718Skanadd_abstract_origin_attribute (dw_die_ref die, tree origin)
1091150397Sobrien{
1091250397Sobrien  dw_die_ref origin_die = NULL;
1091390075Sobrien
1091490075Sobrien  if (TREE_CODE (origin) != FUNCTION_DECL)
1091590075Sobrien    {
1091690075Sobrien      /* We may have gotten separated from the block for the inlined
1091790075Sobrien	 function, if we're in an exception handler or some such; make
1091890075Sobrien	 sure that the abstract function has been written out.
1091990075Sobrien
10920132718Skan	 Doing this for nested functions is wrong, however; functions are
1092190075Sobrien	 distinct units, and our context might not even be inline.  */
1092290075Sobrien      tree fn = origin;
1092390075Sobrien
1092490075Sobrien      if (TYPE_P (fn))
1092590075Sobrien	fn = TYPE_STUB_DECL (fn);
10926169689Skan
1092790075Sobrien      fn = decl_function_context (fn);
1092890075Sobrien      if (fn)
1092990075Sobrien	dwarf2out_abstract_function (fn);
1093090075Sobrien    }
1093190075Sobrien
1093290075Sobrien  if (DECL_P (origin))
1093350397Sobrien    origin_die = lookup_decl_die (origin);
1093490075Sobrien  else if (TYPE_P (origin))
1093550397Sobrien    origin_die = lookup_type_die (origin);
1093650397Sobrien
10937169689Skan  /* XXX: Functions that are never lowered don't always have correct block
10938169689Skan     trees (in the case of java, they simply have no block tree, in some other
10939169689Skan     languages).  For these functions, there is nothing we can really do to
10940169689Skan     output correct debug info for inlined functions in all cases.  Rather
10941169689Skan     than die, we'll just produce deficient debug info now, in that we will
10942169689Skan     have variables without a proper abstract origin.  In the future, when all
10943169689Skan     functions are lowered, we should re-add a gcc_assert (origin_die)
10944169689Skan     here.  */
1094590075Sobrien
10946169689Skan  if (origin_die)
10947169689Skan      add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
1094850397Sobrien}
1094950397Sobrien
1095050397Sobrien/* We do not currently support the pure_virtual attribute.  */
1095150397Sobrien
1095250397Sobrienstatic inline void
10953132718Skanadd_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
1095450397Sobrien{
1095550397Sobrien  if (DECL_VINDEX (func_decl))
1095650397Sobrien    {
1095750397Sobrien      add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
1095850397Sobrien
1095990075Sobrien      if (host_integerp (DECL_VINDEX (func_decl), 0))
1096090075Sobrien	add_AT_loc (die, DW_AT_vtable_elem_location,
1096190075Sobrien		    new_loc_descr (DW_OP_constu,
1096290075Sobrien				   tree_low_cst (DECL_VINDEX (func_decl), 0),
1096390075Sobrien				   0));
1096490075Sobrien
1096550397Sobrien      /* GNU extension: Record what type this method came from originally.  */
1096650397Sobrien      if (debug_info_level > DINFO_LEVEL_TERSE)
1096750397Sobrien	add_AT_die_ref (die, DW_AT_containing_type,
1096850397Sobrien			lookup_type_die (DECL_CONTEXT (func_decl)));
1096950397Sobrien    }
1097050397Sobrien}
1097150397Sobrien
1097250397Sobrien/* Add source coordinate attributes for the given decl.  */
1097350397Sobrien
1097450397Sobrienstatic void
10975132718Skanadd_src_coords_attributes (dw_die_ref die, tree decl)
1097650397Sobrien{
10977169689Skan  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
1097850397Sobrien
10979169689Skan  add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
10980169689Skan  add_AT_unsigned (die, DW_AT_decl_line, s.line);
1098150397Sobrien}
1098250397Sobrien
10983132718Skan/* Add a DW_AT_name attribute and source coordinate attribute for the
1098450397Sobrien   given decl, but only if it actually has a name.  */
1098550397Sobrien
1098650397Sobrienstatic void
10987132718Skanadd_name_and_src_coords_attributes (dw_die_ref die, tree decl)
1098850397Sobrien{
1098990075Sobrien  tree decl_name;
1099050397Sobrien
1099190075Sobrien  decl_name = DECL_NAME (decl);
1099250397Sobrien  if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
1099350397Sobrien    {
1099450397Sobrien      add_name_attribute (die, dwarf2_name (decl, 0));
1099590075Sobrien      if (! DECL_ARTIFICIAL (decl))
1099690075Sobrien	add_src_coords_attributes (die, decl);
1099790075Sobrien
1099850397Sobrien      if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
1099990075Sobrien	  && TREE_PUBLIC (decl)
1100090075Sobrien	  && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11001169689Skan	  && !DECL_ABSTRACT (decl)
11002169689Skan	  && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
1100350397Sobrien	add_AT_string (die, DW_AT_MIPS_linkage_name,
1100450397Sobrien		       IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
1100550397Sobrien    }
1100690075Sobrien
1100790075Sobrien#ifdef VMS_DEBUGGING_INFO
1100890075Sobrien  /* Get the function's name, as described by its RTL.  This may be different
1100990075Sobrien     from the DECL_NAME name used in the source file.  */
1101090075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
1101190075Sobrien    {
1101290075Sobrien      add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
1101390075Sobrien		   XEXP (DECL_RTL (decl), 0));
11014169689Skan      VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
1101590075Sobrien    }
1101690075Sobrien#endif
1101750397Sobrien}
1101850397Sobrien
1101990075Sobrien/* Push a new declaration scope.  */
1102050397Sobrien
1102150397Sobrienstatic void
11022132718Skanpush_decl_scope (tree scope)
1102350397Sobrien{
11024169689Skan  VEC_safe_push (tree, gc, decl_scope_table, scope);
1102590075Sobrien}
1102650397Sobrien
1102790075Sobrien/* Pop a declaration scope.  */
1102850397Sobrien
1102990075Sobrienstatic inline void
11030132718Skanpop_decl_scope (void)
1103190075Sobrien{
11032169689Skan  VEC_pop (tree, decl_scope_table);
1103350397Sobrien}
1103450397Sobrien
1103590075Sobrien/* Return the DIE for the scope that immediately contains this type.
1103690075Sobrien   Non-named types get global scope.  Named types nested in other
1103790075Sobrien   types get their containing scope if it's open, or global scope
1103890075Sobrien   otherwise.  All other types (i.e. function-local named types) get
1103990075Sobrien   the current active scope.  */
1104050397Sobrien
1104150397Sobrienstatic dw_die_ref
11042132718Skanscope_die_for (tree t, dw_die_ref context_die)
1104350397Sobrien{
1104490075Sobrien  dw_die_ref scope_die = NULL;
1104590075Sobrien  tree containing_scope;
1104690075Sobrien  int i;
1104750397Sobrien
1104890075Sobrien  /* Non-types always go in the current scope.  */
11049169689Skan  gcc_assert (TYPE_P (t));
1105050397Sobrien
1105190075Sobrien  containing_scope = TYPE_CONTEXT (t);
1105290075Sobrien
11053132718Skan  /* Use the containing namespace if it was passed in (for a declaration).  */
1105450397Sobrien  if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11055132718Skan    {
11056132718Skan      if (context_die == lookup_decl_die (containing_scope))
11057132718Skan	/* OK */;
11058132718Skan      else
11059132718Skan	containing_scope = NULL_TREE;
11060132718Skan    }
1106150397Sobrien
1106252284Sobrien  /* Ignore function type "scopes" from the C frontend.  They mean that
1106352284Sobrien     a tagged type is local to a parmlist of a function declarator, but
1106452284Sobrien     that isn't useful to DWARF.  */
1106552284Sobrien  if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
1106652284Sobrien    containing_scope = NULL_TREE;
1106752284Sobrien
11068169689Skan  if (containing_scope == NULL_TREE)
1106950397Sobrien    scope_die = comp_unit_die;
1107090075Sobrien  else if (TYPE_P (containing_scope))
1107150397Sobrien    {
1107290075Sobrien      /* For types, we can just look up the appropriate DIE.  But
1107390075Sobrien	 first we check to see if we're in the middle of emitting it
1107490075Sobrien	 so we know where the new DIE should go.  */
11075169689Skan      for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11076169689Skan	if (VEC_index (tree, decl_scope_table, i) == containing_scope)
1107790075Sobrien	  break;
1107850397Sobrien
1107950397Sobrien      if (i < 0)
1108050397Sobrien	{
11081169689Skan	  gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11082169689Skan		      || TREE_ASM_WRITTEN (containing_scope));
1108352284Sobrien
1108452284Sobrien	  /* If none of the current dies are suitable, we get file scope.  */
1108552284Sobrien	  scope_die = comp_unit_die;
1108650397Sobrien	}
1108790075Sobrien      else
1108890075Sobrien	scope_die = lookup_type_die (containing_scope);
1108950397Sobrien    }
1109090075Sobrien  else
1109190075Sobrien    scope_die = context_die;
1109250397Sobrien
1109350397Sobrien  return scope_die;
1109450397Sobrien}
1109550397Sobrien
1109690075Sobrien/* Returns nonzero if CONTEXT_DIE is internal to a function.  */
1109790075Sobrien
1109890075Sobrienstatic inline int
11099132718Skanlocal_scope_p (dw_die_ref context_die)
1110050397Sobrien{
1110190075Sobrien  for (; context_die; context_die = context_die->die_parent)
1110290075Sobrien    if (context_die->die_tag == DW_TAG_inlined_subroutine
1110390075Sobrien	|| context_die->die_tag == DW_TAG_subprogram)
1110490075Sobrien      return 1;
1110590075Sobrien
1110690075Sobrien  return 0;
1110750397Sobrien}
1110850397Sobrien
11109132718Skan/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11110132718Skan   whether or not to treat a DIE in this context as a declaration.  */
1111190075Sobrien
1111290075Sobrienstatic inline int
11113132718Skanclass_or_namespace_scope_p (dw_die_ref context_die)
1111490075Sobrien{
1111590075Sobrien  return (context_die
1111690075Sobrien	  && (context_die->die_tag == DW_TAG_structure_type
11117132718Skan	      || context_die->die_tag == DW_TAG_union_type
11118132718Skan	      || context_die->die_tag == DW_TAG_namespace));
1111990075Sobrien}
1112090075Sobrien
1112150397Sobrien/* Many forms of DIEs require a "type description" attribute.  This
1112250397Sobrien   routine locates the proper "type descriptor" die for the type given
11123132718Skan   by 'type', and adds a DW_AT_type attribute below the given die.  */
1112450397Sobrien
1112550397Sobrienstatic void
11126132718Skanadd_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11127132718Skan		    int decl_volatile, dw_die_ref context_die)
1112850397Sobrien{
1112990075Sobrien  enum tree_code code  = TREE_CODE (type);
1113090075Sobrien  dw_die_ref type_die  = NULL;
1113150397Sobrien
1113250397Sobrien  /* ??? If this type is an unnamed subrange type of an integral or
1113350397Sobrien     floating-point type, use the inner type.  This is because we have no
1113450397Sobrien     support for unnamed types in base_type_die.  This can happen if this is
1113550397Sobrien     an Ada subrange type.  Correct solution is emit a subrange type die.  */
1113650397Sobrien  if ((code == INTEGER_TYPE || code == REAL_TYPE)
1113750397Sobrien      && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
1113850397Sobrien    type = TREE_TYPE (type), code = TREE_CODE (type);
1113950397Sobrien
1114090075Sobrien  if (code == ERROR_MARK
1114190075Sobrien      /* Handle a special case.  For functions whose return type is void, we
1114290075Sobrien	 generate *no* type attribute.  (Note that no object may have type
1114390075Sobrien	 `void', so this only applies to function return types).  */
1114490075Sobrien      || code == VOID_TYPE)
1114550397Sobrien    return;
1114650397Sobrien
1114750397Sobrien  type_die = modified_type_die (type,
1114850397Sobrien				decl_const || TYPE_READONLY (type),
1114950397Sobrien				decl_volatile || TYPE_VOLATILE (type),
1115050397Sobrien				context_die);
1115190075Sobrien
1115250397Sobrien  if (type_die != NULL)
1115350397Sobrien    add_AT_die_ref (object_die, DW_AT_type, type_die);
1115450397Sobrien}
1115550397Sobrien
11156169689Skan/* Given an object die, add the calling convention attribute for the
11157169689Skan   function call type.  */
11158169689Skanstatic void
11159169689Skanadd_calling_convention_attribute (dw_die_ref subr_die, tree type)
11160169689Skan{
11161169689Skan  enum dwarf_calling_convention value = DW_CC_normal;
11162169689Skan
11163169689Skan  value = targetm.dwarf_calling_convention (type);
11164169689Skan
11165169689Skan  /* Only add the attribute if the backend requests it, and
11166169689Skan     is not DW_CC_normal.  */
11167169689Skan  if (value && (value != DW_CC_normal))
11168169689Skan    add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11169169689Skan}
11170169689Skan
1117150397Sobrien/* Given a tree pointer to a struct, class, union, or enum type node, return
1117250397Sobrien   a pointer to the (string) tag name for the given type, or zero if the type
1117350397Sobrien   was declared without a tag.  */
1117450397Sobrien
1117590075Sobrienstatic const char *
11176132718Skantype_tag (tree type)
1117750397Sobrien{
1117890075Sobrien  const char *name = 0;
1117950397Sobrien
1118050397Sobrien  if (TYPE_NAME (type) != 0)
1118150397Sobrien    {
1118290075Sobrien      tree t = 0;
1118350397Sobrien
1118450397Sobrien      /* Find the IDENTIFIER_NODE for the type name.  */
1118550397Sobrien      if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
1118650397Sobrien	t = TYPE_NAME (type);
1118750397Sobrien
1118890075Sobrien      /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11189132718Skan	 a TYPE_DECL node, regardless of whether or not a `typedef' was
11190132718Skan	 involved.  */
1119150397Sobrien      else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1119250397Sobrien	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
1119350397Sobrien	t = DECL_NAME (TYPE_NAME (type));
1119450397Sobrien
1119550397Sobrien      /* Now get the name as a string, or invent one.  */
1119650397Sobrien      if (t != 0)
1119750397Sobrien	name = IDENTIFIER_POINTER (t);
1119850397Sobrien    }
1119950397Sobrien
1120050397Sobrien  return (name == 0 || *name == '\0') ? 0 : name;
1120150397Sobrien}
1120250397Sobrien
1120350397Sobrien/* Return the type associated with a data member, make a special check
1120450397Sobrien   for bit field types.  */
1120550397Sobrien
1120650397Sobrienstatic inline tree
11207132718Skanmember_declared_type (tree member)
1120850397Sobrien{
1120950397Sobrien  return (DECL_BIT_FIELD_TYPE (member)
1121090075Sobrien	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
1121150397Sobrien}
1121250397Sobrien
1121350397Sobrien/* Get the decl's label, as described by its RTL. This may be different
1121450397Sobrien   from the DECL_NAME name used in the source file.  */
1121550397Sobrien
1121650397Sobrien#if 0
1121790075Sobrienstatic const char *
11218132718Skandecl_start_label (tree decl)
1121950397Sobrien{
1122050397Sobrien  rtx x;
1122190075Sobrien  const char *fnname;
1122290075Sobrien
1122350397Sobrien  x = DECL_RTL (decl);
11224169689Skan  gcc_assert (MEM_P (x));
1122550397Sobrien
1122650397Sobrien  x = XEXP (x, 0);
11227169689Skan  gcc_assert (GET_CODE (x) == SYMBOL_REF);
1122850397Sobrien
1122950397Sobrien  fnname = XSTR (x, 0);
1123050397Sobrien  return fnname;
1123150397Sobrien}
1123250397Sobrien#endif
1123350397Sobrien
1123450397Sobrien/* These routines generate the internal representation of the DIE's for
1123550397Sobrien   the compilation unit.  Debugging information is collected by walking
1123650397Sobrien   the declaration trees passed in from dwarf2out_decl().  */
1123750397Sobrien
1123850397Sobrienstatic void
11239132718Skangen_array_type_die (tree type, dw_die_ref context_die)
1124050397Sobrien{
1124190075Sobrien  dw_die_ref scope_die = scope_die_for (type, context_die);
1124290075Sobrien  dw_die_ref array_die;
1124390075Sobrien  tree element_type;
1124450397Sobrien
1124550397Sobrien  /* ??? The SGI dwarf reader fails for array of array of enum types unless
1124650397Sobrien     the inner array type comes before the outer array type.  Thus we must
1124750397Sobrien     call gen_type_die before we call new_die.  See below also.  */
1124850397Sobrien#ifdef MIPS_DEBUGGING_INFO
1124950397Sobrien  gen_type_die (TREE_TYPE (type), context_die);
1125050397Sobrien#endif
1125150397Sobrien
1125290075Sobrien  array_die = new_die (DW_TAG_array_type, scope_die, type);
1125396263Sobrien  add_name_attribute (array_die, type_tag (type));
1125496263Sobrien  equate_type_number_to_die (type, array_die);
1125550397Sobrien
1125696263Sobrien  if (TREE_CODE (type) == VECTOR_TYPE)
1125796263Sobrien    {
1125896263Sobrien      /* The frontend feeds us a representation for the vector as a struct
1125996263Sobrien	 containing an array.  Pull out the array type.  */
1126096263Sobrien      type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
1126196263Sobrien      add_AT_flag (array_die, DW_AT_GNU_vector, 1);
1126296263Sobrien    }
1126396263Sobrien
1126450397Sobrien#if 0
1126550397Sobrien  /* We default the array ordering.  SDB will probably do
1126650397Sobrien     the right things even if DW_AT_ordering is not present.  It's not even
1126750397Sobrien     an issue until we start to get into multidimensional arrays anyway.  If
1126850397Sobrien     SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
1126950397Sobrien     then we'll have to put the DW_AT_ordering attribute back in.  (But if
1127050397Sobrien     and when we find out that we need to put these in, we will only do so
1127150397Sobrien     for multidimensional arrays.  */
1127250397Sobrien  add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
1127350397Sobrien#endif
1127450397Sobrien
1127550397Sobrien#ifdef MIPS_DEBUGGING_INFO
1127650397Sobrien  /* The SGI compilers handle arrays of unknown bound by setting
1127750397Sobrien     AT_declaration and not emitting any subrange DIEs.  */
1127850397Sobrien  if (! TYPE_DOMAIN (type))
11279132718Skan    add_AT_flag (array_die, DW_AT_declaration, 1);
1128050397Sobrien  else
1128150397Sobrien#endif
1128250397Sobrien    add_subscript_info (array_die, type);
1128350397Sobrien
1128450397Sobrien  /* Add representation of the type of the elements of this array type.  */
1128550397Sobrien  element_type = TREE_TYPE (type);
1128650397Sobrien
1128750397Sobrien  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
1128850397Sobrien     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
1128950397Sobrien     We work around this by disabling this feature.  See also
1129050397Sobrien     add_subscript_info.  */
1129150397Sobrien#ifndef MIPS_DEBUGGING_INFO
1129250397Sobrien  while (TREE_CODE (element_type) == ARRAY_TYPE)
1129350397Sobrien    element_type = TREE_TYPE (element_type);
1129450397Sobrien
1129550397Sobrien  gen_type_die (element_type, context_die);
1129650397Sobrien#endif
1129750397Sobrien
1129850397Sobrien  add_type_attribute (array_die, element_type, 0, 0, context_die);
1129950397Sobrien}
1130050397Sobrien
1130150397Sobrien#if 0
1130250397Sobrienstatic void
11303132718Skangen_entry_point_die (tree decl, dw_die_ref context_die)
1130450397Sobrien{
1130590075Sobrien  tree origin = decl_ultimate_origin (decl);
1130690075Sobrien  dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
1130790075Sobrien
1130850397Sobrien  if (origin != NULL)
1130950397Sobrien    add_abstract_origin_attribute (decl_die, origin);
1131050397Sobrien  else
1131150397Sobrien    {
1131250397Sobrien      add_name_and_src_coords_attributes (decl_die, decl);
1131350397Sobrien      add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
1131450397Sobrien			  0, 0, context_die);
1131550397Sobrien    }
1131650397Sobrien
1131750397Sobrien  if (DECL_ABSTRACT (decl))
1131850397Sobrien    equate_decl_number_to_die (decl, decl_die);
1131950397Sobrien  else
1132050397Sobrien    add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
1132150397Sobrien}
1132250397Sobrien#endif
1132350397Sobrien
1132452284Sobrien/* Walk through the list of incomplete types again, trying once more to
1132552284Sobrien   emit full debugging info for them.  */
1132652284Sobrien
1132752284Sobrienstatic void
11328132718Skanretry_incomplete_types (void)
1132952284Sobrien{
1133090075Sobrien  int i;
1133152284Sobrien
11332169689Skan  for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11333169689Skan    gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
1133452284Sobrien}
1133552284Sobrien
1133650397Sobrien/* Generate a DIE to represent an inlined instance of an enumeration type.  */
1133750397Sobrien
1133850397Sobrienstatic void
11339132718Skangen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
1134050397Sobrien{
1134190075Sobrien  dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
1134250397Sobrien
1134390075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1134490075Sobrien     be incomplete and such types are not marked.  */
1134550397Sobrien  add_abstract_origin_attribute (type_die, type);
1134650397Sobrien}
1134750397Sobrien
1134850397Sobrien/* Generate a DIE to represent an inlined instance of a structure type.  */
1134950397Sobrien
1135050397Sobrienstatic void
11351132718Skangen_inlined_structure_type_die (tree type, dw_die_ref context_die)
1135250397Sobrien{
1135390075Sobrien  dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
1135450397Sobrien
1135590075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1135690075Sobrien     be incomplete and such types are not marked.  */
1135750397Sobrien  add_abstract_origin_attribute (type_die, type);
1135850397Sobrien}
1135950397Sobrien
1136050397Sobrien/* Generate a DIE to represent an inlined instance of a union type.  */
1136150397Sobrien
1136250397Sobrienstatic void
11363132718Skangen_inlined_union_type_die (tree type, dw_die_ref context_die)
1136450397Sobrien{
1136590075Sobrien  dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
1136650397Sobrien
1136790075Sobrien  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
1136890075Sobrien     be incomplete and such types are not marked.  */
1136950397Sobrien  add_abstract_origin_attribute (type_die, type);
1137050397Sobrien}
1137150397Sobrien
1137250397Sobrien/* Generate a DIE to represent an enumeration type.  Note that these DIEs
1137350397Sobrien   include all of the information about the enumeration values also. Each
1137450397Sobrien   enumerated type name/value is listed as a child of the enumerated type
1137550397Sobrien   DIE.  */
1137650397Sobrien
11377132718Skanstatic dw_die_ref
11378132718Skangen_enumeration_type_die (tree type, dw_die_ref context_die)
1137950397Sobrien{
1138090075Sobrien  dw_die_ref type_die = lookup_type_die (type);
1138150397Sobrien
1138250397Sobrien  if (type_die == NULL)
1138350397Sobrien    {
1138450397Sobrien      type_die = new_die (DW_TAG_enumeration_type,
1138590075Sobrien			  scope_die_for (type, context_die), type);
1138650397Sobrien      equate_type_number_to_die (type, type_die);
1138750397Sobrien      add_name_attribute (type_die, type_tag (type));
1138850397Sobrien    }
1138950397Sobrien  else if (! TYPE_SIZE (type))
11390132718Skan    return type_die;
1139150397Sobrien  else
1139250397Sobrien    remove_AT (type_die, DW_AT_declaration);
1139350397Sobrien
1139450397Sobrien  /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
1139550397Sobrien     given enum type is incomplete, do not generate the DW_AT_byte_size
1139650397Sobrien     attribute or the DW_AT_element_list attribute.  */
1139750397Sobrien  if (TYPE_SIZE (type))
1139850397Sobrien    {
1139990075Sobrien      tree link;
1140050397Sobrien
1140150397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1140250397Sobrien      add_byte_size_attribute (type_die, type);
1140350397Sobrien      if (TYPE_STUB_DECL (type) != NULL_TREE)
1140450397Sobrien	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
1140550397Sobrien
1140650397Sobrien      /* If the first reference to this type was as the return type of an
1140750397Sobrien	 inline function, then it may not have a parent.  Fix this now.  */
1140850397Sobrien      if (type_die->die_parent == NULL)
1140950397Sobrien	add_child_die (scope_die_for (type, context_die), type_die);
1141050397Sobrien
11411169689Skan      for (link = TYPE_VALUES (type);
1141250397Sobrien	   link != NULL; link = TREE_CHAIN (link))
1141350397Sobrien	{
1141490075Sobrien	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11415132718Skan	  tree value = TREE_VALUE (link);
1141650397Sobrien
1141750397Sobrien	  add_name_attribute (enum_die,
1141850397Sobrien			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
1141990075Sobrien
11420169689Skan	  if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11421132718Skan	    /* DWARF2 does not provide a way of indicating whether or
11422132718Skan	       not enumeration constants are signed or unsigned.  GDB
11423132718Skan	       always assumes the values are signed, so we output all
11424132718Skan	       values as if they were signed.  That means that
11425132718Skan	       enumeration constants with very large unsigned values
11426132718Skan	       will appear to have negative values in the debugger.  */
11427132718Skan	    add_AT_int (enum_die, DW_AT_const_value,
11428132718Skan			tree_low_cst (value, tree_int_cst_sgn (value) > 0));
1142950397Sobrien	}
1143050397Sobrien    }
1143150397Sobrien  else
1143250397Sobrien    add_AT_flag (type_die, DW_AT_declaration, 1);
11433132718Skan
11434132718Skan  return type_die;
1143550397Sobrien}
1143650397Sobrien
1143750397Sobrien/* Generate a DIE to represent either a real live formal parameter decl or to
1143850397Sobrien   represent just the type of some formal parameter position in some function
1143950397Sobrien   type.
1144050397Sobrien
1144150397Sobrien   Note that this routine is a bit unusual because its argument may be a
1144250397Sobrien   ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
1144350397Sobrien   represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
1144450397Sobrien   node.  If it's the former then this function is being called to output a
1144550397Sobrien   DIE to represent a formal parameter object (or some inlining thereof).  If
1144650397Sobrien   it's the latter, then this function is only being called to output a
1144750397Sobrien   DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
1144850397Sobrien   argument type of some subprogram type.  */
1144950397Sobrien
1145050397Sobrienstatic dw_die_ref
11451132718Skangen_formal_parameter_die (tree node, dw_die_ref context_die)
1145250397Sobrien{
1145390075Sobrien  dw_die_ref parm_die
1145490075Sobrien    = new_die (DW_TAG_formal_parameter, context_die, node);
1145590075Sobrien  tree origin;
1145650397Sobrien
1145750397Sobrien  switch (TREE_CODE_CLASS (TREE_CODE (node)))
1145850397Sobrien    {
11459169689Skan    case tcc_declaration:
1146050397Sobrien      origin = decl_ultimate_origin (node);
1146150397Sobrien      if (origin != NULL)
1146250397Sobrien	add_abstract_origin_attribute (parm_die, origin);
1146350397Sobrien      else
1146450397Sobrien	{
1146550397Sobrien	  add_name_and_src_coords_attributes (parm_die, node);
1146650397Sobrien	  add_type_attribute (parm_die, TREE_TYPE (node),
1146750397Sobrien			      TREE_READONLY (node),
1146850397Sobrien			      TREE_THIS_VOLATILE (node),
1146950397Sobrien			      context_die);
1147050397Sobrien	  if (DECL_ARTIFICIAL (node))
1147150397Sobrien	    add_AT_flag (parm_die, DW_AT_artificial, 1);
1147250397Sobrien	}
1147350397Sobrien
1147450397Sobrien      equate_decl_number_to_die (node, parm_die);
1147550397Sobrien      if (! DECL_ABSTRACT (node))
11476169689Skan	add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
1147750397Sobrien
1147850397Sobrien      break;
1147950397Sobrien
11480169689Skan    case tcc_type:
1148150397Sobrien      /* We were called with some kind of a ..._TYPE node.  */
1148250397Sobrien      add_type_attribute (parm_die, node, 0, 0, context_die);
1148350397Sobrien      break;
1148450397Sobrien
1148550397Sobrien    default:
11486169689Skan      gcc_unreachable ();
1148750397Sobrien    }
1148850397Sobrien
1148950397Sobrien  return parm_die;
1149050397Sobrien}
1149150397Sobrien
1149250397Sobrien/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
1149350397Sobrien   at the end of an (ANSI prototyped) formal parameters list.  */
1149450397Sobrien
1149550397Sobrienstatic void
11496132718Skangen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
1149750397Sobrien{
1149890075Sobrien  new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
1149950397Sobrien}
1150050397Sobrien
1150150397Sobrien/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
1150250397Sobrien   DW_TAG_unspecified_parameters DIE) to represent the types of the formal
1150350397Sobrien   parameters as specified in some function type specification (except for
1150490075Sobrien   those which appear as part of a function *definition*).  */
1150550397Sobrien
1150650397Sobrienstatic void
11507132718Skangen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
1150850397Sobrien{
1150990075Sobrien  tree link;
1151090075Sobrien  tree formal_type = NULL;
1151190075Sobrien  tree first_parm_type;
1151290075Sobrien  tree arg;
1151350397Sobrien
1151490075Sobrien  if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
1151590075Sobrien    {
1151690075Sobrien      arg = DECL_ARGUMENTS (function_or_method_type);
1151790075Sobrien      function_or_method_type = TREE_TYPE (function_or_method_type);
1151890075Sobrien    }
1151990075Sobrien  else
1152090075Sobrien    arg = NULL_TREE;
11521117395Skan
1152290075Sobrien  first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
1152350397Sobrien
1152490075Sobrien  /* Make our first pass over the list of formal parameter types and output a
1152550397Sobrien     DW_TAG_formal_parameter DIE for each one.  */
1152690075Sobrien  for (link = first_parm_type; link; )
1152750397Sobrien    {
1152890075Sobrien      dw_die_ref parm_die;
1152990075Sobrien
1153050397Sobrien      formal_type = TREE_VALUE (link);
1153150397Sobrien      if (formal_type == void_type_node)
1153250397Sobrien	break;
1153350397Sobrien
1153450397Sobrien      /* Output a (nameless) DIE to represent the formal parameter itself.  */
1153550397Sobrien      parm_die = gen_formal_parameter_die (formal_type, context_die);
1153690075Sobrien      if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
1153790075Sobrien	   && link == first_parm_type)
1153890075Sobrien	  || (arg && DECL_ARTIFICIAL (arg)))
1153950397Sobrien	add_AT_flag (parm_die, DW_AT_artificial, 1);
1154090075Sobrien
1154190075Sobrien      link = TREE_CHAIN (link);
1154290075Sobrien      if (arg)
1154390075Sobrien	arg = TREE_CHAIN (arg);
1154450397Sobrien    }
1154550397Sobrien
1154650397Sobrien  /* If this function type has an ellipsis, add a
1154750397Sobrien     DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
1154850397Sobrien  if (formal_type != void_type_node)
1154950397Sobrien    gen_unspecified_parameters_die (function_or_method_type, context_die);
1155050397Sobrien
1155190075Sobrien  /* Make our second (and final) pass over the list of formal parameter types
1155250397Sobrien     and output DIEs to represent those types (as necessary).  */
1155350397Sobrien  for (link = TYPE_ARG_TYPES (function_or_method_type);
1155490075Sobrien       link && TREE_VALUE (link);
1155550397Sobrien       link = TREE_CHAIN (link))
1155690075Sobrien    gen_type_die (TREE_VALUE (link), context_die);
1155790075Sobrien}
1155890075Sobrien
1155990075Sobrien/* We want to generate the DIE for TYPE so that we can generate the
1156090075Sobrien   die for MEMBER, which has been defined; we will need to refer back
1156190075Sobrien   to the member declaration nested within TYPE.  If we're trying to
1156290075Sobrien   generate minimal debug info for TYPE, processing TYPE won't do the
1156390075Sobrien   trick; we need to attach the member declaration by hand.  */
1156490075Sobrien
1156590075Sobrienstatic void
11566132718Skangen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
1156790075Sobrien{
1156890075Sobrien  gen_type_die (type, context_die);
1156990075Sobrien
1157090075Sobrien  /* If we're trying to avoid duplicate debug info, we may not have
1157190075Sobrien     emitted the member decl for this function.  Emit it now.  */
1157290075Sobrien  if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
1157390075Sobrien      && ! lookup_decl_die (member))
1157450397Sobrien    {
11575169689Skan      dw_die_ref type_die;
11576169689Skan      gcc_assert (!decl_ultimate_origin (member));
1157750397Sobrien
1157890075Sobrien      push_decl_scope (type);
11579169689Skan      type_die = lookup_type_die (type);
1158090075Sobrien      if (TREE_CODE (member) == FUNCTION_DECL)
11581169689Skan	gen_subprogram_die (member, type_die);
11582169689Skan      else if (TREE_CODE (member) == FIELD_DECL)
11583169689Skan	{
11584169689Skan	  /* Ignore the nameless fields that are used to skip bits but handle
11585169689Skan	     C++ anonymous unions and structs.  */
11586169689Skan	  if (DECL_NAME (member) != NULL_TREE
11587169689Skan	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11588169689Skan	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11589169689Skan	    {
11590169689Skan	      gen_type_die (member_declared_type (member), type_die);
11591169689Skan	      gen_field_die (member, type_die);
11592169689Skan	    }
11593169689Skan	}
1159490075Sobrien      else
11595169689Skan	gen_variable_die (member, type_die);
1159690075Sobrien
1159790075Sobrien      pop_decl_scope ();
1159850397Sobrien    }
1159950397Sobrien}
1160050397Sobrien
1160190075Sobrien/* Generate the DWARF2 info for the "abstract" instance of a function which we
1160290075Sobrien   may later generate inlined and/or out-of-line instances of.  */
1160390075Sobrien
1160490075Sobrienstatic void
11605132718Skandwarf2out_abstract_function (tree decl)
1160690075Sobrien{
1160790075Sobrien  dw_die_ref old_die;
1160890075Sobrien  tree save_fn;
11609169689Skan  struct function *save_cfun;
1161090075Sobrien  tree context;
1161190075Sobrien  int was_abstract = DECL_ABSTRACT (decl);
1161290075Sobrien
1161390075Sobrien  /* Make sure we have the actual abstract inline, not a clone.  */
1161490075Sobrien  decl = DECL_ORIGIN (decl);
1161590075Sobrien
11616117395Skan  old_die = lookup_decl_die (decl);
11617132718Skan  if (old_die && get_AT (old_die, DW_AT_inline))
1161890075Sobrien    /* We've already generated the abstract instance.  */
1161990075Sobrien    return;
1162090075Sobrien
1162190075Sobrien  /* Be sure we've emitted the in-class declaration DIE (if any) first, so
1162290075Sobrien     we don't get confused by DECL_ABSTRACT.  */
1162390075Sobrien  if (debug_info_level > DINFO_LEVEL_TERSE)
1162490075Sobrien    {
1162590075Sobrien      context = decl_class_context (decl);
1162690075Sobrien      if (context)
1162790075Sobrien	gen_type_die_for_member
1162890075Sobrien	  (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
1162990075Sobrien    }
11630117395Skan
1163190075Sobrien  /* Pretend we've just finished compiling this function.  */
1163290075Sobrien  save_fn = current_function_decl;
11633169689Skan  save_cfun = cfun;
1163490075Sobrien  current_function_decl = decl;
11635169689Skan  cfun = DECL_STRUCT_FUNCTION (decl);
1163690075Sobrien
1163790075Sobrien  set_decl_abstract_flags (decl, 1);
1163890075Sobrien  dwarf2out_decl (decl);
1163990075Sobrien  if (! was_abstract)
1164090075Sobrien    set_decl_abstract_flags (decl, 0);
1164190075Sobrien
1164290075Sobrien  current_function_decl = save_fn;
11643169689Skan  cfun = save_cfun;
1164490075Sobrien}
1164590075Sobrien
11646169689Skan/* Helper function of premark_used_types() which gets called through
11647169689Skan   htab_traverse_resize().
11648169689Skan
11649169689Skan   Marks the DIE of a given type in *SLOT as perennial, so it never gets
11650169689Skan   marked as unused by prune_unused_types.  */
11651169689Skanstatic int
11652169689Skanpremark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11653169689Skan{
11654169689Skan  tree type;
11655169689Skan  dw_die_ref die;
11656169689Skan
11657169689Skan  type = *slot;
11658169689Skan  die = lookup_type_die (type);
11659169689Skan  if (die != NULL)
11660169689Skan    die->die_perennial_p = 1;
11661169689Skan  return 1;
11662169689Skan}
11663169689Skan
11664169689Skan/* Mark all members of used_types_hash as perennial.  */
11665169689Skanstatic void
11666169689Skanpremark_used_types (void)
11667169689Skan{
11668169689Skan  if (cfun && cfun->used_types_hash)
11669169689Skan    htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11670169689Skan}
11671169689Skan
1167250397Sobrien/* Generate a DIE to represent a declared function (either file-scope or
1167350397Sobrien   block-local).  */
1167450397Sobrien
1167550397Sobrienstatic void
11676132718Skangen_subprogram_die (tree decl, dw_die_ref context_die)
1167750397Sobrien{
1167850397Sobrien  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
1167990075Sobrien  tree origin = decl_ultimate_origin (decl);
1168090075Sobrien  dw_die_ref subr_die;
1168190075Sobrien  tree fn_arg_types;
1168290075Sobrien  tree outer_scope;
1168390075Sobrien  dw_die_ref old_die = lookup_decl_die (decl);
1168490075Sobrien  int declaration = (current_function_decl != decl
11685132718Skan		     || class_or_namespace_scope_p (context_die));
1168650397Sobrien
11687169689Skan  premark_used_types ();
11688169689Skan
1168990075Sobrien  /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
1169090075Sobrien     started to generate the abstract instance of an inline, decided to output
1169190075Sobrien     its containing class, and proceeded to emit the declaration of the inline
1169290075Sobrien     from the member list for the class.  If so, DECLARATION takes priority;
1169390075Sobrien     we'll get back to the abstract instance when done with the class.  */
1169490075Sobrien
1169590075Sobrien  /* The class-scope declaration DIE must be the primary DIE.  */
11696132718Skan  if (origin && declaration && class_or_namespace_scope_p (context_die))
1169750397Sobrien    {
1169890075Sobrien      origin = NULL;
11699169689Skan      gcc_assert (!old_die);
1170050397Sobrien    }
1170190075Sobrien
11702169689Skan  /* Now that the C++ front end lazily declares artificial member fns, we
11703169689Skan     might need to retrofit the declaration into its class.  */
11704169689Skan  if (!declaration && !origin && !old_die
11705169689Skan      && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11706169689Skan      && !class_or_namespace_scope_p (context_die)
11707169689Skan      && debug_info_level > DINFO_LEVEL_TERSE)
11708169689Skan    old_die = force_decl_die (decl);
11709169689Skan
1171090075Sobrien  if (origin != NULL)
1171150397Sobrien    {
11712169689Skan      gcc_assert (!declaration || local_scope_p (context_die));
1171350397Sobrien
1171490075Sobrien      /* Fixup die_parent for the abstract instance of a nested
1171590075Sobrien	 inline function.  */
1171690075Sobrien      if (old_die && old_die->die_parent == NULL)
1171790075Sobrien	add_child_die (context_die, old_die);
1171890075Sobrien
1171990075Sobrien      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
1172090075Sobrien      add_abstract_origin_attribute (subr_die, origin);
1172150397Sobrien    }
1172250397Sobrien  else if (old_die)
1172350397Sobrien    {
11724169689Skan      expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11725169689Skan      struct dwarf_file_data * file_index = lookup_filename (s.file);
1172650397Sobrien
1172790075Sobrien      if (!get_AT_flag (old_die, DW_AT_declaration)
1172890075Sobrien	  /* We can have a normal definition following an inline one in the
1172990075Sobrien	     case of redefinition of GNU C extern inlines.
1173090075Sobrien	     It seems reasonable to use AT_specification in this case.  */
11731132718Skan	  && !get_AT (old_die, DW_AT_inline))
1173252284Sobrien	{
11733169689Skan	  /* Detect and ignore this case, where we are trying to output
11734169689Skan	     something we have already output.  */
11735169689Skan	  return;
1173652284Sobrien	}
1173750397Sobrien
1173850397Sobrien      /* If the definition comes from the same place as the declaration,
1173950397Sobrien	 maybe use the old DIE.  We always want the DIE for this function
1174050397Sobrien	 that has the *_pc attributes to be under comp_unit_die so the
1174190075Sobrien	 debugger can find it.  We also need to do this for abstract
1174290075Sobrien	 instances of inlines, since the spec requires the out-of-line copy
1174390075Sobrien	 to have the same parent.  For local class methods, this doesn't
1174490075Sobrien	 apply; we just use the old DIE.  */
1174590075Sobrien      if ((old_die->die_parent == comp_unit_die || context_die == NULL)
1174690075Sobrien	  && (DECL_ARTIFICIAL (decl)
11747169689Skan	      || (get_AT_file (old_die, DW_AT_decl_file) == file_index
1174890075Sobrien		  && (get_AT_unsigned (old_die, DW_AT_decl_line)
11749169689Skan		      == (unsigned) s.line))))
1175050397Sobrien	{
1175150397Sobrien	  subr_die = old_die;
1175250397Sobrien
11753169689Skan	  /* Clear out the declaration attribute and the formal parameters.
11754169689Skan	     Do not remove all children, because it is possible that this
11755169689Skan	     declaration die was forced using force_decl_die(). In such
11756169689Skan	     cases die that forced declaration die (e.g. TAG_imported_module)
11757169689Skan	     is one of the children that we do not want to remove.  */
1175850397Sobrien	  remove_AT (subr_die, DW_AT_declaration);
11759146895Skan	  remove_child_TAG (subr_die, DW_TAG_formal_parameter);
1176050397Sobrien	}
1176150397Sobrien      else
1176250397Sobrien	{
1176390075Sobrien	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11764132718Skan	  add_AT_specification (subr_die, old_die);
11765169689Skan	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11766169689Skan	    add_AT_file (subr_die, DW_AT_decl_file, file_index);
11767169689Skan	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11768169689Skan	    add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
1176950397Sobrien	}
1177050397Sobrien    }
1177150397Sobrien  else
1177250397Sobrien    {
1177390075Sobrien      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
1177450397Sobrien
1177550397Sobrien      if (TREE_PUBLIC (decl))
1177650397Sobrien	add_AT_flag (subr_die, DW_AT_external, 1);
1177750397Sobrien
1177850397Sobrien      add_name_and_src_coords_attributes (subr_die, decl);
1177950397Sobrien      if (debug_info_level > DINFO_LEVEL_TERSE)
1178050397Sobrien	{
1178190075Sobrien	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
1178290075Sobrien	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
1178390075Sobrien			      0, 0, context_die);
1178450397Sobrien	}
1178550397Sobrien
1178650397Sobrien      add_pure_or_virtual_attribute (subr_die, decl);
1178750397Sobrien      if (DECL_ARTIFICIAL (decl))
1178850397Sobrien	add_AT_flag (subr_die, DW_AT_artificial, 1);
1178990075Sobrien
1179050397Sobrien      if (TREE_PROTECTED (decl))
1179150397Sobrien	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
1179250397Sobrien      else if (TREE_PRIVATE (decl))
1179350397Sobrien	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
1179450397Sobrien    }
1179550397Sobrien
1179650397Sobrien  if (declaration)
1179750397Sobrien    {
11798132718Skan      if (!old_die || !get_AT (old_die, DW_AT_inline))
1179990075Sobrien	{
1180090075Sobrien	  add_AT_flag (subr_die, DW_AT_declaration, 1);
1180150397Sobrien
1180290075Sobrien	  /* The first time we see a member function, it is in the context of
1180390075Sobrien	     the class to which it belongs.  We make sure of this by emitting
1180490075Sobrien	     the class first.  The next time is the definition, which is
11805169689Skan	     handled above.  The two may come from the same source text.
11806169689Skan
11807169689Skan	     Note that force_decl_die() forces function declaration die. It is
11808169689Skan	     later reused to represent definition.  */
11809169689Skan	  equate_decl_number_to_die (decl, subr_die);
1181090075Sobrien	}
1181150397Sobrien    }
1181250397Sobrien  else if (DECL_ABSTRACT (decl))
1181350397Sobrien    {
11814132718Skan      if (DECL_DECLARED_INLINE_P (decl))
1181550397Sobrien	{
11816132718Skan          if (cgraph_function_possibly_inlined_p (decl))
1181750397Sobrien	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
1181850397Sobrien	  else
11819132718Skan	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
1182050397Sobrien	}
1182150397Sobrien      else
11822132718Skan	{
11823132718Skan	  if (cgraph_function_possibly_inlined_p (decl))
11824132718Skan            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11825132718Skan	  else
11826132718Skan            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11827132718Skan	}
1182850397Sobrien
1182950397Sobrien      equate_decl_number_to_die (decl, subr_die);
1183050397Sobrien    }
1183150397Sobrien  else if (!DECL_EXTERNAL (decl))
1183250397Sobrien    {
11833169689Skan      HOST_WIDE_INT cfa_fb_offset;
11834169689Skan
11835132718Skan      if (!old_die || !get_AT (old_die, DW_AT_inline))
1183650397Sobrien	equate_decl_number_to_die (decl, subr_die);
1183750397Sobrien
11838169689Skan      if (!flag_reorder_blocks_and_partition)
11839169689Skan	{
11840169689Skan	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11841169689Skan				       current_function_funcdef_no);
11842169689Skan	  add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11843169689Skan	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11844169689Skan				       current_function_funcdef_no);
11845169689Skan	  add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11846169689Skan
11847169689Skan	  add_pubname (decl, subr_die);
11848169689Skan	  add_arange (decl, subr_die);
11849169689Skan	}
11850169689Skan      else
11851169689Skan	{  /* Do nothing for now; maybe need to duplicate die, one for
11852169689Skan	      hot section and ond for cold section, then use the hot/cold
11853169689Skan	      section begin/end labels to generate the aranges...  */
11854169689Skan	  /*
11855169689Skan	    add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11856169689Skan	    add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11857169689Skan	    add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11858169689Skan	    add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
1185950397Sobrien
11860169689Skan	    add_pubname (decl, subr_die);
11861169689Skan	    add_arange (decl, subr_die);
11862169689Skan	    add_arange (decl, subr_die);
11863169689Skan	   */
11864169689Skan	}
1186550397Sobrien
1186650397Sobrien#ifdef MIPS_DEBUGGING_INFO
1186750397Sobrien      /* Add a reference to the FDE for this routine.  */
1186850397Sobrien      add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
1186950397Sobrien#endif
1187050397Sobrien
11871169689Skan      cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
1187250397Sobrien
11873169689Skan      /* We define the "frame base" as the function's CFA.  This is more
11874169689Skan	 convenient for several reasons: (1) It's stable across the prologue
11875169689Skan	 and epilogue, which makes it better than just a frame pointer,
11876169689Skan	 (2) With dwarf3, there exists a one-byte encoding that allows us
11877169689Skan	 to reference the .debug_frame data by proxy, but failing that,
11878169689Skan	 (3) We can at least reuse the code inspection and interpretation
11879169689Skan	 code that determines the CFA position at various points in the
11880169689Skan	 function.  */
11881169689Skan      /* ??? Use some command-line or configury switch to enable the use
11882169689Skan	 of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11883169689Skan	 consumers that understand it; fall back to "pure" dwarf2 and
11884169689Skan	 convert the CFA data into a location list.  */
11885169689Skan      {
11886169689Skan	dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11887169689Skan	if (list->dw_loc_next)
11888169689Skan	  add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11889169689Skan	else
11890169689Skan	  add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11891169689Skan      }
11892169689Skan
11893169689Skan      /* Compute a displacement from the "steady-state frame pointer" to
11894169689Skan	 the CFA.  The former is what all stack slots and argument slots
11895169689Skan	 will reference in the rtl; the later is what we've told the
11896169689Skan	 debugger about.  We'll need to adjust all frame_base references
11897169689Skan	 by this displacement.  */
11898169689Skan      compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11899169689Skan
11900169689Skan      if (cfun->static_chain_decl)
1190150397Sobrien	add_AT_location_description (subr_die, DW_AT_static_link,
11902169689Skan		 loc_descriptor_from_tree (cfun->static_chain_decl));
1190350397Sobrien    }
1190450397Sobrien
1190550397Sobrien  /* Now output descriptions of the arguments for this function. This gets
1190690075Sobrien     (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
1190750397Sobrien     for a FUNCTION_DECL doesn't indicate cases where there was a trailing
1190850397Sobrien     `...' at the end of the formal parameter list.  In order to find out if
1190950397Sobrien     there was a trailing ellipsis or not, we must instead look at the type
1191050397Sobrien     associated with the FUNCTION_DECL.  This will be a node of type
1191150397Sobrien     FUNCTION_TYPE. If the chain of type nodes hanging off of this
1191290075Sobrien     FUNCTION_TYPE node ends with a void_type_node then there should *not* be
1191350397Sobrien     an ellipsis at the end.  */
1191450397Sobrien
1191550397Sobrien  /* In the case where we are describing a mere function declaration, all we
1191690075Sobrien     need to do here (and all we *can* do here) is to describe the *types* of
1191750397Sobrien     its formal parameters.  */
1191850397Sobrien  if (debug_info_level <= DINFO_LEVEL_TERSE)
1191950397Sobrien    ;
1192050397Sobrien  else if (declaration)
1192190075Sobrien    gen_formal_types_die (decl, subr_die);
1192250397Sobrien  else
1192350397Sobrien    {
11924132718Skan      /* Generate DIEs to represent all known formal parameters.  */
1192590075Sobrien      tree arg_decls = DECL_ARGUMENTS (decl);
1192690075Sobrien      tree parm;
1192750397Sobrien
1192850397Sobrien      /* When generating DIEs, generate the unspecified_parameters DIE
11929132718Skan	 instead if we come across the arg "__builtin_va_alist" */
1193050397Sobrien      for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
1193150397Sobrien	if (TREE_CODE (parm) == PARM_DECL)
1193250397Sobrien	  {
1193350397Sobrien	    if (DECL_NAME (parm)
1193450397Sobrien		&& !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
1193550397Sobrien			    "__builtin_va_alist"))
1193650397Sobrien	      gen_unspecified_parameters_die (parm, subr_die);
1193750397Sobrien	    else
1193850397Sobrien	      gen_decl_die (parm, subr_die);
1193950397Sobrien	  }
1194050397Sobrien
1194190075Sobrien      /* Decide whether we need an unspecified_parameters DIE at the end.
11942132718Skan	 There are 2 more cases to do this for: 1) the ansi ... declaration -
11943132718Skan	 this is detectable when the end of the arg list is not a
11944132718Skan	 void_type_node 2) an unprototyped function declaration (not a
11945132718Skan	 definition).  This just means that we have no info about the
11946132718Skan	 parameters at all.  */
1194750397Sobrien      fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1194850397Sobrien      if (fn_arg_types != NULL)
1194950397Sobrien	{
11950132718Skan	  /* This is the prototyped case, check for....  */
1195150397Sobrien	  if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
1195250397Sobrien	    gen_unspecified_parameters_die (decl, subr_die);
1195350397Sobrien	}
1195450397Sobrien      else if (DECL_INITIAL (decl) == NULL_TREE)
1195550397Sobrien	gen_unspecified_parameters_die (decl, subr_die);
1195650397Sobrien    }
1195750397Sobrien
1195850397Sobrien  /* Output Dwarf info for all of the stuff within the body of the function
1195950397Sobrien     (if it has one - it may be just a declaration).  */
1196050397Sobrien  outer_scope = DECL_INITIAL (decl);
1196150397Sobrien
1196290075Sobrien  /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
1196390075Sobrien     a function.  This BLOCK actually represents the outermost binding contour
1196490075Sobrien     for the function, i.e. the contour in which the function's formal
1196590075Sobrien     parameters and labels get declared. Curiously, it appears that the front
1196690075Sobrien     end doesn't actually put the PARM_DECL nodes for the current function onto
1196790075Sobrien     the BLOCK_VARS list for this outer scope, but are strung off of the
1196890075Sobrien     DECL_ARGUMENTS list for the function instead.
1196990075Sobrien
1197090075Sobrien     The BLOCK_VARS list for the `outer_scope' does provide us with a list of
1197190075Sobrien     the LABEL_DECL nodes for the function however, and we output DWARF info
1197290075Sobrien     for those in decls_for_scope.  Just within the `outer_scope' there will be
1197390075Sobrien     a BLOCK node representing the function's outermost pair of curly braces,
1197490075Sobrien     and any blocks used for the base and member initializers of a C++
1197550397Sobrien     constructor function.  */
1197650397Sobrien  if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
1197750397Sobrien    {
11978169689Skan      /* Emit a DW_TAG_variable DIE for a named return value.  */
11979169689Skan      if (DECL_NAME (DECL_RESULT (decl)))
11980169689Skan	gen_decl_die (DECL_RESULT (decl), subr_die);
11981169689Skan
1198250397Sobrien      current_function_has_inlines = 0;
1198350397Sobrien      decls_for_scope (outer_scope, subr_die, 0);
1198450397Sobrien
1198550397Sobrien#if 0 && defined (MIPS_DEBUGGING_INFO)
1198650397Sobrien      if (current_function_has_inlines)
1198750397Sobrien	{
1198850397Sobrien	  add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
1198950397Sobrien	  if (! comp_unit_has_inlines)
1199050397Sobrien	    {
1199150397Sobrien	      add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
1199250397Sobrien	      comp_unit_has_inlines = 1;
1199350397Sobrien	    }
1199450397Sobrien	}
1199550397Sobrien#endif
1199650397Sobrien    }
11997169689Skan  /* Add the calling convention attribute if requested.  */
11998169689Skan  add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11999169689Skan
1200050397Sobrien}
1200150397Sobrien
1200250397Sobrien/* Generate a DIE to represent a declared data object.  */
1200350397Sobrien
1200450397Sobrienstatic void
12005132718Skangen_variable_die (tree decl, dw_die_ref context_die)
1200650397Sobrien{
1200790075Sobrien  tree origin = decl_ultimate_origin (decl);
1200890075Sobrien  dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
1200950397Sobrien
1201050397Sobrien  dw_die_ref old_die = lookup_decl_die (decl);
1201190075Sobrien  int declaration = (DECL_EXTERNAL (decl)
12012169689Skan		     /* If DECL is COMDAT and has not actually been
12013169689Skan			emitted, we cannot take its address; there
12014169689Skan			might end up being no definition anywhere in
12015169689Skan			the program.  For example, consider the C++
12016169689Skan			test case:
12017169689Skan
12018169689Skan                          template <class T>
12019169689Skan                          struct S { static const int i = 7; };
12020169689Skan
12021169689Skan                          template <class T>
12022169689Skan                          const int S<T>::i;
12023169689Skan
12024169689Skan                          int f() { return S<int>::i; }
12025169689Skan
12026169689Skan			Here, S<int>::i is not DECL_EXTERNAL, but no
12027169689Skan			definition is required, so the compiler will
12028169689Skan			not emit a definition.  */
12029169689Skan		     || (TREE_CODE (decl) == VAR_DECL
12030169689Skan			 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12031132718Skan		     || class_or_namespace_scope_p (context_die));
1203250397Sobrien
1203350397Sobrien  if (origin != NULL)
1203450397Sobrien    add_abstract_origin_attribute (var_die, origin);
1203590075Sobrien
1203650397Sobrien  /* Loop unrolling can create multiple blocks that refer to the same
1203790075Sobrien     static variable, so we must test for the DW_AT_declaration flag.
1203890075Sobrien
1203990075Sobrien     ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
1204050397Sobrien     copy decls and set the DECL_ABSTRACT flag on them instead of
1204190075Sobrien     sharing them.
1204290075Sobrien
12043169689Skan     ??? Duplicated blocks have been rewritten to use .debug_ranges.
12044169689Skan
12045169689Skan     ??? The declare_in_namespace support causes us to get two DIEs for one
12046169689Skan     variable, both of which are declarations.  We want to avoid considering
12047169689Skan     one to be a specification, so we must test that this DIE is not a
12048169689Skan     declaration.  */
12049169689Skan  else if (old_die && TREE_STATIC (decl) && ! declaration
12050117395Skan	   && get_AT_flag (old_die, DW_AT_declaration) == 1)
1205150397Sobrien    {
1205290075Sobrien      /* This is a definition of a C++ class level static.  */
12053132718Skan      add_AT_specification (var_die, old_die);
1205450397Sobrien      if (DECL_NAME (decl))
1205550397Sobrien	{
12056169689Skan	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12057169689Skan	  struct dwarf_file_data * file_index = lookup_filename (s.file);
1205850397Sobrien
12059169689Skan	  if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12060169689Skan	    add_AT_file (var_die, DW_AT_decl_file, file_index);
1206150397Sobrien
12062169689Skan	  if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
1206350397Sobrien
12064169689Skan	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
1206550397Sobrien	}
1206650397Sobrien    }
1206750397Sobrien  else
1206850397Sobrien    {
1206950397Sobrien      add_name_and_src_coords_attributes (var_die, decl);
1207090075Sobrien      add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
1207150397Sobrien			  TREE_THIS_VOLATILE (decl), context_die);
1207250397Sobrien
1207350397Sobrien      if (TREE_PUBLIC (decl))
1207450397Sobrien	add_AT_flag (var_die, DW_AT_external, 1);
1207550397Sobrien
1207650397Sobrien      if (DECL_ARTIFICIAL (decl))
1207750397Sobrien	add_AT_flag (var_die, DW_AT_artificial, 1);
1207850397Sobrien
1207950397Sobrien      if (TREE_PROTECTED (decl))
1208050397Sobrien	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
1208150397Sobrien      else if (TREE_PRIVATE (decl))
1208250397Sobrien	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
1208350397Sobrien    }
1208450397Sobrien
1208550397Sobrien  if (declaration)
1208650397Sobrien    add_AT_flag (var_die, DW_AT_declaration, 1);
1208790075Sobrien
12088169689Skan  if (DECL_ABSTRACT (decl) || declaration)
1208950397Sobrien    equate_decl_number_to_die (decl, var_die);
1209050397Sobrien
1209150397Sobrien  if (! declaration && ! DECL_ABSTRACT (decl))
1209250397Sobrien    {
12093169689Skan      add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
1209450397Sobrien      add_pubname (decl, var_die);
1209550397Sobrien    }
1209690075Sobrien  else
1209790075Sobrien    tree_add_const_value_attribute (var_die, decl);
1209850397Sobrien}
1209950397Sobrien
1210050397Sobrien/* Generate a DIE to represent a label identifier.  */
1210150397Sobrien
1210250397Sobrienstatic void
12103132718Skangen_label_die (tree decl, dw_die_ref context_die)
1210450397Sobrien{
1210590075Sobrien  tree origin = decl_ultimate_origin (decl);
1210690075Sobrien  dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
1210790075Sobrien  rtx insn;
1210850397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
1210950397Sobrien
1211050397Sobrien  if (origin != NULL)
1211150397Sobrien    add_abstract_origin_attribute (lbl_die, origin);
1211250397Sobrien  else
1211350397Sobrien    add_name_and_src_coords_attributes (lbl_die, decl);
1211450397Sobrien
1211550397Sobrien  if (DECL_ABSTRACT (decl))
1211650397Sobrien    equate_decl_number_to_die (decl, lbl_die);
1211750397Sobrien  else
1211850397Sobrien    {
12119132718Skan      insn = DECL_RTL_IF_SET (decl);
1212052284Sobrien
1212152284Sobrien      /* Deleted labels are programmer specified labels which have been
12122132718Skan	 eliminated because of various optimizations.  We still emit them
1212352284Sobrien	 here so that it is possible to put breakpoints on them.  */
12124132718Skan      if (insn
12125169689Skan	  && (LABEL_P (insn)
12126169689Skan	      || ((NOTE_P (insn)
12127132718Skan	           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
1212850397Sobrien	{
1212990075Sobrien	  /* When optimization is enabled (via -O) some parts of the compiler
1213090075Sobrien	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
1213150397Sobrien	     represent source-level labels which were explicitly declared by
1213250397Sobrien	     the user.  This really shouldn't be happening though, so catch
1213350397Sobrien	     it if it ever does happen.  */
12134169689Skan	  gcc_assert (!INSN_DELETED_P (insn));
1213550397Sobrien
1213690075Sobrien	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
1213750397Sobrien	  add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
1213850397Sobrien	}
1213950397Sobrien    }
1214050397Sobrien}
1214150397Sobrien
12142169689Skan/* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12143169689Skan   attributes to the DIE for a block STMT, to describe where the inlined
12144169689Skan   function was called from.  This is similar to add_src_coords_attributes.  */
1214550397Sobrien
12146169689Skanstatic inline void
12147169689Skanadd_call_src_coords_attributes (tree stmt, dw_die_ref die)
1214850397Sobrien{
12149169689Skan  expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12150169689Skan
12151169689Skan  add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12152169689Skan  add_AT_unsigned (die, DW_AT_call_line, s.line);
12153169689Skan}
12154169689Skan
12155169689Skan/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12156169689Skan   Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12157169689Skan
12158169689Skanstatic inline void
12159169689Skanadd_high_low_attributes (tree stmt, dw_die_ref die)
12160169689Skan{
1216150397Sobrien  char label[MAX_ARTIFICIAL_LABEL_BYTES];
1216250397Sobrien
12163169689Skan  if (BLOCK_FRAGMENT_CHAIN (stmt))
1216450397Sobrien    {
12165169689Skan      tree chain;
1216690075Sobrien
12167169689Skan      add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
1216890075Sobrien
12169169689Skan      chain = BLOCK_FRAGMENT_CHAIN (stmt);
12170169689Skan      do
1217190075Sobrien	{
12172169689Skan	  add_ranges (chain);
12173169689Skan	  chain = BLOCK_FRAGMENT_CHAIN (chain);
1217490075Sobrien	}
12175169689Skan      while (chain);
12176169689Skan      add_ranges (NULL);
1217750397Sobrien    }
12178169689Skan  else
12179169689Skan    {
12180169689Skan      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12181169689Skan				   BLOCK_NUMBER (stmt));
12182169689Skan      add_AT_lbl_id (die, DW_AT_low_pc, label);
12183169689Skan      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12184169689Skan				   BLOCK_NUMBER (stmt));
12185169689Skan      add_AT_lbl_id (die, DW_AT_high_pc, label);
12186169689Skan    }
12187169689Skan}
1218850397Sobrien
12189169689Skan/* Generate a DIE for a lexical block.  */
12190169689Skan
12191169689Skanstatic void
12192169689Skangen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12193169689Skan{
12194169689Skan  dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12195169689Skan
12196169689Skan  if (! BLOCK_ABSTRACT (stmt))
12197169689Skan    add_high_low_attributes (stmt, stmt_die);
12198169689Skan
1219950397Sobrien  decls_for_scope (stmt, stmt_die, depth);
1220050397Sobrien}
1220150397Sobrien
1220250397Sobrien/* Generate a DIE for an inlined subprogram.  */
1220350397Sobrien
1220450397Sobrienstatic void
12205132718Skangen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
1220650397Sobrien{
12207122180Skan  tree decl = block_ultimate_origin (stmt);
12208122180Skan
12209122180Skan  /* Emit info for the abstract instance first, if we haven't yet.  We
12210122180Skan     must emit this even if the block is abstract, otherwise when we
12211122180Skan     emit the block below (or elsewhere), we may end up trying to emit
12212122180Skan     a die whose origin die hasn't been emitted, and crashing.  */
12213122180Skan  dwarf2out_abstract_function (decl);
12214122180Skan
1221550397Sobrien  if (! BLOCK_ABSTRACT (stmt))
1221650397Sobrien    {
1221790075Sobrien      dw_die_ref subr_die
1221890075Sobrien	= new_die (DW_TAG_inlined_subroutine, context_die, stmt);
1221950397Sobrien
1222050397Sobrien      add_abstract_origin_attribute (subr_die, decl);
12221169689Skan      add_high_low_attributes (stmt, subr_die);
12222169689Skan      add_call_src_coords_attributes (stmt, subr_die);
12223169689Skan
1222450397Sobrien      decls_for_scope (stmt, subr_die, depth);
1222550397Sobrien      current_function_has_inlines = 1;
1222650397Sobrien    }
1222796263Sobrien  else
1222896263Sobrien    /* We may get here if we're the outer block of function A that was
1222996263Sobrien       inlined into function B that was inlined into function C.  When
1223096263Sobrien       generating debugging info for C, dwarf2out_abstract_function(B)
1223196263Sobrien       would mark all inlined blocks as abstract, including this one.
1223296263Sobrien       So, we wouldn't (and shouldn't) expect labels to be generated
1223396263Sobrien       for this one.  Instead, just emit debugging info for
1223496263Sobrien       declarations within the block.  This is particularly important
1223596263Sobrien       in the case of initializers of arguments passed from B to us:
1223696263Sobrien       if they're statement expressions containing declarations, we
1223796263Sobrien       wouldn't generate dies for their abstract variables, and then,
1223896263Sobrien       when generating dies for the real variables, we'd die (pun
1223996263Sobrien       intended :-)  */
1224096263Sobrien    gen_lexical_block_die (stmt, context_die, depth);
1224150397Sobrien}
1224250397Sobrien
1224350397Sobrien/* Generate a DIE for a field in a record, or structure.  */
1224450397Sobrien
1224550397Sobrienstatic void
12246132718Skangen_field_die (tree decl, dw_die_ref context_die)
1224750397Sobrien{
12248117395Skan  dw_die_ref decl_die;
1224950397Sobrien
12250117395Skan  if (TREE_TYPE (decl) == error_mark_node)
12251117395Skan    return;
12252132718Skan
12253117395Skan  decl_die = new_die (DW_TAG_member, context_die, decl);
1225450397Sobrien  add_name_and_src_coords_attributes (decl_die, decl);
1225550397Sobrien  add_type_attribute (decl_die, member_declared_type (decl),
1225650397Sobrien		      TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
1225750397Sobrien		      context_die);
1225850397Sobrien
1225950397Sobrien  if (DECL_BIT_FIELD_TYPE (decl))
1226050397Sobrien    {
1226150397Sobrien      add_byte_size_attribute (decl_die, decl);
1226250397Sobrien      add_bit_size_attribute (decl_die, decl);
1226350397Sobrien      add_bit_offset_attribute (decl_die, decl);
1226450397Sobrien    }
1226550397Sobrien
1226650397Sobrien  if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
1226750397Sobrien    add_data_member_location_attribute (decl_die, decl);
1226850397Sobrien
1226950397Sobrien  if (DECL_ARTIFICIAL (decl))
1227050397Sobrien    add_AT_flag (decl_die, DW_AT_artificial, 1);
1227150397Sobrien
1227250397Sobrien  if (TREE_PROTECTED (decl))
1227350397Sobrien    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
1227450397Sobrien  else if (TREE_PRIVATE (decl))
1227550397Sobrien    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12276169689Skan
12277169689Skan  /* Equate decl number to die, so that we can look up this decl later on.  */
12278169689Skan  equate_decl_number_to_die (decl, decl_die);
1227950397Sobrien}
1228050397Sobrien
1228150397Sobrien#if 0
1228250397Sobrien/* Don't generate either pointer_type DIEs or reference_type DIEs here.
1228350397Sobrien   Use modified_type_die instead.
1228450397Sobrien   We keep this code here just in case these types of DIEs may be needed to
1228550397Sobrien   represent certain things in other languages (e.g. Pascal) someday.  */
1228690075Sobrien
1228750397Sobrienstatic void
12288132718Skangen_pointer_type_die (tree type, dw_die_ref context_die)
1228950397Sobrien{
1229090075Sobrien  dw_die_ref ptr_die
1229190075Sobrien    = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
1229250397Sobrien
1229350397Sobrien  equate_type_number_to_die (type, ptr_die);
1229450397Sobrien  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
1229550397Sobrien  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
1229650397Sobrien}
1229750397Sobrien
1229850397Sobrien/* Don't generate either pointer_type DIEs or reference_type DIEs here.
1229950397Sobrien   Use modified_type_die instead.
1230050397Sobrien   We keep this code here just in case these types of DIEs may be needed to
1230150397Sobrien   represent certain things in other languages (e.g. Pascal) someday.  */
1230290075Sobrien
1230350397Sobrienstatic void
12304132718Skangen_reference_type_die (tree type, dw_die_ref context_die)
1230550397Sobrien{
1230690075Sobrien  dw_die_ref ref_die
1230790075Sobrien    = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
1230850397Sobrien
1230950397Sobrien  equate_type_number_to_die (type, ref_die);
1231050397Sobrien  add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
1231150397Sobrien  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
1231250397Sobrien}
1231350397Sobrien#endif
1231450397Sobrien
1231550397Sobrien/* Generate a DIE for a pointer to a member type.  */
1231690075Sobrien
1231750397Sobrienstatic void
12318132718Skangen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
1231950397Sobrien{
1232090075Sobrien  dw_die_ref ptr_die
1232190075Sobrien    = new_die (DW_TAG_ptr_to_member_type,
1232290075Sobrien	       scope_die_for (type, context_die), type);
1232350397Sobrien
1232450397Sobrien  equate_type_number_to_die (type, ptr_die);
1232550397Sobrien  add_AT_die_ref (ptr_die, DW_AT_containing_type,
1232650397Sobrien		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
1232750397Sobrien  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
1232850397Sobrien}
1232950397Sobrien
1233050397Sobrien/* Generate the DIE for the compilation unit.  */
1233150397Sobrien
1233290075Sobrienstatic dw_die_ref
12333132718Skangen_compile_unit_die (const char *filename)
1233450397Sobrien{
1233590075Sobrien  dw_die_ref die;
1233650397Sobrien  char producer[250];
1233790075Sobrien  const char *language_string = lang_hooks.name;
1233890075Sobrien  int language;
1233950397Sobrien
1234090075Sobrien  die = new_die (DW_TAG_compile_unit, NULL, NULL);
1234150397Sobrien
12342132718Skan  if (filename)
12343132718Skan    {
12344132718Skan      add_name_attribute (die, filename);
12345132718Skan      /* Don't add cwd for <built-in>.  */
12346132718Skan      if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12347132718Skan	add_comp_dir_attribute (die);
12348132718Skan    }
1234950397Sobrien
1235050397Sobrien  sprintf (producer, "%s %s", language_string, version_string);
1235150397Sobrien
1235250397Sobrien#ifdef MIPS_DEBUGGING_INFO
1235350397Sobrien  /* The MIPS/SGI compilers place the 'cc' command line options in the producer
1235450397Sobrien     string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
1235550397Sobrien     not appear in the producer string, the debugger reaches the conclusion
1235650397Sobrien     that the object file is stripped and has no debugging information.
1235750397Sobrien     To get the MIPS/SGI debugger to believe that there is debugging
1235850397Sobrien     information in the object file, we add a -g to the producer string.  */
1235950397Sobrien  if (debug_info_level > DINFO_LEVEL_TERSE)
1236050397Sobrien    strcat (producer, " -g");
1236150397Sobrien#endif
1236250397Sobrien
1236390075Sobrien  add_AT_string (die, DW_AT_producer, producer);
1236450397Sobrien
1236550397Sobrien  if (strcmp (language_string, "GNU C++") == 0)
1236690075Sobrien    language = DW_LANG_C_plus_plus;
1236750397Sobrien  else if (strcmp (language_string, "GNU Ada") == 0)
12368132718Skan    language = DW_LANG_Ada95;
1236950397Sobrien  else if (strcmp (language_string, "GNU F77") == 0)
1237090075Sobrien    language = DW_LANG_Fortran77;
12371169689Skan  else if (strcmp (language_string, "GNU F95") == 0)
12372169689Skan    language = DW_LANG_Fortran95;
1237350397Sobrien  else if (strcmp (language_string, "GNU Pascal") == 0)
1237490075Sobrien    language = DW_LANG_Pascal83;
1237590075Sobrien  else if (strcmp (language_string, "GNU Java") == 0)
1237690075Sobrien    language = DW_LANG_Java;
12377169689Skan  else if (strcmp (language_string, "GNU Objective-C") == 0)
12378169689Skan    language = DW_LANG_ObjC;
12379169689Skan  else if (strcmp (language_string, "GNU Objective-C++") == 0)
12380169689Skan    language = DW_LANG_ObjC_plus_plus;
1238150397Sobrien  else
1238290075Sobrien    language = DW_LANG_C89;
1238350397Sobrien
1238490075Sobrien  add_AT_unsigned (die, DW_AT_language, language);
1238590075Sobrien  return die;
1238650397Sobrien}
1238750397Sobrien
1238850397Sobrien/* Generate the DIE for a base class.  */
1238950397Sobrien
1239050397Sobrienstatic void
12391132718Skangen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
1239250397Sobrien{
1239390075Sobrien  dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
1239450397Sobrien
1239550397Sobrien  add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
1239650397Sobrien  add_data_member_location_attribute (die, binfo);
1239750397Sobrien
12398169689Skan  if (BINFO_VIRTUAL_P (binfo))
1239950397Sobrien    add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
1240090075Sobrien
12401132718Skan  if (access == access_public_node)
1240250397Sobrien    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12403132718Skan  else if (access == access_protected_node)
1240450397Sobrien    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
1240550397Sobrien}
1240650397Sobrien
1240750397Sobrien/* Generate a DIE for a class member.  */
1240850397Sobrien
1240950397Sobrienstatic void
12410132718Skangen_member_die (tree type, dw_die_ref context_die)
1241150397Sobrien{
1241290075Sobrien  tree member;
12413132718Skan  tree binfo = TYPE_BINFO (type);
1241490075Sobrien  dw_die_ref child;
1241550397Sobrien
1241650397Sobrien  /* If this is not an incomplete type, output descriptions of each of its
1241750397Sobrien     members. Note that as we output the DIEs necessary to represent the
1241850397Sobrien     members of this record or union type, we will also be trying to output
1241950397Sobrien     DIEs to represent the *types* of those members. However the `type'
1242090075Sobrien     function (above) will specifically avoid generating type DIEs for member
1242190075Sobrien     types *within* the list of member DIEs for this (containing) type except
1242250397Sobrien     for those types (of members) which are explicitly marked as also being
1242350397Sobrien     members of this (containing) type themselves.  The g++ front- end can
1242490075Sobrien     force any given type to be treated as a member of some other (containing)
1242590075Sobrien     type by setting the TYPE_CONTEXT of the given (member) type to point to
1242690075Sobrien     the TREE node representing the appropriate (containing) type.  */
1242750397Sobrien
1242850397Sobrien  /* First output info about the base classes.  */
12429169689Skan  if (binfo)
1243050397Sobrien    {
12431169689Skan      VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
1243290075Sobrien      int i;
12433169689Skan      tree base;
1243450397Sobrien
12435169689Skan      for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12436169689Skan	gen_inheritance_die (base,
12437169689Skan			     (accesses ? VEC_index (tree, accesses, i)
12438132718Skan			      : access_public_node), context_die);
1243950397Sobrien    }
1244050397Sobrien
1244150397Sobrien  /* Now output info about the data members and type members.  */
1244250397Sobrien  for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
1244390075Sobrien    {
1244490075Sobrien      /* If we thought we were generating minimal debug info for TYPE
1244590075Sobrien	 and then changed our minds, some of the member declarations
1244690075Sobrien	 may have already been defined.  Don't define them again, but
1244790075Sobrien	 do put them in the right order.  */
1244850397Sobrien
1244990075Sobrien      child = lookup_decl_die (member);
1245090075Sobrien      if (child)
1245190075Sobrien	splice_child_die (context_die, child);
1245290075Sobrien      else
1245390075Sobrien	gen_decl_die (member, context_die);
1245490075Sobrien    }
1245590075Sobrien
1245650397Sobrien  /* Now output info about the function members (if any).  */
1245750397Sobrien  for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
1245890075Sobrien    {
1245990075Sobrien      /* Don't include clones in the member list.  */
1246090075Sobrien      if (DECL_ABSTRACT_ORIGIN (member))
1246190075Sobrien	continue;
1246290075Sobrien
1246390075Sobrien      child = lookup_decl_die (member);
1246490075Sobrien      if (child)
1246590075Sobrien	splice_child_die (context_die, child);
1246690075Sobrien      else
1246790075Sobrien	gen_decl_die (member, context_die);
1246890075Sobrien    }
1246950397Sobrien}
1247050397Sobrien
1247190075Sobrien/* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
1247290075Sobrien   is set, we pretend that the type was never defined, so we only get the
1247390075Sobrien   member DIEs needed by later specification DIEs.  */
1247450397Sobrien
1247550397Sobrienstatic void
12476132718Skangen_struct_or_union_type_die (tree type, dw_die_ref context_die)
1247750397Sobrien{
1247890075Sobrien  dw_die_ref type_die = lookup_type_die (type);
1247990075Sobrien  dw_die_ref scope_die = 0;
1248090075Sobrien  int nested = 0;
1248190075Sobrien  int complete = (TYPE_SIZE (type)
1248290075Sobrien		  && (! TYPE_STUB_DECL (type)
1248390075Sobrien		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12484132718Skan  int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
1248550397Sobrien
1248690075Sobrien  if (type_die && ! complete)
1248750397Sobrien    return;
1248850397Sobrien
1248950397Sobrien  if (TYPE_CONTEXT (type) != NULL_TREE
12490132718Skan      && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12491132718Skan	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
1249250397Sobrien    nested = 1;
1249350397Sobrien
1249450397Sobrien  scope_die = scope_die_for (type, context_die);
1249550397Sobrien
1249650397Sobrien  if (! type_die || (nested && scope_die == comp_unit_die))
1249750397Sobrien    /* First occurrence of type or toplevel definition of nested class.  */
1249850397Sobrien    {
1249990075Sobrien      dw_die_ref old_die = type_die;
1250050397Sobrien
1250150397Sobrien      type_die = new_die (TREE_CODE (type) == RECORD_TYPE
1250250397Sobrien			  ? DW_TAG_structure_type : DW_TAG_union_type,
1250390075Sobrien			  scope_die, type);
1250450397Sobrien      equate_type_number_to_die (type, type_die);
1250550397Sobrien      if (old_die)
12506132718Skan	add_AT_specification (type_die, old_die);
1250790075Sobrien      else
1250890075Sobrien	add_name_attribute (type_die, type_tag (type));
1250950397Sobrien    }
1251050397Sobrien  else
1251150397Sobrien    remove_AT (type_die, DW_AT_declaration);
1251250397Sobrien
1251350397Sobrien  /* If this type has been completed, then give it a byte_size attribute and
1251450397Sobrien     then give a list of members.  */
12515132718Skan  if (complete && !ns_decl)
1251650397Sobrien    {
1251790075Sobrien      /* Prevent infinite recursion in cases where the type of some member of
12518132718Skan	 this type is expressed in terms of this type itself.  */
1251950397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1252050397Sobrien      add_byte_size_attribute (type_die, type);
1252150397Sobrien      if (TYPE_STUB_DECL (type) != NULL_TREE)
1252250397Sobrien	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
1252350397Sobrien
1252450397Sobrien      /* If the first reference to this type was as the return type of an
1252550397Sobrien	 inline function, then it may not have a parent.  Fix this now.  */
1252650397Sobrien      if (type_die->die_parent == NULL)
1252750397Sobrien	add_child_die (scope_die, type_die);
1252850397Sobrien
1252950397Sobrien      push_decl_scope (type);
1253050397Sobrien      gen_member_die (type, type_die);
1253150397Sobrien      pop_decl_scope ();
1253250397Sobrien
1253350397Sobrien      /* GNU extension: Record what type our vtable lives in.  */
1253450397Sobrien      if (TYPE_VFIELD (type))
1253550397Sobrien	{
1253650397Sobrien	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
1253750397Sobrien
1253850397Sobrien	  gen_type_die (vtype, context_die);
1253950397Sobrien	  add_AT_die_ref (type_die, DW_AT_containing_type,
1254050397Sobrien			  lookup_type_die (vtype));
1254150397Sobrien	}
1254250397Sobrien    }
1254350397Sobrien  else
1254452284Sobrien    {
1254552284Sobrien      add_AT_flag (type_die, DW_AT_declaration, 1);
1254652284Sobrien
1254790075Sobrien      /* We don't need to do this for function-local types.  */
1254890075Sobrien      if (TYPE_STUB_DECL (type)
1254990075Sobrien	  && ! decl_function_context (TYPE_STUB_DECL (type)))
12550169689Skan	VEC_safe_push (tree, gc, incomplete_types, type);
1255152284Sobrien    }
1255250397Sobrien}
1255350397Sobrien
1255450397Sobrien/* Generate a DIE for a subroutine _type_.  */
1255550397Sobrien
1255650397Sobrienstatic void
12557132718Skangen_subroutine_type_die (tree type, dw_die_ref context_die)
1255850397Sobrien{
1255990075Sobrien  tree return_type = TREE_TYPE (type);
1256090075Sobrien  dw_die_ref subr_die
1256190075Sobrien    = new_die (DW_TAG_subroutine_type,
1256290075Sobrien	       scope_die_for (type, context_die), type);
1256350397Sobrien
1256450397Sobrien  equate_type_number_to_die (type, subr_die);
1256550397Sobrien  add_prototyped_attribute (subr_die, type);
1256650397Sobrien  add_type_attribute (subr_die, return_type, 0, 0, context_die);
1256750397Sobrien  gen_formal_types_die (type, subr_die);
1256850397Sobrien}
1256950397Sobrien
12570132718Skan/* Generate a DIE for a type definition.  */
1257150397Sobrien
1257250397Sobrienstatic void
12573132718Skangen_typedef_die (tree decl, dw_die_ref context_die)
1257450397Sobrien{
1257590075Sobrien  dw_die_ref type_die;
1257690075Sobrien  tree origin;
1257750397Sobrien
1257850397Sobrien  if (TREE_ASM_WRITTEN (decl))
1257950397Sobrien    return;
1258090075Sobrien
1258150397Sobrien  TREE_ASM_WRITTEN (decl) = 1;
1258290075Sobrien  type_die = new_die (DW_TAG_typedef, context_die, decl);
1258350397Sobrien  origin = decl_ultimate_origin (decl);
1258450397Sobrien  if (origin != NULL)
1258550397Sobrien    add_abstract_origin_attribute (type_die, origin);
1258650397Sobrien  else
1258750397Sobrien    {
1258890075Sobrien      tree type;
1258990075Sobrien
1259050397Sobrien      add_name_and_src_coords_attributes (type_die, decl);
1259150397Sobrien      if (DECL_ORIGINAL_TYPE (decl))
1259250397Sobrien	{
1259350397Sobrien	  type = DECL_ORIGINAL_TYPE (decl);
1259490075Sobrien
12595169689Skan	  gcc_assert (type != TREE_TYPE (decl));
12596169689Skan	  equate_type_number_to_die (TREE_TYPE (decl), type_die);
1259750397Sobrien	}
1259850397Sobrien      else
1259950397Sobrien	type = TREE_TYPE (decl);
1260090075Sobrien
1260150397Sobrien      add_type_attribute (type_die, type, TREE_READONLY (decl),
1260250397Sobrien			  TREE_THIS_VOLATILE (decl), context_die);
1260350397Sobrien    }
1260450397Sobrien
1260550397Sobrien  if (DECL_ABSTRACT (decl))
1260650397Sobrien    equate_decl_number_to_die (decl, type_die);
1260750397Sobrien}
1260850397Sobrien
1260950397Sobrien/* Generate a type description DIE.  */
1261050397Sobrien
1261150397Sobrienstatic void
12612132718Skangen_type_die (tree type, dw_die_ref context_die)
1261350397Sobrien{
1261490075Sobrien  int need_pop;
1261590075Sobrien
1261650397Sobrien  if (type == NULL_TREE || type == error_mark_node)
1261750397Sobrien    return;
1261850397Sobrien
1261950397Sobrien  if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1262050397Sobrien      && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1262190075Sobrien    {
12622117395Skan      if (TREE_ASM_WRITTEN (type))
12623117395Skan	return;
12624117395Skan
12625117395Skan      /* Prevent broken recursion; we can't hand off to the same type.  */
12626169689Skan      gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12627117395Skan
1262850397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1262950397Sobrien      gen_decl_die (TYPE_NAME (type), context_die);
1263050397Sobrien      return;
1263150397Sobrien    }
1263250397Sobrien
12633117395Skan  /* We are going to output a DIE to represent the unqualified version
12634117395Skan     of this type (i.e. without any const or volatile qualifiers) so
12635117395Skan     get the main variant (i.e. the unqualified version) of this type
12636117395Skan     now.  (Vectors are special because the debugging info is in the
12637117395Skan     cloned type itself).  */
12638117395Skan  if (TREE_CODE (type) != VECTOR_TYPE)
12639117395Skan    type = type_main_variant (type);
12640117395Skan
12641117395Skan  if (TREE_ASM_WRITTEN (type))
12642117395Skan    return;
12643117395Skan
1264450397Sobrien  switch (TREE_CODE (type))
1264550397Sobrien    {
1264650397Sobrien    case ERROR_MARK:
1264750397Sobrien      break;
1264850397Sobrien
1264950397Sobrien    case POINTER_TYPE:
1265050397Sobrien    case REFERENCE_TYPE:
1265150397Sobrien      /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
1265250397Sobrien	 ensures that the gen_type_die recursion will terminate even if the
1265350397Sobrien	 type is recursive.  Recursive types are possible in Ada.  */
1265450397Sobrien      /* ??? We could perhaps do this for all types before the switch
1265550397Sobrien	 statement.  */
1265650397Sobrien      TREE_ASM_WRITTEN (type) = 1;
1265750397Sobrien
1265850397Sobrien      /* For these types, all that is required is that we output a DIE (or a
12659132718Skan	 set of DIEs) to represent the "basis" type.  */
1266050397Sobrien      gen_type_die (TREE_TYPE (type), context_die);
1266150397Sobrien      break;
1266250397Sobrien
1266350397Sobrien    case OFFSET_TYPE:
1266490075Sobrien      /* This code is used for C++ pointer-to-data-member types.
1266550397Sobrien	 Output a description of the relevant class type.  */
1266650397Sobrien      gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
1266750397Sobrien
1266850397Sobrien      /* Output a description of the type of the object pointed to.  */
1266950397Sobrien      gen_type_die (TREE_TYPE (type), context_die);
1267050397Sobrien
1267150397Sobrien      /* Now output a DIE to represent this pointer-to-data-member type
12672132718Skan	 itself.  */
1267350397Sobrien      gen_ptr_to_mbr_type_die (type, context_die);
1267450397Sobrien      break;
1267550397Sobrien
1267650397Sobrien    case FUNCTION_TYPE:
1267750397Sobrien      /* Force out return type (in case it wasn't forced out already).  */
1267850397Sobrien      gen_type_die (TREE_TYPE (type), context_die);
1267950397Sobrien      gen_subroutine_type_die (type, context_die);
1268050397Sobrien      break;
1268150397Sobrien
1268250397Sobrien    case METHOD_TYPE:
1268350397Sobrien      /* Force out return type (in case it wasn't forced out already).  */
1268450397Sobrien      gen_type_die (TREE_TYPE (type), context_die);
1268550397Sobrien      gen_subroutine_type_die (type, context_die);
1268650397Sobrien      break;
1268750397Sobrien
1268850397Sobrien    case ARRAY_TYPE:
12689169689Skan      gen_array_type_die (type, context_die);
1269050397Sobrien      break;
1269150397Sobrien
1269290075Sobrien    case VECTOR_TYPE:
1269396263Sobrien      gen_array_type_die (type, context_die);
1269490075Sobrien      break;
1269590075Sobrien
1269650397Sobrien    case ENUMERAL_TYPE:
1269750397Sobrien    case RECORD_TYPE:
1269850397Sobrien    case UNION_TYPE:
1269950397Sobrien    case QUAL_UNION_TYPE:
1270090075Sobrien      /* If this is a nested type whose containing class hasn't been written
12701132718Skan	 out yet, writing it out will cover this one, too.  This does not apply
12702132718Skan	 to instantiations of member class templates; they need to be added to
12703132718Skan	 the containing class as they are generated.  FIXME: This hurts the
12704132718Skan	 idea of combining type decls from multiple TUs, since we can't predict
12705132718Skan	 what set of template instantiations we'll get.  */
1270650397Sobrien      if (TYPE_CONTEXT (type)
1270752284Sobrien	  && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
1270850397Sobrien	  && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
1270950397Sobrien	{
1271050397Sobrien	  gen_type_die (TYPE_CONTEXT (type), context_die);
1271150397Sobrien
1271290075Sobrien	  if (TREE_ASM_WRITTEN (type))
1271350397Sobrien	    return;
1271450397Sobrien
1271550397Sobrien	  /* If that failed, attach ourselves to the stub.  */
1271650397Sobrien	  push_decl_scope (TYPE_CONTEXT (type));
1271750397Sobrien	  context_die = lookup_type_die (TYPE_CONTEXT (type));
1271890075Sobrien	  need_pop = 1;
1271950397Sobrien	}
1272090075Sobrien      else
12721132718Skan	{
12722132718Skan	  declare_in_namespace (type, context_die);
12723132718Skan	  need_pop = 0;
12724132718Skan	}
1272550397Sobrien
1272650397Sobrien      if (TREE_CODE (type) == ENUMERAL_TYPE)
12727169689Skan	{
12728169689Skan	  /* This might have been written out by the call to
12729169689Skan	     declare_in_namespace.  */
12730169689Skan	  if (!TREE_ASM_WRITTEN (type))
12731169689Skan	    gen_enumeration_type_die (type, context_die);
12732169689Skan	}
1273350397Sobrien      else
1273450397Sobrien	gen_struct_or_union_type_die (type, context_die);
1273550397Sobrien
1273690075Sobrien      if (need_pop)
1273750397Sobrien	pop_decl_scope ();
1273850397Sobrien
1273950397Sobrien      /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
1274050397Sobrien	 it up if it is ever completed.  gen_*_type_die will set it for us
1274150397Sobrien	 when appropriate.  */
1274250397Sobrien      return;
1274350397Sobrien
1274450397Sobrien    case VOID_TYPE:
1274550397Sobrien    case INTEGER_TYPE:
1274650397Sobrien    case REAL_TYPE:
1274750397Sobrien    case COMPLEX_TYPE:
1274850397Sobrien    case BOOLEAN_TYPE:
1274950397Sobrien      /* No DIEs needed for fundamental types.  */
1275050397Sobrien      break;
1275150397Sobrien
1275250397Sobrien    case LANG_TYPE:
1275350397Sobrien      /* No Dwarf representation currently defined.  */
1275450397Sobrien      break;
1275550397Sobrien
1275650397Sobrien    default:
12757169689Skan      gcc_unreachable ();
1275850397Sobrien    }
1275950397Sobrien
1276050397Sobrien  TREE_ASM_WRITTEN (type) = 1;
1276150397Sobrien}
1276250397Sobrien
1276350397Sobrien/* Generate a DIE for a tagged type instantiation.  */
1276450397Sobrien
1276550397Sobrienstatic void
12766132718Skangen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
1276750397Sobrien{
1276850397Sobrien  if (type == NULL_TREE || type == error_mark_node)
1276950397Sobrien    return;
1277050397Sobrien
1277150397Sobrien  /* We are going to output a DIE to represent the unqualified version of
1277250397Sobrien     this type (i.e. without any const or volatile qualifiers) so make sure
1277350397Sobrien     that we have the main variant (i.e. the unqualified version) of this
1277450397Sobrien     type now.  */
12775169689Skan  gcc_assert (type == type_main_variant (type));
1277650397Sobrien
1277790075Sobrien  /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
1277890075Sobrien     an instance of an unresolved type.  */
1277990075Sobrien
1278050397Sobrien  switch (TREE_CODE (type))
1278150397Sobrien    {
1278250397Sobrien    case ERROR_MARK:
1278350397Sobrien      break;
1278450397Sobrien
1278550397Sobrien    case ENUMERAL_TYPE:
1278650397Sobrien      gen_inlined_enumeration_type_die (type, context_die);
1278750397Sobrien      break;
1278850397Sobrien
1278950397Sobrien    case RECORD_TYPE:
1279050397Sobrien      gen_inlined_structure_type_die (type, context_die);
1279150397Sobrien      break;
1279250397Sobrien
1279350397Sobrien    case UNION_TYPE:
1279450397Sobrien    case QUAL_UNION_TYPE:
1279550397Sobrien      gen_inlined_union_type_die (type, context_die);
1279650397Sobrien      break;
1279750397Sobrien
1279850397Sobrien    default:
12799169689Skan      gcc_unreachable ();
1280050397Sobrien    }
1280150397Sobrien}
1280250397Sobrien
1280350397Sobrien/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
1280450397Sobrien   things which are local to the given block.  */
1280550397Sobrien
1280650397Sobrienstatic void
12807132718Skangen_block_die (tree stmt, dw_die_ref context_die, int depth)
1280850397Sobrien{
1280990075Sobrien  int must_output_die = 0;
1281090075Sobrien  tree origin;
1281190075Sobrien  tree decl;
1281290075Sobrien  enum tree_code origin_code;
1281350397Sobrien
12814169689Skan  /* Ignore blocks that are NULL.  */
12815169689Skan  if (stmt == NULL_TREE)
1281650397Sobrien    return;
1281750397Sobrien
1281890075Sobrien  /* If the block is one fragment of a non-contiguous block, do not
1281990075Sobrien     process the variables, since they will have been done by the
1282090075Sobrien     origin block.  Do process subblocks.  */
1282190075Sobrien  if (BLOCK_FRAGMENT_ORIGIN (stmt))
1282290075Sobrien    {
1282390075Sobrien      tree sub;
1282490075Sobrien
1282590075Sobrien      for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
1282690075Sobrien	gen_block_die (sub, context_die, depth + 1);
1282790075Sobrien
1282890075Sobrien      return;
1282990075Sobrien    }
1283090075Sobrien
1283150397Sobrien  /* Determine the "ultimate origin" of this block.  This block may be an
1283250397Sobrien     inlined instance of an inlined instance of inline function, so we have
1283350397Sobrien     to trace all of the way back through the origin chain to find out what
1283450397Sobrien     sort of node actually served as the original seed for the creation of
1283550397Sobrien     the current block.  */
1283650397Sobrien  origin = block_ultimate_origin (stmt);
1283750397Sobrien  origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
1283850397Sobrien
1283950397Sobrien  /* Determine if we need to output any Dwarf DIEs at all to represent this
1284050397Sobrien     block.  */
1284150397Sobrien  if (origin_code == FUNCTION_DECL)
1284250397Sobrien    /* The outer scopes for inlinings *must* always be represented.  We
1284350397Sobrien       generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
1284450397Sobrien    must_output_die = 1;
1284550397Sobrien  else
1284650397Sobrien    {
1284750397Sobrien      /* In the case where the current block represents an inlining of the
12848132718Skan	 "body block" of an inline function, we must *NOT* output any DIE for
12849132718Skan	 this block because we have already output a DIE to represent the whole
12850132718Skan	 inlined function scope and the "body block" of any function doesn't
12851132718Skan	 really represent a different scope according to ANSI C rules.  So we
12852132718Skan	 check here to make sure that this block does not represent a "body
12853132718Skan	 block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
1285450397Sobrien      if (! is_body_block (origin ? origin : stmt))
1285550397Sobrien	{
1285650397Sobrien	  /* Determine if this block directly contains any "significant"
1285750397Sobrien	     local declarations which we will need to output DIEs for.  */
1285850397Sobrien	  if (debug_info_level > DINFO_LEVEL_TERSE)
1285950397Sobrien	    /* We are not in terse mode so *any* local declaration counts
1286050397Sobrien	       as being a "significant" one.  */
12861169689Skan	    must_output_die = (BLOCK_VARS (stmt) != NULL
12862169689Skan			       && (TREE_USED (stmt)
12863169689Skan				   || TREE_ASM_WRITTEN (stmt)
12864169689Skan				   || BLOCK_ABSTRACT (stmt)));
1286550397Sobrien	  else
1286650397Sobrien	    /* We are in terse mode, so only local (nested) function
1286750397Sobrien	       definitions count as "significant" local declarations.  */
1286850397Sobrien	    for (decl = BLOCK_VARS (stmt);
1286950397Sobrien		 decl != NULL; decl = TREE_CHAIN (decl))
1287050397Sobrien	      if (TREE_CODE (decl) == FUNCTION_DECL
1287150397Sobrien		  && DECL_INITIAL (decl))
1287250397Sobrien		{
1287350397Sobrien		  must_output_die = 1;
1287450397Sobrien		  break;
1287550397Sobrien		}
1287650397Sobrien	}
1287750397Sobrien    }
1287850397Sobrien
1287950397Sobrien  /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
1288050397Sobrien     DIE for any block which contains no significant local declarations at
1288150397Sobrien     all.  Rather, in such cases we just call `decls_for_scope' so that any
1288250397Sobrien     needed Dwarf info for any sub-blocks will get properly generated. Note
1288350397Sobrien     that in terse mode, our definition of what constitutes a "significant"
1288450397Sobrien     local declaration gets restricted to include only inlined function
1288550397Sobrien     instances and local (nested) function definitions.  */
1288650397Sobrien  if (must_output_die)
1288750397Sobrien    {
1288850397Sobrien      if (origin_code == FUNCTION_DECL)
1288950397Sobrien	gen_inlined_subroutine_die (stmt, context_die, depth);
1289050397Sobrien      else
1289150397Sobrien	gen_lexical_block_die (stmt, context_die, depth);
1289250397Sobrien    }
1289350397Sobrien  else
1289450397Sobrien    decls_for_scope (stmt, context_die, depth);
1289550397Sobrien}
1289650397Sobrien
1289750397Sobrien/* Generate all of the decls declared within a given scope and (recursively)
1289850397Sobrien   all of its sub-blocks.  */
1289950397Sobrien
1290050397Sobrienstatic void
12901132718Skandecls_for_scope (tree stmt, dw_die_ref context_die, int depth)
1290250397Sobrien{
1290390075Sobrien  tree decl;
1290490075Sobrien  tree subblocks;
1290550397Sobrien
12906169689Skan  /* Ignore NULL blocks.  */
12907169689Skan  if (stmt == NULL_TREE)
1290850397Sobrien    return;
1290950397Sobrien
12910169689Skan  if (TREE_USED (stmt))
1291150397Sobrien    {
12912169689Skan      /* Output the DIEs to represent all of the data objects and typedefs
12913169689Skan	 declared directly within this block but not within any nested
12914169689Skan	 sub-blocks.  Also, nested function and tag DIEs have been
12915169689Skan	 generated with a parent of NULL; fix that up now.  */
12916169689Skan      for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12917169689Skan	{
12918169689Skan	  dw_die_ref die;
12919169689Skan
12920169689Skan	  if (TREE_CODE (decl) == FUNCTION_DECL)
12921169689Skan	    die = lookup_decl_die (decl);
12922169689Skan	  else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12923169689Skan	    die = lookup_type_die (TREE_TYPE (decl));
12924169689Skan	  else
12925169689Skan	    die = NULL;
12926169689Skan
12927169689Skan	  if (die != NULL && die->die_parent == NULL)
12928169689Skan	    add_child_die (context_die, die);
12929169689Skan	  /* Do not produce debug information for static variables since
12930169689Skan	     these might be optimized out.  We are called for these later
12931169689Skan	     in cgraph_varpool_analyze_pending_decls. */
12932169689Skan	  if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12933169689Skan	    ;
12934169689Skan	  else
12935169689Skan	    gen_decl_die (decl, context_die);
12936169689Skan	}
1293750397Sobrien    }
1293850397Sobrien
12939117395Skan  /* If we're at -g1, we're not interested in subblocks.  */
12940117395Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
12941117395Skan    return;
12942117395Skan
1294350397Sobrien  /* Output the DIEs to represent all sub-blocks (and the items declared
1294450397Sobrien     therein) of this block.  */
1294550397Sobrien  for (subblocks = BLOCK_SUBBLOCKS (stmt);
1294650397Sobrien       subblocks != NULL;
1294750397Sobrien       subblocks = BLOCK_CHAIN (subblocks))
1294850397Sobrien    gen_block_die (subblocks, context_die, depth + 1);
1294950397Sobrien}
1295050397Sobrien
1295150397Sobrien/* Is this a typedef we can avoid emitting?  */
1295250397Sobrien
1295350397Sobrienstatic inline int
12954132718Skanis_redundant_typedef (tree decl)
1295550397Sobrien{
1295650397Sobrien  if (TYPE_DECL_IS_STUB (decl))
1295750397Sobrien    return 1;
1295850397Sobrien
1295950397Sobrien  if (DECL_ARTIFICIAL (decl)
1296050397Sobrien      && DECL_CONTEXT (decl)
1296150397Sobrien      && is_tagged_type (DECL_CONTEXT (decl))
1296250397Sobrien      && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
1296350397Sobrien      && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
1296450397Sobrien    /* Also ignore the artificial member typedef for the class name.  */
1296550397Sobrien    return 1;
1296650397Sobrien
1296750397Sobrien  return 0;
1296850397Sobrien}
1296950397Sobrien
12970169689Skan/* Returns the DIE for decl.  A DIE will always be returned.  */
12971132718Skan
12972169689Skanstatic dw_die_ref
12973169689Skanforce_decl_die (tree decl)
12974169689Skan{
12975169689Skan  dw_die_ref decl_die;
12976169689Skan  unsigned saved_external_flag;
12977169689Skan  tree save_fn = NULL_TREE;
12978169689Skan  decl_die = lookup_decl_die (decl);
12979169689Skan  if (!decl_die)
12980169689Skan    {
12981169689Skan      dw_die_ref context_die;
12982169689Skan      tree decl_context = DECL_CONTEXT (decl);
12983169689Skan      if (decl_context)
12984169689Skan	{
12985169689Skan	  /* Find die that represents this context.  */
12986169689Skan	  if (TYPE_P (decl_context))
12987169689Skan	    context_die = force_type_die (decl_context);
12988169689Skan	  else
12989169689Skan	    context_die = force_decl_die (decl_context);
12990169689Skan	}
12991169689Skan      else
12992169689Skan	context_die = comp_unit_die;
12993132718Skan
12994169689Skan      decl_die = lookup_decl_die (decl);
12995169689Skan      if (decl_die)
12996169689Skan	return decl_die;
12997169689Skan
12998169689Skan      switch (TREE_CODE (decl))
12999169689Skan	{
13000169689Skan	case FUNCTION_DECL:
13001169689Skan	  /* Clear current_function_decl, so that gen_subprogram_die thinks
13002169689Skan	     that this is a declaration. At this point, we just want to force
13003169689Skan	     declaration die.  */
13004169689Skan	  save_fn = current_function_decl;
13005169689Skan	  current_function_decl = NULL_TREE;
13006169689Skan	  gen_subprogram_die (decl, context_die);
13007169689Skan	  current_function_decl = save_fn;
13008169689Skan	  break;
13009169689Skan
13010169689Skan	case VAR_DECL:
13011169689Skan	  /* Set external flag to force declaration die. Restore it after
13012169689Skan	   gen_decl_die() call.  */
13013169689Skan	  saved_external_flag = DECL_EXTERNAL (decl);
13014169689Skan	  DECL_EXTERNAL (decl) = 1;
13015169689Skan	  gen_decl_die (decl, context_die);
13016169689Skan	  DECL_EXTERNAL (decl) = saved_external_flag;
13017169689Skan	  break;
13018169689Skan
13019169689Skan	case NAMESPACE_DECL:
13020169689Skan	  dwarf2out_decl (decl);
13021169689Skan	  break;
13022169689Skan
13023169689Skan	default:
13024169689Skan	  gcc_unreachable ();
13025169689Skan	}
13026169689Skan
13027169689Skan      /* We should be able to find the DIE now.  */
13028169689Skan      if (!decl_die)
13029169689Skan	decl_die = lookup_decl_die (decl);
13030169689Skan      gcc_assert (decl_die);
13031169689Skan    }
13032169689Skan
13033169689Skan  return decl_die;
13034169689Skan}
13035169689Skan
13036169689Skan/* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13037169689Skan   always returned.  */
13038169689Skan
13039132718Skanstatic dw_die_ref
13040169689Skanforce_type_die (tree type)
13041132718Skan{
13042169689Skan  dw_die_ref type_die;
13043132718Skan
13044169689Skan  type_die = lookup_type_die (type);
13045169689Skan  if (!type_die)
13046169689Skan    {
13047169689Skan      dw_die_ref context_die;
13048169689Skan      if (TYPE_CONTEXT (type))
13049169689Skan	{
13050169689Skan	  if (TYPE_P (TYPE_CONTEXT (type)))
13051169689Skan	    context_die = force_type_die (TYPE_CONTEXT (type));
13052169689Skan	  else
13053169689Skan	    context_die = force_decl_die (TYPE_CONTEXT (type));
13054169689Skan	}
13055169689Skan      else
13056169689Skan	context_die = comp_unit_die;
13057132718Skan
13058169689Skan      type_die = lookup_type_die (type);
13059169689Skan      if (type_die)
13060169689Skan	return type_die;
13061169689Skan      gen_type_die (type, context_die);
13062169689Skan      type_die = lookup_type_die (type);
13063169689Skan      gcc_assert (type_die);
13064169689Skan    }
13065169689Skan  return type_die;
13066132718Skan}
13067132718Skan
13068132718Skan/* Force out any required namespaces to be able to output DECL,
13069132718Skan   and return the new context_die for it, if it's changed.  */
13070132718Skan
13071132718Skanstatic dw_die_ref
13072132718Skansetup_namespace_context (tree thing, dw_die_ref context_die)
13073132718Skan{
13074169689Skan  tree context = (DECL_P (thing)
13075169689Skan		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13076132718Skan  if (context && TREE_CODE (context) == NAMESPACE_DECL)
13077132718Skan    /* Force out the namespace.  */
13078169689Skan    context_die = force_decl_die (context);
13079132718Skan
13080132718Skan  return context_die;
13081132718Skan}
13082132718Skan
13083132718Skan/* Emit a declaration DIE for THING (which is either a DECL or a tagged
13084132718Skan   type) within its namespace, if appropriate.
13085132718Skan
13086132718Skan   For compatibility with older debuggers, namespace DIEs only contain
13087132718Skan   declarations; all definitions are emitted at CU scope.  */
13088132718Skan
13089132718Skanstatic void
13090132718Skandeclare_in_namespace (tree thing, dw_die_ref context_die)
13091132718Skan{
13092132718Skan  dw_die_ref ns_context;
13093132718Skan
13094132718Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13095132718Skan    return;
13096132718Skan
13097169689Skan  /* If this decl is from an inlined function, then don't try to emit it in its
13098169689Skan     namespace, as we will get confused.  It would have already been emitted
13099169689Skan     when the abstract instance of the inline function was emitted anyways.  */
13100169689Skan  if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13101169689Skan    return;
13102169689Skan
13103132718Skan  ns_context = setup_namespace_context (thing, context_die);
13104132718Skan
13105132718Skan  if (ns_context != context_die)
13106132718Skan    {
13107132718Skan      if (DECL_P (thing))
13108132718Skan	gen_decl_die (thing, ns_context);
13109132718Skan      else
13110132718Skan	gen_type_die (thing, ns_context);
13111132718Skan    }
13112132718Skan}
13113132718Skan
13114132718Skan/* Generate a DIE for a namespace or namespace alias.  */
13115132718Skan
13116132718Skanstatic void
13117132718Skangen_namespace_die (tree decl)
13118132718Skan{
13119132718Skan  dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13120132718Skan
13121132718Skan  /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13122132718Skan     they are an alias of.  */
13123132718Skan  if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13124132718Skan    {
13125132718Skan      /* Output a real namespace.  */
13126132718Skan      dw_die_ref namespace_die
13127132718Skan	= new_die (DW_TAG_namespace, context_die, decl);
13128132718Skan      add_name_and_src_coords_attributes (namespace_die, decl);
13129132718Skan      equate_decl_number_to_die (decl, namespace_die);
13130132718Skan    }
13131132718Skan  else
13132132718Skan    {
13133132718Skan      /* Output a namespace alias.  */
13134132718Skan
13135132718Skan      /* Force out the namespace we are an alias of, if necessary.  */
13136132718Skan      dw_die_ref origin_die
13137169689Skan	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13138132718Skan
13139132718Skan      /* Now create the namespace alias DIE.  */
13140132718Skan      dw_die_ref namespace_die
13141132718Skan	= new_die (DW_TAG_imported_declaration, context_die, decl);
13142132718Skan      add_name_and_src_coords_attributes (namespace_die, decl);
13143132718Skan      add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13144132718Skan      equate_decl_number_to_die (decl, namespace_die);
13145132718Skan    }
13146132718Skan}
13147132718Skan
1314850397Sobrien/* Generate Dwarf debug information for a decl described by DECL.  */
1314950397Sobrien
1315050397Sobrienstatic void
13151132718Skangen_decl_die (tree decl, dw_die_ref context_die)
1315250397Sobrien{
1315390075Sobrien  tree origin;
1315450397Sobrien
1315590075Sobrien  if (DECL_P (decl) && DECL_IGNORED_P (decl))
1315650397Sobrien    return;
1315750397Sobrien
1315850397Sobrien  switch (TREE_CODE (decl))
1315950397Sobrien    {
1316090075Sobrien    case ERROR_MARK:
1316190075Sobrien      break;
1316290075Sobrien
1316350397Sobrien    case CONST_DECL:
1316490075Sobrien      /* The individual enumerators of an enum type get output when we output
13165132718Skan	 the Dwarf representation of the relevant enum type itself.  */
1316650397Sobrien      break;
1316750397Sobrien
1316850397Sobrien    case FUNCTION_DECL:
1316950397Sobrien      /* Don't output any DIEs to represent mere function declarations,
1317050397Sobrien	 unless they are class members or explicit block externs.  */
1317150397Sobrien      if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
1317290075Sobrien	  && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
1317350397Sobrien	break;
1317450397Sobrien
13175169689Skan#if 0
13176169689Skan      /* FIXME */
13177169689Skan      /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13178169689Skan	 on local redeclarations of global functions.  That seems broken.  */
13179169689Skan      if (current_function_decl != decl)
13180169689Skan	/* This is only a declaration.  */;
13181169689Skan#endif
13182169689Skan
1318390075Sobrien      /* If we're emitting a clone, emit info for the abstract instance.  */
1318490075Sobrien      if (DECL_ORIGIN (decl) != decl)
1318590075Sobrien	dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
1318690075Sobrien
1318790075Sobrien      /* If we're emitting an out-of-line copy of an inline function,
1318890075Sobrien	 emit info for the abstract instance and set up to refer to it.  */
13189132718Skan      else if (cgraph_function_possibly_inlined_p (decl)
13190132718Skan	       && ! DECL_ABSTRACT (decl)
13191132718Skan	       && ! class_or_namespace_scope_p (context_die)
1319290075Sobrien	       /* dwarf2out_abstract_function won't emit a die if this is just
1319390075Sobrien		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
1319490075Sobrien		  that case, because that works only if we have a die.  */
1319590075Sobrien	       && DECL_INITIAL (decl) != NULL_TREE)
1319650397Sobrien	{
1319790075Sobrien	  dwarf2out_abstract_function (decl);
1319890075Sobrien	  set_decl_origin_self (decl);
1319990075Sobrien	}
1320090075Sobrien
1320190075Sobrien      /* Otherwise we're emitting the primary DIE for this decl.  */
1320290075Sobrien      else if (debug_info_level > DINFO_LEVEL_TERSE)
1320390075Sobrien	{
1320450397Sobrien	  /* Before we describe the FUNCTION_DECL itself, make sure that we
1320550397Sobrien	     have described its return type.  */
1320650397Sobrien	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
1320750397Sobrien
1320890075Sobrien	  /* And its virtual context.  */
1320990075Sobrien	  if (DECL_VINDEX (decl) != NULL_TREE)
1321090075Sobrien	    gen_type_die (DECL_CONTEXT (decl), context_die);
1321190075Sobrien
1321250397Sobrien	  /* And its containing type.  */
1321350397Sobrien	  origin = decl_class_context (decl);
1321450397Sobrien	  if (origin != NULL_TREE)
1321590075Sobrien	    gen_type_die_for_member (origin, decl, context_die);
13216132718Skan
13217132718Skan	  /* And its containing namespace.  */
13218132718Skan	  declare_in_namespace (decl, context_die);
1321950397Sobrien	}
1322050397Sobrien
1322150397Sobrien      /* Now output a DIE to represent the function itself.  */
1322250397Sobrien      gen_subprogram_die (decl, context_die);
1322350397Sobrien      break;
1322450397Sobrien
1322550397Sobrien    case TYPE_DECL:
1322650397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13227132718Skan	 actual typedefs.  */
1322850397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1322950397Sobrien	break;
1323050397Sobrien
1323190075Sobrien      /* In the special case of a TYPE_DECL node representing the declaration
13232132718Skan	 of some type tag, if the given TYPE_DECL is marked as having been
13233132718Skan	 instantiated from some other (original) TYPE_DECL node (e.g. one which
13234132718Skan	 was generated within the original definition of an inline function) we
13235132718Skan	 have to generate a special (abbreviated) DW_TAG_structure_type,
13236132718Skan	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13237220150Smm      if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13238220150Smm	  && is_tagged_type (TREE_TYPE (decl)))
1323950397Sobrien	{
1324050397Sobrien	  gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
1324150397Sobrien	  break;
1324250397Sobrien	}
1324350397Sobrien
1324450397Sobrien      if (is_redundant_typedef (decl))
1324550397Sobrien	gen_type_die (TREE_TYPE (decl), context_die);
1324650397Sobrien      else
1324750397Sobrien	/* Output a DIE to represent the typedef itself.  */
1324850397Sobrien	gen_typedef_die (decl, context_die);
1324950397Sobrien      break;
1325050397Sobrien
1325150397Sobrien    case LABEL_DECL:
1325250397Sobrien      if (debug_info_level >= DINFO_LEVEL_NORMAL)
1325350397Sobrien	gen_label_die (decl, context_die);
1325450397Sobrien      break;
1325550397Sobrien
1325650397Sobrien    case VAR_DECL:
13257169689Skan    case RESULT_DECL:
1325850397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13259132718Skan	 variable declarations or definitions.  */
1326050397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1326150397Sobrien	break;
1326250397Sobrien
1326350397Sobrien      /* Output any DIEs that are needed to specify the type of this data
13264132718Skan	 object.  */
1326550397Sobrien      gen_type_die (TREE_TYPE (decl), context_die);
1326650397Sobrien
1326750397Sobrien      /* And its containing type.  */
1326850397Sobrien      origin = decl_class_context (decl);
1326950397Sobrien      if (origin != NULL_TREE)
1327090075Sobrien	gen_type_die_for_member (origin, decl, context_die);
1327150397Sobrien
13272132718Skan      /* And its containing namespace.  */
13273132718Skan      declare_in_namespace (decl, context_die);
13274132718Skan
1327550397Sobrien      /* Now output the DIE to represent the data object itself.  This gets
13276132718Skan	 complicated because of the possibility that the VAR_DECL really
13277132718Skan	 represents an inlined instance of a formal parameter for an inline
13278132718Skan	 function.  */
1327950397Sobrien      origin = decl_ultimate_origin (decl);
1328050397Sobrien      if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
1328150397Sobrien	gen_formal_parameter_die (decl, context_die);
1328250397Sobrien      else
1328350397Sobrien	gen_variable_die (decl, context_die);
1328450397Sobrien      break;
1328550397Sobrien
1328650397Sobrien    case FIELD_DECL:
1328790075Sobrien      /* Ignore the nameless fields that are used to skip bits but handle C++
13288169689Skan	 anonymous unions and structs.  */
1328950397Sobrien      if (DECL_NAME (decl) != NULL_TREE
13290169689Skan	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13291169689Skan	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
1329250397Sobrien	{
1329350397Sobrien	  gen_type_die (member_declared_type (decl), context_die);
1329450397Sobrien	  gen_field_die (decl, context_die);
1329550397Sobrien	}
1329650397Sobrien      break;
1329750397Sobrien
1329850397Sobrien    case PARM_DECL:
1329950397Sobrien      gen_type_die (TREE_TYPE (decl), context_die);
1330050397Sobrien      gen_formal_parameter_die (decl, context_die);
1330150397Sobrien      break;
1330250397Sobrien
1330390075Sobrien    case NAMESPACE_DECL:
13304132718Skan      gen_namespace_die (decl);
1330590075Sobrien      break;
1330690075Sobrien
1330750397Sobrien    default:
13308169689Skan      /* Probably some frontend-internal decl.  Assume we don't care.  */
13309169689Skan      gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13310169689Skan      break;
1331150397Sobrien    }
1331250397Sobrien}
1331350397Sobrien
1331490075Sobrien/* Output debug information for global decl DECL.  Called from toplev.c after
1331590075Sobrien   compilation proper has finished.  */
1331690075Sobrien
1331790075Sobrienstatic void
13318132718Skandwarf2out_global_decl (tree decl)
1331990075Sobrien{
1332090075Sobrien  /* Output DWARF2 information for file-scope tentative data object
1332190075Sobrien     declarations, file-scope (extern) function declarations (which had no
1332290075Sobrien     corresponding body) and file-scope tagged type declarations and
1332390075Sobrien     definitions which have not yet been forced out.  */
1332490075Sobrien  if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
1332590075Sobrien    dwarf2out_decl (decl);
1332690075Sobrien}
1332790075Sobrien
13328169689Skan/* Output debug information for type decl DECL.  Called from toplev.c
13329169689Skan   and from language front ends (to record built-in types).  */
13330169689Skanstatic void
13331169689Skandwarf2out_type_decl (tree decl, int local)
13332169689Skan{
13333169689Skan  if (!local)
13334169689Skan    dwarf2out_decl (decl);
13335169689Skan}
13336169689Skan
13337169689Skan/* Output debug information for imported module or decl.  */
13338169689Skan
13339169689Skanstatic void
13340169689Skandwarf2out_imported_module_or_decl (tree decl, tree context)
13341169689Skan{
13342169689Skan  dw_die_ref imported_die, at_import_die;
13343169689Skan  dw_die_ref scope_die;
13344169689Skan  expanded_location xloc;
13345169689Skan
13346169689Skan  if (debug_info_level <= DINFO_LEVEL_TERSE)
13347169689Skan    return;
13348169689Skan
13349169689Skan  gcc_assert (decl);
13350169689Skan
13351169689Skan  /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13352169689Skan     We need decl DIE for reference and scope die. First, get DIE for the decl
13353169689Skan     itself.  */
13354169689Skan
13355169689Skan  /* Get the scope die for decl context. Use comp_unit_die for global module
13356169689Skan     or decl. If die is not found for non globals, force new die.  */
13357169689Skan  if (!context)
13358169689Skan    scope_die = comp_unit_die;
13359169689Skan  else if (TYPE_P (context))
13360169689Skan    scope_die = force_type_die (context);
13361169689Skan  else
13362169689Skan    scope_die = force_decl_die (context);
13363169689Skan
13364169689Skan  /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13365169689Skan  if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13366169689Skan    {
13367169689Skan      if (is_base_type (TREE_TYPE (decl)))
13368169689Skan	at_import_die = base_type_die (TREE_TYPE (decl));
13369169689Skan      else
13370169689Skan	at_import_die = force_type_die (TREE_TYPE (decl));
13371169689Skan    }
13372169689Skan  else
13373169689Skan    {
13374169689Skan      at_import_die = lookup_decl_die (decl);
13375169689Skan      if (!at_import_die)
13376169689Skan	{
13377169689Skan	  /* If we're trying to avoid duplicate debug info, we may not have
13378169689Skan	     emitted the member decl for this field.  Emit it now.  */
13379169689Skan	  if (TREE_CODE (decl) == FIELD_DECL)
13380169689Skan	    {
13381169689Skan	      tree type = DECL_CONTEXT (decl);
13382169689Skan	      dw_die_ref type_context_die;
13383169689Skan
13384169689Skan	      if (TYPE_CONTEXT (type))
13385169689Skan		if (TYPE_P (TYPE_CONTEXT (type)))
13386169689Skan		  type_context_die = force_type_die (TYPE_CONTEXT (type));
13387169689Skan	      else
13388169689Skan		type_context_die = force_decl_die (TYPE_CONTEXT (type));
13389169689Skan	      else
13390169689Skan		type_context_die = comp_unit_die;
13391169689Skan	      gen_type_die_for_member (type, decl, type_context_die);
13392169689Skan	    }
13393169689Skan	  at_import_die = force_decl_die (decl);
13394169689Skan	}
13395169689Skan    }
13396169689Skan
13397169689Skan  /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13398169689Skan  if (TREE_CODE (decl) == NAMESPACE_DECL)
13399169689Skan    imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13400169689Skan  else
13401169689Skan    imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13402169689Skan
13403169689Skan  xloc = expand_location (input_location);
13404169689Skan  add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13405169689Skan  add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13406169689Skan  add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13407169689Skan}
13408169689Skan
1340990075Sobrien/* Write the debugging output for DECL.  */
1341090075Sobrien
1341190075Sobrienvoid
13412132718Skandwarf2out_decl (tree decl)
1341390075Sobrien{
1341490075Sobrien  dw_die_ref context_die = comp_unit_die;
1341590075Sobrien
1341650397Sobrien  switch (TREE_CODE (decl))
1341750397Sobrien    {
1341890075Sobrien    case ERROR_MARK:
1341990075Sobrien      return;
1342090075Sobrien
1342150397Sobrien    case FUNCTION_DECL:
1342250397Sobrien      /* What we would really like to do here is to filter out all mere
13423132718Skan	 file-scope declarations of file-scope functions which are never
13424132718Skan	 referenced later within this translation unit (and keep all of ones
13425132718Skan	 that *are* referenced later on) but we aren't clairvoyant, so we have
13426132718Skan	 no idea which functions will be referenced in the future (i.e. later
13427132718Skan	 on within the current translation unit). So here we just ignore all
13428132718Skan	 file-scope function declarations which are not also definitions.  If
13429132718Skan	 and when the debugger needs to know something about these functions,
13430132718Skan	 it will have to hunt around and find the DWARF information associated
13431132718Skan	 with the definition of the function.
1343290075Sobrien
1343390075Sobrien	 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13434132718Skan	 nodes represent definitions and which ones represent mere
13435132718Skan	 declarations.  We have to check DECL_INITIAL instead. That's because
13436132718Skan	 the C front-end supports some weird semantics for "extern inline"
13437132718Skan	 function definitions.  These can get inlined within the current
13438169689Skan	 translation unit (and thus, we need to generate Dwarf info for their
13439132718Skan	 abstract instances so that the Dwarf info for the concrete inlined
13440132718Skan	 instances can have something to refer to) but the compiler never
13441132718Skan	 generates any out-of-lines instances of such things (despite the fact
13442132718Skan	 that they *are* definitions).
1344390075Sobrien
1344490075Sobrien	 The important point is that the C front-end marks these "extern
1344590075Sobrien	 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
1344690075Sobrien	 them anyway. Note that the C++ front-end also plays some similar games
1344790075Sobrien	 for inline function definitions appearing within include files which
1344890075Sobrien	 also contain `#pragma interface' pragmas.  */
1344950397Sobrien      if (DECL_INITIAL (decl) == NULL_TREE)
1345050397Sobrien	return;
1345150397Sobrien
1345250397Sobrien      /* If we're a nested function, initially use a parent of NULL; if we're
1345350397Sobrien	 a plain function, this will be fixed up in decls_for_scope.  If
1345450397Sobrien	 we're a method, it will be ignored, since we already have a DIE.  */
13455117395Skan      if (decl_function_context (decl)
13456117395Skan	  /* But if we're in terse mode, we don't care about scope.  */
13457117395Skan	  && debug_info_level > DINFO_LEVEL_TERSE)
1345850397Sobrien	context_die = NULL;
1345950397Sobrien      break;
1346050397Sobrien
1346150397Sobrien    case VAR_DECL:
1346290075Sobrien      /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13463132718Skan	 declaration and if the declaration was never even referenced from
13464132718Skan	 within this entire compilation unit.  We suppress these DIEs in
13465132718Skan	 order to save space in the .debug section (by eliminating entries
13466132718Skan	 which are probably useless).  Note that we must not suppress
13467132718Skan	 block-local extern declarations (whether used or not) because that
13468132718Skan	 would screw-up the debugger's name lookup mechanism and cause it to
13469132718Skan	 miss things which really ought to be in scope at a given point.  */
1347050397Sobrien      if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
1347150397Sobrien	return;
1347250397Sobrien
13473169689Skan      /* For local statics lookup proper context die.  */
13474169689Skan      if (TREE_STATIC (decl) && decl_function_context (decl))
13475169689Skan	context_die = lookup_decl_die (DECL_CONTEXT (decl));
13476169689Skan
1347750397Sobrien      /* If we are in terse mode, don't generate any DIEs to represent any
13478132718Skan	 variable declarations or definitions.  */
1347950397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1348050397Sobrien	return;
1348150397Sobrien      break;
1348250397Sobrien
13483132718Skan    case NAMESPACE_DECL:
13484132718Skan      if (debug_info_level <= DINFO_LEVEL_TERSE)
13485132718Skan	return;
13486132718Skan      if (lookup_decl_die (decl) != NULL)
13487132718Skan        return;
13488132718Skan      break;
13489132718Skan
1349050397Sobrien    case TYPE_DECL:
1349190075Sobrien      /* Don't emit stubs for types unless they are needed by other DIEs.  */
1349290075Sobrien      if (TYPE_DECL_SUPPRESS_DEBUG (decl))
1349390075Sobrien	return;
1349490075Sobrien
1349550397Sobrien      /* Don't bother trying to generate any DIEs to represent any of the
13496132718Skan	 normal built-in types for the language we are compiling.  */
13497169689Skan      if (DECL_IS_BUILTIN (decl))
1349850397Sobrien	{
1349950397Sobrien	  /* OK, we need to generate one for `bool' so GDB knows what type
13500132718Skan	     comparisons have.  */
13501169689Skan	  if (is_cxx ()
1350290075Sobrien	      && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
1350390075Sobrien	      && ! DECL_IGNORED_P (decl))
1350450397Sobrien	    modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
1350550397Sobrien
1350650397Sobrien	  return;
1350750397Sobrien	}
1350850397Sobrien
1350950397Sobrien      /* If we are in terse mode, don't generate any DIEs for types.  */
1351050397Sobrien      if (debug_info_level <= DINFO_LEVEL_TERSE)
1351150397Sobrien	return;
1351250397Sobrien
1351350397Sobrien      /* If we're a function-scope tag, initially use a parent of NULL;
1351450397Sobrien	 this will be fixed up in decls_for_scope.  */
1351550397Sobrien      if (decl_function_context (decl))
1351650397Sobrien	context_die = NULL;
1351750397Sobrien
1351850397Sobrien      break;
1351950397Sobrien
1352050397Sobrien    default:
1352150397Sobrien      return;
1352250397Sobrien    }
1352350397Sobrien
1352450397Sobrien  gen_decl_die (decl, context_die);
1352550397Sobrien}
1352650397Sobrien
1352750397Sobrien/* Output a marker (i.e. a label) for the beginning of the generated code for
1352850397Sobrien   a lexical block.  */
1352950397Sobrien
1353090075Sobrienstatic void
13531132718Skandwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13532132718Skan		       unsigned int blocknum)
1353350397Sobrien{
13534169689Skan  switch_to_section (current_function_section ());
1353590075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
1353650397Sobrien}
1353750397Sobrien
1353850397Sobrien/* Output a marker (i.e. a label) for the end of the generated code for a
1353950397Sobrien   lexical block.  */
1354050397Sobrien
1354190075Sobrienstatic void
13542132718Skandwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
1354350397Sobrien{
13544169689Skan  switch_to_section (current_function_section ());
1354590075Sobrien  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
1354650397Sobrien}
1354750397Sobrien
1354890075Sobrien/* Returns nonzero if it is appropriate not to emit any debugging
1354990075Sobrien   information for BLOCK, because it doesn't contain any instructions.
1355050397Sobrien
1355190075Sobrien   Don't allow this for blocks with nested functions or local classes
1355290075Sobrien   as we would end up with orphans, and in the presence of scheduling
1355390075Sobrien   we may end up calling them anyway.  */
1355490075Sobrien
1355590075Sobrienstatic bool
13556132718Skandwarf2out_ignore_block (tree block)
1355750397Sobrien{
1355890075Sobrien  tree decl;
1355950397Sobrien
1356090075Sobrien  for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
1356190075Sobrien    if (TREE_CODE (decl) == FUNCTION_DECL
1356290075Sobrien	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
1356390075Sobrien      return 0;
1356490075Sobrien
1356590075Sobrien  return 1;
1356650397Sobrien}
1356750397Sobrien
13568169689Skan/* Hash table routines for file_hash.  */
13569169689Skan
13570169689Skanstatic int
13571169689Skanfile_table_eq (const void *p1_p, const void *p2_p)
13572169689Skan{
13573169689Skan  const struct dwarf_file_data * p1 = p1_p;
13574169689Skan  const char * p2 = p2_p;
13575169689Skan  return strcmp (p1->filename, p2) == 0;
13576169689Skan}
13577169689Skan
13578169689Skanstatic hashval_t
13579169689Skanfile_table_hash (const void *p_p)
13580169689Skan{
13581169689Skan  const struct dwarf_file_data * p = p_p;
13582169689Skan  return htab_hash_string (p->filename);
13583169689Skan}
13584169689Skan
1358590075Sobrien/* Lookup FILE_NAME (in the list of filenames that we know about here in
1358650397Sobrien   dwarf2out.c) and return its "index".  The index of each (known) filename is
1358790075Sobrien   just a unique number which is associated with only that one filename.  We
1358890075Sobrien   need such numbers for the sake of generating labels (in the .debug_sfnames
1358990075Sobrien   section) and references to those files numbers (in the .debug_srcinfo
1359090075Sobrien   and.debug_macinfo sections).  If the filename given as an argument is not
1359190075Sobrien   found in our current list, add it to the list and assign it the next
1359290075Sobrien   available unique index number.  In order to speed up searches, we remember
1359390075Sobrien   the index of the filename was looked up last.  This handles the majority of
1359490075Sobrien   all searches.  */
1359550397Sobrien
13596169689Skanstatic struct dwarf_file_data *
13597132718Skanlookup_filename (const char *file_name)
1359850397Sobrien{
13599169689Skan  void ** slot;
13600169689Skan  struct dwarf_file_data * created;
1360150397Sobrien
1360290075Sobrien  /* Check to see if the file name that was searched on the previous
1360390075Sobrien     call matches this file name.  If so, return the index.  */
13604169689Skan  if (file_table_last_lookup
13605169689Skan      && (file_name == file_table_last_lookup->filename
13606169689Skan	  || strcmp (file_table_last_lookup->filename, file_name) == 0))
13607169689Skan    return file_table_last_lookup;
1360890075Sobrien
13609169689Skan  /* Didn't match the previous lookup, search the table.  */
13610169689Skan  slot = htab_find_slot_with_hash (file_table, file_name,
13611169689Skan				   htab_hash_string (file_name), INSERT);
13612169689Skan  if (*slot)
13613169689Skan    return *slot;
1361450397Sobrien
13615169689Skan  created = ggc_alloc (sizeof (struct dwarf_file_data));
13616169689Skan  created->filename = file_name;
13617169689Skan  created->emitted_number = 0;
13618169689Skan  *slot = created;
13619169689Skan  return created;
13620132718Skan}
13621132718Skan
13622169689Skan/* If the assembler will construct the file table, then translate the compiler
13623169689Skan   internal file table number into the assembler file table number, and emit
13624169689Skan   a .file directive if we haven't already emitted one yet.  The file table
13625169689Skan   numbers are different because we prune debug info for unused variables and
13626169689Skan   types, which may include filenames.  */
13627169689Skan
13628132718Skanstatic int
13629169689Skanmaybe_emit_file (struct dwarf_file_data * fd)
13630132718Skan{
13631169689Skan  if (! fd->emitted_number)
13632117395Skan    {
13633169689Skan      if (last_emitted_file)
13634169689Skan	fd->emitted_number = last_emitted_file->emitted_number + 1;
13635169689Skan      else
13636169689Skan	fd->emitted_number = 1;
13637169689Skan      last_emitted_file = fd;
13638169689Skan
13639169689Skan      if (DWARF2_ASM_LINE_DEBUG_INFO)
13640132718Skan	{
13641169689Skan	  fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13642169689Skan	  output_quoted_string (asm_out_file, fd->filename);
13643132718Skan	  fputc ('\n', asm_out_file);
13644132718Skan	}
13645117395Skan    }
13646169689Skan
13647169689Skan  return fd->emitted_number;
1364850397Sobrien}
1364950397Sobrien
13650169689Skan/* Called by the final INSN scan whenever we see a var location.  We
13651169689Skan   use it to drop labels in the right places, and throw the location in
13652169689Skan   our lookup table.  */
13653169689Skan
1365490075Sobrienstatic void
13655169689Skandwarf2out_var_location (rtx loc_note)
1365690075Sobrien{
13657169689Skan  char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13658169689Skan  struct var_loc_node *newloc;
13659169689Skan  rtx prev_insn;
13660169689Skan  static rtx last_insn;
13661169689Skan  static const char *last_label;
13662169689Skan  tree decl;
1366390075Sobrien
13664169689Skan  if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13665169689Skan    return;
13666169689Skan  prev_insn = PREV_INSN (loc_note);
13667169689Skan
13668169689Skan  newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13669169689Skan  /* If the insn we processed last time is the previous insn
13670169689Skan     and it is also a var location note, use the label we emitted
13671169689Skan     last time.  */
13672169689Skan  if (last_insn != NULL_RTX
13673169689Skan      && last_insn == prev_insn
13674169689Skan      && NOTE_P (prev_insn)
13675169689Skan      && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13676169689Skan    {
13677169689Skan      newloc->label = last_label;
13678169689Skan    }
13679169689Skan  else
13680169689Skan    {
13681169689Skan      ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13682169689Skan      ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13683169689Skan      loclabel_num++;
13684169689Skan      newloc->label = ggc_strdup (loclabel);
13685169689Skan    }
13686169689Skan  newloc->var_loc_note = loc_note;
13687169689Skan  newloc->next = NULL;
13688169689Skan
13689169689Skan  if (cfun && in_cold_section_p)
13690169689Skan    newloc->section_label = cfun->cold_section_label;
13691169689Skan  else
13692169689Skan    newloc->section_label = text_section_label;
13693169689Skan
13694169689Skan  last_insn = loc_note;
13695169689Skan  last_label = newloc->label;
13696169689Skan  decl = NOTE_VAR_LOCATION_DECL (loc_note);
13697169689Skan  add_var_loc_to_decl (decl, newloc);
1369890075Sobrien}
1369990075Sobrien
13700169689Skan/* We need to reset the locations at the beginning of each
13701169689Skan   function. We can't do this in the end_function hook, because the
13702169689Skan   declarations that use the locations won't have been output when
13703169689Skan   that hook is called.  Also compute have_multiple_function_sections here.  */
13704169689Skan
13705169689Skanstatic void
13706169689Skandwarf2out_begin_function (tree fun)
13707169689Skan{
13708169689Skan  htab_empty (decl_loc_table);
13709169689Skan
13710169689Skan  if (function_section (fun) != text_section)
13711169689Skan    have_multiple_function_sections = true;
13712169689Skan}
13713169689Skan
1371450397Sobrien/* Output a label to mark the beginning of a source code line entry
1371550397Sobrien   and record information relating to this source line, in
1371650397Sobrien   'line_info_table' for later output of the .debug_line section.  */
1371750397Sobrien
1371890075Sobrienstatic void
13719132718Skandwarf2out_source_line (unsigned int line, const char *filename)
1372050397Sobrien{
13721132718Skan  if (debug_info_level >= DINFO_LEVEL_NORMAL
13722132718Skan      && line != 0)
1372350397Sobrien    {
13724169689Skan      int file_num = maybe_emit_file (lookup_filename (filename));
13725169689Skan
13726169689Skan      switch_to_section (current_function_section ());
1372750397Sobrien
1372890075Sobrien      /* If requested, emit something human-readable.  */
1372990075Sobrien      if (flag_debug_asm)
1373090075Sobrien	fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
1373190075Sobrien		 filename, line);
1373290075Sobrien
1373390075Sobrien      if (DWARF2_ASM_LINE_DEBUG_INFO)
1373450397Sobrien	{
1373590075Sobrien	  /* Emit the .loc directive understood by GNU as.  */
1373690075Sobrien	  fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
1373790075Sobrien
1373890075Sobrien	  /* Indicate that line number info exists.  */
1373990075Sobrien	  line_info_table_in_use++;
1374090075Sobrien	}
13741169689Skan      else if (function_section (current_function_decl) != text_section)
1374290075Sobrien	{
1374390075Sobrien	  dw_separate_line_info_ref line_info;
13744169689Skan	  targetm.asm_out.internal_label (asm_out_file,
13745169689Skan					  SEPARATE_LINE_CODE_LABEL,
13746169689Skan					  separate_line_info_table_in_use);
1374750397Sobrien
13748169689Skan	  /* Expand the line info table if necessary.  */
1374950397Sobrien	  if (separate_line_info_table_in_use
1375050397Sobrien	      == separate_line_info_table_allocated)
1375150397Sobrien	    {
1375250397Sobrien	      separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1375350397Sobrien	      separate_line_info_table
13754132718Skan		= ggc_realloc (separate_line_info_table,
13755132718Skan			       separate_line_info_table_allocated
13756132718Skan			       * sizeof (dw_separate_line_info_entry));
13757132718Skan	      memset (separate_line_info_table
13758132718Skan		       + separate_line_info_table_in_use,
13759132718Skan		      0,
13760132718Skan		      (LINE_INFO_TABLE_INCREMENT
13761132718Skan		       * sizeof (dw_separate_line_info_entry)));
1376250397Sobrien	    }
1376350397Sobrien
1376450397Sobrien	  /* Add the new entry at the end of the line_info_table.  */
1376550397Sobrien	  line_info
1376650397Sobrien	    = &separate_line_info_table[separate_line_info_table_in_use++];
13767169689Skan	  line_info->dw_file_num = file_num;
1376850397Sobrien	  line_info->dw_line_num = line;
13769117395Skan	  line_info->function = current_function_funcdef_no;
1377050397Sobrien	}
1377150397Sobrien      else
1377250397Sobrien	{
1377390075Sobrien	  dw_line_info_ref line_info;
1377450397Sobrien
13775169689Skan	  targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
1377650397Sobrien				     line_info_table_in_use);
1377750397Sobrien
1377850397Sobrien	  /* Expand the line info table if necessary.  */
1377950397Sobrien	  if (line_info_table_in_use == line_info_table_allocated)
1378050397Sobrien	    {
1378150397Sobrien	      line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1378250397Sobrien	      line_info_table
13783132718Skan		= ggc_realloc (line_info_table,
13784132718Skan			       (line_info_table_allocated
13785132718Skan				* sizeof (dw_line_info_entry)));
13786132718Skan	      memset (line_info_table + line_info_table_in_use, 0,
13787132718Skan		      LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
1378850397Sobrien	    }
1378950397Sobrien
1379050397Sobrien	  /* Add the new entry at the end of the line_info_table.  */
1379150397Sobrien	  line_info = &line_info_table[line_info_table_in_use++];
13792169689Skan	  line_info->dw_file_num = file_num;
1379350397Sobrien	  line_info->dw_line_num = line;
1379450397Sobrien	}
1379550397Sobrien    }
1379650397Sobrien}
1379750397Sobrien
1379890075Sobrien/* Record the beginning of a new source file.  */
1379950397Sobrien
1380090075Sobrienstatic void
13801132718Skandwarf2out_start_source_file (unsigned int lineno, const char *filename)
1380250397Sobrien{
13803132718Skan  if (flag_eliminate_dwarf2_dups)
1380490075Sobrien    {
1380590075Sobrien      /* Record the beginning of the file for break_out_includes.  */
13806117395Skan      dw_die_ref bincl_die;
13807117395Skan
13808117395Skan      bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
1380990075Sobrien      add_AT_string (bincl_die, DW_AT_name, filename);
1381090075Sobrien    }
1381190075Sobrien
1381290075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1381390075Sobrien    {
13814169689Skan      int file_num = maybe_emit_file (lookup_filename (filename));
13815169689Skan
13816169689Skan      switch_to_section (debug_macinfo_section);
1381790075Sobrien      dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
1381890075Sobrien      dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
1381990075Sobrien				   lineno);
13820169689Skan
13821169689Skan      dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
1382290075Sobrien    }
1382350397Sobrien}
1382450397Sobrien
1382590075Sobrien/* Record the end of a source file.  */
1382650397Sobrien
1382790075Sobrienstatic void
13828132718Skandwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
1382950397Sobrien{
1383090075Sobrien  if (flag_eliminate_dwarf2_dups)
1383190075Sobrien    /* Record the end of the file for break_out_includes.  */
1383290075Sobrien    new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
1383390075Sobrien
1383490075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1383590075Sobrien    {
13836169689Skan      switch_to_section (debug_macinfo_section);
1383790075Sobrien      dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
1383890075Sobrien    }
1383950397Sobrien}
1384050397Sobrien
1384190075Sobrien/* Called from debug_define in toplev.c.  The `buffer' parameter contains
1384250397Sobrien   the tail part of the directive line, i.e. the part which is past the
1384350397Sobrien   initial whitespace, #, whitespace, directive-name, whitespace part.  */
1384450397Sobrien
1384590075Sobrienstatic void
13846132718Skandwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13847132718Skan		  const char *buffer ATTRIBUTE_UNUSED)
1384850397Sobrien{
1384990075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1385090075Sobrien    {
13851169689Skan      switch_to_section (debug_macinfo_section);
1385290075Sobrien      dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
1385390075Sobrien      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
1385490075Sobrien      dw2_asm_output_nstring (buffer, -1, "The macro");
1385590075Sobrien    }
1385650397Sobrien}
1385750397Sobrien
1385890075Sobrien/* Called from debug_undef in toplev.c.  The `buffer' parameter contains
1385950397Sobrien   the tail part of the directive line, i.e. the part which is past the
1386050397Sobrien   initial whitespace, #, whitespace, directive-name, whitespace part.  */
1386150397Sobrien
1386290075Sobrienstatic void
13863132718Skandwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13864132718Skan		 const char *buffer ATTRIBUTE_UNUSED)
1386550397Sobrien{
1386690075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1386790075Sobrien    {
13868169689Skan      switch_to_section (debug_macinfo_section);
1386990075Sobrien      dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
1387090075Sobrien      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
1387190075Sobrien      dw2_asm_output_nstring (buffer, -1, "The macro");
1387290075Sobrien    }
1387350397Sobrien}
1387450397Sobrien
1387550397Sobrien/* Set up for Dwarf output at the start of compilation.  */
1387650397Sobrien
1387790075Sobrienstatic void
13878132718Skandwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
1387950397Sobrien{
13880169689Skan  /* Allocate the file_table.  */
13881169689Skan  file_table = htab_create_ggc (50, file_table_hash,
13882169689Skan				file_table_eq, NULL);
1388390075Sobrien
13884169689Skan  /* Allocate the decl_die_table.  */
13885169689Skan  decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13886169689Skan				    decl_die_table_eq, NULL);
1388750397Sobrien
13888169689Skan  /* Allocate the decl_loc_table.  */
13889169689Skan  decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13890169689Skan				    decl_loc_table_eq, NULL);
13891169689Skan
1389250397Sobrien  /* Allocate the initial hunk of the decl_scope_table.  */
13893169689Skan  decl_scope_table = VEC_alloc (tree, gc, 256);
1389450397Sobrien
1389550397Sobrien  /* Allocate the initial hunk of the abbrev_die_table.  */
13896132718Skan  abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13897132718Skan					* sizeof (dw_die_ref));
1389850397Sobrien  abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13899169689Skan  /* Zero-th entry is allocated, but unused.  */
1390050397Sobrien  abbrev_die_table_in_use = 1;
1390150397Sobrien
1390250397Sobrien  /* Allocate the initial hunk of the line_info_table.  */
13903132718Skan  line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13904132718Skan				       * sizeof (dw_line_info_entry));
1390550397Sobrien  line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
1390690075Sobrien
13907169689Skan  /* Zero-th entry is allocated, but unused.  */
1390850397Sobrien  line_info_table_in_use = 1;
1390950397Sobrien
1391090075Sobrien  /* Generate the initial DIE for the .debug section.  Note that the (string)
1391150397Sobrien     value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
1391290075Sobrien     will (typically) be a relative pathname and that this pathname should be
1391350397Sobrien     taken as being relative to the directory from which the compiler was
13914132718Skan     invoked when the given (base) source file was compiled.  We will fill
13915132718Skan     in this value in dwarf2out_finish.  */
13916132718Skan  comp_unit_die = gen_compile_unit_die (NULL);
1391750397Sobrien
13918169689Skan  incomplete_types = VEC_alloc (tree, gc, 64);
1391990075Sobrien
13920169689Skan  used_rtx_array = VEC_alloc (rtx, gc, 32);
1392190075Sobrien
13922169689Skan  debug_info_section = get_section (DEBUG_INFO_SECTION,
13923169689Skan				    SECTION_DEBUG, NULL);
13924169689Skan  debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
13925169689Skan				      SECTION_DEBUG, NULL);
13926169689Skan  debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
13927169689Skan				       SECTION_DEBUG, NULL);
13928169689Skan  debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
13929169689Skan				       SECTION_DEBUG, NULL);
13930169689Skan  debug_line_section = get_section (DEBUG_LINE_SECTION,
13931169689Skan				    SECTION_DEBUG, NULL);
13932169689Skan  debug_loc_section = get_section (DEBUG_LOC_SECTION,
13933169689Skan				   SECTION_DEBUG, NULL);
13934169689Skan  debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
13935169689Skan					SECTION_DEBUG, NULL);
13936169689Skan  debug_str_section = get_section (DEBUG_STR_SECTION,
13937169689Skan				   DEBUG_STR_SECTION_FLAGS, NULL);
13938169689Skan  debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
13939169689Skan				      SECTION_DEBUG, NULL);
13940169689Skan  debug_frame_section = get_section (DEBUG_FRAME_SECTION,
13941169689Skan				     SECTION_DEBUG, NULL);
13942169689Skan
1394350397Sobrien  ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
1394490075Sobrien  ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
1394590075Sobrien			       DEBUG_ABBREV_SECTION_LABEL, 0);
13946169689Skan  ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13947169689Skan  ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13948169689Skan			       COLD_TEXT_SECTION_LABEL, 0);
13949169689Skan  ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
1395090075Sobrien
1395190075Sobrien  ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
1395252284Sobrien			       DEBUG_INFO_SECTION_LABEL, 0);
1395390075Sobrien  ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
1395452284Sobrien			       DEBUG_LINE_SECTION_LABEL, 0);
1395590075Sobrien  ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
1395690075Sobrien			       DEBUG_RANGES_SECTION_LABEL, 0);
13957169689Skan  switch_to_section (debug_abbrev_section);
1395852284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13959169689Skan  switch_to_section (debug_info_section);
1396052284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13961169689Skan  switch_to_section (debug_line_section);
1396252284Sobrien  ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
1396390075Sobrien
1396490075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
1396590075Sobrien    {
13966169689Skan      switch_to_section (debug_macinfo_section);
1396790075Sobrien      ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
1396890075Sobrien				   DEBUG_MACINFO_SECTION_LABEL, 0);
1396990075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
1397090075Sobrien    }
1397190075Sobrien
13972169689Skan  switch_to_section (text_section);
13973169689Skan  ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13974169689Skan  if (flag_reorder_blocks_and_partition)
1397590075Sobrien    {
13976169689Skan      switch_to_section (unlikely_text_section ());
13977169689Skan      ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
1397890075Sobrien    }
1397950397Sobrien}
1398050397Sobrien
1398190075Sobrien/* A helper function for dwarf2out_finish called through
1398290075Sobrien   ht_forall.  Emit one queued .debug_str string.  */
1398390075Sobrien
1398490075Sobrienstatic int
13985132718Skanoutput_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
1398690075Sobrien{
13987132718Skan  struct indirect_string_node *node = (struct indirect_string_node *) *h;
1398890075Sobrien
1398990075Sobrien  if (node->form == DW_FORM_strp)
1399090075Sobrien    {
13991169689Skan      switch_to_section (debug_str_section);
1399290075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, node->label);
13993132718Skan      assemble_string (node->str, strlen (node->str) + 1);
1399490075Sobrien    }
1399590075Sobrien
1399690075Sobrien  return 1;
1399790075Sobrien}
1399890075Sobrien
13999169689Skan#if ENABLE_ASSERT_CHECKING
14000169689Skan/* Verify that all marks are clear.  */
14001132718Skan
14002169689Skanstatic void
14003169689Skanverify_marks_clear (dw_die_ref die)
14004169689Skan{
14005169689Skan  dw_die_ref c;
14006169689Skan
14007169689Skan  gcc_assert (! die->die_mark);
14008169689Skan  FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14009169689Skan}
14010169689Skan#endif /* ENABLE_ASSERT_CHECKING */
14011132718Skan
14012132718Skan/* Clear the marks for a die and its children.
14013132718Skan   Be cool if the mark isn't set.  */
14014132718Skan
14015132718Skanstatic void
14016132718Skanprune_unmark_dies (dw_die_ref die)
14017132718Skan{
14018132718Skan  dw_die_ref c;
14019169689Skan
14020169689Skan  if (die->die_mark)
14021169689Skan    die->die_mark = 0;
14022169689Skan  FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14023132718Skan}
14024132718Skan
14025132718Skan/* Given DIE that we're marking as used, find any other dies
14026132718Skan   it references as attributes and mark them as used.  */
14027132718Skan
14028132718Skanstatic void
14029132718Skanprune_unused_types_walk_attribs (dw_die_ref die)
14030132718Skan{
14031132718Skan  dw_attr_ref a;
14032169689Skan  unsigned ix;
14033132718Skan
14034169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14035132718Skan    {
14036132718Skan      if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14037132718Skan	{
14038132718Skan	  /* A reference to another DIE.
14039132718Skan	     Make sure that it will get emitted.  */
14040132718Skan	  prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14041132718Skan	}
14042169689Skan      /* Set the string's refcount to 0 so that prune_unused_types_mark
14043169689Skan	 accounts properly for it.  */
14044169689Skan      if (AT_class (a) == dw_val_class_str)
14045169689Skan	a->dw_attr_val.v.val_str->refcount = 0;
14046132718Skan    }
14047132718Skan}
14048132718Skan
14049132718Skan
14050132718Skan/* Mark DIE as being used.  If DOKIDS is true, then walk down
14051132718Skan   to DIE's children.  */
14052132718Skan
14053132718Skanstatic void
14054132718Skanprune_unused_types_mark (dw_die_ref die, int dokids)
14055132718Skan{
14056132718Skan  dw_die_ref c;
14057132718Skan
14058132718Skan  if (die->die_mark == 0)
14059132718Skan    {
14060132718Skan      /* We haven't done this node yet.  Mark it as used.  */
14061132718Skan      die->die_mark = 1;
14062132718Skan
14063132718Skan      /* We also have to mark its parents as used.
14064132718Skan	 (But we don't want to mark our parents' kids due to this.)  */
14065132718Skan      if (die->die_parent)
14066132718Skan	prune_unused_types_mark (die->die_parent, 0);
14067132718Skan
14068132718Skan      /* Mark any referenced nodes.  */
14069132718Skan      prune_unused_types_walk_attribs (die);
14070132718Skan
14071132718Skan      /* If this node is a specification,
14072132718Skan         also mark the definition, if it exists.  */
14073132718Skan      if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14074132718Skan        prune_unused_types_mark (die->die_definition, 1);
14075132718Skan    }
14076132718Skan
14077132718Skan  if (dokids && die->die_mark != 2)
14078132718Skan    {
14079132718Skan      /* We need to walk the children, but haven't done so yet.
14080132718Skan	 Remember that we've walked the kids.  */
14081132718Skan      die->die_mark = 2;
14082132718Skan
14083169689Skan      /* If this is an array type, we need to make sure our
14084169689Skan	 kids get marked, even if they're types.  */
14085169689Skan      if (die->die_tag == DW_TAG_array_type)
14086169689Skan	FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14087169689Skan      else
14088169689Skan	FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14089132718Skan    }
14090132718Skan}
14091132718Skan
14092132718Skan
14093132718Skan/* Walk the tree DIE and mark types that we actually use.  */
14094132718Skan
14095132718Skanstatic void
14096132718Skanprune_unused_types_walk (dw_die_ref die)
14097132718Skan{
14098132718Skan  dw_die_ref c;
14099132718Skan
14100132718Skan  /* Don't do anything if this node is already marked.  */
14101132718Skan  if (die->die_mark)
14102132718Skan    return;
14103132718Skan
14104132718Skan  switch (die->die_tag) {
14105132718Skan  case DW_TAG_const_type:
14106132718Skan  case DW_TAG_packed_type:
14107132718Skan  case DW_TAG_pointer_type:
14108132718Skan  case DW_TAG_reference_type:
14109132718Skan  case DW_TAG_volatile_type:
14110132718Skan  case DW_TAG_typedef:
14111132718Skan  case DW_TAG_array_type:
14112132718Skan  case DW_TAG_structure_type:
14113132718Skan  case DW_TAG_union_type:
14114132718Skan  case DW_TAG_class_type:
14115132718Skan  case DW_TAG_friend:
14116132718Skan  case DW_TAG_variant_part:
14117132718Skan  case DW_TAG_enumeration_type:
14118132718Skan  case DW_TAG_subroutine_type:
14119132718Skan  case DW_TAG_string_type:
14120132718Skan  case DW_TAG_set_type:
14121132718Skan  case DW_TAG_subrange_type:
14122132718Skan  case DW_TAG_ptr_to_member_type:
14123132718Skan  case DW_TAG_file_type:
14124169689Skan    if (die->die_perennial_p)
14125169689Skan      break;
14126169689Skan
14127132718Skan    /* It's a type node --- don't mark it.  */
14128132718Skan    return;
14129132718Skan
14130132718Skan  default:
14131132718Skan    /* Mark everything else.  */
14132132718Skan    break;
14133132718Skan  }
14134132718Skan
14135132718Skan  die->die_mark = 1;
14136132718Skan
14137132718Skan  /* Now, mark any dies referenced from here.  */
14138132718Skan  prune_unused_types_walk_attribs (die);
14139132718Skan
14140132718Skan  /* Mark children.  */
14141169689Skan  FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14142132718Skan}
14143132718Skan
14144169689Skan/* Increment the string counts on strings referred to from DIE's
14145169689Skan   attributes.  */
14146132718Skan
14147169689Skanstatic void
14148169689Skanprune_unused_types_update_strings (dw_die_ref die)
14149169689Skan{
14150169689Skan  dw_attr_ref a;
14151169689Skan  unsigned ix;
14152169689Skan
14153169689Skan  for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14154169689Skan    if (AT_class (a) == dw_val_class_str)
14155169689Skan      {
14156169689Skan	struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14157169689Skan	s->refcount++;
14158169689Skan	/* Avoid unnecessarily putting strings that are used less than
14159169689Skan	   twice in the hash table.  */
14160169689Skan	if (s->refcount
14161169689Skan	    == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14162169689Skan	  {
14163169689Skan	    void ** slot;
14164169689Skan	    slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14165169689Skan					     htab_hash_string (s->str),
14166169689Skan					     INSERT);
14167169689Skan	    gcc_assert (*slot == NULL);
14168169689Skan	    *slot = s;
14169169689Skan	  }
14170169689Skan      }
14171169689Skan}
14172169689Skan
14173132718Skan/* Remove from the tree DIE any dies that aren't marked.  */
14174132718Skan
14175132718Skanstatic void
14176132718Skanprune_unused_types_prune (dw_die_ref die)
14177132718Skan{
14178169689Skan  dw_die_ref c;
14179132718Skan
14180169689Skan  gcc_assert (die->die_mark);
14181169689Skan  prune_unused_types_update_strings (die);
14182169689Skan
14183169689Skan  if (! die->die_child)
14184169689Skan    return;
14185169689Skan
14186169689Skan  c = die->die_child;
14187169689Skan  do {
14188169689Skan    dw_die_ref prev = c;
14189169689Skan    for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14190169689Skan      if (c == die->die_child)
14191132718Skan	{
14192169689Skan	  /* No marked children between 'prev' and the end of the list.  */
14193169689Skan	  if (prev == c)
14194169689Skan	    /* No marked children at all.  */
14195169689Skan	    die->die_child = NULL;
14196132718Skan	  else
14197169689Skan	    {
14198169689Skan	      prev->die_sib = c->die_sib;
14199169689Skan	      die->die_child = prev;
14200169689Skan	    }
14201169689Skan	  return;
14202132718Skan	}
14203169689Skan
14204169689Skan    if (c != prev->die_sib)
14205169689Skan      prev->die_sib = c;
14206169689Skan    prune_unused_types_prune (c);
14207169689Skan  } while (c != die->die_child);
14208132718Skan}
14209132718Skan
14210132718Skan
14211132718Skan/* Remove dies representing declarations that we never use.  */
14212132718Skan
14213132718Skanstatic void
14214132718Skanprune_unused_types (void)
14215132718Skan{
14216132718Skan  unsigned int i;
14217132718Skan  limbo_die_node *node;
14218132718Skan
14219169689Skan#if ENABLE_ASSERT_CHECKING
14220169689Skan  /* All the marks should already be clear.  */
14221169689Skan  verify_marks_clear (comp_unit_die);
14222132718Skan  for (node = limbo_die_list; node; node = node->next)
14223169689Skan    verify_marks_clear (node->die);
14224169689Skan#endif /* ENABLE_ASSERT_CHECKING */
14225132718Skan
14226132718Skan  /* Set the mark on nodes that are actually used.  */
14227132718Skan  prune_unused_types_walk (comp_unit_die);
14228132718Skan  for (node = limbo_die_list; node; node = node->next)
14229132718Skan    prune_unused_types_walk (node->die);
14230132718Skan
14231132718Skan  /* Also set the mark on nodes referenced from the
14232132718Skan     pubname_table or arange_table.  */
14233132718Skan  for (i = 0; i < pubname_table_in_use; i++)
14234132718Skan    prune_unused_types_mark (pubname_table[i].die, 1);
14235132718Skan  for (i = 0; i < arange_table_in_use; i++)
14236132718Skan    prune_unused_types_mark (arange_table[i], 1);
14237132718Skan
14238169689Skan  /* Get rid of nodes that aren't marked; and update the string counts.  */
14239169689Skan  if (debug_str_hash)
14240169689Skan    htab_empty (debug_str_hash);
14241132718Skan  prune_unused_types_prune (comp_unit_die);
14242132718Skan  for (node = limbo_die_list; node; node = node->next)
14243132718Skan    prune_unused_types_prune (node->die);
14244132718Skan
14245132718Skan  /* Leave the marks clear.  */
14246132718Skan  prune_unmark_dies (comp_unit_die);
14247132718Skan  for (node = limbo_die_list; node; node = node->next)
14248132718Skan    prune_unmark_dies (node->die);
14249132718Skan}
14250132718Skan
14251169689Skan/* Set the parameter to true if there are any relative pathnames in
14252169689Skan   the file table.  */
14253169689Skanstatic int
14254169689Skanfile_table_relative_p (void ** slot, void *param)
14255169689Skan{
14256169689Skan  bool *p = param;
14257169689Skan  struct dwarf_file_data *d = *slot;
14258169689Skan  if (d->emitted_number && d->filename[0] != DIR_SEPARATOR)
14259169689Skan    {
14260169689Skan      *p = true;
14261169689Skan      return 0;
14262169689Skan    }
14263169689Skan  return 1;
14264169689Skan}
14265169689Skan
1426650397Sobrien/* Output stuff that dwarf requires at the end of every file,
1426750397Sobrien   and generate the DWARF-2 debugging info.  */
1426850397Sobrien
1426990075Sobrienstatic void
14270132718Skandwarf2out_finish (const char *filename)
1427150397Sobrien{
1427250397Sobrien  limbo_die_node *node, *next_node;
1427390075Sobrien  dw_die_ref die = 0;
1427450397Sobrien
14275132718Skan  /* Add the name for the main input file now.  We delayed this from
14276132718Skan     dwarf2out_init to avoid complications with PCH.  */
14277132718Skan  add_name_attribute (comp_unit_die, filename);
14278132718Skan  if (filename[0] != DIR_SEPARATOR)
14279132718Skan    add_comp_dir_attribute (comp_unit_die);
14280132718Skan  else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14281132718Skan    {
14282169689Skan      bool p = false;
14283169689Skan      htab_traverse (file_table, file_table_relative_p, &p);
14284169689Skan      if (p)
14285169689Skan	add_comp_dir_attribute (comp_unit_die);
14286132718Skan    }
14287132718Skan
1428850397Sobrien  /* Traverse the limbo die list, and add parent/child links.  The only
1428950397Sobrien     dies without parents that should be here are concrete instances of
1429050397Sobrien     inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
1429150397Sobrien     For concrete instances, we can get the parent die from the abstract
1429250397Sobrien     instance.  */
1429350397Sobrien  for (node = limbo_die_list; node; node = next_node)
1429450397Sobrien    {
1429550397Sobrien      next_node = node->next;
1429650397Sobrien      die = node->die;
1429750397Sobrien
1429850397Sobrien      if (die->die_parent == NULL)
1429950397Sobrien	{
1430090075Sobrien	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
1430190075Sobrien
1430290075Sobrien	  if (origin)
1430390075Sobrien	    add_child_die (origin->die_parent, die);
1430450397Sobrien	  else if (die == comp_unit_die)
1430590075Sobrien	    ;
1430690075Sobrien	  else if (errorcount > 0 || sorrycount > 0)
1430790075Sobrien	    /* It's OK to be confused by errors in the input.  */
1430890075Sobrien	    add_child_die (comp_unit_die, die);
14309169689Skan	  else
1431090075Sobrien	    {
1431190075Sobrien	      /* In certain situations, the lexical block containing a
1431290075Sobrien		 nested function can be optimized away, which results
1431390075Sobrien		 in the nested function die being orphaned.  Likewise
1431490075Sobrien		 with the return type of that nested function.  Force
14315169689Skan		 this to be a child of the containing function.
14316169689Skan
14317169689Skan		 It may happen that even the containing function got fully
14318169689Skan		 inlined and optimized out.  In that case we are lost and
14319169689Skan		 assign the empty child.  This should not be big issue as
14320169689Skan		 the function is likely unreachable too.  */
14321169689Skan	      tree context = NULL_TREE;
14322169689Skan
14323169689Skan	      gcc_assert (node->created_for);
14324169689Skan
14325169689Skan	      if (DECL_P (node->created_for))
14326169689Skan		context = DECL_CONTEXT (node->created_for);
14327169689Skan	      else if (TYPE_P (node->created_for))
14328169689Skan		context = TYPE_CONTEXT (node->created_for);
14329169689Skan
14330171825Skan	      gcc_assert (context
14331171825Skan			  && (TREE_CODE (context) == FUNCTION_DECL
14332171825Skan			      || TREE_CODE (context) == NAMESPACE_DECL));
14333169689Skan
1433490075Sobrien	      origin = lookup_decl_die (context);
14335169689Skan	      if (origin)
14336169689Skan	        add_child_die (origin, die);
14337169689Skan	      else
14338169689Skan	        add_child_die (comp_unit_die, die);
1433990075Sobrien	    }
1434050397Sobrien	}
1434150397Sobrien    }
1434250397Sobrien
1434390075Sobrien  limbo_die_list = NULL;
1434490075Sobrien
1434552284Sobrien  /* Walk through the list of incomplete types again, trying once more to
1434652284Sobrien     emit full debugging info for them.  */
1434752284Sobrien  retry_incomplete_types ();
1434852284Sobrien
14349132718Skan  if (flag_eliminate_unused_debug_types)
14350132718Skan    prune_unused_types ();
14351132718Skan
1435290075Sobrien  /* Generate separate CUs for each of the include files we've seen.
1435390075Sobrien     They will go into limbo_die_list.  */
1435490075Sobrien  if (flag_eliminate_dwarf2_dups)
1435590075Sobrien    break_out_includes (comp_unit_die);
1435690075Sobrien
1435790075Sobrien  /* Traverse the DIE's and add add sibling attributes to those DIE's
1435850397Sobrien     that have children.  */
1435950397Sobrien  add_sibling_attributes (comp_unit_die);
1436090075Sobrien  for (node = limbo_die_list; node; node = node->next)
1436190075Sobrien    add_sibling_attributes (node->die);
1436250397Sobrien
1436350397Sobrien  /* Output a terminator label for the .text section.  */
14364169689Skan  switch_to_section (text_section);
14365169689Skan  targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14366169689Skan  if (flag_reorder_blocks_and_partition)
1436750397Sobrien    {
14368169689Skan      switch_to_section (unlikely_text_section ());
14369169689Skan      targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
1437090075Sobrien    }
1437150397Sobrien
1437290075Sobrien  /* We can only use the low/high_pc attributes if all of the code was
1437390075Sobrien     in .text.  */
14374169689Skan  if (!have_multiple_function_sections)
1437590075Sobrien    {
1437690075Sobrien      add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
1437790075Sobrien      add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
1437850397Sobrien    }
1437950397Sobrien
1438090075Sobrien  /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
1438190075Sobrien     "base address".  Use zero so that these addresses become absolute.  */
1438290075Sobrien  else if (have_location_lists || ranges_table_in_use)
1438390075Sobrien    add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
1438490075Sobrien
14385169689Skan  /* Output location list section if necessary.  */
14386169689Skan  if (have_location_lists)
14387169689Skan    {
14388169689Skan      /* Output the location lists info.  */
14389169689Skan      switch_to_section (debug_loc_section);
14390169689Skan      ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14391169689Skan				   DEBUG_LOC_SECTION_LABEL, 0);
14392169689Skan      ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14393169689Skan      output_location_lists (die);
14394169689Skan    }
14395169689Skan
1439690075Sobrien  if (debug_info_level >= DINFO_LEVEL_NORMAL)
14397169689Skan    add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14398169689Skan		    debug_line_section_label);
1439990075Sobrien
1440090075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14401169689Skan    add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
1440290075Sobrien
1440390075Sobrien  /* Output all of the compilation units.  We put the main one last so that
1440490075Sobrien     the offsets are available to output_pubnames.  */
1440590075Sobrien  for (node = limbo_die_list; node; node = node->next)
14406117395Skan    output_comp_unit (node->die, 0);
1440790075Sobrien
14408117395Skan  output_comp_unit (comp_unit_die, 0);
1440990075Sobrien
1441050397Sobrien  /* Output the abbreviation table.  */
14411169689Skan  switch_to_section (debug_abbrev_section);
1441250397Sobrien  output_abbrev_section ();
1441350397Sobrien
1441490075Sobrien  /* Output public names table if necessary.  */
1441550397Sobrien  if (pubname_table_in_use)
1441650397Sobrien    {
14417169689Skan      switch_to_section (debug_pubnames_section);
1441850397Sobrien      output_pubnames ();
1441950397Sobrien    }
1442050397Sobrien
1442190075Sobrien  /* Output the address range information.  We only put functions in the arange
1442290075Sobrien     table, so don't write it out if we don't have any.  */
1442350397Sobrien  if (fde_table_in_use)
1442450397Sobrien    {
14425169689Skan      switch_to_section (debug_aranges_section);
1442650397Sobrien      output_aranges ();
1442750397Sobrien    }
1442890075Sobrien
1442990075Sobrien  /* Output ranges section if necessary.  */
1443090075Sobrien  if (ranges_table_in_use)
1443190075Sobrien    {
14432169689Skan      switch_to_section (debug_ranges_section);
1443390075Sobrien      ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
1443490075Sobrien      output_ranges ();
1443590075Sobrien    }
1443690075Sobrien
14437169689Skan  /* Output the source line correspondence table.  We must do this
14438169689Skan     even if there is no line information.  Otherwise, on an empty
14439169689Skan     translation unit, we will generate a present, but empty,
14440169689Skan     .debug_info section.  IRIX 6.5 `nm' will then complain when
14441169689Skan     examining the file.  This is done late so that any filenames
14442169689Skan     used by the debug_info section are marked as 'used'.  */
14443169689Skan  if (! DWARF2_ASM_LINE_DEBUG_INFO)
14444169689Skan    {
14445169689Skan      switch_to_section (debug_line_section);
14446169689Skan      output_line_info ();
14447169689Skan    }
14448169689Skan
14449169689Skan  /* Have to end the macro section.  */
1445090075Sobrien  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14451117395Skan    {
14452169689Skan      switch_to_section (debug_macinfo_section);
14453117395Skan      dw2_asm_output_data (1, 0, "End compilation unit");
1445490075Sobrien    }
1445590075Sobrien
1445690075Sobrien  /* If we emitted any DW_FORM_strp form attribute, output the string
1445790075Sobrien     table too.  */
1445890075Sobrien  if (debug_str_hash)
14459132718Skan    htab_traverse (debug_str_hash, output_indirect_string, NULL);
1446050397Sobrien}
14461117395Skan#else
14462117395Skan
14463117395Skan/* This should never be used, but its address is needed for comparisons.  */
14464117395Skanconst struct gcc_debug_hooks dwarf2_debug_hooks;
14465117395Skan
14466117395Skan#endif /* DWARF2_DEBUGGING_INFO */
14467117395Skan
14468117395Skan#include "gt-dwarf2out.h"
14469