1/* Output Dwarf2 format symbol table information from GCC.
2   Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3   2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4   Contributed by Gary Funck (gary@intrepid.com).
5   Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6   Extensively modified by Jason Merrill (jason@cygnus.com).
7
8This file is part of GCC.
9
10GCC is free software; you can redistribute it and/or modify it under
11the terms of the GNU General Public License as published by the Free
12Software Foundation; either version 2, or (at your option) any later
13version.
14
15GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18for more details.
19
20You should have received a copy of the GNU General Public License
21along with GCC; see the file COPYING.  If not, write to the Free
22Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2302110-1301, USA.  */
24
25/* TODO: Emit .debug_line header even when there are no functions, since
26	   the file numbers are used by .debug_info.  Alternately, leave
27	   out locations for types and decls.
28	 Avoid talking about ctors and op= for PODs.
29	 Factor out common prologue sequences into multiple CIEs.  */
30
31/* The first part of this file deals with the DWARF 2 frame unwind
32   information, which is also used by the GCC efficient exception handling
33   mechanism.  The second part, controlled only by an #ifdef
34   DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35   information.  */
36
37#include "config.h"
38#include "system.h"
39#include "coretypes.h"
40#include "tm.h"
41#include "tree.h"
42#include "version.h"
43#include "flags.h"
44#include "real.h"
45#include "rtl.h"
46#include "hard-reg-set.h"
47#include "regs.h"
48#include "insn-config.h"
49#include "reload.h"
50#include "function.h"
51#include "output.h"
52#include "expr.h"
53#include "libfuncs.h"
54#include "except.h"
55#include "dwarf2.h"
56#include "dwarf2out.h"
57#include "dwarf2asm.h"
58#include "toplev.h"
59#include "varray.h"
60#include "ggc.h"
61#include "md5.h"
62#include "tm_p.h"
63#include "diagnostic.h"
64#include "debug.h"
65#include "target.h"
66#include "langhooks.h"
67#include "hashtab.h"
68#include "cgraph.h"
69#include "input.h"
70
71#ifdef DWARF2_DEBUGGING_INFO
72static void dwarf2out_source_line (unsigned int, const char *);
73#endif
74
75/* DWARF2 Abbreviation Glossary:
76   CFA = Canonical Frame Address
77	   a fixed address on the stack which identifies a call frame.
78	   We define it to be the value of SP just before the call insn.
79	   The CFA register and offset, which may change during the course
80	   of the function, are used to calculate its value at runtime.
81   CFI = Call Frame Instruction
82	   an instruction for the DWARF2 abstract machine
83   CIE = Common Information Entry
84	   information describing information common to one or more FDEs
85   DIE = Debugging Information Entry
86   FDE = Frame Description Entry
87	   information describing the stack call frame, in particular,
88	   how to restore registers
89
90   DW_CFA_... = DWARF2 CFA call frame instruction
91   DW_TAG_... = DWARF2 DIE tag */
92
93#ifndef DWARF2_FRAME_INFO
94# ifdef DWARF2_DEBUGGING_INFO
95#  define DWARF2_FRAME_INFO \
96  (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97# else
98#  define DWARF2_FRAME_INFO 0
99# endif
100#endif
101
102/* Decide whether we want to emit frame unwind information for the current
103   translation unit.  */
104
105int
106dwarf2out_do_frame (void)
107{
108  /* We want to emit correct CFA location expressions or lists, so we
109     have to return true if we're going to output debug info, even if
110     we're not going to output frame or unwind info.  */
111  return (write_symbols == DWARF2_DEBUG
112	  || write_symbols == VMS_AND_DWARF2_DEBUG
113	  || DWARF2_FRAME_INFO
114#ifdef DWARF2_UNWIND_INFO
115	  || (DWARF2_UNWIND_INFO
116	      && (flag_unwind_tables
117		  || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
118#endif
119	  );
120}
121
122/* The size of the target's pointer type.  */
123#ifndef PTR_SIZE
124#define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
125#endif
126
127/* Various versions of targetm.eh_frame_section.  Note these must appear
128   outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro guards.  */
129
130/* Version of targetm.eh_frame_section for systems with named sections.  */
131void
132named_section_eh_frame_section (void)
133{
134#ifdef EH_FRAME_SECTION_NAME
135  int flags;
136
137  if (EH_TABLES_CAN_BE_READ_ONLY)
138    {
139      int fde_encoding;
140      int per_encoding;
141      int lsda_encoding;
142
143      fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
144      per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
145      lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
146      flags = (! flag_pic
147	       || ((fde_encoding & 0x70) != DW_EH_PE_absptr
148		   && (fde_encoding & 0x70) != DW_EH_PE_aligned
149		   && (per_encoding & 0x70) != DW_EH_PE_absptr
150		   && (per_encoding & 0x70) != DW_EH_PE_aligned
151		   && (lsda_encoding & 0x70) != DW_EH_PE_absptr
152		   && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
153	      ? 0 : SECTION_WRITE;
154    }
155  else
156    flags = SECTION_WRITE;
157  named_section_flags (EH_FRAME_SECTION_NAME, flags);
158#endif
159}
160
161/* Version of targetm.eh_frame_section for systems using collect2.  */
162void
163collect2_eh_frame_section (void)
164{
165  tree label = get_file_function_name ('F');
166
167  data_section ();
168  ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
169  targetm.asm_out.globalize_label (asm_out_file, IDENTIFIER_POINTER (label));
170  ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
171}
172
173/* Default version of targetm.eh_frame_section.  */
174void
175default_eh_frame_section (void)
176{
177#ifdef EH_FRAME_SECTION_NAME
178  named_section_eh_frame_section ();
179#else
180  collect2_eh_frame_section ();
181#endif
182}
183
184DEF_VEC_P(rtx);
185DEF_VEC_ALLOC_P(rtx,gc);
186
187/* Array of RTXes referenced by the debugging information, which therefore
188   must be kept around forever.  */
189static GTY(()) VEC(rtx,gc) *used_rtx_array;
190
191/* A pointer to the base of a list of incomplete types which might be
192   completed at some later time.  incomplete_types_list needs to be a
193   VEC(tree,gc) because we want to tell the garbage collector about
194   it.  */
195static GTY(()) VEC(tree,gc) *incomplete_types;
196
197/* A pointer to the base of a table of references to declaration
198   scopes.  This table is a display which tracks the nesting
199   of declaration scopes at the current scope and containing
200   scopes.  This table is used to find the proper place to
201   define type declaration DIE's.  */
202static GTY(()) VEC(tree,gc) *decl_scope_table;
203
204/* How to start an assembler comment.  */
205#ifndef ASM_COMMENT_START
206#define ASM_COMMENT_START ";#"
207#endif
208
209typedef struct dw_cfi_struct *dw_cfi_ref;
210typedef struct dw_fde_struct *dw_fde_ref;
211typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
212
213/* Call frames are described using a sequence of Call Frame
214   Information instructions.  The register number, offset
215   and address fields are provided as possible operands;
216   their use is selected by the opcode field.  */
217
218enum dw_cfi_oprnd_type {
219  dw_cfi_oprnd_unused,
220  dw_cfi_oprnd_reg_num,
221  dw_cfi_oprnd_offset,
222  dw_cfi_oprnd_addr,
223  dw_cfi_oprnd_loc
224};
225
226typedef union dw_cfi_oprnd_struct GTY(())
227{
228  unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
229  HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
230  const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
231  struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
232}
233dw_cfi_oprnd;
234
235typedef struct dw_cfi_struct GTY(())
236{
237  dw_cfi_ref dw_cfi_next;
238  enum dwarf_call_frame_info dw_cfi_opc;
239  dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
240    dw_cfi_oprnd1;
241  dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
242    dw_cfi_oprnd2;
243}
244dw_cfi_node;
245
246/* This is how we define the location of the CFA. We use to handle it
247   as REG + OFFSET all the time,  but now it can be more complex.
248   It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
249   Instead of passing around REG and OFFSET, we pass a copy
250   of this structure.  */
251typedef struct cfa_loc GTY(())
252{
253  HOST_WIDE_INT offset;
254  HOST_WIDE_INT base_offset;
255  unsigned int reg;
256  int indirect;            /* 1 if CFA is accessed via a dereference.  */
257} dw_cfa_location;
258
259/* All call frame descriptions (FDE's) in the GCC generated DWARF
260   refer to a single Common Information Entry (CIE), defined at
261   the beginning of the .debug_frame section.  This use of a single
262   CIE obviates the need to keep track of multiple CIE's
263   in the DWARF generation routines below.  */
264
265typedef struct dw_fde_struct GTY(())
266{
267  tree decl;
268  const char *dw_fde_begin;
269  const char *dw_fde_current_label;
270  const char *dw_fde_end;
271  const char *dw_fde_hot_section_label;
272  const char *dw_fde_hot_section_end_label;
273  const char *dw_fde_unlikely_section_label;
274  const char *dw_fde_unlikely_section_end_label;
275  bool dw_fde_switched_sections;
276  dw_cfi_ref dw_fde_cfi;
277  unsigned funcdef_number;
278  unsigned all_throwers_are_sibcalls : 1;
279  unsigned nothrow : 1;
280  unsigned uses_eh_lsda : 1;
281}
282dw_fde_node;
283
284/* Maximum size (in bytes) of an artificially generated label.  */
285#define MAX_ARTIFICIAL_LABEL_BYTES	30
286
287/* The size of addresses as they appear in the Dwarf 2 data.
288   Some architectures use word addresses to refer to code locations,
289   but Dwarf 2 info always uses byte addresses.  On such machines,
290   Dwarf 2 addresses need to be larger than the architecture's
291   pointers.  */
292#ifndef DWARF2_ADDR_SIZE
293#define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
294#endif
295
296/* The size in bytes of a DWARF field indicating an offset or length
297   relative to a debug info section, specified to be 4 bytes in the
298   DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
299   as PTR_SIZE.  */
300
301#ifndef DWARF_OFFSET_SIZE
302#define DWARF_OFFSET_SIZE 4
303#endif
304
305/* According to the (draft) DWARF 3 specification, the initial length
306   should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
307   bytes are 0xffffffff, followed by the length stored in the next 8
308   bytes.
309
310   However, the SGI/MIPS ABI uses an initial length which is equal to
311   DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
312
313#ifndef DWARF_INITIAL_LENGTH_SIZE
314#define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
315#endif
316
317#define DWARF_VERSION 2
318
319/* Round SIZE up to the nearest BOUNDARY.  */
320#define DWARF_ROUND(SIZE,BOUNDARY) \
321  ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
322
323/* Offsets recorded in opcodes are a multiple of this alignment factor.  */
324#ifndef DWARF_CIE_DATA_ALIGNMENT
325#ifdef STACK_GROWS_DOWNWARD
326#define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
327#else
328#define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
329#endif
330#endif
331
332/* A pointer to the base of a table that contains frame description
333   information for each routine.  */
334static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
335
336/* Number of elements currently allocated for fde_table.  */
337static GTY(()) unsigned fde_table_allocated;
338
339/* Number of elements in fde_table currently in use.  */
340static GTY(()) unsigned fde_table_in_use;
341
342/* Size (in elements) of increments by which we may expand the
343   fde_table.  */
344#define FDE_TABLE_INCREMENT 256
345
346/* A list of call frame insns for the CIE.  */
347static GTY(()) dw_cfi_ref cie_cfi_head;
348
349#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
350/* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
351   attribute that accelerates the lookup of the FDE associated
352   with the subprogram.  This variable holds the table index of the FDE
353   associated with the current function (body) definition.  */
354static unsigned current_funcdef_fde;
355#endif
356
357struct indirect_string_node GTY(())
358{
359  const char *str;
360  unsigned int refcount;
361  unsigned int form;
362  char *label;
363};
364
365static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
366
367static GTY(()) int dw2_string_counter;
368static GTY(()) unsigned long dwarf2out_cfi_label_num;
369
370#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
371
372/* Forward declarations for functions defined in this file.  */
373
374static char *stripattributes (const char *);
375static const char *dwarf_cfi_name (unsigned);
376static dw_cfi_ref new_cfi (void);
377static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
378static void add_fde_cfi (const char *, dw_cfi_ref);
379static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
380static void lookup_cfa (dw_cfa_location *);
381static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
382static void initial_return_save (rtx);
383static HOST_WIDE_INT stack_adjust_offset (rtx);
384static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
385static void output_call_frame_info (int);
386static void dwarf2out_stack_adjust (rtx, bool);
387static void flush_queued_reg_saves (void);
388static bool clobbers_queued_reg_save (rtx);
389static void dwarf2out_frame_debug_expr (rtx, const char *);
390
391/* Support for complex CFA locations.  */
392static void output_cfa_loc (dw_cfi_ref);
393static void get_cfa_from_loc_descr (dw_cfa_location *,
394				    struct dw_loc_descr_struct *);
395static struct dw_loc_descr_struct *build_cfa_loc
396 (dw_cfa_location *);
397static void def_cfa_1 (const char *, dw_cfa_location *);
398
399/* How to start an assembler comment.  */
400#ifndef ASM_COMMENT_START
401#define ASM_COMMENT_START ";#"
402#endif
403
404/* Data and reference forms for relocatable data.  */
405#define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
406#define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
407
408#ifndef DEBUG_FRAME_SECTION
409#define DEBUG_FRAME_SECTION	".debug_frame"
410#endif
411
412#ifndef FUNC_BEGIN_LABEL
413#define FUNC_BEGIN_LABEL	"LFB"
414#endif
415
416#ifndef FUNC_END_LABEL
417#define FUNC_END_LABEL		"LFE"
418#endif
419
420#ifndef FRAME_BEGIN_LABEL
421#define FRAME_BEGIN_LABEL	"Lframe"
422#endif
423#define CIE_AFTER_SIZE_LABEL	"LSCIE"
424#define CIE_END_LABEL		"LECIE"
425#define FDE_LABEL		"LSFDE"
426#define FDE_AFTER_SIZE_LABEL	"LASFDE"
427#define FDE_END_LABEL		"LEFDE"
428#define LINE_NUMBER_BEGIN_LABEL	"LSLT"
429#define LINE_NUMBER_END_LABEL	"LELT"
430#define LN_PROLOG_AS_LABEL	"LASLTP"
431#define LN_PROLOG_END_LABEL	"LELTP"
432#define DIE_LABEL_PREFIX	"DW"
433
434/* The DWARF 2 CFA column which tracks the return address.  Normally this
435   is the column for PC, or the first column after all of the hard
436   registers.  */
437#ifndef DWARF_FRAME_RETURN_COLUMN
438#ifdef PC_REGNUM
439#define DWARF_FRAME_RETURN_COLUMN	DWARF_FRAME_REGNUM (PC_REGNUM)
440#else
441#define DWARF_FRAME_RETURN_COLUMN	DWARF_FRAME_REGISTERS
442#endif
443#endif
444
445/* The mapping from gcc register number to DWARF 2 CFA column number.  By
446   default, we just provide columns for all registers.  */
447#ifndef DWARF_FRAME_REGNUM
448#define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
449#endif
450
451/* Hook used by __throw.  */
452
453rtx
454expand_builtin_dwarf_sp_column (void)
455{
456  return GEN_INT (DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
457}
458
459/* Return a pointer to a copy of the section string name S with all
460   attributes stripped off, and an asterisk prepended (for assemble_name).  */
461
462static inline char *
463stripattributes (const char *s)
464{
465  char *stripped = xmalloc (strlen (s) + 2);
466  char *p = stripped;
467
468  *p++ = '*';
469
470  while (*s && *s != ',')
471    *p++ = *s++;
472
473  *p = '\0';
474  return stripped;
475}
476
477/* Generate code to initialize the register size table.  */
478
479void
480expand_builtin_init_dwarf_reg_sizes (tree address)
481{
482  int i;
483  enum machine_mode mode = TYPE_MODE (char_type_node);
484  rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
485  rtx mem = gen_rtx_MEM (BLKmode, addr);
486  bool wrote_return_column = false;
487
488  for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
489    if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
490      {
491	HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
492	enum machine_mode save_mode = reg_raw_mode[i];
493	HOST_WIDE_INT size;
494
495	if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
496	  save_mode = choose_hard_reg_mode (i, 1, true);
497	if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
498	  {
499	    if (save_mode == VOIDmode)
500	      continue;
501	    wrote_return_column = true;
502	  }
503	size = GET_MODE_SIZE (save_mode);
504	if (offset < 0)
505	  continue;
506
507	emit_move_insn (adjust_address (mem, mode, offset),
508			gen_int_mode (size, mode));
509      }
510
511#ifdef DWARF_ALT_FRAME_RETURN_COLUMN
512  gcc_assert (wrote_return_column);
513  i = DWARF_ALT_FRAME_RETURN_COLUMN;
514  wrote_return_column = false;
515#else
516  i = DWARF_FRAME_RETURN_COLUMN;
517#endif
518
519  if (! wrote_return_column)
520    {
521      enum machine_mode save_mode = Pmode;
522      HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
523      HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
524      emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
525    }
526}
527
528/* Convert a DWARF call frame info. operation to its string name */
529
530static const char *
531dwarf_cfi_name (unsigned int cfi_opc)
532{
533  switch (cfi_opc)
534    {
535    case DW_CFA_advance_loc:
536      return "DW_CFA_advance_loc";
537    case DW_CFA_offset:
538      return "DW_CFA_offset";
539    case DW_CFA_restore:
540      return "DW_CFA_restore";
541    case DW_CFA_nop:
542      return "DW_CFA_nop";
543    case DW_CFA_set_loc:
544      return "DW_CFA_set_loc";
545    case DW_CFA_advance_loc1:
546      return "DW_CFA_advance_loc1";
547    case DW_CFA_advance_loc2:
548      return "DW_CFA_advance_loc2";
549    case DW_CFA_advance_loc4:
550      return "DW_CFA_advance_loc4";
551    case DW_CFA_offset_extended:
552      return "DW_CFA_offset_extended";
553    case DW_CFA_restore_extended:
554      return "DW_CFA_restore_extended";
555    case DW_CFA_undefined:
556      return "DW_CFA_undefined";
557    case DW_CFA_same_value:
558      return "DW_CFA_same_value";
559    case DW_CFA_register:
560      return "DW_CFA_register";
561    case DW_CFA_remember_state:
562      return "DW_CFA_remember_state";
563    case DW_CFA_restore_state:
564      return "DW_CFA_restore_state";
565    case DW_CFA_def_cfa:
566      return "DW_CFA_def_cfa";
567    case DW_CFA_def_cfa_register:
568      return "DW_CFA_def_cfa_register";
569    case DW_CFA_def_cfa_offset:
570      return "DW_CFA_def_cfa_offset";
571
572    /* DWARF 3 */
573    case DW_CFA_def_cfa_expression:
574      return "DW_CFA_def_cfa_expression";
575    case DW_CFA_expression:
576      return "DW_CFA_expression";
577    case DW_CFA_offset_extended_sf:
578      return "DW_CFA_offset_extended_sf";
579    case DW_CFA_def_cfa_sf:
580      return "DW_CFA_def_cfa_sf";
581    case DW_CFA_def_cfa_offset_sf:
582      return "DW_CFA_def_cfa_offset_sf";
583
584    /* SGI/MIPS specific */
585    case DW_CFA_MIPS_advance_loc8:
586      return "DW_CFA_MIPS_advance_loc8";
587
588    /* GNU extensions */
589    case DW_CFA_GNU_window_save:
590      return "DW_CFA_GNU_window_save";
591    case DW_CFA_GNU_args_size:
592      return "DW_CFA_GNU_args_size";
593    case DW_CFA_GNU_negative_offset_extended:
594      return "DW_CFA_GNU_negative_offset_extended";
595
596    default:
597      return "DW_CFA_<unknown>";
598    }
599}
600
601/* Return a pointer to a newly allocated Call Frame Instruction.  */
602
603static inline dw_cfi_ref
604new_cfi (void)
605{
606  dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
607
608  cfi->dw_cfi_next = NULL;
609  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
610  cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
611
612  return cfi;
613}
614
615/* Add a Call Frame Instruction to list of instructions.  */
616
617static inline void
618add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
619{
620  dw_cfi_ref *p;
621
622  /* Find the end of the chain.  */
623  for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
624    ;
625
626  *p = cfi;
627}
628
629/* Generate a new label for the CFI info to refer to.  */
630
631char *
632dwarf2out_cfi_label (void)
633{
634  static char label[20];
635
636  ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
637  ASM_OUTPUT_LABEL (asm_out_file, label);
638  return label;
639}
640
641/* Add CFI to the current fde at the PC value indicated by LABEL if specified,
642   or to the CIE if LABEL is NULL.  */
643
644static void
645add_fde_cfi (const char *label, dw_cfi_ref cfi)
646{
647  if (label)
648    {
649      dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
650
651      if (*label == 0)
652	label = dwarf2out_cfi_label ();
653
654      if (fde->dw_fde_current_label == NULL
655	  || strcmp (label, fde->dw_fde_current_label) != 0)
656	{
657	  dw_cfi_ref xcfi;
658
659	  label = xstrdup (label);
660
661	  /* Set the location counter to the new label.  */
662	  xcfi = new_cfi ();
663	  /* If we have a current label, advance from there, otherwise
664	     set the location directly using set_loc.  */
665	  xcfi->dw_cfi_opc = fde->dw_fde_current_label
666			     ? DW_CFA_advance_loc4
667			     : DW_CFA_set_loc;
668	  xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
669	  add_cfi (&fde->dw_fde_cfi, xcfi);
670
671	  fde->dw_fde_current_label = label;
672	}
673
674      add_cfi (&fde->dw_fde_cfi, cfi);
675    }
676
677  else
678    add_cfi (&cie_cfi_head, cfi);
679}
680
681/* Subroutine of lookup_cfa.  */
682
683static void
684lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
685{
686  switch (cfi->dw_cfi_opc)
687    {
688    case DW_CFA_def_cfa_offset:
689      loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
690      break;
691    case DW_CFA_def_cfa_offset_sf:
692      loc->offset
693	= cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
694      break;
695    case DW_CFA_def_cfa_register:
696      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
697      break;
698    case DW_CFA_def_cfa:
699      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
700      loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
701      break;
702    case DW_CFA_def_cfa_sf:
703      loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
704      loc->offset
705	= cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
706      break;
707    case DW_CFA_def_cfa_expression:
708      get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
709      break;
710    default:
711      break;
712    }
713}
714
715/* Find the previous value for the CFA.  */
716
717static void
718lookup_cfa (dw_cfa_location *loc)
719{
720  dw_cfi_ref cfi;
721
722  loc->reg = INVALID_REGNUM;
723  loc->offset = 0;
724  loc->indirect = 0;
725  loc->base_offset = 0;
726
727  for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
728    lookup_cfa_1 (cfi, loc);
729
730  if (fde_table_in_use)
731    {
732      dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
733      for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
734	lookup_cfa_1 (cfi, loc);
735    }
736}
737
738/* The current rule for calculating the DWARF2 canonical frame address.  */
739static dw_cfa_location cfa;
740
741/* The register used for saving registers to the stack, and its offset
742   from the CFA.  */
743static dw_cfa_location cfa_store;
744
745/* The running total of the size of arguments pushed onto the stack.  */
746static HOST_WIDE_INT args_size;
747
748/* The last args_size we actually output.  */
749static HOST_WIDE_INT old_args_size;
750
751/* Entry point to update the canonical frame address (CFA).
752   LABEL is passed to add_fde_cfi.  The value of CFA is now to be
753   calculated from REG+OFFSET.  */
754
755void
756dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
757{
758  dw_cfa_location loc;
759  loc.indirect = 0;
760  loc.base_offset = 0;
761  loc.reg = reg;
762  loc.offset = offset;
763  def_cfa_1 (label, &loc);
764}
765
766/* Determine if two dw_cfa_location structures define the same data.  */
767
768static bool
769cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
770{
771  return (loc1->reg == loc2->reg
772	  && loc1->offset == loc2->offset
773	  && loc1->indirect == loc2->indirect
774	  && (loc1->indirect == 0
775	      || loc1->base_offset == loc2->base_offset));
776}
777
778/* This routine does the actual work.  The CFA is now calculated from
779   the dw_cfa_location structure.  */
780
781static void
782def_cfa_1 (const char *label, dw_cfa_location *loc_p)
783{
784  dw_cfi_ref cfi;
785  dw_cfa_location old_cfa, loc;
786
787  cfa = *loc_p;
788  loc = *loc_p;
789
790  if (cfa_store.reg == loc.reg && loc.indirect == 0)
791    cfa_store.offset = loc.offset;
792
793  loc.reg = DWARF_FRAME_REGNUM (loc.reg);
794  lookup_cfa (&old_cfa);
795
796  /* If nothing changed, no need to issue any call frame instructions.  */
797  if (cfa_equal_p (&loc, &old_cfa))
798    return;
799
800  cfi = new_cfi ();
801
802  if (loc.reg == old_cfa.reg && !loc.indirect)
803    {
804      /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
805	 the CFA register did not change but the offset did.  */
806      if (loc.offset < 0)
807	{
808	  HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
809	  gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
810
811	  cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
812	  cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
813	}
814      else
815	{
816	  cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
817	  cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
818	}
819    }
820
821#ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
822  else if (loc.offset == old_cfa.offset
823	   && old_cfa.reg != INVALID_REGNUM
824	   && !loc.indirect)
825    {
826      /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
827	 indicating the CFA register has changed to <register> but the
828	 offset has not changed.  */
829      cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
830      cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
831    }
832#endif
833
834  else if (loc.indirect == 0)
835    {
836      /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
837	 indicating the CFA register has changed to <register> with
838	 the specified offset.  */
839      if (loc.offset < 0)
840	{
841	  HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
842	  gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
843
844	  cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
845	  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
846	  cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
847	}
848      else
849	{
850	  cfi->dw_cfi_opc = DW_CFA_def_cfa;
851	  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
852	  cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
853	}
854    }
855  else
856    {
857      /* Construct a DW_CFA_def_cfa_expression instruction to
858	 calculate the CFA using a full location expression since no
859	 register-offset pair is available.  */
860      struct dw_loc_descr_struct *loc_list;
861
862      cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
863      loc_list = build_cfa_loc (&loc);
864      cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
865    }
866
867  add_fde_cfi (label, cfi);
868}
869
870/* Add the CFI for saving a register.  REG is the CFA column number.
871   LABEL is passed to add_fde_cfi.
872   If SREG is -1, the register is saved at OFFSET from the CFA;
873   otherwise it is saved in SREG.  */
874
875static void
876reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
877{
878  dw_cfi_ref cfi = new_cfi ();
879
880  cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
881
882  if (sreg == INVALID_REGNUM)
883    {
884      if (reg & ~0x3f)
885	/* The register number won't fit in 6 bits, so we have to use
886	   the long form.  */
887	cfi->dw_cfi_opc = DW_CFA_offset_extended;
888      else
889	cfi->dw_cfi_opc = DW_CFA_offset;
890
891#ifdef ENABLE_CHECKING
892      {
893	/* If we get an offset that is not a multiple of
894	   DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
895	   definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
896	   description.  */
897	HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
898
899	gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
900      }
901#endif
902      offset /= DWARF_CIE_DATA_ALIGNMENT;
903      if (offset < 0)
904	cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
905
906      cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
907    }
908  else if (sreg == reg)
909    cfi->dw_cfi_opc = DW_CFA_same_value;
910  else
911    {
912      cfi->dw_cfi_opc = DW_CFA_register;
913      cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
914    }
915
916  add_fde_cfi (label, cfi);
917}
918
919/* Add the CFI for saving a register window.  LABEL is passed to reg_save.
920   This CFI tells the unwinder that it needs to restore the window registers
921   from the previous frame's window save area.
922
923   ??? Perhaps we should note in the CIE where windows are saved (instead of
924   assuming 0(cfa)) and what registers are in the window.  */
925
926void
927dwarf2out_window_save (const char *label)
928{
929  dw_cfi_ref cfi = new_cfi ();
930
931  cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
932  add_fde_cfi (label, cfi);
933}
934
935/* Add a CFI to update the running total of the size of arguments
936   pushed onto the stack.  */
937
938void
939dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
940{
941  dw_cfi_ref cfi;
942
943  if (size == old_args_size)
944    return;
945
946  old_args_size = size;
947
948  cfi = new_cfi ();
949  cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
950  cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
951  add_fde_cfi (label, cfi);
952}
953
954/* Entry point for saving a register to the stack.  REG is the GCC register
955   number.  LABEL and OFFSET are passed to reg_save.  */
956
957void
958dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
959{
960  reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
961}
962
963/* Entry point for saving the return address in the stack.
964   LABEL and OFFSET are passed to reg_save.  */
965
966void
967dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
968{
969  reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
970}
971
972/* Entry point for saving the return address in a register.
973   LABEL and SREG are passed to reg_save.  */
974
975void
976dwarf2out_return_reg (const char *label, unsigned int sreg)
977{
978  reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
979}
980
981/* Record the initial position of the return address.  RTL is
982   INCOMING_RETURN_ADDR_RTX.  */
983
984static void
985initial_return_save (rtx rtl)
986{
987  unsigned int reg = INVALID_REGNUM;
988  HOST_WIDE_INT offset = 0;
989
990  switch (GET_CODE (rtl))
991    {
992    case REG:
993      /* RA is in a register.  */
994      reg = DWARF_FRAME_REGNUM (REGNO (rtl));
995      break;
996
997    case MEM:
998      /* RA is on the stack.  */
999      rtl = XEXP (rtl, 0);
1000      switch (GET_CODE (rtl))
1001	{
1002	case REG:
1003	  gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1004	  offset = 0;
1005	  break;
1006
1007	case PLUS:
1008	  gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1009	  offset = INTVAL (XEXP (rtl, 1));
1010	  break;
1011
1012	case MINUS:
1013	  gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1014	  offset = -INTVAL (XEXP (rtl, 1));
1015	  break;
1016
1017	default:
1018	  gcc_unreachable ();
1019	}
1020
1021      break;
1022
1023    case PLUS:
1024      /* The return address is at some offset from any value we can
1025	 actually load.  For instance, on the SPARC it is in %i7+8. Just
1026	 ignore the offset for now; it doesn't matter for unwinding frames.  */
1027      gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1028      initial_return_save (XEXP (rtl, 0));
1029      return;
1030
1031    default:
1032      gcc_unreachable ();
1033    }
1034
1035  if (reg != DWARF_FRAME_RETURN_COLUMN)
1036    reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1037}
1038
1039/* Given a SET, calculate the amount of stack adjustment it
1040   contains.  */
1041
1042static HOST_WIDE_INT
1043stack_adjust_offset (rtx pattern)
1044{
1045  rtx src = SET_SRC (pattern);
1046  rtx dest = SET_DEST (pattern);
1047  HOST_WIDE_INT offset = 0;
1048  enum rtx_code code;
1049
1050  if (dest == stack_pointer_rtx)
1051    {
1052      /* (set (reg sp) (plus (reg sp) (const_int))) */
1053      code = GET_CODE (src);
1054      if (! (code == PLUS || code == MINUS)
1055	  || XEXP (src, 0) != stack_pointer_rtx
1056	  || GET_CODE (XEXP (src, 1)) != CONST_INT)
1057	return 0;
1058
1059      offset = INTVAL (XEXP (src, 1));
1060      if (code == PLUS)
1061	offset = -offset;
1062    }
1063  else if (MEM_P (dest))
1064    {
1065      /* (set (mem (pre_dec (reg sp))) (foo)) */
1066      src = XEXP (dest, 0);
1067      code = GET_CODE (src);
1068
1069      switch (code)
1070	{
1071	case PRE_MODIFY:
1072	case POST_MODIFY:
1073	  if (XEXP (src, 0) == stack_pointer_rtx)
1074	    {
1075	      rtx val = XEXP (XEXP (src, 1), 1);
1076	      /* We handle only adjustments by constant amount.  */
1077	      gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1078			  && GET_CODE (val) == CONST_INT);
1079	      offset = -INTVAL (val);
1080	      break;
1081	    }
1082	  return 0;
1083
1084	case PRE_DEC:
1085	case POST_DEC:
1086	  if (XEXP (src, 0) == stack_pointer_rtx)
1087	    {
1088	      offset = GET_MODE_SIZE (GET_MODE (dest));
1089	      break;
1090	    }
1091	  return 0;
1092
1093	case PRE_INC:
1094	case POST_INC:
1095	  if (XEXP (src, 0) == stack_pointer_rtx)
1096	    {
1097	      offset = -GET_MODE_SIZE (GET_MODE (dest));
1098	      break;
1099	    }
1100	  return 0;
1101
1102	default:
1103	  return 0;
1104	}
1105    }
1106  else
1107    return 0;
1108
1109  return offset;
1110}
1111
1112/* Check INSN to see if it looks like a push or a stack adjustment, and
1113   make a note of it if it does.  EH uses this information to find out how
1114   much extra space it needs to pop off the stack.  */
1115
1116static void
1117dwarf2out_stack_adjust (rtx insn, bool after_p)
1118{
1119  HOST_WIDE_INT offset;
1120  const char *label;
1121  int i;
1122
1123  /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1124     with this function.  Proper support would require all frame-related
1125     insns to be marked, and to be able to handle saving state around
1126     epilogues textually in the middle of the function.  */
1127  if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1128    return;
1129
1130  /* If only calls can throw, and we have a frame pointer,
1131     save up adjustments until we see the CALL_INSN.  */
1132  if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1133    {
1134      if (CALL_P (insn) && !after_p)
1135	{
1136	  /* Extract the size of the args from the CALL rtx itself.  */
1137	  insn = PATTERN (insn);
1138	  if (GET_CODE (insn) == PARALLEL)
1139	    insn = XVECEXP (insn, 0, 0);
1140	  if (GET_CODE (insn) == SET)
1141	    insn = SET_SRC (insn);
1142	  gcc_assert (GET_CODE (insn) == CALL);
1143	  dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1144	}
1145      return;
1146    }
1147
1148  if (CALL_P (insn) && !after_p)
1149    {
1150      if (!flag_asynchronous_unwind_tables)
1151	dwarf2out_args_size ("", args_size);
1152      return;
1153    }
1154  else if (BARRIER_P (insn))
1155    {
1156      /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1157	 the compiler will have already emitted a stack adjustment, but
1158	 doesn't bother for calls to noreturn functions.  */
1159#ifdef STACK_GROWS_DOWNWARD
1160      offset = -args_size;
1161#else
1162      offset = args_size;
1163#endif
1164    }
1165  else if (GET_CODE (PATTERN (insn)) == SET)
1166    offset = stack_adjust_offset (PATTERN (insn));
1167  else if (GET_CODE (PATTERN (insn)) == PARALLEL
1168	   || GET_CODE (PATTERN (insn)) == SEQUENCE)
1169    {
1170      /* There may be stack adjustments inside compound insns.  Search
1171	 for them.  */
1172      for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1173	if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1174	  offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1175    }
1176  else
1177    return;
1178
1179  if (offset == 0)
1180    return;
1181
1182  if (cfa.reg == STACK_POINTER_REGNUM)
1183    cfa.offset += offset;
1184
1185#ifndef STACK_GROWS_DOWNWARD
1186  offset = -offset;
1187#endif
1188
1189  args_size += offset;
1190  if (args_size < 0)
1191    args_size = 0;
1192
1193  label = dwarf2out_cfi_label ();
1194  def_cfa_1 (label, &cfa);
1195  if (flag_asynchronous_unwind_tables)
1196    dwarf2out_args_size (label, args_size);
1197}
1198
1199#endif
1200
1201/* We delay emitting a register save until either (a) we reach the end
1202   of the prologue or (b) the register is clobbered.  This clusters
1203   register saves so that there are fewer pc advances.  */
1204
1205struct queued_reg_save GTY(())
1206{
1207  struct queued_reg_save *next;
1208  rtx reg;
1209  HOST_WIDE_INT cfa_offset;
1210  rtx saved_reg;
1211};
1212
1213static GTY(()) struct queued_reg_save *queued_reg_saves;
1214
1215/* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1216struct reg_saved_in_data GTY(()) {
1217  rtx orig_reg;
1218  rtx saved_in_reg;
1219};
1220
1221/* A list of registers saved in other registers.
1222   The list intentionally has a small maximum capacity of 4; if your
1223   port needs more than that, you might consider implementing a
1224   more efficient data structure.  */
1225static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1226static GTY(()) size_t num_regs_saved_in_regs;
1227
1228#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1229static const char *last_reg_save_label;
1230
1231/* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1232   SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1233
1234static void
1235queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1236{
1237  struct queued_reg_save *q;
1238
1239  /* Duplicates waste space, but it's also necessary to remove them
1240     for correctness, since the queue gets output in reverse
1241     order.  */
1242  for (q = queued_reg_saves; q != NULL; q = q->next)
1243    if (REGNO (q->reg) == REGNO (reg))
1244      break;
1245
1246  if (q == NULL)
1247    {
1248      q = ggc_alloc (sizeof (*q));
1249      q->next = queued_reg_saves;
1250      queued_reg_saves = q;
1251    }
1252
1253  q->reg = reg;
1254  q->cfa_offset = offset;
1255  q->saved_reg = sreg;
1256
1257  last_reg_save_label = label;
1258}
1259
1260/* Output all the entries in QUEUED_REG_SAVES.  */
1261
1262static void
1263flush_queued_reg_saves (void)
1264{
1265  struct queued_reg_save *q;
1266
1267  for (q = queued_reg_saves; q; q = q->next)
1268    {
1269      size_t i;
1270      unsigned int reg, sreg;
1271
1272      for (i = 0; i < num_regs_saved_in_regs; i++)
1273	if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1274	  break;
1275      if (q->saved_reg && i == num_regs_saved_in_regs)
1276	{
1277	  gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1278	  num_regs_saved_in_regs++;
1279	}
1280      if (i != num_regs_saved_in_regs)
1281	{
1282	  regs_saved_in_regs[i].orig_reg = q->reg;
1283	  regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1284	}
1285
1286      reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1287      if (q->saved_reg)
1288	sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1289      else
1290	sreg = INVALID_REGNUM;
1291      reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1292    }
1293
1294  queued_reg_saves = NULL;
1295  last_reg_save_label = NULL;
1296}
1297
1298/* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1299   location for?  Or, does it clobber a register which we've previously
1300   said that some other register is saved in, and for which we now
1301   have a new location for?  */
1302
1303static bool
1304clobbers_queued_reg_save (rtx insn)
1305{
1306  struct queued_reg_save *q;
1307
1308  for (q = queued_reg_saves; q; q = q->next)
1309    {
1310      size_t i;
1311      if (modified_in_p (q->reg, insn))
1312	return true;
1313      for (i = 0; i < num_regs_saved_in_regs; i++)
1314	if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1315	    && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1316	  return true;
1317    }
1318
1319  return false;
1320}
1321
1322/* Entry point for saving the first register into the second.  */
1323
1324void
1325dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1326{
1327  size_t i;
1328  unsigned int regno, sregno;
1329
1330  for (i = 0; i < num_regs_saved_in_regs; i++)
1331    if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1332      break;
1333  if (i == num_regs_saved_in_regs)
1334    {
1335      gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1336      num_regs_saved_in_regs++;
1337    }
1338  regs_saved_in_regs[i].orig_reg = reg;
1339  regs_saved_in_regs[i].saved_in_reg = sreg;
1340
1341  regno = DWARF_FRAME_REGNUM (REGNO (reg));
1342  sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1343  reg_save (label, regno, sregno, 0);
1344}
1345
1346/* What register, if any, is currently saved in REG?  */
1347
1348static rtx
1349reg_saved_in (rtx reg)
1350{
1351  unsigned int regn = REGNO (reg);
1352  size_t i;
1353  struct queued_reg_save *q;
1354
1355  for (q = queued_reg_saves; q; q = q->next)
1356    if (q->saved_reg && regn == REGNO (q->saved_reg))
1357      return q->reg;
1358
1359  for (i = 0; i < num_regs_saved_in_regs; i++)
1360    if (regs_saved_in_regs[i].saved_in_reg
1361	&& regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1362      return regs_saved_in_regs[i].orig_reg;
1363
1364  return NULL_RTX;
1365}
1366
1367
1368/* A temporary register holding an integral value used in adjusting SP
1369   or setting up the store_reg.  The "offset" field holds the integer
1370   value, not an offset.  */
1371static dw_cfa_location cfa_temp;
1372
1373/* Record call frame debugging information for an expression EXPR,
1374   which either sets SP or FP (adjusting how we calculate the frame
1375   address) or saves a register to the stack or another register.
1376   LABEL indicates the address of EXPR.
1377
1378   This function encodes a state machine mapping rtxes to actions on
1379   cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1380   users need not read the source code.
1381
1382  The High-Level Picture
1383
1384  Changes in the register we use to calculate the CFA: Currently we
1385  assume that if you copy the CFA register into another register, we
1386  should take the other one as the new CFA register; this seems to
1387  work pretty well.  If it's wrong for some target, it's simple
1388  enough not to set RTX_FRAME_RELATED_P on the insn in question.
1389
1390  Changes in the register we use for saving registers to the stack:
1391  This is usually SP, but not always.  Again, we deduce that if you
1392  copy SP into another register (and SP is not the CFA register),
1393  then the new register is the one we will be using for register
1394  saves.  This also seems to work.
1395
1396  Register saves: There's not much guesswork about this one; if
1397  RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1398  register save, and the register used to calculate the destination
1399  had better be the one we think we're using for this purpose.
1400  It's also assumed that a copy from a call-saved register to another
1401  register is saving that register if RTX_FRAME_RELATED_P is set on
1402  that instruction.  If the copy is from a call-saved register to
1403  the *same* register, that means that the register is now the same
1404  value as in the caller.
1405
1406  Except: If the register being saved is the CFA register, and the
1407  offset is nonzero, we are saving the CFA, so we assume we have to
1408  use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1409  the intent is to save the value of SP from the previous frame.
1410
1411  In addition, if a register has previously been saved to a different
1412  register,
1413
1414  Invariants / Summaries of Rules
1415
1416  cfa	       current rule for calculating the CFA.  It usually
1417	       consists of a register and an offset.
1418  cfa_store    register used by prologue code to save things to the stack
1419	       cfa_store.offset is the offset from the value of
1420	       cfa_store.reg to the actual CFA
1421  cfa_temp     register holding an integral value.  cfa_temp.offset
1422	       stores the value, which will be used to adjust the
1423	       stack pointer.  cfa_temp is also used like cfa_store,
1424	       to track stores to the stack via fp or a temp reg.
1425
1426  Rules  1- 4: Setting a register's value to cfa.reg or an expression
1427	       with cfa.reg as the first operand changes the cfa.reg and its
1428	       cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1429	       cfa_temp.offset.
1430
1431  Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1432	       expression yielding a constant.  This sets cfa_temp.reg
1433	       and cfa_temp.offset.
1434
1435  Rule 5:      Create a new register cfa_store used to save items to the
1436	       stack.
1437
1438  Rules 10-14: Save a register to the stack.  Define offset as the
1439	       difference of the original location and cfa_store's
1440	       location (or cfa_temp's location if cfa_temp is used).
1441
1442  The Rules
1443
1444  "{a,b}" indicates a choice of a xor b.
1445  "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1446
1447  Rule 1:
1448  (set <reg1> <reg2>:cfa.reg)
1449  effects: cfa.reg = <reg1>
1450	   cfa.offset unchanged
1451	   cfa_temp.reg = <reg1>
1452	   cfa_temp.offset = cfa.offset
1453
1454  Rule 2:
1455  (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1456			      {<const_int>,<reg>:cfa_temp.reg}))
1457  effects: cfa.reg = sp if fp used
1458	   cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1459	   cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1460	     if cfa_store.reg==sp
1461
1462  Rule 3:
1463  (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1464  effects: cfa.reg = fp
1465	   cfa_offset += +/- <const_int>
1466
1467  Rule 4:
1468  (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1469  constraints: <reg1> != fp
1470	       <reg1> != sp
1471  effects: cfa.reg = <reg1>
1472	   cfa_temp.reg = <reg1>
1473	   cfa_temp.offset = cfa.offset
1474
1475  Rule 5:
1476  (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1477  constraints: <reg1> != fp
1478	       <reg1> != sp
1479  effects: cfa_store.reg = <reg1>
1480	   cfa_store.offset = cfa.offset - cfa_temp.offset
1481
1482  Rule 6:
1483  (set <reg> <const_int>)
1484  effects: cfa_temp.reg = <reg>
1485	   cfa_temp.offset = <const_int>
1486
1487  Rule 7:
1488  (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1489  effects: cfa_temp.reg = <reg1>
1490	   cfa_temp.offset |= <const_int>
1491
1492  Rule 8:
1493  (set <reg> (high <exp>))
1494  effects: none
1495
1496  Rule 9:
1497  (set <reg> (lo_sum <exp> <const_int>))
1498  effects: cfa_temp.reg = <reg>
1499	   cfa_temp.offset = <const_int>
1500
1501  Rule 10:
1502  (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1503  effects: cfa_store.offset -= <const_int>
1504	   cfa.offset = cfa_store.offset if cfa.reg == sp
1505	   cfa.reg = sp
1506	   cfa.base_offset = -cfa_store.offset
1507
1508  Rule 11:
1509  (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1510  effects: cfa_store.offset += -/+ mode_size(mem)
1511	   cfa.offset = cfa_store.offset if cfa.reg == sp
1512	   cfa.reg = sp
1513	   cfa.base_offset = -cfa_store.offset
1514
1515  Rule 12:
1516  (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1517
1518       <reg2>)
1519  effects: cfa.reg = <reg1>
1520	   cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1521
1522  Rule 13:
1523  (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1524  effects: cfa.reg = <reg1>
1525	   cfa.base_offset = -{cfa_store,cfa_temp}.offset
1526
1527  Rule 14:
1528  (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1529  effects: cfa.reg = <reg1>
1530	   cfa.base_offset = -cfa_temp.offset
1531	   cfa_temp.offset -= mode_size(mem)
1532
1533  Rule 15:
1534  (set <reg> {unspec, unspec_volatile})
1535  effects: target-dependent  */
1536
1537static void
1538dwarf2out_frame_debug_expr (rtx expr, const char *label)
1539{
1540  rtx src, dest;
1541  HOST_WIDE_INT offset;
1542
1543  /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1544     the PARALLEL independently. The first element is always processed if
1545     it is a SET. This is for backward compatibility.   Other elements
1546     are processed only if they are SETs and the RTX_FRAME_RELATED_P
1547     flag is set in them.  */
1548  if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1549    {
1550      int par_index;
1551      int limit = XVECLEN (expr, 0);
1552
1553      for (par_index = 0; par_index < limit; par_index++)
1554	if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1555	    && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1556		|| par_index == 0))
1557	  dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1558
1559      return;
1560    }
1561
1562  gcc_assert (GET_CODE (expr) == SET);
1563
1564  src = SET_SRC (expr);
1565  dest = SET_DEST (expr);
1566
1567  if (REG_P (src))
1568    {
1569      rtx rsi = reg_saved_in (src);
1570      if (rsi)
1571	src = rsi;
1572    }
1573
1574  switch (GET_CODE (dest))
1575    {
1576    case REG:
1577      switch (GET_CODE (src))
1578	{
1579	  /* Setting FP from SP.  */
1580	case REG:
1581	  if (cfa.reg == (unsigned) REGNO (src))
1582	    {
1583	      /* Rule 1 */
1584	      /* Update the CFA rule wrt SP or FP.  Make sure src is
1585		 relative to the current CFA register.
1586
1587		 We used to require that dest be either SP or FP, but the
1588		 ARM copies SP to a temporary register, and from there to
1589		 FP.  So we just rely on the backends to only set
1590		 RTX_FRAME_RELATED_P on appropriate insns.  */
1591	      cfa.reg = REGNO (dest);
1592	      cfa_temp.reg = cfa.reg;
1593	      cfa_temp.offset = cfa.offset;
1594	    }
1595	  else
1596	    {
1597	      /* Saving a register in a register.  */
1598	      gcc_assert (!fixed_regs [REGNO (dest)]
1599			  /* For the SPARC and its register window.  */
1600			  || (DWARF_FRAME_REGNUM (REGNO (src))
1601			      == DWARF_FRAME_RETURN_COLUMN));
1602	      queue_reg_save (label, src, dest, 0);
1603	    }
1604	  break;
1605
1606	case PLUS:
1607	case MINUS:
1608	case LO_SUM:
1609	  if (dest == stack_pointer_rtx)
1610	    {
1611	      /* Rule 2 */
1612	      /* Adjusting SP.  */
1613	      switch (GET_CODE (XEXP (src, 1)))
1614		{
1615		case CONST_INT:
1616		  offset = INTVAL (XEXP (src, 1));
1617		  break;
1618		case REG:
1619		  gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1620			      == cfa_temp.reg);
1621		  offset = cfa_temp.offset;
1622		  break;
1623		default:
1624		  gcc_unreachable ();
1625		}
1626
1627	      if (XEXP (src, 0) == hard_frame_pointer_rtx)
1628		{
1629		  /* Restoring SP from FP in the epilogue.  */
1630		  gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1631		  cfa.reg = STACK_POINTER_REGNUM;
1632		}
1633	      else if (GET_CODE (src) == LO_SUM)
1634		/* Assume we've set the source reg of the LO_SUM from sp.  */
1635		;
1636	      else
1637		gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1638
1639	      if (GET_CODE (src) != MINUS)
1640		offset = -offset;
1641	      if (cfa.reg == STACK_POINTER_REGNUM)
1642		cfa.offset += offset;
1643	      if (cfa_store.reg == STACK_POINTER_REGNUM)
1644		cfa_store.offset += offset;
1645	    }
1646	  else if (dest == hard_frame_pointer_rtx)
1647	    {
1648	      /* Rule 3 */
1649	      /* Either setting the FP from an offset of the SP,
1650		 or adjusting the FP */
1651	      gcc_assert (frame_pointer_needed);
1652
1653	      gcc_assert (REG_P (XEXP (src, 0))
1654			  && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1655			  && GET_CODE (XEXP (src, 1)) == CONST_INT);
1656	      offset = INTVAL (XEXP (src, 1));
1657	      if (GET_CODE (src) != MINUS)
1658		offset = -offset;
1659	      cfa.offset += offset;
1660	      cfa.reg = HARD_FRAME_POINTER_REGNUM;
1661	    }
1662	  else
1663	    {
1664	      gcc_assert (GET_CODE (src) != MINUS);
1665
1666	      /* Rule 4 */
1667	      if (REG_P (XEXP (src, 0))
1668		  && REGNO (XEXP (src, 0)) == cfa.reg
1669		  && GET_CODE (XEXP (src, 1)) == CONST_INT)
1670		{
1671		  /* Setting a temporary CFA register that will be copied
1672		     into the FP later on.  */
1673		  offset = - INTVAL (XEXP (src, 1));
1674		  cfa.offset += offset;
1675		  cfa.reg = REGNO (dest);
1676		  /* Or used to save regs to the stack.  */
1677		  cfa_temp.reg = cfa.reg;
1678		  cfa_temp.offset = cfa.offset;
1679		}
1680
1681	      /* Rule 5 */
1682	      else if (REG_P (XEXP (src, 0))
1683		       && REGNO (XEXP (src, 0)) == cfa_temp.reg
1684		       && XEXP (src, 1) == stack_pointer_rtx)
1685		{
1686		  /* Setting a scratch register that we will use instead
1687		     of SP for saving registers to the stack.  */
1688		  gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1689		  cfa_store.reg = REGNO (dest);
1690		  cfa_store.offset = cfa.offset - cfa_temp.offset;
1691		}
1692
1693	      /* Rule 9 */
1694	      else if (GET_CODE (src) == LO_SUM
1695		       && GET_CODE (XEXP (src, 1)) == CONST_INT)
1696		{
1697		  cfa_temp.reg = REGNO (dest);
1698		  cfa_temp.offset = INTVAL (XEXP (src, 1));
1699		}
1700	      else
1701		gcc_unreachable ();
1702	    }
1703	  break;
1704
1705	  /* Rule 6 */
1706	case CONST_INT:
1707	  cfa_temp.reg = REGNO (dest);
1708	  cfa_temp.offset = INTVAL (src);
1709	  break;
1710
1711	  /* Rule 7 */
1712	case IOR:
1713	  gcc_assert (REG_P (XEXP (src, 0))
1714		      && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1715		      && GET_CODE (XEXP (src, 1)) == CONST_INT);
1716
1717	  if ((unsigned) REGNO (dest) != cfa_temp.reg)
1718	    cfa_temp.reg = REGNO (dest);
1719	  cfa_temp.offset |= INTVAL (XEXP (src, 1));
1720	  break;
1721
1722	  /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1723	     which will fill in all of the bits.  */
1724	  /* Rule 8 */
1725	case HIGH:
1726	  break;
1727
1728	  /* Rule 15 */
1729	case UNSPEC:
1730	case UNSPEC_VOLATILE:
1731	  gcc_assert (targetm.dwarf_handle_frame_unspec);
1732	  targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1733	  return;
1734
1735	default:
1736	  gcc_unreachable ();
1737	}
1738
1739      def_cfa_1 (label, &cfa);
1740      break;
1741
1742    case MEM:
1743      gcc_assert (REG_P (src));
1744
1745      /* Saving a register to the stack.  Make sure dest is relative to the
1746	 CFA register.  */
1747      switch (GET_CODE (XEXP (dest, 0)))
1748	{
1749	  /* Rule 10 */
1750	  /* With a push.  */
1751	case PRE_MODIFY:
1752	  /* We can't handle variable size modifications.  */
1753	  gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1754		      == CONST_INT);
1755	  offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1756
1757	  gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1758		      && cfa_store.reg == STACK_POINTER_REGNUM);
1759
1760	  cfa_store.offset += offset;
1761	  if (cfa.reg == STACK_POINTER_REGNUM)
1762	    cfa.offset = cfa_store.offset;
1763
1764	  offset = -cfa_store.offset;
1765	  break;
1766
1767	  /* Rule 11 */
1768	case PRE_INC:
1769	case PRE_DEC:
1770	  offset = GET_MODE_SIZE (GET_MODE (dest));
1771	  if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1772	    offset = -offset;
1773
1774	  gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1775		      && cfa_store.reg == STACK_POINTER_REGNUM);
1776
1777	  cfa_store.offset += offset;
1778	  if (cfa.reg == STACK_POINTER_REGNUM)
1779	    cfa.offset = cfa_store.offset;
1780
1781	  offset = -cfa_store.offset;
1782	  break;
1783
1784	  /* Rule 12 */
1785	  /* With an offset.  */
1786	case PLUS:
1787	case MINUS:
1788	case LO_SUM:
1789	  {
1790	    int regno;
1791
1792	    gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT);
1793	    offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1794	    if (GET_CODE (XEXP (dest, 0)) == MINUS)
1795	      offset = -offset;
1796
1797	    regno = REGNO (XEXP (XEXP (dest, 0), 0));
1798
1799	    if (cfa_store.reg == (unsigned) regno)
1800	      offset -= cfa_store.offset;
1801	    else
1802	      {
1803		gcc_assert (cfa_temp.reg == (unsigned) regno);
1804		offset -= cfa_temp.offset;
1805	      }
1806	  }
1807	  break;
1808
1809	  /* Rule 13 */
1810	  /* Without an offset.  */
1811	case REG:
1812	  {
1813	    int regno = REGNO (XEXP (dest, 0));
1814
1815	    if (cfa_store.reg == (unsigned) regno)
1816	      offset = -cfa_store.offset;
1817	    else
1818	      {
1819		gcc_assert (cfa_temp.reg == (unsigned) regno);
1820		offset = -cfa_temp.offset;
1821	      }
1822	  }
1823	  break;
1824
1825	  /* Rule 14 */
1826	case POST_INC:
1827	  gcc_assert (cfa_temp.reg
1828		      == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1829	  offset = -cfa_temp.offset;
1830	  cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1831	  break;
1832
1833	default:
1834	  gcc_unreachable ();
1835	}
1836
1837      if (REGNO (src) != STACK_POINTER_REGNUM
1838	  && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1839	  && (unsigned) REGNO (src) == cfa.reg)
1840	{
1841	  /* We're storing the current CFA reg into the stack.  */
1842
1843	  if (cfa.offset == 0)
1844	    {
1845	      /* If the source register is exactly the CFA, assume
1846		 we're saving SP like any other register; this happens
1847		 on the ARM.  */
1848	      def_cfa_1 (label, &cfa);
1849	      queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1850	      break;
1851	    }
1852	  else
1853	    {
1854	      /* Otherwise, we'll need to look in the stack to
1855		 calculate the CFA.  */
1856	      rtx x = XEXP (dest, 0);
1857
1858	      if (!REG_P (x))
1859		x = XEXP (x, 0);
1860	      gcc_assert (REG_P (x));
1861
1862	      cfa.reg = REGNO (x);
1863	      cfa.base_offset = offset;
1864	      cfa.indirect = 1;
1865	      def_cfa_1 (label, &cfa);
1866	      break;
1867	    }
1868	}
1869
1870      def_cfa_1 (label, &cfa);
1871      queue_reg_save (label, src, NULL_RTX, offset);
1872      break;
1873
1874    default:
1875      gcc_unreachable ();
1876    }
1877}
1878
1879/* Record call frame debugging information for INSN, which either
1880   sets SP or FP (adjusting how we calculate the frame address) or saves a
1881   register to the stack.  If INSN is NULL_RTX, initialize our state.
1882
1883   If AFTER_P is false, we're being called before the insn is emitted,
1884   otherwise after.  Call instructions get invoked twice.  */
1885
1886void
1887dwarf2out_frame_debug (rtx insn, bool after_p)
1888{
1889  const char *label;
1890  rtx src;
1891
1892  if (insn == NULL_RTX)
1893    {
1894      size_t i;
1895
1896      /* Flush any queued register saves.  */
1897      flush_queued_reg_saves ();
1898
1899      /* Set up state for generating call frame debug info.  */
1900      lookup_cfa (&cfa);
1901      gcc_assert (cfa.reg
1902		  == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1903
1904      cfa.reg = STACK_POINTER_REGNUM;
1905      cfa_store = cfa;
1906      cfa_temp.reg = -1;
1907      cfa_temp.offset = 0;
1908
1909      for (i = 0; i < num_regs_saved_in_regs; i++)
1910	{
1911	  regs_saved_in_regs[i].orig_reg = NULL_RTX;
1912	  regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1913	}
1914      num_regs_saved_in_regs = 0;
1915      return;
1916    }
1917
1918  if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1919    flush_queued_reg_saves ();
1920
1921  if (! RTX_FRAME_RELATED_P (insn))
1922    {
1923      if (!ACCUMULATE_OUTGOING_ARGS)
1924	dwarf2out_stack_adjust (insn, after_p);
1925      return;
1926    }
1927
1928  label = dwarf2out_cfi_label ();
1929  src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1930  if (src)
1931    insn = XEXP (src, 0);
1932  else
1933    insn = PATTERN (insn);
1934
1935  dwarf2out_frame_debug_expr (insn, label);
1936}
1937
1938#endif
1939
1940/* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1941static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1942 (enum dwarf_call_frame_info cfi);
1943
1944static enum dw_cfi_oprnd_type
1945dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1946{
1947  switch (cfi)
1948    {
1949    case DW_CFA_nop:
1950    case DW_CFA_GNU_window_save:
1951      return dw_cfi_oprnd_unused;
1952
1953    case DW_CFA_set_loc:
1954    case DW_CFA_advance_loc1:
1955    case DW_CFA_advance_loc2:
1956    case DW_CFA_advance_loc4:
1957    case DW_CFA_MIPS_advance_loc8:
1958      return dw_cfi_oprnd_addr;
1959
1960    case DW_CFA_offset:
1961    case DW_CFA_offset_extended:
1962    case DW_CFA_def_cfa:
1963    case DW_CFA_offset_extended_sf:
1964    case DW_CFA_def_cfa_sf:
1965    case DW_CFA_restore_extended:
1966    case DW_CFA_undefined:
1967    case DW_CFA_same_value:
1968    case DW_CFA_def_cfa_register:
1969    case DW_CFA_register:
1970      return dw_cfi_oprnd_reg_num;
1971
1972    case DW_CFA_def_cfa_offset:
1973    case DW_CFA_GNU_args_size:
1974    case DW_CFA_def_cfa_offset_sf:
1975      return dw_cfi_oprnd_offset;
1976
1977    case DW_CFA_def_cfa_expression:
1978    case DW_CFA_expression:
1979      return dw_cfi_oprnd_loc;
1980
1981    default:
1982      gcc_unreachable ();
1983    }
1984}
1985
1986/* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1987static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1988 (enum dwarf_call_frame_info cfi);
1989
1990static enum dw_cfi_oprnd_type
1991dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1992{
1993  switch (cfi)
1994    {
1995    case DW_CFA_def_cfa:
1996    case DW_CFA_def_cfa_sf:
1997    case DW_CFA_offset:
1998    case DW_CFA_offset_extended_sf:
1999    case DW_CFA_offset_extended:
2000      return dw_cfi_oprnd_offset;
2001
2002    case DW_CFA_register:
2003      return dw_cfi_oprnd_reg_num;
2004
2005    default:
2006      return dw_cfi_oprnd_unused;
2007    }
2008}
2009
2010#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2011
2012/* Map register numbers held in the call frame info that gcc has
2013   collected using DWARF_FRAME_REGNUM to those that should be output in
2014   .debug_frame and .eh_frame.  */
2015#ifndef DWARF2_FRAME_REG_OUT
2016#define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
2017#endif
2018
2019/* Output a Call Frame Information opcode and its operand(s).  */
2020
2021static void
2022output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2023{
2024  unsigned long r;
2025  if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2026    dw2_asm_output_data (1, (cfi->dw_cfi_opc
2027			     | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2028			 "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2029			 cfi->dw_cfi_oprnd1.dw_cfi_offset);
2030  else if (cfi->dw_cfi_opc == DW_CFA_offset)
2031    {
2032      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2033      dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2034			   "DW_CFA_offset, column 0x%lx", r);
2035      dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2036    }
2037  else if (cfi->dw_cfi_opc == DW_CFA_restore)
2038    {
2039      r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2040      dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2041			   "DW_CFA_restore, column 0x%lx", r);
2042    }
2043  else
2044    {
2045      dw2_asm_output_data (1, cfi->dw_cfi_opc,
2046			   "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2047
2048      switch (cfi->dw_cfi_opc)
2049	{
2050	case DW_CFA_set_loc:
2051	  if (for_eh)
2052	    dw2_asm_output_encoded_addr_rtx (
2053		ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2054		gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2055		false, NULL);
2056	  else
2057	    dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2058				 cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2059	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2060	  break;
2061
2062	case DW_CFA_advance_loc1:
2063	  dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2064				fde->dw_fde_current_label, NULL);
2065	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2066	  break;
2067
2068	case DW_CFA_advance_loc2:
2069	  dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2070				fde->dw_fde_current_label, NULL);
2071	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2072	  break;
2073
2074	case DW_CFA_advance_loc4:
2075	  dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2076				fde->dw_fde_current_label, NULL);
2077	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2078	  break;
2079
2080	case DW_CFA_MIPS_advance_loc8:
2081	  dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2082				fde->dw_fde_current_label, NULL);
2083	  fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2084	  break;
2085
2086	case DW_CFA_offset_extended:
2087	case DW_CFA_def_cfa:
2088	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2089	  dw2_asm_output_data_uleb128 (r, NULL);
2090	  dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2091	  break;
2092
2093	case DW_CFA_offset_extended_sf:
2094	case DW_CFA_def_cfa_sf:
2095	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2096	  dw2_asm_output_data_uleb128 (r, NULL);
2097	  dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2098	  break;
2099
2100	case DW_CFA_restore_extended:
2101	case DW_CFA_undefined:
2102	case DW_CFA_same_value:
2103	case DW_CFA_def_cfa_register:
2104	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2105	  dw2_asm_output_data_uleb128 (r, NULL);
2106	  break;
2107
2108	case DW_CFA_register:
2109	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2110	  dw2_asm_output_data_uleb128 (r, NULL);
2111	  r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2112	  dw2_asm_output_data_uleb128 (r, NULL);
2113	  break;
2114
2115	case DW_CFA_def_cfa_offset:
2116	case DW_CFA_GNU_args_size:
2117	  dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2118	  break;
2119
2120	case DW_CFA_def_cfa_offset_sf:
2121	  dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2122	  break;
2123
2124	case DW_CFA_GNU_window_save:
2125	  break;
2126
2127	case DW_CFA_def_cfa_expression:
2128	case DW_CFA_expression:
2129	  output_cfa_loc (cfi);
2130	  break;
2131
2132	case DW_CFA_GNU_negative_offset_extended:
2133	  /* Obsoleted by DW_CFA_offset_extended_sf.  */
2134	  gcc_unreachable ();
2135
2136	default:
2137	  break;
2138	}
2139    }
2140}
2141
2142/* Output the call frame information used to record information
2143   that relates to calculating the frame pointer, and records the
2144   location of saved registers.  */
2145
2146static void
2147output_call_frame_info (int for_eh)
2148{
2149  unsigned int i;
2150  dw_fde_ref fde;
2151  dw_cfi_ref cfi;
2152  char l1[20], l2[20], section_start_label[20];
2153  bool any_lsda_needed = false;
2154  char augmentation[6];
2155  int augmentation_size;
2156  int fde_encoding = DW_EH_PE_absptr;
2157  int per_encoding = DW_EH_PE_absptr;
2158  int lsda_encoding = DW_EH_PE_absptr;
2159  int return_reg;
2160
2161  /* Don't emit a CIE if there won't be any FDEs.  */
2162  if (fde_table_in_use == 0)
2163    return;
2164
2165  /* If we make FDEs linkonce, we may have to emit an empty label for
2166     an FDE that wouldn't otherwise be emitted.  We want to avoid
2167     having an FDE kept around when the function it refers to is
2168     discarded.  Example where this matters: a primary function
2169     template in C++ requires EH information, but an explicit
2170     specialization doesn't.  */
2171  if (TARGET_USES_WEAK_UNWIND_INFO
2172      && ! flag_asynchronous_unwind_tables
2173      && for_eh)
2174    for (i = 0; i < fde_table_in_use; i++)
2175      if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2176          && !fde_table[i].uses_eh_lsda
2177	  && ! DECL_WEAK (fde_table[i].decl))
2178	targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2179				      for_eh, /* empty */ 1);
2180
2181  /* If we don't have any functions we'll want to unwind out of, don't
2182     emit any EH unwind information.  Note that if exceptions aren't
2183     enabled, we won't have collected nothrow information, and if we
2184     asked for asynchronous tables, we always want this info.  */
2185  if (for_eh)
2186    {
2187      bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2188
2189      for (i = 0; i < fde_table_in_use; i++)
2190	if (fde_table[i].uses_eh_lsda)
2191	  any_eh_needed = any_lsda_needed = true;
2192        else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2193	  any_eh_needed = true;
2194	else if (! fde_table[i].nothrow
2195		 && ! fde_table[i].all_throwers_are_sibcalls)
2196	  any_eh_needed = true;
2197
2198      if (! any_eh_needed)
2199	return;
2200    }
2201
2202  /* We're going to be generating comments, so turn on app.  */
2203  if (flag_debug_asm)
2204    app_enable ();
2205
2206  if (for_eh)
2207    targetm.asm_out.eh_frame_section ();
2208  else
2209    named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
2210
2211  ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2212  ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2213
2214  /* Output the CIE.  */
2215  ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2216  ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2217  dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2218			"Length of Common Information Entry");
2219  ASM_OUTPUT_LABEL (asm_out_file, l1);
2220
2221  /* Now that the CIE pointer is PC-relative for EH,
2222     use 0 to identify the CIE.  */
2223  dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2224		       (for_eh ? 0 : DW_CIE_ID),
2225		       "CIE Identifier Tag");
2226
2227  dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2228
2229  augmentation[0] = 0;
2230  augmentation_size = 0;
2231  if (for_eh)
2232    {
2233      char *p;
2234
2235      /* Augmentation:
2236	 z	Indicates that a uleb128 is present to size the
2237		augmentation section.
2238	 L	Indicates the encoding (and thus presence) of
2239		an LSDA pointer in the FDE augmentation.
2240	 R	Indicates a non-default pointer encoding for
2241		FDE code pointers.
2242	 P	Indicates the presence of an encoding + language
2243		personality routine in the CIE augmentation.  */
2244
2245      fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2246      per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2247      lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2248
2249      p = augmentation + 1;
2250      if (eh_personality_libfunc)
2251	{
2252	  *p++ = 'P';
2253	  augmentation_size += 1 + size_of_encoded_value (per_encoding);
2254	}
2255      if (any_lsda_needed)
2256	{
2257	  *p++ = 'L';
2258	  augmentation_size += 1;
2259	}
2260      if (fde_encoding != DW_EH_PE_absptr)
2261	{
2262	  *p++ = 'R';
2263	  augmentation_size += 1;
2264	}
2265      if (p > augmentation + 1)
2266	{
2267	  augmentation[0] = 'z';
2268	  *p = '\0';
2269	}
2270
2271      /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2272      if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2273	{
2274	  int offset = (  4		/* Length */
2275			+ 4		/* CIE Id */
2276			+ 1		/* CIE version */
2277			+ strlen (augmentation) + 1	/* Augmentation */
2278			+ size_of_uleb128 (1)		/* Code alignment */
2279			+ size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2280			+ 1		/* RA column */
2281			+ 1		/* Augmentation size */
2282			+ 1		/* Personality encoding */ );
2283	  int pad = -offset & (PTR_SIZE - 1);
2284
2285	  augmentation_size += pad;
2286
2287	  /* Augmentations should be small, so there's scarce need to
2288	     iterate for a solution.  Die if we exceed one uleb128 byte.  */
2289	  gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2290	}
2291    }
2292
2293  dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2294  dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2295  dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2296			       "CIE Data Alignment Factor");
2297
2298  return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2299  if (DW_CIE_VERSION == 1)
2300    dw2_asm_output_data (1, return_reg, "CIE RA Column");
2301  else
2302    dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2303
2304  if (augmentation[0])
2305    {
2306      dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2307      if (eh_personality_libfunc)
2308	{
2309	  dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2310			       eh_data_format_name (per_encoding));
2311	  dw2_asm_output_encoded_addr_rtx (per_encoding,
2312					   eh_personality_libfunc,
2313					   true, NULL);
2314	}
2315
2316      if (any_lsda_needed)
2317	dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2318			     eh_data_format_name (lsda_encoding));
2319
2320      if (fde_encoding != DW_EH_PE_absptr)
2321	dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2322			     eh_data_format_name (fde_encoding));
2323    }
2324
2325  for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2326    output_cfi (cfi, NULL, for_eh);
2327
2328  /* Pad the CIE out to an address sized boundary.  */
2329  ASM_OUTPUT_ALIGN (asm_out_file,
2330		    floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2331  ASM_OUTPUT_LABEL (asm_out_file, l2);
2332
2333  /* Loop through all of the FDE's.  */
2334  for (i = 0; i < fde_table_in_use; i++)
2335    {
2336      fde = &fde_table[i];
2337
2338      /* Don't emit EH unwind info for leaf functions that don't need it.  */
2339      if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2340	  && (fde->nothrow || fde->all_throwers_are_sibcalls)
2341	  && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2342	  && !fde->uses_eh_lsda)
2343	continue;
2344
2345      targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2346      targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2347      ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2348      ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2349      dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2350			    "FDE Length");
2351      ASM_OUTPUT_LABEL (asm_out_file, l1);
2352
2353      if (for_eh)
2354	dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2355      else
2356	dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2357			       "FDE CIE offset");
2358
2359      if (for_eh)
2360	{
2361	  rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2362	  SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2363	  dw2_asm_output_encoded_addr_rtx (fde_encoding,
2364					   sym_ref,
2365					   false,
2366					   "FDE initial location");
2367	  if (fde->dw_fde_switched_sections)
2368	    {
2369	      rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2370				      fde->dw_fde_unlikely_section_label);
2371	      rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2372				      fde->dw_fde_hot_section_label);
2373	      SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2374	      SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2375	      dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2376					       "FDE initial location");
2377	      dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2378				    fde->dw_fde_hot_section_end_label,
2379				    fde->dw_fde_hot_section_label,
2380				    "FDE address range");
2381	      dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2382					       "FDE initial location");
2383	      dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2384				    fde->dw_fde_unlikely_section_end_label,
2385				    fde->dw_fde_unlikely_section_label,
2386				    "FDE address range");
2387	    }
2388	  else
2389	    dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2390				  fde->dw_fde_end, fde->dw_fde_begin,
2391				  "FDE address range");
2392	}
2393      else
2394	{
2395	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2396			       "FDE initial location");
2397	  if (fde->dw_fde_switched_sections)
2398	    {
2399	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2400				   fde->dw_fde_hot_section_label,
2401				   "FDE initial location");
2402	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2403				    fde->dw_fde_hot_section_end_label,
2404				    fde->dw_fde_hot_section_label,
2405				    "FDE address range");
2406	      dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2407				   fde->dw_fde_unlikely_section_label,
2408				   "FDE initial location");
2409	      dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2410				    fde->dw_fde_unlikely_section_end_label,
2411				    fde->dw_fde_unlikely_section_label,
2412				    "FDE address range");
2413	    }
2414	  else
2415	    dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2416				  fde->dw_fde_end, fde->dw_fde_begin,
2417				  "FDE address range");
2418	}
2419
2420      if (augmentation[0])
2421	{
2422	  if (any_lsda_needed)
2423	    {
2424	      int size = size_of_encoded_value (lsda_encoding);
2425
2426	      if (lsda_encoding == DW_EH_PE_aligned)
2427		{
2428		  int offset = (  4		/* Length */
2429				+ 4		/* CIE offset */
2430				+ 2 * size_of_encoded_value (fde_encoding)
2431				+ 1		/* Augmentation size */ );
2432		  int pad = -offset & (PTR_SIZE - 1);
2433
2434		  size += pad;
2435		  gcc_assert (size_of_uleb128 (size) == 1);
2436		}
2437
2438	      dw2_asm_output_data_uleb128 (size, "Augmentation size");
2439
2440	      if (fde->uses_eh_lsda)
2441		{
2442		  ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2443					       fde->funcdef_number);
2444		  dw2_asm_output_encoded_addr_rtx (
2445			lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2446			false, "Language Specific Data Area");
2447		}
2448	      else
2449		{
2450		  if (lsda_encoding == DW_EH_PE_aligned)
2451		    ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2452		  dw2_asm_output_data
2453		    (size_of_encoded_value (lsda_encoding), 0,
2454		     "Language Specific Data Area (none)");
2455		}
2456	    }
2457	  else
2458	    dw2_asm_output_data_uleb128 (0, "Augmentation size");
2459	}
2460
2461      /* Loop through the Call Frame Instructions associated with
2462	 this FDE.  */
2463      fde->dw_fde_current_label = fde->dw_fde_begin;
2464      for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2465	output_cfi (cfi, fde, for_eh);
2466
2467      /* Pad the FDE out to an address sized boundary.  */
2468      ASM_OUTPUT_ALIGN (asm_out_file,
2469			floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2470      ASM_OUTPUT_LABEL (asm_out_file, l2);
2471    }
2472
2473  if (for_eh && targetm.terminate_dw2_eh_frame_info)
2474    dw2_asm_output_data (4, 0, "End of Table");
2475#ifdef MIPS_DEBUGGING_INFO
2476  /* Work around Irix 6 assembler bug whereby labels at the end of a section
2477     get a value of 0.  Putting .align 0 after the label fixes it.  */
2478  ASM_OUTPUT_ALIGN (asm_out_file, 0);
2479#endif
2480
2481  /* Turn off app to make assembly quicker.  */
2482  if (flag_debug_asm)
2483    app_disable ();
2484}
2485
2486/* Output a marker (i.e. a label) for the beginning of a function, before
2487   the prologue.  */
2488
2489void
2490dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2491			  const char *file ATTRIBUTE_UNUSED)
2492{
2493  char label[MAX_ARTIFICIAL_LABEL_BYTES];
2494  char * dup_label;
2495  dw_fde_ref fde;
2496
2497  current_function_func_begin_label = NULL;
2498
2499#ifdef TARGET_UNWIND_INFO
2500  /* ??? current_function_func_begin_label is also used by except.c
2501     for call-site information.  We must emit this label if it might
2502     be used.  */
2503  if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2504      && ! dwarf2out_do_frame ())
2505    return;
2506#else
2507  if (! dwarf2out_do_frame ())
2508    return;
2509#endif
2510
2511  function_section (current_function_decl);
2512  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2513			       current_function_funcdef_no);
2514  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2515			  current_function_funcdef_no);
2516  dup_label = xstrdup (label);
2517  current_function_func_begin_label = dup_label;
2518
2519#ifdef TARGET_UNWIND_INFO
2520  /* We can elide the fde allocation if we're not emitting debug info.  */
2521  if (! dwarf2out_do_frame ())
2522    return;
2523#endif
2524
2525  /* Expand the fde table if necessary.  */
2526  if (fde_table_in_use == fde_table_allocated)
2527    {
2528      fde_table_allocated += FDE_TABLE_INCREMENT;
2529      fde_table = ggc_realloc (fde_table,
2530			       fde_table_allocated * sizeof (dw_fde_node));
2531      memset (fde_table + fde_table_in_use, 0,
2532	      FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2533    }
2534
2535  /* Record the FDE associated with this function.  */
2536  current_funcdef_fde = fde_table_in_use;
2537
2538  /* Add the new FDE at the end of the fde_table.  */
2539  fde = &fde_table[fde_table_in_use++];
2540  fde->decl = current_function_decl;
2541  fde->dw_fde_begin = dup_label;
2542  fde->dw_fde_current_label = dup_label;
2543  fde->dw_fde_hot_section_label = NULL;
2544  fde->dw_fde_hot_section_end_label = NULL;
2545  fde->dw_fde_unlikely_section_label = NULL;
2546  fde->dw_fde_unlikely_section_end_label = NULL;
2547  fde->dw_fde_switched_sections = false;
2548  fde->dw_fde_end = NULL;
2549  fde->dw_fde_cfi = NULL;
2550  fde->funcdef_number = current_function_funcdef_no;
2551  fde->nothrow = TREE_NOTHROW (current_function_decl);
2552  fde->uses_eh_lsda = cfun->uses_eh_lsda;
2553  fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2554
2555  args_size = old_args_size = 0;
2556
2557  /* We only want to output line number information for the genuine dwarf2
2558     prologue case, not the eh frame case.  */
2559#ifdef DWARF2_DEBUGGING_INFO
2560  if (file)
2561    dwarf2out_source_line (line, file);
2562#endif
2563}
2564
2565/* Output a marker (i.e. a label) for the absolute end of the generated code
2566   for a function definition.  This gets called *after* the epilogue code has
2567   been generated.  */
2568
2569void
2570dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2571			const char *file ATTRIBUTE_UNUSED)
2572{
2573  dw_fde_ref fde;
2574  char label[MAX_ARTIFICIAL_LABEL_BYTES];
2575
2576  /* Output a label to mark the endpoint of the code generated for this
2577     function.  */
2578  ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2579			       current_function_funcdef_no);
2580  ASM_OUTPUT_LABEL (asm_out_file, label);
2581  fde = &fde_table[fde_table_in_use - 1];
2582  fde->dw_fde_end = xstrdup (label);
2583}
2584
2585void
2586dwarf2out_frame_init (void)
2587{
2588  /* Allocate the initial hunk of the fde_table.  */
2589  fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2590  fde_table_allocated = FDE_TABLE_INCREMENT;
2591  fde_table_in_use = 0;
2592
2593  /* Generate the CFA instructions common to all FDE's.  Do it now for the
2594     sake of lookup_cfa.  */
2595
2596  /* On entry, the Canonical Frame Address is at SP.  */
2597  dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2598
2599#ifdef DWARF2_UNWIND_INFO
2600  if (DWARF2_UNWIND_INFO)
2601    initial_return_save (INCOMING_RETURN_ADDR_RTX);
2602#endif
2603}
2604
2605void
2606dwarf2out_frame_finish (void)
2607{
2608  /* Output call frame information.  */
2609  if (DWARF2_FRAME_INFO)
2610    output_call_frame_info (0);
2611
2612#ifndef TARGET_UNWIND_INFO
2613  /* Output another copy for the unwinder.  */
2614  if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2615    output_call_frame_info (1);
2616#endif
2617}
2618#endif
2619
2620/* And now, the subset of the debugging information support code necessary
2621   for emitting location expressions.  */
2622
2623/* We need some way to distinguish DW_OP_addr with a direct symbol
2624   relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2625#define INTERNAL_DW_OP_tls_addr		(0x100 + DW_OP_addr)
2626
2627
2628typedef struct dw_val_struct *dw_val_ref;
2629typedef struct die_struct *dw_die_ref;
2630typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2631typedef struct dw_loc_list_struct *dw_loc_list_ref;
2632
2633/* Each DIE may have a series of attribute/value pairs.  Values
2634   can take on several forms.  The forms that are used in this
2635   implementation are listed below.  */
2636
2637enum dw_val_class
2638{
2639  dw_val_class_addr,
2640  dw_val_class_offset,
2641  dw_val_class_loc,
2642  dw_val_class_loc_list,
2643  dw_val_class_range_list,
2644  dw_val_class_const,
2645  dw_val_class_unsigned_const,
2646  dw_val_class_long_long,
2647  dw_val_class_vec,
2648  dw_val_class_flag,
2649  dw_val_class_die_ref,
2650  dw_val_class_fde_ref,
2651  dw_val_class_lbl_id,
2652  dw_val_class_lbl_offset,
2653  dw_val_class_str
2654};
2655
2656/* Describe a double word constant value.  */
2657/* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2658
2659typedef struct dw_long_long_struct GTY(())
2660{
2661  unsigned long hi;
2662  unsigned long low;
2663}
2664dw_long_long_const;
2665
2666/* Describe a floating point constant value, or a vector constant value.  */
2667
2668typedef struct dw_vec_struct GTY(())
2669{
2670  unsigned char * GTY((length ("%h.length"))) array;
2671  unsigned length;
2672  unsigned elt_size;
2673}
2674dw_vec_const;
2675
2676/* The dw_val_node describes an attribute's value, as it is
2677   represented internally.  */
2678
2679typedef struct dw_val_struct GTY(())
2680{
2681  enum dw_val_class val_class;
2682  union dw_val_struct_union
2683    {
2684      rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2685      unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2686      dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2687      dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2688      HOST_WIDE_INT GTY ((default)) val_int;
2689      unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2690      dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2691      dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2692      struct dw_val_die_union
2693	{
2694	  dw_die_ref die;
2695	  int external;
2696	} GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2697      unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2698      struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2699      char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2700      unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2701    }
2702  GTY ((desc ("%1.val_class"))) v;
2703}
2704dw_val_node;
2705
2706/* Locations in memory are described using a sequence of stack machine
2707   operations.  */
2708
2709typedef struct dw_loc_descr_struct GTY(())
2710{
2711  dw_loc_descr_ref dw_loc_next;
2712  enum dwarf_location_atom dw_loc_opc;
2713  dw_val_node dw_loc_oprnd1;
2714  dw_val_node dw_loc_oprnd2;
2715  int dw_loc_addr;
2716}
2717dw_loc_descr_node;
2718
2719/* Location lists are ranges + location descriptions for that range,
2720   so you can track variables that are in different places over
2721   their entire life.  */
2722typedef struct dw_loc_list_struct GTY(())
2723{
2724  dw_loc_list_ref dw_loc_next;
2725  const char *begin; /* Label for begin address of range */
2726  const char *end;  /* Label for end address of range */
2727  char *ll_symbol; /* Label for beginning of location list.
2728		      Only on head of list */
2729  const char *section; /* Section this loclist is relative to */
2730  dw_loc_descr_ref expr;
2731} dw_loc_list_node;
2732
2733#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2734
2735static const char *dwarf_stack_op_name (unsigned);
2736static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2737				       unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2738static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2739static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2740static unsigned long size_of_locs (dw_loc_descr_ref);
2741static void output_loc_operands (dw_loc_descr_ref);
2742static void output_loc_sequence (dw_loc_descr_ref);
2743
2744/* Convert a DWARF stack opcode into its string name.  */
2745
2746static const char *
2747dwarf_stack_op_name (unsigned int op)
2748{
2749  switch (op)
2750    {
2751    case DW_OP_addr:
2752    case INTERNAL_DW_OP_tls_addr:
2753      return "DW_OP_addr";
2754    case DW_OP_deref:
2755      return "DW_OP_deref";
2756    case DW_OP_const1u:
2757      return "DW_OP_const1u";
2758    case DW_OP_const1s:
2759      return "DW_OP_const1s";
2760    case DW_OP_const2u:
2761      return "DW_OP_const2u";
2762    case DW_OP_const2s:
2763      return "DW_OP_const2s";
2764    case DW_OP_const4u:
2765      return "DW_OP_const4u";
2766    case DW_OP_const4s:
2767      return "DW_OP_const4s";
2768    case DW_OP_const8u:
2769      return "DW_OP_const8u";
2770    case DW_OP_const8s:
2771      return "DW_OP_const8s";
2772    case DW_OP_constu:
2773      return "DW_OP_constu";
2774    case DW_OP_consts:
2775      return "DW_OP_consts";
2776    case DW_OP_dup:
2777      return "DW_OP_dup";
2778    case DW_OP_drop:
2779      return "DW_OP_drop";
2780    case DW_OP_over:
2781      return "DW_OP_over";
2782    case DW_OP_pick:
2783      return "DW_OP_pick";
2784    case DW_OP_swap:
2785      return "DW_OP_swap";
2786    case DW_OP_rot:
2787      return "DW_OP_rot";
2788    case DW_OP_xderef:
2789      return "DW_OP_xderef";
2790    case DW_OP_abs:
2791      return "DW_OP_abs";
2792    case DW_OP_and:
2793      return "DW_OP_and";
2794    case DW_OP_div:
2795      return "DW_OP_div";
2796    case DW_OP_minus:
2797      return "DW_OP_minus";
2798    case DW_OP_mod:
2799      return "DW_OP_mod";
2800    case DW_OP_mul:
2801      return "DW_OP_mul";
2802    case DW_OP_neg:
2803      return "DW_OP_neg";
2804    case DW_OP_not:
2805      return "DW_OP_not";
2806    case DW_OP_or:
2807      return "DW_OP_or";
2808    case DW_OP_plus:
2809      return "DW_OP_plus";
2810    case DW_OP_plus_uconst:
2811      return "DW_OP_plus_uconst";
2812    case DW_OP_shl:
2813      return "DW_OP_shl";
2814    case DW_OP_shr:
2815      return "DW_OP_shr";
2816    case DW_OP_shra:
2817      return "DW_OP_shra";
2818    case DW_OP_xor:
2819      return "DW_OP_xor";
2820    case DW_OP_bra:
2821      return "DW_OP_bra";
2822    case DW_OP_eq:
2823      return "DW_OP_eq";
2824    case DW_OP_ge:
2825      return "DW_OP_ge";
2826    case DW_OP_gt:
2827      return "DW_OP_gt";
2828    case DW_OP_le:
2829      return "DW_OP_le";
2830    case DW_OP_lt:
2831      return "DW_OP_lt";
2832    case DW_OP_ne:
2833      return "DW_OP_ne";
2834    case DW_OP_skip:
2835      return "DW_OP_skip";
2836    case DW_OP_lit0:
2837      return "DW_OP_lit0";
2838    case DW_OP_lit1:
2839      return "DW_OP_lit1";
2840    case DW_OP_lit2:
2841      return "DW_OP_lit2";
2842    case DW_OP_lit3:
2843      return "DW_OP_lit3";
2844    case DW_OP_lit4:
2845      return "DW_OP_lit4";
2846    case DW_OP_lit5:
2847      return "DW_OP_lit5";
2848    case DW_OP_lit6:
2849      return "DW_OP_lit6";
2850    case DW_OP_lit7:
2851      return "DW_OP_lit7";
2852    case DW_OP_lit8:
2853      return "DW_OP_lit8";
2854    case DW_OP_lit9:
2855      return "DW_OP_lit9";
2856    case DW_OP_lit10:
2857      return "DW_OP_lit10";
2858    case DW_OP_lit11:
2859      return "DW_OP_lit11";
2860    case DW_OP_lit12:
2861      return "DW_OP_lit12";
2862    case DW_OP_lit13:
2863      return "DW_OP_lit13";
2864    case DW_OP_lit14:
2865      return "DW_OP_lit14";
2866    case DW_OP_lit15:
2867      return "DW_OP_lit15";
2868    case DW_OP_lit16:
2869      return "DW_OP_lit16";
2870    case DW_OP_lit17:
2871      return "DW_OP_lit17";
2872    case DW_OP_lit18:
2873      return "DW_OP_lit18";
2874    case DW_OP_lit19:
2875      return "DW_OP_lit19";
2876    case DW_OP_lit20:
2877      return "DW_OP_lit20";
2878    case DW_OP_lit21:
2879      return "DW_OP_lit21";
2880    case DW_OP_lit22:
2881      return "DW_OP_lit22";
2882    case DW_OP_lit23:
2883      return "DW_OP_lit23";
2884    case DW_OP_lit24:
2885      return "DW_OP_lit24";
2886    case DW_OP_lit25:
2887      return "DW_OP_lit25";
2888    case DW_OP_lit26:
2889      return "DW_OP_lit26";
2890    case DW_OP_lit27:
2891      return "DW_OP_lit27";
2892    case DW_OP_lit28:
2893      return "DW_OP_lit28";
2894    case DW_OP_lit29:
2895      return "DW_OP_lit29";
2896    case DW_OP_lit30:
2897      return "DW_OP_lit30";
2898    case DW_OP_lit31:
2899      return "DW_OP_lit31";
2900    case DW_OP_reg0:
2901      return "DW_OP_reg0";
2902    case DW_OP_reg1:
2903      return "DW_OP_reg1";
2904    case DW_OP_reg2:
2905      return "DW_OP_reg2";
2906    case DW_OP_reg3:
2907      return "DW_OP_reg3";
2908    case DW_OP_reg4:
2909      return "DW_OP_reg4";
2910    case DW_OP_reg5:
2911      return "DW_OP_reg5";
2912    case DW_OP_reg6:
2913      return "DW_OP_reg6";
2914    case DW_OP_reg7:
2915      return "DW_OP_reg7";
2916    case DW_OP_reg8:
2917      return "DW_OP_reg8";
2918    case DW_OP_reg9:
2919      return "DW_OP_reg9";
2920    case DW_OP_reg10:
2921      return "DW_OP_reg10";
2922    case DW_OP_reg11:
2923      return "DW_OP_reg11";
2924    case DW_OP_reg12:
2925      return "DW_OP_reg12";
2926    case DW_OP_reg13:
2927      return "DW_OP_reg13";
2928    case DW_OP_reg14:
2929      return "DW_OP_reg14";
2930    case DW_OP_reg15:
2931      return "DW_OP_reg15";
2932    case DW_OP_reg16:
2933      return "DW_OP_reg16";
2934    case DW_OP_reg17:
2935      return "DW_OP_reg17";
2936    case DW_OP_reg18:
2937      return "DW_OP_reg18";
2938    case DW_OP_reg19:
2939      return "DW_OP_reg19";
2940    case DW_OP_reg20:
2941      return "DW_OP_reg20";
2942    case DW_OP_reg21:
2943      return "DW_OP_reg21";
2944    case DW_OP_reg22:
2945      return "DW_OP_reg22";
2946    case DW_OP_reg23:
2947      return "DW_OP_reg23";
2948    case DW_OP_reg24:
2949      return "DW_OP_reg24";
2950    case DW_OP_reg25:
2951      return "DW_OP_reg25";
2952    case DW_OP_reg26:
2953      return "DW_OP_reg26";
2954    case DW_OP_reg27:
2955      return "DW_OP_reg27";
2956    case DW_OP_reg28:
2957      return "DW_OP_reg28";
2958    case DW_OP_reg29:
2959      return "DW_OP_reg29";
2960    case DW_OP_reg30:
2961      return "DW_OP_reg30";
2962    case DW_OP_reg31:
2963      return "DW_OP_reg31";
2964    case DW_OP_breg0:
2965      return "DW_OP_breg0";
2966    case DW_OP_breg1:
2967      return "DW_OP_breg1";
2968    case DW_OP_breg2:
2969      return "DW_OP_breg2";
2970    case DW_OP_breg3:
2971      return "DW_OP_breg3";
2972    case DW_OP_breg4:
2973      return "DW_OP_breg4";
2974    case DW_OP_breg5:
2975      return "DW_OP_breg5";
2976    case DW_OP_breg6:
2977      return "DW_OP_breg6";
2978    case DW_OP_breg7:
2979      return "DW_OP_breg7";
2980    case DW_OP_breg8:
2981      return "DW_OP_breg8";
2982    case DW_OP_breg9:
2983      return "DW_OP_breg9";
2984    case DW_OP_breg10:
2985      return "DW_OP_breg10";
2986    case DW_OP_breg11:
2987      return "DW_OP_breg11";
2988    case DW_OP_breg12:
2989      return "DW_OP_breg12";
2990    case DW_OP_breg13:
2991      return "DW_OP_breg13";
2992    case DW_OP_breg14:
2993      return "DW_OP_breg14";
2994    case DW_OP_breg15:
2995      return "DW_OP_breg15";
2996    case DW_OP_breg16:
2997      return "DW_OP_breg16";
2998    case DW_OP_breg17:
2999      return "DW_OP_breg17";
3000    case DW_OP_breg18:
3001      return "DW_OP_breg18";
3002    case DW_OP_breg19:
3003      return "DW_OP_breg19";
3004    case DW_OP_breg20:
3005      return "DW_OP_breg20";
3006    case DW_OP_breg21:
3007      return "DW_OP_breg21";
3008    case DW_OP_breg22:
3009      return "DW_OP_breg22";
3010    case DW_OP_breg23:
3011      return "DW_OP_breg23";
3012    case DW_OP_breg24:
3013      return "DW_OP_breg24";
3014    case DW_OP_breg25:
3015      return "DW_OP_breg25";
3016    case DW_OP_breg26:
3017      return "DW_OP_breg26";
3018    case DW_OP_breg27:
3019      return "DW_OP_breg27";
3020    case DW_OP_breg28:
3021      return "DW_OP_breg28";
3022    case DW_OP_breg29:
3023      return "DW_OP_breg29";
3024    case DW_OP_breg30:
3025      return "DW_OP_breg30";
3026    case DW_OP_breg31:
3027      return "DW_OP_breg31";
3028    case DW_OP_regx:
3029      return "DW_OP_regx";
3030    case DW_OP_fbreg:
3031      return "DW_OP_fbreg";
3032    case DW_OP_bregx:
3033      return "DW_OP_bregx";
3034    case DW_OP_piece:
3035      return "DW_OP_piece";
3036    case DW_OP_deref_size:
3037      return "DW_OP_deref_size";
3038    case DW_OP_xderef_size:
3039      return "DW_OP_xderef_size";
3040    case DW_OP_nop:
3041      return "DW_OP_nop";
3042    case DW_OP_push_object_address:
3043      return "DW_OP_push_object_address";
3044    case DW_OP_call2:
3045      return "DW_OP_call2";
3046    case DW_OP_call4:
3047      return "DW_OP_call4";
3048    case DW_OP_call_ref:
3049      return "DW_OP_call_ref";
3050    case DW_OP_GNU_push_tls_address:
3051      return "DW_OP_GNU_push_tls_address";
3052    default:
3053      return "OP_<unknown>";
3054    }
3055}
3056
3057/* Return a pointer to a newly allocated location description.  Location
3058   descriptions are simple expression terms that can be strung
3059   together to form more complicated location (address) descriptions.  */
3060
3061static inline dw_loc_descr_ref
3062new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3063	       unsigned HOST_WIDE_INT oprnd2)
3064{
3065  dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3066
3067  descr->dw_loc_opc = op;
3068  descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3069  descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3070  descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3071  descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3072
3073  return descr;
3074}
3075
3076/* Add a location description term to a location description expression.  */
3077
3078static inline void
3079add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3080{
3081  dw_loc_descr_ref *d;
3082
3083  /* Find the end of the chain.  */
3084  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3085    ;
3086
3087  *d = descr;
3088}
3089
3090/* Return the size of a location descriptor.  */
3091
3092static unsigned long
3093size_of_loc_descr (dw_loc_descr_ref loc)
3094{
3095  unsigned long size = 1;
3096
3097  switch (loc->dw_loc_opc)
3098    {
3099    case DW_OP_addr:
3100    case INTERNAL_DW_OP_tls_addr:
3101      size += DWARF2_ADDR_SIZE;
3102      break;
3103    case DW_OP_const1u:
3104    case DW_OP_const1s:
3105      size += 1;
3106      break;
3107    case DW_OP_const2u:
3108    case DW_OP_const2s:
3109      size += 2;
3110      break;
3111    case DW_OP_const4u:
3112    case DW_OP_const4s:
3113      size += 4;
3114      break;
3115    case DW_OP_const8u:
3116    case DW_OP_const8s:
3117      size += 8;
3118      break;
3119    case DW_OP_constu:
3120      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3121      break;
3122    case DW_OP_consts:
3123      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3124      break;
3125    case DW_OP_pick:
3126      size += 1;
3127      break;
3128    case DW_OP_plus_uconst:
3129      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3130      break;
3131    case DW_OP_skip:
3132    case DW_OP_bra:
3133      size += 2;
3134      break;
3135    case DW_OP_breg0:
3136    case DW_OP_breg1:
3137    case DW_OP_breg2:
3138    case DW_OP_breg3:
3139    case DW_OP_breg4:
3140    case DW_OP_breg5:
3141    case DW_OP_breg6:
3142    case DW_OP_breg7:
3143    case DW_OP_breg8:
3144    case DW_OP_breg9:
3145    case DW_OP_breg10:
3146    case DW_OP_breg11:
3147    case DW_OP_breg12:
3148    case DW_OP_breg13:
3149    case DW_OP_breg14:
3150    case DW_OP_breg15:
3151    case DW_OP_breg16:
3152    case DW_OP_breg17:
3153    case DW_OP_breg18:
3154    case DW_OP_breg19:
3155    case DW_OP_breg20:
3156    case DW_OP_breg21:
3157    case DW_OP_breg22:
3158    case DW_OP_breg23:
3159    case DW_OP_breg24:
3160    case DW_OP_breg25:
3161    case DW_OP_breg26:
3162    case DW_OP_breg27:
3163    case DW_OP_breg28:
3164    case DW_OP_breg29:
3165    case DW_OP_breg30:
3166    case DW_OP_breg31:
3167      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3168      break;
3169    case DW_OP_regx:
3170      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3171      break;
3172    case DW_OP_fbreg:
3173      size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3174      break;
3175    case DW_OP_bregx:
3176      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3177      size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3178      break;
3179    case DW_OP_piece:
3180      size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3181      break;
3182    case DW_OP_deref_size:
3183    case DW_OP_xderef_size:
3184      size += 1;
3185      break;
3186    case DW_OP_call2:
3187      size += 2;
3188      break;
3189    case DW_OP_call4:
3190      size += 4;
3191      break;
3192    case DW_OP_call_ref:
3193      size += DWARF2_ADDR_SIZE;
3194      break;
3195    default:
3196      break;
3197    }
3198
3199  return size;
3200}
3201
3202/* Return the size of a series of location descriptors.  */
3203
3204static unsigned long
3205size_of_locs (dw_loc_descr_ref loc)
3206{
3207  unsigned long size;
3208
3209  for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3210    {
3211      loc->dw_loc_addr = size;
3212      size += size_of_loc_descr (loc);
3213    }
3214
3215  return size;
3216}
3217
3218/* Output location description stack opcode's operands (if any).  */
3219
3220static void
3221output_loc_operands (dw_loc_descr_ref loc)
3222{
3223  dw_val_ref val1 = &loc->dw_loc_oprnd1;
3224  dw_val_ref val2 = &loc->dw_loc_oprnd2;
3225
3226  switch (loc->dw_loc_opc)
3227    {
3228#ifdef DWARF2_DEBUGGING_INFO
3229    case DW_OP_addr:
3230      dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3231      break;
3232    case DW_OP_const2u:
3233    case DW_OP_const2s:
3234      dw2_asm_output_data (2, val1->v.val_int, NULL);
3235      break;
3236    case DW_OP_const4u:
3237    case DW_OP_const4s:
3238      dw2_asm_output_data (4, val1->v.val_int, NULL);
3239      break;
3240    case DW_OP_const8u:
3241    case DW_OP_const8s:
3242      gcc_assert (HOST_BITS_PER_LONG >= 64);
3243      dw2_asm_output_data (8, val1->v.val_int, NULL);
3244      break;
3245    case DW_OP_skip:
3246    case DW_OP_bra:
3247      {
3248	int offset;
3249
3250	gcc_assert (val1->val_class == dw_val_class_loc);
3251	offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3252
3253	dw2_asm_output_data (2, offset, NULL);
3254      }
3255      break;
3256#else
3257    case DW_OP_addr:
3258    case DW_OP_const2u:
3259    case DW_OP_const2s:
3260    case DW_OP_const4u:
3261    case DW_OP_const4s:
3262    case DW_OP_const8u:
3263    case DW_OP_const8s:
3264    case DW_OP_skip:
3265    case DW_OP_bra:
3266      /* We currently don't make any attempt to make sure these are
3267	 aligned properly like we do for the main unwind info, so
3268	 don't support emitting things larger than a byte if we're
3269	 only doing unwinding.  */
3270      gcc_unreachable ();
3271#endif
3272    case DW_OP_const1u:
3273    case DW_OP_const1s:
3274      dw2_asm_output_data (1, val1->v.val_int, NULL);
3275      break;
3276    case DW_OP_constu:
3277      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3278      break;
3279    case DW_OP_consts:
3280      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3281      break;
3282    case DW_OP_pick:
3283      dw2_asm_output_data (1, val1->v.val_int, NULL);
3284      break;
3285    case DW_OP_plus_uconst:
3286      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3287      break;
3288    case DW_OP_breg0:
3289    case DW_OP_breg1:
3290    case DW_OP_breg2:
3291    case DW_OP_breg3:
3292    case DW_OP_breg4:
3293    case DW_OP_breg5:
3294    case DW_OP_breg6:
3295    case DW_OP_breg7:
3296    case DW_OP_breg8:
3297    case DW_OP_breg9:
3298    case DW_OP_breg10:
3299    case DW_OP_breg11:
3300    case DW_OP_breg12:
3301    case DW_OP_breg13:
3302    case DW_OP_breg14:
3303    case DW_OP_breg15:
3304    case DW_OP_breg16:
3305    case DW_OP_breg17:
3306    case DW_OP_breg18:
3307    case DW_OP_breg19:
3308    case DW_OP_breg20:
3309    case DW_OP_breg21:
3310    case DW_OP_breg22:
3311    case DW_OP_breg23:
3312    case DW_OP_breg24:
3313    case DW_OP_breg25:
3314    case DW_OP_breg26:
3315    case DW_OP_breg27:
3316    case DW_OP_breg28:
3317    case DW_OP_breg29:
3318    case DW_OP_breg30:
3319    case DW_OP_breg31:
3320      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3321      break;
3322    case DW_OP_regx:
3323      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3324      break;
3325    case DW_OP_fbreg:
3326      dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3327      break;
3328    case DW_OP_bregx:
3329      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3330      dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3331      break;
3332    case DW_OP_piece:
3333      dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3334      break;
3335    case DW_OP_deref_size:
3336    case DW_OP_xderef_size:
3337      dw2_asm_output_data (1, val1->v.val_int, NULL);
3338      break;
3339
3340    case INTERNAL_DW_OP_tls_addr:
3341      if (targetm.asm_out.output_dwarf_dtprel)
3342	{
3343	  targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3344					       DWARF2_ADDR_SIZE,
3345					       val1->v.val_addr);
3346	  fputc ('\n', asm_out_file);
3347	}
3348      else
3349	gcc_unreachable ();
3350      break;
3351
3352    default:
3353      /* Other codes have no operands.  */
3354      break;
3355    }
3356}
3357
3358/* Output a sequence of location operations.  */
3359
3360static void
3361output_loc_sequence (dw_loc_descr_ref loc)
3362{
3363  for (; loc != NULL; loc = loc->dw_loc_next)
3364    {
3365      /* Output the opcode.  */
3366      dw2_asm_output_data (1, loc->dw_loc_opc,
3367			   "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3368
3369      /* Output the operand(s) (if any).  */
3370      output_loc_operands (loc);
3371    }
3372}
3373
3374/* This routine will generate the correct assembly data for a location
3375   description based on a cfi entry with a complex address.  */
3376
3377static void
3378output_cfa_loc (dw_cfi_ref cfi)
3379{
3380  dw_loc_descr_ref loc;
3381  unsigned long size;
3382
3383  /* Output the size of the block.  */
3384  loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3385  size = size_of_locs (loc);
3386  dw2_asm_output_data_uleb128 (size, NULL);
3387
3388  /* Now output the operations themselves.  */
3389  output_loc_sequence (loc);
3390}
3391
3392/* This function builds a dwarf location descriptor sequence from
3393   a dw_cfa_location.  */
3394
3395static struct dw_loc_descr_struct *
3396build_cfa_loc (dw_cfa_location *cfa)
3397{
3398  struct dw_loc_descr_struct *head, *tmp;
3399
3400  if (cfa->indirect)
3401    {
3402      if (cfa->base_offset)
3403	{
3404	  if (cfa->reg <= 31)
3405	    head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3406	  else
3407	    head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3408	}
3409      else if (cfa->reg <= 31)
3410	head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3411      else
3412	head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3413
3414      head->dw_loc_oprnd1.val_class = dw_val_class_const;
3415      tmp = new_loc_descr (DW_OP_deref, 0, 0);
3416      add_loc_descr (&head, tmp);
3417      if (cfa->offset != 0)
3418	{
3419	  tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3420	  add_loc_descr (&head, tmp);
3421	}
3422    }
3423  else
3424    {
3425      if (cfa->offset == 0)
3426	if (cfa->reg <= 31)
3427	  head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3428	else
3429	  head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3430      else if (cfa->reg <= 31)
3431	head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->offset, 0);
3432      else
3433	head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->offset);
3434    }
3435
3436  return head;
3437}
3438
3439/* This function fills in aa dw_cfa_location structure from a dwarf location
3440   descriptor sequence.  */
3441
3442static void
3443get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3444{
3445  struct dw_loc_descr_struct *ptr;
3446  cfa->offset = 0;
3447  cfa->base_offset = 0;
3448  cfa->indirect = 0;
3449  cfa->reg = -1;
3450
3451  for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3452    {
3453      enum dwarf_location_atom op = ptr->dw_loc_opc;
3454
3455      switch (op)
3456	{
3457	case DW_OP_reg0:
3458	case DW_OP_reg1:
3459	case DW_OP_reg2:
3460	case DW_OP_reg3:
3461	case DW_OP_reg4:
3462	case DW_OP_reg5:
3463	case DW_OP_reg6:
3464	case DW_OP_reg7:
3465	case DW_OP_reg8:
3466	case DW_OP_reg9:
3467	case DW_OP_reg10:
3468	case DW_OP_reg11:
3469	case DW_OP_reg12:
3470	case DW_OP_reg13:
3471	case DW_OP_reg14:
3472	case DW_OP_reg15:
3473	case DW_OP_reg16:
3474	case DW_OP_reg17:
3475	case DW_OP_reg18:
3476	case DW_OP_reg19:
3477	case DW_OP_reg20:
3478	case DW_OP_reg21:
3479	case DW_OP_reg22:
3480	case DW_OP_reg23:
3481	case DW_OP_reg24:
3482	case DW_OP_reg25:
3483	case DW_OP_reg26:
3484	case DW_OP_reg27:
3485	case DW_OP_reg28:
3486	case DW_OP_reg29:
3487	case DW_OP_reg30:
3488	case DW_OP_reg31:
3489	  cfa->reg = op - DW_OP_reg0;
3490	  break;
3491	case DW_OP_regx:
3492	  cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3493	  break;
3494	case DW_OP_breg0:
3495	case DW_OP_breg1:
3496	case DW_OP_breg2:
3497	case DW_OP_breg3:
3498	case DW_OP_breg4:
3499	case DW_OP_breg5:
3500	case DW_OP_breg6:
3501	case DW_OP_breg7:
3502	case DW_OP_breg8:
3503	case DW_OP_breg9:
3504	case DW_OP_breg10:
3505	case DW_OP_breg11:
3506	case DW_OP_breg12:
3507	case DW_OP_breg13:
3508	case DW_OP_breg14:
3509	case DW_OP_breg15:
3510	case DW_OP_breg16:
3511	case DW_OP_breg17:
3512	case DW_OP_breg18:
3513	case DW_OP_breg19:
3514	case DW_OP_breg20:
3515	case DW_OP_breg21:
3516	case DW_OP_breg22:
3517	case DW_OP_breg23:
3518	case DW_OP_breg24:
3519	case DW_OP_breg25:
3520	case DW_OP_breg26:
3521	case DW_OP_breg27:
3522	case DW_OP_breg28:
3523	case DW_OP_breg29:
3524	case DW_OP_breg30:
3525	case DW_OP_breg31:
3526	  cfa->reg = op - DW_OP_breg0;
3527	  cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3528	  break;
3529	case DW_OP_bregx:
3530	  cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3531	  cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3532	  break;
3533	case DW_OP_deref:
3534	  cfa->indirect = 1;
3535	  break;
3536	case DW_OP_plus_uconst:
3537	  cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3538	  break;
3539	default:
3540	  internal_error ("DW_LOC_OP %s not implemented",
3541			  dwarf_stack_op_name (ptr->dw_loc_opc));
3542	}
3543    }
3544}
3545#endif /* .debug_frame support */
3546
3547/* And now, the support for symbolic debugging information.  */
3548#ifdef DWARF2_DEBUGGING_INFO
3549
3550/* .debug_str support.  */
3551static int output_indirect_string (void **, void *);
3552
3553static void dwarf2out_init (const char *);
3554static void dwarf2out_finish (const char *);
3555static void dwarf2out_define (unsigned int, const char *);
3556static void dwarf2out_undef (unsigned int, const char *);
3557static void dwarf2out_start_source_file (unsigned, const char *);
3558static void dwarf2out_end_source_file (unsigned);
3559static void dwarf2out_begin_block (unsigned, unsigned);
3560static void dwarf2out_end_block (unsigned, unsigned);
3561static bool dwarf2out_ignore_block (tree);
3562static void dwarf2out_global_decl (tree);
3563static void dwarf2out_type_decl (tree, int);
3564static void dwarf2out_imported_module_or_decl (tree, tree);
3565static void dwarf2out_abstract_function (tree);
3566static void dwarf2out_var_location (rtx);
3567static void dwarf2out_begin_function (tree);
3568
3569/* The debug hooks structure.  */
3570
3571const struct gcc_debug_hooks dwarf2_debug_hooks =
3572{
3573  dwarf2out_init,
3574  dwarf2out_finish,
3575  dwarf2out_define,
3576  dwarf2out_undef,
3577  dwarf2out_start_source_file,
3578  dwarf2out_end_source_file,
3579  dwarf2out_begin_block,
3580  dwarf2out_end_block,
3581  dwarf2out_ignore_block,
3582  dwarf2out_source_line,
3583  dwarf2out_begin_prologue,
3584  debug_nothing_int_charstar,	/* end_prologue */
3585  dwarf2out_end_epilogue,
3586  dwarf2out_begin_function,
3587  debug_nothing_int,		/* end_function */
3588  dwarf2out_decl,		/* function_decl */
3589  dwarf2out_global_decl,
3590  dwarf2out_type_decl,		/* type_decl */
3591  dwarf2out_imported_module_or_decl,
3592  debug_nothing_tree,		/* deferred_inline_function */
3593  /* The DWARF 2 backend tries to reduce debugging bloat by not
3594     emitting the abstract description of inline functions until
3595     something tries to reference them.  */
3596  dwarf2out_abstract_function,	/* outlining_inline_function */
3597  debug_nothing_rtx,		/* label */
3598  debug_nothing_int,		/* handle_pch */
3599  dwarf2out_var_location,
3600  dwarf2out_switch_text_section,
3601  1                             /* start_end_main_source_file */
3602};
3603#endif
3604
3605/* NOTE: In the comments in this file, many references are made to
3606   "Debugging Information Entries".  This term is abbreviated as `DIE'
3607   throughout the remainder of this file.  */
3608
3609/* An internal representation of the DWARF output is built, and then
3610   walked to generate the DWARF debugging info.  The walk of the internal
3611   representation is done after the entire program has been compiled.
3612   The types below are used to describe the internal representation.  */
3613
3614/* Various DIE's use offsets relative to the beginning of the
3615   .debug_info section to refer to each other.  */
3616
3617typedef long int dw_offset;
3618
3619/* Define typedefs here to avoid circular dependencies.  */
3620
3621typedef struct dw_attr_struct *dw_attr_ref;
3622typedef struct dw_line_info_struct *dw_line_info_ref;
3623typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3624typedef struct pubname_struct *pubname_ref;
3625typedef struct dw_ranges_struct *dw_ranges_ref;
3626
3627/* Each entry in the line_info_table maintains the file and
3628   line number associated with the label generated for that
3629   entry.  The label gives the PC value associated with
3630   the line number entry.  */
3631
3632typedef struct dw_line_info_struct GTY(())
3633{
3634  unsigned long dw_file_num;
3635  unsigned long dw_line_num;
3636}
3637dw_line_info_entry;
3638
3639/* Line information for functions in separate sections; each one gets its
3640   own sequence.  */
3641typedef struct dw_separate_line_info_struct GTY(())
3642{
3643  unsigned long dw_file_num;
3644  unsigned long dw_line_num;
3645  unsigned long function;
3646}
3647dw_separate_line_info_entry;
3648
3649/* Each DIE attribute has a field specifying the attribute kind,
3650   a link to the next attribute in the chain, and an attribute value.
3651   Attributes are typically linked below the DIE they modify.  */
3652
3653typedef struct dw_attr_struct GTY(())
3654{
3655  enum dwarf_attribute dw_attr;
3656  dw_attr_ref dw_attr_next;
3657  dw_val_node dw_attr_val;
3658}
3659dw_attr_node;
3660
3661/* The Debugging Information Entry (DIE) structure */
3662
3663typedef struct die_struct GTY(())
3664{
3665  enum dwarf_tag die_tag;
3666  char *die_symbol;
3667  dw_attr_ref die_attr;
3668  dw_die_ref die_parent;
3669  dw_die_ref die_child;
3670  dw_die_ref die_sib;
3671  dw_die_ref die_definition; /* ref from a specification to its definition */
3672  dw_offset die_offset;
3673  unsigned long die_abbrev;
3674  int die_mark;
3675  unsigned int decl_id;
3676}
3677die_node;
3678
3679/* The pubname structure */
3680
3681typedef struct pubname_struct GTY(())
3682{
3683  dw_die_ref die;
3684  char *name;
3685}
3686pubname_entry;
3687
3688struct dw_ranges_struct GTY(())
3689{
3690  int block_num;
3691};
3692
3693/* The limbo die list structure.  */
3694typedef struct limbo_die_struct GTY(())
3695{
3696  dw_die_ref die;
3697  tree created_for;
3698  struct limbo_die_struct *next;
3699}
3700limbo_die_node;
3701
3702/* How to start an assembler comment.  */
3703#ifndef ASM_COMMENT_START
3704#define ASM_COMMENT_START ";#"
3705#endif
3706
3707/* Define a macro which returns nonzero for a TYPE_DECL which was
3708   implicitly generated for a tagged type.
3709
3710   Note that unlike the gcc front end (which generates a NULL named
3711   TYPE_DECL node for each complete tagged type, each array type, and
3712   each function type node created) the g++ front end generates a
3713   _named_ TYPE_DECL node for each tagged type node created.
3714   These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3715   generate a DW_TAG_typedef DIE for them.  */
3716
3717#define TYPE_DECL_IS_STUB(decl)				\
3718  (DECL_NAME (decl) == NULL_TREE			\
3719   || (DECL_ARTIFICIAL (decl)				\
3720       && is_tagged_type (TREE_TYPE (decl))		\
3721       && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))	\
3722	   /* This is necessary for stub decls that	\
3723	      appear in nested inline functions.  */	\
3724	   || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE	\
3725	       && (decl_ultimate_origin (decl)		\
3726		   == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3727
3728/* Information concerning the compilation unit's programming
3729   language, and compiler version.  */
3730
3731/* Fixed size portion of the DWARF compilation unit header.  */
3732#define DWARF_COMPILE_UNIT_HEADER_SIZE \
3733  (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3734
3735/* Fixed size portion of public names info.  */
3736#define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3737
3738/* Fixed size portion of the address range info.  */
3739#define DWARF_ARANGES_HEADER_SIZE					\
3740  (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,	\
3741                DWARF2_ADDR_SIZE * 2)					\
3742   - DWARF_INITIAL_LENGTH_SIZE)
3743
3744/* Size of padding portion in the address range info.  It must be
3745   aligned to twice the pointer size.  */
3746#define DWARF_ARANGES_PAD_SIZE \
3747  (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3748                DWARF2_ADDR_SIZE * 2) \
3749   - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3750
3751/* Use assembler line directives if available.  */
3752#ifndef DWARF2_ASM_LINE_DEBUG_INFO
3753#ifdef HAVE_AS_DWARF2_DEBUG_LINE
3754#define DWARF2_ASM_LINE_DEBUG_INFO 1
3755#else
3756#define DWARF2_ASM_LINE_DEBUG_INFO 0
3757#endif
3758#endif
3759
3760/* Minimum line offset in a special line info. opcode.
3761   This value was chosen to give a reasonable range of values.  */
3762#define DWARF_LINE_BASE  -10
3763
3764/* First special line opcode - leave room for the standard opcodes.  */
3765#define DWARF_LINE_OPCODE_BASE  10
3766
3767/* Range of line offsets in a special line info. opcode.  */
3768#define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3769
3770/* Flag that indicates the initial value of the is_stmt_start flag.
3771   In the present implementation, we do not mark any lines as
3772   the beginning of a source statement, because that information
3773   is not made available by the GCC front-end.  */
3774#define	DWARF_LINE_DEFAULT_IS_STMT_START 1
3775
3776#ifdef DWARF2_DEBUGGING_INFO
3777/* This location is used by calc_die_sizes() to keep track
3778   the offset of each DIE within the .debug_info section.  */
3779static unsigned long next_die_offset;
3780#endif
3781
3782/* Record the root of the DIE's built for the current compilation unit.  */
3783static GTY(()) dw_die_ref comp_unit_die;
3784
3785/* A list of DIEs with a NULL parent waiting to be relocated.  */
3786static GTY(()) limbo_die_node *limbo_die_list;
3787
3788/* Filenames referenced by this compilation unit.  */
3789static GTY(()) varray_type file_table;
3790static GTY(()) varray_type file_table_emitted;
3791static GTY(()) size_t file_table_last_lookup_index;
3792
3793/* A hash table of references to DIE's that describe declarations.
3794   The key is a DECL_UID() which is a unique number identifying each decl.  */
3795static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3796
3797/* Node of the variable location list.  */
3798struct var_loc_node GTY ((chain_next ("%h.next")))
3799{
3800  rtx GTY (()) var_loc_note;
3801  const char * GTY (()) label;
3802  const char * GTY (()) section_label;
3803  struct var_loc_node * GTY (()) next;
3804};
3805
3806/* Variable location list.  */
3807struct var_loc_list_def GTY (())
3808{
3809  struct var_loc_node * GTY (()) first;
3810
3811  /* Do not mark the last element of the chained list because
3812     it is marked through the chain.  */
3813  struct var_loc_node * GTY ((skip ("%h"))) last;
3814
3815  /* DECL_UID of the variable decl.  */
3816  unsigned int decl_id;
3817};
3818typedef struct var_loc_list_def var_loc_list;
3819
3820
3821/* Table of decl location linked lists.  */
3822static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3823
3824/* A pointer to the base of a list of references to DIE's that
3825   are uniquely identified by their tag, presence/absence of
3826   children DIE's, and list of attribute/value pairs.  */
3827static GTY((length ("abbrev_die_table_allocated")))
3828  dw_die_ref *abbrev_die_table;
3829
3830/* Number of elements currently allocated for abbrev_die_table.  */
3831static GTY(()) unsigned abbrev_die_table_allocated;
3832
3833/* Number of elements in type_die_table currently in use.  */
3834static GTY(()) unsigned abbrev_die_table_in_use;
3835
3836/* Size (in elements) of increments by which we may expand the
3837   abbrev_die_table.  */
3838#define ABBREV_DIE_TABLE_INCREMENT 256
3839
3840/* A pointer to the base of a table that contains line information
3841   for each source code line in .text in the compilation unit.  */
3842static GTY((length ("line_info_table_allocated")))
3843     dw_line_info_ref line_info_table;
3844
3845/* Number of elements currently allocated for line_info_table.  */
3846static GTY(()) unsigned line_info_table_allocated;
3847
3848/* Number of elements in line_info_table currently in use.  */
3849static GTY(()) unsigned line_info_table_in_use;
3850
3851/* True if the compilation unit contains more than one .text section.  */
3852static GTY(()) bool have_switched_text_section = false;
3853
3854/* A pointer to the base of a table that contains line information
3855   for each source code line outside of .text in the compilation unit.  */
3856static GTY ((length ("separate_line_info_table_allocated")))
3857     dw_separate_line_info_ref separate_line_info_table;
3858
3859/* Number of elements currently allocated for separate_line_info_table.  */
3860static GTY(()) unsigned separate_line_info_table_allocated;
3861
3862/* Number of elements in separate_line_info_table currently in use.  */
3863static GTY(()) unsigned separate_line_info_table_in_use;
3864
3865/* Size (in elements) of increments by which we may expand the
3866   line_info_table.  */
3867#define LINE_INFO_TABLE_INCREMENT 1024
3868
3869/* A pointer to the base of a table that contains a list of publicly
3870   accessible names.  */
3871static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3872
3873/* Number of elements currently allocated for pubname_table.  */
3874static GTY(()) unsigned pubname_table_allocated;
3875
3876/* Number of elements in pubname_table currently in use.  */
3877static GTY(()) unsigned pubname_table_in_use;
3878
3879/* Size (in elements) of increments by which we may expand the
3880   pubname_table.  */
3881#define PUBNAME_TABLE_INCREMENT 64
3882
3883/* Array of dies for which we should generate .debug_arange info.  */
3884static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3885
3886/* Number of elements currently allocated for arange_table.  */
3887static GTY(()) unsigned arange_table_allocated;
3888
3889/* Number of elements in arange_table currently in use.  */
3890static GTY(()) unsigned arange_table_in_use;
3891
3892/* Size (in elements) of increments by which we may expand the
3893   arange_table.  */
3894#define ARANGE_TABLE_INCREMENT 64
3895
3896/* Array of dies for which we should generate .debug_ranges info.  */
3897static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3898
3899/* Number of elements currently allocated for ranges_table.  */
3900static GTY(()) unsigned ranges_table_allocated;
3901
3902/* Number of elements in ranges_table currently in use.  */
3903static GTY(()) unsigned ranges_table_in_use;
3904
3905/* Size (in elements) of increments by which we may expand the
3906   ranges_table.  */
3907#define RANGES_TABLE_INCREMENT 64
3908
3909/* Whether we have location lists that need outputting */
3910static GTY(()) unsigned have_location_lists;
3911
3912/* Unique label counter.  */
3913static GTY(()) unsigned int loclabel_num;
3914
3915#ifdef DWARF2_DEBUGGING_INFO
3916/* Record whether the function being analyzed contains inlined functions.  */
3917static int current_function_has_inlines;
3918#endif
3919#if 0 && defined (MIPS_DEBUGGING_INFO)
3920static int comp_unit_has_inlines;
3921#endif
3922
3923/* Number of file tables emitted in maybe_emit_file().  */
3924static GTY(()) int emitcount = 0;
3925
3926/* Number of internal labels generated by gen_internal_sym().  */
3927static GTY(()) int label_num;
3928
3929#if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3930void
3931dwarf2out_switch_text_section (void)
3932{
3933  dw_fde_ref fde;
3934
3935  gcc_assert (cfun);
3936
3937  fde = &fde_table[fde_table_in_use - 1];
3938  fde->dw_fde_switched_sections = true;
3939  fde->dw_fde_hot_section_label = cfun->hot_section_label;
3940  fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
3941  fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
3942  fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
3943  have_switched_text_section = true;
3944
3945  /* Reset the current label on switching text sections, so that we
3946     don't attempt to advance_loc4 between labels in different sections.  */
3947  fde->dw_fde_current_label = NULL;
3948}
3949
3950#endif
3951
3952#ifdef DWARF2_DEBUGGING_INFO
3953
3954/* Offset from the "steady-state frame pointer" to the CFA,
3955   within the current function.  */
3956static HOST_WIDE_INT frame_pointer_cfa_offset;
3957
3958/* Forward declarations for functions defined in this file.  */
3959
3960static int is_pseudo_reg (rtx);
3961static tree type_main_variant (tree);
3962static int is_tagged_type (tree);
3963static const char *dwarf_tag_name (unsigned);
3964static const char *dwarf_attr_name (unsigned);
3965static const char *dwarf_form_name (unsigned);
3966static tree decl_ultimate_origin (tree);
3967static tree block_ultimate_origin (tree);
3968static tree decl_class_context (tree);
3969static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3970static inline enum dw_val_class AT_class (dw_attr_ref);
3971static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3972static inline unsigned AT_flag (dw_attr_ref);
3973static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3974static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3975static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3976static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3977static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3978			      unsigned long);
3979static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3980			       unsigned int, unsigned char *);
3981static hashval_t debug_str_do_hash (const void *);
3982static int debug_str_eq (const void *, const void *);
3983static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3984static inline const char *AT_string (dw_attr_ref);
3985static int AT_string_form (dw_attr_ref);
3986static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3987static void add_AT_specification (dw_die_ref, dw_die_ref);
3988static inline dw_die_ref AT_ref (dw_attr_ref);
3989static inline int AT_ref_external (dw_attr_ref);
3990static inline void set_AT_ref_external (dw_attr_ref, int);
3991static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3992static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3993static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3994static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3995			     dw_loc_list_ref);
3996static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3997static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3998static inline rtx AT_addr (dw_attr_ref);
3999static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4000static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
4001static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4002			   unsigned HOST_WIDE_INT);
4003static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4004			       unsigned long);
4005static inline const char *AT_lbl (dw_attr_ref);
4006static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4007static const char *get_AT_low_pc (dw_die_ref);
4008static const char *get_AT_hi_pc (dw_die_ref);
4009static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4010static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4011static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4012static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4013static bool is_c_family (void);
4014static bool is_cxx (void);
4015static bool is_java (void);
4016static bool is_fortran (void);
4017static bool is_ada (void);
4018static void remove_AT (dw_die_ref, enum dwarf_attribute);
4019static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4020static inline void free_die (dw_die_ref);
4021static void remove_children (dw_die_ref);
4022static void add_child_die (dw_die_ref, dw_die_ref);
4023static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4024static dw_die_ref lookup_type_die (tree);
4025static void equate_type_number_to_die (tree, dw_die_ref);
4026static hashval_t decl_die_table_hash (const void *);
4027static int decl_die_table_eq (const void *, const void *);
4028static dw_die_ref lookup_decl_die (tree);
4029static hashval_t decl_loc_table_hash (const void *);
4030static int decl_loc_table_eq (const void *, const void *);
4031static var_loc_list *lookup_decl_loc (tree);
4032static void equate_decl_number_to_die (tree, dw_die_ref);
4033static void add_var_loc_to_decl (tree, struct var_loc_node *);
4034static void print_spaces (FILE *);
4035static void print_die (dw_die_ref, FILE *);
4036static void print_dwarf_line_table (FILE *);
4037static void reverse_die_lists (dw_die_ref);
4038static void reverse_all_dies (dw_die_ref);
4039static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4040static dw_die_ref pop_compile_unit (dw_die_ref);
4041static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4042static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4043static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4044static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4045static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4046static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4047static int same_die_p (dw_die_ref, dw_die_ref, int *);
4048static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4049static void compute_section_prefix (dw_die_ref);
4050static int is_type_die (dw_die_ref);
4051static int is_comdat_die (dw_die_ref);
4052static int is_symbol_die (dw_die_ref);
4053static void assign_symbol_names (dw_die_ref);
4054static void break_out_includes (dw_die_ref);
4055static hashval_t htab_cu_hash (const void *);
4056static int htab_cu_eq (const void *, const void *);
4057static void htab_cu_del (void *);
4058static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4059static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4060static void add_sibling_attributes (dw_die_ref);
4061static void build_abbrev_table (dw_die_ref);
4062static void output_location_lists (dw_die_ref);
4063static int constant_size (long unsigned);
4064static unsigned long size_of_die (dw_die_ref);
4065static void calc_die_sizes (dw_die_ref);
4066static void mark_dies (dw_die_ref);
4067static void unmark_dies (dw_die_ref);
4068static void unmark_all_dies (dw_die_ref);
4069static unsigned long size_of_pubnames (void);
4070static unsigned long size_of_aranges (void);
4071static enum dwarf_form value_format (dw_attr_ref);
4072static void output_value_format (dw_attr_ref);
4073static void output_abbrev_section (void);
4074static void output_die_symbol (dw_die_ref);
4075static void output_die (dw_die_ref);
4076static void output_compilation_unit_header (void);
4077static void output_comp_unit (dw_die_ref, int);
4078static const char *dwarf2_name (tree, int);
4079static void add_pubname (tree, dw_die_ref);
4080static void output_pubnames (void);
4081static void add_arange (tree, dw_die_ref);
4082static void output_aranges (void);
4083static unsigned int add_ranges (tree);
4084static void output_ranges (void);
4085static void output_line_info (void);
4086static void output_file_names (void);
4087static dw_die_ref base_type_die (tree);
4088static tree root_type (tree);
4089static int is_base_type (tree);
4090static bool is_subrange_type (tree);
4091static dw_die_ref subrange_type_die (tree, dw_die_ref);
4092static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4093static int type_is_enum (tree);
4094static unsigned int dbx_reg_number (rtx);
4095static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4096static dw_loc_descr_ref reg_loc_descriptor (rtx);
4097static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4098static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4099static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4100static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4101static int is_based_loc (rtx);
4102static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4103static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4104static dw_loc_descr_ref loc_descriptor (rtx);
4105static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4106static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4107static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4108static tree field_type (tree);
4109static unsigned int simple_type_align_in_bits (tree);
4110static unsigned int simple_decl_align_in_bits (tree);
4111static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4112static HOST_WIDE_INT field_byte_offset (tree);
4113static void add_AT_location_description	(dw_die_ref, enum dwarf_attribute,
4114					 dw_loc_descr_ref);
4115static void add_data_member_location_attribute (dw_die_ref, tree);
4116static void add_const_value_attribute (dw_die_ref, rtx);
4117static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4118static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4119static void insert_float (rtx, unsigned char *);
4120static rtx rtl_for_decl_location (tree);
4121static void add_location_or_const_value_attribute (dw_die_ref, tree,
4122						   enum dwarf_attribute);
4123static void tree_add_const_value_attribute (dw_die_ref, tree);
4124static void add_name_attribute (dw_die_ref, const char *);
4125static void add_comp_dir_attribute (dw_die_ref);
4126static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4127static void add_subscript_info (dw_die_ref, tree);
4128static void add_byte_size_attribute (dw_die_ref, tree);
4129static void add_bit_offset_attribute (dw_die_ref, tree);
4130static void add_bit_size_attribute (dw_die_ref, tree);
4131static void add_prototyped_attribute (dw_die_ref, tree);
4132static void add_abstract_origin_attribute (dw_die_ref, tree);
4133static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4134static void add_src_coords_attributes (dw_die_ref, tree);
4135static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4136static void push_decl_scope (tree);
4137static void pop_decl_scope (void);
4138static dw_die_ref scope_die_for (tree, dw_die_ref);
4139static inline int local_scope_p (dw_die_ref);
4140static inline int class_or_namespace_scope_p (dw_die_ref);
4141static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4142static void add_calling_convention_attribute (dw_die_ref, tree);
4143static const char *type_tag (tree);
4144static tree member_declared_type (tree);
4145#if 0
4146static const char *decl_start_label (tree);
4147#endif
4148static void gen_array_type_die (tree, dw_die_ref);
4149#if 0
4150static void gen_entry_point_die (tree, dw_die_ref);
4151#endif
4152static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4153static void gen_inlined_structure_type_die (tree, dw_die_ref);
4154static void gen_inlined_union_type_die (tree, dw_die_ref);
4155static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4156static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4157static void gen_unspecified_parameters_die (tree, dw_die_ref);
4158static void gen_formal_types_die (tree, dw_die_ref);
4159static void gen_subprogram_die (tree, dw_die_ref);
4160static void gen_variable_die (tree, dw_die_ref);
4161static void gen_label_die (tree, dw_die_ref);
4162static void gen_lexical_block_die (tree, dw_die_ref, int);
4163static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4164static void gen_field_die (tree, dw_die_ref);
4165static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4166static dw_die_ref gen_compile_unit_die (const char *);
4167static void gen_string_type_die (tree, dw_die_ref);
4168static void gen_inheritance_die (tree, tree, dw_die_ref);
4169static void gen_member_die (tree, dw_die_ref);
4170static void gen_struct_or_union_type_die (tree, dw_die_ref);
4171static void gen_subroutine_type_die (tree, dw_die_ref);
4172static void gen_typedef_die (tree, dw_die_ref);
4173static void gen_type_die (tree, dw_die_ref);
4174static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4175static void gen_block_die (tree, dw_die_ref, int);
4176static void decls_for_scope (tree, dw_die_ref, int);
4177static int is_redundant_typedef (tree);
4178static void gen_namespace_die (tree);
4179static void gen_decl_die (tree, dw_die_ref);
4180static dw_die_ref force_decl_die (tree);
4181static dw_die_ref force_type_die (tree);
4182static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4183static void declare_in_namespace (tree, dw_die_ref);
4184static unsigned lookup_filename (const char *);
4185static void init_file_table (void);
4186static void retry_incomplete_types (void);
4187static void gen_type_die_for_member (tree, tree, dw_die_ref);
4188static void splice_child_die (dw_die_ref, dw_die_ref);
4189static int file_info_cmp (const void *, const void *);
4190static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4191				     const char *, const char *, unsigned);
4192static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4193				       const char *, const char *,
4194				       const char *);
4195static void output_loc_list (dw_loc_list_ref);
4196static char *gen_internal_sym (const char *);
4197
4198static void prune_unmark_dies (dw_die_ref);
4199static void prune_unused_types_mark (dw_die_ref, int);
4200static void prune_unused_types_walk (dw_die_ref);
4201static void prune_unused_types_walk_attribs (dw_die_ref);
4202static void prune_unused_types_prune (dw_die_ref);
4203static void prune_unused_types (void);
4204static int maybe_emit_file (int);
4205
4206/* Section names used to hold DWARF debugging information.  */
4207#ifndef DEBUG_INFO_SECTION
4208#define DEBUG_INFO_SECTION	".debug_info"
4209#endif
4210#ifndef DEBUG_ABBREV_SECTION
4211#define DEBUG_ABBREV_SECTION	".debug_abbrev"
4212#endif
4213#ifndef DEBUG_ARANGES_SECTION
4214#define DEBUG_ARANGES_SECTION	".debug_aranges"
4215#endif
4216#ifndef DEBUG_MACINFO_SECTION
4217#define DEBUG_MACINFO_SECTION	".debug_macinfo"
4218#endif
4219#ifndef DEBUG_LINE_SECTION
4220#define DEBUG_LINE_SECTION	".debug_line"
4221#endif
4222#ifndef DEBUG_LOC_SECTION
4223#define DEBUG_LOC_SECTION	".debug_loc"
4224#endif
4225#ifndef DEBUG_PUBNAMES_SECTION
4226#define DEBUG_PUBNAMES_SECTION	".debug_pubnames"
4227#endif
4228#ifndef DEBUG_STR_SECTION
4229#define DEBUG_STR_SECTION	".debug_str"
4230#endif
4231#ifndef DEBUG_RANGES_SECTION
4232#define DEBUG_RANGES_SECTION	".debug_ranges"
4233#endif
4234
4235/* Standard ELF section names for compiled code and data.  */
4236#ifndef TEXT_SECTION_NAME
4237#define TEXT_SECTION_NAME	".text"
4238#endif
4239
4240/* Section flags for .debug_str section.  */
4241#define DEBUG_STR_SECTION_FLAGS \
4242  (HAVE_GAS_SHF_MERGE && flag_merge_constants			\
4243   ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1	\
4244   : SECTION_DEBUG)
4245
4246/* Labels we insert at beginning sections we can reference instead of
4247   the section names themselves.  */
4248
4249#ifndef TEXT_SECTION_LABEL
4250#define TEXT_SECTION_LABEL		"Ltext"
4251#endif
4252#ifndef COLD_TEXT_SECTION_LABEL
4253#define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4254#endif
4255#ifndef DEBUG_LINE_SECTION_LABEL
4256#define DEBUG_LINE_SECTION_LABEL	"Ldebug_line"
4257#endif
4258#ifndef DEBUG_INFO_SECTION_LABEL
4259#define DEBUG_INFO_SECTION_LABEL	"Ldebug_info"
4260#endif
4261#ifndef DEBUG_ABBREV_SECTION_LABEL
4262#define DEBUG_ABBREV_SECTION_LABEL	"Ldebug_abbrev"
4263#endif
4264#ifndef DEBUG_LOC_SECTION_LABEL
4265#define DEBUG_LOC_SECTION_LABEL		"Ldebug_loc"
4266#endif
4267#ifndef DEBUG_RANGES_SECTION_LABEL
4268#define DEBUG_RANGES_SECTION_LABEL	"Ldebug_ranges"
4269#endif
4270#ifndef DEBUG_MACINFO_SECTION_LABEL
4271#define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4272#endif
4273
4274/* Definitions of defaults for formats and names of various special
4275   (artificial) labels which may be generated within this file (when the -g
4276   options is used and DWARF2_DEBUGGING_INFO is in effect.
4277   If necessary, these may be overridden from within the tm.h file, but
4278   typically, overriding these defaults is unnecessary.  */
4279
4280static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4281static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4282static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4283static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4284static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4285static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4286static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4287static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4288static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4289static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4290
4291#ifndef TEXT_END_LABEL
4292#define TEXT_END_LABEL		"Letext"
4293#endif
4294#ifndef COLD_END_LABEL
4295#define COLD_END_LABEL          "Letext_cold"
4296#endif
4297#ifndef BLOCK_BEGIN_LABEL
4298#define BLOCK_BEGIN_LABEL	"LBB"
4299#endif
4300#ifndef BLOCK_END_LABEL
4301#define BLOCK_END_LABEL		"LBE"
4302#endif
4303#ifndef LINE_CODE_LABEL
4304#define LINE_CODE_LABEL		"LM"
4305#endif
4306#ifndef SEPARATE_LINE_CODE_LABEL
4307#define SEPARATE_LINE_CODE_LABEL	"LSM"
4308#endif
4309
4310/* We allow a language front-end to designate a function that is to be
4311   called to "demangle" any name before it is put into a DIE.  */
4312
4313static const char *(*demangle_name_func) (const char *);
4314
4315void
4316dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4317{
4318  demangle_name_func = func;
4319}
4320
4321/* Test if rtl node points to a pseudo register.  */
4322
4323static inline int
4324is_pseudo_reg (rtx rtl)
4325{
4326  return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4327	  || (GET_CODE (rtl) == SUBREG
4328	      && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4329}
4330
4331/* Return a reference to a type, with its const and volatile qualifiers
4332   removed.  */
4333
4334static inline tree
4335type_main_variant (tree type)
4336{
4337  type = TYPE_MAIN_VARIANT (type);
4338
4339  /* ??? There really should be only one main variant among any group of
4340     variants of a given type (and all of the MAIN_VARIANT values for all
4341     members of the group should point to that one type) but sometimes the C
4342     front-end messes this up for array types, so we work around that bug
4343     here.  */
4344  if (TREE_CODE (type) == ARRAY_TYPE)
4345    while (type != TYPE_MAIN_VARIANT (type))
4346      type = TYPE_MAIN_VARIANT (type);
4347
4348  return type;
4349}
4350
4351/* Return nonzero if the given type node represents a tagged type.  */
4352
4353static inline int
4354is_tagged_type (tree type)
4355{
4356  enum tree_code code = TREE_CODE (type);
4357
4358  return (code == RECORD_TYPE || code == UNION_TYPE
4359	  || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4360}
4361
4362/* Convert a DIE tag into its string name.  */
4363
4364static const char *
4365dwarf_tag_name (unsigned int tag)
4366{
4367  switch (tag)
4368    {
4369    case DW_TAG_padding:
4370      return "DW_TAG_padding";
4371    case DW_TAG_array_type:
4372      return "DW_TAG_array_type";
4373    case DW_TAG_class_type:
4374      return "DW_TAG_class_type";
4375    case DW_TAG_entry_point:
4376      return "DW_TAG_entry_point";
4377    case DW_TAG_enumeration_type:
4378      return "DW_TAG_enumeration_type";
4379    case DW_TAG_formal_parameter:
4380      return "DW_TAG_formal_parameter";
4381    case DW_TAG_imported_declaration:
4382      return "DW_TAG_imported_declaration";
4383    case DW_TAG_label:
4384      return "DW_TAG_label";
4385    case DW_TAG_lexical_block:
4386      return "DW_TAG_lexical_block";
4387    case DW_TAG_member:
4388      return "DW_TAG_member";
4389    case DW_TAG_pointer_type:
4390      return "DW_TAG_pointer_type";
4391    case DW_TAG_reference_type:
4392      return "DW_TAG_reference_type";
4393    case DW_TAG_compile_unit:
4394      return "DW_TAG_compile_unit";
4395    case DW_TAG_string_type:
4396      return "DW_TAG_string_type";
4397    case DW_TAG_structure_type:
4398      return "DW_TAG_structure_type";
4399    case DW_TAG_subroutine_type:
4400      return "DW_TAG_subroutine_type";
4401    case DW_TAG_typedef:
4402      return "DW_TAG_typedef";
4403    case DW_TAG_union_type:
4404      return "DW_TAG_union_type";
4405    case DW_TAG_unspecified_parameters:
4406      return "DW_TAG_unspecified_parameters";
4407    case DW_TAG_variant:
4408      return "DW_TAG_variant";
4409    case DW_TAG_common_block:
4410      return "DW_TAG_common_block";
4411    case DW_TAG_common_inclusion:
4412      return "DW_TAG_common_inclusion";
4413    case DW_TAG_inheritance:
4414      return "DW_TAG_inheritance";
4415    case DW_TAG_inlined_subroutine:
4416      return "DW_TAG_inlined_subroutine";
4417    case DW_TAG_module:
4418      return "DW_TAG_module";
4419    case DW_TAG_ptr_to_member_type:
4420      return "DW_TAG_ptr_to_member_type";
4421    case DW_TAG_set_type:
4422      return "DW_TAG_set_type";
4423    case DW_TAG_subrange_type:
4424      return "DW_TAG_subrange_type";
4425    case DW_TAG_with_stmt:
4426      return "DW_TAG_with_stmt";
4427    case DW_TAG_access_declaration:
4428      return "DW_TAG_access_declaration";
4429    case DW_TAG_base_type:
4430      return "DW_TAG_base_type";
4431    case DW_TAG_catch_block:
4432      return "DW_TAG_catch_block";
4433    case DW_TAG_const_type:
4434      return "DW_TAG_const_type";
4435    case DW_TAG_constant:
4436      return "DW_TAG_constant";
4437    case DW_TAG_enumerator:
4438      return "DW_TAG_enumerator";
4439    case DW_TAG_file_type:
4440      return "DW_TAG_file_type";
4441    case DW_TAG_friend:
4442      return "DW_TAG_friend";
4443    case DW_TAG_namelist:
4444      return "DW_TAG_namelist";
4445    case DW_TAG_namelist_item:
4446      return "DW_TAG_namelist_item";
4447    case DW_TAG_namespace:
4448      return "DW_TAG_namespace";
4449    case DW_TAG_packed_type:
4450      return "DW_TAG_packed_type";
4451    case DW_TAG_subprogram:
4452      return "DW_TAG_subprogram";
4453    case DW_TAG_template_type_param:
4454      return "DW_TAG_template_type_param";
4455    case DW_TAG_template_value_param:
4456      return "DW_TAG_template_value_param";
4457    case DW_TAG_thrown_type:
4458      return "DW_TAG_thrown_type";
4459    case DW_TAG_try_block:
4460      return "DW_TAG_try_block";
4461    case DW_TAG_variant_part:
4462      return "DW_TAG_variant_part";
4463    case DW_TAG_variable:
4464      return "DW_TAG_variable";
4465    case DW_TAG_volatile_type:
4466      return "DW_TAG_volatile_type";
4467    case DW_TAG_imported_module:
4468      return "DW_TAG_imported_module";
4469    case DW_TAG_MIPS_loop:
4470      return "DW_TAG_MIPS_loop";
4471    case DW_TAG_format_label:
4472      return "DW_TAG_format_label";
4473    case DW_TAG_function_template:
4474      return "DW_TAG_function_template";
4475    case DW_TAG_class_template:
4476      return "DW_TAG_class_template";
4477    case DW_TAG_GNU_BINCL:
4478      return "DW_TAG_GNU_BINCL";
4479    case DW_TAG_GNU_EINCL:
4480      return "DW_TAG_GNU_EINCL";
4481    default:
4482      return "DW_TAG_<unknown>";
4483    }
4484}
4485
4486/* Convert a DWARF attribute code into its string name.  */
4487
4488static const char *
4489dwarf_attr_name (unsigned int attr)
4490{
4491  switch (attr)
4492    {
4493    case DW_AT_sibling:
4494      return "DW_AT_sibling";
4495    case DW_AT_location:
4496      return "DW_AT_location";
4497    case DW_AT_name:
4498      return "DW_AT_name";
4499    case DW_AT_ordering:
4500      return "DW_AT_ordering";
4501    case DW_AT_subscr_data:
4502      return "DW_AT_subscr_data";
4503    case DW_AT_byte_size:
4504      return "DW_AT_byte_size";
4505    case DW_AT_bit_offset:
4506      return "DW_AT_bit_offset";
4507    case DW_AT_bit_size:
4508      return "DW_AT_bit_size";
4509    case DW_AT_element_list:
4510      return "DW_AT_element_list";
4511    case DW_AT_stmt_list:
4512      return "DW_AT_stmt_list";
4513    case DW_AT_low_pc:
4514      return "DW_AT_low_pc";
4515    case DW_AT_high_pc:
4516      return "DW_AT_high_pc";
4517    case DW_AT_language:
4518      return "DW_AT_language";
4519    case DW_AT_member:
4520      return "DW_AT_member";
4521    case DW_AT_discr:
4522      return "DW_AT_discr";
4523    case DW_AT_discr_value:
4524      return "DW_AT_discr_value";
4525    case DW_AT_visibility:
4526      return "DW_AT_visibility";
4527    case DW_AT_import:
4528      return "DW_AT_import";
4529    case DW_AT_string_length:
4530      return "DW_AT_string_length";
4531    case DW_AT_common_reference:
4532      return "DW_AT_common_reference";
4533    case DW_AT_comp_dir:
4534      return "DW_AT_comp_dir";
4535    case DW_AT_const_value:
4536      return "DW_AT_const_value";
4537    case DW_AT_containing_type:
4538      return "DW_AT_containing_type";
4539    case DW_AT_default_value:
4540      return "DW_AT_default_value";
4541    case DW_AT_inline:
4542      return "DW_AT_inline";
4543    case DW_AT_is_optional:
4544      return "DW_AT_is_optional";
4545    case DW_AT_lower_bound:
4546      return "DW_AT_lower_bound";
4547    case DW_AT_producer:
4548      return "DW_AT_producer";
4549    case DW_AT_prototyped:
4550      return "DW_AT_prototyped";
4551    case DW_AT_return_addr:
4552      return "DW_AT_return_addr";
4553    case DW_AT_start_scope:
4554      return "DW_AT_start_scope";
4555    case DW_AT_stride_size:
4556      return "DW_AT_stride_size";
4557    case DW_AT_upper_bound:
4558      return "DW_AT_upper_bound";
4559    case DW_AT_abstract_origin:
4560      return "DW_AT_abstract_origin";
4561    case DW_AT_accessibility:
4562      return "DW_AT_accessibility";
4563    case DW_AT_address_class:
4564      return "DW_AT_address_class";
4565    case DW_AT_artificial:
4566      return "DW_AT_artificial";
4567    case DW_AT_base_types:
4568      return "DW_AT_base_types";
4569    case DW_AT_calling_convention:
4570      return "DW_AT_calling_convention";
4571    case DW_AT_count:
4572      return "DW_AT_count";
4573    case DW_AT_data_member_location:
4574      return "DW_AT_data_member_location";
4575    case DW_AT_decl_column:
4576      return "DW_AT_decl_column";
4577    case DW_AT_decl_file:
4578      return "DW_AT_decl_file";
4579    case DW_AT_decl_line:
4580      return "DW_AT_decl_line";
4581    case DW_AT_declaration:
4582      return "DW_AT_declaration";
4583    case DW_AT_discr_list:
4584      return "DW_AT_discr_list";
4585    case DW_AT_encoding:
4586      return "DW_AT_encoding";
4587    case DW_AT_external:
4588      return "DW_AT_external";
4589    case DW_AT_frame_base:
4590      return "DW_AT_frame_base";
4591    case DW_AT_friend:
4592      return "DW_AT_friend";
4593    case DW_AT_identifier_case:
4594      return "DW_AT_identifier_case";
4595    case DW_AT_macro_info:
4596      return "DW_AT_macro_info";
4597    case DW_AT_namelist_items:
4598      return "DW_AT_namelist_items";
4599    case DW_AT_priority:
4600      return "DW_AT_priority";
4601    case DW_AT_segment:
4602      return "DW_AT_segment";
4603    case DW_AT_specification:
4604      return "DW_AT_specification";
4605    case DW_AT_static_link:
4606      return "DW_AT_static_link";
4607    case DW_AT_type:
4608      return "DW_AT_type";
4609    case DW_AT_use_location:
4610      return "DW_AT_use_location";
4611    case DW_AT_variable_parameter:
4612      return "DW_AT_variable_parameter";
4613    case DW_AT_virtuality:
4614      return "DW_AT_virtuality";
4615    case DW_AT_vtable_elem_location:
4616      return "DW_AT_vtable_elem_location";
4617
4618    case DW_AT_allocated:
4619      return "DW_AT_allocated";
4620    case DW_AT_associated:
4621      return "DW_AT_associated";
4622    case DW_AT_data_location:
4623      return "DW_AT_data_location";
4624    case DW_AT_stride:
4625      return "DW_AT_stride";
4626    case DW_AT_entry_pc:
4627      return "DW_AT_entry_pc";
4628    case DW_AT_use_UTF8:
4629      return "DW_AT_use_UTF8";
4630    case DW_AT_extension:
4631      return "DW_AT_extension";
4632    case DW_AT_ranges:
4633      return "DW_AT_ranges";
4634    case DW_AT_trampoline:
4635      return "DW_AT_trampoline";
4636    case DW_AT_call_column:
4637      return "DW_AT_call_column";
4638    case DW_AT_call_file:
4639      return "DW_AT_call_file";
4640    case DW_AT_call_line:
4641      return "DW_AT_call_line";
4642
4643    case DW_AT_MIPS_fde:
4644      return "DW_AT_MIPS_fde";
4645    case DW_AT_MIPS_loop_begin:
4646      return "DW_AT_MIPS_loop_begin";
4647    case DW_AT_MIPS_tail_loop_begin:
4648      return "DW_AT_MIPS_tail_loop_begin";
4649    case DW_AT_MIPS_epilog_begin:
4650      return "DW_AT_MIPS_epilog_begin";
4651    case DW_AT_MIPS_loop_unroll_factor:
4652      return "DW_AT_MIPS_loop_unroll_factor";
4653    case DW_AT_MIPS_software_pipeline_depth:
4654      return "DW_AT_MIPS_software_pipeline_depth";
4655    case DW_AT_MIPS_linkage_name:
4656      return "DW_AT_MIPS_linkage_name";
4657    case DW_AT_MIPS_stride:
4658      return "DW_AT_MIPS_stride";
4659    case DW_AT_MIPS_abstract_name:
4660      return "DW_AT_MIPS_abstract_name";
4661    case DW_AT_MIPS_clone_origin:
4662      return "DW_AT_MIPS_clone_origin";
4663    case DW_AT_MIPS_has_inlines:
4664      return "DW_AT_MIPS_has_inlines";
4665
4666    case DW_AT_sf_names:
4667      return "DW_AT_sf_names";
4668    case DW_AT_src_info:
4669      return "DW_AT_src_info";
4670    case DW_AT_mac_info:
4671      return "DW_AT_mac_info";
4672    case DW_AT_src_coords:
4673      return "DW_AT_src_coords";
4674    case DW_AT_body_begin:
4675      return "DW_AT_body_begin";
4676    case DW_AT_body_end:
4677      return "DW_AT_body_end";
4678    case DW_AT_GNU_vector:
4679      return "DW_AT_GNU_vector";
4680
4681    case DW_AT_VMS_rtnbeg_pd_address:
4682      return "DW_AT_VMS_rtnbeg_pd_address";
4683
4684    default:
4685      return "DW_AT_<unknown>";
4686    }
4687}
4688
4689/* Convert a DWARF value form code into its string name.  */
4690
4691static const char *
4692dwarf_form_name (unsigned int form)
4693{
4694  switch (form)
4695    {
4696    case DW_FORM_addr:
4697      return "DW_FORM_addr";
4698    case DW_FORM_block2:
4699      return "DW_FORM_block2";
4700    case DW_FORM_block4:
4701      return "DW_FORM_block4";
4702    case DW_FORM_data2:
4703      return "DW_FORM_data2";
4704    case DW_FORM_data4:
4705      return "DW_FORM_data4";
4706    case DW_FORM_data8:
4707      return "DW_FORM_data8";
4708    case DW_FORM_string:
4709      return "DW_FORM_string";
4710    case DW_FORM_block:
4711      return "DW_FORM_block";
4712    case DW_FORM_block1:
4713      return "DW_FORM_block1";
4714    case DW_FORM_data1:
4715      return "DW_FORM_data1";
4716    case DW_FORM_flag:
4717      return "DW_FORM_flag";
4718    case DW_FORM_sdata:
4719      return "DW_FORM_sdata";
4720    case DW_FORM_strp:
4721      return "DW_FORM_strp";
4722    case DW_FORM_udata:
4723      return "DW_FORM_udata";
4724    case DW_FORM_ref_addr:
4725      return "DW_FORM_ref_addr";
4726    case DW_FORM_ref1:
4727      return "DW_FORM_ref1";
4728    case DW_FORM_ref2:
4729      return "DW_FORM_ref2";
4730    case DW_FORM_ref4:
4731      return "DW_FORM_ref4";
4732    case DW_FORM_ref8:
4733      return "DW_FORM_ref8";
4734    case DW_FORM_ref_udata:
4735      return "DW_FORM_ref_udata";
4736    case DW_FORM_indirect:
4737      return "DW_FORM_indirect";
4738    default:
4739      return "DW_FORM_<unknown>";
4740    }
4741}
4742
4743/* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4744   instance of an inlined instance of a decl which is local to an inline
4745   function, so we have to trace all of the way back through the origin chain
4746   to find out what sort of node actually served as the original seed for the
4747   given block.  */
4748
4749static tree
4750decl_ultimate_origin (tree decl)
4751{
4752  if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4753    return NULL_TREE;
4754
4755  /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4756     nodes in the function to point to themselves; ignore that if
4757     we're trying to output the abstract instance of this function.  */
4758  if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4759    return NULL_TREE;
4760
4761  /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4762     most distant ancestor, this should never happen.  */
4763  gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4764
4765  return DECL_ABSTRACT_ORIGIN (decl);
4766}
4767
4768/* Determine the "ultimate origin" of a block.  The block may be an inlined
4769   instance of an inlined instance of a block which is local to an inline
4770   function, so we have to trace all of the way back through the origin chain
4771   to find out what sort of node actually served as the original seed for the
4772   given block.  */
4773
4774static tree
4775block_ultimate_origin (tree block)
4776{
4777  tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4778
4779  /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4780     nodes in the function to point to themselves; ignore that if
4781     we're trying to output the abstract instance of this function.  */
4782  if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4783    return NULL_TREE;
4784
4785  if (immediate_origin == NULL_TREE)
4786    return NULL_TREE;
4787  else
4788    {
4789      tree ret_val;
4790      tree lookahead = immediate_origin;
4791
4792      do
4793	{
4794	  ret_val = lookahead;
4795	  lookahead = (TREE_CODE (ret_val) == BLOCK
4796		       ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4797	}
4798      while (lookahead != NULL && lookahead != ret_val);
4799
4800      /* The block's abstract origin chain may not be the *ultimate* origin of
4801	 the block. It could lead to a DECL that has an abstract origin set.
4802	 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4803	 will give us if it has one).  Note that DECL's abstract origins are
4804	 supposed to be the most distant ancestor (or so decl_ultimate_origin
4805	 claims), so we don't need to loop following the DECL origins.  */
4806      if (DECL_P (ret_val))
4807	return DECL_ORIGIN (ret_val);
4808
4809      return ret_val;
4810    }
4811}
4812
4813/* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4814   of a virtual function may refer to a base class, so we check the 'this'
4815   parameter.  */
4816
4817static tree
4818decl_class_context (tree decl)
4819{
4820  tree context = NULL_TREE;
4821
4822  if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4823    context = DECL_CONTEXT (decl);
4824  else
4825    context = TYPE_MAIN_VARIANT
4826      (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4827
4828  if (context && !TYPE_P (context))
4829    context = NULL_TREE;
4830
4831  return context;
4832}
4833
4834/* Add an attribute/value pair to a DIE.  We build the lists up in reverse
4835   addition order, and correct that in reverse_all_dies.  */
4836
4837static inline void
4838add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4839{
4840  if (die != NULL && attr != NULL)
4841    {
4842      attr->dw_attr_next = die->die_attr;
4843      die->die_attr = attr;
4844    }
4845}
4846
4847static inline enum dw_val_class
4848AT_class (dw_attr_ref a)
4849{
4850  return a->dw_attr_val.val_class;
4851}
4852
4853/* Add a flag value attribute to a DIE.  */
4854
4855static inline void
4856add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4857{
4858  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4859
4860  attr->dw_attr_next = NULL;
4861  attr->dw_attr = attr_kind;
4862  attr->dw_attr_val.val_class = dw_val_class_flag;
4863  attr->dw_attr_val.v.val_flag = flag;
4864  add_dwarf_attr (die, attr);
4865}
4866
4867static inline unsigned
4868AT_flag (dw_attr_ref a)
4869{
4870  gcc_assert (a && AT_class (a) == dw_val_class_flag);
4871  return a->dw_attr_val.v.val_flag;
4872}
4873
4874/* Add a signed integer attribute value to a DIE.  */
4875
4876static inline void
4877add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4878{
4879  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4880
4881  attr->dw_attr_next = NULL;
4882  attr->dw_attr = attr_kind;
4883  attr->dw_attr_val.val_class = dw_val_class_const;
4884  attr->dw_attr_val.v.val_int = int_val;
4885  add_dwarf_attr (die, attr);
4886}
4887
4888static inline HOST_WIDE_INT
4889AT_int (dw_attr_ref a)
4890{
4891  gcc_assert (a && AT_class (a) == dw_val_class_const);
4892  return a->dw_attr_val.v.val_int;
4893}
4894
4895/* Add an unsigned integer attribute value to a DIE.  */
4896
4897static inline void
4898add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4899		 unsigned HOST_WIDE_INT unsigned_val)
4900{
4901  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4902
4903  attr->dw_attr_next = NULL;
4904  attr->dw_attr = attr_kind;
4905  attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4906  attr->dw_attr_val.v.val_unsigned = unsigned_val;
4907  add_dwarf_attr (die, attr);
4908}
4909
4910static inline unsigned HOST_WIDE_INT
4911AT_unsigned (dw_attr_ref a)
4912{
4913  gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4914  return a->dw_attr_val.v.val_unsigned;
4915}
4916
4917/* Add an unsigned double integer attribute value to a DIE.  */
4918
4919static inline void
4920add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4921		  long unsigned int val_hi, long unsigned int val_low)
4922{
4923  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4924
4925  attr->dw_attr_next = NULL;
4926  attr->dw_attr = attr_kind;
4927  attr->dw_attr_val.val_class = dw_val_class_long_long;
4928  attr->dw_attr_val.v.val_long_long.hi = val_hi;
4929  attr->dw_attr_val.v.val_long_long.low = val_low;
4930  add_dwarf_attr (die, attr);
4931}
4932
4933/* Add a floating point attribute value to a DIE and return it.  */
4934
4935static inline void
4936add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4937	    unsigned int length, unsigned int elt_size, unsigned char *array)
4938{
4939  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4940
4941  attr->dw_attr_next = NULL;
4942  attr->dw_attr = attr_kind;
4943  attr->dw_attr_val.val_class = dw_val_class_vec;
4944  attr->dw_attr_val.v.val_vec.length = length;
4945  attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4946  attr->dw_attr_val.v.val_vec.array = array;
4947  add_dwarf_attr (die, attr);
4948}
4949
4950/* Hash and equality functions for debug_str_hash.  */
4951
4952static hashval_t
4953debug_str_do_hash (const void *x)
4954{
4955  return htab_hash_string (((const struct indirect_string_node *)x)->str);
4956}
4957
4958static int
4959debug_str_eq (const void *x1, const void *x2)
4960{
4961  return strcmp ((((const struct indirect_string_node *)x1)->str),
4962		 (const char *)x2) == 0;
4963}
4964
4965/* Add a string attribute value to a DIE.  */
4966
4967static inline void
4968add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4969{
4970  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4971  struct indirect_string_node *node;
4972  void **slot;
4973
4974  if (! debug_str_hash)
4975    debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4976				      debug_str_eq, NULL);
4977
4978  slot = htab_find_slot_with_hash (debug_str_hash, str,
4979				   htab_hash_string (str), INSERT);
4980  if (*slot == NULL)
4981    *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4982  node = (struct indirect_string_node *) *slot;
4983  node->str = ggc_strdup (str);
4984  node->refcount++;
4985
4986  attr->dw_attr_next = NULL;
4987  attr->dw_attr = attr_kind;
4988  attr->dw_attr_val.val_class = dw_val_class_str;
4989  attr->dw_attr_val.v.val_str = node;
4990  add_dwarf_attr (die, attr);
4991}
4992
4993static inline const char *
4994AT_string (dw_attr_ref a)
4995{
4996  gcc_assert (a && AT_class (a) == dw_val_class_str);
4997  return a->dw_attr_val.v.val_str->str;
4998}
4999
5000/* Find out whether a string should be output inline in DIE
5001   or out-of-line in .debug_str section.  */
5002
5003static int
5004AT_string_form (dw_attr_ref a)
5005{
5006  struct indirect_string_node *node;
5007  unsigned int len;
5008  char label[32];
5009
5010  gcc_assert (a && AT_class (a) == dw_val_class_str);
5011
5012  node = a->dw_attr_val.v.val_str;
5013  if (node->form)
5014    return node->form;
5015
5016  len = strlen (node->str) + 1;
5017
5018  /* If the string is shorter or equal to the size of the reference, it is
5019     always better to put it inline.  */
5020  if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5021    return node->form = DW_FORM_string;
5022
5023  /* If we cannot expect the linker to merge strings in .debug_str
5024     section, only put it into .debug_str if it is worth even in this
5025     single module.  */
5026  if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
5027      && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5028    return node->form = DW_FORM_string;
5029
5030  ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5031  ++dw2_string_counter;
5032  node->label = xstrdup (label);
5033
5034  return node->form = DW_FORM_strp;
5035}
5036
5037/* Add a DIE reference attribute value to a DIE.  */
5038
5039static inline void
5040add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5041{
5042  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5043
5044  attr->dw_attr_next = NULL;
5045  attr->dw_attr = attr_kind;
5046  attr->dw_attr_val.val_class = dw_val_class_die_ref;
5047  attr->dw_attr_val.v.val_die_ref.die = targ_die;
5048  attr->dw_attr_val.v.val_die_ref.external = 0;
5049  add_dwarf_attr (die, attr);
5050}
5051
5052/* Add an AT_specification attribute to a DIE, and also make the back
5053   pointer from the specification to the definition.  */
5054
5055static inline void
5056add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5057{
5058  add_AT_die_ref (die, DW_AT_specification, targ_die);
5059  gcc_assert (!targ_die->die_definition);
5060  targ_die->die_definition = die;
5061}
5062
5063static inline dw_die_ref
5064AT_ref (dw_attr_ref a)
5065{
5066  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5067  return a->dw_attr_val.v.val_die_ref.die;
5068}
5069
5070static inline int
5071AT_ref_external (dw_attr_ref a)
5072{
5073  if (a && AT_class (a) == dw_val_class_die_ref)
5074    return a->dw_attr_val.v.val_die_ref.external;
5075
5076  return 0;
5077}
5078
5079static inline void
5080set_AT_ref_external (dw_attr_ref a, int i)
5081{
5082  gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5083  a->dw_attr_val.v.val_die_ref.external = i;
5084}
5085
5086/* Add an FDE reference attribute value to a DIE.  */
5087
5088static inline void
5089add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5090{
5091  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5092
5093  attr->dw_attr_next = NULL;
5094  attr->dw_attr = attr_kind;
5095  attr->dw_attr_val.val_class = dw_val_class_fde_ref;
5096  attr->dw_attr_val.v.val_fde_index = targ_fde;
5097  add_dwarf_attr (die, attr);
5098}
5099
5100/* Add a location description attribute value to a DIE.  */
5101
5102static inline void
5103add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5104{
5105  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5106
5107  attr->dw_attr_next = NULL;
5108  attr->dw_attr = attr_kind;
5109  attr->dw_attr_val.val_class = dw_val_class_loc;
5110  attr->dw_attr_val.v.val_loc = loc;
5111  add_dwarf_attr (die, attr);
5112}
5113
5114static inline dw_loc_descr_ref
5115AT_loc (dw_attr_ref a)
5116{
5117  gcc_assert (a && AT_class (a) == dw_val_class_loc);
5118  return a->dw_attr_val.v.val_loc;
5119}
5120
5121static inline void
5122add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5123{
5124  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5125
5126  attr->dw_attr_next = NULL;
5127  attr->dw_attr = attr_kind;
5128  attr->dw_attr_val.val_class = dw_val_class_loc_list;
5129  attr->dw_attr_val.v.val_loc_list = loc_list;
5130  add_dwarf_attr (die, attr);
5131  have_location_lists = 1;
5132}
5133
5134static inline dw_loc_list_ref
5135AT_loc_list (dw_attr_ref a)
5136{
5137  gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5138  return a->dw_attr_val.v.val_loc_list;
5139}
5140
5141/* Add an address constant attribute value to a DIE.  */
5142
5143static inline void
5144add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5145{
5146  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5147
5148  attr->dw_attr_next = NULL;
5149  attr->dw_attr = attr_kind;
5150  attr->dw_attr_val.val_class = dw_val_class_addr;
5151  attr->dw_attr_val.v.val_addr = addr;
5152  add_dwarf_attr (die, attr);
5153}
5154
5155static inline rtx
5156AT_addr (dw_attr_ref a)
5157{
5158  gcc_assert (a && AT_class (a) == dw_val_class_addr);
5159  return a->dw_attr_val.v.val_addr;
5160}
5161
5162/* Add a label identifier attribute value to a DIE.  */
5163
5164static inline void
5165add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5166{
5167  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5168
5169  attr->dw_attr_next = NULL;
5170  attr->dw_attr = attr_kind;
5171  attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5172  attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5173  add_dwarf_attr (die, attr);
5174}
5175
5176/* Add a section offset attribute value to a DIE.  */
5177
5178static inline void
5179add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5180{
5181  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5182
5183  attr->dw_attr_next = NULL;
5184  attr->dw_attr = attr_kind;
5185  attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5186  attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5187  add_dwarf_attr (die, attr);
5188}
5189
5190/* Add an offset attribute value to a DIE.  */
5191
5192static inline void
5193add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5194	       unsigned HOST_WIDE_INT offset)
5195{
5196  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5197
5198  attr->dw_attr_next = NULL;
5199  attr->dw_attr = attr_kind;
5200  attr->dw_attr_val.val_class = dw_val_class_offset;
5201  attr->dw_attr_val.v.val_offset = offset;
5202  add_dwarf_attr (die, attr);
5203}
5204
5205/* Add an range_list attribute value to a DIE.  */
5206
5207static void
5208add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5209		   long unsigned int offset)
5210{
5211  dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5212
5213  attr->dw_attr_next = NULL;
5214  attr->dw_attr = attr_kind;
5215  attr->dw_attr_val.val_class = dw_val_class_range_list;
5216  attr->dw_attr_val.v.val_offset = offset;
5217  add_dwarf_attr (die, attr);
5218}
5219
5220static inline const char *
5221AT_lbl (dw_attr_ref a)
5222{
5223  gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5224		    || AT_class (a) == dw_val_class_lbl_offset));
5225  return a->dw_attr_val.v.val_lbl_id;
5226}
5227
5228/* Get the attribute of type attr_kind.  */
5229
5230static dw_attr_ref
5231get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5232{
5233  dw_attr_ref a;
5234  dw_die_ref spec = NULL;
5235
5236  if (die != NULL)
5237    {
5238      for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5239	if (a->dw_attr == attr_kind)
5240	  return a;
5241	else if (a->dw_attr == DW_AT_specification
5242		 || a->dw_attr == DW_AT_abstract_origin)
5243	  spec = AT_ref (a);
5244
5245      if (spec)
5246	return get_AT (spec, attr_kind);
5247    }
5248
5249  return NULL;
5250}
5251
5252/* Return the "low pc" attribute value, typically associated with a subprogram
5253   DIE.  Return null if the "low pc" attribute is either not present, or if it
5254   cannot be represented as an assembler label identifier.  */
5255
5256static inline const char *
5257get_AT_low_pc (dw_die_ref die)
5258{
5259  dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5260
5261  return a ? AT_lbl (a) : NULL;
5262}
5263
5264/* Return the "high pc" attribute value, typically associated with a subprogram
5265   DIE.  Return null if the "high pc" attribute is either not present, or if it
5266   cannot be represented as an assembler label identifier.  */
5267
5268static inline const char *
5269get_AT_hi_pc (dw_die_ref die)
5270{
5271  dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5272
5273  return a ? AT_lbl (a) : NULL;
5274}
5275
5276/* Return the value of the string attribute designated by ATTR_KIND, or
5277   NULL if it is not present.  */
5278
5279static inline const char *
5280get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5281{
5282  dw_attr_ref a = get_AT (die, attr_kind);
5283
5284  return a ? AT_string (a) : NULL;
5285}
5286
5287/* Return the value of the flag attribute designated by ATTR_KIND, or -1
5288   if it is not present.  */
5289
5290static inline int
5291get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5292{
5293  dw_attr_ref a = get_AT (die, attr_kind);
5294
5295  return a ? AT_flag (a) : 0;
5296}
5297
5298/* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5299   if it is not present.  */
5300
5301static inline unsigned
5302get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5303{
5304  dw_attr_ref a = get_AT (die, attr_kind);
5305
5306  return a ? AT_unsigned (a) : 0;
5307}
5308
5309static inline dw_die_ref
5310get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5311{
5312  dw_attr_ref a = get_AT (die, attr_kind);
5313
5314  return a ? AT_ref (a) : NULL;
5315}
5316
5317/* Return TRUE if the language is C or C++.  */
5318
5319static inline bool
5320is_c_family (void)
5321{
5322  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5323
5324  return (lang == DW_LANG_C || lang == DW_LANG_C89
5325	  || lang == DW_LANG_C_plus_plus);
5326}
5327
5328/* Return TRUE if the language is C++.  */
5329
5330static inline bool
5331is_cxx (void)
5332{
5333  return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5334	  == DW_LANG_C_plus_plus);
5335}
5336
5337/* Return TRUE if the language is Fortran.  */
5338
5339static inline bool
5340is_fortran (void)
5341{
5342  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5343
5344  return (lang == DW_LANG_Fortran77
5345	  || lang == DW_LANG_Fortran90
5346	  || lang == DW_LANG_Fortran95);
5347}
5348
5349/* Return TRUE if the language is Java.  */
5350
5351static inline bool
5352is_java (void)
5353{
5354  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5355
5356  return lang == DW_LANG_Java;
5357}
5358
5359/* Return TRUE if the language is Ada.  */
5360
5361static inline bool
5362is_ada (void)
5363{
5364  unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5365
5366  return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5367}
5368
5369/* Free up the memory used by A.  */
5370
5371static inline void free_AT (dw_attr_ref);
5372static inline void
5373free_AT (dw_attr_ref a)
5374{
5375  if (AT_class (a) == dw_val_class_str)
5376    if (a->dw_attr_val.v.val_str->refcount)
5377      a->dw_attr_val.v.val_str->refcount--;
5378}
5379
5380/* Remove the specified attribute if present.  */
5381
5382static void
5383remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5384{
5385  dw_attr_ref *p;
5386  dw_attr_ref removed = NULL;
5387
5388  if (die != NULL)
5389    {
5390      for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5391	if ((*p)->dw_attr == attr_kind)
5392	  {
5393	    removed = *p;
5394	    *p = (*p)->dw_attr_next;
5395	    break;
5396	  }
5397
5398      if (removed != 0)
5399	free_AT (removed);
5400    }
5401}
5402
5403/* Remove child die whose die_tag is specified tag.  */
5404
5405static void
5406remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5407{
5408  dw_die_ref current, prev, next;
5409  current = die->die_child;
5410  prev = NULL;
5411  while (current != NULL)
5412    {
5413      if (current->die_tag == tag)
5414	{
5415	  next = current->die_sib;
5416	  if (prev == NULL)
5417	    die->die_child = next;
5418	  else
5419	    prev->die_sib = next;
5420	  free_die (current);
5421	  current = next;
5422	}
5423      else
5424	{
5425	  prev = current;
5426	  current = current->die_sib;
5427	}
5428    }
5429}
5430
5431/* Free up the memory used by DIE.  */
5432
5433static inline void
5434free_die (dw_die_ref die)
5435{
5436  remove_children (die);
5437}
5438
5439/* Discard the children of this DIE.  */
5440
5441static void
5442remove_children (dw_die_ref die)
5443{
5444  dw_die_ref child_die = die->die_child;
5445
5446  die->die_child = NULL;
5447
5448  while (child_die != NULL)
5449    {
5450      dw_die_ref tmp_die = child_die;
5451      dw_attr_ref a;
5452
5453      child_die = child_die->die_sib;
5454
5455      for (a = tmp_die->die_attr; a != NULL;)
5456	{
5457	  dw_attr_ref tmp_a = a;
5458
5459	  a = a->dw_attr_next;
5460	  free_AT (tmp_a);
5461	}
5462
5463      free_die (tmp_die);
5464    }
5465}
5466
5467/* Add a child DIE below its parent.  We build the lists up in reverse
5468   addition order, and correct that in reverse_all_dies.  */
5469
5470static inline void
5471add_child_die (dw_die_ref die, dw_die_ref child_die)
5472{
5473  if (die != NULL && child_die != NULL)
5474    {
5475      gcc_assert (die != child_die);
5476
5477      child_die->die_parent = die;
5478      child_die->die_sib = die->die_child;
5479      die->die_child = child_die;
5480    }
5481}
5482
5483/* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5484   is the specification, to the front of PARENT's list of children.  */
5485
5486static void
5487splice_child_die (dw_die_ref parent, dw_die_ref child)
5488{
5489  dw_die_ref *p;
5490
5491  /* We want the declaration DIE from inside the class, not the
5492     specification DIE at toplevel.  */
5493  if (child->die_parent != parent)
5494    {
5495      dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5496
5497      if (tmp)
5498	child = tmp;
5499    }
5500
5501  gcc_assert (child->die_parent == parent
5502	      || (child->die_parent
5503		  == get_AT_ref (parent, DW_AT_specification)));
5504
5505  for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5506    if (*p == child)
5507      {
5508	*p = child->die_sib;
5509	break;
5510      }
5511
5512  child->die_parent = parent;
5513  child->die_sib = parent->die_child;
5514  parent->die_child = child;
5515}
5516
5517/* Return a pointer to a newly created DIE node.  */
5518
5519static inline dw_die_ref
5520new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5521{
5522  dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5523
5524  die->die_tag = tag_value;
5525
5526  if (parent_die != NULL)
5527    add_child_die (parent_die, die);
5528  else
5529    {
5530      limbo_die_node *limbo_node;
5531
5532      limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5533      limbo_node->die = die;
5534      limbo_node->created_for = t;
5535      limbo_node->next = limbo_die_list;
5536      limbo_die_list = limbo_node;
5537    }
5538
5539  return die;
5540}
5541
5542/* Return the DIE associated with the given type specifier.  */
5543
5544static inline dw_die_ref
5545lookup_type_die (tree type)
5546{
5547  return TYPE_SYMTAB_DIE (type);
5548}
5549
5550/* Equate a DIE to a given type specifier.  */
5551
5552static inline void
5553equate_type_number_to_die (tree type, dw_die_ref type_die)
5554{
5555  TYPE_SYMTAB_DIE (type) = type_die;
5556}
5557
5558/* Returns a hash value for X (which really is a die_struct).  */
5559
5560static hashval_t
5561decl_die_table_hash (const void *x)
5562{
5563  return (hashval_t) ((const dw_die_ref) x)->decl_id;
5564}
5565
5566/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5567
5568static int
5569decl_die_table_eq (const void *x, const void *y)
5570{
5571  return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5572}
5573
5574/* Return the DIE associated with a given declaration.  */
5575
5576static inline dw_die_ref
5577lookup_decl_die (tree decl)
5578{
5579  return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5580}
5581
5582/* Returns a hash value for X (which really is a var_loc_list).  */
5583
5584static hashval_t
5585decl_loc_table_hash (const void *x)
5586{
5587  return (hashval_t) ((const var_loc_list *) x)->decl_id;
5588}
5589
5590/* Return nonzero if decl_id of var_loc_list X is the same as
5591   UID of decl *Y.  */
5592
5593static int
5594decl_loc_table_eq (const void *x, const void *y)
5595{
5596  return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5597}
5598
5599/* Return the var_loc list associated with a given declaration.  */
5600
5601static inline var_loc_list *
5602lookup_decl_loc (tree decl)
5603{
5604  return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5605}
5606
5607/* Equate a DIE to a particular declaration.  */
5608
5609static void
5610equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5611{
5612  unsigned int decl_id = DECL_UID (decl);
5613  void **slot;
5614
5615  slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5616  *slot = decl_die;
5617  decl_die->decl_id = decl_id;
5618}
5619
5620/* Add a variable location node to the linked list for DECL.  */
5621
5622static void
5623add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5624{
5625  unsigned int decl_id = DECL_UID (decl);
5626  var_loc_list *temp;
5627  void **slot;
5628
5629  slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5630  if (*slot == NULL)
5631    {
5632      temp = ggc_alloc_cleared (sizeof (var_loc_list));
5633      temp->decl_id = decl_id;
5634      *slot = temp;
5635    }
5636  else
5637    temp = *slot;
5638
5639  if (temp->last)
5640    {
5641      /* If the current location is the same as the end of the list,
5642	 we have nothing to do.  */
5643      if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5644			NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5645	{
5646	  /* Add LOC to the end of list and update LAST.  */
5647	  temp->last->next = loc;
5648	  temp->last = loc;
5649	}
5650    }
5651  /* Do not add empty location to the beginning of the list.  */
5652  else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5653    {
5654      temp->first = loc;
5655      temp->last = loc;
5656    }
5657}
5658
5659/* Keep track of the number of spaces used to indent the
5660   output of the debugging routines that print the structure of
5661   the DIE internal representation.  */
5662static int print_indent;
5663
5664/* Indent the line the number of spaces given by print_indent.  */
5665
5666static inline void
5667print_spaces (FILE *outfile)
5668{
5669  fprintf (outfile, "%*s", print_indent, "");
5670}
5671
5672/* Print the information associated with a given DIE, and its children.
5673   This routine is a debugging aid only.  */
5674
5675static void
5676print_die (dw_die_ref die, FILE *outfile)
5677{
5678  dw_attr_ref a;
5679  dw_die_ref c;
5680
5681  print_spaces (outfile);
5682  fprintf (outfile, "DIE %4lu: %s\n",
5683	   die->die_offset, dwarf_tag_name (die->die_tag));
5684  print_spaces (outfile);
5685  fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5686  fprintf (outfile, " offset: %lu\n", die->die_offset);
5687
5688  for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5689    {
5690      print_spaces (outfile);
5691      fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5692
5693      switch (AT_class (a))
5694	{
5695	case dw_val_class_addr:
5696	  fprintf (outfile, "address");
5697	  break;
5698	case dw_val_class_offset:
5699	  fprintf (outfile, "offset");
5700	  break;
5701	case dw_val_class_loc:
5702	  fprintf (outfile, "location descriptor");
5703	  break;
5704	case dw_val_class_loc_list:
5705	  fprintf (outfile, "location list -> label:%s",
5706		   AT_loc_list (a)->ll_symbol);
5707	  break;
5708	case dw_val_class_range_list:
5709	  fprintf (outfile, "range list");
5710	  break;
5711	case dw_val_class_const:
5712	  fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5713	  break;
5714	case dw_val_class_unsigned_const:
5715	  fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5716	  break;
5717	case dw_val_class_long_long:
5718	  fprintf (outfile, "constant (%lu,%lu)",
5719		   a->dw_attr_val.v.val_long_long.hi,
5720		   a->dw_attr_val.v.val_long_long.low);
5721	  break;
5722	case dw_val_class_vec:
5723	  fprintf (outfile, "floating-point or vector constant");
5724	  break;
5725	case dw_val_class_flag:
5726	  fprintf (outfile, "%u", AT_flag (a));
5727	  break;
5728	case dw_val_class_die_ref:
5729	  if (AT_ref (a) != NULL)
5730	    {
5731	      if (AT_ref (a)->die_symbol)
5732		fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5733	      else
5734		fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5735	    }
5736	  else
5737	    fprintf (outfile, "die -> <null>");
5738	  break;
5739	case dw_val_class_lbl_id:
5740	case dw_val_class_lbl_offset:
5741	  fprintf (outfile, "label: %s", AT_lbl (a));
5742	  break;
5743	case dw_val_class_str:
5744	  if (AT_string (a) != NULL)
5745	    fprintf (outfile, "\"%s\"", AT_string (a));
5746	  else
5747	    fprintf (outfile, "<null>");
5748	  break;
5749	default:
5750	  break;
5751	}
5752
5753      fprintf (outfile, "\n");
5754    }
5755
5756  if (die->die_child != NULL)
5757    {
5758      print_indent += 4;
5759      for (c = die->die_child; c != NULL; c = c->die_sib)
5760	print_die (c, outfile);
5761
5762      print_indent -= 4;
5763    }
5764  if (print_indent == 0)
5765    fprintf (outfile, "\n");
5766}
5767
5768/* Print the contents of the source code line number correspondence table.
5769   This routine is a debugging aid only.  */
5770
5771static void
5772print_dwarf_line_table (FILE *outfile)
5773{
5774  unsigned i;
5775  dw_line_info_ref line_info;
5776
5777  fprintf (outfile, "\n\nDWARF source line information\n");
5778  for (i = 1; i < line_info_table_in_use; i++)
5779    {
5780      line_info = &line_info_table[i];
5781      fprintf (outfile, "%5d: ", i);
5782      fprintf (outfile, "%-20s",
5783	       VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5784      fprintf (outfile, "%6ld", line_info->dw_line_num);
5785      fprintf (outfile, "\n");
5786    }
5787
5788  fprintf (outfile, "\n\n");
5789}
5790
5791/* Print the information collected for a given DIE.  */
5792
5793void
5794debug_dwarf_die (dw_die_ref die)
5795{
5796  print_die (die, stderr);
5797}
5798
5799/* Print all DWARF information collected for the compilation unit.
5800   This routine is a debugging aid only.  */
5801
5802void
5803debug_dwarf (void)
5804{
5805  print_indent = 0;
5806  print_die (comp_unit_die, stderr);
5807  if (! DWARF2_ASM_LINE_DEBUG_INFO)
5808    print_dwarf_line_table (stderr);
5809}
5810
5811/* We build up the lists of children and attributes by pushing new ones
5812   onto the beginning of the list.  Reverse the lists for DIE so that
5813   they are in order of addition.  */
5814
5815static void
5816reverse_die_lists (dw_die_ref die)
5817{
5818  dw_die_ref c, cp, cn;
5819  dw_attr_ref a, ap, an;
5820
5821  for (a = die->die_attr, ap = 0; a; a = an)
5822    {
5823      an = a->dw_attr_next;
5824      a->dw_attr_next = ap;
5825      ap = a;
5826    }
5827
5828  die->die_attr = ap;
5829
5830  for (c = die->die_child, cp = 0; c; c = cn)
5831    {
5832      cn = c->die_sib;
5833      c->die_sib = cp;
5834      cp = c;
5835    }
5836
5837  die->die_child = cp;
5838}
5839
5840/* reverse_die_lists only reverses the single die you pass it. Since we used to
5841   reverse all dies in add_sibling_attributes, which runs through all the dies,
5842   it would reverse all the dies.  Now, however, since we don't call
5843   reverse_die_lists in add_sibling_attributes, we need a routine to
5844   recursively reverse all the dies. This is that routine.  */
5845
5846static void
5847reverse_all_dies (dw_die_ref die)
5848{
5849  dw_die_ref c;
5850
5851  reverse_die_lists (die);
5852
5853  for (c = die->die_child; c; c = c->die_sib)
5854    reverse_all_dies (c);
5855}
5856
5857/* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5858   for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5859   DIE that marks the start of the DIEs for this include file.  */
5860
5861static dw_die_ref
5862push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5863{
5864  const char *filename = get_AT_string (bincl_die, DW_AT_name);
5865  dw_die_ref new_unit = gen_compile_unit_die (filename);
5866
5867  new_unit->die_sib = old_unit;
5868  return new_unit;
5869}
5870
5871/* Close an include-file CU and reopen the enclosing one.  */
5872
5873static dw_die_ref
5874pop_compile_unit (dw_die_ref old_unit)
5875{
5876  dw_die_ref new_unit = old_unit->die_sib;
5877
5878  old_unit->die_sib = NULL;
5879  return new_unit;
5880}
5881
5882#define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5883#define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5884
5885/* Calculate the checksum of a location expression.  */
5886
5887static inline void
5888loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5889{
5890  CHECKSUM (loc->dw_loc_opc);
5891  CHECKSUM (loc->dw_loc_oprnd1);
5892  CHECKSUM (loc->dw_loc_oprnd2);
5893}
5894
5895/* Calculate the checksum of an attribute.  */
5896
5897static void
5898attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5899{
5900  dw_loc_descr_ref loc;
5901  rtx r;
5902
5903  CHECKSUM (at->dw_attr);
5904
5905  /* We don't care about differences in file numbering.  */
5906  if (at->dw_attr == DW_AT_decl_file
5907      /* Or that this was compiled with a different compiler snapshot; if
5908	 the output is the same, that's what matters.  */
5909      || at->dw_attr == DW_AT_producer)
5910    return;
5911
5912  switch (AT_class (at))
5913    {
5914    case dw_val_class_const:
5915      CHECKSUM (at->dw_attr_val.v.val_int);
5916      break;
5917    case dw_val_class_unsigned_const:
5918      CHECKSUM (at->dw_attr_val.v.val_unsigned);
5919      break;
5920    case dw_val_class_long_long:
5921      CHECKSUM (at->dw_attr_val.v.val_long_long);
5922      break;
5923    case dw_val_class_vec:
5924      CHECKSUM (at->dw_attr_val.v.val_vec);
5925      break;
5926    case dw_val_class_flag:
5927      CHECKSUM (at->dw_attr_val.v.val_flag);
5928      break;
5929    case dw_val_class_str:
5930      CHECKSUM_STRING (AT_string (at));
5931      break;
5932
5933    case dw_val_class_addr:
5934      r = AT_addr (at);
5935      gcc_assert (GET_CODE (r) == SYMBOL_REF);
5936      CHECKSUM_STRING (XSTR (r, 0));
5937      break;
5938
5939    case dw_val_class_offset:
5940      CHECKSUM (at->dw_attr_val.v.val_offset);
5941      break;
5942
5943    case dw_val_class_loc:
5944      for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5945	loc_checksum (loc, ctx);
5946      break;
5947
5948    case dw_val_class_die_ref:
5949      die_checksum (AT_ref (at), ctx, mark);
5950      break;
5951
5952    case dw_val_class_fde_ref:
5953    case dw_val_class_lbl_id:
5954    case dw_val_class_lbl_offset:
5955      break;
5956
5957    default:
5958      break;
5959    }
5960}
5961
5962/* Calculate the checksum of a DIE.  */
5963
5964static void
5965die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5966{
5967  dw_die_ref c;
5968  dw_attr_ref a;
5969
5970  /* To avoid infinite recursion.  */
5971  if (die->die_mark)
5972    {
5973      CHECKSUM (die->die_mark);
5974      return;
5975    }
5976  die->die_mark = ++(*mark);
5977
5978  CHECKSUM (die->die_tag);
5979
5980  for (a = die->die_attr; a; a = a->dw_attr_next)
5981    attr_checksum (a, ctx, mark);
5982
5983  for (c = die->die_child; c; c = c->die_sib)
5984    die_checksum (c, ctx, mark);
5985}
5986
5987#undef CHECKSUM
5988#undef CHECKSUM_STRING
5989
5990/* Do the location expressions look same?  */
5991static inline int
5992same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5993{
5994  return loc1->dw_loc_opc == loc2->dw_loc_opc
5995	 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5996	 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5997}
5998
5999/* Do the values look the same?  */
6000static int
6001same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6002{
6003  dw_loc_descr_ref loc1, loc2;
6004  rtx r1, r2;
6005
6006  if (v1->val_class != v2->val_class)
6007    return 0;
6008
6009  switch (v1->val_class)
6010    {
6011    case dw_val_class_const:
6012      return v1->v.val_int == v2->v.val_int;
6013    case dw_val_class_unsigned_const:
6014      return v1->v.val_unsigned == v2->v.val_unsigned;
6015    case dw_val_class_long_long:
6016      return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6017	     && v1->v.val_long_long.low == v2->v.val_long_long.low;
6018    case dw_val_class_vec:
6019      if (v1->v.val_vec.length != v2->v.val_vec.length
6020	  || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6021	return 0;
6022      if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6023		  v1->v.val_vec.length * v1->v.val_vec.elt_size))
6024	return 0;
6025      return 1;
6026    case dw_val_class_flag:
6027      return v1->v.val_flag == v2->v.val_flag;
6028    case dw_val_class_str:
6029      return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6030
6031    case dw_val_class_addr:
6032      r1 = v1->v.val_addr;
6033      r2 = v2->v.val_addr;
6034      if (GET_CODE (r1) != GET_CODE (r2))
6035	return 0;
6036      gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6037      return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6038
6039    case dw_val_class_offset:
6040      return v1->v.val_offset == v2->v.val_offset;
6041
6042    case dw_val_class_loc:
6043      for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6044	   loc1 && loc2;
6045	   loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6046	if (!same_loc_p (loc1, loc2, mark))
6047	  return 0;
6048      return !loc1 && !loc2;
6049
6050    case dw_val_class_die_ref:
6051      return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6052
6053    case dw_val_class_fde_ref:
6054    case dw_val_class_lbl_id:
6055    case dw_val_class_lbl_offset:
6056      return 1;
6057
6058    default:
6059      return 1;
6060    }
6061}
6062
6063/* Do the attributes look the same?  */
6064
6065static int
6066same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6067{
6068  if (at1->dw_attr != at2->dw_attr)
6069    return 0;
6070
6071  /* We don't care about differences in file numbering.  */
6072  if (at1->dw_attr == DW_AT_decl_file
6073      /* Or that this was compiled with a different compiler snapshot; if
6074	 the output is the same, that's what matters.  */
6075      || at1->dw_attr == DW_AT_producer)
6076    return 1;
6077
6078  return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6079}
6080
6081/* Do the dies look the same?  */
6082
6083static int
6084same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6085{
6086  dw_die_ref c1, c2;
6087  dw_attr_ref a1, a2;
6088
6089  /* To avoid infinite recursion.  */
6090  if (die1->die_mark)
6091    return die1->die_mark == die2->die_mark;
6092  die1->die_mark = die2->die_mark = ++(*mark);
6093
6094  if (die1->die_tag != die2->die_tag)
6095    return 0;
6096
6097  for (a1 = die1->die_attr, a2 = die2->die_attr;
6098       a1 && a2;
6099       a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
6100    if (!same_attr_p (a1, a2, mark))
6101      return 0;
6102  if (a1 || a2)
6103    return 0;
6104
6105  for (c1 = die1->die_child, c2 = die2->die_child;
6106       c1 && c2;
6107       c1 = c1->die_sib, c2 = c2->die_sib)
6108    if (!same_die_p (c1, c2, mark))
6109      return 0;
6110  if (c1 || c2)
6111    return 0;
6112
6113  return 1;
6114}
6115
6116/* Do the dies look the same?  Wrapper around same_die_p.  */
6117
6118static int
6119same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6120{
6121  int mark = 0;
6122  int ret = same_die_p (die1, die2, &mark);
6123
6124  unmark_all_dies (die1);
6125  unmark_all_dies (die2);
6126
6127  return ret;
6128}
6129
6130/* The prefix to attach to symbols on DIEs in the current comdat debug
6131   info section.  */
6132static char *comdat_symbol_id;
6133
6134/* The index of the current symbol within the current comdat CU.  */
6135static unsigned int comdat_symbol_number;
6136
6137/* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6138   children, and set comdat_symbol_id accordingly.  */
6139
6140static void
6141compute_section_prefix (dw_die_ref unit_die)
6142{
6143  const char *die_name = get_AT_string (unit_die, DW_AT_name);
6144  const char *base = die_name ? lbasename (die_name) : "anonymous";
6145  char *name = alloca (strlen (base) + 64);
6146  char *p;
6147  int i, mark;
6148  unsigned char checksum[16];
6149  struct md5_ctx ctx;
6150
6151  /* Compute the checksum of the DIE, then append part of it as hex digits to
6152     the name filename of the unit.  */
6153
6154  md5_init_ctx (&ctx);
6155  mark = 0;
6156  die_checksum (unit_die, &ctx, &mark);
6157  unmark_all_dies (unit_die);
6158  md5_finish_ctx (&ctx, checksum);
6159
6160  sprintf (name, "%s.", base);
6161  clean_symbol_name (name);
6162
6163  p = name + strlen (name);
6164  for (i = 0; i < 4; i++)
6165    {
6166      sprintf (p, "%.2x", checksum[i]);
6167      p += 2;
6168    }
6169
6170  comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6171  comdat_symbol_number = 0;
6172}
6173
6174/* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6175
6176static int
6177is_type_die (dw_die_ref die)
6178{
6179  switch (die->die_tag)
6180    {
6181    case DW_TAG_array_type:
6182    case DW_TAG_class_type:
6183    case DW_TAG_enumeration_type:
6184    case DW_TAG_pointer_type:
6185    case DW_TAG_reference_type:
6186    case DW_TAG_string_type:
6187    case DW_TAG_structure_type:
6188    case DW_TAG_subroutine_type:
6189    case DW_TAG_union_type:
6190    case DW_TAG_ptr_to_member_type:
6191    case DW_TAG_set_type:
6192    case DW_TAG_subrange_type:
6193    case DW_TAG_base_type:
6194    case DW_TAG_const_type:
6195    case DW_TAG_file_type:
6196    case DW_TAG_packed_type:
6197    case DW_TAG_volatile_type:
6198    case DW_TAG_typedef:
6199      return 1;
6200    default:
6201      return 0;
6202    }
6203}
6204
6205/* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6206   Basically, we want to choose the bits that are likely to be shared between
6207   compilations (types) and leave out the bits that are specific to individual
6208   compilations (functions).  */
6209
6210static int
6211is_comdat_die (dw_die_ref c)
6212{
6213  /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6214     we do for stabs.  The advantage is a greater likelihood of sharing between
6215     objects that don't include headers in the same order (and therefore would
6216     put the base types in a different comdat).  jason 8/28/00 */
6217
6218  if (c->die_tag == DW_TAG_base_type)
6219    return 0;
6220
6221  if (c->die_tag == DW_TAG_pointer_type
6222      || c->die_tag == DW_TAG_reference_type
6223      || c->die_tag == DW_TAG_const_type
6224      || c->die_tag == DW_TAG_volatile_type)
6225    {
6226      dw_die_ref t = get_AT_ref (c, DW_AT_type);
6227
6228      return t ? is_comdat_die (t) : 0;
6229    }
6230
6231  return is_type_die (c);
6232}
6233
6234/* Returns 1 iff C is the sort of DIE that might be referred to from another
6235   compilation unit.  */
6236
6237static int
6238is_symbol_die (dw_die_ref c)
6239{
6240  return (is_type_die (c)
6241	  || (get_AT (c, DW_AT_declaration)
6242	      && !get_AT (c, DW_AT_specification))
6243	  || c->die_tag == DW_TAG_namespace);
6244}
6245
6246static char *
6247gen_internal_sym (const char *prefix)
6248{
6249  char buf[256];
6250
6251  ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6252  return xstrdup (buf);
6253}
6254
6255/* Assign symbols to all worthy DIEs under DIE.  */
6256
6257static void
6258assign_symbol_names (dw_die_ref die)
6259{
6260  dw_die_ref c;
6261
6262  if (is_symbol_die (die))
6263    {
6264      if (comdat_symbol_id)
6265	{
6266	  char *p = alloca (strlen (comdat_symbol_id) + 64);
6267
6268	  sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6269		   comdat_symbol_id, comdat_symbol_number++);
6270	  die->die_symbol = xstrdup (p);
6271	}
6272      else
6273	die->die_symbol = gen_internal_sym ("LDIE");
6274    }
6275
6276  for (c = die->die_child; c != NULL; c = c->die_sib)
6277    assign_symbol_names (c);
6278}
6279
6280struct cu_hash_table_entry
6281{
6282  dw_die_ref cu;
6283  unsigned min_comdat_num, max_comdat_num;
6284  struct cu_hash_table_entry *next;
6285};
6286
6287/* Routines to manipulate hash table of CUs.  */
6288static hashval_t
6289htab_cu_hash (const void *of)
6290{
6291  const struct cu_hash_table_entry *entry = of;
6292
6293  return htab_hash_string (entry->cu->die_symbol);
6294}
6295
6296static int
6297htab_cu_eq (const void *of1, const void *of2)
6298{
6299  const struct cu_hash_table_entry *entry1 = of1;
6300  const struct die_struct *entry2 = of2;
6301
6302  return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6303}
6304
6305static void
6306htab_cu_del (void *what)
6307{
6308  struct cu_hash_table_entry *next, *entry = what;
6309
6310  while (entry)
6311    {
6312      next = entry->next;
6313      free (entry);
6314      entry = next;
6315    }
6316}
6317
6318/* Check whether we have already seen this CU and set up SYM_NUM
6319   accordingly.  */
6320static int
6321check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6322{
6323  struct cu_hash_table_entry dummy;
6324  struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6325
6326  dummy.max_comdat_num = 0;
6327
6328  slot = (struct cu_hash_table_entry **)
6329    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6330	INSERT);
6331  entry = *slot;
6332
6333  for (; entry; last = entry, entry = entry->next)
6334    {
6335      if (same_die_p_wrap (cu, entry->cu))
6336	break;
6337    }
6338
6339  if (entry)
6340    {
6341      *sym_num = entry->min_comdat_num;
6342      return 1;
6343    }
6344
6345  entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
6346  entry->cu = cu;
6347  entry->min_comdat_num = *sym_num = last->max_comdat_num;
6348  entry->next = *slot;
6349  *slot = entry;
6350
6351  return 0;
6352}
6353
6354/* Record SYM_NUM to record of CU in HTABLE.  */
6355static void
6356record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6357{
6358  struct cu_hash_table_entry **slot, *entry;
6359
6360  slot = (struct cu_hash_table_entry **)
6361    htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6362	NO_INSERT);
6363  entry = *slot;
6364
6365  entry->max_comdat_num = sym_num;
6366}
6367
6368/* Traverse the DIE (which is always comp_unit_die), and set up
6369   additional compilation units for each of the include files we see
6370   bracketed by BINCL/EINCL.  */
6371
6372static void
6373break_out_includes (dw_die_ref die)
6374{
6375  dw_die_ref *ptr;
6376  dw_die_ref unit = NULL;
6377  limbo_die_node *node, **pnode;
6378  htab_t cu_hash_table;
6379
6380  for (ptr = &(die->die_child); *ptr;)
6381    {
6382      dw_die_ref c = *ptr;
6383
6384      if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6385	  || (unit && is_comdat_die (c)))
6386	{
6387	  /* This DIE is for a secondary CU; remove it from the main one.  */
6388	  *ptr = c->die_sib;
6389
6390	  if (c->die_tag == DW_TAG_GNU_BINCL)
6391	    {
6392	      unit = push_new_compile_unit (unit, c);
6393	      free_die (c);
6394	    }
6395	  else if (c->die_tag == DW_TAG_GNU_EINCL)
6396	    {
6397	      unit = pop_compile_unit (unit);
6398	      free_die (c);
6399	    }
6400	  else
6401	    add_child_die (unit, c);
6402	}
6403      else
6404	{
6405	  /* Leave this DIE in the main CU.  */
6406	  ptr = &(c->die_sib);
6407	  continue;
6408	}
6409    }
6410
6411#if 0
6412  /* We can only use this in debugging, since the frontend doesn't check
6413     to make sure that we leave every include file we enter.  */
6414  gcc_assert (!unit);
6415#endif
6416
6417  assign_symbol_names (die);
6418  cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6419  for (node = limbo_die_list, pnode = &limbo_die_list;
6420       node;
6421       node = node->next)
6422    {
6423      int is_dupl;
6424
6425      compute_section_prefix (node->die);
6426      is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6427			&comdat_symbol_number);
6428      assign_symbol_names (node->die);
6429      if (is_dupl)
6430	*pnode = node->next;
6431      else
6432	{
6433	  pnode = &node->next;
6434	  record_comdat_symbol_number (node->die, cu_hash_table,
6435		comdat_symbol_number);
6436	}
6437    }
6438  htab_delete (cu_hash_table);
6439}
6440
6441/* Traverse the DIE and add a sibling attribute if it may have the
6442   effect of speeding up access to siblings.  To save some space,
6443   avoid generating sibling attributes for DIE's without children.  */
6444
6445static void
6446add_sibling_attributes (dw_die_ref die)
6447{
6448  dw_die_ref c;
6449
6450  if (die->die_tag != DW_TAG_compile_unit
6451      && die->die_sib && die->die_child != NULL)
6452    /* Add the sibling link to the front of the attribute list.  */
6453    add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6454
6455  for (c = die->die_child; c != NULL; c = c->die_sib)
6456    add_sibling_attributes (c);
6457}
6458
6459/* Output all location lists for the DIE and its children.  */
6460
6461static void
6462output_location_lists (dw_die_ref die)
6463{
6464  dw_die_ref c;
6465  dw_attr_ref d_attr;
6466
6467  for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6468    if (AT_class (d_attr) == dw_val_class_loc_list)
6469      output_loc_list (AT_loc_list (d_attr));
6470
6471  for (c = die->die_child; c != NULL; c = c->die_sib)
6472    output_location_lists (c);
6473
6474}
6475
6476/* The format of each DIE (and its attribute value pairs) is encoded in an
6477   abbreviation table.  This routine builds the abbreviation table and assigns
6478   a unique abbreviation id for each abbreviation entry.  The children of each
6479   die are visited recursively.  */
6480
6481static void
6482build_abbrev_table (dw_die_ref die)
6483{
6484  unsigned long abbrev_id;
6485  unsigned int n_alloc;
6486  dw_die_ref c;
6487  dw_attr_ref d_attr, a_attr;
6488
6489  /* Scan the DIE references, and mark as external any that refer to
6490     DIEs from other CUs (i.e. those which are not marked).  */
6491  for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6492    if (AT_class (d_attr) == dw_val_class_die_ref
6493	&& AT_ref (d_attr)->die_mark == 0)
6494      {
6495	gcc_assert (AT_ref (d_attr)->die_symbol);
6496
6497	set_AT_ref_external (d_attr, 1);
6498      }
6499
6500  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6501    {
6502      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6503
6504      if (abbrev->die_tag == die->die_tag)
6505	{
6506	  if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6507	    {
6508	      a_attr = abbrev->die_attr;
6509	      d_attr = die->die_attr;
6510
6511	      while (a_attr != NULL && d_attr != NULL)
6512		{
6513		  if ((a_attr->dw_attr != d_attr->dw_attr)
6514		      || (value_format (a_attr) != value_format (d_attr)))
6515		    break;
6516
6517		  a_attr = a_attr->dw_attr_next;
6518		  d_attr = d_attr->dw_attr_next;
6519		}
6520
6521	      if (a_attr == NULL && d_attr == NULL)
6522		break;
6523	    }
6524	}
6525    }
6526
6527  if (abbrev_id >= abbrev_die_table_in_use)
6528    {
6529      if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6530	{
6531	  n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6532	  abbrev_die_table = ggc_realloc (abbrev_die_table,
6533					  sizeof (dw_die_ref) * n_alloc);
6534
6535	  memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6536		 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6537	  abbrev_die_table_allocated = n_alloc;
6538	}
6539
6540      ++abbrev_die_table_in_use;
6541      abbrev_die_table[abbrev_id] = die;
6542    }
6543
6544  die->die_abbrev = abbrev_id;
6545  for (c = die->die_child; c != NULL; c = c->die_sib)
6546    build_abbrev_table (c);
6547}
6548
6549/* Return the power-of-two number of bytes necessary to represent VALUE.  */
6550
6551static int
6552constant_size (long unsigned int value)
6553{
6554  int log;
6555
6556  if (value == 0)
6557    log = 0;
6558  else
6559    log = floor_log2 (value);
6560
6561  log = log / 8;
6562  log = 1 << (floor_log2 (log) + 1);
6563
6564  return log;
6565}
6566
6567/* Return the size of a DIE as it is represented in the
6568   .debug_info section.  */
6569
6570static unsigned long
6571size_of_die (dw_die_ref die)
6572{
6573  unsigned long size = 0;
6574  dw_attr_ref a;
6575
6576  size += size_of_uleb128 (die->die_abbrev);
6577  for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6578    {
6579      switch (AT_class (a))
6580	{
6581	case dw_val_class_addr:
6582	  size += DWARF2_ADDR_SIZE;
6583	  break;
6584	case dw_val_class_offset:
6585	  size += DWARF_OFFSET_SIZE;
6586	  break;
6587	case dw_val_class_loc:
6588	  {
6589	    unsigned long lsize = size_of_locs (AT_loc (a));
6590
6591	    /* Block length.  */
6592	    size += constant_size (lsize);
6593	    size += lsize;
6594	  }
6595	  break;
6596	case dw_val_class_loc_list:
6597	  size += DWARF_OFFSET_SIZE;
6598	  break;
6599	case dw_val_class_range_list:
6600	  size += DWARF_OFFSET_SIZE;
6601	  break;
6602	case dw_val_class_const:
6603	  size += size_of_sleb128 (AT_int (a));
6604	  break;
6605	case dw_val_class_unsigned_const:
6606	  size += constant_size (AT_unsigned (a));
6607	  break;
6608	case dw_val_class_long_long:
6609	  size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6610	  break;
6611	case dw_val_class_vec:
6612	  size += 1 + (a->dw_attr_val.v.val_vec.length
6613		       * a->dw_attr_val.v.val_vec.elt_size); /* block */
6614	  break;
6615	case dw_val_class_flag:
6616	  size += 1;
6617	  break;
6618	case dw_val_class_die_ref:
6619	  if (AT_ref_external (a))
6620	    size += DWARF2_ADDR_SIZE;
6621	  else
6622	    size += DWARF_OFFSET_SIZE;
6623	  break;
6624	case dw_val_class_fde_ref:
6625	  size += DWARF_OFFSET_SIZE;
6626	  break;
6627	case dw_val_class_lbl_id:
6628	  size += DWARF2_ADDR_SIZE;
6629	  break;
6630	case dw_val_class_lbl_offset:
6631	  size += DWARF_OFFSET_SIZE;
6632	  break;
6633	case dw_val_class_str:
6634	  if (AT_string_form (a) == DW_FORM_strp)
6635	    size += DWARF_OFFSET_SIZE;
6636	  else
6637	    size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6638	  break;
6639	default:
6640	  gcc_unreachable ();
6641	}
6642    }
6643
6644  return size;
6645}
6646
6647/* Size the debugging information associated with a given DIE.  Visits the
6648   DIE's children recursively.  Updates the global variable next_die_offset, on
6649   each time through.  Uses the current value of next_die_offset to update the
6650   die_offset field in each DIE.  */
6651
6652static void
6653calc_die_sizes (dw_die_ref die)
6654{
6655  dw_die_ref c;
6656
6657  die->die_offset = next_die_offset;
6658  next_die_offset += size_of_die (die);
6659
6660  for (c = die->die_child; c != NULL; c = c->die_sib)
6661    calc_die_sizes (c);
6662
6663  if (die->die_child != NULL)
6664    /* Count the null byte used to terminate sibling lists.  */
6665    next_die_offset += 1;
6666}
6667
6668/* Set the marks for a die and its children.  We do this so
6669   that we know whether or not a reference needs to use FORM_ref_addr; only
6670   DIEs in the same CU will be marked.  We used to clear out the offset
6671   and use that as the flag, but ran into ordering problems.  */
6672
6673static void
6674mark_dies (dw_die_ref die)
6675{
6676  dw_die_ref c;
6677
6678  gcc_assert (!die->die_mark);
6679
6680  die->die_mark = 1;
6681  for (c = die->die_child; c; c = c->die_sib)
6682    mark_dies (c);
6683}
6684
6685/* Clear the marks for a die and its children.  */
6686
6687static void
6688unmark_dies (dw_die_ref die)
6689{
6690  dw_die_ref c;
6691
6692  gcc_assert (die->die_mark);
6693
6694  die->die_mark = 0;
6695  for (c = die->die_child; c; c = c->die_sib)
6696    unmark_dies (c);
6697}
6698
6699/* Clear the marks for a die, its children and referred dies.  */
6700
6701static void
6702unmark_all_dies (dw_die_ref die)
6703{
6704  dw_die_ref c;
6705  dw_attr_ref a;
6706
6707  if (!die->die_mark)
6708    return;
6709  die->die_mark = 0;
6710
6711  for (c = die->die_child; c; c = c->die_sib)
6712    unmark_all_dies (c);
6713
6714  for (a = die->die_attr; a; a = a->dw_attr_next)
6715    if (AT_class (a) == dw_val_class_die_ref)
6716      unmark_all_dies (AT_ref (a));
6717}
6718
6719/* Return the size of the .debug_pubnames table  generated for the
6720   compilation unit.  */
6721
6722static unsigned long
6723size_of_pubnames (void)
6724{
6725  unsigned long size;
6726  unsigned i;
6727
6728  size = DWARF_PUBNAMES_HEADER_SIZE;
6729  for (i = 0; i < pubname_table_in_use; i++)
6730    {
6731      pubname_ref p = &pubname_table[i];
6732      size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6733    }
6734
6735  size += DWARF_OFFSET_SIZE;
6736  return size;
6737}
6738
6739/* Return the size of the information in the .debug_aranges section.  */
6740
6741static unsigned long
6742size_of_aranges (void)
6743{
6744  unsigned long size;
6745
6746  size = DWARF_ARANGES_HEADER_SIZE;
6747
6748  /* Count the address/length pair for this compilation unit.  */
6749  size += 2 * DWARF2_ADDR_SIZE;
6750  size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6751
6752  /* Count the two zero words used to terminated the address range table.  */
6753  size += 2 * DWARF2_ADDR_SIZE;
6754  return size;
6755}
6756
6757/* Select the encoding of an attribute value.  */
6758
6759static enum dwarf_form
6760value_format (dw_attr_ref a)
6761{
6762  switch (a->dw_attr_val.val_class)
6763    {
6764    case dw_val_class_addr:
6765      return DW_FORM_addr;
6766    case dw_val_class_range_list:
6767    case dw_val_class_offset:
6768      switch (DWARF_OFFSET_SIZE)
6769	{
6770	case 4:
6771	  return DW_FORM_data4;
6772	case 8:
6773	  return DW_FORM_data8;
6774	default:
6775	  gcc_unreachable ();
6776	}
6777    case dw_val_class_loc_list:
6778      /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6779	 .debug_loc section */
6780      return DW_FORM_data4;
6781    case dw_val_class_loc:
6782      switch (constant_size (size_of_locs (AT_loc (a))))
6783	{
6784	case 1:
6785	  return DW_FORM_block1;
6786	case 2:
6787	  return DW_FORM_block2;
6788	default:
6789	  gcc_unreachable ();
6790	}
6791    case dw_val_class_const:
6792      return DW_FORM_sdata;
6793    case dw_val_class_unsigned_const:
6794      switch (constant_size (AT_unsigned (a)))
6795	{
6796	case 1:
6797	  return DW_FORM_data1;
6798	case 2:
6799	  return DW_FORM_data2;
6800	case 4:
6801	  return DW_FORM_data4;
6802	case 8:
6803	  return DW_FORM_data8;
6804	default:
6805	  gcc_unreachable ();
6806	}
6807    case dw_val_class_long_long:
6808      return DW_FORM_block1;
6809    case dw_val_class_vec:
6810      return DW_FORM_block1;
6811    case dw_val_class_flag:
6812      return DW_FORM_flag;
6813    case dw_val_class_die_ref:
6814      if (AT_ref_external (a))
6815	return DW_FORM_ref_addr;
6816      else
6817	return DW_FORM_ref;
6818    case dw_val_class_fde_ref:
6819      return DW_FORM_data;
6820    case dw_val_class_lbl_id:
6821      return DW_FORM_addr;
6822    case dw_val_class_lbl_offset:
6823      return DW_FORM_data;
6824    case dw_val_class_str:
6825      return AT_string_form (a);
6826
6827    default:
6828      gcc_unreachable ();
6829    }
6830}
6831
6832/* Output the encoding of an attribute value.  */
6833
6834static void
6835output_value_format (dw_attr_ref a)
6836{
6837  enum dwarf_form form = value_format (a);
6838
6839  dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6840}
6841
6842/* Output the .debug_abbrev section which defines the DIE abbreviation
6843   table.  */
6844
6845static void
6846output_abbrev_section (void)
6847{
6848  unsigned long abbrev_id;
6849
6850  dw_attr_ref a_attr;
6851
6852  for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6853    {
6854      dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6855
6856      dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6857      dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6858				   dwarf_tag_name (abbrev->die_tag));
6859
6860      if (abbrev->die_child != NULL)
6861	dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6862      else
6863	dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6864
6865      for (a_attr = abbrev->die_attr; a_attr != NULL;
6866	   a_attr = a_attr->dw_attr_next)
6867	{
6868	  dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6869				       dwarf_attr_name (a_attr->dw_attr));
6870	  output_value_format (a_attr);
6871	}
6872
6873      dw2_asm_output_data (1, 0, NULL);
6874      dw2_asm_output_data (1, 0, NULL);
6875    }
6876
6877  /* Terminate the table.  */
6878  dw2_asm_output_data (1, 0, NULL);
6879}
6880
6881/* Output a symbol we can use to refer to this DIE from another CU.  */
6882
6883static inline void
6884output_die_symbol (dw_die_ref die)
6885{
6886  char *sym = die->die_symbol;
6887
6888  if (sym == 0)
6889    return;
6890
6891  if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6892    /* We make these global, not weak; if the target doesn't support
6893       .linkonce, it doesn't support combining the sections, so debugging
6894       will break.  */
6895    targetm.asm_out.globalize_label (asm_out_file, sym);
6896
6897  ASM_OUTPUT_LABEL (asm_out_file, sym);
6898}
6899
6900/* Return a new location list, given the begin and end range, and the
6901   expression. gensym tells us whether to generate a new internal symbol for
6902   this location list node, which is done for the head of the list only.  */
6903
6904static inline dw_loc_list_ref
6905new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6906	      const char *section, unsigned int gensym)
6907{
6908  dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6909
6910  retlist->begin = begin;
6911  retlist->end = end;
6912  retlist->expr = expr;
6913  retlist->section = section;
6914  if (gensym)
6915    retlist->ll_symbol = gen_internal_sym ("LLST");
6916
6917  return retlist;
6918}
6919
6920/* Add a location description expression to a location list.  */
6921
6922static inline void
6923add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6924			   const char *begin, const char *end,
6925			   const char *section)
6926{
6927  dw_loc_list_ref *d;
6928
6929  /* Find the end of the chain.  */
6930  for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6931    ;
6932
6933  /* Add a new location list node to the list.  */
6934  *d = new_loc_list (descr, begin, end, section, 0);
6935}
6936
6937/* Output the location list given to us.  */
6938
6939static void
6940output_loc_list (dw_loc_list_ref list_head)
6941{
6942  dw_loc_list_ref curr = list_head;
6943
6944  ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6945
6946  /* Walk the location list, and output each range + expression.  */
6947  for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6948    {
6949      unsigned long size;
6950      if (!separate_line_info_table_in_use && !have_switched_text_section)
6951	{
6952	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6953				"Location list begin address (%s)",
6954				list_head->ll_symbol);
6955	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6956				"Location list end address (%s)",
6957				list_head->ll_symbol);
6958	}
6959      else
6960	{
6961	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6962			       "Location list begin address (%s)",
6963			       list_head->ll_symbol);
6964	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6965			       "Location list end address (%s)",
6966			       list_head->ll_symbol);
6967	}
6968      size = size_of_locs (curr->expr);
6969
6970      /* Output the block length for this list of location operations.  */
6971      gcc_assert (size <= 0xffff);
6972      dw2_asm_output_data (2, size, "%s", "Location expression size");
6973
6974      output_loc_sequence (curr->expr);
6975    }
6976
6977  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6978		       "Location list terminator begin (%s)",
6979		       list_head->ll_symbol);
6980  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6981		       "Location list terminator end (%s)",
6982		       list_head->ll_symbol);
6983}
6984
6985/* Output the DIE and its attributes.  Called recursively to generate
6986   the definitions of each child DIE.  */
6987
6988static void
6989output_die (dw_die_ref die)
6990{
6991  dw_attr_ref a;
6992  dw_die_ref c;
6993  unsigned long size;
6994
6995  /* If someone in another CU might refer to us, set up a symbol for
6996     them to point to.  */
6997  if (die->die_symbol)
6998    output_die_symbol (die);
6999
7000  dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7001			       die->die_offset, dwarf_tag_name (die->die_tag));
7002
7003  for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
7004    {
7005      const char *name = dwarf_attr_name (a->dw_attr);
7006
7007      switch (AT_class (a))
7008	{
7009	case dw_val_class_addr:
7010	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7011	  break;
7012
7013	case dw_val_class_offset:
7014	  dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7015			       "%s", name);
7016	  break;
7017
7018	case dw_val_class_range_list:
7019	  {
7020	    char *p = strchr (ranges_section_label, '\0');
7021
7022	    sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7023		     a->dw_attr_val.v.val_offset);
7024	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7025				   "%s", name);
7026	    *p = '\0';
7027	  }
7028	  break;
7029
7030	case dw_val_class_loc:
7031	  size = size_of_locs (AT_loc (a));
7032
7033	  /* Output the block length for this list of location operations.  */
7034	  dw2_asm_output_data (constant_size (size), size, "%s", name);
7035
7036	  output_loc_sequence (AT_loc (a));
7037	  break;
7038
7039	case dw_val_class_const:
7040	  /* ??? It would be slightly more efficient to use a scheme like is
7041	     used for unsigned constants below, but gdb 4.x does not sign
7042	     extend.  Gdb 5.x does sign extend.  */
7043	  dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7044	  break;
7045
7046	case dw_val_class_unsigned_const:
7047	  dw2_asm_output_data (constant_size (AT_unsigned (a)),
7048			       AT_unsigned (a), "%s", name);
7049	  break;
7050
7051	case dw_val_class_long_long:
7052	  {
7053	    unsigned HOST_WIDE_INT first, second;
7054
7055	    dw2_asm_output_data (1,
7056				 2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7057				 "%s", name);
7058
7059	    if (WORDS_BIG_ENDIAN)
7060	      {
7061		first = a->dw_attr_val.v.val_long_long.hi;
7062		second = a->dw_attr_val.v.val_long_long.low;
7063	      }
7064	    else
7065	      {
7066		first = a->dw_attr_val.v.val_long_long.low;
7067		second = a->dw_attr_val.v.val_long_long.hi;
7068	      }
7069
7070	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7071				 first, "long long constant");
7072	    dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7073				 second, NULL);
7074	  }
7075	  break;
7076
7077	case dw_val_class_vec:
7078	  {
7079	    unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7080	    unsigned int len = a->dw_attr_val.v.val_vec.length;
7081	    unsigned int i;
7082	    unsigned char *p;
7083
7084	    dw2_asm_output_data (1, len * elt_size, "%s", name);
7085	    if (elt_size > sizeof (HOST_WIDE_INT))
7086	      {
7087		elt_size /= 2;
7088		len *= 2;
7089	      }
7090	    for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7091		 i < len;
7092		 i++, p += elt_size)
7093	      dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7094				   "fp or vector constant word %u", i);
7095	    break;
7096	  }
7097
7098	case dw_val_class_flag:
7099	  dw2_asm_output_data (1, AT_flag (a), "%s", name);
7100	  break;
7101
7102	case dw_val_class_loc_list:
7103	  {
7104	    char *sym = AT_loc_list (a)->ll_symbol;
7105
7106	    gcc_assert (sym);
7107	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
7108	  }
7109	  break;
7110
7111	case dw_val_class_die_ref:
7112	  if (AT_ref_external (a))
7113	    {
7114	      char *sym = AT_ref (a)->die_symbol;
7115
7116	      gcc_assert (sym);
7117	      dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
7118	    }
7119	  else
7120	    {
7121	      gcc_assert (AT_ref (a)->die_offset);
7122	      dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7123				   "%s", name);
7124	    }
7125	  break;
7126
7127	case dw_val_class_fde_ref:
7128	  {
7129	    char l1[20];
7130
7131	    ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7132					 a->dw_attr_val.v.val_fde_index * 2);
7133	    dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
7134	  }
7135	  break;
7136
7137	case dw_val_class_lbl_id:
7138	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7139	  break;
7140
7141	case dw_val_class_lbl_offset:
7142	  dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
7143	  break;
7144
7145	case dw_val_class_str:
7146	  if (AT_string_form (a) == DW_FORM_strp)
7147	    dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7148				   a->dw_attr_val.v.val_str->label,
7149				   "%s: \"%s\"", name, AT_string (a));
7150	  else
7151	    dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7152	  break;
7153
7154	default:
7155	  gcc_unreachable ();
7156	}
7157    }
7158
7159  for (c = die->die_child; c != NULL; c = c->die_sib)
7160    output_die (c);
7161
7162  /* Add null byte to terminate sibling list.  */
7163  if (die->die_child != NULL)
7164    dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7165			 die->die_offset);
7166}
7167
7168/* Output the compilation unit that appears at the beginning of the
7169   .debug_info section, and precedes the DIE descriptions.  */
7170
7171static void
7172output_compilation_unit_header (void)
7173{
7174  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7175    dw2_asm_output_data (4, 0xffffffff,
7176      "Initial length escape value indicating 64-bit DWARF extension");
7177  dw2_asm_output_data (DWARF_OFFSET_SIZE,
7178                       next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7179		       "Length of Compilation Unit Info");
7180  dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7181  dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7182			 "Offset Into Abbrev. Section");
7183  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7184}
7185
7186/* Output the compilation unit DIE and its children.  */
7187
7188static void
7189output_comp_unit (dw_die_ref die, int output_if_empty)
7190{
7191  const char *secname;
7192  char *oldsym, *tmp;
7193
7194  /* Unless we are outputting main CU, we may throw away empty ones.  */
7195  if (!output_if_empty && die->die_child == NULL)
7196    return;
7197
7198  /* Even if there are no children of this DIE, we must output the information
7199     about the compilation unit.  Otherwise, on an empty translation unit, we
7200     will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7201     will then complain when examining the file.  First mark all the DIEs in
7202     this CU so we know which get local refs.  */
7203  mark_dies (die);
7204
7205  build_abbrev_table (die);
7206
7207  /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7208  next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7209  calc_die_sizes (die);
7210
7211  oldsym = die->die_symbol;
7212  if (oldsym)
7213    {
7214      tmp = alloca (strlen (oldsym) + 24);
7215
7216      sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7217      secname = tmp;
7218      die->die_symbol = NULL;
7219    }
7220  else
7221    secname = (const char *) DEBUG_INFO_SECTION;
7222
7223  /* Output debugging information.  */
7224  named_section_flags (secname, SECTION_DEBUG);
7225  output_compilation_unit_header ();
7226  output_die (die);
7227
7228  /* Leave the marks on the main CU, so we can check them in
7229     output_pubnames.  */
7230  if (oldsym)
7231    {
7232      unmark_dies (die);
7233      die->die_symbol = oldsym;
7234    }
7235}
7236
7237/* The DWARF2 pubname for a nested thingy looks like "A::f".  The
7238   output of lang_hooks.decl_printable_name for C++ looks like
7239   "A::f(int)".  Let's drop the argument list, and maybe the scope.  */
7240
7241static const char *
7242dwarf2_name (tree decl, int scope)
7243{
7244  return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7245}
7246
7247/* Add a new entry to .debug_pubnames if appropriate.  */
7248
7249static void
7250add_pubname (tree decl, dw_die_ref die)
7251{
7252  pubname_ref p;
7253
7254  if (! TREE_PUBLIC (decl))
7255    return;
7256
7257  if (pubname_table_in_use == pubname_table_allocated)
7258    {
7259      pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7260      pubname_table
7261	= ggc_realloc (pubname_table,
7262		       (pubname_table_allocated * sizeof (pubname_entry)));
7263      memset (pubname_table + pubname_table_in_use, 0,
7264	      PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7265    }
7266
7267  p = &pubname_table[pubname_table_in_use++];
7268  p->die = die;
7269  p->name = xstrdup (dwarf2_name (decl, 1));
7270}
7271
7272/* Output the public names table used to speed up access to externally
7273   visible names.  For now, only generate entries for externally
7274   visible procedures.  */
7275
7276static void
7277output_pubnames (void)
7278{
7279  unsigned i;
7280  unsigned long pubnames_length = size_of_pubnames ();
7281
7282  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7283    dw2_asm_output_data (4, 0xffffffff,
7284      "Initial length escape value indicating 64-bit DWARF extension");
7285  dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7286		       "Length of Public Names Info");
7287  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7288  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7289			 "Offset of Compilation Unit Info");
7290  dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7291		       "Compilation Unit Length");
7292
7293  for (i = 0; i < pubname_table_in_use; i++)
7294    {
7295      pubname_ref pub = &pubname_table[i];
7296
7297      /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7298      gcc_assert (pub->die->die_mark);
7299
7300      dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7301			   "DIE offset");
7302
7303      dw2_asm_output_nstring (pub->name, -1, "external name");
7304    }
7305
7306  dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7307}
7308
7309/* Add a new entry to .debug_aranges if appropriate.  */
7310
7311static void
7312add_arange (tree decl, dw_die_ref die)
7313{
7314  if (! DECL_SECTION_NAME (decl))
7315    return;
7316
7317  if (arange_table_in_use == arange_table_allocated)
7318    {
7319      arange_table_allocated += ARANGE_TABLE_INCREMENT;
7320      arange_table = ggc_realloc (arange_table,
7321				  (arange_table_allocated
7322				   * sizeof (dw_die_ref)));
7323      memset (arange_table + arange_table_in_use, 0,
7324	      ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7325    }
7326
7327  arange_table[arange_table_in_use++] = die;
7328}
7329
7330/* Output the information that goes into the .debug_aranges table.
7331   Namely, define the beginning and ending address range of the
7332   text section generated for this compilation unit.  */
7333
7334static void
7335output_aranges (void)
7336{
7337  unsigned i;
7338  unsigned long aranges_length = size_of_aranges ();
7339
7340  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7341    dw2_asm_output_data (4, 0xffffffff,
7342      "Initial length escape value indicating 64-bit DWARF extension");
7343  dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7344		       "Length of Address Ranges Info");
7345  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7346  dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7347			 "Offset of Compilation Unit Info");
7348  dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7349  dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7350
7351  /* We need to align to twice the pointer size here.  */
7352  if (DWARF_ARANGES_PAD_SIZE)
7353    {
7354      /* Pad using a 2 byte words so that padding is correct for any
7355	 pointer size.  */
7356      dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7357			   2 * DWARF2_ADDR_SIZE);
7358      for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7359	dw2_asm_output_data (2, 0, NULL);
7360    }
7361
7362  dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7363  dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7364			text_section_label, "Length");
7365  if (flag_reorder_blocks_and_partition)
7366    {
7367      dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7368			   "Address");
7369      dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7370			    cold_text_section_label, "Length");
7371    }
7372
7373  for (i = 0; i < arange_table_in_use; i++)
7374    {
7375      dw_die_ref die = arange_table[i];
7376
7377      /* We shouldn't see aranges for DIEs outside of the main CU.  */
7378      gcc_assert (die->die_mark);
7379
7380      if (die->die_tag == DW_TAG_subprogram)
7381	{
7382	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7383			       "Address");
7384	  dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7385				get_AT_low_pc (die), "Length");
7386	}
7387      else
7388	{
7389	  /* A static variable; extract the symbol from DW_AT_location.
7390	     Note that this code isn't currently hit, as we only emit
7391	     aranges for functions (jason 9/23/99).  */
7392	  dw_attr_ref a = get_AT (die, DW_AT_location);
7393	  dw_loc_descr_ref loc;
7394
7395	  gcc_assert (a && AT_class (a) == dw_val_class_loc);
7396
7397	  loc = AT_loc (a);
7398	  gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7399
7400	  dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7401				   loc->dw_loc_oprnd1.v.val_addr, "Address");
7402	  dw2_asm_output_data (DWARF2_ADDR_SIZE,
7403			       get_AT_unsigned (die, DW_AT_byte_size),
7404			       "Length");
7405	}
7406    }
7407
7408  /* Output the terminator words.  */
7409  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7410  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7411}
7412
7413/* Add a new entry to .debug_ranges.  Return the offset at which it
7414   was placed.  */
7415
7416static unsigned int
7417add_ranges (tree block)
7418{
7419  unsigned int in_use = ranges_table_in_use;
7420
7421  if (in_use == ranges_table_allocated)
7422    {
7423      ranges_table_allocated += RANGES_TABLE_INCREMENT;
7424      ranges_table
7425	= ggc_realloc (ranges_table, (ranges_table_allocated
7426				      * sizeof (struct dw_ranges_struct)));
7427      memset (ranges_table + ranges_table_in_use, 0,
7428	      RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7429    }
7430
7431  ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7432  ranges_table_in_use = in_use + 1;
7433
7434  return in_use * 2 * DWARF2_ADDR_SIZE;
7435}
7436
7437static void
7438output_ranges (void)
7439{
7440  unsigned i;
7441  static const char *const start_fmt = "Offset 0x%x";
7442  const char *fmt = start_fmt;
7443
7444  for (i = 0; i < ranges_table_in_use; i++)
7445    {
7446      int block_num = ranges_table[i].block_num;
7447
7448      if (block_num)
7449	{
7450	  char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7451	  char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7452
7453	  ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7454	  ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7455
7456	  /* If all code is in the text section, then the compilation
7457	     unit base address defaults to DW_AT_low_pc, which is the
7458	     base of the text section.  */
7459	  if (!separate_line_info_table_in_use && !have_switched_text_section)
7460	    {
7461	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7462				    text_section_label,
7463				    fmt, i * 2 * DWARF2_ADDR_SIZE);
7464	      dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7465				    text_section_label, NULL);
7466	    }
7467
7468	  /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7469	     compilation unit base address to zero, which allows us to
7470	     use absolute addresses, and not worry about whether the
7471	     target supports cross-section arithmetic.  */
7472	  else
7473	    {
7474	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7475				   fmt, i * 2 * DWARF2_ADDR_SIZE);
7476	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7477	    }
7478
7479	  fmt = NULL;
7480	}
7481      else
7482	{
7483	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7484	  dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7485	  fmt = start_fmt;
7486	}
7487    }
7488}
7489
7490/* Data structure containing information about input files.  */
7491struct file_info
7492{
7493  char *path;		/* Complete file name.  */
7494  char *fname;		/* File name part.  */
7495  int length;		/* Length of entire string.  */
7496  int file_idx;		/* Index in input file table.  */
7497  int dir_idx;		/* Index in directory table.  */
7498};
7499
7500/* Data structure containing information about directories with source
7501   files.  */
7502struct dir_info
7503{
7504  char *path;		/* Path including directory name.  */
7505  int length;		/* Path length.  */
7506  int prefix;		/* Index of directory entry which is a prefix.  */
7507  int count;		/* Number of files in this directory.  */
7508  int dir_idx;		/* Index of directory used as base.  */
7509  int used;		/* Used in the end?  */
7510};
7511
7512/* Callback function for file_info comparison.  We sort by looking at
7513   the directories in the path.  */
7514
7515static int
7516file_info_cmp (const void *p1, const void *p2)
7517{
7518  const struct file_info *s1 = p1;
7519  const struct file_info *s2 = p2;
7520  unsigned char *cp1;
7521  unsigned char *cp2;
7522
7523  /* Take care of file names without directories.  We need to make sure that
7524     we return consistent values to qsort since some will get confused if
7525     we return the same value when identical operands are passed in opposite
7526     orders.  So if neither has a directory, return 0 and otherwise return
7527     1 or -1 depending on which one has the directory.  */
7528  if ((s1->path == s1->fname || s2->path == s2->fname))
7529    return (s2->path == s2->fname) - (s1->path == s1->fname);
7530
7531  cp1 = (unsigned char *) s1->path;
7532  cp2 = (unsigned char *) s2->path;
7533
7534  while (1)
7535    {
7536      ++cp1;
7537      ++cp2;
7538      /* Reached the end of the first path?  If so, handle like above.  */
7539      if ((cp1 == (unsigned char *) s1->fname)
7540	  || (cp2 == (unsigned char *) s2->fname))
7541	return ((cp2 == (unsigned char *) s2->fname)
7542		- (cp1 == (unsigned char *) s1->fname));
7543
7544      /* Character of current path component the same?  */
7545      else if (*cp1 != *cp2)
7546	return *cp1 - *cp2;
7547    }
7548}
7549
7550/* Output the directory table and the file name table.  We try to minimize
7551   the total amount of memory needed.  A heuristic is used to avoid large
7552   slowdowns with many input files.  */
7553
7554static void
7555output_file_names (void)
7556{
7557  struct file_info *files;
7558  struct dir_info *dirs;
7559  int *saved;
7560  int *savehere;
7561  int *backmap;
7562  size_t ndirs;
7563  int idx_offset;
7564  size_t i;
7565  int idx;
7566
7567  /* Handle the case where file_table is empty.  */
7568  if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7569    {
7570      dw2_asm_output_data (1, 0, "End directory table");
7571      dw2_asm_output_data (1, 0, "End file name table");
7572      return;
7573    }
7574
7575  /* Allocate the various arrays we need.  */
7576  files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7577  dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7578
7579  /* Sort the file names.  */
7580  for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7581    {
7582      char *f;
7583
7584      /* Skip all leading "./".  */
7585      f = VARRAY_CHAR_PTR (file_table, i);
7586      while (f[0] == '.' && f[1] == '/')
7587	f += 2;
7588
7589      /* Create a new array entry.  */
7590      files[i].path = f;
7591      files[i].length = strlen (f);
7592      files[i].file_idx = i;
7593
7594      /* Search for the file name part.  */
7595      f = strrchr (f, '/');
7596      files[i].fname = f == NULL ? files[i].path : f + 1;
7597    }
7598
7599  qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7600	 sizeof (files[0]), file_info_cmp);
7601
7602  /* Find all the different directories used.  */
7603  dirs[0].path = files[1].path;
7604  dirs[0].length = files[1].fname - files[1].path;
7605  dirs[0].prefix = -1;
7606  dirs[0].count = 1;
7607  dirs[0].dir_idx = 0;
7608  dirs[0].used = 0;
7609  files[1].dir_idx = 0;
7610  ndirs = 1;
7611
7612  for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7613    if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7614	&& memcmp (dirs[ndirs - 1].path, files[i].path,
7615		   dirs[ndirs - 1].length) == 0)
7616      {
7617	/* Same directory as last entry.  */
7618	files[i].dir_idx = ndirs - 1;
7619	++dirs[ndirs - 1].count;
7620      }
7621    else
7622      {
7623	size_t j;
7624
7625	/* This is a new directory.  */
7626	dirs[ndirs].path = files[i].path;
7627	dirs[ndirs].length = files[i].fname - files[i].path;
7628	dirs[ndirs].count = 1;
7629	dirs[ndirs].dir_idx = ndirs;
7630	dirs[ndirs].used = 0;
7631	files[i].dir_idx = ndirs;
7632
7633	/* Search for a prefix.  */
7634	dirs[ndirs].prefix = -1;
7635	for (j = 0; j < ndirs; j++)
7636	  if (dirs[j].length < dirs[ndirs].length
7637	      && dirs[j].length > 1
7638	      && (dirs[ndirs].prefix == -1
7639		  || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7640	      && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7641	    dirs[ndirs].prefix = j;
7642
7643	++ndirs;
7644      }
7645
7646  /* Now to the actual work.  We have to find a subset of the directories which
7647     allow expressing the file name using references to the directory table
7648     with the least amount of characters.  We do not do an exhaustive search
7649     where we would have to check out every combination of every single
7650     possible prefix.  Instead we use a heuristic which provides nearly optimal
7651     results in most cases and never is much off.  */
7652  saved = alloca (ndirs * sizeof (int));
7653  savehere = alloca (ndirs * sizeof (int));
7654
7655  memset (saved, '\0', ndirs * sizeof (saved[0]));
7656  for (i = 0; i < ndirs; i++)
7657    {
7658      size_t j;
7659      int total;
7660
7661      /* We can always save some space for the current directory.  But this
7662	 does not mean it will be enough to justify adding the directory.  */
7663      savehere[i] = dirs[i].length;
7664      total = (savehere[i] - saved[i]) * dirs[i].count;
7665
7666      for (j = i + 1; j < ndirs; j++)
7667	{
7668	  savehere[j] = 0;
7669	  if (saved[j] < dirs[i].length)
7670	    {
7671	      /* Determine whether the dirs[i] path is a prefix of the
7672		 dirs[j] path.  */
7673	      int k;
7674
7675	      k = dirs[j].prefix;
7676	      while (k != -1 && k != (int) i)
7677		k = dirs[k].prefix;
7678
7679	      if (k == (int) i)
7680		{
7681		  /* Yes it is.  We can possibly safe some memory but
7682		     writing the filenames in dirs[j] relative to
7683		     dirs[i].  */
7684		  savehere[j] = dirs[i].length;
7685		  total += (savehere[j] - saved[j]) * dirs[j].count;
7686		}
7687	    }
7688	}
7689
7690      /* Check whether we can safe enough to justify adding the dirs[i]
7691	 directory.  */
7692      if (total > dirs[i].length + 1)
7693	{
7694	  /* It's worthwhile adding.  */
7695	  for (j = i; j < ndirs; j++)
7696	    if (savehere[j] > 0)
7697	      {
7698		/* Remember how much we saved for this directory so far.  */
7699		saved[j] = savehere[j];
7700
7701		/* Remember the prefix directory.  */
7702		dirs[j].dir_idx = i;
7703	      }
7704	}
7705    }
7706
7707  /* We have to emit them in the order they appear in the file_table array
7708     since the index is used in the debug info generation.  To do this
7709     efficiently we generate a back-mapping of the indices first.  */
7710  backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7711  for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7712    {
7713      backmap[files[i].file_idx] = i;
7714
7715      /* Mark this directory as used.  */
7716      dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7717    }
7718
7719  /* That was it.  We are ready to emit the information.  First emit the
7720     directory name table.  We have to make sure the first actually emitted
7721     directory name has index one; zero is reserved for the current working
7722     directory.  Make sure we do not confuse these indices with the one for the
7723     constructed table (even though most of the time they are identical).  */
7724  idx = 1;
7725  idx_offset = dirs[0].length > 0 ? 1 : 0;
7726  for (i = 1 - idx_offset; i < ndirs; i++)
7727    if (dirs[i].used != 0)
7728      {
7729	dirs[i].used = idx++;
7730	dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7731				"Directory Entry: 0x%x", dirs[i].used);
7732      }
7733
7734  dw2_asm_output_data (1, 0, "End directory table");
7735
7736  /* Correct the index for the current working directory entry if it
7737     exists.  */
7738  if (idx_offset == 0)
7739    dirs[0].used = 0;
7740
7741  /* Now write all the file names.  */
7742  for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7743    {
7744      int file_idx = backmap[i];
7745      int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7746
7747      dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7748			      "File Entry: 0x%lx", (unsigned long) i);
7749
7750      /* Include directory index.  */
7751      dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7752
7753      /* Modification time.  */
7754      dw2_asm_output_data_uleb128 (0, NULL);
7755
7756      /* File length in bytes.  */
7757      dw2_asm_output_data_uleb128 (0, NULL);
7758    }
7759
7760  dw2_asm_output_data (1, 0, "End file name table");
7761}
7762
7763
7764/* Output the source line number correspondence information.  This
7765   information goes into the .debug_line section.  */
7766
7767static void
7768output_line_info (void)
7769{
7770  char l1[20], l2[20], p1[20], p2[20];
7771  char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7772  char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7773  unsigned opc;
7774  unsigned n_op_args;
7775  unsigned long lt_index;
7776  unsigned long current_line;
7777  long line_offset;
7778  long line_delta;
7779  unsigned long current_file;
7780  unsigned long function;
7781
7782  ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7783  ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7784  ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7785  ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7786
7787  if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7788    dw2_asm_output_data (4, 0xffffffff,
7789      "Initial length escape value indicating 64-bit DWARF extension");
7790  dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7791			"Length of Source Line Info");
7792  ASM_OUTPUT_LABEL (asm_out_file, l1);
7793
7794  dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7795  dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7796  ASM_OUTPUT_LABEL (asm_out_file, p1);
7797
7798  /* Define the architecture-dependent minimum instruction length (in
7799   bytes).  In this implementation of DWARF, this field is used for
7800   information purposes only.  Since GCC generates assembly language,
7801   we have no a priori knowledge of how many instruction bytes are
7802   generated for each source line, and therefore can use only the
7803   DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7804   commands.  Accordingly, we fix this as `1', which is "correct
7805   enough" for all architectures, and don't let the target override.  */
7806  dw2_asm_output_data (1, 1,
7807		       "Minimum Instruction Length");
7808
7809  dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7810		       "Default is_stmt_start flag");
7811  dw2_asm_output_data (1, DWARF_LINE_BASE,
7812		       "Line Base Value (Special Opcodes)");
7813  dw2_asm_output_data (1, DWARF_LINE_RANGE,
7814		       "Line Range Value (Special Opcodes)");
7815  dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7816		       "Special Opcode Base");
7817
7818  for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7819    {
7820      switch (opc)
7821	{
7822	case DW_LNS_advance_pc:
7823	case DW_LNS_advance_line:
7824	case DW_LNS_set_file:
7825	case DW_LNS_set_column:
7826	case DW_LNS_fixed_advance_pc:
7827	  n_op_args = 1;
7828	  break;
7829	default:
7830	  n_op_args = 0;
7831	  break;
7832	}
7833
7834      dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7835			   opc, n_op_args);
7836    }
7837
7838  /* Write out the information about the files we use.  */
7839  output_file_names ();
7840  ASM_OUTPUT_LABEL (asm_out_file, p2);
7841
7842  /* We used to set the address register to the first location in the text
7843     section here, but that didn't accomplish anything since we already
7844     have a line note for the opening brace of the first function.  */
7845
7846  /* Generate the line number to PC correspondence table, encoded as
7847     a series of state machine operations.  */
7848  current_file = 1;
7849  current_line = 1;
7850
7851  if (cfun
7852      && (last_text_section == in_unlikely_executed_text
7853	  || (last_text_section == in_named
7854	      && last_text_section_name == cfun->unlikely_text_section_name)))
7855    strcpy (prev_line_label, cfun->cold_section_label);
7856  else
7857    strcpy (prev_line_label, text_section_label);
7858  for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7859    {
7860      dw_line_info_ref line_info = &line_info_table[lt_index];
7861
7862#if 0
7863      /* Disable this optimization for now; GDB wants to see two line notes
7864	 at the beginning of a function so it can find the end of the
7865	 prologue.  */
7866
7867      /* Don't emit anything for redundant notes.  Just updating the
7868	 address doesn't accomplish anything, because we already assume
7869	 that anything after the last address is this line.  */
7870      if (line_info->dw_line_num == current_line
7871	  && line_info->dw_file_num == current_file)
7872	continue;
7873#endif
7874
7875      /* Emit debug info for the address of the current line.
7876
7877	 Unfortunately, we have little choice here currently, and must always
7878	 use the most general form.  GCC does not know the address delta
7879	 itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
7880	 attributes which will give an upper bound on the address range.  We
7881	 could perhaps use length attributes to determine when it is safe to
7882	 use DW_LNS_fixed_advance_pc.  */
7883
7884      ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7885      if (0)
7886	{
7887	  /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
7888	  dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7889			       "DW_LNS_fixed_advance_pc");
7890	  dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7891	}
7892      else
7893	{
7894	  /* This can handle any delta.  This takes
7895	     4+DWARF2_ADDR_SIZE bytes.  */
7896	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7897	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7898	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7899	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7900	}
7901
7902      strcpy (prev_line_label, line_label);
7903
7904      /* Emit debug info for the source file of the current line, if
7905	 different from the previous line.  */
7906      if (line_info->dw_file_num != current_file)
7907	{
7908	  current_file = line_info->dw_file_num;
7909	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7910	  dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7911				       VARRAY_CHAR_PTR (file_table,
7912							current_file));
7913	}
7914
7915      /* Emit debug info for the current line number, choosing the encoding
7916	 that uses the least amount of space.  */
7917      if (line_info->dw_line_num != current_line)
7918	{
7919	  line_offset = line_info->dw_line_num - current_line;
7920	  line_delta = line_offset - DWARF_LINE_BASE;
7921	  current_line = line_info->dw_line_num;
7922	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7923	    /* This can handle deltas from -10 to 234, using the current
7924	       definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
7925	       takes 1 byte.  */
7926	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7927				 "line %lu", current_line);
7928	  else
7929	    {
7930	      /* This can handle any delta.  This takes at least 4 bytes,
7931		 depending on the value being encoded.  */
7932	      dw2_asm_output_data (1, DW_LNS_advance_line,
7933				   "advance to line %lu", current_line);
7934	      dw2_asm_output_data_sleb128 (line_offset, NULL);
7935	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7936	    }
7937	}
7938      else
7939	/* We still need to start a new row, so output a copy insn.  */
7940	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7941    }
7942
7943  /* Emit debug info for the address of the end of the function.  */
7944  if (0)
7945    {
7946      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7947			   "DW_LNS_fixed_advance_pc");
7948      dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7949    }
7950  else
7951    {
7952      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7953      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7954      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7955      dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7956    }
7957
7958  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7959  dw2_asm_output_data_uleb128 (1, NULL);
7960  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7961
7962  function = 0;
7963  current_file = 1;
7964  current_line = 1;
7965  for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7966    {
7967      dw_separate_line_info_ref line_info
7968	= &separate_line_info_table[lt_index];
7969
7970#if 0
7971      /* Don't emit anything for redundant notes.  */
7972      if (line_info->dw_line_num == current_line
7973	  && line_info->dw_file_num == current_file
7974	  && line_info->function == function)
7975	goto cont;
7976#endif
7977
7978      /* Emit debug info for the address of the current line.  If this is
7979	 a new function, or the first line of a function, then we need
7980	 to handle it differently.  */
7981      ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7982				   lt_index);
7983      if (function != line_info->function)
7984	{
7985	  function = line_info->function;
7986
7987	  /* Set the address register to the first line in the function.  */
7988	  dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7989	  dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7990	  dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7991	  dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7992	}
7993      else
7994	{
7995	  /* ??? See the DW_LNS_advance_pc comment above.  */
7996	  if (0)
7997	    {
7998	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7999				   "DW_LNS_fixed_advance_pc");
8000	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8001	    }
8002	  else
8003	    {
8004	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8005	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8006	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8007	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8008	    }
8009	}
8010
8011      strcpy (prev_line_label, line_label);
8012
8013      /* Emit debug info for the source file of the current line, if
8014	 different from the previous line.  */
8015      if (line_info->dw_file_num != current_file)
8016	{
8017	  current_file = line_info->dw_file_num;
8018	  dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8019	  dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
8020				       VARRAY_CHAR_PTR (file_table,
8021							current_file));
8022	}
8023
8024      /* Emit debug info for the current line number, choosing the encoding
8025	 that uses the least amount of space.  */
8026      if (line_info->dw_line_num != current_line)
8027	{
8028	  line_offset = line_info->dw_line_num - current_line;
8029	  line_delta = line_offset - DWARF_LINE_BASE;
8030	  current_line = line_info->dw_line_num;
8031	  if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8032	    dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8033				 "line %lu", current_line);
8034	  else
8035	    {
8036	      dw2_asm_output_data (1, DW_LNS_advance_line,
8037				   "advance to line %lu", current_line);
8038	      dw2_asm_output_data_sleb128 (line_offset, NULL);
8039	      dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8040	    }
8041	}
8042      else
8043	dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8044
8045#if 0
8046    cont:
8047#endif
8048
8049      lt_index++;
8050
8051      /* If we're done with a function, end its sequence.  */
8052      if (lt_index == separate_line_info_table_in_use
8053	  || separate_line_info_table[lt_index].function != function)
8054	{
8055	  current_file = 1;
8056	  current_line = 1;
8057
8058	  /* Emit debug info for the address of the end of the function.  */
8059	  ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8060	  if (0)
8061	    {
8062	      dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8063				   "DW_LNS_fixed_advance_pc");
8064	      dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8065	    }
8066	  else
8067	    {
8068	      dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8069	      dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8070	      dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8071	      dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8072	    }
8073
8074	  /* Output the marker for the end of this sequence.  */
8075	  dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8076	  dw2_asm_output_data_uleb128 (1, NULL);
8077	  dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8078	}
8079    }
8080
8081  /* Output the marker for the end of the line number info.  */
8082  ASM_OUTPUT_LABEL (asm_out_file, l2);
8083}
8084
8085/* Given a pointer to a tree node for some base type, return a pointer to
8086   a DIE that describes the given type.
8087
8088   This routine must only be called for GCC type nodes that correspond to
8089   Dwarf base (fundamental) types.  */
8090
8091static dw_die_ref
8092base_type_die (tree type)
8093{
8094  dw_die_ref base_type_result;
8095  const char *type_name;
8096  enum dwarf_type encoding;
8097  tree name = TYPE_NAME (type);
8098
8099  if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8100    return 0;
8101
8102  if (name)
8103    {
8104      if (TREE_CODE (name) == TYPE_DECL)
8105	name = DECL_NAME (name);
8106
8107      type_name = IDENTIFIER_POINTER (name);
8108    }
8109  else
8110    type_name = "__unknown__";
8111
8112  switch (TREE_CODE (type))
8113    {
8114    case INTEGER_TYPE:
8115      /* Carefully distinguish the C character types, without messing
8116	 up if the language is not C. Note that we check only for the names
8117	 that contain spaces; other names might occur by coincidence in other
8118	 languages.  */
8119      if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
8120	     && (TYPE_MAIN_VARIANT (type) == char_type_node
8121		 || ! strcmp (type_name, "signed char")
8122		 || ! strcmp (type_name, "unsigned char"))))
8123	{
8124	  if (TYPE_UNSIGNED (type))
8125	    encoding = DW_ATE_unsigned;
8126	  else
8127	    encoding = DW_ATE_signed;
8128	  break;
8129	}
8130      /* else fall through.  */
8131
8132    case CHAR_TYPE:
8133      /* GNU Pascal/Ada CHAR type.  Not used in C.  */
8134      if (TYPE_UNSIGNED (type))
8135	encoding = DW_ATE_unsigned_char;
8136      else
8137	encoding = DW_ATE_signed_char;
8138      break;
8139
8140    case REAL_TYPE:
8141      encoding = DW_ATE_float;
8142      break;
8143
8144      /* Dwarf2 doesn't know anything about complex ints, so use
8145	 a user defined type for it.  */
8146    case COMPLEX_TYPE:
8147      if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8148	encoding = DW_ATE_complex_float;
8149      else
8150	encoding = DW_ATE_lo_user;
8151      break;
8152
8153    case BOOLEAN_TYPE:
8154      /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8155      encoding = DW_ATE_boolean;
8156      break;
8157
8158    default:
8159      /* No other TREE_CODEs are Dwarf fundamental types.  */
8160      gcc_unreachable ();
8161    }
8162
8163  base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8164  if (demangle_name_func)
8165    type_name = (*demangle_name_func) (type_name);
8166
8167  add_AT_string (base_type_result, DW_AT_name, type_name);
8168  add_AT_unsigned (base_type_result, DW_AT_byte_size,
8169		   int_size_in_bytes (type));
8170  add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8171
8172  return base_type_result;
8173}
8174
8175/* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8176   the Dwarf "root" type for the given input type.  The Dwarf "root" type of
8177   a given type is generally the same as the given type, except that if the
8178   given type is a pointer or reference type, then the root type of the given
8179   type is the root type of the "basis" type for the pointer or reference
8180   type.  (This definition of the "root" type is recursive.) Also, the root
8181   type of a `const' qualified type or a `volatile' qualified type is the
8182   root type of the given type without the qualifiers.  */
8183
8184static tree
8185root_type (tree type)
8186{
8187  if (TREE_CODE (type) == ERROR_MARK)
8188    return error_mark_node;
8189
8190  switch (TREE_CODE (type))
8191    {
8192    case ERROR_MARK:
8193      return error_mark_node;
8194
8195    case POINTER_TYPE:
8196    case REFERENCE_TYPE:
8197      return type_main_variant (root_type (TREE_TYPE (type)));
8198
8199    default:
8200      return type_main_variant (type);
8201    }
8202}
8203
8204/* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8205   given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8206
8207static inline int
8208is_base_type (tree type)
8209{
8210  switch (TREE_CODE (type))
8211    {
8212    case ERROR_MARK:
8213    case VOID_TYPE:
8214    case INTEGER_TYPE:
8215    case REAL_TYPE:
8216    case COMPLEX_TYPE:
8217    case BOOLEAN_TYPE:
8218    case CHAR_TYPE:
8219      return 1;
8220
8221    case ARRAY_TYPE:
8222    case RECORD_TYPE:
8223    case UNION_TYPE:
8224    case QUAL_UNION_TYPE:
8225    case ENUMERAL_TYPE:
8226    case FUNCTION_TYPE:
8227    case METHOD_TYPE:
8228    case POINTER_TYPE:
8229    case REFERENCE_TYPE:
8230    case OFFSET_TYPE:
8231    case LANG_TYPE:
8232    case VECTOR_TYPE:
8233      return 0;
8234
8235    default:
8236      gcc_unreachable ();
8237    }
8238
8239  return 0;
8240}
8241
8242/* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8243   node, return the size in bits for the type if it is a constant, or else
8244   return the alignment for the type if the type's size is not constant, or
8245   else return BITS_PER_WORD if the type actually turns out to be an
8246   ERROR_MARK node.  */
8247
8248static inline unsigned HOST_WIDE_INT
8249simple_type_size_in_bits (tree type)
8250{
8251  if (TREE_CODE (type) == ERROR_MARK)
8252    return BITS_PER_WORD;
8253  else if (TYPE_SIZE (type) == NULL_TREE)
8254    return 0;
8255  else if (host_integerp (TYPE_SIZE (type), 1))
8256    return tree_low_cst (TYPE_SIZE (type), 1);
8257  else
8258    return TYPE_ALIGN (type);
8259}
8260
8261/* Return true if the debug information for the given type should be
8262   emitted as a subrange type.  */
8263
8264static inline bool
8265is_subrange_type (tree type)
8266{
8267  tree subtype = TREE_TYPE (type);
8268
8269  /* Subrange types are identified by the fact that they are integer
8270     types, and that they have a subtype which is either an integer type
8271     or an enumeral type.  */
8272
8273  if (TREE_CODE (type) != INTEGER_TYPE
8274      || subtype == NULL_TREE)
8275    return false;
8276
8277  if (TREE_CODE (subtype) != INTEGER_TYPE
8278      && TREE_CODE (subtype) != ENUMERAL_TYPE)
8279    return false;
8280
8281  if (TREE_CODE (type) == TREE_CODE (subtype)
8282      && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8283      && TYPE_MIN_VALUE (type) != NULL
8284      && TYPE_MIN_VALUE (subtype) != NULL
8285      && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8286      && TYPE_MAX_VALUE (type) != NULL
8287      && TYPE_MAX_VALUE (subtype) != NULL
8288      && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8289    {
8290      /* The type and its subtype have the same representation.  If in
8291         addition the two types also have the same name, then the given
8292         type is not a subrange type, but rather a plain base type.  */
8293      /* FIXME: brobecker/2004-03-22:
8294         Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8295         therefore be sufficient to check the TYPE_SIZE node pointers
8296         rather than checking the actual size.  Unfortunately, we have
8297         found some cases, such as in the Ada "integer" type, where
8298         this is not the case.  Until this problem is solved, we need to
8299         keep checking the actual size.  */
8300      tree type_name = TYPE_NAME (type);
8301      tree subtype_name = TYPE_NAME (subtype);
8302
8303      if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8304        type_name = DECL_NAME (type_name);
8305
8306      if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8307        subtype_name = DECL_NAME (subtype_name);
8308
8309      if (type_name == subtype_name)
8310        return false;
8311    }
8312
8313  return true;
8314}
8315
8316/*  Given a pointer to a tree node for a subrange type, return a pointer
8317    to a DIE that describes the given type.  */
8318
8319static dw_die_ref
8320subrange_type_die (tree type, dw_die_ref context_die)
8321{
8322  dw_die_ref subtype_die;
8323  dw_die_ref subrange_die;
8324  tree name = TYPE_NAME (type);
8325  const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8326  tree subtype = TREE_TYPE (type);
8327
8328  if (context_die == NULL)
8329    context_die = comp_unit_die;
8330
8331  if (TREE_CODE (subtype) == ENUMERAL_TYPE)
8332    subtype_die = gen_enumeration_type_die (subtype, context_die);
8333  else
8334    subtype_die = base_type_die (subtype);
8335
8336  subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8337
8338  if (name != NULL)
8339    {
8340      if (TREE_CODE (name) == TYPE_DECL)
8341        name = DECL_NAME (name);
8342      add_name_attribute (subrange_die, IDENTIFIER_POINTER (name));
8343    }
8344
8345  if (int_size_in_bytes (subtype) != size_in_bytes)
8346    {
8347      /* The size of the subrange type and its base type do not match,
8348         so we need to generate a size attribute for the subrange type.  */
8349      add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8350    }
8351
8352  if (TYPE_MIN_VALUE (type) != NULL)
8353    add_bound_info (subrange_die, DW_AT_lower_bound,
8354                    TYPE_MIN_VALUE (type));
8355  if (TYPE_MAX_VALUE (type) != NULL)
8356    add_bound_info (subrange_die, DW_AT_upper_bound,
8357                    TYPE_MAX_VALUE (type));
8358  add_AT_die_ref (subrange_die, DW_AT_type, subtype_die);
8359
8360  return subrange_die;
8361}
8362
8363/* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8364   entry that chains various modifiers in front of the given type.  */
8365
8366static dw_die_ref
8367modified_type_die (tree type, int is_const_type, int is_volatile_type,
8368		   dw_die_ref context_die)
8369{
8370  enum tree_code code = TREE_CODE (type);
8371  dw_die_ref mod_type_die = NULL;
8372  dw_die_ref sub_die = NULL;
8373  tree item_type = NULL;
8374
8375  if (code != ERROR_MARK)
8376    {
8377      tree qualified_type;
8378
8379      /* See if we already have the appropriately qualified variant of
8380	 this type.  */
8381      qualified_type
8382	= get_qualified_type (type,
8383			      ((is_const_type ? TYPE_QUAL_CONST : 0)
8384			       | (is_volatile_type
8385				  ? TYPE_QUAL_VOLATILE : 0)));
8386
8387      /* If we do, then we can just use its DIE, if it exists.  */
8388      if (qualified_type)
8389	{
8390	  mod_type_die = lookup_type_die (qualified_type);
8391	  if (mod_type_die)
8392	    return mod_type_die;
8393	}
8394
8395      /* Handle C typedef types.  */
8396      if (qualified_type && TYPE_NAME (qualified_type)
8397	  && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
8398	  && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
8399	{
8400	  tree type_name = TYPE_NAME (qualified_type);
8401	  tree dtype = TREE_TYPE (type_name);
8402
8403	  if (qualified_type == dtype)
8404	    {
8405	      /* For a named type, use the typedef.  */
8406	      gen_type_die (qualified_type, context_die);
8407	      mod_type_die = lookup_type_die (qualified_type);
8408	    }
8409	  else if (is_const_type < TYPE_READONLY (dtype)
8410		   || is_volatile_type < TYPE_VOLATILE (dtype)
8411		   || (is_const_type <= TYPE_READONLY (dtype)
8412		       && is_volatile_type <= TYPE_VOLATILE (dtype)
8413		       && DECL_ORIGINAL_TYPE (type_name) != type))
8414	    /* cv-unqualified version of named type.  Just use the unnamed
8415	       type to which it refers.  */
8416	    mod_type_die
8417	      = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
8418				   is_const_type, is_volatile_type,
8419				   context_die);
8420
8421	  /* Else cv-qualified version of named type; fall through.  */
8422	}
8423
8424      if (mod_type_die)
8425	/* OK.  */
8426	;
8427      else if (is_const_type)
8428	{
8429	  mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8430	  sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8431	}
8432      else if (is_volatile_type)
8433	{
8434	  mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8435	  sub_die = modified_type_die (type, 0, 0, context_die);
8436	}
8437      else if (code == POINTER_TYPE)
8438	{
8439	  mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8440	  add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8441			   simple_type_size_in_bits (type) / BITS_PER_UNIT);
8442#if 0
8443	  add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8444#endif
8445	  item_type = TREE_TYPE (type);
8446	}
8447      else if (code == REFERENCE_TYPE)
8448	{
8449	  mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8450	  add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8451			   simple_type_size_in_bits (type) / BITS_PER_UNIT);
8452#if 0
8453	  add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
8454#endif
8455	  item_type = TREE_TYPE (type);
8456	}
8457      else if (is_subrange_type (type))
8458        mod_type_die = subrange_type_die (type, context_die);
8459      else if (is_base_type (type))
8460	mod_type_die = base_type_die (type);
8461      else
8462	{
8463	  gen_type_die (type, context_die);
8464
8465	  /* We have to get the type_main_variant here (and pass that to the
8466	     `lookup_type_die' routine) because the ..._TYPE node we have
8467	     might simply be a *copy* of some original type node (where the
8468	     copy was created to help us keep track of typedef names) and
8469	     that copy might have a different TYPE_UID from the original
8470	     ..._TYPE node.  */
8471	  if (TREE_CODE (type) != VECTOR_TYPE)
8472	    mod_type_die = lookup_type_die (type_main_variant (type));
8473	  else
8474	    /* Vectors have the debugging information in the type,
8475	       not the main variant.  */
8476	    mod_type_die = lookup_type_die (type);
8477	  gcc_assert (mod_type_die);
8478	}
8479
8480      /* We want to equate the qualified type to the die below.  */
8481      type = qualified_type;
8482    }
8483
8484  if (type)
8485    equate_type_number_to_die (type, mod_type_die);
8486  if (item_type)
8487    /* We must do this after the equate_type_number_to_die call, in case
8488       this is a recursive type.  This ensures that the modified_type_die
8489       recursion will terminate even if the type is recursive.  Recursive
8490       types are possible in Ada.  */
8491    sub_die = modified_type_die (item_type,
8492				 TYPE_READONLY (item_type),
8493				 TYPE_VOLATILE (item_type),
8494				 context_die);
8495
8496  if (sub_die != NULL)
8497    add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8498
8499  return mod_type_die;
8500}
8501
8502/* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8503   an enumerated type.  */
8504
8505static inline int
8506type_is_enum (tree type)
8507{
8508  return TREE_CODE (type) == ENUMERAL_TYPE;
8509}
8510
8511/* Return the DBX register number described by a given RTL node.  */
8512
8513static unsigned int
8514dbx_reg_number (rtx rtl)
8515{
8516  unsigned regno = REGNO (rtl);
8517
8518  gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8519
8520#ifdef LEAF_REG_REMAP
8521  if (current_function_uses_only_leaf_regs)
8522    {
8523      int leaf_reg = LEAF_REG_REMAP (regno);
8524      if (leaf_reg != -1)
8525	regno = (unsigned) leaf_reg;
8526    }
8527#endif
8528
8529  return DBX_REGISTER_NUMBER (regno);
8530}
8531
8532/* Optionally add a DW_OP_piece term to a location description expression.
8533   DW_OP_piece is only added if the location description expression already
8534   doesn't end with DW_OP_piece.  */
8535
8536static void
8537add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8538{
8539  dw_loc_descr_ref loc;
8540
8541  if (*list_head != NULL)
8542    {
8543      /* Find the end of the chain.  */
8544      for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8545	;
8546
8547      if (loc->dw_loc_opc != DW_OP_piece)
8548	loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8549    }
8550}
8551
8552/* Return a location descriptor that designates a machine register or
8553   zero if there is none.  */
8554
8555static dw_loc_descr_ref
8556reg_loc_descriptor (rtx rtl)
8557{
8558  rtx regs;
8559
8560  if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8561    return 0;
8562
8563  regs = targetm.dwarf_register_span (rtl);
8564
8565  if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8566    return multiple_reg_loc_descriptor (rtl, regs);
8567  else
8568    return one_reg_loc_descriptor (dbx_reg_number (rtl));
8569}
8570
8571/* Return a location descriptor that designates a machine register for
8572   a given hard register number.  */
8573
8574static dw_loc_descr_ref
8575one_reg_loc_descriptor (unsigned int regno)
8576{
8577  if (regno <= 31)
8578    return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8579  else
8580    return new_loc_descr (DW_OP_regx, regno, 0);
8581}
8582
8583/* Given an RTL of a register, return a location descriptor that
8584   designates a value that spans more than one register.  */
8585
8586static dw_loc_descr_ref
8587multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8588{
8589  int nregs, size, i;
8590  unsigned reg;
8591  dw_loc_descr_ref loc_result = NULL;
8592
8593  reg = REGNO (rtl);
8594#ifdef LEAF_REG_REMAP
8595  if (current_function_uses_only_leaf_regs)
8596    {
8597      int leaf_reg = LEAF_REG_REMAP (reg);
8598      if (leaf_reg != -1)
8599	reg = (unsigned) leaf_reg;
8600    }
8601#endif
8602  gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8603  nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8604
8605  /* Simple, contiguous registers.  */
8606  if (regs == NULL_RTX)
8607    {
8608      size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8609
8610      loc_result = NULL;
8611      while (nregs--)
8612	{
8613	  dw_loc_descr_ref t;
8614
8615	  t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8616	  add_loc_descr (&loc_result, t);
8617	  add_loc_descr_op_piece (&loc_result, size);
8618	  ++reg;
8619	}
8620      return loc_result;
8621    }
8622
8623  /* Now onto stupid register sets in non contiguous locations.  */
8624
8625  gcc_assert (GET_CODE (regs) == PARALLEL);
8626
8627  size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8628  loc_result = NULL;
8629
8630  for (i = 0; i < XVECLEN (regs, 0); ++i)
8631    {
8632      dw_loc_descr_ref t;
8633
8634      t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8635      add_loc_descr (&loc_result, t);
8636      size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8637      add_loc_descr_op_piece (&loc_result, size);
8638    }
8639  return loc_result;
8640}
8641
8642/* Return a location descriptor that designates a constant.  */
8643
8644static dw_loc_descr_ref
8645int_loc_descriptor (HOST_WIDE_INT i)
8646{
8647  enum dwarf_location_atom op;
8648
8649  /* Pick the smallest representation of a constant, rather than just
8650     defaulting to the LEB encoding.  */
8651  if (i >= 0)
8652    {
8653      if (i <= 31)
8654	op = DW_OP_lit0 + i;
8655      else if (i <= 0xff)
8656	op = DW_OP_const1u;
8657      else if (i <= 0xffff)
8658	op = DW_OP_const2u;
8659      else if (HOST_BITS_PER_WIDE_INT == 32
8660	       || i <= 0xffffffff)
8661	op = DW_OP_const4u;
8662      else
8663	op = DW_OP_constu;
8664    }
8665  else
8666    {
8667      if (i >= -0x80)
8668	op = DW_OP_const1s;
8669      else if (i >= -0x8000)
8670	op = DW_OP_const2s;
8671      else if (HOST_BITS_PER_WIDE_INT == 32
8672	       || i >= -0x80000000)
8673	op = DW_OP_const4s;
8674      else
8675	op = DW_OP_consts;
8676    }
8677
8678  return new_loc_descr (op, i, 0);
8679}
8680
8681/* Return a location descriptor that designates a base+offset location.  */
8682
8683static dw_loc_descr_ref
8684based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8685{
8686  unsigned int regno;
8687
8688  /* We only use "frame base" when we're sure we're talking about the
8689     post-prologue local stack frame.  We do this by *not* running
8690     register elimination until this point, and recognizing the special
8691     argument pointer and soft frame pointer rtx's.  */
8692  if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8693    {
8694      rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8695
8696      if (elim != reg)
8697	{
8698	  if (GET_CODE (elim) == PLUS)
8699	    {
8700	      offset += INTVAL (XEXP (elim, 1));
8701	      elim = XEXP (elim, 0);
8702	    }
8703	  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8704		      : stack_pointer_rtx));
8705          offset += frame_pointer_cfa_offset;
8706
8707          return new_loc_descr (DW_OP_fbreg, offset, 0);
8708	}
8709    }
8710
8711  regno = dbx_reg_number (reg);
8712  if (regno <= 31)
8713    return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8714  else
8715    return new_loc_descr (DW_OP_bregx, regno, offset);
8716}
8717
8718/* Return true if this RTL expression describes a base+offset calculation.  */
8719
8720static inline int
8721is_based_loc (rtx rtl)
8722{
8723  return (GET_CODE (rtl) == PLUS
8724	  && ((REG_P (XEXP (rtl, 0))
8725	       && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8726	       && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8727}
8728
8729/* The following routine converts the RTL for a variable or parameter
8730   (resident in memory) into an equivalent Dwarf representation of a
8731   mechanism for getting the address of that same variable onto the top of a
8732   hypothetical "address evaluation" stack.
8733
8734   When creating memory location descriptors, we are effectively transforming
8735   the RTL for a memory-resident object into its Dwarf postfix expression
8736   equivalent.  This routine recursively descends an RTL tree, turning
8737   it into Dwarf postfix code as it goes.
8738
8739   MODE is the mode of the memory reference, needed to handle some
8740   autoincrement addressing modes.
8741
8742   CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8743   location list for RTL.
8744
8745   Return 0 if we can't represent the location.  */
8746
8747static dw_loc_descr_ref
8748mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8749{
8750  dw_loc_descr_ref mem_loc_result = NULL;
8751  enum dwarf_location_atom op;
8752
8753  /* Note that for a dynamically sized array, the location we will generate a
8754     description of here will be the lowest numbered location which is
8755     actually within the array.  That's *not* necessarily the same as the
8756     zeroth element of the array.  */
8757
8758  rtl = targetm.delegitimize_address (rtl);
8759
8760  switch (GET_CODE (rtl))
8761    {
8762    case POST_INC:
8763    case POST_DEC:
8764    case POST_MODIFY:
8765      /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
8766	 just fall into the SUBREG code.  */
8767
8768      /* ... fall through ...  */
8769
8770    case SUBREG:
8771      /* The case of a subreg may arise when we have a local (register)
8772	 variable or a formal (register) parameter which doesn't quite fill
8773	 up an entire register.  For now, just assume that it is
8774	 legitimate to make the Dwarf info refer to the whole register which
8775	 contains the given subreg.  */
8776      rtl = XEXP (rtl, 0);
8777
8778      /* ... fall through ...  */
8779
8780    case REG:
8781      /* Whenever a register number forms a part of the description of the
8782	 method for calculating the (dynamic) address of a memory resident
8783	 object, DWARF rules require the register number be referred to as
8784	 a "base register".  This distinction is not based in any way upon
8785	 what category of register the hardware believes the given register
8786	 belongs to.  This is strictly DWARF terminology we're dealing with
8787	 here. Note that in cases where the location of a memory-resident
8788	 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8789	 OP_CONST (0)) the actual DWARF location descriptor that we generate
8790	 may just be OP_BASEREG (basereg).  This may look deceptively like
8791	 the object in question was allocated to a register (rather than in
8792	 memory) so DWARF consumers need to be aware of the subtle
8793	 distinction between OP_REG and OP_BASEREG.  */
8794      if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8795	mem_loc_result = based_loc_descr (rtl, 0);
8796      break;
8797
8798    case MEM:
8799      mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8800      if (mem_loc_result != 0)
8801	add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8802      break;
8803
8804    case LO_SUM:
8805	 rtl = XEXP (rtl, 1);
8806
8807      /* ... fall through ...  */
8808
8809    case LABEL_REF:
8810      /* Some ports can transform a symbol ref into a label ref, because
8811	 the symbol ref is too far away and has to be dumped into a constant
8812	 pool.  */
8813    case CONST:
8814    case SYMBOL_REF:
8815      /* Alternatively, the symbol in the constant pool might be referenced
8816	 by a different symbol.  */
8817      if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8818	{
8819	  bool marked;
8820	  rtx tmp = get_pool_constant_mark (rtl, &marked);
8821
8822	  if (GET_CODE (tmp) == SYMBOL_REF)
8823	    {
8824	      rtl = tmp;
8825	      if (CONSTANT_POOL_ADDRESS_P (tmp))
8826		get_pool_constant_mark (tmp, &marked);
8827	      else
8828		marked = true;
8829	    }
8830
8831	  /* If all references to this pool constant were optimized away,
8832	     it was not output and thus we can't represent it.
8833	     FIXME: might try to use DW_OP_const_value here, though
8834	     DW_OP_piece complicates it.  */
8835	  if (!marked)
8836	    return 0;
8837	}
8838
8839      mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8840      mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8841      mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8842      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8843      break;
8844
8845    case PRE_MODIFY:
8846      /* Extract the PLUS expression nested inside and fall into
8847	 PLUS code below.  */
8848      rtl = XEXP (rtl, 1);
8849      goto plus;
8850
8851    case PRE_INC:
8852    case PRE_DEC:
8853      /* Turn these into a PLUS expression and fall into the PLUS code
8854	 below.  */
8855      rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8856			  GEN_INT (GET_CODE (rtl) == PRE_INC
8857				   ? GET_MODE_UNIT_SIZE (mode)
8858				   : -GET_MODE_UNIT_SIZE (mode)));
8859
8860      /* ... fall through ...  */
8861
8862    case PLUS:
8863    plus:
8864      if (is_based_loc (rtl))
8865	mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8866					  INTVAL (XEXP (rtl, 1)));
8867      else
8868	{
8869	  mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8870	  if (mem_loc_result == 0)
8871	    break;
8872
8873	  if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8874	      && INTVAL (XEXP (rtl, 1)) >= 0)
8875	    add_loc_descr (&mem_loc_result,
8876			   new_loc_descr (DW_OP_plus_uconst,
8877					  INTVAL (XEXP (rtl, 1)), 0));
8878	  else
8879	    {
8880	      add_loc_descr (&mem_loc_result,
8881			     mem_loc_descriptor (XEXP (rtl, 1), mode));
8882	      add_loc_descr (&mem_loc_result,
8883			     new_loc_descr (DW_OP_plus, 0, 0));
8884	    }
8885	}
8886      break;
8887
8888    /* If a pseudo-reg is optimized away, it is possible for it to
8889       be replaced with a MEM containing a multiply or shift.  */
8890    case MULT:
8891      op = DW_OP_mul;
8892      goto do_binop;
8893
8894    case ASHIFT:
8895      op = DW_OP_shl;
8896      goto do_binop;
8897
8898    case ASHIFTRT:
8899      op = DW_OP_shra;
8900      goto do_binop;
8901
8902    case LSHIFTRT:
8903      op = DW_OP_shr;
8904      goto do_binop;
8905
8906    do_binop:
8907      {
8908	dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8909	dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8910
8911	if (op0 == 0 || op1 == 0)
8912	  break;
8913
8914	mem_loc_result = op0;
8915	add_loc_descr (&mem_loc_result, op1);
8916	add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8917	break;
8918      }
8919
8920    case CONST_INT:
8921      mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8922      break;
8923
8924    default:
8925      gcc_unreachable ();
8926    }
8927
8928  return mem_loc_result;
8929}
8930
8931/* Return a descriptor that describes the concatenation of two locations.
8932   This is typically a complex variable.  */
8933
8934static dw_loc_descr_ref
8935concat_loc_descriptor (rtx x0, rtx x1)
8936{
8937  dw_loc_descr_ref cc_loc_result = NULL;
8938  dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8939  dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8940
8941  if (x0_ref == 0 || x1_ref == 0)
8942    return 0;
8943
8944  cc_loc_result = x0_ref;
8945  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8946
8947  add_loc_descr (&cc_loc_result, x1_ref);
8948  add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8949
8950  return cc_loc_result;
8951}
8952
8953/* Output a proper Dwarf location descriptor for a variable or parameter
8954   which is either allocated in a register or in a memory location.  For a
8955   register, we just generate an OP_REG and the register number.  For a
8956   memory location we provide a Dwarf postfix expression describing how to
8957   generate the (dynamic) address of the object onto the address stack.
8958
8959   If we don't know how to describe it, return 0.  */
8960
8961static dw_loc_descr_ref
8962loc_descriptor (rtx rtl)
8963{
8964  dw_loc_descr_ref loc_result = NULL;
8965
8966  switch (GET_CODE (rtl))
8967    {
8968    case SUBREG:
8969      /* The case of a subreg may arise when we have a local (register)
8970	 variable or a formal (register) parameter which doesn't quite fill
8971	 up an entire register.  For now, just assume that it is
8972	 legitimate to make the Dwarf info refer to the whole register which
8973	 contains the given subreg.  */
8974      rtl = SUBREG_REG (rtl);
8975
8976      /* ... fall through ...  */
8977
8978    case REG:
8979      loc_result = reg_loc_descriptor (rtl);
8980      break;
8981
8982    case MEM:
8983      loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8984      break;
8985
8986    case CONCAT:
8987      loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8988      break;
8989
8990    case VAR_LOCATION:
8991      /* Single part.  */
8992      if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8993	{
8994	  loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
8995	  break;
8996	}
8997
8998      rtl = XEXP (rtl, 1);
8999      /* FALLTHRU */
9000
9001    case PARALLEL:
9002      {
9003	rtvec par_elems = XVEC (rtl, 0);
9004	int num_elem = GET_NUM_ELEM (par_elems);
9005	enum machine_mode mode;
9006	int i;
9007
9008	/* Create the first one, so we have something to add to.  */
9009	loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9010	mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9011	add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9012	for (i = 1; i < num_elem; i++)
9013	  {
9014	    dw_loc_descr_ref temp;
9015
9016	    temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9017	    add_loc_descr (&loc_result, temp);
9018	    mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9019	    add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9020	  }
9021      }
9022      break;
9023
9024    default:
9025      gcc_unreachable ();
9026    }
9027
9028  return loc_result;
9029}
9030
9031/* Similar, but generate the descriptor from trees instead of rtl.  This comes
9032   up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9033   a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9034   top-level invocation, and we require the address of LOC; is 0 if we require
9035   the value of LOC.  */
9036
9037static dw_loc_descr_ref
9038loc_descriptor_from_tree_1 (tree loc, int want_address)
9039{
9040  dw_loc_descr_ref ret, ret1;
9041  int have_address = 0;
9042  int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9043  enum dwarf_location_atom op;
9044
9045  /* ??? Most of the time we do not take proper care for sign/zero
9046     extending the values properly.  Hopefully this won't be a real
9047     problem...  */
9048
9049  switch (TREE_CODE (loc))
9050    {
9051    case ERROR_MARK:
9052      return 0;
9053
9054    case PLACEHOLDER_EXPR:
9055      /* This case involves extracting fields from an object to determine the
9056	 position of other fields.  We don't try to encode this here.  The
9057	 only user of this is Ada, which encodes the needed information using
9058	 the names of types.  */
9059      return 0;
9060
9061    case CALL_EXPR:
9062      return 0;
9063
9064    case PREINCREMENT_EXPR:
9065    case PREDECREMENT_EXPR:
9066    case POSTINCREMENT_EXPR:
9067    case POSTDECREMENT_EXPR:
9068      /* There are no opcodes for these operations.  */
9069      return 0;
9070
9071    case ADDR_EXPR:
9072      /* If we already want an address, there's nothing we can do.  */
9073      if (want_address)
9074	return 0;
9075
9076      /* Otherwise, process the argument and look for the address.  */
9077      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9078
9079    case VAR_DECL:
9080      if (DECL_THREAD_LOCAL_P (loc))
9081	{
9082	  rtx rtl;
9083
9084	  /* If this is not defined, we have no way to emit the data.  */
9085	  if (!targetm.asm_out.output_dwarf_dtprel)
9086	    return 0;
9087
9088	  /* The way DW_OP_GNU_push_tls_address is specified, we can only
9089	     look up addresses of objects in the current module.  */
9090	  if (DECL_EXTERNAL (loc))
9091	    return 0;
9092
9093	  rtl = rtl_for_decl_location (loc);
9094	  if (rtl == NULL_RTX)
9095	    return 0;
9096
9097	  if (!MEM_P (rtl))
9098	    return 0;
9099	  rtl = XEXP (rtl, 0);
9100	  if (! CONSTANT_P (rtl))
9101	    return 0;
9102
9103	  ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9104	  ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9105	  ret->dw_loc_oprnd1.v.val_addr = rtl;
9106
9107	  ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9108	  add_loc_descr (&ret, ret1);
9109
9110	  have_address = 1;
9111	  break;
9112	}
9113      /* FALLTHRU */
9114
9115    case PARM_DECL:
9116      if (DECL_HAS_VALUE_EXPR_P (loc))
9117	return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9118					   want_address);
9119      /* FALLTHRU */
9120
9121    case RESULT_DECL:
9122    case FUNCTION_DECL:
9123      {
9124	rtx rtl = rtl_for_decl_location (loc);
9125
9126	if (rtl == NULL_RTX)
9127	  return 0;
9128        else if (GET_CODE (rtl) == CONST_INT)
9129	  {
9130	    HOST_WIDE_INT val = INTVAL (rtl);
9131	    if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9132	      val &= GET_MODE_MASK (DECL_MODE (loc));
9133	    ret = int_loc_descriptor (val);
9134	  }
9135	else if (GET_CODE (rtl) == CONST_STRING)
9136	  return 0;
9137	else if (CONSTANT_P (rtl))
9138	  {
9139	    ret = new_loc_descr (DW_OP_addr, 0, 0);
9140	    ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9141	    ret->dw_loc_oprnd1.v.val_addr = rtl;
9142	  }
9143	else
9144	  {
9145	    enum machine_mode mode;
9146
9147	    /* Certain constructs can only be represented at top-level.  */
9148	    if (want_address == 2)
9149	      return loc_descriptor (rtl);
9150
9151	    mode = GET_MODE (rtl);
9152	    if (MEM_P (rtl))
9153	      {
9154		rtl = XEXP (rtl, 0);
9155		have_address = 1;
9156	      }
9157	    ret = mem_loc_descriptor (rtl, mode);
9158	  }
9159      }
9160      break;
9161
9162    case INDIRECT_REF:
9163      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9164      have_address = 1;
9165      break;
9166
9167    case COMPOUND_EXPR:
9168      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9169
9170    case NOP_EXPR:
9171    case CONVERT_EXPR:
9172    case NON_LVALUE_EXPR:
9173    case VIEW_CONVERT_EXPR:
9174    case SAVE_EXPR:
9175    case MODIFY_EXPR:
9176      return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9177
9178    case COMPONENT_REF:
9179    case BIT_FIELD_REF:
9180    case ARRAY_REF:
9181    case ARRAY_RANGE_REF:
9182      {
9183	tree obj, offset;
9184	HOST_WIDE_INT bitsize, bitpos, bytepos;
9185	enum machine_mode mode;
9186	int volatilep;
9187
9188	obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9189				   &unsignedp, &volatilep, false);
9190
9191	if (obj == loc)
9192	  return 0;
9193
9194	ret = loc_descriptor_from_tree_1 (obj, 1);
9195	if (ret == 0
9196	    || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9197	  return 0;
9198
9199	if (offset != NULL_TREE)
9200	  {
9201	    /* Variable offset.  */
9202	    add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9203	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9204	  }
9205
9206	bytepos = bitpos / BITS_PER_UNIT;
9207	if (bytepos > 0)
9208	  add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9209	else if (bytepos < 0)
9210	  {
9211	    add_loc_descr (&ret, int_loc_descriptor (bytepos));
9212	    add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9213	  }
9214
9215	have_address = 1;
9216	break;
9217      }
9218
9219    case INTEGER_CST:
9220      if (host_integerp (loc, 0))
9221	ret = int_loc_descriptor (tree_low_cst (loc, 0));
9222      else
9223	return 0;
9224      break;
9225
9226    case CONSTRUCTOR:
9227      {
9228	/* Get an RTL for this, if something has been emitted.  */
9229	rtx rtl = lookup_constant_def (loc);
9230	enum machine_mode mode;
9231
9232	if (!rtl || !MEM_P (rtl))
9233	  return 0;
9234	mode = GET_MODE (rtl);
9235	rtl = XEXP (rtl, 0);
9236	ret = mem_loc_descriptor (rtl, mode);
9237	have_address = 1;
9238	break;
9239      }
9240
9241    case TRUTH_AND_EXPR:
9242    case TRUTH_ANDIF_EXPR:
9243    case BIT_AND_EXPR:
9244      op = DW_OP_and;
9245      goto do_binop;
9246
9247    case TRUTH_XOR_EXPR:
9248    case BIT_XOR_EXPR:
9249      op = DW_OP_xor;
9250      goto do_binop;
9251
9252    case TRUTH_OR_EXPR:
9253    case TRUTH_ORIF_EXPR:
9254    case BIT_IOR_EXPR:
9255      op = DW_OP_or;
9256      goto do_binop;
9257
9258    case FLOOR_DIV_EXPR:
9259    case CEIL_DIV_EXPR:
9260    case ROUND_DIV_EXPR:
9261    case TRUNC_DIV_EXPR:
9262      op = DW_OP_div;
9263      goto do_binop;
9264
9265    case MINUS_EXPR:
9266      op = DW_OP_minus;
9267      goto do_binop;
9268
9269    case FLOOR_MOD_EXPR:
9270    case CEIL_MOD_EXPR:
9271    case ROUND_MOD_EXPR:
9272    case TRUNC_MOD_EXPR:
9273      op = DW_OP_mod;
9274      goto do_binop;
9275
9276    case MULT_EXPR:
9277      op = DW_OP_mul;
9278      goto do_binop;
9279
9280    case LSHIFT_EXPR:
9281      op = DW_OP_shl;
9282      goto do_binop;
9283
9284    case RSHIFT_EXPR:
9285      op = (unsignedp ? DW_OP_shr : DW_OP_shra);
9286      goto do_binop;
9287
9288    case PLUS_EXPR:
9289      if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9290	  && host_integerp (TREE_OPERAND (loc, 1), 0))
9291	{
9292	  ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9293	  if (ret == 0)
9294	    return 0;
9295
9296	  add_loc_descr (&ret,
9297			 new_loc_descr (DW_OP_plus_uconst,
9298					tree_low_cst (TREE_OPERAND (loc, 1),
9299						      0),
9300					0));
9301	  break;
9302	}
9303
9304      op = DW_OP_plus;
9305      goto do_binop;
9306
9307    case LE_EXPR:
9308      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9309	return 0;
9310
9311      op = DW_OP_le;
9312      goto do_binop;
9313
9314    case GE_EXPR:
9315      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9316	return 0;
9317
9318      op = DW_OP_ge;
9319      goto do_binop;
9320
9321    case LT_EXPR:
9322      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9323	return 0;
9324
9325      op = DW_OP_lt;
9326      goto do_binop;
9327
9328    case GT_EXPR:
9329      if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9330	return 0;
9331
9332      op = DW_OP_gt;
9333      goto do_binop;
9334
9335    case EQ_EXPR:
9336      op = DW_OP_eq;
9337      goto do_binop;
9338
9339    case NE_EXPR:
9340      op = DW_OP_ne;
9341      goto do_binop;
9342
9343    do_binop:
9344      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9345      ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9346      if (ret == 0 || ret1 == 0)
9347	return 0;
9348
9349      add_loc_descr (&ret, ret1);
9350      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9351      break;
9352
9353    case TRUTH_NOT_EXPR:
9354    case BIT_NOT_EXPR:
9355      op = DW_OP_not;
9356      goto do_unop;
9357
9358    case ABS_EXPR:
9359      op = DW_OP_abs;
9360      goto do_unop;
9361
9362    case NEGATE_EXPR:
9363      op = DW_OP_neg;
9364      goto do_unop;
9365
9366    do_unop:
9367      ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9368      if (ret == 0)
9369	return 0;
9370
9371      add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9372      break;
9373
9374    case MIN_EXPR:
9375    case MAX_EXPR:
9376      {
9377        const enum tree_code code =
9378          TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9379
9380        loc = build3 (COND_EXPR, TREE_TYPE (loc),
9381		      build2 (code, integer_type_node,
9382			      TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9383                      TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9384      }
9385
9386      /* ... fall through ...  */
9387
9388    case COND_EXPR:
9389      {
9390	dw_loc_descr_ref lhs
9391	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9392	dw_loc_descr_ref rhs
9393	  = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9394	dw_loc_descr_ref bra_node, jump_node, tmp;
9395
9396	ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9397	if (ret == 0 || lhs == 0 || rhs == 0)
9398	  return 0;
9399
9400	bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9401	add_loc_descr (&ret, bra_node);
9402
9403	add_loc_descr (&ret, rhs);
9404	jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9405	add_loc_descr (&ret, jump_node);
9406
9407	add_loc_descr (&ret, lhs);
9408	bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9409	bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9410
9411	/* ??? Need a node to point the skip at.  Use a nop.  */
9412	tmp = new_loc_descr (DW_OP_nop, 0, 0);
9413	add_loc_descr (&ret, tmp);
9414	jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9415	jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9416      }
9417      break;
9418
9419    case FIX_TRUNC_EXPR:
9420    case FIX_CEIL_EXPR:
9421    case FIX_FLOOR_EXPR:
9422    case FIX_ROUND_EXPR:
9423      return 0;
9424
9425    default:
9426      /* Leave front-end specific codes as simply unknown.  This comes
9427	 up, for instance, with the C STMT_EXPR.  */
9428      if ((unsigned int) TREE_CODE (loc)
9429          >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9430	return 0;
9431
9432#ifdef ENABLE_CHECKING
9433      /* Otherwise this is a generic code; we should just lists all of
9434	 these explicitly.  We forgot one.  */
9435      gcc_unreachable ();
9436#else
9437      /* In a release build, we want to degrade gracefully: better to
9438	 generate incomplete debugging information than to crash.  */
9439      return NULL;
9440#endif
9441    }
9442
9443  /* Show if we can't fill the request for an address.  */
9444  if (want_address && !have_address)
9445    return 0;
9446
9447  /* If we've got an address and don't want one, dereference.  */
9448  if (!want_address && have_address && ret)
9449    {
9450      HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9451
9452      if (size > DWARF2_ADDR_SIZE || size == -1)
9453	return 0;
9454      else if (size == DWARF2_ADDR_SIZE)
9455	op = DW_OP_deref;
9456      else
9457	op = DW_OP_deref_size;
9458
9459      add_loc_descr (&ret, new_loc_descr (op, size, 0));
9460    }
9461
9462  return ret;
9463}
9464
9465static inline dw_loc_descr_ref
9466loc_descriptor_from_tree (tree loc)
9467{
9468  return loc_descriptor_from_tree_1 (loc, 2);
9469}
9470
9471/* Given a value, round it up to the lowest multiple of `boundary'
9472   which is not less than the value itself.  */
9473
9474static inline HOST_WIDE_INT
9475ceiling (HOST_WIDE_INT value, unsigned int boundary)
9476{
9477  return (((value + boundary - 1) / boundary) * boundary);
9478}
9479
9480/* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9481   pointer to the declared type for the relevant field variable, or return
9482   `integer_type_node' if the given node turns out to be an
9483   ERROR_MARK node.  */
9484
9485static inline tree
9486field_type (tree decl)
9487{
9488  tree type;
9489
9490  if (TREE_CODE (decl) == ERROR_MARK)
9491    return integer_type_node;
9492
9493  type = DECL_BIT_FIELD_TYPE (decl);
9494  if (type == NULL_TREE)
9495    type = TREE_TYPE (decl);
9496
9497  return type;
9498}
9499
9500/* Given a pointer to a tree node, return the alignment in bits for
9501   it, or else return BITS_PER_WORD if the node actually turns out to
9502   be an ERROR_MARK node.  */
9503
9504static inline unsigned
9505simple_type_align_in_bits (tree type)
9506{
9507  return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9508}
9509
9510static inline unsigned
9511simple_decl_align_in_bits (tree decl)
9512{
9513  return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9514}
9515
9516/* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9517   lowest addressed byte of the "containing object" for the given FIELD_DECL,
9518   or return 0 if we are unable to determine what that offset is, either
9519   because the argument turns out to be a pointer to an ERROR_MARK node, or
9520   because the offset is actually variable.  (We can't handle the latter case
9521   just yet).  */
9522
9523static HOST_WIDE_INT
9524field_byte_offset (tree decl)
9525{
9526  unsigned int type_align_in_bits;
9527  unsigned int decl_align_in_bits;
9528  unsigned HOST_WIDE_INT type_size_in_bits;
9529  HOST_WIDE_INT object_offset_in_bits;
9530  tree type;
9531  tree field_size_tree;
9532  HOST_WIDE_INT bitpos_int;
9533  HOST_WIDE_INT deepest_bitpos;
9534  unsigned HOST_WIDE_INT field_size_in_bits;
9535
9536  if (TREE_CODE (decl) == ERROR_MARK)
9537    return 0;
9538
9539  gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9540
9541  type = field_type (decl);
9542  field_size_tree = DECL_SIZE (decl);
9543
9544  /* The size could be unspecified if there was an error, or for
9545     a flexible array member.  */
9546  if (! field_size_tree)
9547    field_size_tree = bitsize_zero_node;
9548
9549  /* We cannot yet cope with fields whose positions are variable, so
9550     for now, when we see such things, we simply return 0.  Someday, we may
9551     be able to handle such cases, but it will be damn difficult.  */
9552  if (! host_integerp (bit_position (decl), 0))
9553    return 0;
9554
9555  bitpos_int = int_bit_position (decl);
9556
9557  /* If we don't know the size of the field, pretend it's a full word.  */
9558  if (host_integerp (field_size_tree, 1))
9559    field_size_in_bits = tree_low_cst (field_size_tree, 1);
9560  else
9561    field_size_in_bits = BITS_PER_WORD;
9562
9563  type_size_in_bits = simple_type_size_in_bits (type);
9564  type_align_in_bits = simple_type_align_in_bits (type);
9565  decl_align_in_bits = simple_decl_align_in_bits (decl);
9566
9567  /* The GCC front-end doesn't make any attempt to keep track of the starting
9568     bit offset (relative to the start of the containing structure type) of the
9569     hypothetical "containing object" for a bit-field.  Thus, when computing
9570     the byte offset value for the start of the "containing object" of a
9571     bit-field, we must deduce this information on our own. This can be rather
9572     tricky to do in some cases.  For example, handling the following structure
9573     type definition when compiling for an i386/i486 target (which only aligns
9574     long long's to 32-bit boundaries) can be very tricky:
9575
9576	 struct S { int field1; long long field2:31; };
9577
9578     Fortunately, there is a simple rule-of-thumb which can be used in such
9579     cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
9580     structure shown above.  It decides to do this based upon one simple rule
9581     for bit-field allocation.  GCC allocates each "containing object" for each
9582     bit-field at the first (i.e. lowest addressed) legitimate alignment
9583     boundary (based upon the required minimum alignment for the declared type
9584     of the field) which it can possibly use, subject to the condition that
9585     there is still enough available space remaining in the containing object
9586     (when allocated at the selected point) to fully accommodate all of the
9587     bits of the bit-field itself.
9588
9589     This simple rule makes it obvious why GCC allocates 8 bytes for each
9590     object of the structure type shown above.  When looking for a place to
9591     allocate the "containing object" for `field2', the compiler simply tries
9592     to allocate a 64-bit "containing object" at each successive 32-bit
9593     boundary (starting at zero) until it finds a place to allocate that 64-
9594     bit field such that at least 31 contiguous (and previously unallocated)
9595     bits remain within that selected 64 bit field.  (As it turns out, for the
9596     example above, the compiler finds it is OK to allocate the "containing
9597     object" 64-bit field at bit-offset zero within the structure type.)
9598
9599     Here we attempt to work backwards from the limited set of facts we're
9600     given, and we try to deduce from those facts, where GCC must have believed
9601     that the containing object started (within the structure type). The value
9602     we deduce is then used (by the callers of this routine) to generate
9603     DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9604     and, in the case of DW_AT_location, regular fields as well).  */
9605
9606  /* Figure out the bit-distance from the start of the structure to the
9607     "deepest" bit of the bit-field.  */
9608  deepest_bitpos = bitpos_int + field_size_in_bits;
9609
9610  /* This is the tricky part.  Use some fancy footwork to deduce where the
9611     lowest addressed bit of the containing object must be.  */
9612  object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9613
9614  /* Round up to type_align by default.  This works best for bitfields.  */
9615  object_offset_in_bits += type_align_in_bits - 1;
9616  object_offset_in_bits /= type_align_in_bits;
9617  object_offset_in_bits *= type_align_in_bits;
9618
9619  if (object_offset_in_bits > bitpos_int)
9620    {
9621      /* Sigh, the decl must be packed.  */
9622      object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9623
9624      /* Round up to decl_align instead.  */
9625      object_offset_in_bits += decl_align_in_bits - 1;
9626      object_offset_in_bits /= decl_align_in_bits;
9627      object_offset_in_bits *= decl_align_in_bits;
9628    }
9629
9630  return object_offset_in_bits / BITS_PER_UNIT;
9631}
9632
9633/* The following routines define various Dwarf attributes and any data
9634   associated with them.  */
9635
9636/* Add a location description attribute value to a DIE.
9637
9638   This emits location attributes suitable for whole variables and
9639   whole parameters.  Note that the location attributes for struct fields are
9640   generated by the routine `data_member_location_attribute' below.  */
9641
9642static inline void
9643add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9644			     dw_loc_descr_ref descr)
9645{
9646  if (descr != 0)
9647    add_AT_loc (die, attr_kind, descr);
9648}
9649
9650/* Attach the specialized form of location attribute used for data members of
9651   struct and union types.  In the special case of a FIELD_DECL node which
9652   represents a bit-field, the "offset" part of this special location
9653   descriptor must indicate the distance in bytes from the lowest-addressed
9654   byte of the containing struct or union type to the lowest-addressed byte of
9655   the "containing object" for the bit-field.  (See the `field_byte_offset'
9656   function above).
9657
9658   For any given bit-field, the "containing object" is a hypothetical object
9659   (of some integral or enum type) within which the given bit-field lives.  The
9660   type of this hypothetical "containing object" is always the same as the
9661   declared type of the individual bit-field itself (for GCC anyway... the
9662   DWARF spec doesn't actually mandate this).  Note that it is the size (in
9663   bytes) of the hypothetical "containing object" which will be given in the
9664   DW_AT_byte_size attribute for this bit-field.  (See the
9665   `byte_size_attribute' function below.)  It is also used when calculating the
9666   value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
9667   function below.)  */
9668
9669static void
9670add_data_member_location_attribute (dw_die_ref die, tree decl)
9671{
9672  HOST_WIDE_INT offset;
9673  dw_loc_descr_ref loc_descr = 0;
9674
9675  if (TREE_CODE (decl) == TREE_BINFO)
9676    {
9677      /* We're working on the TAG_inheritance for a base class.  */
9678      if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9679	{
9680	  /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9681	     aren't at a fixed offset from all (sub)objects of the same
9682	     type.  We need to extract the appropriate offset from our
9683	     vtable.  The following dwarf expression means
9684
9685	       BaseAddr = ObAddr + *((*ObAddr) - Offset)
9686
9687	     This is specific to the V3 ABI, of course.  */
9688
9689	  dw_loc_descr_ref tmp;
9690
9691	  /* Make a copy of the object address.  */
9692	  tmp = new_loc_descr (DW_OP_dup, 0, 0);
9693	  add_loc_descr (&loc_descr, tmp);
9694
9695	  /* Extract the vtable address.  */
9696	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
9697	  add_loc_descr (&loc_descr, tmp);
9698
9699	  /* Calculate the address of the offset.  */
9700	  offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9701	  gcc_assert (offset < 0);
9702
9703	  tmp = int_loc_descriptor (-offset);
9704	  add_loc_descr (&loc_descr, tmp);
9705	  tmp = new_loc_descr (DW_OP_minus, 0, 0);
9706	  add_loc_descr (&loc_descr, tmp);
9707
9708	  /* Extract the offset.  */
9709	  tmp = new_loc_descr (DW_OP_deref, 0, 0);
9710	  add_loc_descr (&loc_descr, tmp);
9711
9712	  /* Add it to the object address.  */
9713	  tmp = new_loc_descr (DW_OP_plus, 0, 0);
9714	  add_loc_descr (&loc_descr, tmp);
9715	}
9716      else
9717	offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9718    }
9719  else
9720    offset = field_byte_offset (decl);
9721
9722  if (! loc_descr)
9723    {
9724      enum dwarf_location_atom op;
9725
9726      /* The DWARF2 standard says that we should assume that the structure
9727	 address is already on the stack, so we can specify a structure field
9728	 address by using DW_OP_plus_uconst.  */
9729
9730#ifdef MIPS_DEBUGGING_INFO
9731      /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9732	 operator correctly.  It works only if we leave the offset on the
9733	 stack.  */
9734      op = DW_OP_constu;
9735#else
9736      op = DW_OP_plus_uconst;
9737#endif
9738
9739      loc_descr = new_loc_descr (op, offset, 0);
9740    }
9741
9742  add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9743}
9744
9745/* Writes integer values to dw_vec_const array.  */
9746
9747static void
9748insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9749{
9750  while (size != 0)
9751    {
9752      *dest++ = val & 0xff;
9753      val >>= 8;
9754      --size;
9755    }
9756}
9757
9758/* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9759
9760static HOST_WIDE_INT
9761extract_int (const unsigned char *src, unsigned int size)
9762{
9763  HOST_WIDE_INT val = 0;
9764
9765  src += size;
9766  while (size != 0)
9767    {
9768      val <<= 8;
9769      val |= *--src & 0xff;
9770      --size;
9771    }
9772  return val;
9773}
9774
9775/* Writes floating point values to dw_vec_const array.  */
9776
9777static void
9778insert_float (rtx rtl, unsigned char *array)
9779{
9780  REAL_VALUE_TYPE rv;
9781  long val[4];
9782  int i;
9783
9784  REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9785  real_to_target (val, &rv, GET_MODE (rtl));
9786
9787  /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9788  for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9789    {
9790      insert_int (val[i], 4, array);
9791      array += 4;
9792    }
9793}
9794
9795/* Attach a DW_AT_const_value attribute for a variable or a parameter which
9796   does not have a "location" either in memory or in a register.  These
9797   things can arise in GNU C when a constant is passed as an actual parameter
9798   to an inlined function.  They can also arise in C++ where declared
9799   constants do not necessarily get memory "homes".  */
9800
9801static void
9802add_const_value_attribute (dw_die_ref die, rtx rtl)
9803{
9804  switch (GET_CODE (rtl))
9805    {
9806    case CONST_INT:
9807      {
9808	HOST_WIDE_INT val = INTVAL (rtl);
9809
9810	if (val < 0)
9811	  add_AT_int (die, DW_AT_const_value, val);
9812	else
9813	  add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9814      }
9815      break;
9816
9817    case CONST_DOUBLE:
9818      /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9819	 floating-point constant.  A CONST_DOUBLE is used whenever the
9820	 constant requires more than one word in order to be adequately
9821	 represented.  We output CONST_DOUBLEs as blocks.  */
9822      {
9823	enum machine_mode mode = GET_MODE (rtl);
9824
9825	if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9826	  {
9827	    unsigned int length = GET_MODE_SIZE (mode);
9828	    unsigned char *array = ggc_alloc (length);
9829
9830	    insert_float (rtl, array);
9831	    add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9832	  }
9833	else
9834	  {
9835	    /* ??? We really should be using HOST_WIDE_INT throughout.  */
9836	    gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9837
9838	    add_AT_long_long (die, DW_AT_const_value,
9839			      CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9840	  }
9841      }
9842      break;
9843
9844    case CONST_VECTOR:
9845      {
9846	enum machine_mode mode = GET_MODE (rtl);
9847	unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9848	unsigned int length = CONST_VECTOR_NUNITS (rtl);
9849	unsigned char *array = ggc_alloc (length * elt_size);
9850	unsigned int i;
9851	unsigned char *p;
9852
9853	switch (GET_MODE_CLASS (mode))
9854	  {
9855	  case MODE_VECTOR_INT:
9856	    for (i = 0, p = array; i < length; i++, p += elt_size)
9857	      {
9858		rtx elt = CONST_VECTOR_ELT (rtl, i);
9859		HOST_WIDE_INT lo, hi;
9860
9861		switch (GET_CODE (elt))
9862		  {
9863		  case CONST_INT:
9864		    lo = INTVAL (elt);
9865		    hi = -(lo < 0);
9866		    break;
9867
9868		  case CONST_DOUBLE:
9869		    lo = CONST_DOUBLE_LOW (elt);
9870		    hi = CONST_DOUBLE_HIGH (elt);
9871		    break;
9872
9873		  default:
9874		    gcc_unreachable ();
9875		  }
9876
9877		if (elt_size <= sizeof (HOST_WIDE_INT))
9878		  insert_int (lo, elt_size, p);
9879		else
9880		  {
9881		    unsigned char *p0 = p;
9882		    unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9883
9884		    gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9885		    if (WORDS_BIG_ENDIAN)
9886		      {
9887			p0 = p1;
9888			p1 = p;
9889		      }
9890		    insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9891		    insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9892		  }
9893	      }
9894	    break;
9895
9896	  case MODE_VECTOR_FLOAT:
9897	    for (i = 0, p = array; i < length; i++, p += elt_size)
9898	      {
9899		rtx elt = CONST_VECTOR_ELT (rtl, i);
9900		insert_float (elt, p);
9901	      }
9902	    break;
9903
9904	  default:
9905	    gcc_unreachable ();
9906	  }
9907
9908	add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9909      }
9910      break;
9911
9912    case CONST_STRING:
9913      add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9914      break;
9915
9916    case SYMBOL_REF:
9917    case LABEL_REF:
9918    case CONST:
9919      add_AT_addr (die, DW_AT_const_value, rtl);
9920      VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9921      break;
9922
9923    case PLUS:
9924      /* In cases where an inlined instance of an inline function is passed
9925	 the address of an `auto' variable (which is local to the caller) we
9926	 can get a situation where the DECL_RTL of the artificial local
9927	 variable (for the inlining) which acts as a stand-in for the
9928	 corresponding formal parameter (of the inline function) will look
9929	 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
9930	 exactly a compile-time constant expression, but it isn't the address
9931	 of the (artificial) local variable either.  Rather, it represents the
9932	 *value* which the artificial local variable always has during its
9933	 lifetime.  We currently have no way to represent such quasi-constant
9934	 values in Dwarf, so for now we just punt and generate nothing.  */
9935      break;
9936
9937    default:
9938      /* No other kinds of rtx should be possible here.  */
9939      gcc_unreachable ();
9940    }
9941
9942}
9943
9944/* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9945   for use in a later add_const_value_attribute call.  */
9946
9947static rtx
9948rtl_for_decl_init (tree init, tree type)
9949{
9950  rtx rtl = NULL_RTX;
9951
9952  /* If a variable is initialized with a string constant without embedded
9953     zeros, build CONST_STRING.  */
9954  if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9955    {
9956      tree enttype = TREE_TYPE (type);
9957      tree domain = TYPE_DOMAIN (type);
9958      enum machine_mode mode = TYPE_MODE (enttype);
9959
9960      if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9961	  && domain
9962	  && integer_zerop (TYPE_MIN_VALUE (domain))
9963	  && compare_tree_int (TYPE_MAX_VALUE (domain),
9964			       TREE_STRING_LENGTH (init) - 1) == 0
9965	  && ((size_t) TREE_STRING_LENGTH (init)
9966	      == strlen (TREE_STRING_POINTER (init)) + 1))
9967	rtl = gen_rtx_CONST_STRING (VOIDmode,
9968				    ggc_strdup (TREE_STRING_POINTER (init)));
9969    }
9970  /* If the initializer is something that we know will expand into an
9971     immediate RTL constant, expand it now.  Expanding anything else
9972     tends to produce unresolved symbols; see debug/5770 and c++/6381.  */
9973  /* Aggregate, vector, and complex types may contain constructors that may
9974     result in code being generated when expand_expr is called, so we can't
9975     handle them here.  Integer and float are useful and safe types to handle
9976     here.  */
9977  else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9978	   && initializer_constant_valid_p (init, type) == null_pointer_node)
9979    {
9980      rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9981
9982      /* If expand_expr returns a MEM, it wasn't immediate.  */
9983      gcc_assert (!rtl || !MEM_P (rtl));
9984    }
9985
9986  return rtl;
9987}
9988
9989/* Generate RTL for the variable DECL to represent its location.  */
9990
9991static rtx
9992rtl_for_decl_location (tree decl)
9993{
9994  rtx rtl;
9995
9996  /* Here we have to decide where we are going to say the parameter "lives"
9997     (as far as the debugger is concerned).  We only have a couple of
9998     choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9999
10000     DECL_RTL normally indicates where the parameter lives during most of the
10001     activation of the function.  If optimization is enabled however, this
10002     could be either NULL or else a pseudo-reg.  Both of those cases indicate
10003     that the parameter doesn't really live anywhere (as far as the code
10004     generation parts of GCC are concerned) during most of the function's
10005     activation.  That will happen (for example) if the parameter is never
10006     referenced within the function.
10007
10008     We could just generate a location descriptor here for all non-NULL
10009     non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10010     a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10011     where DECL_RTL is NULL or is a pseudo-reg.
10012
10013     Note however that we can only get away with using DECL_INCOMING_RTL as
10014     a backup substitute for DECL_RTL in certain limited cases.  In cases
10015     where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10016     we can be sure that the parameter was passed using the same type as it is
10017     declared to have within the function, and that its DECL_INCOMING_RTL
10018     points us to a place where a value of that type is passed.
10019
10020     In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10021     we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10022     because in these cases DECL_INCOMING_RTL points us to a value of some
10023     type which is *different* from the type of the parameter itself.  Thus,
10024     if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10025     such cases, the debugger would end up (for example) trying to fetch a
10026     `float' from a place which actually contains the first part of a
10027     `double'.  That would lead to really incorrect and confusing
10028     output at debug-time.
10029
10030     So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10031     in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10032     are a couple of exceptions however.  On little-endian machines we can
10033     get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10034     not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10035     an integral type that is smaller than TREE_TYPE (decl). These cases arise
10036     when (on a little-endian machine) a non-prototyped function has a
10037     parameter declared to be of type `short' or `char'.  In such cases,
10038     TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10039     be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10040     passed `int' value.  If the debugger then uses that address to fetch
10041     a `short' or a `char' (on a little-endian machine) the result will be
10042     the correct data, so we allow for such exceptional cases below.
10043
10044     Note that our goal here is to describe the place where the given formal
10045     parameter lives during most of the function's activation (i.e. between the
10046     end of the prologue and the start of the epilogue).  We'll do that as best
10047     as we can. Note however that if the given formal parameter is modified
10048     sometime during the execution of the function, then a stack backtrace (at
10049     debug-time) will show the function as having been called with the *new*
10050     value rather than the value which was originally passed in.  This happens
10051     rarely enough that it is not a major problem, but it *is* a problem, and
10052     I'd like to fix it.
10053
10054     A future version of dwarf2out.c may generate two additional attributes for
10055     any given DW_TAG_formal_parameter DIE which will describe the "passed
10056     type" and the "passed location" for the given formal parameter in addition
10057     to the attributes we now generate to indicate the "declared type" and the
10058     "active location" for each parameter.  This additional set of attributes
10059     could be used by debuggers for stack backtraces. Separately, note that
10060     sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10061     This happens (for example) for inlined-instances of inline function formal
10062     parameters which are never referenced.  This really shouldn't be
10063     happening.  All PARM_DECL nodes should get valid non-NULL
10064     DECL_INCOMING_RTL values.  FIXME.  */
10065
10066  /* Use DECL_RTL as the "location" unless we find something better.  */
10067  rtl = DECL_RTL_IF_SET (decl);
10068
10069  /* When generating abstract instances, ignore everything except
10070     constants, symbols living in memory, and symbols living in
10071     fixed registers.  */
10072  if (! reload_completed)
10073    {
10074      if (rtl
10075	  && (CONSTANT_P (rtl)
10076	      || (MEM_P (rtl)
10077	          && CONSTANT_P (XEXP (rtl, 0)))
10078	      || (REG_P (rtl)
10079	          && TREE_CODE (decl) == VAR_DECL
10080		  && TREE_STATIC (decl))))
10081	{
10082	  rtl = targetm.delegitimize_address (rtl);
10083	  return rtl;
10084	}
10085      rtl = NULL_RTX;
10086    }
10087  else if (TREE_CODE (decl) == PARM_DECL)
10088    {
10089      if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10090	{
10091	  tree declared_type = TREE_TYPE (decl);
10092	  tree passed_type = DECL_ARG_TYPE (decl);
10093	  enum machine_mode dmode = TYPE_MODE (declared_type);
10094	  enum machine_mode pmode = TYPE_MODE (passed_type);
10095
10096	  /* This decl represents a formal parameter which was optimized out.
10097	     Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10098	     all cases where (rtl == NULL_RTX) just below.  */
10099	  if (dmode == pmode)
10100	    rtl = DECL_INCOMING_RTL (decl);
10101	  else if (SCALAR_INT_MODE_P (dmode)
10102		   && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10103		   && DECL_INCOMING_RTL (decl))
10104	    {
10105	      rtx inc = DECL_INCOMING_RTL (decl);
10106	      if (REG_P (inc))
10107		rtl = inc;
10108	      else if (MEM_P (inc))
10109		{
10110		  if (BYTES_BIG_ENDIAN)
10111		    rtl = adjust_address_nv (inc, dmode,
10112					     GET_MODE_SIZE (pmode)
10113					     - GET_MODE_SIZE (dmode));
10114		  else
10115		    rtl = inc;
10116		}
10117	    }
10118	}
10119
10120      /* If the parm was passed in registers, but lives on the stack, then
10121	 make a big endian correction if the mode of the type of the
10122	 parameter is not the same as the mode of the rtl.  */
10123      /* ??? This is the same series of checks that are made in dbxout.c before
10124	 we reach the big endian correction code there.  It isn't clear if all
10125	 of these checks are necessary here, but keeping them all is the safe
10126	 thing to do.  */
10127      else if (MEM_P (rtl)
10128	       && XEXP (rtl, 0) != const0_rtx
10129	       && ! CONSTANT_P (XEXP (rtl, 0))
10130	       /* Not passed in memory.  */
10131	       && !MEM_P (DECL_INCOMING_RTL (decl))
10132	       /* Not passed by invisible reference.  */
10133	       && (!REG_P (XEXP (rtl, 0))
10134		   || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10135		   || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10136#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10137		   || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10138#endif
10139		     )
10140	       /* Big endian correction check.  */
10141	       && BYTES_BIG_ENDIAN
10142	       && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10143	       && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10144		   < UNITS_PER_WORD))
10145	{
10146	  int offset = (UNITS_PER_WORD
10147			- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10148
10149	  rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10150			     plus_constant (XEXP (rtl, 0), offset));
10151	}
10152    }
10153  else if (TREE_CODE (decl) == VAR_DECL
10154	   && rtl
10155	   && MEM_P (rtl)
10156	   && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10157	   && BYTES_BIG_ENDIAN)
10158    {
10159      int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10160      int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10161
10162      /* If a variable is declared "register" yet is smaller than
10163	 a register, then if we store the variable to memory, it
10164	 looks like we're storing a register-sized value, when in
10165	 fact we are not.  We need to adjust the offset of the
10166	 storage location to reflect the actual value's bytes,
10167	 else gdb will not be able to display it.  */
10168      if (rsize > dsize)
10169	rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10170			   plus_constant (XEXP (rtl, 0), rsize-dsize));
10171    }
10172
10173  /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10174     and will have been substituted directly into all expressions that use it.
10175     C does not have such a concept, but C++ and other languages do.  */
10176  if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10177    rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10178
10179  if (rtl)
10180    rtl = targetm.delegitimize_address (rtl);
10181
10182  /* If we don't look past the constant pool, we risk emitting a
10183     reference to a constant pool entry that isn't referenced from
10184     code, and thus is not emitted.  */
10185  if (rtl)
10186    rtl = avoid_constant_pool_reference (rtl);
10187
10188  return rtl;
10189}
10190
10191/* We need to figure out what section we should use as the base for the
10192   address ranges where a given location is valid.
10193   1. If this particular DECL has a section associated with it, use that.
10194   2. If this function has a section associated with it, use that.
10195   3. Otherwise, use the text section.
10196   XXX: If you split a variable across multiple sections, we won't notice.  */
10197
10198static const char *
10199secname_for_decl (tree decl)
10200{
10201  const char *secname;
10202
10203  if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10204    {
10205      tree sectree = DECL_SECTION_NAME (decl);
10206      secname = TREE_STRING_POINTER (sectree);
10207    }
10208  else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10209    {
10210      tree sectree = DECL_SECTION_NAME (current_function_decl);
10211      secname = TREE_STRING_POINTER (sectree);
10212    }
10213  else if (cfun
10214	   && (last_text_section == in_unlikely_executed_text
10215	       || (last_text_section == in_named
10216		   && last_text_section_name
10217		      == cfun->unlikely_text_section_name)))
10218    secname = cfun->cold_section_label;
10219  else
10220    secname = text_section_label;
10221
10222  return secname;
10223}
10224
10225/* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10226   data attribute for a variable or a parameter.  We generate the
10227   DW_AT_const_value attribute only in those cases where the given variable
10228   or parameter does not have a true "location" either in memory or in a
10229   register.  This can happen (for example) when a constant is passed as an
10230   actual argument in a call to an inline function.  (It's possible that
10231   these things can crop up in other ways also.)  Note that one type of
10232   constant value which can be passed into an inlined function is a constant
10233   pointer.  This can happen for example if an actual argument in an inlined
10234   function call evaluates to a compile-time constant address.  */
10235
10236static void
10237add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10238				       enum dwarf_attribute attr)
10239{
10240  rtx rtl;
10241  dw_loc_descr_ref descr;
10242  var_loc_list *loc_list;
10243  struct var_loc_node *node;
10244  if (TREE_CODE (decl) == ERROR_MARK)
10245    return;
10246
10247  gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10248	      || TREE_CODE (decl) == RESULT_DECL);
10249
10250  /* See if we possibly have multiple locations for this variable.  */
10251  loc_list = lookup_decl_loc (decl);
10252
10253  /* If it truly has multiple locations, the first and last node will
10254     differ.  */
10255  if (loc_list && loc_list->first != loc_list->last)
10256    {
10257      const char *endname, *secname;
10258      dw_loc_list_ref list;
10259      rtx varloc;
10260
10261      /* Now that we know what section we are using for a base,
10262         actually construct the list of locations.
10263	 The first location information is what is passed to the
10264	 function that creates the location list, and the remaining
10265	 locations just get added on to that list.
10266	 Note that we only know the start address for a location
10267	 (IE location changes), so to build the range, we use
10268	 the range [current location start, next location start].
10269	 This means we have to special case the last node, and generate
10270	 a range of [last location start, end of function label].  */
10271
10272      node = loc_list->first;
10273      varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10274      secname = secname_for_decl (decl);
10275
10276      list = new_loc_list (loc_descriptor (varloc),
10277			   node->label, node->next->label, secname, 1);
10278      node = node->next;
10279
10280      for (; node->next; node = node->next)
10281	if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10282	  {
10283	    /* The variable has a location between NODE->LABEL and
10284	       NODE->NEXT->LABEL.  */
10285	    varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10286	    add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10287				       node->label, node->next->label, secname);
10288	  }
10289
10290      /* If the variable has a location at the last label
10291	 it keeps its location until the end of function.  */
10292      if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10293	{
10294	  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10295
10296	  varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10297	  if (!current_function_decl)
10298	    endname = text_end_label;
10299	  else
10300	    {
10301	      ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10302					   current_function_funcdef_no);
10303	      endname = ggc_strdup (label_id);
10304	    }
10305	  add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10306				     node->label, endname, secname);
10307	}
10308
10309      /* Finally, add the location list to the DIE, and we are done.  */
10310      add_AT_loc_list (die, attr, list);
10311      return;
10312    }
10313
10314  /* Try to get some constant RTL for this decl, and use that as the value of
10315     the location.  */
10316
10317  rtl = rtl_for_decl_location (decl);
10318  if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10319    {
10320      add_const_value_attribute (die, rtl);
10321      return;
10322    }
10323
10324  /* If we have tried to generate the location otherwise, and it
10325     didn't work out (we wouldn't be here if we did), and we have a one entry
10326     location list, try generating a location from that.  */
10327  if (loc_list && loc_list->first)
10328    {
10329      node = loc_list->first;
10330      descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10331      if (descr)
10332	{
10333	  add_AT_location_description (die, attr, descr);
10334	  return;
10335	}
10336    }
10337
10338  /* We couldn't get any rtl, so try directly generating the location
10339     description from the tree.  */
10340  descr = loc_descriptor_from_tree (decl);
10341  if (descr)
10342    {
10343      add_AT_location_description (die, attr, descr);
10344      return;
10345    }
10346}
10347
10348/* If we don't have a copy of this variable in memory for some reason (such
10349   as a C++ member constant that doesn't have an out-of-line definition),
10350   we should tell the debugger about the constant value.  */
10351
10352static void
10353tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10354{
10355  tree init = DECL_INITIAL (decl);
10356  tree type = TREE_TYPE (decl);
10357  rtx rtl;
10358
10359  if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10360    /* OK */;
10361  else
10362    return;
10363
10364  rtl = rtl_for_decl_init (init, type);
10365  if (rtl)
10366    add_const_value_attribute (var_die, rtl);
10367}
10368
10369/* Convert the CFI instructions for the current function into a location
10370   list.  This is used for DW_AT_frame_base when we targeting a dwarf2
10371   consumer that does not support the dwarf3 DW_OP_call_frame_cfa.  */
10372
10373static dw_loc_list_ref
10374convert_cfa_to_loc_list (void)
10375{
10376  dw_fde_ref fde;
10377  dw_loc_list_ref list, *list_tail;
10378  dw_cfi_ref cfi;
10379  dw_cfa_location last_cfa, next_cfa;
10380  const char *start_label, *last_label, *section;
10381
10382  fde = &fde_table[fde_table_in_use - 1];
10383
10384  section = secname_for_decl (current_function_decl);
10385  list_tail = &list;
10386  list = NULL;
10387
10388  next_cfa.reg = INVALID_REGNUM;
10389  next_cfa.offset = 0;
10390  next_cfa.indirect = 0;
10391  next_cfa.base_offset = 0;
10392
10393  start_label = fde->dw_fde_begin;
10394
10395  /* ??? Bald assumption that the CIE opcode list does not contain
10396     advance opcodes.  */
10397  for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10398    lookup_cfa_1 (cfi, &next_cfa);
10399
10400  last_cfa = next_cfa;
10401  last_label = start_label;
10402
10403  for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10404    switch (cfi->dw_cfi_opc)
10405      {
10406      case DW_CFA_set_loc:
10407      case DW_CFA_advance_loc1:
10408      case DW_CFA_advance_loc2:
10409      case DW_CFA_advance_loc4:
10410	if (!cfa_equal_p (&last_cfa, &next_cfa))
10411	  {
10412	    *list_tail = new_loc_list (build_cfa_loc (&last_cfa), start_label,
10413				       last_label, section, list == NULL);
10414
10415	    list_tail = &(*list_tail)->dw_loc_next;
10416	    last_cfa = next_cfa;
10417	    start_label = last_label;
10418	  }
10419	last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10420	break;
10421
10422      case DW_CFA_advance_loc:
10423	/* The encoding is complex enough that we should never emit this.  */
10424      case DW_CFA_remember_state:
10425      case DW_CFA_restore_state:
10426	/* We don't handle these two in this function.  It would be possible
10427	   if it were to be required.  */
10428	gcc_unreachable ();
10429
10430      default:
10431	lookup_cfa_1 (cfi, &next_cfa);
10432	break;
10433      }
10434
10435  if (!cfa_equal_p (&last_cfa, &next_cfa))
10436    {
10437      *list_tail = new_loc_list (build_cfa_loc (&last_cfa), start_label,
10438				 last_label, section, list == NULL);
10439      list_tail = &(*list_tail)->dw_loc_next;
10440      start_label = last_label;
10441    }
10442  *list_tail = new_loc_list (build_cfa_loc (&next_cfa), start_label,
10443			     fde->dw_fde_end, section, list == NULL);
10444
10445  return list;
10446}
10447
10448/* Compute a displacement from the "steady-state frame pointer" to
10449   the CFA, and store it in frame_pointer_cfa_offset.  */
10450
10451static void
10452compute_frame_pointer_to_cfa_displacement (void)
10453{
10454  HOST_WIDE_INT offset;
10455  rtx reg, elim;
10456
10457#ifdef FRAME_POINTER_CFA_OFFSET
10458  reg = frame_pointer_rtx;
10459  offset = FRAME_POINTER_CFA_OFFSET (current_function_decl);
10460#else
10461  reg = arg_pointer_rtx;
10462  offset = ARG_POINTER_CFA_OFFSET (current_function_decl);
10463#endif
10464
10465  elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10466  if (GET_CODE (elim) == PLUS)
10467    {
10468      offset += INTVAL (XEXP (elim, 1));
10469      elim = XEXP (elim, 0);
10470    }
10471  gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10472		       : stack_pointer_rtx));
10473
10474  frame_pointer_cfa_offset = -offset;
10475}
10476
10477/* Generate a DW_AT_name attribute given some string value to be included as
10478   the value of the attribute.  */
10479
10480static void
10481add_name_attribute (dw_die_ref die, const char *name_string)
10482{
10483  if (name_string != NULL && *name_string != 0)
10484    {
10485      if (demangle_name_func)
10486	name_string = (*demangle_name_func) (name_string);
10487
10488      add_AT_string (die, DW_AT_name, name_string);
10489    }
10490}
10491
10492/* Generate a DW_AT_comp_dir attribute for DIE.  */
10493
10494static void
10495add_comp_dir_attribute (dw_die_ref die)
10496{
10497  const char *wd = get_src_pwd ();
10498  if (wd != NULL)
10499    add_AT_string (die, DW_AT_comp_dir, wd);
10500}
10501
10502/* Given a tree node describing an array bound (either lower or upper) output
10503   a representation for that bound.  */
10504
10505static void
10506add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10507{
10508  switch (TREE_CODE (bound))
10509    {
10510    case ERROR_MARK:
10511      return;
10512
10513    /* All fixed-bounds are represented by INTEGER_CST nodes.  */
10514    case INTEGER_CST:
10515      if (! host_integerp (bound, 0)
10516	  || (bound_attr == DW_AT_lower_bound
10517	      && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
10518		  || (is_fortran () && integer_onep (bound)))))
10519	/* Use the default.  */
10520	;
10521      else
10522	add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10523      break;
10524
10525    case CONVERT_EXPR:
10526    case NOP_EXPR:
10527    case NON_LVALUE_EXPR:
10528    case VIEW_CONVERT_EXPR:
10529      add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10530      break;
10531
10532    case SAVE_EXPR:
10533      break;
10534
10535    case VAR_DECL:
10536    case PARM_DECL:
10537    case RESULT_DECL:
10538      {
10539	dw_die_ref decl_die = lookup_decl_die (bound);
10540
10541	/* ??? Can this happen, or should the variable have been bound
10542	   first?  Probably it can, since I imagine that we try to create
10543	   the types of parameters in the order in which they exist in
10544	   the list, and won't have created a forward reference to a
10545	   later parameter.  */
10546	if (decl_die != NULL)
10547	  add_AT_die_ref (subrange_die, bound_attr, decl_die);
10548	break;
10549      }
10550
10551    default:
10552      {
10553	/* Otherwise try to create a stack operation procedure to
10554	   evaluate the value of the array bound.  */
10555
10556	dw_die_ref ctx, decl_die;
10557	dw_loc_descr_ref loc;
10558
10559	loc = loc_descriptor_from_tree (bound);
10560	if (loc == NULL)
10561	  break;
10562
10563	if (current_function_decl == 0)
10564	  ctx = comp_unit_die;
10565	else
10566	  ctx = lookup_decl_die (current_function_decl);
10567
10568	decl_die = new_die (DW_TAG_variable, ctx, bound);
10569	add_AT_flag (decl_die, DW_AT_artificial, 1);
10570	add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10571	add_AT_loc (decl_die, DW_AT_location, loc);
10572
10573	add_AT_die_ref (subrange_die, bound_attr, decl_die);
10574	break;
10575      }
10576    }
10577}
10578
10579/* Note that the block of subscript information for an array type also
10580   includes information about the element type of type given array type.  */
10581
10582static void
10583add_subscript_info (dw_die_ref type_die, tree type)
10584{
10585#ifndef MIPS_DEBUGGING_INFO
10586  unsigned dimension_number;
10587#endif
10588  tree lower, upper;
10589  dw_die_ref subrange_die;
10590
10591  /* The GNU compilers represent multidimensional array types as sequences of
10592     one dimensional array types whose element types are themselves array
10593     types.  Here we squish that down, so that each multidimensional array
10594     type gets only one array_type DIE in the Dwarf debugging info. The draft
10595     Dwarf specification say that we are allowed to do this kind of
10596     compression in C (because there is no difference between an array or
10597     arrays and a multidimensional array in C) but for other source languages
10598     (e.g. Ada) we probably shouldn't do this.  */
10599
10600  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10601     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
10602     We work around this by disabling this feature.  See also
10603     gen_array_type_die.  */
10604#ifndef MIPS_DEBUGGING_INFO
10605  for (dimension_number = 0;
10606       TREE_CODE (type) == ARRAY_TYPE;
10607       type = TREE_TYPE (type), dimension_number++)
10608#endif
10609    {
10610      tree domain = TYPE_DOMAIN (type);
10611
10612      /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10613	 and (in GNU C only) variable bounds.  Handle all three forms
10614	 here.  */
10615      subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10616      if (domain)
10617	{
10618	  /* We have an array type with specified bounds.  */
10619	  lower = TYPE_MIN_VALUE (domain);
10620	  upper = TYPE_MAX_VALUE (domain);
10621
10622	  /* Define the index type.  */
10623	  if (TREE_TYPE (domain))
10624	    {
10625	      /* ??? This is probably an Ada unnamed subrange type.  Ignore the
10626		 TREE_TYPE field.  We can't emit debug info for this
10627		 because it is an unnamed integral type.  */
10628	      if (TREE_CODE (domain) == INTEGER_TYPE
10629		  && TYPE_NAME (domain) == NULL_TREE
10630		  && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10631		  && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10632		;
10633	      else
10634		add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10635				    type_die);
10636	    }
10637
10638	  /* ??? If upper is NULL, the array has unspecified length,
10639	     but it does have a lower bound.  This happens with Fortran
10640	       dimension arr(N:*)
10641	     Since the debugger is definitely going to need to know N
10642	     to produce useful results, go ahead and output the lower
10643	     bound solo, and hope the debugger can cope.  */
10644
10645	  add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10646	  if (upper)
10647	    add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10648	}
10649
10650      /* Otherwise we have an array type with an unspecified length.  The
10651	 DWARF-2 spec does not say how to handle this; let's just leave out the
10652	 bounds.  */
10653    }
10654}
10655
10656static void
10657add_byte_size_attribute (dw_die_ref die, tree tree_node)
10658{
10659  unsigned size;
10660
10661  switch (TREE_CODE (tree_node))
10662    {
10663    case ERROR_MARK:
10664      size = 0;
10665      break;
10666    case ENUMERAL_TYPE:
10667    case RECORD_TYPE:
10668    case UNION_TYPE:
10669    case QUAL_UNION_TYPE:
10670      size = int_size_in_bytes (tree_node);
10671      break;
10672    case FIELD_DECL:
10673      /* For a data member of a struct or union, the DW_AT_byte_size is
10674	 generally given as the number of bytes normally allocated for an
10675	 object of the *declared* type of the member itself.  This is true
10676	 even for bit-fields.  */
10677      size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10678      break;
10679    default:
10680      gcc_unreachable ();
10681    }
10682
10683  /* Note that `size' might be -1 when we get to this point.  If it is, that
10684     indicates that the byte size of the entity in question is variable.  We
10685     have no good way of expressing this fact in Dwarf at the present time,
10686     so just let the -1 pass on through.  */
10687  add_AT_unsigned (die, DW_AT_byte_size, size);
10688}
10689
10690/* For a FIELD_DECL node which represents a bit-field, output an attribute
10691   which specifies the distance in bits from the highest order bit of the
10692   "containing object" for the bit-field to the highest order bit of the
10693   bit-field itself.
10694
10695   For any given bit-field, the "containing object" is a hypothetical object
10696   (of some integral or enum type) within which the given bit-field lives.  The
10697   type of this hypothetical "containing object" is always the same as the
10698   declared type of the individual bit-field itself.  The determination of the
10699   exact location of the "containing object" for a bit-field is rather
10700   complicated.  It's handled by the `field_byte_offset' function (above).
10701
10702   Note that it is the size (in bytes) of the hypothetical "containing object"
10703   which will be given in the DW_AT_byte_size attribute for this bit-field.
10704   (See `byte_size_attribute' above).  */
10705
10706static inline void
10707add_bit_offset_attribute (dw_die_ref die, tree decl)
10708{
10709  HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10710  tree type = DECL_BIT_FIELD_TYPE (decl);
10711  HOST_WIDE_INT bitpos_int;
10712  HOST_WIDE_INT highest_order_object_bit_offset;
10713  HOST_WIDE_INT highest_order_field_bit_offset;
10714  HOST_WIDE_INT unsigned bit_offset;
10715
10716  /* Must be a field and a bit field.  */
10717  gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10718
10719  /* We can't yet handle bit-fields whose offsets are variable, so if we
10720     encounter such things, just return without generating any attribute
10721     whatsoever.  Likewise for variable or too large size.  */
10722  if (! host_integerp (bit_position (decl), 0)
10723      || ! host_integerp (DECL_SIZE (decl), 1))
10724    return;
10725
10726  bitpos_int = int_bit_position (decl);
10727
10728  /* Note that the bit offset is always the distance (in bits) from the
10729     highest-order bit of the "containing object" to the highest-order bit of
10730     the bit-field itself.  Since the "high-order end" of any object or field
10731     is different on big-endian and little-endian machines, the computation
10732     below must take account of these differences.  */
10733  highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10734  highest_order_field_bit_offset = bitpos_int;
10735
10736  if (! BYTES_BIG_ENDIAN)
10737    {
10738      highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10739      highest_order_object_bit_offset += simple_type_size_in_bits (type);
10740    }
10741
10742  bit_offset
10743    = (! BYTES_BIG_ENDIAN
10744       ? highest_order_object_bit_offset - highest_order_field_bit_offset
10745       : highest_order_field_bit_offset - highest_order_object_bit_offset);
10746
10747  add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10748}
10749
10750/* For a FIELD_DECL node which represents a bit field, output an attribute
10751   which specifies the length in bits of the given field.  */
10752
10753static inline void
10754add_bit_size_attribute (dw_die_ref die, tree decl)
10755{
10756  /* Must be a field and a bit field.  */
10757  gcc_assert (TREE_CODE (decl) == FIELD_DECL
10758	      && DECL_BIT_FIELD_TYPE (decl));
10759
10760  if (host_integerp (DECL_SIZE (decl), 1))
10761    add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10762}
10763
10764/* If the compiled language is ANSI C, then add a 'prototyped'
10765   attribute, if arg types are given for the parameters of a function.  */
10766
10767static inline void
10768add_prototyped_attribute (dw_die_ref die, tree func_type)
10769{
10770  if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10771      && TYPE_ARG_TYPES (func_type) != NULL)
10772    add_AT_flag (die, DW_AT_prototyped, 1);
10773}
10774
10775/* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
10776   by looking in either the type declaration or object declaration
10777   equate table.  */
10778
10779static inline void
10780add_abstract_origin_attribute (dw_die_ref die, tree origin)
10781{
10782  dw_die_ref origin_die = NULL;
10783
10784  if (TREE_CODE (origin) != FUNCTION_DECL)
10785    {
10786      /* We may have gotten separated from the block for the inlined
10787	 function, if we're in an exception handler or some such; make
10788	 sure that the abstract function has been written out.
10789
10790	 Doing this for nested functions is wrong, however; functions are
10791	 distinct units, and our context might not even be inline.  */
10792      tree fn = origin;
10793
10794      if (TYPE_P (fn))
10795	fn = TYPE_STUB_DECL (fn);
10796
10797      fn = decl_function_context (fn);
10798      if (fn)
10799	dwarf2out_abstract_function (fn);
10800    }
10801
10802  if (DECL_P (origin))
10803    origin_die = lookup_decl_die (origin);
10804  else if (TYPE_P (origin))
10805    origin_die = lookup_type_die (origin);
10806
10807  /* XXX: Functions that are never lowered don't always have correct block
10808     trees (in the case of java, they simply have no block tree, in some other
10809     languages).  For these functions, there is nothing we can really do to
10810     output correct debug info for inlined functions in all cases.  Rather
10811     than die, we'll just produce deficient debug info now, in that we will
10812     have variables without a proper abstract origin.  In the future, when all
10813     functions are lowered, we should re-add a gcc_assert (origin_die)
10814     here.  */
10815
10816  if (origin_die)
10817      add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10818}
10819
10820/* We do not currently support the pure_virtual attribute.  */
10821
10822static inline void
10823add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10824{
10825  if (DECL_VINDEX (func_decl))
10826    {
10827      add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10828
10829      if (host_integerp (DECL_VINDEX (func_decl), 0))
10830	add_AT_loc (die, DW_AT_vtable_elem_location,
10831		    new_loc_descr (DW_OP_constu,
10832				   tree_low_cst (DECL_VINDEX (func_decl), 0),
10833				   0));
10834
10835      /* GNU extension: Record what type this method came from originally.  */
10836      if (debug_info_level > DINFO_LEVEL_TERSE)
10837	add_AT_die_ref (die, DW_AT_containing_type,
10838			lookup_type_die (DECL_CONTEXT (func_decl)));
10839    }
10840}
10841
10842/* Add source coordinate attributes for the given decl.  */
10843
10844static void
10845add_src_coords_attributes (dw_die_ref die, tree decl)
10846{
10847  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10848  unsigned file_index = lookup_filename (s.file);
10849
10850  add_AT_unsigned (die, DW_AT_decl_file, file_index);
10851  add_AT_unsigned (die, DW_AT_decl_line, s.line);
10852}
10853
10854/* Add a DW_AT_name attribute and source coordinate attribute for the
10855   given decl, but only if it actually has a name.  */
10856
10857static void
10858add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10859{
10860  tree decl_name;
10861
10862  decl_name = DECL_NAME (decl);
10863  if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10864    {
10865      add_name_attribute (die, dwarf2_name (decl, 0));
10866      if (! DECL_ARTIFICIAL (decl))
10867	add_src_coords_attributes (die, decl);
10868
10869      if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10870	  && TREE_PUBLIC (decl)
10871	  && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10872	  && !DECL_ABSTRACT (decl)
10873	  && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
10874	add_AT_string (die, DW_AT_MIPS_linkage_name,
10875		       IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10876    }
10877
10878#ifdef VMS_DEBUGGING_INFO
10879  /* Get the function's name, as described by its RTL.  This may be different
10880     from the DECL_NAME name used in the source file.  */
10881  if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10882    {
10883      add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10884		   XEXP (DECL_RTL (decl), 0));
10885      VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10886    }
10887#endif
10888}
10889
10890/* Push a new declaration scope.  */
10891
10892static void
10893push_decl_scope (tree scope)
10894{
10895  VEC_safe_push (tree, gc, decl_scope_table, scope);
10896}
10897
10898/* Pop a declaration scope.  */
10899
10900static inline void
10901pop_decl_scope (void)
10902{
10903  VEC_pop (tree, decl_scope_table);
10904}
10905
10906/* Return the DIE for the scope that immediately contains this type.
10907   Non-named types get global scope.  Named types nested in other
10908   types get their containing scope if it's open, or global scope
10909   otherwise.  All other types (i.e. function-local named types) get
10910   the current active scope.  */
10911
10912static dw_die_ref
10913scope_die_for (tree t, dw_die_ref context_die)
10914{
10915  dw_die_ref scope_die = NULL;
10916  tree containing_scope;
10917  int i;
10918
10919  /* Non-types always go in the current scope.  */
10920  gcc_assert (TYPE_P (t));
10921
10922  containing_scope = TYPE_CONTEXT (t);
10923
10924  /* Use the containing namespace if it was passed in (for a declaration).  */
10925  if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10926    {
10927      if (context_die == lookup_decl_die (containing_scope))
10928	/* OK */;
10929      else
10930	containing_scope = NULL_TREE;
10931    }
10932
10933  /* Ignore function type "scopes" from the C frontend.  They mean that
10934     a tagged type is local to a parmlist of a function declarator, but
10935     that isn't useful to DWARF.  */
10936  if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10937    containing_scope = NULL_TREE;
10938
10939  if (containing_scope == NULL_TREE)
10940    scope_die = comp_unit_die;
10941  else if (TYPE_P (containing_scope))
10942    {
10943      /* For types, we can just look up the appropriate DIE.  But
10944	 first we check to see if we're in the middle of emitting it
10945	 so we know where the new DIE should go.  */
10946      for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10947	if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10948	  break;
10949
10950      if (i < 0)
10951	{
10952	  gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10953		      || TREE_ASM_WRITTEN (containing_scope));
10954
10955	  /* If none of the current dies are suitable, we get file scope.  */
10956	  scope_die = comp_unit_die;
10957	}
10958      else
10959	scope_die = lookup_type_die (containing_scope);
10960    }
10961  else
10962    scope_die = context_die;
10963
10964  return scope_die;
10965}
10966
10967/* Returns nonzero if CONTEXT_DIE is internal to a function.  */
10968
10969static inline int
10970local_scope_p (dw_die_ref context_die)
10971{
10972  for (; context_die; context_die = context_die->die_parent)
10973    if (context_die->die_tag == DW_TAG_inlined_subroutine
10974	|| context_die->die_tag == DW_TAG_subprogram)
10975      return 1;
10976
10977  return 0;
10978}
10979
10980/* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10981   whether or not to treat a DIE in this context as a declaration.  */
10982
10983static inline int
10984class_or_namespace_scope_p (dw_die_ref context_die)
10985{
10986  return (context_die
10987	  && (context_die->die_tag == DW_TAG_structure_type
10988	      || context_die->die_tag == DW_TAG_union_type
10989	      || context_die->die_tag == DW_TAG_namespace));
10990}
10991
10992/* Many forms of DIEs require a "type description" attribute.  This
10993   routine locates the proper "type descriptor" die for the type given
10994   by 'type', and adds a DW_AT_type attribute below the given die.  */
10995
10996static void
10997add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10998		    int decl_volatile, dw_die_ref context_die)
10999{
11000  enum tree_code code  = TREE_CODE (type);
11001  dw_die_ref type_die  = NULL;
11002
11003  /* ??? If this type is an unnamed subrange type of an integral or
11004     floating-point type, use the inner type.  This is because we have no
11005     support for unnamed types in base_type_die.  This can happen if this is
11006     an Ada subrange type.  Correct solution is emit a subrange type die.  */
11007  if ((code == INTEGER_TYPE || code == REAL_TYPE)
11008      && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11009    type = TREE_TYPE (type), code = TREE_CODE (type);
11010
11011  if (code == ERROR_MARK
11012      /* Handle a special case.  For functions whose return type is void, we
11013	 generate *no* type attribute.  (Note that no object may have type
11014	 `void', so this only applies to function return types).  */
11015      || code == VOID_TYPE)
11016    return;
11017
11018  type_die = modified_type_die (type,
11019				decl_const || TYPE_READONLY (type),
11020				decl_volatile || TYPE_VOLATILE (type),
11021				context_die);
11022
11023  if (type_die != NULL)
11024    add_AT_die_ref (object_die, DW_AT_type, type_die);
11025}
11026
11027/* Given an object die, add the calling convention attribute for the
11028   function call type.  */
11029static void
11030add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11031{
11032  enum dwarf_calling_convention value = DW_CC_normal;
11033
11034  value = targetm.dwarf_calling_convention (type);
11035
11036  /* Only add the attribute if the backend requests it, and
11037     is not DW_CC_normal.  */
11038  if (value && (value != DW_CC_normal))
11039    add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11040}
11041
11042/* Given a tree pointer to a struct, class, union, or enum type node, return
11043   a pointer to the (string) tag name for the given type, or zero if the type
11044   was declared without a tag.  */
11045
11046static const char *
11047type_tag (tree type)
11048{
11049  const char *name = 0;
11050
11051  if (TYPE_NAME (type) != 0)
11052    {
11053      tree t = 0;
11054
11055      /* Find the IDENTIFIER_NODE for the type name.  */
11056      if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11057	t = TYPE_NAME (type);
11058
11059      /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11060	 a TYPE_DECL node, regardless of whether or not a `typedef' was
11061	 involved.  */
11062      else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11063	       && ! DECL_IGNORED_P (TYPE_NAME (type)))
11064	t = DECL_NAME (TYPE_NAME (type));
11065
11066      /* Now get the name as a string, or invent one.  */
11067      if (t != 0)
11068	name = IDENTIFIER_POINTER (t);
11069    }
11070
11071  return (name == 0 || *name == '\0') ? 0 : name;
11072}
11073
11074/* Return the type associated with a data member, make a special check
11075   for bit field types.  */
11076
11077static inline tree
11078member_declared_type (tree member)
11079{
11080  return (DECL_BIT_FIELD_TYPE (member)
11081	  ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11082}
11083
11084/* Get the decl's label, as described by its RTL. This may be different
11085   from the DECL_NAME name used in the source file.  */
11086
11087#if 0
11088static const char *
11089decl_start_label (tree decl)
11090{
11091  rtx x;
11092  const char *fnname;
11093
11094  x = DECL_RTL (decl);
11095  gcc_assert (MEM_P (x));
11096
11097  x = XEXP (x, 0);
11098  gcc_assert (GET_CODE (x) == SYMBOL_REF);
11099
11100  fnname = XSTR (x, 0);
11101  return fnname;
11102}
11103#endif
11104
11105/* These routines generate the internal representation of the DIE's for
11106   the compilation unit.  Debugging information is collected by walking
11107   the declaration trees passed in from dwarf2out_decl().  */
11108
11109static void
11110gen_array_type_die (tree type, dw_die_ref context_die)
11111{
11112  dw_die_ref scope_die = scope_die_for (type, context_die);
11113  dw_die_ref array_die;
11114  tree element_type;
11115
11116  /* ??? The SGI dwarf reader fails for array of array of enum types unless
11117     the inner array type comes before the outer array type.  Thus we must
11118     call gen_type_die before we call new_die.  See below also.  */
11119#ifdef MIPS_DEBUGGING_INFO
11120  gen_type_die (TREE_TYPE (type), context_die);
11121#endif
11122
11123  array_die = new_die (DW_TAG_array_type, scope_die, type);
11124  add_name_attribute (array_die, type_tag (type));
11125  equate_type_number_to_die (type, array_die);
11126
11127  if (TREE_CODE (type) == VECTOR_TYPE)
11128    {
11129      /* The frontend feeds us a representation for the vector as a struct
11130	 containing an array.  Pull out the array type.  */
11131      type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11132      add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11133    }
11134
11135#if 0
11136  /* We default the array ordering.  SDB will probably do
11137     the right things even if DW_AT_ordering is not present.  It's not even
11138     an issue until we start to get into multidimensional arrays anyway.  If
11139     SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11140     then we'll have to put the DW_AT_ordering attribute back in.  (But if
11141     and when we find out that we need to put these in, we will only do so
11142     for multidimensional arrays.  */
11143  add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11144#endif
11145
11146#ifdef MIPS_DEBUGGING_INFO
11147  /* The SGI compilers handle arrays of unknown bound by setting
11148     AT_declaration and not emitting any subrange DIEs.  */
11149  if (! TYPE_DOMAIN (type))
11150    add_AT_flag (array_die, DW_AT_declaration, 1);
11151  else
11152#endif
11153    add_subscript_info (array_die, type);
11154
11155  /* Add representation of the type of the elements of this array type.  */
11156  element_type = TREE_TYPE (type);
11157
11158  /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11159     const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11160     We work around this by disabling this feature.  See also
11161     add_subscript_info.  */
11162#ifndef MIPS_DEBUGGING_INFO
11163  while (TREE_CODE (element_type) == ARRAY_TYPE)
11164    element_type = TREE_TYPE (element_type);
11165
11166  gen_type_die (element_type, context_die);
11167#endif
11168
11169  add_type_attribute (array_die, element_type, 0, 0, context_die);
11170}
11171
11172#if 0
11173static void
11174gen_entry_point_die (tree decl, dw_die_ref context_die)
11175{
11176  tree origin = decl_ultimate_origin (decl);
11177  dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11178
11179  if (origin != NULL)
11180    add_abstract_origin_attribute (decl_die, origin);
11181  else
11182    {
11183      add_name_and_src_coords_attributes (decl_die, decl);
11184      add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11185			  0, 0, context_die);
11186    }
11187
11188  if (DECL_ABSTRACT (decl))
11189    equate_decl_number_to_die (decl, decl_die);
11190  else
11191    add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11192}
11193#endif
11194
11195/* Walk through the list of incomplete types again, trying once more to
11196   emit full debugging info for them.  */
11197
11198static void
11199retry_incomplete_types (void)
11200{
11201  int i;
11202
11203  for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11204    gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11205}
11206
11207/* Generate a DIE to represent an inlined instance of an enumeration type.  */
11208
11209static void
11210gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11211{
11212  dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11213
11214  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11215     be incomplete and such types are not marked.  */
11216  add_abstract_origin_attribute (type_die, type);
11217}
11218
11219/* Generate a DIE to represent an inlined instance of a structure type.  */
11220
11221static void
11222gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11223{
11224  dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11225
11226  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11227     be incomplete and such types are not marked.  */
11228  add_abstract_origin_attribute (type_die, type);
11229}
11230
11231/* Generate a DIE to represent an inlined instance of a union type.  */
11232
11233static void
11234gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11235{
11236  dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11237
11238  /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11239     be incomplete and such types are not marked.  */
11240  add_abstract_origin_attribute (type_die, type);
11241}
11242
11243/* Generate a DIE to represent an enumeration type.  Note that these DIEs
11244   include all of the information about the enumeration values also. Each
11245   enumerated type name/value is listed as a child of the enumerated type
11246   DIE.  */
11247
11248static dw_die_ref
11249gen_enumeration_type_die (tree type, dw_die_ref context_die)
11250{
11251  dw_die_ref type_die = lookup_type_die (type);
11252
11253  if (type_die == NULL)
11254    {
11255      type_die = new_die (DW_TAG_enumeration_type,
11256			  scope_die_for (type, context_die), type);
11257      equate_type_number_to_die (type, type_die);
11258      add_name_attribute (type_die, type_tag (type));
11259    }
11260  else if (! TYPE_SIZE (type))
11261    return type_die;
11262  else
11263    remove_AT (type_die, DW_AT_declaration);
11264
11265  /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11266     given enum type is incomplete, do not generate the DW_AT_byte_size
11267     attribute or the DW_AT_element_list attribute.  */
11268  if (TYPE_SIZE (type))
11269    {
11270      tree link;
11271
11272      TREE_ASM_WRITTEN (type) = 1;
11273      add_byte_size_attribute (type_die, type);
11274      if (TYPE_STUB_DECL (type) != NULL_TREE)
11275	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11276
11277      /* If the first reference to this type was as the return type of an
11278	 inline function, then it may not have a parent.  Fix this now.  */
11279      if (type_die->die_parent == NULL)
11280	add_child_die (scope_die_for (type, context_die), type_die);
11281
11282      for (link = TYPE_VALUES (type);
11283	   link != NULL; link = TREE_CHAIN (link))
11284	{
11285	  dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11286	  tree value = TREE_VALUE (link);
11287
11288	  add_name_attribute (enum_die,
11289			      IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11290
11291	  if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11292	    /* DWARF2 does not provide a way of indicating whether or
11293	       not enumeration constants are signed or unsigned.  GDB
11294	       always assumes the values are signed, so we output all
11295	       values as if they were signed.  That means that
11296	       enumeration constants with very large unsigned values
11297	       will appear to have negative values in the debugger.  */
11298	    add_AT_int (enum_die, DW_AT_const_value,
11299			tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11300	}
11301    }
11302  else
11303    add_AT_flag (type_die, DW_AT_declaration, 1);
11304
11305  return type_die;
11306}
11307
11308/* Generate a DIE to represent either a real live formal parameter decl or to
11309   represent just the type of some formal parameter position in some function
11310   type.
11311
11312   Note that this routine is a bit unusual because its argument may be a
11313   ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11314   represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11315   node.  If it's the former then this function is being called to output a
11316   DIE to represent a formal parameter object (or some inlining thereof).  If
11317   it's the latter, then this function is only being called to output a
11318   DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11319   argument type of some subprogram type.  */
11320
11321static dw_die_ref
11322gen_formal_parameter_die (tree node, dw_die_ref context_die)
11323{
11324  dw_die_ref parm_die
11325    = new_die (DW_TAG_formal_parameter, context_die, node);
11326  tree origin;
11327
11328  switch (TREE_CODE_CLASS (TREE_CODE (node)))
11329    {
11330    case tcc_declaration:
11331      origin = decl_ultimate_origin (node);
11332      if (origin != NULL)
11333	add_abstract_origin_attribute (parm_die, origin);
11334      else
11335	{
11336	  add_name_and_src_coords_attributes (parm_die, node);
11337	  add_type_attribute (parm_die, TREE_TYPE (node),
11338			      TREE_READONLY (node),
11339			      TREE_THIS_VOLATILE (node),
11340			      context_die);
11341	  if (DECL_ARTIFICIAL (node))
11342	    add_AT_flag (parm_die, DW_AT_artificial, 1);
11343	}
11344
11345      equate_decl_number_to_die (node, parm_die);
11346      if (! DECL_ABSTRACT (node))
11347	add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11348
11349      break;
11350
11351    case tcc_type:
11352      /* We were called with some kind of a ..._TYPE node.  */
11353      add_type_attribute (parm_die, node, 0, 0, context_die);
11354      break;
11355
11356    default:
11357      gcc_unreachable ();
11358    }
11359
11360  return parm_die;
11361}
11362
11363/* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11364   at the end of an (ANSI prototyped) formal parameters list.  */
11365
11366static void
11367gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11368{
11369  new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11370}
11371
11372/* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11373   DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11374   parameters as specified in some function type specification (except for
11375   those which appear as part of a function *definition*).  */
11376
11377static void
11378gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11379{
11380  tree link;
11381  tree formal_type = NULL;
11382  tree first_parm_type;
11383  tree arg;
11384
11385  if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11386    {
11387      arg = DECL_ARGUMENTS (function_or_method_type);
11388      function_or_method_type = TREE_TYPE (function_or_method_type);
11389    }
11390  else
11391    arg = NULL_TREE;
11392
11393  first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11394
11395  /* Make our first pass over the list of formal parameter types and output a
11396     DW_TAG_formal_parameter DIE for each one.  */
11397  for (link = first_parm_type; link; )
11398    {
11399      dw_die_ref parm_die;
11400
11401      formal_type = TREE_VALUE (link);
11402      if (formal_type == void_type_node)
11403	break;
11404
11405      /* Output a (nameless) DIE to represent the formal parameter itself.  */
11406      parm_die = gen_formal_parameter_die (formal_type, context_die);
11407      if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11408	   && link == first_parm_type)
11409	  || (arg && DECL_ARTIFICIAL (arg)))
11410	add_AT_flag (parm_die, DW_AT_artificial, 1);
11411
11412      link = TREE_CHAIN (link);
11413      if (arg)
11414	arg = TREE_CHAIN (arg);
11415    }
11416
11417  /* If this function type has an ellipsis, add a
11418     DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
11419  if (formal_type != void_type_node)
11420    gen_unspecified_parameters_die (function_or_method_type, context_die);
11421
11422  /* Make our second (and final) pass over the list of formal parameter types
11423     and output DIEs to represent those types (as necessary).  */
11424  for (link = TYPE_ARG_TYPES (function_or_method_type);
11425       link && TREE_VALUE (link);
11426       link = TREE_CHAIN (link))
11427    gen_type_die (TREE_VALUE (link), context_die);
11428}
11429
11430/* We want to generate the DIE for TYPE so that we can generate the
11431   die for MEMBER, which has been defined; we will need to refer back
11432   to the member declaration nested within TYPE.  If we're trying to
11433   generate minimal debug info for TYPE, processing TYPE won't do the
11434   trick; we need to attach the member declaration by hand.  */
11435
11436static void
11437gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11438{
11439  gen_type_die (type, context_die);
11440
11441  /* If we're trying to avoid duplicate debug info, we may not have
11442     emitted the member decl for this function.  Emit it now.  */
11443  if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11444      && ! lookup_decl_die (member))
11445    {
11446      dw_die_ref type_die;
11447      gcc_assert (!decl_ultimate_origin (member));
11448
11449      push_decl_scope (type);
11450      type_die = lookup_type_die (type);
11451      if (TREE_CODE (member) == FUNCTION_DECL)
11452	gen_subprogram_die (member, type_die);
11453      else if (TREE_CODE (member) == FIELD_DECL)
11454	{
11455	  /* Ignore the nameless fields that are used to skip bits but handle
11456	     C++ anonymous unions and structs.  */
11457	  if (DECL_NAME (member) != NULL_TREE
11458	      || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11459	      || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11460	    {
11461	      gen_type_die (member_declared_type (member), type_die);
11462	      gen_field_die (member, type_die);
11463	    }
11464	}
11465      else
11466	gen_variable_die (member, type_die);
11467
11468      pop_decl_scope ();
11469    }
11470}
11471
11472/* Generate the DWARF2 info for the "abstract" instance of a function which we
11473   may later generate inlined and/or out-of-line instances of.  */
11474
11475static void
11476dwarf2out_abstract_function (tree decl)
11477{
11478  dw_die_ref old_die;
11479  tree save_fn;
11480  tree context;
11481  int was_abstract = DECL_ABSTRACT (decl);
11482
11483  /* Make sure we have the actual abstract inline, not a clone.  */
11484  decl = DECL_ORIGIN (decl);
11485
11486  old_die = lookup_decl_die (decl);
11487  if (old_die && get_AT (old_die, DW_AT_inline))
11488    /* We've already generated the abstract instance.  */
11489    return;
11490
11491  /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11492     we don't get confused by DECL_ABSTRACT.  */
11493  if (debug_info_level > DINFO_LEVEL_TERSE)
11494    {
11495      context = decl_class_context (decl);
11496      if (context)
11497	gen_type_die_for_member
11498	  (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11499    }
11500
11501  /* Pretend we've just finished compiling this function.  */
11502  save_fn = current_function_decl;
11503  current_function_decl = decl;
11504
11505  set_decl_abstract_flags (decl, 1);
11506  dwarf2out_decl (decl);
11507  if (! was_abstract)
11508    set_decl_abstract_flags (decl, 0);
11509
11510  current_function_decl = save_fn;
11511}
11512
11513/* Generate a DIE to represent a declared function (either file-scope or
11514   block-local).  */
11515
11516static void
11517gen_subprogram_die (tree decl, dw_die_ref context_die)
11518{
11519  char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11520  tree origin = decl_ultimate_origin (decl);
11521  dw_die_ref subr_die;
11522  tree fn_arg_types;
11523  tree outer_scope;
11524  dw_die_ref old_die = lookup_decl_die (decl);
11525  int declaration = (current_function_decl != decl
11526		     || class_or_namespace_scope_p (context_die));
11527
11528  /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11529     started to generate the abstract instance of an inline, decided to output
11530     its containing class, and proceeded to emit the declaration of the inline
11531     from the member list for the class.  If so, DECLARATION takes priority;
11532     we'll get back to the abstract instance when done with the class.  */
11533
11534  /* The class-scope declaration DIE must be the primary DIE.  */
11535  if (origin && declaration && class_or_namespace_scope_p (context_die))
11536    {
11537      origin = NULL;
11538      gcc_assert (!old_die);
11539    }
11540
11541  /* Now that the C++ front end lazily declares artificial member fns, we
11542     might need to retrofit the declaration into its class.  */
11543  if (!declaration && !origin && !old_die
11544      && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11545      && !class_or_namespace_scope_p (context_die)
11546      && debug_info_level > DINFO_LEVEL_TERSE)
11547    old_die = force_decl_die (decl);
11548
11549  if (origin != NULL)
11550    {
11551      gcc_assert (!declaration || local_scope_p (context_die));
11552
11553      /* Fixup die_parent for the abstract instance of a nested
11554	 inline function.  */
11555      if (old_die && old_die->die_parent == NULL)
11556	add_child_die (context_die, old_die);
11557
11558      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11559      add_abstract_origin_attribute (subr_die, origin);
11560    }
11561  else if (old_die)
11562    {
11563      expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11564      unsigned file_index = lookup_filename (s.file);
11565
11566      if (!get_AT_flag (old_die, DW_AT_declaration)
11567	  /* We can have a normal definition following an inline one in the
11568	     case of redefinition of GNU C extern inlines.
11569	     It seems reasonable to use AT_specification in this case.  */
11570	  && !get_AT (old_die, DW_AT_inline))
11571	{
11572	  /* Detect and ignore this case, where we are trying to output
11573	     something we have already output.  */
11574	  return;
11575	}
11576
11577      /* If the definition comes from the same place as the declaration,
11578	 maybe use the old DIE.  We always want the DIE for this function
11579	 that has the *_pc attributes to be under comp_unit_die so the
11580	 debugger can find it.  We also need to do this for abstract
11581	 instances of inlines, since the spec requires the out-of-line copy
11582	 to have the same parent.  For local class methods, this doesn't
11583	 apply; we just use the old DIE.  */
11584      if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11585	  && (DECL_ARTIFICIAL (decl)
11586	      || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11587		  && (get_AT_unsigned (old_die, DW_AT_decl_line)
11588		      == (unsigned) s.line))))
11589	{
11590	  subr_die = old_die;
11591
11592	  /* Clear out the declaration attribute and the formal parameters.
11593	     Do not remove all children, because it is possible that this
11594	     declaration die was forced using force_decl_die(). In such
11595	     cases die that forced declaration die (e.g. TAG_imported_module)
11596	     is one of the children that we do not want to remove.  */
11597	  remove_AT (subr_die, DW_AT_declaration);
11598	  remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11599	}
11600      else
11601	{
11602	  subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11603	  add_AT_specification (subr_die, old_die);
11604	  if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11605	    add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11606	  if (get_AT_unsigned (old_die, DW_AT_decl_line)
11607	      != (unsigned) s.line)
11608	    add_AT_unsigned
11609	      (subr_die, DW_AT_decl_line, s.line);
11610	}
11611    }
11612  else
11613    {
11614      subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11615
11616      if (TREE_PUBLIC (decl))
11617	add_AT_flag (subr_die, DW_AT_external, 1);
11618
11619      add_name_and_src_coords_attributes (subr_die, decl);
11620      if (debug_info_level > DINFO_LEVEL_TERSE)
11621	{
11622	  add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11623	  add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11624			      0, 0, context_die);
11625	}
11626
11627      add_pure_or_virtual_attribute (subr_die, decl);
11628      if (DECL_ARTIFICIAL (decl))
11629	add_AT_flag (subr_die, DW_AT_artificial, 1);
11630
11631      if (TREE_PROTECTED (decl))
11632	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11633      else if (TREE_PRIVATE (decl))
11634	add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11635    }
11636
11637  if (declaration)
11638    {
11639      if (!old_die || !get_AT (old_die, DW_AT_inline))
11640	{
11641	  add_AT_flag (subr_die, DW_AT_declaration, 1);
11642
11643	  /* The first time we see a member function, it is in the context of
11644	     the class to which it belongs.  We make sure of this by emitting
11645	     the class first.  The next time is the definition, which is
11646	     handled above.  The two may come from the same source text.
11647
11648	     Note that force_decl_die() forces function declaration die. It is
11649	     later reused to represent definition.  */
11650	  equate_decl_number_to_die (decl, subr_die);
11651	}
11652    }
11653  else if (DECL_ABSTRACT (decl))
11654    {
11655      if (DECL_DECLARED_INLINE_P (decl))
11656	{
11657          if (cgraph_function_possibly_inlined_p (decl))
11658	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11659	  else
11660	    add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11661	}
11662      else
11663	{
11664	  if (cgraph_function_possibly_inlined_p (decl))
11665            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11666	  else
11667            add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11668	}
11669
11670      equate_decl_number_to_die (decl, subr_die);
11671    }
11672  else if (!DECL_EXTERNAL (decl))
11673    {
11674      if (!old_die || !get_AT (old_die, DW_AT_inline))
11675	equate_decl_number_to_die (decl, subr_die);
11676
11677      if (!flag_reorder_blocks_and_partition)
11678	{
11679	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11680				       current_function_funcdef_no);
11681	  add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11682	  ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11683				       current_function_funcdef_no);
11684	  add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11685
11686	  add_pubname (decl, subr_die);
11687	  add_arange (decl, subr_die);
11688	}
11689      else
11690	{  /* Do nothing for now; maybe need to duplicate die, one for
11691	      hot section and ond for cold section, then use the hot/cold
11692	      section begin/end labels to generate the aranges...  */
11693	  /*
11694	    add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11695	    add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11696	    add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11697	    add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11698
11699	    add_pubname (decl, subr_die);
11700	    add_arange (decl, subr_die);
11701	    add_arange (decl, subr_die);
11702	   */
11703	}
11704
11705#ifdef MIPS_DEBUGGING_INFO
11706      /* Add a reference to the FDE for this routine.  */
11707      add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11708#endif
11709
11710      /* We define the "frame base" as the function's CFA.  This is more
11711	 convenient for several reasons: (1) It's stable across the prologue
11712	 and epilogue, which makes it better than just a frame pointer,
11713	 (2) With dwarf3, there exists a one-byte encoding that allows us
11714	 to reference the .debug_frame data by proxy, but failing that,
11715	 (3) We can at least reuse the code inspection and interpretation
11716	 code that determines the CFA position at various points in the
11717	 function.  */
11718      /* ??? Use some command-line or configury switch to enable the use
11719	 of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11720	 consumers that understand it; fall back to "pure" dwarf2 and
11721	 convert the CFA data into a location list.  */
11722      {
11723	dw_loc_list_ref list = convert_cfa_to_loc_list ();
11724	if (list->dw_loc_next)
11725	  add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11726	else
11727	  add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11728      }
11729
11730      /* Compute a displacement from the "steady-state frame pointer" to
11731	 the CFA.  The former is what all stack slots and argument slots
11732	 will reference in the rtl; the later is what we've told the
11733	 debugger about.  We'll need to adjust all frame_base references
11734	 by this displacement.  */
11735      compute_frame_pointer_to_cfa_displacement ();
11736
11737      if (cfun->static_chain_decl)
11738	add_AT_location_description (subr_die, DW_AT_static_link,
11739		 loc_descriptor_from_tree (cfun->static_chain_decl));
11740    }
11741
11742  /* Now output descriptions of the arguments for this function. This gets
11743     (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11744     for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11745     `...' at the end of the formal parameter list.  In order to find out if
11746     there was a trailing ellipsis or not, we must instead look at the type
11747     associated with the FUNCTION_DECL.  This will be a node of type
11748     FUNCTION_TYPE. If the chain of type nodes hanging off of this
11749     FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11750     an ellipsis at the end.  */
11751
11752  /* In the case where we are describing a mere function declaration, all we
11753     need to do here (and all we *can* do here) is to describe the *types* of
11754     its formal parameters.  */
11755  if (debug_info_level <= DINFO_LEVEL_TERSE)
11756    ;
11757  else if (declaration)
11758    gen_formal_types_die (decl, subr_die);
11759  else
11760    {
11761      /* Generate DIEs to represent all known formal parameters.  */
11762      tree arg_decls = DECL_ARGUMENTS (decl);
11763      tree parm;
11764
11765      /* When generating DIEs, generate the unspecified_parameters DIE
11766	 instead if we come across the arg "__builtin_va_alist" */
11767      for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11768	if (TREE_CODE (parm) == PARM_DECL)
11769	  {
11770	    if (DECL_NAME (parm)
11771		&& !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11772			    "__builtin_va_alist"))
11773	      gen_unspecified_parameters_die (parm, subr_die);
11774	    else
11775	      gen_decl_die (parm, subr_die);
11776	  }
11777
11778      /* Decide whether we need an unspecified_parameters DIE at the end.
11779	 There are 2 more cases to do this for: 1) the ansi ... declaration -
11780	 this is detectable when the end of the arg list is not a
11781	 void_type_node 2) an unprototyped function declaration (not a
11782	 definition).  This just means that we have no info about the
11783	 parameters at all.  */
11784      fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11785      if (fn_arg_types != NULL)
11786	{
11787	  /* This is the prototyped case, check for....  */
11788	  if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11789	    gen_unspecified_parameters_die (decl, subr_die);
11790	}
11791      else if (DECL_INITIAL (decl) == NULL_TREE)
11792	gen_unspecified_parameters_die (decl, subr_die);
11793    }
11794
11795  /* Output Dwarf info for all of the stuff within the body of the function
11796     (if it has one - it may be just a declaration).  */
11797  outer_scope = DECL_INITIAL (decl);
11798
11799  /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11800     a function.  This BLOCK actually represents the outermost binding contour
11801     for the function, i.e. the contour in which the function's formal
11802     parameters and labels get declared. Curiously, it appears that the front
11803     end doesn't actually put the PARM_DECL nodes for the current function onto
11804     the BLOCK_VARS list for this outer scope, but are strung off of the
11805     DECL_ARGUMENTS list for the function instead.
11806
11807     The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11808     the LABEL_DECL nodes for the function however, and we output DWARF info
11809     for those in decls_for_scope.  Just within the `outer_scope' there will be
11810     a BLOCK node representing the function's outermost pair of curly braces,
11811     and any blocks used for the base and member initializers of a C++
11812     constructor function.  */
11813  if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11814    {
11815      /* Emit a DW_TAG_variable DIE for a named return value.  */
11816      if (DECL_NAME (DECL_RESULT (decl)))
11817	gen_decl_die (DECL_RESULT (decl), subr_die);
11818
11819      current_function_has_inlines = 0;
11820      decls_for_scope (outer_scope, subr_die, 0);
11821
11822#if 0 && defined (MIPS_DEBUGGING_INFO)
11823      if (current_function_has_inlines)
11824	{
11825	  add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11826	  if (! comp_unit_has_inlines)
11827	    {
11828	      add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11829	      comp_unit_has_inlines = 1;
11830	    }
11831	}
11832#endif
11833    }
11834  /* Add the calling convention attribute if requested.  */
11835  add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11836
11837}
11838
11839/* Generate a DIE to represent a declared data object.  */
11840
11841static void
11842gen_variable_die (tree decl, dw_die_ref context_die)
11843{
11844  tree origin = decl_ultimate_origin (decl);
11845  dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11846
11847  dw_die_ref old_die = lookup_decl_die (decl);
11848  int declaration = (DECL_EXTERNAL (decl)
11849		     /* If DECL is COMDAT and has not actually been
11850			emitted, we cannot take its address; there
11851			might end up being no definition anywhere in
11852			the program.  For example, consider the C++
11853			test case:
11854
11855                          template <class T>
11856                          struct S { static const int i = 7; };
11857
11858                          template <class T>
11859                          const int S<T>::i;
11860
11861                          int f() { return S<int>::i; }
11862
11863			Here, S<int>::i is not DECL_EXTERNAL, but no
11864			definition is required, so the compiler will
11865			not emit a definition.  */
11866		     || (TREE_CODE (decl) == VAR_DECL
11867			 && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
11868		     || class_or_namespace_scope_p (context_die));
11869
11870  if (origin != NULL)
11871    add_abstract_origin_attribute (var_die, origin);
11872
11873  /* Loop unrolling can create multiple blocks that refer to the same
11874     static variable, so we must test for the DW_AT_declaration flag.
11875
11876     ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11877     copy decls and set the DECL_ABSTRACT flag on them instead of
11878     sharing them.
11879
11880     ??? Duplicated blocks have been rewritten to use .debug_ranges.
11881
11882     ??? The declare_in_namespace support causes us to get two DIEs for one
11883     variable, both of which are declarations.  We want to avoid considering
11884     one to be a specification, so we must test that this DIE is not a
11885     declaration.  */
11886  else if (old_die && TREE_STATIC (decl) && ! declaration
11887	   && get_AT_flag (old_die, DW_AT_declaration) == 1)
11888    {
11889      /* This is a definition of a C++ class level static.  */
11890      add_AT_specification (var_die, old_die);
11891      if (DECL_NAME (decl))
11892	{
11893	  expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11894	  unsigned file_index = lookup_filename (s.file);
11895
11896	  if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11897	    add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11898
11899	  if (get_AT_unsigned (old_die, DW_AT_decl_line)
11900	      != (unsigned) s.line)
11901
11902	    add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11903	}
11904    }
11905  else
11906    {
11907      add_name_and_src_coords_attributes (var_die, decl);
11908      add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11909			  TREE_THIS_VOLATILE (decl), context_die);
11910
11911      if (TREE_PUBLIC (decl))
11912	add_AT_flag (var_die, DW_AT_external, 1);
11913
11914      if (DECL_ARTIFICIAL (decl))
11915	add_AT_flag (var_die, DW_AT_artificial, 1);
11916
11917      if (TREE_PROTECTED (decl))
11918	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11919      else if (TREE_PRIVATE (decl))
11920	add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11921    }
11922
11923  if (declaration)
11924    add_AT_flag (var_die, DW_AT_declaration, 1);
11925
11926  if (DECL_ABSTRACT (decl) || declaration)
11927    equate_decl_number_to_die (decl, var_die);
11928
11929  if (! declaration && ! DECL_ABSTRACT (decl))
11930    {
11931      add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11932      add_pubname (decl, var_die);
11933    }
11934  else
11935    tree_add_const_value_attribute (var_die, decl);
11936}
11937
11938/* Generate a DIE to represent a label identifier.  */
11939
11940static void
11941gen_label_die (tree decl, dw_die_ref context_die)
11942{
11943  tree origin = decl_ultimate_origin (decl);
11944  dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11945  rtx insn;
11946  char label[MAX_ARTIFICIAL_LABEL_BYTES];
11947
11948  if (origin != NULL)
11949    add_abstract_origin_attribute (lbl_die, origin);
11950  else
11951    add_name_and_src_coords_attributes (lbl_die, decl);
11952
11953  if (DECL_ABSTRACT (decl))
11954    equate_decl_number_to_die (decl, lbl_die);
11955  else
11956    {
11957      insn = DECL_RTL_IF_SET (decl);
11958
11959      /* Deleted labels are programmer specified labels which have been
11960	 eliminated because of various optimizations.  We still emit them
11961	 here so that it is possible to put breakpoints on them.  */
11962      if (insn
11963	  && (LABEL_P (insn)
11964	      || ((NOTE_P (insn)
11965	           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11966	{
11967	  /* When optimization is enabled (via -O) some parts of the compiler
11968	     (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11969	     represent source-level labels which were explicitly declared by
11970	     the user.  This really shouldn't be happening though, so catch
11971	     it if it ever does happen.  */
11972	  gcc_assert (!INSN_DELETED_P (insn));
11973
11974	  ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11975	  add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11976	}
11977    }
11978}
11979
11980/* A helper function for gen_inlined_subroutine_die.  Add source coordinate
11981   attributes to the DIE for a block STMT, to describe where the inlined
11982   function was called from.  This is similar to add_src_coords_attributes.  */
11983
11984static inline void
11985add_call_src_coords_attributes (tree stmt, dw_die_ref die)
11986{
11987  expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
11988  unsigned file_index = lookup_filename (s.file);
11989
11990  add_AT_unsigned (die, DW_AT_call_file, file_index);
11991  add_AT_unsigned (die, DW_AT_call_line, s.line);
11992}
11993
11994/* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11995   Add low_pc and high_pc attributes to the DIE for a block STMT.  */
11996
11997static inline void
11998add_high_low_attributes (tree stmt, dw_die_ref die)
11999{
12000  char label[MAX_ARTIFICIAL_LABEL_BYTES];
12001
12002  if (BLOCK_FRAGMENT_CHAIN (stmt))
12003    {
12004      tree chain;
12005
12006      add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12007
12008      chain = BLOCK_FRAGMENT_CHAIN (stmt);
12009      do
12010	{
12011	  add_ranges (chain);
12012	  chain = BLOCK_FRAGMENT_CHAIN (chain);
12013	}
12014      while (chain);
12015      add_ranges (NULL);
12016    }
12017  else
12018    {
12019      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12020				   BLOCK_NUMBER (stmt));
12021      add_AT_lbl_id (die, DW_AT_low_pc, label);
12022      ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12023				   BLOCK_NUMBER (stmt));
12024      add_AT_lbl_id (die, DW_AT_high_pc, label);
12025    }
12026}
12027
12028/* Generate a DIE for a lexical block.  */
12029
12030static void
12031gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12032{
12033  dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12034
12035  if (! BLOCK_ABSTRACT (stmt))
12036    add_high_low_attributes (stmt, stmt_die);
12037
12038  decls_for_scope (stmt, stmt_die, depth);
12039}
12040
12041/* Generate a DIE for an inlined subprogram.  */
12042
12043static void
12044gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12045{
12046  tree decl = block_ultimate_origin (stmt);
12047
12048  /* Emit info for the abstract instance first, if we haven't yet.  We
12049     must emit this even if the block is abstract, otherwise when we
12050     emit the block below (or elsewhere), we may end up trying to emit
12051     a die whose origin die hasn't been emitted, and crashing.  */
12052  dwarf2out_abstract_function (decl);
12053
12054  if (! BLOCK_ABSTRACT (stmt))
12055    {
12056      dw_die_ref subr_die
12057	= new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12058
12059      add_abstract_origin_attribute (subr_die, decl);
12060      add_high_low_attributes (stmt, subr_die);
12061      add_call_src_coords_attributes (stmt, subr_die);
12062
12063      decls_for_scope (stmt, subr_die, depth);
12064      current_function_has_inlines = 1;
12065    }
12066  else
12067    /* We may get here if we're the outer block of function A that was
12068       inlined into function B that was inlined into function C.  When
12069       generating debugging info for C, dwarf2out_abstract_function(B)
12070       would mark all inlined blocks as abstract, including this one.
12071       So, we wouldn't (and shouldn't) expect labels to be generated
12072       for this one.  Instead, just emit debugging info for
12073       declarations within the block.  This is particularly important
12074       in the case of initializers of arguments passed from B to us:
12075       if they're statement expressions containing declarations, we
12076       wouldn't generate dies for their abstract variables, and then,
12077       when generating dies for the real variables, we'd die (pun
12078       intended :-)  */
12079    gen_lexical_block_die (stmt, context_die, depth);
12080}
12081
12082/* Generate a DIE for a field in a record, or structure.  */
12083
12084static void
12085gen_field_die (tree decl, dw_die_ref context_die)
12086{
12087  dw_die_ref decl_die;
12088
12089  if (TREE_TYPE (decl) == error_mark_node)
12090    return;
12091
12092  decl_die = new_die (DW_TAG_member, context_die, decl);
12093  add_name_and_src_coords_attributes (decl_die, decl);
12094  add_type_attribute (decl_die, member_declared_type (decl),
12095		      TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12096		      context_die);
12097
12098  if (DECL_BIT_FIELD_TYPE (decl))
12099    {
12100      add_byte_size_attribute (decl_die, decl);
12101      add_bit_size_attribute (decl_die, decl);
12102      add_bit_offset_attribute (decl_die, decl);
12103    }
12104
12105  if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12106    add_data_member_location_attribute (decl_die, decl);
12107
12108  if (DECL_ARTIFICIAL (decl))
12109    add_AT_flag (decl_die, DW_AT_artificial, 1);
12110
12111  if (TREE_PROTECTED (decl))
12112    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12113  else if (TREE_PRIVATE (decl))
12114    add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12115
12116  /* Equate decl number to die, so that we can look up this decl later on.  */
12117  equate_decl_number_to_die (decl, decl_die);
12118}
12119
12120#if 0
12121/* Don't generate either pointer_type DIEs or reference_type DIEs here.
12122   Use modified_type_die instead.
12123   We keep this code here just in case these types of DIEs may be needed to
12124   represent certain things in other languages (e.g. Pascal) someday.  */
12125
12126static void
12127gen_pointer_type_die (tree type, dw_die_ref context_die)
12128{
12129  dw_die_ref ptr_die
12130    = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12131
12132  equate_type_number_to_die (type, ptr_die);
12133  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12134  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12135}
12136
12137/* Don't generate either pointer_type DIEs or reference_type DIEs here.
12138   Use modified_type_die instead.
12139   We keep this code here just in case these types of DIEs may be needed to
12140   represent certain things in other languages (e.g. Pascal) someday.  */
12141
12142static void
12143gen_reference_type_die (tree type, dw_die_ref context_die)
12144{
12145  dw_die_ref ref_die
12146    = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12147
12148  equate_type_number_to_die (type, ref_die);
12149  add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12150  add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12151}
12152#endif
12153
12154/* Generate a DIE for a pointer to a member type.  */
12155
12156static void
12157gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12158{
12159  dw_die_ref ptr_die
12160    = new_die (DW_TAG_ptr_to_member_type,
12161	       scope_die_for (type, context_die), type);
12162
12163  equate_type_number_to_die (type, ptr_die);
12164  add_AT_die_ref (ptr_die, DW_AT_containing_type,
12165		  lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12166  add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12167}
12168
12169/* Generate the DIE for the compilation unit.  */
12170
12171static dw_die_ref
12172gen_compile_unit_die (const char *filename)
12173{
12174  dw_die_ref die;
12175  char producer[250];
12176  const char *language_string = lang_hooks.name;
12177  int language;
12178
12179  die = new_die (DW_TAG_compile_unit, NULL, NULL);
12180
12181  if (filename)
12182    {
12183      add_name_attribute (die, filename);
12184      /* Don't add cwd for <built-in>.  */
12185      if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12186	add_comp_dir_attribute (die);
12187    }
12188
12189  sprintf (producer, "%s %s", language_string, version_string);
12190
12191#ifdef MIPS_DEBUGGING_INFO
12192  /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12193     string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12194     not appear in the producer string, the debugger reaches the conclusion
12195     that the object file is stripped and has no debugging information.
12196     To get the MIPS/SGI debugger to believe that there is debugging
12197     information in the object file, we add a -g to the producer string.  */
12198  if (debug_info_level > DINFO_LEVEL_TERSE)
12199    strcat (producer, " -g");
12200#endif
12201
12202  add_AT_string (die, DW_AT_producer, producer);
12203
12204  if (strcmp (language_string, "GNU C++") == 0)
12205    language = DW_LANG_C_plus_plus;
12206  else if (strcmp (language_string, "GNU Ada") == 0)
12207    language = DW_LANG_Ada95;
12208  else if (strcmp (language_string, "GNU F77") == 0)
12209    language = DW_LANG_Fortran77;
12210  else if (strcmp (language_string, "GNU F95") == 0)
12211    language = DW_LANG_Fortran95;
12212  else if (strcmp (language_string, "GNU Pascal") == 0)
12213    language = DW_LANG_Pascal83;
12214  else if (strcmp (language_string, "GNU Java") == 0)
12215    language = DW_LANG_Java;
12216  else
12217    language = DW_LANG_C89;
12218
12219  add_AT_unsigned (die, DW_AT_language, language);
12220  return die;
12221}
12222
12223/* Generate a DIE for a string type.  */
12224
12225static void
12226gen_string_type_die (tree type, dw_die_ref context_die)
12227{
12228  dw_die_ref type_die
12229    = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
12230
12231  equate_type_number_to_die (type, type_die);
12232
12233  /* ??? Fudge the string length attribute for now.
12234     TODO: add string length info.  */
12235#if 0
12236  string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12237  bound_representation (upper_bound, 0, 'u');
12238#endif
12239}
12240
12241/* Generate the DIE for a base class.  */
12242
12243static void
12244gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12245{
12246  dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12247
12248  add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12249  add_data_member_location_attribute (die, binfo);
12250
12251  if (BINFO_VIRTUAL_P (binfo))
12252    add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12253
12254  if (access == access_public_node)
12255    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12256  else if (access == access_protected_node)
12257    add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12258}
12259
12260/* Generate a DIE for a class member.  */
12261
12262static void
12263gen_member_die (tree type, dw_die_ref context_die)
12264{
12265  tree member;
12266  tree binfo = TYPE_BINFO (type);
12267  dw_die_ref child;
12268
12269  /* If this is not an incomplete type, output descriptions of each of its
12270     members. Note that as we output the DIEs necessary to represent the
12271     members of this record or union type, we will also be trying to output
12272     DIEs to represent the *types* of those members. However the `type'
12273     function (above) will specifically avoid generating type DIEs for member
12274     types *within* the list of member DIEs for this (containing) type except
12275     for those types (of members) which are explicitly marked as also being
12276     members of this (containing) type themselves.  The g++ front- end can
12277     force any given type to be treated as a member of some other (containing)
12278     type by setting the TYPE_CONTEXT of the given (member) type to point to
12279     the TREE node representing the appropriate (containing) type.  */
12280
12281  /* First output info about the base classes.  */
12282  if (binfo)
12283    {
12284      VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12285      int i;
12286      tree base;
12287
12288      for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12289	gen_inheritance_die (base,
12290			     (accesses ? VEC_index (tree, accesses, i)
12291			      : access_public_node), context_die);
12292    }
12293
12294  /* Now output info about the data members and type members.  */
12295  for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12296    {
12297      /* If we thought we were generating minimal debug info for TYPE
12298	 and then changed our minds, some of the member declarations
12299	 may have already been defined.  Don't define them again, but
12300	 do put them in the right order.  */
12301
12302      child = lookup_decl_die (member);
12303      if (child)
12304	splice_child_die (context_die, child);
12305      else
12306	gen_decl_die (member, context_die);
12307    }
12308
12309  /* Now output info about the function members (if any).  */
12310  for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12311    {
12312      /* Don't include clones in the member list.  */
12313      if (DECL_ABSTRACT_ORIGIN (member))
12314	continue;
12315
12316      child = lookup_decl_die (member);
12317      if (child)
12318	splice_child_die (context_die, child);
12319      else
12320	gen_decl_die (member, context_die);
12321    }
12322}
12323
12324/* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
12325   is set, we pretend that the type was never defined, so we only get the
12326   member DIEs needed by later specification DIEs.  */
12327
12328static void
12329gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12330{
12331  dw_die_ref type_die = lookup_type_die (type);
12332  dw_die_ref scope_die = 0;
12333  int nested = 0;
12334  int complete = (TYPE_SIZE (type)
12335		  && (! TYPE_STUB_DECL (type)
12336		      || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12337  int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12338
12339  if (type_die && ! complete)
12340    return;
12341
12342  if (TYPE_CONTEXT (type) != NULL_TREE
12343      && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12344	  || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12345    nested = 1;
12346
12347  scope_die = scope_die_for (type, context_die);
12348
12349  if (! type_die || (nested && scope_die == comp_unit_die))
12350    /* First occurrence of type or toplevel definition of nested class.  */
12351    {
12352      dw_die_ref old_die = type_die;
12353
12354      type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12355			  ? DW_TAG_structure_type : DW_TAG_union_type,
12356			  scope_die, type);
12357      equate_type_number_to_die (type, type_die);
12358      if (old_die)
12359	add_AT_specification (type_die, old_die);
12360      else
12361	add_name_attribute (type_die, type_tag (type));
12362    }
12363  else
12364    remove_AT (type_die, DW_AT_declaration);
12365
12366  /* If this type has been completed, then give it a byte_size attribute and
12367     then give a list of members.  */
12368  if (complete && !ns_decl)
12369    {
12370      /* Prevent infinite recursion in cases where the type of some member of
12371	 this type is expressed in terms of this type itself.  */
12372      TREE_ASM_WRITTEN (type) = 1;
12373      add_byte_size_attribute (type_die, type);
12374      if (TYPE_STUB_DECL (type) != NULL_TREE)
12375	add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12376
12377      /* If the first reference to this type was as the return type of an
12378	 inline function, then it may not have a parent.  Fix this now.  */
12379      if (type_die->die_parent == NULL)
12380	add_child_die (scope_die, type_die);
12381
12382      push_decl_scope (type);
12383      gen_member_die (type, type_die);
12384      pop_decl_scope ();
12385
12386      /* GNU extension: Record what type our vtable lives in.  */
12387      if (TYPE_VFIELD (type))
12388	{
12389	  tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12390
12391	  gen_type_die (vtype, context_die);
12392	  add_AT_die_ref (type_die, DW_AT_containing_type,
12393			  lookup_type_die (vtype));
12394	}
12395    }
12396  else
12397    {
12398      add_AT_flag (type_die, DW_AT_declaration, 1);
12399
12400      /* We don't need to do this for function-local types.  */
12401      if (TYPE_STUB_DECL (type)
12402	  && ! decl_function_context (TYPE_STUB_DECL (type)))
12403	VEC_safe_push (tree, gc, incomplete_types, type);
12404    }
12405}
12406
12407/* Generate a DIE for a subroutine _type_.  */
12408
12409static void
12410gen_subroutine_type_die (tree type, dw_die_ref context_die)
12411{
12412  tree return_type = TREE_TYPE (type);
12413  dw_die_ref subr_die
12414    = new_die (DW_TAG_subroutine_type,
12415	       scope_die_for (type, context_die), type);
12416
12417  equate_type_number_to_die (type, subr_die);
12418  add_prototyped_attribute (subr_die, type);
12419  add_type_attribute (subr_die, return_type, 0, 0, context_die);
12420  gen_formal_types_die (type, subr_die);
12421}
12422
12423/* Generate a DIE for a type definition.  */
12424
12425static void
12426gen_typedef_die (tree decl, dw_die_ref context_die)
12427{
12428  dw_die_ref type_die;
12429  tree origin;
12430
12431  if (TREE_ASM_WRITTEN (decl))
12432    return;
12433
12434  TREE_ASM_WRITTEN (decl) = 1;
12435  type_die = new_die (DW_TAG_typedef, context_die, decl);
12436  origin = decl_ultimate_origin (decl);
12437  if (origin != NULL)
12438    add_abstract_origin_attribute (type_die, origin);
12439  else
12440    {
12441      tree type;
12442
12443      add_name_and_src_coords_attributes (type_die, decl);
12444      if (DECL_ORIGINAL_TYPE (decl))
12445	{
12446	  type = DECL_ORIGINAL_TYPE (decl);
12447
12448	  gcc_assert (type != TREE_TYPE (decl));
12449	  equate_type_number_to_die (TREE_TYPE (decl), type_die);
12450	}
12451      else
12452	type = TREE_TYPE (decl);
12453
12454      add_type_attribute (type_die, type, TREE_READONLY (decl),
12455			  TREE_THIS_VOLATILE (decl), context_die);
12456    }
12457
12458  if (DECL_ABSTRACT (decl))
12459    equate_decl_number_to_die (decl, type_die);
12460}
12461
12462/* Generate a type description DIE.  */
12463
12464static void
12465gen_type_die (tree type, dw_die_ref context_die)
12466{
12467  int need_pop;
12468
12469  if (type == NULL_TREE || type == error_mark_node)
12470    return;
12471
12472  if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12473      && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12474    {
12475      if (TREE_ASM_WRITTEN (type))
12476	return;
12477
12478      /* Prevent broken recursion; we can't hand off to the same type.  */
12479      gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12480
12481      TREE_ASM_WRITTEN (type) = 1;
12482      gen_decl_die (TYPE_NAME (type), context_die);
12483      return;
12484    }
12485
12486  /* We are going to output a DIE to represent the unqualified version
12487     of this type (i.e. without any const or volatile qualifiers) so
12488     get the main variant (i.e. the unqualified version) of this type
12489     now.  (Vectors are special because the debugging info is in the
12490     cloned type itself).  */
12491  if (TREE_CODE (type) != VECTOR_TYPE)
12492    type = type_main_variant (type);
12493
12494  if (TREE_ASM_WRITTEN (type))
12495    return;
12496
12497  switch (TREE_CODE (type))
12498    {
12499    case ERROR_MARK:
12500      break;
12501
12502    case POINTER_TYPE:
12503    case REFERENCE_TYPE:
12504      /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
12505	 ensures that the gen_type_die recursion will terminate even if the
12506	 type is recursive.  Recursive types are possible in Ada.  */
12507      /* ??? We could perhaps do this for all types before the switch
12508	 statement.  */
12509      TREE_ASM_WRITTEN (type) = 1;
12510
12511      /* For these types, all that is required is that we output a DIE (or a
12512	 set of DIEs) to represent the "basis" type.  */
12513      gen_type_die (TREE_TYPE (type), context_die);
12514      break;
12515
12516    case OFFSET_TYPE:
12517      /* This code is used for C++ pointer-to-data-member types.
12518	 Output a description of the relevant class type.  */
12519      gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12520
12521      /* Output a description of the type of the object pointed to.  */
12522      gen_type_die (TREE_TYPE (type), context_die);
12523
12524      /* Now output a DIE to represent this pointer-to-data-member type
12525	 itself.  */
12526      gen_ptr_to_mbr_type_die (type, context_die);
12527      break;
12528
12529    case FUNCTION_TYPE:
12530      /* Force out return type (in case it wasn't forced out already).  */
12531      gen_type_die (TREE_TYPE (type), context_die);
12532      gen_subroutine_type_die (type, context_die);
12533      break;
12534
12535    case METHOD_TYPE:
12536      /* Force out return type (in case it wasn't forced out already).  */
12537      gen_type_die (TREE_TYPE (type), context_die);
12538      gen_subroutine_type_die (type, context_die);
12539      break;
12540
12541    case ARRAY_TYPE:
12542      if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
12543	{
12544	  gen_type_die (TREE_TYPE (type), context_die);
12545	  gen_string_type_die (type, context_die);
12546	}
12547      else
12548	gen_array_type_die (type, context_die);
12549      break;
12550
12551    case VECTOR_TYPE:
12552      gen_array_type_die (type, context_die);
12553      break;
12554
12555    case ENUMERAL_TYPE:
12556    case RECORD_TYPE:
12557    case UNION_TYPE:
12558    case QUAL_UNION_TYPE:
12559      /* If this is a nested type whose containing class hasn't been written
12560	 out yet, writing it out will cover this one, too.  This does not apply
12561	 to instantiations of member class templates; they need to be added to
12562	 the containing class as they are generated.  FIXME: This hurts the
12563	 idea of combining type decls from multiple TUs, since we can't predict
12564	 what set of template instantiations we'll get.  */
12565      if (TYPE_CONTEXT (type)
12566	  && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12567	  && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12568	{
12569	  gen_type_die (TYPE_CONTEXT (type), context_die);
12570
12571	  if (TREE_ASM_WRITTEN (type))
12572	    return;
12573
12574	  /* If that failed, attach ourselves to the stub.  */
12575	  push_decl_scope (TYPE_CONTEXT (type));
12576	  context_die = lookup_type_die (TYPE_CONTEXT (type));
12577	  need_pop = 1;
12578	}
12579      else
12580	{
12581	  declare_in_namespace (type, context_die);
12582	  need_pop = 0;
12583	}
12584
12585      if (TREE_CODE (type) == ENUMERAL_TYPE)
12586	{
12587	  /* This might have been written out by the call to
12588	     declare_in_namespace.  */
12589	  if (!TREE_ASM_WRITTEN (type))
12590	    gen_enumeration_type_die (type, context_die);
12591	}
12592      else
12593	gen_struct_or_union_type_die (type, context_die);
12594
12595      if (need_pop)
12596	pop_decl_scope ();
12597
12598      /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12599	 it up if it is ever completed.  gen_*_type_die will set it for us
12600	 when appropriate.  */
12601      return;
12602
12603    case VOID_TYPE:
12604    case INTEGER_TYPE:
12605    case REAL_TYPE:
12606    case COMPLEX_TYPE:
12607    case BOOLEAN_TYPE:
12608    case CHAR_TYPE:
12609      /* No DIEs needed for fundamental types.  */
12610      break;
12611
12612    case LANG_TYPE:
12613      /* No Dwarf representation currently defined.  */
12614      break;
12615
12616    default:
12617      gcc_unreachable ();
12618    }
12619
12620  TREE_ASM_WRITTEN (type) = 1;
12621}
12622
12623/* Generate a DIE for a tagged type instantiation.  */
12624
12625static void
12626gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12627{
12628  if (type == NULL_TREE || type == error_mark_node)
12629    return;
12630
12631  /* We are going to output a DIE to represent the unqualified version of
12632     this type (i.e. without any const or volatile qualifiers) so make sure
12633     that we have the main variant (i.e. the unqualified version) of this
12634     type now.  */
12635  gcc_assert (type == type_main_variant (type));
12636
12637  /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12638     an instance of an unresolved type.  */
12639
12640  switch (TREE_CODE (type))
12641    {
12642    case ERROR_MARK:
12643      break;
12644
12645    case ENUMERAL_TYPE:
12646      gen_inlined_enumeration_type_die (type, context_die);
12647      break;
12648
12649    case RECORD_TYPE:
12650      gen_inlined_structure_type_die (type, context_die);
12651      break;
12652
12653    case UNION_TYPE:
12654    case QUAL_UNION_TYPE:
12655      gen_inlined_union_type_die (type, context_die);
12656      break;
12657
12658    default:
12659      gcc_unreachable ();
12660    }
12661}
12662
12663/* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12664   things which are local to the given block.  */
12665
12666static void
12667gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12668{
12669  int must_output_die = 0;
12670  tree origin;
12671  tree decl;
12672  enum tree_code origin_code;
12673
12674  /* Ignore blocks that are NULL.  */
12675  if (stmt == NULL_TREE)
12676    return;
12677
12678  /* If the block is one fragment of a non-contiguous block, do not
12679     process the variables, since they will have been done by the
12680     origin block.  Do process subblocks.  */
12681  if (BLOCK_FRAGMENT_ORIGIN (stmt))
12682    {
12683      tree sub;
12684
12685      for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12686	gen_block_die (sub, context_die, depth + 1);
12687
12688      return;
12689    }
12690
12691  /* Determine the "ultimate origin" of this block.  This block may be an
12692     inlined instance of an inlined instance of inline function, so we have
12693     to trace all of the way back through the origin chain to find out what
12694     sort of node actually served as the original seed for the creation of
12695     the current block.  */
12696  origin = block_ultimate_origin (stmt);
12697  origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12698
12699  /* Determine if we need to output any Dwarf DIEs at all to represent this
12700     block.  */
12701  if (origin_code == FUNCTION_DECL)
12702    /* The outer scopes for inlinings *must* always be represented.  We
12703       generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
12704    must_output_die = 1;
12705  else
12706    {
12707      /* In the case where the current block represents an inlining of the
12708	 "body block" of an inline function, we must *NOT* output any DIE for
12709	 this block because we have already output a DIE to represent the whole
12710	 inlined function scope and the "body block" of any function doesn't
12711	 really represent a different scope according to ANSI C rules.  So we
12712	 check here to make sure that this block does not represent a "body
12713	 block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
12714      if (! is_body_block (origin ? origin : stmt))
12715	{
12716	  /* Determine if this block directly contains any "significant"
12717	     local declarations which we will need to output DIEs for.  */
12718	  if (debug_info_level > DINFO_LEVEL_TERSE)
12719	    /* We are not in terse mode so *any* local declaration counts
12720	       as being a "significant" one.  */
12721	    must_output_die = (BLOCK_VARS (stmt) != NULL
12722			       && (TREE_USED (stmt)
12723				   || TREE_ASM_WRITTEN (stmt)
12724				   || BLOCK_ABSTRACT (stmt)));
12725	  else
12726	    /* We are in terse mode, so only local (nested) function
12727	       definitions count as "significant" local declarations.  */
12728	    for (decl = BLOCK_VARS (stmt);
12729		 decl != NULL; decl = TREE_CHAIN (decl))
12730	      if (TREE_CODE (decl) == FUNCTION_DECL
12731		  && DECL_INITIAL (decl))
12732		{
12733		  must_output_die = 1;
12734		  break;
12735		}
12736	}
12737    }
12738
12739  /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12740     DIE for any block which contains no significant local declarations at
12741     all.  Rather, in such cases we just call `decls_for_scope' so that any
12742     needed Dwarf info for any sub-blocks will get properly generated. Note
12743     that in terse mode, our definition of what constitutes a "significant"
12744     local declaration gets restricted to include only inlined function
12745     instances and local (nested) function definitions.  */
12746  if (must_output_die)
12747    {
12748      if (origin_code == FUNCTION_DECL)
12749	gen_inlined_subroutine_die (stmt, context_die, depth);
12750      else
12751	gen_lexical_block_die (stmt, context_die, depth);
12752    }
12753  else
12754    decls_for_scope (stmt, context_die, depth);
12755}
12756
12757/* Generate all of the decls declared within a given scope and (recursively)
12758   all of its sub-blocks.  */
12759
12760static void
12761decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12762{
12763  tree decl;
12764  tree subblocks;
12765
12766  /* Ignore NULL blocks.  */
12767  if (stmt == NULL_TREE)
12768    return;
12769
12770  if (TREE_USED (stmt))
12771    {
12772      /* Output the DIEs to represent all of the data objects and typedefs
12773	 declared directly within this block but not within any nested
12774	 sub-blocks.  Also, nested function and tag DIEs have been
12775	 generated with a parent of NULL; fix that up now.  */
12776      for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12777	{
12778	  dw_die_ref die;
12779
12780	  if (TREE_CODE (decl) == FUNCTION_DECL)
12781	    die = lookup_decl_die (decl);
12782	  else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12783	    die = lookup_type_die (TREE_TYPE (decl));
12784	  else
12785	    die = NULL;
12786
12787	  if (die != NULL && die->die_parent == NULL)
12788	    add_child_die (context_die, die);
12789	  /* Do not produce debug information for static variables since
12790	     these might be optimized out.  We are called for these later
12791	     in cgraph_varpool_analyze_pending_decls. */
12792	  if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12793	    ;
12794	  else
12795	    gen_decl_die (decl, context_die);
12796	}
12797    }
12798
12799  /* If we're at -g1, we're not interested in subblocks.  */
12800  if (debug_info_level <= DINFO_LEVEL_TERSE)
12801    return;
12802
12803  /* Output the DIEs to represent all sub-blocks (and the items declared
12804     therein) of this block.  */
12805  for (subblocks = BLOCK_SUBBLOCKS (stmt);
12806       subblocks != NULL;
12807       subblocks = BLOCK_CHAIN (subblocks))
12808    gen_block_die (subblocks, context_die, depth + 1);
12809}
12810
12811/* Is this a typedef we can avoid emitting?  */
12812
12813static inline int
12814is_redundant_typedef (tree decl)
12815{
12816  if (TYPE_DECL_IS_STUB (decl))
12817    return 1;
12818
12819  if (DECL_ARTIFICIAL (decl)
12820      && DECL_CONTEXT (decl)
12821      && is_tagged_type (DECL_CONTEXT (decl))
12822      && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12823      && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12824    /* Also ignore the artificial member typedef for the class name.  */
12825    return 1;
12826
12827  return 0;
12828}
12829
12830/* Returns the DIE for decl.  A DIE will always be returned.  */
12831
12832static dw_die_ref
12833force_decl_die (tree decl)
12834{
12835  dw_die_ref decl_die;
12836  unsigned saved_external_flag;
12837  tree save_fn = NULL_TREE;
12838  decl_die = lookup_decl_die (decl);
12839  if (!decl_die)
12840    {
12841      dw_die_ref context_die;
12842      tree decl_context = DECL_CONTEXT (decl);
12843      if (decl_context)
12844	{
12845	  /* Find die that represents this context.  */
12846	  if (TYPE_P (decl_context))
12847	    context_die = force_type_die (decl_context);
12848	  else
12849	    context_die = force_decl_die (decl_context);
12850	}
12851      else
12852	context_die = comp_unit_die;
12853
12854      decl_die = lookup_decl_die (decl);
12855      if (decl_die)
12856	return decl_die;
12857
12858      switch (TREE_CODE (decl))
12859	{
12860	case FUNCTION_DECL:
12861	  /* Clear current_function_decl, so that gen_subprogram_die thinks
12862	     that this is a declaration. At this point, we just want to force
12863	     declaration die.  */
12864	  save_fn = current_function_decl;
12865	  current_function_decl = NULL_TREE;
12866	  gen_subprogram_die (decl, context_die);
12867	  current_function_decl = save_fn;
12868	  break;
12869
12870	case VAR_DECL:
12871	  /* Set external flag to force declaration die. Restore it after
12872	   gen_decl_die() call.  */
12873	  saved_external_flag = DECL_EXTERNAL (decl);
12874	  DECL_EXTERNAL (decl) = 1;
12875	  gen_decl_die (decl, context_die);
12876	  DECL_EXTERNAL (decl) = saved_external_flag;
12877	  break;
12878
12879	case NAMESPACE_DECL:
12880	  dwarf2out_decl (decl);
12881	  break;
12882
12883	default:
12884	  gcc_unreachable ();
12885	}
12886
12887      /* We should be able to find the DIE now.  */
12888      if (!decl_die)
12889	decl_die = lookup_decl_die (decl);
12890      gcc_assert (decl_die);
12891    }
12892
12893  return decl_die;
12894}
12895
12896/* Returns the DIE for TYPE, that must not be a base type.  A DIE is
12897   always returned.  */
12898
12899static dw_die_ref
12900force_type_die (tree type)
12901{
12902  dw_die_ref type_die;
12903
12904  type_die = lookup_type_die (type);
12905  if (!type_die)
12906    {
12907      dw_die_ref context_die;
12908      if (TYPE_CONTEXT (type))
12909	{
12910	  if (TYPE_P (TYPE_CONTEXT (type)))
12911	    context_die = force_type_die (TYPE_CONTEXT (type));
12912	  else
12913	    context_die = force_decl_die (TYPE_CONTEXT (type));
12914	}
12915      else
12916	context_die = comp_unit_die;
12917
12918      type_die = lookup_type_die (type);
12919      if (type_die)
12920	return type_die;
12921      gen_type_die (type, context_die);
12922      type_die = lookup_type_die (type);
12923      gcc_assert (type_die);
12924    }
12925  return type_die;
12926}
12927
12928/* Force out any required namespaces to be able to output DECL,
12929   and return the new context_die for it, if it's changed.  */
12930
12931static dw_die_ref
12932setup_namespace_context (tree thing, dw_die_ref context_die)
12933{
12934  tree context = (DECL_P (thing)
12935		  ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12936  if (context && TREE_CODE (context) == NAMESPACE_DECL)
12937    /* Force out the namespace.  */
12938    context_die = force_decl_die (context);
12939
12940  return context_die;
12941}
12942
12943/* Emit a declaration DIE for THING (which is either a DECL or a tagged
12944   type) within its namespace, if appropriate.
12945
12946   For compatibility with older debuggers, namespace DIEs only contain
12947   declarations; all definitions are emitted at CU scope.  */
12948
12949static void
12950declare_in_namespace (tree thing, dw_die_ref context_die)
12951{
12952  dw_die_ref ns_context;
12953
12954  if (debug_info_level <= DINFO_LEVEL_TERSE)
12955    return;
12956
12957  /* If this decl is from an inlined function, then don't try to emit it in its
12958     namespace, as we will get confused.  It would have already been emitted
12959     when the abstract instance of the inline function was emitted anyways.  */
12960  if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12961    return;
12962
12963  ns_context = setup_namespace_context (thing, context_die);
12964
12965  if (ns_context != context_die)
12966    {
12967      if (DECL_P (thing))
12968	gen_decl_die (thing, ns_context);
12969      else
12970	gen_type_die (thing, ns_context);
12971    }
12972}
12973
12974/* Generate a DIE for a namespace or namespace alias.  */
12975
12976static void
12977gen_namespace_die (tree decl)
12978{
12979  dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12980
12981  /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12982     they are an alias of.  */
12983  if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12984    {
12985      /* Output a real namespace.  */
12986      dw_die_ref namespace_die
12987	= new_die (DW_TAG_namespace, context_die, decl);
12988      add_name_and_src_coords_attributes (namespace_die, decl);
12989      equate_decl_number_to_die (decl, namespace_die);
12990    }
12991  else
12992    {
12993      /* Output a namespace alias.  */
12994
12995      /* Force out the namespace we are an alias of, if necessary.  */
12996      dw_die_ref origin_die
12997	= force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12998
12999      /* Now create the namespace alias DIE.  */
13000      dw_die_ref namespace_die
13001	= new_die (DW_TAG_imported_declaration, context_die, decl);
13002      add_name_and_src_coords_attributes (namespace_die, decl);
13003      add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13004      equate_decl_number_to_die (decl, namespace_die);
13005    }
13006}
13007
13008/* Generate Dwarf debug information for a decl described by DECL.  */
13009
13010static void
13011gen_decl_die (tree decl, dw_die_ref context_die)
13012{
13013  tree origin;
13014
13015  if (DECL_P (decl) && DECL_IGNORED_P (decl))
13016    return;
13017
13018  switch (TREE_CODE (decl))
13019    {
13020    case ERROR_MARK:
13021      break;
13022
13023    case CONST_DECL:
13024      /* The individual enumerators of an enum type get output when we output
13025	 the Dwarf representation of the relevant enum type itself.  */
13026      break;
13027
13028    case FUNCTION_DECL:
13029      /* Don't output any DIEs to represent mere function declarations,
13030	 unless they are class members or explicit block externs.  */
13031      if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13032	  && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13033	break;
13034
13035#if 0
13036      /* FIXME */
13037      /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13038	 on local redeclarations of global functions.  That seems broken.  */
13039      if (current_function_decl != decl)
13040	/* This is only a declaration.  */;
13041#endif
13042
13043      /* If we're emitting a clone, emit info for the abstract instance.  */
13044      if (DECL_ORIGIN (decl) != decl)
13045	dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13046
13047      /* If we're emitting an out-of-line copy of an inline function,
13048	 emit info for the abstract instance and set up to refer to it.  */
13049      else if (cgraph_function_possibly_inlined_p (decl)
13050	       && ! DECL_ABSTRACT (decl)
13051	       && ! class_or_namespace_scope_p (context_die)
13052	       /* dwarf2out_abstract_function won't emit a die if this is just
13053		  a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13054		  that case, because that works only if we have a die.  */
13055	       && DECL_INITIAL (decl) != NULL_TREE)
13056	{
13057	  dwarf2out_abstract_function (decl);
13058	  set_decl_origin_self (decl);
13059	}
13060
13061      /* Otherwise we're emitting the primary DIE for this decl.  */
13062      else if (debug_info_level > DINFO_LEVEL_TERSE)
13063	{
13064	  /* Before we describe the FUNCTION_DECL itself, make sure that we
13065	     have described its return type.  */
13066	  gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13067
13068	  /* And its virtual context.  */
13069	  if (DECL_VINDEX (decl) != NULL_TREE)
13070	    gen_type_die (DECL_CONTEXT (decl), context_die);
13071
13072	  /* And its containing type.  */
13073	  origin = decl_class_context (decl);
13074	  if (origin != NULL_TREE)
13075	    gen_type_die_for_member (origin, decl, context_die);
13076
13077	  /* And its containing namespace.  */
13078	  declare_in_namespace (decl, context_die);
13079	}
13080
13081      /* Now output a DIE to represent the function itself.  */
13082      gen_subprogram_die (decl, context_die);
13083      break;
13084
13085    case TYPE_DECL:
13086      /* If we are in terse mode, don't generate any DIEs to represent any
13087	 actual typedefs.  */
13088      if (debug_info_level <= DINFO_LEVEL_TERSE)
13089	break;
13090
13091      /* In the special case of a TYPE_DECL node representing the declaration
13092	 of some type tag, if the given TYPE_DECL is marked as having been
13093	 instantiated from some other (original) TYPE_DECL node (e.g. one which
13094	 was generated within the original definition of an inline function) we
13095	 have to generate a special (abbreviated) DW_TAG_structure_type,
13096	 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13097      if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13098	{
13099	  gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13100	  break;
13101	}
13102
13103      if (is_redundant_typedef (decl))
13104	gen_type_die (TREE_TYPE (decl), context_die);
13105      else
13106	/* Output a DIE to represent the typedef itself.  */
13107	gen_typedef_die (decl, context_die);
13108      break;
13109
13110    case LABEL_DECL:
13111      if (debug_info_level >= DINFO_LEVEL_NORMAL)
13112	gen_label_die (decl, context_die);
13113      break;
13114
13115    case VAR_DECL:
13116    case RESULT_DECL:
13117      /* If we are in terse mode, don't generate any DIEs to represent any
13118	 variable declarations or definitions.  */
13119      if (debug_info_level <= DINFO_LEVEL_TERSE)
13120	break;
13121
13122      /* Output any DIEs that are needed to specify the type of this data
13123	 object.  */
13124      gen_type_die (TREE_TYPE (decl), context_die);
13125
13126      /* And its containing type.  */
13127      origin = decl_class_context (decl);
13128      if (origin != NULL_TREE)
13129	gen_type_die_for_member (origin, decl, context_die);
13130
13131      /* And its containing namespace.  */
13132      declare_in_namespace (decl, context_die);
13133
13134      /* Now output the DIE to represent the data object itself.  This gets
13135	 complicated because of the possibility that the VAR_DECL really
13136	 represents an inlined instance of a formal parameter for an inline
13137	 function.  */
13138      origin = decl_ultimate_origin (decl);
13139      if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13140	gen_formal_parameter_die (decl, context_die);
13141      else
13142	gen_variable_die (decl, context_die);
13143      break;
13144
13145    case FIELD_DECL:
13146      /* Ignore the nameless fields that are used to skip bits but handle C++
13147	 anonymous unions and structs.  */
13148      if (DECL_NAME (decl) != NULL_TREE
13149	  || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13150	  || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13151	{
13152	  gen_type_die (member_declared_type (decl), context_die);
13153	  gen_field_die (decl, context_die);
13154	}
13155      break;
13156
13157    case PARM_DECL:
13158      gen_type_die (TREE_TYPE (decl), context_die);
13159      gen_formal_parameter_die (decl, context_die);
13160      break;
13161
13162    case NAMESPACE_DECL:
13163      gen_namespace_die (decl);
13164      break;
13165
13166    default:
13167      /* Probably some frontend-internal decl.  Assume we don't care.  */
13168      gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13169      break;
13170    }
13171}
13172
13173/* Add Ada "use" clause information for SGI Workshop debugger.  */
13174
13175void
13176dwarf2out_add_library_unit_info (const char *filename, const char *context_list)
13177{
13178  unsigned int file_index;
13179
13180  if (filename != NULL)
13181    {
13182      dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
13183      tree context_list_decl
13184	= build_decl (LABEL_DECL, get_identifier (context_list),
13185		      void_type_node);
13186
13187      TREE_PUBLIC (context_list_decl) = TRUE;
13188      add_name_attribute (unit_die, context_list);
13189      file_index = lookup_filename (filename);
13190      add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
13191      add_pubname (context_list_decl, unit_die);
13192    }
13193}
13194
13195/* Output debug information for global decl DECL.  Called from toplev.c after
13196   compilation proper has finished.  */
13197
13198static void
13199dwarf2out_global_decl (tree decl)
13200{
13201  /* Output DWARF2 information for file-scope tentative data object
13202     declarations, file-scope (extern) function declarations (which had no
13203     corresponding body) and file-scope tagged type declarations and
13204     definitions which have not yet been forced out.  */
13205  if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13206    dwarf2out_decl (decl);
13207}
13208
13209/* Output debug information for type decl DECL.  Called from toplev.c
13210   and from language front ends (to record built-in types).  */
13211static void
13212dwarf2out_type_decl (tree decl, int local)
13213{
13214  if (!local)
13215    dwarf2out_decl (decl);
13216}
13217
13218/* Output debug information for imported module or decl.  */
13219
13220static void
13221dwarf2out_imported_module_or_decl (tree decl, tree context)
13222{
13223  dw_die_ref imported_die, at_import_die;
13224  dw_die_ref scope_die;
13225  unsigned file_index;
13226  expanded_location xloc;
13227
13228  if (debug_info_level <= DINFO_LEVEL_TERSE)
13229    return;
13230
13231  gcc_assert (decl);
13232
13233  /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13234     We need decl DIE for reference and scope die. First, get DIE for the decl
13235     itself.  */
13236
13237  /* Get the scope die for decl context. Use comp_unit_die for global module
13238     or decl. If die is not found for non globals, force new die.  */
13239  if (!context)
13240    scope_die = comp_unit_die;
13241  else if (TYPE_P (context))
13242    scope_die = force_type_die (context);
13243  else
13244    scope_die = force_decl_die (context);
13245
13246  /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13247  if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13248    {
13249      if (is_base_type (TREE_TYPE (decl)))
13250	at_import_die = base_type_die (TREE_TYPE (decl));
13251      else
13252	at_import_die = force_type_die (TREE_TYPE (decl));
13253    }
13254  else
13255    {
13256      at_import_die = lookup_decl_die (decl);
13257      if (!at_import_die)
13258	{
13259	  /* If we're trying to avoid duplicate debug info, we may not have
13260	     emitted the member decl for this field.  Emit it now.  */
13261	  if (TREE_CODE (decl) == FIELD_DECL)
13262	    {
13263	      tree type = DECL_CONTEXT (decl);
13264	      dw_die_ref type_context_die;
13265
13266	      if (TYPE_CONTEXT (type))
13267		if (TYPE_P (TYPE_CONTEXT (type)))
13268		  type_context_die = force_type_die (TYPE_CONTEXT (type));
13269	      else
13270		type_context_die = force_decl_die (TYPE_CONTEXT (type));
13271	      else
13272		type_context_die = comp_unit_die;
13273	      gen_type_die_for_member (type, decl, type_context_die);
13274	    }
13275	  at_import_die = force_decl_die (decl);
13276	}
13277    }
13278
13279  /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13280  if (TREE_CODE (decl) == NAMESPACE_DECL)
13281    imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13282  else
13283    imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13284
13285  xloc = expand_location (input_location);
13286  file_index = lookup_filename (xloc.file);
13287  add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13288  add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13289  add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13290}
13291
13292/* Write the debugging output for DECL.  */
13293
13294void
13295dwarf2out_decl (tree decl)
13296{
13297  dw_die_ref context_die = comp_unit_die;
13298
13299  switch (TREE_CODE (decl))
13300    {
13301    case ERROR_MARK:
13302      return;
13303
13304    case FUNCTION_DECL:
13305      /* What we would really like to do here is to filter out all mere
13306	 file-scope declarations of file-scope functions which are never
13307	 referenced later within this translation unit (and keep all of ones
13308	 that *are* referenced later on) but we aren't clairvoyant, so we have
13309	 no idea which functions will be referenced in the future (i.e. later
13310	 on within the current translation unit). So here we just ignore all
13311	 file-scope function declarations which are not also definitions.  If
13312	 and when the debugger needs to know something about these functions,
13313	 it will have to hunt around and find the DWARF information associated
13314	 with the definition of the function.
13315
13316	 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13317	 nodes represent definitions and which ones represent mere
13318	 declarations.  We have to check DECL_INITIAL instead. That's because
13319	 the C front-end supports some weird semantics for "extern inline"
13320	 function definitions.  These can get inlined within the current
13321	 translation unit (and thus, we need to generate Dwarf info for their
13322	 abstract instances so that the Dwarf info for the concrete inlined
13323	 instances can have something to refer to) but the compiler never
13324	 generates any out-of-lines instances of such things (despite the fact
13325	 that they *are* definitions).
13326
13327	 The important point is that the C front-end marks these "extern
13328	 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13329	 them anyway. Note that the C++ front-end also plays some similar games
13330	 for inline function definitions appearing within include files which
13331	 also contain `#pragma interface' pragmas.  */
13332      if (DECL_INITIAL (decl) == NULL_TREE)
13333	return;
13334
13335      /* If we're a nested function, initially use a parent of NULL; if we're
13336	 a plain function, this will be fixed up in decls_for_scope.  If
13337	 we're a method, it will be ignored, since we already have a DIE.  */
13338      if (decl_function_context (decl)
13339	  /* But if we're in terse mode, we don't care about scope.  */
13340	  && debug_info_level > DINFO_LEVEL_TERSE)
13341	context_die = NULL;
13342      break;
13343
13344    case VAR_DECL:
13345      /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13346	 declaration and if the declaration was never even referenced from
13347	 within this entire compilation unit.  We suppress these DIEs in
13348	 order to save space in the .debug section (by eliminating entries
13349	 which are probably useless).  Note that we must not suppress
13350	 block-local extern declarations (whether used or not) because that
13351	 would screw-up the debugger's name lookup mechanism and cause it to
13352	 miss things which really ought to be in scope at a given point.  */
13353      if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13354	return;
13355
13356      /* For local statics lookup proper context die.  */
13357      if (TREE_STATIC (decl) && decl_function_context (decl))
13358	context_die = lookup_decl_die (DECL_CONTEXT (decl));
13359
13360      /* If we are in terse mode, don't generate any DIEs to represent any
13361	 variable declarations or definitions.  */
13362      if (debug_info_level <= DINFO_LEVEL_TERSE)
13363	return;
13364      break;
13365
13366    case NAMESPACE_DECL:
13367      if (debug_info_level <= DINFO_LEVEL_TERSE)
13368	return;
13369      if (lookup_decl_die (decl) != NULL)
13370        return;
13371      break;
13372
13373    case TYPE_DECL:
13374      /* Don't emit stubs for types unless they are needed by other DIEs.  */
13375      if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13376	return;
13377
13378      /* Don't bother trying to generate any DIEs to represent any of the
13379	 normal built-in types for the language we are compiling.  */
13380      if (DECL_IS_BUILTIN (decl))
13381	{
13382	  /* OK, we need to generate one for `bool' so GDB knows what type
13383	     comparisons have.  */
13384	  if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13385	       == DW_LANG_C_plus_plus)
13386	      && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13387	      && ! DECL_IGNORED_P (decl))
13388	    modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13389
13390	  return;
13391	}
13392
13393      /* If we are in terse mode, don't generate any DIEs for types.  */
13394      if (debug_info_level <= DINFO_LEVEL_TERSE)
13395	return;
13396
13397      /* If we're a function-scope tag, initially use a parent of NULL;
13398	 this will be fixed up in decls_for_scope.  */
13399      if (decl_function_context (decl))
13400	context_die = NULL;
13401
13402      break;
13403
13404    default:
13405      return;
13406    }
13407
13408  gen_decl_die (decl, context_die);
13409}
13410
13411/* Output a marker (i.e. a label) for the beginning of the generated code for
13412   a lexical block.  */
13413
13414static void
13415dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13416		       unsigned int blocknum)
13417{
13418  current_function_section (current_function_decl);
13419  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13420}
13421
13422/* Output a marker (i.e. a label) for the end of the generated code for a
13423   lexical block.  */
13424
13425static void
13426dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13427{
13428  current_function_section (current_function_decl);
13429  ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13430}
13431
13432/* Returns nonzero if it is appropriate not to emit any debugging
13433   information for BLOCK, because it doesn't contain any instructions.
13434
13435   Don't allow this for blocks with nested functions or local classes
13436   as we would end up with orphans, and in the presence of scheduling
13437   we may end up calling them anyway.  */
13438
13439static bool
13440dwarf2out_ignore_block (tree block)
13441{
13442  tree decl;
13443
13444  for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13445    if (TREE_CODE (decl) == FUNCTION_DECL
13446	|| (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13447      return 0;
13448
13449  return 1;
13450}
13451
13452/* Lookup FILE_NAME (in the list of filenames that we know about here in
13453   dwarf2out.c) and return its "index".  The index of each (known) filename is
13454   just a unique number which is associated with only that one filename.  We
13455   need such numbers for the sake of generating labels (in the .debug_sfnames
13456   section) and references to those files numbers (in the .debug_srcinfo
13457   and.debug_macinfo sections).  If the filename given as an argument is not
13458   found in our current list, add it to the list and assign it the next
13459   available unique index number.  In order to speed up searches, we remember
13460   the index of the filename was looked up last.  This handles the majority of
13461   all searches.  */
13462
13463static unsigned
13464lookup_filename (const char *file_name)
13465{
13466  size_t i, n;
13467  char *save_file_name;
13468
13469  /* Check to see if the file name that was searched on the previous
13470     call matches this file name.  If so, return the index.  */
13471  if (file_table_last_lookup_index != 0)
13472    {
13473      const char *last
13474	= VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13475      if (strcmp (file_name, last) == 0)
13476	return file_table_last_lookup_index;
13477    }
13478
13479  /* Didn't match the previous lookup, search the table.  */
13480  n = VARRAY_ACTIVE_SIZE (file_table);
13481  for (i = 1; i < n; i++)
13482    if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13483      {
13484	file_table_last_lookup_index = i;
13485	return i;
13486      }
13487
13488  /* Add the new entry to the end of the filename table.  */
13489  file_table_last_lookup_index = n;
13490  save_file_name = (char *) ggc_strdup (file_name);
13491  VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13492  VARRAY_PUSH_UINT (file_table_emitted, 0);
13493
13494  /* If the assembler is emitting the file table, and we aren't eliminating
13495     unused debug types, then we must emit .file here.  If we are eliminating
13496     unused debug types, then this will be done by the maybe_emit_file call in
13497     prune_unused_types_walk_attribs.  */
13498
13499  if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13500    return maybe_emit_file (i);
13501
13502  return i;
13503}
13504
13505/* If the assembler will construct the file table, then translate the compiler
13506   internal file table number into the assembler file table number, and emit
13507   a .file directive if we haven't already emitted one yet.  The file table
13508   numbers are different because we prune debug info for unused variables and
13509   types, which may include filenames.  */
13510
13511static int
13512maybe_emit_file (int fileno)
13513{
13514  if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13515    {
13516      if (!VARRAY_UINT (file_table_emitted, fileno))
13517	{
13518	  VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13519	  fprintf (asm_out_file, "\t.file %u ",
13520		   VARRAY_UINT (file_table_emitted, fileno));
13521	  output_quoted_string (asm_out_file,
13522				VARRAY_CHAR_PTR (file_table, fileno));
13523	  fputc ('\n', asm_out_file);
13524	}
13525      return VARRAY_UINT (file_table_emitted, fileno);
13526    }
13527  else
13528    return fileno;
13529}
13530
13531/* Initialize the compiler internal file table.  */
13532
13533static void
13534init_file_table (void)
13535{
13536  /* Allocate the initial hunk of the file_table.  */
13537  VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13538  VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13539
13540  /* Skip the first entry - file numbers begin at 1.  */
13541  VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13542  VARRAY_PUSH_UINT (file_table_emitted, 0);
13543  file_table_last_lookup_index = 0;
13544}
13545
13546/* Called by the final INSN scan whenever we see a var location.  We
13547   use it to drop labels in the right places, and throw the location in
13548   our lookup table.  */
13549
13550static void
13551dwarf2out_var_location (rtx loc_note)
13552{
13553  char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13554  struct var_loc_node *newloc;
13555  rtx prev_insn;
13556  static rtx last_insn;
13557  static const char *last_label;
13558  tree decl;
13559
13560  if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13561    return;
13562  prev_insn = PREV_INSN (loc_note);
13563
13564  newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13565  /* If the insn we processed last time is the previous insn
13566     and it is also a var location note, use the label we emitted
13567     last time.  */
13568  if (last_insn != NULL_RTX
13569      && last_insn == prev_insn
13570      && NOTE_P (prev_insn)
13571      && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13572    {
13573      newloc->label = last_label;
13574    }
13575  else
13576    {
13577      ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13578      ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13579      loclabel_num++;
13580      newloc->label = ggc_strdup (loclabel);
13581    }
13582  newloc->var_loc_note = loc_note;
13583  newloc->next = NULL;
13584
13585  if (cfun
13586      && (last_text_section == in_unlikely_executed_text
13587	  || (last_text_section == in_named
13588	      && last_text_section_name == cfun->unlikely_text_section_name)))
13589    newloc->section_label = cfun->cold_section_label;
13590  else
13591    newloc->section_label = text_section_label;
13592
13593  last_insn = loc_note;
13594  last_label = newloc->label;
13595  decl = NOTE_VAR_LOCATION_DECL (loc_note);
13596  if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl)
13597      && DECL_P (DECL_DEBUG_EXPR (decl)))
13598    decl = DECL_DEBUG_EXPR (decl);
13599  add_var_loc_to_decl (decl, newloc);
13600}
13601
13602/* We need to reset the locations at the beginning of each
13603   function. We can't do this in the end_function hook, because the
13604   declarations that use the locations won't have been outputted when
13605   that hook is called.  */
13606
13607static void
13608dwarf2out_begin_function (tree unused ATTRIBUTE_UNUSED)
13609{
13610  htab_empty (decl_loc_table);
13611}
13612
13613/* Output a label to mark the beginning of a source code line entry
13614   and record information relating to this source line, in
13615   'line_info_table' for later output of the .debug_line section.  */
13616
13617static void
13618dwarf2out_source_line (unsigned int line, const char *filename)
13619{
13620  if (debug_info_level >= DINFO_LEVEL_NORMAL
13621      && line != 0)
13622    {
13623      current_function_section (current_function_decl);
13624
13625      /* If requested, emit something human-readable.  */
13626      if (flag_debug_asm)
13627	fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13628		 filename, line);
13629
13630      if (DWARF2_ASM_LINE_DEBUG_INFO)
13631	{
13632	  unsigned file_num = lookup_filename (filename);
13633
13634	  file_num = maybe_emit_file (file_num);
13635
13636	  /* Emit the .loc directive understood by GNU as.  */
13637	  fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13638
13639	  /* Indicate that line number info exists.  */
13640	  line_info_table_in_use++;
13641
13642	  /* Indicate that multiple line number tables exist.  */
13643	  if (DECL_SECTION_NAME (current_function_decl))
13644	    separate_line_info_table_in_use++;
13645	}
13646      else if (DECL_SECTION_NAME (current_function_decl))
13647	{
13648	  dw_separate_line_info_ref line_info;
13649	  targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13650				     separate_line_info_table_in_use);
13651
13652	  /* Expand the line info table if necessary.  */
13653	  if (separate_line_info_table_in_use
13654	      == separate_line_info_table_allocated)
13655	    {
13656	      separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13657	      separate_line_info_table
13658		= ggc_realloc (separate_line_info_table,
13659			       separate_line_info_table_allocated
13660			       * sizeof (dw_separate_line_info_entry));
13661	      memset (separate_line_info_table
13662		       + separate_line_info_table_in_use,
13663		      0,
13664		      (LINE_INFO_TABLE_INCREMENT
13665		       * sizeof (dw_separate_line_info_entry)));
13666	    }
13667
13668	  /* Add the new entry at the end of the line_info_table.  */
13669	  line_info
13670	    = &separate_line_info_table[separate_line_info_table_in_use++];
13671	  line_info->dw_file_num = lookup_filename (filename);
13672	  line_info->dw_line_num = line;
13673	  line_info->function = current_function_funcdef_no;
13674	}
13675      else
13676	{
13677	  dw_line_info_ref line_info;
13678
13679	  targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13680				     line_info_table_in_use);
13681
13682	  /* Expand the line info table if necessary.  */
13683	  if (line_info_table_in_use == line_info_table_allocated)
13684	    {
13685	      line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13686	      line_info_table
13687		= ggc_realloc (line_info_table,
13688			       (line_info_table_allocated
13689				* sizeof (dw_line_info_entry)));
13690	      memset (line_info_table + line_info_table_in_use, 0,
13691		      LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13692	    }
13693
13694	  /* Add the new entry at the end of the line_info_table.  */
13695	  line_info = &line_info_table[line_info_table_in_use++];
13696	  line_info->dw_file_num = lookup_filename (filename);
13697	  line_info->dw_line_num = line;
13698	}
13699    }
13700}
13701
13702/* Record the beginning of a new source file.  */
13703
13704static void
13705dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13706{
13707  if (flag_eliminate_dwarf2_dups)
13708    {
13709      /* Record the beginning of the file for break_out_includes.  */
13710      dw_die_ref bincl_die;
13711
13712      bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13713      add_AT_string (bincl_die, DW_AT_name, filename);
13714    }
13715
13716  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13717    {
13718      int fileno;
13719
13720      named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13721      dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13722      dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13723				   lineno);
13724
13725      fileno = maybe_emit_file (lookup_filename (filename));
13726      dw2_asm_output_data_uleb128 (fileno, "Filename we just started");
13727    }
13728}
13729
13730/* Record the end of a source file.  */
13731
13732static void
13733dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13734{
13735  if (flag_eliminate_dwarf2_dups)
13736    /* Record the end of the file for break_out_includes.  */
13737    new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13738
13739  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13740    {
13741      named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13742      dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13743    }
13744}
13745
13746/* Called from debug_define in toplev.c.  The `buffer' parameter contains
13747   the tail part of the directive line, i.e. the part which is past the
13748   initial whitespace, #, whitespace, directive-name, whitespace part.  */
13749
13750static void
13751dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13752		  const char *buffer ATTRIBUTE_UNUSED)
13753{
13754  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13755    {
13756      named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13757      dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13758      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13759      dw2_asm_output_nstring (buffer, -1, "The macro");
13760    }
13761}
13762
13763/* Called from debug_undef in toplev.c.  The `buffer' parameter contains
13764   the tail part of the directive line, i.e. the part which is past the
13765   initial whitespace, #, whitespace, directive-name, whitespace part.  */
13766
13767static void
13768dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13769		 const char *buffer ATTRIBUTE_UNUSED)
13770{
13771  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13772    {
13773      named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13774      dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13775      dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13776      dw2_asm_output_nstring (buffer, -1, "The macro");
13777    }
13778}
13779
13780/* Set up for Dwarf output at the start of compilation.  */
13781
13782static void
13783dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13784{
13785  init_file_table ();
13786
13787  /* Allocate the decl_die_table.  */
13788  decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13789				    decl_die_table_eq, NULL);
13790
13791  /* Allocate the decl_loc_table.  */
13792  decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13793				    decl_loc_table_eq, NULL);
13794
13795  /* Allocate the initial hunk of the decl_scope_table.  */
13796  decl_scope_table = VEC_alloc (tree, gc, 256);
13797
13798  /* Allocate the initial hunk of the abbrev_die_table.  */
13799  abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13800					* sizeof (dw_die_ref));
13801  abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13802  /* Zero-th entry is allocated, but unused.  */
13803  abbrev_die_table_in_use = 1;
13804
13805  /* Allocate the initial hunk of the line_info_table.  */
13806  line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13807				       * sizeof (dw_line_info_entry));
13808  line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13809
13810  /* Zero-th entry is allocated, but unused.  */
13811  line_info_table_in_use = 1;
13812
13813  /* Generate the initial DIE for the .debug section.  Note that the (string)
13814     value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13815     will (typically) be a relative pathname and that this pathname should be
13816     taken as being relative to the directory from which the compiler was
13817     invoked when the given (base) source file was compiled.  We will fill
13818     in this value in dwarf2out_finish.  */
13819  comp_unit_die = gen_compile_unit_die (NULL);
13820
13821  incomplete_types = VEC_alloc (tree, gc, 64);
13822
13823  used_rtx_array = VEC_alloc (rtx, gc, 32);
13824
13825  ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13826  ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13827			       DEBUG_ABBREV_SECTION_LABEL, 0);
13828  ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13829  ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
13830			       COLD_TEXT_SECTION_LABEL, 0);
13831  ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13832
13833  ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13834			       DEBUG_INFO_SECTION_LABEL, 0);
13835  ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13836			       DEBUG_LINE_SECTION_LABEL, 0);
13837  ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13838			       DEBUG_RANGES_SECTION_LABEL, 0);
13839  named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
13840  ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13841  named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
13842  ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13843  named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
13844  ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13845
13846  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13847    {
13848      named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
13849      ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13850				   DEBUG_MACINFO_SECTION_LABEL, 0);
13851      ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13852    }
13853
13854  text_section ();
13855  ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13856  if (flag_reorder_blocks_and_partition)
13857    {
13858      unlikely_text_section ();
13859      ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13860    }
13861}
13862
13863/* A helper function for dwarf2out_finish called through
13864   ht_forall.  Emit one queued .debug_str string.  */
13865
13866static int
13867output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13868{
13869  struct indirect_string_node *node = (struct indirect_string_node *) *h;
13870
13871  if (node->form == DW_FORM_strp)
13872    {
13873      named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
13874      ASM_OUTPUT_LABEL (asm_out_file, node->label);
13875      assemble_string (node->str, strlen (node->str) + 1);
13876    }
13877
13878  return 1;
13879}
13880
13881
13882
13883/* Clear the marks for a die and its children.
13884   Be cool if the mark isn't set.  */
13885
13886static void
13887prune_unmark_dies (dw_die_ref die)
13888{
13889  dw_die_ref c;
13890  die->die_mark = 0;
13891  for (c = die->die_child; c; c = c->die_sib)
13892    prune_unmark_dies (c);
13893}
13894
13895
13896/* Given DIE that we're marking as used, find any other dies
13897   it references as attributes and mark them as used.  */
13898
13899static void
13900prune_unused_types_walk_attribs (dw_die_ref die)
13901{
13902  dw_attr_ref a;
13903
13904  for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13905    {
13906      if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13907	{
13908	  /* A reference to another DIE.
13909	     Make sure that it will get emitted.  */
13910	  prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13911	}
13912      else if (a->dw_attr == DW_AT_decl_file || a->dw_attr == DW_AT_call_file)
13913	{
13914	  /* A reference to a file.  Make sure the file name is emitted.  */
13915	  a->dw_attr_val.v.val_unsigned =
13916	    maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13917	}
13918    }
13919}
13920
13921
13922/* Mark DIE as being used.  If DOKIDS is true, then walk down
13923   to DIE's children.  */
13924
13925static void
13926prune_unused_types_mark (dw_die_ref die, int dokids)
13927{
13928  dw_die_ref c;
13929
13930  if (die->die_mark == 0)
13931    {
13932      /* We haven't done this node yet.  Mark it as used.  */
13933      die->die_mark = 1;
13934
13935      /* We also have to mark its parents as used.
13936	 (But we don't want to mark our parents' kids due to this.)  */
13937      if (die->die_parent)
13938	prune_unused_types_mark (die->die_parent, 0);
13939
13940      /* Mark any referenced nodes.  */
13941      prune_unused_types_walk_attribs (die);
13942
13943      /* If this node is a specification,
13944         also mark the definition, if it exists.  */
13945      if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13946        prune_unused_types_mark (die->die_definition, 1);
13947    }
13948
13949  if (dokids && die->die_mark != 2)
13950    {
13951      /* We need to walk the children, but haven't done so yet.
13952	 Remember that we've walked the kids.  */
13953      die->die_mark = 2;
13954
13955      /* Walk them.  */
13956      for (c = die->die_child; c; c = c->die_sib)
13957	{
13958	  /* If this is an array type, we need to make sure our
13959	     kids get marked, even if they're types.  */
13960	  if (die->die_tag == DW_TAG_array_type)
13961	    prune_unused_types_mark (c, 1);
13962	  else
13963	    prune_unused_types_walk (c);
13964	}
13965    }
13966}
13967
13968
13969/* Walk the tree DIE and mark types that we actually use.  */
13970
13971static void
13972prune_unused_types_walk (dw_die_ref die)
13973{
13974  dw_die_ref c;
13975
13976  /* Don't do anything if this node is already marked.  */
13977  if (die->die_mark)
13978    return;
13979
13980  switch (die->die_tag) {
13981  case DW_TAG_const_type:
13982  case DW_TAG_packed_type:
13983  case DW_TAG_pointer_type:
13984  case DW_TAG_reference_type:
13985  case DW_TAG_volatile_type:
13986  case DW_TAG_typedef:
13987  case DW_TAG_array_type:
13988  case DW_TAG_structure_type:
13989  case DW_TAG_union_type:
13990  case DW_TAG_class_type:
13991  case DW_TAG_friend:
13992  case DW_TAG_variant_part:
13993  case DW_TAG_enumeration_type:
13994  case DW_TAG_subroutine_type:
13995  case DW_TAG_string_type:
13996  case DW_TAG_set_type:
13997  case DW_TAG_subrange_type:
13998  case DW_TAG_ptr_to_member_type:
13999  case DW_TAG_file_type:
14000    /* It's a type node --- don't mark it.  */
14001    return;
14002
14003  default:
14004    /* Mark everything else.  */
14005    break;
14006  }
14007
14008  die->die_mark = 1;
14009
14010  /* Now, mark any dies referenced from here.  */
14011  prune_unused_types_walk_attribs (die);
14012
14013  /* Mark children.  */
14014  for (c = die->die_child; c; c = c->die_sib)
14015    prune_unused_types_walk (c);
14016}
14017
14018
14019/* Remove from the tree DIE any dies that aren't marked.  */
14020
14021static void
14022prune_unused_types_prune (dw_die_ref die)
14023{
14024  dw_die_ref c, p, n;
14025
14026  gcc_assert (die->die_mark);
14027
14028  p = NULL;
14029  for (c = die->die_child; c; c = n)
14030    {
14031      n = c->die_sib;
14032      if (c->die_mark)
14033	{
14034	  prune_unused_types_prune (c);
14035	  p = c;
14036	}
14037      else
14038	{
14039	  if (p)
14040	    p->die_sib = n;
14041	  else
14042	    die->die_child = n;
14043	  free_die (c);
14044	}
14045    }
14046}
14047
14048
14049/* Remove dies representing declarations that we never use.  */
14050
14051static void
14052prune_unused_types (void)
14053{
14054  unsigned int i;
14055  limbo_die_node *node;
14056
14057  /* Clear all the marks.  */
14058  prune_unmark_dies (comp_unit_die);
14059  for (node = limbo_die_list; node; node = node->next)
14060    prune_unmark_dies (node->die);
14061
14062  /* Set the mark on nodes that are actually used.  */
14063  prune_unused_types_walk (comp_unit_die);
14064  for (node = limbo_die_list; node; node = node->next)
14065    prune_unused_types_walk (node->die);
14066
14067  /* Also set the mark on nodes referenced from the
14068     pubname_table or arange_table.  */
14069  for (i = 0; i < pubname_table_in_use; i++)
14070    prune_unused_types_mark (pubname_table[i].die, 1);
14071  for (i = 0; i < arange_table_in_use; i++)
14072    prune_unused_types_mark (arange_table[i], 1);
14073
14074  /* Get rid of nodes that aren't marked.  */
14075  prune_unused_types_prune (comp_unit_die);
14076  for (node = limbo_die_list; node; node = node->next)
14077    prune_unused_types_prune (node->die);
14078
14079  /* Leave the marks clear.  */
14080  prune_unmark_dies (comp_unit_die);
14081  for (node = limbo_die_list; node; node = node->next)
14082    prune_unmark_dies (node->die);
14083}
14084
14085/* Output stuff that dwarf requires at the end of every file,
14086   and generate the DWARF-2 debugging info.  */
14087
14088static void
14089dwarf2out_finish (const char *filename)
14090{
14091  limbo_die_node *node, *next_node;
14092  dw_die_ref die = 0;
14093
14094  /* Add the name for the main input file now.  We delayed this from
14095     dwarf2out_init to avoid complications with PCH.  */
14096  add_name_attribute (comp_unit_die, filename);
14097  if (filename[0] != DIR_SEPARATOR)
14098    add_comp_dir_attribute (comp_unit_die);
14099  else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14100    {
14101      size_t i;
14102      for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
14103	if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
14104	    /* Don't add cwd for <built-in>.  */
14105	    && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
14106	  {
14107	    add_comp_dir_attribute (comp_unit_die);
14108	    break;
14109	  }
14110    }
14111
14112  /* Traverse the limbo die list, and add parent/child links.  The only
14113     dies without parents that should be here are concrete instances of
14114     inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14115     For concrete instances, we can get the parent die from the abstract
14116     instance.  */
14117  for (node = limbo_die_list; node; node = next_node)
14118    {
14119      next_node = node->next;
14120      die = node->die;
14121
14122      if (die->die_parent == NULL)
14123	{
14124	  dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14125
14126	  if (origin)
14127	    add_child_die (origin->die_parent, die);
14128	  else if (die == comp_unit_die)
14129	    ;
14130	  else if (errorcount > 0 || sorrycount > 0)
14131	    /* It's OK to be confused by errors in the input.  */
14132	    add_child_die (comp_unit_die, die);
14133	  else
14134	    {
14135	      /* In certain situations, the lexical block containing a
14136		 nested function can be optimized away, which results
14137		 in the nested function die being orphaned.  Likewise
14138		 with the return type of that nested function.  Force
14139		 this to be a child of the containing function.
14140
14141		 It may happen that even the containing function got fully
14142		 inlined and optimized out.  In that case we are lost and
14143		 assign the empty child.  This should not be big issue as
14144		 the function is likely unreachable too.  */
14145	      tree context = NULL_TREE;
14146
14147	      gcc_assert (node->created_for);
14148
14149	      if (DECL_P (node->created_for))
14150		context = DECL_CONTEXT (node->created_for);
14151	      else if (TYPE_P (node->created_for))
14152		context = TYPE_CONTEXT (node->created_for);
14153
14154	      gcc_assert (context
14155			  && (TREE_CODE (context) == FUNCTION_DECL
14156			      || TREE_CODE (context) == NAMESPACE_DECL));
14157
14158	      origin = lookup_decl_die (context);
14159	      if (origin)
14160	        add_child_die (origin, die);
14161	      else
14162	        add_child_die (comp_unit_die, die);
14163	    }
14164	}
14165    }
14166
14167  limbo_die_list = NULL;
14168
14169  /* Walk through the list of incomplete types again, trying once more to
14170     emit full debugging info for them.  */
14171  retry_incomplete_types ();
14172
14173  /* We need to reverse all the dies before break_out_includes, or
14174     we'll see the end of an include file before the beginning.  */
14175  reverse_all_dies (comp_unit_die);
14176
14177  if (flag_eliminate_unused_debug_types)
14178    prune_unused_types ();
14179
14180  /* Generate separate CUs for each of the include files we've seen.
14181     They will go into limbo_die_list.  */
14182  if (flag_eliminate_dwarf2_dups)
14183    break_out_includes (comp_unit_die);
14184
14185  /* Traverse the DIE's and add add sibling attributes to those DIE's
14186     that have children.  */
14187  add_sibling_attributes (comp_unit_die);
14188  for (node = limbo_die_list; node; node = node->next)
14189    add_sibling_attributes (node->die);
14190
14191  /* Output a terminator label for the .text section.  */
14192  text_section ();
14193  targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14194  if (flag_reorder_blocks_and_partition)
14195    {
14196      unlikely_text_section ();
14197      targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14198    }
14199
14200  /* Output the source line correspondence table.  We must do this
14201     even if there is no line information.  Otherwise, on an empty
14202     translation unit, we will generate a present, but empty,
14203     .debug_info section.  IRIX 6.5 `nm' will then complain when
14204     examining the file.  */
14205  if (! DWARF2_ASM_LINE_DEBUG_INFO)
14206    {
14207      named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
14208      output_line_info ();
14209    }
14210
14211  /* Output location list section if necessary.  */
14212  if (have_location_lists)
14213    {
14214      /* Output the location lists info.  */
14215      named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
14216      ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14217				   DEBUG_LOC_SECTION_LABEL, 0);
14218      ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14219      output_location_lists (die);
14220      have_location_lists = 0;
14221    }
14222
14223  /* We can only use the low/high_pc attributes if all of the code was
14224     in .text.  */
14225  if (!separate_line_info_table_in_use && !have_switched_text_section)
14226    {
14227      add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14228      add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14229    }
14230
14231  /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14232     "base address".  Use zero so that these addresses become absolute.  */
14233  else if (have_location_lists || ranges_table_in_use)
14234    add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14235
14236  if (debug_info_level >= DINFO_LEVEL_NORMAL)
14237    add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
14238		       debug_line_section_label);
14239
14240  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14241    add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14242
14243  /* Output all of the compilation units.  We put the main one last so that
14244     the offsets are available to output_pubnames.  */
14245  for (node = limbo_die_list; node; node = node->next)
14246    output_comp_unit (node->die, 0);
14247
14248  output_comp_unit (comp_unit_die, 0);
14249
14250  /* Output the abbreviation table.  */
14251  named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
14252  output_abbrev_section ();
14253
14254  /* Output public names table if necessary.  */
14255  if (pubname_table_in_use)
14256    {
14257      named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
14258      output_pubnames ();
14259    }
14260
14261  /* Output the address range information.  We only put functions in the arange
14262     table, so don't write it out if we don't have any.  */
14263  if (fde_table_in_use)
14264    {
14265      named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
14266      output_aranges ();
14267    }
14268
14269  /* Output ranges section if necessary.  */
14270  if (ranges_table_in_use)
14271    {
14272      named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
14273      ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14274      output_ranges ();
14275    }
14276
14277  /* Have to end the macro section.  */
14278  if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14279    {
14280      named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
14281      dw2_asm_output_data (1, 0, "End compilation unit");
14282    }
14283
14284  /* If we emitted any DW_FORM_strp form attribute, output the string
14285     table too.  */
14286  if (debug_str_hash)
14287    htab_traverse (debug_str_hash, output_indirect_string, NULL);
14288}
14289#else
14290
14291/* This should never be used, but its address is needed for comparisons.  */
14292const struct gcc_debug_hooks dwarf2_debug_hooks;
14293
14294#endif /* DWARF2_DEBUGGING_INFO */
14295
14296#include "gt-dwarf2out.h"
14297