OSDN Git Service

PR target/45870
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
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, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
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 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103     which are output in the debug info should end with a separator.  */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106    from generating indirect strings in DWARF2 debug information, for instance
107    if your target is stuck with an old version of GDB that is unable to
108    process them properly or uses VMS Debug.  */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 /* ??? Poison these here until it can be done generically.  They've been
116    totally replaced in this file; make sure it stays that way.  */
117 #undef DWARF2_UNWIND_INFO
118 #undef DWARF2_FRAME_INFO
119 #if (GCC_VERSION >= 3000)
120  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
121 #endif
122
123 #ifndef INCOMING_RETURN_ADDR_RTX
124 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
125 #endif
126
127 /* Map register numbers held in the call frame info that gcc has
128    collected using DWARF_FRAME_REGNUM to those that should be output in
129    .debug_frame and .eh_frame.  */
130 #ifndef DWARF2_FRAME_REG_OUT
131 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
132 #endif
133
134 /* Save the result of dwarf2out_do_frame across PCH.  */
135 static GTY(()) bool saved_do_cfi_asm = 0;
136
137 /* Decide whether we want to emit frame unwind information for the current
138    translation unit.  */
139
140 int
141 dwarf2out_do_frame (void)
142 {
143   /* We want to emit correct CFA location expressions or lists, so we
144      have to return true if we're going to output debug info, even if
145      we're not going to output frame or unwind info.  */
146   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
147     return true;
148
149   if (saved_do_cfi_asm)
150     return true;
151
152   if (targetm.debug_unwind_info () == UI_DWARF2)
153     return true;
154
155   if ((flag_unwind_tables || flag_exceptions)
156       && targetm.except_unwind_info () == UI_DWARF2)
157     return true;
158
159   return false;
160 }
161
162 /* Decide whether to emit frame unwind via assembler directives.  */
163
164 int
165 dwarf2out_do_cfi_asm (void)
166 {
167   int enc;
168
169 #ifdef MIPS_DEBUGGING_INFO
170   return false;
171 #endif
172   if (saved_do_cfi_asm)
173     return true;
174   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
175     return false;
176   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
177     return false;
178
179   /* Make sure the personality encoding is one the assembler can support.
180      In particular, aligned addresses can't be handled.  */
181   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
182   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
183     return false;
184   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
185   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
186     return false;
187
188   /* If we can't get the assembler to emit only .debug_frame, and we don't need
189      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
190   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
191       && !flag_unwind_tables && !flag_exceptions
192       && targetm.except_unwind_info () != UI_DWARF2)
193     return false;
194
195   saved_do_cfi_asm = true;
196   return true;
197 }
198
199 /* The size of the target's pointer type.  */
200 #ifndef PTR_SIZE
201 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
202 #endif
203
204 /* Array of RTXes referenced by the debugging information, which therefore
205    must be kept around forever.  */
206 static GTY(()) VEC(rtx,gc) *used_rtx_array;
207
208 /* A pointer to the base of a list of incomplete types which might be
209    completed at some later time.  incomplete_types_list needs to be a
210    VEC(tree,gc) because we want to tell the garbage collector about
211    it.  */
212 static GTY(()) VEC(tree,gc) *incomplete_types;
213
214 /* A pointer to the base of a table of references to declaration
215    scopes.  This table is a display which tracks the nesting
216    of declaration scopes at the current scope and containing
217    scopes.  This table is used to find the proper place to
218    define type declaration DIE's.  */
219 static GTY(()) VEC(tree,gc) *decl_scope_table;
220
221 /* Pointers to various DWARF2 sections.  */
222 static GTY(()) section *debug_info_section;
223 static GTY(()) section *debug_abbrev_section;
224 static GTY(()) section *debug_aranges_section;
225 static GTY(()) section *debug_macinfo_section;
226 static GTY(()) section *debug_line_section;
227 static GTY(()) section *debug_loc_section;
228 static GTY(()) section *debug_pubnames_section;
229 static GTY(()) section *debug_pubtypes_section;
230 static GTY(()) section *debug_dcall_section;
231 static GTY(()) section *debug_vcall_section;
232 static GTY(()) section *debug_str_section;
233 static GTY(()) section *debug_ranges_section;
234 static GTY(()) section *debug_frame_section;
235
236 /* Personality decl of current unit.  Used only when assembler does not support
237    personality CFI.  */
238 static GTY(()) rtx current_unit_personality;
239
240 /* How to start an assembler comment.  */
241 #ifndef ASM_COMMENT_START
242 #define ASM_COMMENT_START ";#"
243 #endif
244
245 typedef struct dw_cfi_struct *dw_cfi_ref;
246 typedef struct dw_fde_struct *dw_fde_ref;
247 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
248
249 /* Call frames are described using a sequence of Call Frame
250    Information instructions.  The register number, offset
251    and address fields are provided as possible operands;
252    their use is selected by the opcode field.  */
253
254 enum dw_cfi_oprnd_type {
255   dw_cfi_oprnd_unused,
256   dw_cfi_oprnd_reg_num,
257   dw_cfi_oprnd_offset,
258   dw_cfi_oprnd_addr,
259   dw_cfi_oprnd_loc
260 };
261
262 typedef union GTY(()) dw_cfi_oprnd_struct {
263   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
264   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
265   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
266   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
267 }
268 dw_cfi_oprnd;
269
270 typedef struct GTY(()) dw_cfi_struct {
271   dw_cfi_ref dw_cfi_next;
272   enum dwarf_call_frame_info dw_cfi_opc;
273   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
274     dw_cfi_oprnd1;
275   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
276     dw_cfi_oprnd2;
277 }
278 dw_cfi_node;
279
280 /* This is how we define the location of the CFA. We use to handle it
281    as REG + OFFSET all the time,  but now it can be more complex.
282    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
283    Instead of passing around REG and OFFSET, we pass a copy
284    of this structure.  */
285 typedef struct GTY(()) cfa_loc {
286   HOST_WIDE_INT offset;
287   HOST_WIDE_INT base_offset;
288   unsigned int reg;
289   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
290   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
291 } dw_cfa_location;
292
293 /* All call frame descriptions (FDE's) in the GCC generated DWARF
294    refer to a single Common Information Entry (CIE), defined at
295    the beginning of the .debug_frame section.  This use of a single
296    CIE obviates the need to keep track of multiple CIE's
297    in the DWARF generation routines below.  */
298
299 typedef struct GTY(()) dw_fde_struct {
300   tree decl;
301   const char *dw_fde_begin;
302   const char *dw_fde_current_label;
303   const char *dw_fde_end;
304   const char *dw_fde_vms_end_prologue;
305   const char *dw_fde_vms_begin_epilogue;
306   const char *dw_fde_hot_section_label;
307   const char *dw_fde_hot_section_end_label;
308   const char *dw_fde_unlikely_section_label;
309   const char *dw_fde_unlikely_section_end_label;
310   dw_cfi_ref dw_fde_cfi;
311   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
312   HOST_WIDE_INT stack_realignment;
313   unsigned funcdef_number;
314   /* Dynamic realign argument pointer register.  */
315   unsigned int drap_reg;
316   /* Virtual dynamic realign argument pointer register.  */
317   unsigned int vdrap_reg;
318   /* These 3 flags are copied from rtl_data in function.h.  */
319   unsigned all_throwers_are_sibcalls : 1;
320   unsigned uses_eh_lsda : 1;
321   unsigned nothrow : 1;
322   /* Whether we did stack realign in this call frame.  */
323   unsigned stack_realign : 1;
324   /* Whether dynamic realign argument pointer register has been saved.  */
325   unsigned drap_reg_saved: 1;
326   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
327   unsigned in_std_section : 1;
328   /* True iff dw_fde_unlikely_section_label is in text_section or
329      cold_text_section.  */
330   unsigned cold_in_std_section : 1;
331   /* True iff switched sections.  */
332   unsigned dw_fde_switched_sections : 1;
333   /* True iff switching from cold to hot section.  */
334   unsigned dw_fde_switched_cold_to_hot : 1;
335 }
336 dw_fde_node;
337
338 /* Maximum size (in bytes) of an artificially generated label.  */
339 #define MAX_ARTIFICIAL_LABEL_BYTES      30
340
341 /* The size of addresses as they appear in the Dwarf 2 data.
342    Some architectures use word addresses to refer to code locations,
343    but Dwarf 2 info always uses byte addresses.  On such machines,
344    Dwarf 2 addresses need to be larger than the architecture's
345    pointers.  */
346 #ifndef DWARF2_ADDR_SIZE
347 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
348 #endif
349
350 /* The size in bytes of a DWARF field indicating an offset or length
351    relative to a debug info section, specified to be 4 bytes in the
352    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
353    as PTR_SIZE.  */
354
355 #ifndef DWARF_OFFSET_SIZE
356 #define DWARF_OFFSET_SIZE 4
357 #endif
358
359 /* The size in bytes of a DWARF 4 type signature.  */
360
361 #ifndef DWARF_TYPE_SIGNATURE_SIZE
362 #define DWARF_TYPE_SIGNATURE_SIZE 8
363 #endif
364
365 /* According to the (draft) DWARF 3 specification, the initial length
366    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
367    bytes are 0xffffffff, followed by the length stored in the next 8
368    bytes.
369
370    However, the SGI/MIPS ABI uses an initial length which is equal to
371    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
372
373 #ifndef DWARF_INITIAL_LENGTH_SIZE
374 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
375 #endif
376
377 /* Round SIZE up to the nearest BOUNDARY.  */
378 #define DWARF_ROUND(SIZE,BOUNDARY) \
379   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
380
381 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
382 #ifndef DWARF_CIE_DATA_ALIGNMENT
383 #ifdef STACK_GROWS_DOWNWARD
384 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
385 #else
386 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
387 #endif
388 #endif
389
390 /* CIE identifier.  */
391 #if HOST_BITS_PER_WIDE_INT >= 64
392 #define DWARF_CIE_ID \
393   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
394 #else
395 #define DWARF_CIE_ID DW_CIE_ID
396 #endif
397
398 /* A pointer to the base of a table that contains frame description
399    information for each routine.  */
400 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
401
402 /* Number of elements currently allocated for fde_table.  */
403 static GTY(()) unsigned fde_table_allocated;
404
405 /* Number of elements in fde_table currently in use.  */
406 static GTY(()) unsigned fde_table_in_use;
407
408 /* Size (in elements) of increments by which we may expand the
409    fde_table.  */
410 #define FDE_TABLE_INCREMENT 256
411
412 /* Get the current fde_table entry we should use.  */
413
414 static inline dw_fde_ref
415 current_fde (void)
416 {
417   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
418 }
419
420 /* A list of call frame insns for the CIE.  */
421 static GTY(()) dw_cfi_ref cie_cfi_head;
422
423 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
424    attribute that accelerates the lookup of the FDE associated
425    with the subprogram.  This variable holds the table index of the FDE
426    associated with the current function (body) definition.  */
427 static unsigned current_funcdef_fde;
428
429 struct GTY(()) indirect_string_node {
430   const char *str;
431   unsigned int refcount;
432   enum dwarf_form form;
433   char *label;
434 };
435
436 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
437
438 /* True if the compilation unit has location entries that reference
439    debug strings.  */
440 static GTY(()) bool debug_str_hash_forced = false;
441
442 static GTY(()) int dw2_string_counter;
443 static GTY(()) unsigned long dwarf2out_cfi_label_num;
444
445 /* True if the compilation unit places functions in more than one section.  */
446 static GTY(()) bool have_multiple_function_sections = false;
447
448 /* Whether the default text and cold text sections have been used at all.  */
449
450 static GTY(()) bool text_section_used = false;
451 static GTY(()) bool cold_text_section_used = false;
452
453 /* The default cold text section.  */
454 static GTY(()) section *cold_text_section;
455
456 /* Forward declarations for functions defined in this file.  */
457
458 static char *stripattributes (const char *);
459 static const char *dwarf_cfi_name (unsigned);
460 static dw_cfi_ref new_cfi (void);
461 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
462 static void add_fde_cfi (const char *, dw_cfi_ref);
463 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
464 static void lookup_cfa (dw_cfa_location *);
465 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
466 static void initial_return_save (rtx);
467 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
468                                           HOST_WIDE_INT);
469 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
470 static void output_cfi_directive (dw_cfi_ref);
471 static void output_call_frame_info (int);
472 static void dwarf2out_note_section_used (void);
473 static bool clobbers_queued_reg_save (const_rtx);
474 static void dwarf2out_frame_debug_expr (rtx, const char *);
475
476 /* Support for complex CFA locations.  */
477 static void output_cfa_loc (dw_cfi_ref);
478 static void output_cfa_loc_raw (dw_cfi_ref);
479 static void get_cfa_from_loc_descr (dw_cfa_location *,
480                                     struct dw_loc_descr_struct *);
481 static struct dw_loc_descr_struct *build_cfa_loc
482   (dw_cfa_location *, HOST_WIDE_INT);
483 static struct dw_loc_descr_struct *build_cfa_aligned_loc
484   (HOST_WIDE_INT, HOST_WIDE_INT);
485 static void def_cfa_1 (const char *, dw_cfa_location *);
486 static struct dw_loc_descr_struct *mem_loc_descriptor
487   (rtx, enum machine_mode mode, enum var_init_status);
488
489 /* How to start an assembler comment.  */
490 #ifndef ASM_COMMENT_START
491 #define ASM_COMMENT_START ";#"
492 #endif
493
494 /* Data and reference forms for relocatable data.  */
495 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
496 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
497
498 #ifndef DEBUG_FRAME_SECTION
499 #define DEBUG_FRAME_SECTION     ".debug_frame"
500 #endif
501
502 #ifndef FUNC_BEGIN_LABEL
503 #define FUNC_BEGIN_LABEL        "LFB"
504 #endif
505
506 #ifndef FUNC_END_LABEL
507 #define FUNC_END_LABEL          "LFE"
508 #endif
509
510 #ifndef PROLOGUE_END_LABEL
511 #define PROLOGUE_END_LABEL      "LPE"
512 #endif
513
514 #ifndef EPILOGUE_BEGIN_LABEL
515 #define EPILOGUE_BEGIN_LABEL    "LEB"
516 #endif
517
518 #ifndef FRAME_BEGIN_LABEL
519 #define FRAME_BEGIN_LABEL       "Lframe"
520 #endif
521 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
522 #define CIE_END_LABEL           "LECIE"
523 #define FDE_LABEL               "LSFDE"
524 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
525 #define FDE_END_LABEL           "LEFDE"
526 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
527 #define LINE_NUMBER_END_LABEL   "LELT"
528 #define LN_PROLOG_AS_LABEL      "LASLTP"
529 #define LN_PROLOG_END_LABEL     "LELTP"
530 #define DIE_LABEL_PREFIX        "DW"
531
532 /* The DWARF 2 CFA column which tracks the return address.  Normally this
533    is the column for PC, or the first column after all of the hard
534    registers.  */
535 #ifndef DWARF_FRAME_RETURN_COLUMN
536 #ifdef PC_REGNUM
537 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
538 #else
539 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
540 #endif
541 #endif
542
543 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
544    default, we just provide columns for all registers.  */
545 #ifndef DWARF_FRAME_REGNUM
546 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
547 #endif
548 \f
549 /* Hook used by __throw.  */
550
551 rtx
552 expand_builtin_dwarf_sp_column (void)
553 {
554   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
555   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
556 }
557
558 /* Return a pointer to a copy of the section string name S with all
559    attributes stripped off, and an asterisk prepended (for assemble_name).  */
560
561 static inline char *
562 stripattributes (const char *s)
563 {
564   char *stripped = XNEWVEC (char, strlen (s) + 2);
565   char *p = stripped;
566
567   *p++ = '*';
568
569   while (*s && *s != ',')
570     *p++ = *s++;
571
572   *p = '\0';
573   return stripped;
574 }
575
576 /* MEM is a memory reference for the register size table, each element of
577    which has mode MODE.  Initialize column C as a return address column.  */
578
579 static void
580 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
581 {
582   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
583   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
584   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
585 }
586
587 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
588
589 static inline HOST_WIDE_INT
590 div_data_align (HOST_WIDE_INT off)
591 {
592   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
593   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
594   return r;
595 }
596
597 /* Return true if we need a signed version of a given opcode
598    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
599
600 static inline bool
601 need_data_align_sf_opcode (HOST_WIDE_INT off)
602 {
603   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
604 }
605
606 /* Generate code to initialize the register size table.  */
607
608 void
609 expand_builtin_init_dwarf_reg_sizes (tree address)
610 {
611   unsigned int i;
612   enum machine_mode mode = TYPE_MODE (char_type_node);
613   rtx addr = expand_normal (address);
614   rtx mem = gen_rtx_MEM (BLKmode, addr);
615   bool wrote_return_column = false;
616
617   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
618     {
619       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
620
621       if (rnum < DWARF_FRAME_REGISTERS)
622         {
623           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
624           enum machine_mode save_mode = reg_raw_mode[i];
625           HOST_WIDE_INT size;
626
627           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
628             save_mode = choose_hard_reg_mode (i, 1, true);
629           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
630             {
631               if (save_mode == VOIDmode)
632                 continue;
633               wrote_return_column = true;
634             }
635           size = GET_MODE_SIZE (save_mode);
636           if (offset < 0)
637             continue;
638
639           emit_move_insn (adjust_address (mem, mode, offset),
640                           gen_int_mode (size, mode));
641         }
642     }
643
644   if (!wrote_return_column)
645     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
646
647 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
648   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
649 #endif
650
651   targetm.init_dwarf_reg_sizes_extra (address);
652 }
653
654 /* Convert a DWARF call frame info. operation to its string name */
655
656 static const char *
657 dwarf_cfi_name (unsigned int cfi_opc)
658 {
659   switch (cfi_opc)
660     {
661     case DW_CFA_advance_loc:
662       return "DW_CFA_advance_loc";
663     case DW_CFA_offset:
664       return "DW_CFA_offset";
665     case DW_CFA_restore:
666       return "DW_CFA_restore";
667     case DW_CFA_nop:
668       return "DW_CFA_nop";
669     case DW_CFA_set_loc:
670       return "DW_CFA_set_loc";
671     case DW_CFA_advance_loc1:
672       return "DW_CFA_advance_loc1";
673     case DW_CFA_advance_loc2:
674       return "DW_CFA_advance_loc2";
675     case DW_CFA_advance_loc4:
676       return "DW_CFA_advance_loc4";
677     case DW_CFA_offset_extended:
678       return "DW_CFA_offset_extended";
679     case DW_CFA_restore_extended:
680       return "DW_CFA_restore_extended";
681     case DW_CFA_undefined:
682       return "DW_CFA_undefined";
683     case DW_CFA_same_value:
684       return "DW_CFA_same_value";
685     case DW_CFA_register:
686       return "DW_CFA_register";
687     case DW_CFA_remember_state:
688       return "DW_CFA_remember_state";
689     case DW_CFA_restore_state:
690       return "DW_CFA_restore_state";
691     case DW_CFA_def_cfa:
692       return "DW_CFA_def_cfa";
693     case DW_CFA_def_cfa_register:
694       return "DW_CFA_def_cfa_register";
695     case DW_CFA_def_cfa_offset:
696       return "DW_CFA_def_cfa_offset";
697
698     /* DWARF 3 */
699     case DW_CFA_def_cfa_expression:
700       return "DW_CFA_def_cfa_expression";
701     case DW_CFA_expression:
702       return "DW_CFA_expression";
703     case DW_CFA_offset_extended_sf:
704       return "DW_CFA_offset_extended_sf";
705     case DW_CFA_def_cfa_sf:
706       return "DW_CFA_def_cfa_sf";
707     case DW_CFA_def_cfa_offset_sf:
708       return "DW_CFA_def_cfa_offset_sf";
709
710     /* SGI/MIPS specific */
711     case DW_CFA_MIPS_advance_loc8:
712       return "DW_CFA_MIPS_advance_loc8";
713
714     /* GNU extensions */
715     case DW_CFA_GNU_window_save:
716       return "DW_CFA_GNU_window_save";
717     case DW_CFA_GNU_args_size:
718       return "DW_CFA_GNU_args_size";
719     case DW_CFA_GNU_negative_offset_extended:
720       return "DW_CFA_GNU_negative_offset_extended";
721
722     default:
723       return "DW_CFA_<unknown>";
724     }
725 }
726
727 /* Return a pointer to a newly allocated Call Frame Instruction.  */
728
729 static inline dw_cfi_ref
730 new_cfi (void)
731 {
732   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
733
734   cfi->dw_cfi_next = NULL;
735   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
736   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
737
738   return cfi;
739 }
740
741 /* Add a Call Frame Instruction to list of instructions.  */
742
743 static inline void
744 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
745 {
746   dw_cfi_ref *p;
747   dw_fde_ref fde = current_fde ();
748
749   /* When DRAP is used, CFA is defined with an expression.  Redefine
750      CFA may lead to a different CFA value.   */
751   /* ??? Of course, this heuristic fails when we're annotating epilogues,
752      because of course we'll always want to redefine the CFA back to the
753      stack pointer on the way out.  Where should we move this check?  */
754   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
755     switch (cfi->dw_cfi_opc)
756       {
757         case DW_CFA_def_cfa_register:
758         case DW_CFA_def_cfa_offset:
759         case DW_CFA_def_cfa_offset_sf:
760         case DW_CFA_def_cfa:
761         case DW_CFA_def_cfa_sf:
762           gcc_unreachable ();
763
764         default:
765           break;
766       }
767
768   /* Find the end of the chain.  */
769   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
770     ;
771
772   *p = cfi;
773 }
774
775 /* Generate a new label for the CFI info to refer to.  FORCE is true
776    if a label needs to be output even when using .cfi_* directives.  */
777
778 char *
779 dwarf2out_cfi_label (bool force)
780 {
781   static char label[20];
782
783   if (!force && dwarf2out_do_cfi_asm ())
784     {
785       /* In this case, we will be emitting the asm directive instead of
786          the label, so just return a placeholder to keep the rest of the
787          interfaces happy.  */
788       strcpy (label, "<do not output>");
789     }
790   else
791     {
792       int num = dwarf2out_cfi_label_num++;
793       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
794       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
795     }
796
797   return label;
798 }
799
800 /* True if remember_state should be emitted before following CFI directive.  */
801 static bool emit_cfa_remember;
802
803 /* True if any CFI directives were emitted at the current insn.  */
804 static bool any_cfis_emitted;
805
806 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
807    or to the CIE if LABEL is NULL.  */
808
809 static void
810 add_fde_cfi (const char *label, dw_cfi_ref cfi)
811 {
812   dw_cfi_ref *list_head;
813
814   if (emit_cfa_remember)
815     {
816       dw_cfi_ref cfi_remember;
817
818       /* Emit the state save.  */
819       emit_cfa_remember = false;
820       cfi_remember = new_cfi ();
821       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
822       add_fde_cfi (label, cfi_remember);
823     }
824
825   list_head = &cie_cfi_head;
826
827   if (dwarf2out_do_cfi_asm ())
828     {
829       if (label)
830         {
831           dw_fde_ref fde = current_fde ();
832
833           gcc_assert (fde != NULL);
834
835           /* We still have to add the cfi to the list so that lookup_cfa
836              works later on.  When -g2 and above we even need to force
837              emitting of CFI labels and add to list a DW_CFA_set_loc for
838              convert_cfa_to_fb_loc_list purposes.  If we're generating
839              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
840              convert_cfa_to_fb_loc_list.  */
841           if (dwarf_version == 2
842               && debug_info_level > DINFO_LEVEL_TERSE
843               && (write_symbols == DWARF2_DEBUG
844                   || write_symbols == VMS_AND_DWARF2_DEBUG))
845             {
846               switch (cfi->dw_cfi_opc)
847                 {
848                 case DW_CFA_def_cfa_offset:
849                 case DW_CFA_def_cfa_offset_sf:
850                 case DW_CFA_def_cfa_register:
851                 case DW_CFA_def_cfa:
852                 case DW_CFA_def_cfa_sf:
853                 case DW_CFA_def_cfa_expression:
854                 case DW_CFA_restore_state:
855                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
856                     label = dwarf2out_cfi_label (true);
857
858                   if (fde->dw_fde_current_label == NULL
859                       || strcmp (label, fde->dw_fde_current_label) != 0)
860                     {
861                       dw_cfi_ref xcfi;
862
863                       label = xstrdup (label);
864
865                       /* Set the location counter to the new label.  */
866                       xcfi = new_cfi ();
867                       /* It doesn't metter whether DW_CFA_set_loc
868                          or DW_CFA_advance_loc4 is added here, those aren't
869                          emitted into assembly, only looked up by
870                          convert_cfa_to_fb_loc_list.  */
871                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
872                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
873                       add_cfi (&fde->dw_fde_cfi, xcfi);
874                       fde->dw_fde_current_label = label;
875                     }
876                   break;
877                 default:
878                   break;
879                 }
880             }
881
882           output_cfi_directive (cfi);
883
884           list_head = &fde->dw_fde_cfi;
885           any_cfis_emitted = true;
886         }
887       /* ??? If this is a CFI for the CIE, we don't emit.  This
888          assumes that the standard CIE contents that the assembler
889          uses matches the standard CIE contents that the compiler
890          uses.  This is probably a bad assumption.  I'm not quite
891          sure how to address this for now.  */
892     }
893   else if (label)
894     {
895       dw_fde_ref fde = current_fde ();
896
897       gcc_assert (fde != NULL);
898
899       if (*label == 0)
900         label = dwarf2out_cfi_label (false);
901
902       if (fde->dw_fde_current_label == NULL
903           || strcmp (label, fde->dw_fde_current_label) != 0)
904         {
905           dw_cfi_ref xcfi;
906
907           label = xstrdup (label);
908
909           /* Set the location counter to the new label.  */
910           xcfi = new_cfi ();
911           /* If we have a current label, advance from there, otherwise
912              set the location directly using set_loc.  */
913           xcfi->dw_cfi_opc = fde->dw_fde_current_label
914                              ? DW_CFA_advance_loc4
915                              : DW_CFA_set_loc;
916           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
917           add_cfi (&fde->dw_fde_cfi, xcfi);
918
919           fde->dw_fde_current_label = label;
920         }
921
922       list_head = &fde->dw_fde_cfi;
923       any_cfis_emitted = true;
924     }
925
926   add_cfi (list_head, cfi);
927 }
928
929 /* Subroutine of lookup_cfa.  */
930
931 static void
932 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
933 {
934   switch (cfi->dw_cfi_opc)
935     {
936     case DW_CFA_def_cfa_offset:
937     case DW_CFA_def_cfa_offset_sf:
938       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
939       break;
940     case DW_CFA_def_cfa_register:
941       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
942       break;
943     case DW_CFA_def_cfa:
944     case DW_CFA_def_cfa_sf:
945       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
946       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
947       break;
948     case DW_CFA_def_cfa_expression:
949       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
950       break;
951
952     case DW_CFA_remember_state:
953       gcc_assert (!remember->in_use);
954       *remember = *loc;
955       remember->in_use = 1;
956       break;
957     case DW_CFA_restore_state:
958       gcc_assert (remember->in_use);
959       *loc = *remember;
960       remember->in_use = 0;
961       break;
962
963     default:
964       break;
965     }
966 }
967
968 /* Find the previous value for the CFA.  */
969
970 static void
971 lookup_cfa (dw_cfa_location *loc)
972 {
973   dw_cfi_ref cfi;
974   dw_fde_ref fde;
975   dw_cfa_location remember;
976
977   memset (loc, 0, sizeof (*loc));
978   loc->reg = INVALID_REGNUM;
979   remember = *loc;
980
981   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
982     lookup_cfa_1 (cfi, loc, &remember);
983
984   fde = current_fde ();
985   if (fde)
986     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
987       lookup_cfa_1 (cfi, loc, &remember);
988 }
989
990 /* The current rule for calculating the DWARF2 canonical frame address.  */
991 static dw_cfa_location cfa;
992
993 /* The register used for saving registers to the stack, and its offset
994    from the CFA.  */
995 static dw_cfa_location cfa_store;
996
997 /* The current save location around an epilogue.  */
998 static dw_cfa_location cfa_remember;
999
1000 /* The running total of the size of arguments pushed onto the stack.  */
1001 static HOST_WIDE_INT args_size;
1002
1003 /* The last args_size we actually output.  */
1004 static HOST_WIDE_INT old_args_size;
1005
1006 /* Entry point to update the canonical frame address (CFA).
1007    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1008    calculated from REG+OFFSET.  */
1009
1010 void
1011 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1012 {
1013   dw_cfa_location loc;
1014   loc.indirect = 0;
1015   loc.base_offset = 0;
1016   loc.reg = reg;
1017   loc.offset = offset;
1018   def_cfa_1 (label, &loc);
1019 }
1020
1021 /* Determine if two dw_cfa_location structures define the same data.  */
1022
1023 static bool
1024 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1025 {
1026   return (loc1->reg == loc2->reg
1027           && loc1->offset == loc2->offset
1028           && loc1->indirect == loc2->indirect
1029           && (loc1->indirect == 0
1030               || loc1->base_offset == loc2->base_offset));
1031 }
1032
1033 /* This routine does the actual work.  The CFA is now calculated from
1034    the dw_cfa_location structure.  */
1035
1036 static void
1037 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1038 {
1039   dw_cfi_ref cfi;
1040   dw_cfa_location old_cfa, loc;
1041
1042   cfa = *loc_p;
1043   loc = *loc_p;
1044
1045   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1046     cfa_store.offset = loc.offset;
1047
1048   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1049   lookup_cfa (&old_cfa);
1050
1051   /* If nothing changed, no need to issue any call frame instructions.  */
1052   if (cfa_equal_p (&loc, &old_cfa))
1053     return;
1054
1055   cfi = new_cfi ();
1056
1057   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1058     {
1059       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1060          the CFA register did not change but the offset did.  The data
1061          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1062          in the assembler via the .cfi_def_cfa_offset directive.  */
1063       if (loc.offset < 0)
1064         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1065       else
1066         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1067       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1068     }
1069
1070 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1071   else if (loc.offset == old_cfa.offset
1072            && old_cfa.reg != INVALID_REGNUM
1073            && !loc.indirect
1074            && !old_cfa.indirect)
1075     {
1076       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1077          indicating the CFA register has changed to <register> but the
1078          offset has not changed.  */
1079       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1080       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1081     }
1082 #endif
1083
1084   else if (loc.indirect == 0)
1085     {
1086       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1087          indicating the CFA register has changed to <register> with
1088          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1089          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1090          directive.  */
1091       if (loc.offset < 0)
1092         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1093       else
1094         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1095       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1096       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1097     }
1098   else
1099     {
1100       /* Construct a DW_CFA_def_cfa_expression instruction to
1101          calculate the CFA using a full location expression since no
1102          register-offset pair is available.  */
1103       struct dw_loc_descr_struct *loc_list;
1104
1105       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1106       loc_list = build_cfa_loc (&loc, 0);
1107       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1108     }
1109
1110   add_fde_cfi (label, cfi);
1111 }
1112
1113 /* Add the CFI for saving a register.  REG is the CFA column number.
1114    LABEL is passed to add_fde_cfi.
1115    If SREG is -1, the register is saved at OFFSET from the CFA;
1116    otherwise it is saved in SREG.  */
1117
1118 static void
1119 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1120 {
1121   dw_cfi_ref cfi = new_cfi ();
1122   dw_fde_ref fde = current_fde ();
1123
1124   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1125
1126   /* When stack is aligned, store REG using DW_CFA_expression with
1127      FP.  */
1128   if (fde
1129       && fde->stack_realign
1130       && sreg == INVALID_REGNUM)
1131     {
1132       cfi->dw_cfi_opc = DW_CFA_expression;
1133       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1134       cfi->dw_cfi_oprnd2.dw_cfi_loc
1135         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1136     }
1137   else if (sreg == INVALID_REGNUM)
1138     {
1139       if (need_data_align_sf_opcode (offset))
1140         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1141       else if (reg & ~0x3f)
1142         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1143       else
1144         cfi->dw_cfi_opc = DW_CFA_offset;
1145       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1146     }
1147   else if (sreg == reg)
1148     cfi->dw_cfi_opc = DW_CFA_same_value;
1149   else
1150     {
1151       cfi->dw_cfi_opc = DW_CFA_register;
1152       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1153     }
1154
1155   add_fde_cfi (label, cfi);
1156 }
1157
1158 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1159    This CFI tells the unwinder that it needs to restore the window registers
1160    from the previous frame's window save area.
1161
1162    ??? Perhaps we should note in the CIE where windows are saved (instead of
1163    assuming 0(cfa)) and what registers are in the window.  */
1164
1165 void
1166 dwarf2out_window_save (const char *label)
1167 {
1168   dw_cfi_ref cfi = new_cfi ();
1169
1170   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1171   add_fde_cfi (label, cfi);
1172 }
1173
1174 /* Entry point for saving a register to the stack.  REG is the GCC register
1175    number.  LABEL and OFFSET are passed to reg_save.  */
1176
1177 void
1178 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1179 {
1180   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1181 }
1182
1183 /* Entry point for saving the return address in the stack.
1184    LABEL and OFFSET are passed to reg_save.  */
1185
1186 void
1187 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1188 {
1189   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1190 }
1191
1192 /* Entry point for saving the return address in a register.
1193    LABEL and SREG are passed to reg_save.  */
1194
1195 void
1196 dwarf2out_return_reg (const char *label, unsigned int sreg)
1197 {
1198   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1199 }
1200
1201 /* Record the initial position of the return address.  RTL is
1202    INCOMING_RETURN_ADDR_RTX.  */
1203
1204 static void
1205 initial_return_save (rtx rtl)
1206 {
1207   unsigned int reg = INVALID_REGNUM;
1208   HOST_WIDE_INT offset = 0;
1209
1210   switch (GET_CODE (rtl))
1211     {
1212     case REG:
1213       /* RA is in a register.  */
1214       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1215       break;
1216
1217     case MEM:
1218       /* RA is on the stack.  */
1219       rtl = XEXP (rtl, 0);
1220       switch (GET_CODE (rtl))
1221         {
1222         case REG:
1223           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1224           offset = 0;
1225           break;
1226
1227         case PLUS:
1228           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1229           offset = INTVAL (XEXP (rtl, 1));
1230           break;
1231
1232         case MINUS:
1233           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1234           offset = -INTVAL (XEXP (rtl, 1));
1235           break;
1236
1237         default:
1238           gcc_unreachable ();
1239         }
1240
1241       break;
1242
1243     case PLUS:
1244       /* The return address is at some offset from any value we can
1245          actually load.  For instance, on the SPARC it is in %i7+8. Just
1246          ignore the offset for now; it doesn't matter for unwinding frames.  */
1247       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1248       initial_return_save (XEXP (rtl, 0));
1249       return;
1250
1251     default:
1252       gcc_unreachable ();
1253     }
1254
1255   if (reg != DWARF_FRAME_RETURN_COLUMN)
1256     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1257 }
1258
1259 /* Given a SET, calculate the amount of stack adjustment it
1260    contains.  */
1261
1262 static HOST_WIDE_INT
1263 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1264                      HOST_WIDE_INT cur_offset)
1265 {
1266   const_rtx src = SET_SRC (pattern);
1267   const_rtx dest = SET_DEST (pattern);
1268   HOST_WIDE_INT offset = 0;
1269   enum rtx_code code;
1270
1271   if (dest == stack_pointer_rtx)
1272     {
1273       code = GET_CODE (src);
1274
1275       /* Assume (set (reg sp) (reg whatever)) sets args_size
1276          level to 0.  */
1277       if (code == REG && src != stack_pointer_rtx)
1278         {
1279           offset = -cur_args_size;
1280 #ifndef STACK_GROWS_DOWNWARD
1281           offset = -offset;
1282 #endif
1283           return offset - cur_offset;
1284         }
1285
1286       if (! (code == PLUS || code == MINUS)
1287           || XEXP (src, 0) != stack_pointer_rtx
1288           || !CONST_INT_P (XEXP (src, 1)))
1289         return 0;
1290
1291       /* (set (reg sp) (plus (reg sp) (const_int))) */
1292       offset = INTVAL (XEXP (src, 1));
1293       if (code == PLUS)
1294         offset = -offset;
1295       return offset;
1296     }
1297
1298   if (MEM_P (src) && !MEM_P (dest))
1299     dest = src;
1300   if (MEM_P (dest))
1301     {
1302       /* (set (mem (pre_dec (reg sp))) (foo)) */
1303       src = XEXP (dest, 0);
1304       code = GET_CODE (src);
1305
1306       switch (code)
1307         {
1308         case PRE_MODIFY:
1309         case POST_MODIFY:
1310           if (XEXP (src, 0) == stack_pointer_rtx)
1311             {
1312               rtx val = XEXP (XEXP (src, 1), 1);
1313               /* We handle only adjustments by constant amount.  */
1314               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1315                           && CONST_INT_P (val));
1316               offset = -INTVAL (val);
1317               break;
1318             }
1319           return 0;
1320
1321         case PRE_DEC:
1322         case POST_DEC:
1323           if (XEXP (src, 0) == stack_pointer_rtx)
1324             {
1325               offset = GET_MODE_SIZE (GET_MODE (dest));
1326               break;
1327             }
1328           return 0;
1329
1330         case PRE_INC:
1331         case POST_INC:
1332           if (XEXP (src, 0) == stack_pointer_rtx)
1333             {
1334               offset = -GET_MODE_SIZE (GET_MODE (dest));
1335               break;
1336             }
1337           return 0;
1338
1339         default:
1340           return 0;
1341         }
1342     }
1343   else
1344     return 0;
1345
1346   return offset;
1347 }
1348
1349 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1350    indexed by INSN_UID.  */
1351
1352 static HOST_WIDE_INT *barrier_args_size;
1353
1354 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1355
1356 static HOST_WIDE_INT
1357 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1358                              VEC (rtx, heap) **next)
1359 {
1360   HOST_WIDE_INT offset = 0;
1361   int i;
1362
1363   if (! RTX_FRAME_RELATED_P (insn))
1364     {
1365       if (prologue_epilogue_contains (insn))
1366         /* Nothing */;
1367       else if (GET_CODE (PATTERN (insn)) == SET)
1368         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1369       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1370                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1371         {
1372           /* There may be stack adjustments inside compound insns.  Search
1373              for them.  */
1374           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1375             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1376               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1377                                              cur_args_size, offset);
1378         }
1379     }
1380   else
1381     {
1382       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1383
1384       if (expr)
1385         {
1386           expr = XEXP (expr, 0);
1387           if (GET_CODE (expr) == PARALLEL
1388               || GET_CODE (expr) == SEQUENCE)
1389             for (i = 1; i < XVECLEN (expr, 0); i++)
1390               {
1391                 rtx elem = XVECEXP (expr, 0, i);
1392
1393                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1394                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1395               }
1396         }
1397     }
1398
1399 #ifndef STACK_GROWS_DOWNWARD
1400   offset = -offset;
1401 #endif
1402
1403   cur_args_size += offset;
1404   if (cur_args_size < 0)
1405     cur_args_size = 0;
1406
1407   if (JUMP_P (insn))
1408     {
1409       rtx dest = JUMP_LABEL (insn);
1410
1411       if (dest)
1412         {
1413           if (barrier_args_size [INSN_UID (dest)] < 0)
1414             {
1415               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1416               VEC_safe_push (rtx, heap, *next, dest);
1417             }
1418         }
1419     }
1420
1421   return cur_args_size;
1422 }
1423
1424 /* Walk the whole function and compute args_size on BARRIERs.  */
1425
1426 static void
1427 compute_barrier_args_size (void)
1428 {
1429   int max_uid = get_max_uid (), i;
1430   rtx insn;
1431   VEC (rtx, heap) *worklist, *next, *tmp;
1432
1433   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1434   for (i = 0; i < max_uid; i++)
1435     barrier_args_size[i] = -1;
1436
1437   worklist = VEC_alloc (rtx, heap, 20);
1438   next = VEC_alloc (rtx, heap, 20);
1439   insn = get_insns ();
1440   barrier_args_size[INSN_UID (insn)] = 0;
1441   VEC_quick_push (rtx, worklist, insn);
1442   for (;;)
1443     {
1444       while (!VEC_empty (rtx, worklist))
1445         {
1446           rtx prev, body, first_insn;
1447           HOST_WIDE_INT cur_args_size;
1448
1449           first_insn = insn = VEC_pop (rtx, worklist);
1450           cur_args_size = barrier_args_size[INSN_UID (insn)];
1451           prev = prev_nonnote_insn (insn);
1452           if (prev && BARRIER_P (prev))
1453             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1454
1455           for (; insn; insn = NEXT_INSN (insn))
1456             {
1457               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1458                 continue;
1459               if (BARRIER_P (insn))
1460                 break;
1461
1462               if (LABEL_P (insn))
1463                 {
1464                   if (insn == first_insn)
1465                     continue;
1466                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1467                     {
1468                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1469                       continue;
1470                     }
1471                   else
1472                     {
1473                       /* The insns starting with this label have been
1474                          already scanned or are in the worklist.  */
1475                       break;
1476                     }
1477                 }
1478
1479               body = PATTERN (insn);
1480               if (GET_CODE (body) == SEQUENCE)
1481                 {
1482                   HOST_WIDE_INT dest_args_size = cur_args_size;
1483                   for (i = 1; i < XVECLEN (body, 0); i++)
1484                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1485                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1486                       dest_args_size
1487                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1488                                                        dest_args_size, &next);
1489                     else
1490                       cur_args_size
1491                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1492                                                        cur_args_size, &next);
1493
1494                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1495                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1496                                                  dest_args_size, &next);
1497                   else
1498                     cur_args_size
1499                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1500                                                      cur_args_size, &next);
1501                 }
1502               else
1503                 cur_args_size
1504                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1505             }
1506         }
1507
1508       if (VEC_empty (rtx, next))
1509         break;
1510
1511       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1512       tmp = next;
1513       next = worklist;
1514       worklist = tmp;
1515       VEC_truncate (rtx, next, 0);
1516     }
1517
1518   VEC_free (rtx, heap, worklist);
1519   VEC_free (rtx, heap, next);
1520 }
1521
1522 /* Add a CFI to update the running total of the size of arguments
1523    pushed onto the stack.  */
1524
1525 static void
1526 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1527 {
1528   dw_cfi_ref cfi;
1529
1530   if (size == old_args_size)
1531     return;
1532
1533   old_args_size = size;
1534
1535   cfi = new_cfi ();
1536   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1537   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1538   add_fde_cfi (label, cfi);
1539 }
1540
1541 /* Record a stack adjustment of OFFSET bytes.  */
1542
1543 static void
1544 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1545 {
1546   if (cfa.reg == STACK_POINTER_REGNUM)
1547     cfa.offset += offset;
1548
1549   if (cfa_store.reg == STACK_POINTER_REGNUM)
1550     cfa_store.offset += offset;
1551
1552   if (ACCUMULATE_OUTGOING_ARGS)
1553     return;
1554
1555 #ifndef STACK_GROWS_DOWNWARD
1556   offset = -offset;
1557 #endif
1558
1559   args_size += offset;
1560   if (args_size < 0)
1561     args_size = 0;
1562
1563   def_cfa_1 (label, &cfa);
1564   if (flag_asynchronous_unwind_tables)
1565     dwarf2out_args_size (label, args_size);
1566 }
1567
1568 /* Check INSN to see if it looks like a push or a stack adjustment, and
1569    make a note of it if it does.  EH uses this information to find out
1570    how much extra space it needs to pop off the stack.  */
1571
1572 static void
1573 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1574 {
1575   HOST_WIDE_INT offset;
1576   const char *label;
1577   int i;
1578
1579   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1580      with this function.  Proper support would require all frame-related
1581      insns to be marked, and to be able to handle saving state around
1582      epilogues textually in the middle of the function.  */
1583   if (prologue_epilogue_contains (insn))
1584     return;
1585
1586   /* If INSN is an instruction from target of an annulled branch, the
1587      effects are for the target only and so current argument size
1588      shouldn't change at all.  */
1589   if (final_sequence
1590       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1591       && INSN_FROM_TARGET_P (insn))
1592     return;
1593
1594   /* If only calls can throw, and we have a frame pointer,
1595      save up adjustments until we see the CALL_INSN.  */
1596   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1597     {
1598       if (CALL_P (insn) && !after_p)
1599         {
1600           /* Extract the size of the args from the CALL rtx itself.  */
1601           insn = PATTERN (insn);
1602           if (GET_CODE (insn) == PARALLEL)
1603             insn = XVECEXP (insn, 0, 0);
1604           if (GET_CODE (insn) == SET)
1605             insn = SET_SRC (insn);
1606           gcc_assert (GET_CODE (insn) == CALL);
1607           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1608         }
1609       return;
1610     }
1611
1612   if (CALL_P (insn) && !after_p)
1613     {
1614       if (!flag_asynchronous_unwind_tables)
1615         dwarf2out_args_size ("", args_size);
1616       return;
1617     }
1618   else if (BARRIER_P (insn))
1619     {
1620       /* Don't call compute_barrier_args_size () if the only
1621          BARRIER is at the end of function.  */
1622       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1623         compute_barrier_args_size ();
1624       if (barrier_args_size == NULL)
1625         offset = 0;
1626       else
1627         {
1628           offset = barrier_args_size[INSN_UID (insn)];
1629           if (offset < 0)
1630             offset = 0;
1631         }
1632
1633       offset -= args_size;
1634 #ifndef STACK_GROWS_DOWNWARD
1635       offset = -offset;
1636 #endif
1637     }
1638   else if (GET_CODE (PATTERN (insn)) == SET)
1639     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1640   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1641            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1642     {
1643       /* There may be stack adjustments inside compound insns.  Search
1644          for them.  */
1645       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1646         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1647           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1648                                          args_size, offset);
1649     }
1650   else
1651     return;
1652
1653   if (offset == 0)
1654     return;
1655
1656   label = dwarf2out_cfi_label (false);
1657   dwarf2out_stack_adjust (offset, label);
1658 }
1659
1660 /* We delay emitting a register save until either (a) we reach the end
1661    of the prologue or (b) the register is clobbered.  This clusters
1662    register saves so that there are fewer pc advances.  */
1663
1664 struct GTY(()) queued_reg_save {
1665   struct queued_reg_save *next;
1666   rtx reg;
1667   HOST_WIDE_INT cfa_offset;
1668   rtx saved_reg;
1669 };
1670
1671 static GTY(()) struct queued_reg_save *queued_reg_saves;
1672
1673 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1674 struct GTY(()) reg_saved_in_data {
1675   rtx orig_reg;
1676   rtx saved_in_reg;
1677 };
1678
1679 /* A list of registers saved in other registers.
1680    The list intentionally has a small maximum capacity of 4; if your
1681    port needs more than that, you might consider implementing a
1682    more efficient data structure.  */
1683 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1684 static GTY(()) size_t num_regs_saved_in_regs;
1685
1686 static const char *last_reg_save_label;
1687
1688 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1689    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1690
1691 static void
1692 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1693 {
1694   struct queued_reg_save *q;
1695
1696   /* Duplicates waste space, but it's also necessary to remove them
1697      for correctness, since the queue gets output in reverse
1698      order.  */
1699   for (q = queued_reg_saves; q != NULL; q = q->next)
1700     if (REGNO (q->reg) == REGNO (reg))
1701       break;
1702
1703   if (q == NULL)
1704     {
1705       q = ggc_alloc_queued_reg_save ();
1706       q->next = queued_reg_saves;
1707       queued_reg_saves = q;
1708     }
1709
1710   q->reg = reg;
1711   q->cfa_offset = offset;
1712   q->saved_reg = sreg;
1713
1714   last_reg_save_label = label;
1715 }
1716
1717 /* Output all the entries in QUEUED_REG_SAVES.  */
1718
1719 void
1720 dwarf2out_flush_queued_reg_saves (void)
1721 {
1722   struct queued_reg_save *q;
1723
1724   for (q = queued_reg_saves; q; q = q->next)
1725     {
1726       size_t i;
1727       unsigned int reg, sreg;
1728
1729       for (i = 0; i < num_regs_saved_in_regs; i++)
1730         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1731           break;
1732       if (q->saved_reg && i == num_regs_saved_in_regs)
1733         {
1734           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1735           num_regs_saved_in_regs++;
1736         }
1737       if (i != num_regs_saved_in_regs)
1738         {
1739           regs_saved_in_regs[i].orig_reg = q->reg;
1740           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1741         }
1742
1743       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1744       if (q->saved_reg)
1745         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1746       else
1747         sreg = INVALID_REGNUM;
1748       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1749     }
1750
1751   queued_reg_saves = NULL;
1752   last_reg_save_label = NULL;
1753 }
1754
1755 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1756    location for?  Or, does it clobber a register which we've previously
1757    said that some other register is saved in, and for which we now
1758    have a new location for?  */
1759
1760 static bool
1761 clobbers_queued_reg_save (const_rtx insn)
1762 {
1763   struct queued_reg_save *q;
1764
1765   for (q = queued_reg_saves; q; q = q->next)
1766     {
1767       size_t i;
1768       if (modified_in_p (q->reg, insn))
1769         return true;
1770       for (i = 0; i < num_regs_saved_in_regs; i++)
1771         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1772             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1773           return true;
1774     }
1775
1776   return false;
1777 }
1778
1779 /* Entry point for saving the first register into the second.  */
1780
1781 void
1782 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1783 {
1784   size_t i;
1785   unsigned int regno, sregno;
1786
1787   for (i = 0; i < num_regs_saved_in_regs; i++)
1788     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1789       break;
1790   if (i == num_regs_saved_in_regs)
1791     {
1792       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1793       num_regs_saved_in_regs++;
1794     }
1795   regs_saved_in_regs[i].orig_reg = reg;
1796   regs_saved_in_regs[i].saved_in_reg = sreg;
1797
1798   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1799   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1800   reg_save (label, regno, sregno, 0);
1801 }
1802
1803 /* What register, if any, is currently saved in REG?  */
1804
1805 static rtx
1806 reg_saved_in (rtx reg)
1807 {
1808   unsigned int regn = REGNO (reg);
1809   size_t i;
1810   struct queued_reg_save *q;
1811
1812   for (q = queued_reg_saves; q; q = q->next)
1813     if (q->saved_reg && regn == REGNO (q->saved_reg))
1814       return q->reg;
1815
1816   for (i = 0; i < num_regs_saved_in_regs; i++)
1817     if (regs_saved_in_regs[i].saved_in_reg
1818         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1819       return regs_saved_in_regs[i].orig_reg;
1820
1821   return NULL_RTX;
1822 }
1823
1824
1825 /* A temporary register holding an integral value used in adjusting SP
1826    or setting up the store_reg.  The "offset" field holds the integer
1827    value, not an offset.  */
1828 static dw_cfa_location cfa_temp;
1829
1830 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1831
1832 static void
1833 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1834 {
1835   memset (&cfa, 0, sizeof (cfa));
1836
1837   switch (GET_CODE (pat))
1838     {
1839     case PLUS:
1840       cfa.reg = REGNO (XEXP (pat, 0));
1841       cfa.offset = INTVAL (XEXP (pat, 1));
1842       break;
1843
1844     case REG:
1845       cfa.reg = REGNO (pat);
1846       break;
1847
1848     case MEM:
1849       cfa.indirect = 1;
1850       pat = XEXP (pat, 0);
1851       if (GET_CODE (pat) == PLUS)
1852         {
1853           cfa.base_offset = INTVAL (XEXP (pat, 1));
1854           pat = XEXP (pat, 0);
1855         }
1856       cfa.reg = REGNO (pat);
1857       break;
1858
1859     default:
1860       /* Recurse and define an expression.  */
1861       gcc_unreachable ();
1862     }
1863
1864   def_cfa_1 (label, &cfa);
1865 }
1866
1867 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1868
1869 static void
1870 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1871 {
1872   rtx src, dest;
1873
1874   gcc_assert (GET_CODE (pat) == SET);
1875   dest = XEXP (pat, 0);
1876   src = XEXP (pat, 1);
1877
1878   switch (GET_CODE (src))
1879     {
1880     case PLUS:
1881       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1882       cfa.offset -= INTVAL (XEXP (src, 1));
1883       break;
1884
1885     case REG:
1886         break;
1887
1888     default:
1889         gcc_unreachable ();
1890     }
1891
1892   cfa.reg = REGNO (dest);
1893   gcc_assert (cfa.indirect == 0);
1894
1895   def_cfa_1 (label, &cfa);
1896 }
1897
1898 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1899
1900 static void
1901 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1902 {
1903   HOST_WIDE_INT offset;
1904   rtx src, addr, span;
1905
1906   src = XEXP (set, 1);
1907   addr = XEXP (set, 0);
1908   gcc_assert (MEM_P (addr));
1909   addr = XEXP (addr, 0);
1910
1911   /* As documented, only consider extremely simple addresses.  */
1912   switch (GET_CODE (addr))
1913     {
1914     case REG:
1915       gcc_assert (REGNO (addr) == cfa.reg);
1916       offset = -cfa.offset;
1917       break;
1918     case PLUS:
1919       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1920       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1921       break;
1922     default:
1923       gcc_unreachable ();
1924     }
1925
1926   span = targetm.dwarf_register_span (src);
1927
1928   /* ??? We'd like to use queue_reg_save, but we need to come up with
1929      a different flushing heuristic for epilogues.  */
1930   if (!span)
1931     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1932   else
1933     {
1934       /* We have a PARALLEL describing where the contents of SRC live.
1935          Queue register saves for each piece of the PARALLEL.  */
1936       int par_index;
1937       int limit;
1938       HOST_WIDE_INT span_offset = offset;
1939
1940       gcc_assert (GET_CODE (span) == PARALLEL);
1941
1942       limit = XVECLEN (span, 0);
1943       for (par_index = 0; par_index < limit; par_index++)
1944         {
1945           rtx elem = XVECEXP (span, 0, par_index);
1946
1947           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1948                     INVALID_REGNUM, span_offset);
1949           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1950         }
1951     }
1952 }
1953
1954 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1955
1956 static void
1957 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1958 {
1959   rtx src, dest;
1960   unsigned sregno, dregno;
1961
1962   src = XEXP (set, 1);
1963   dest = XEXP (set, 0);
1964
1965   if (src == pc_rtx)
1966     sregno = DWARF_FRAME_RETURN_COLUMN;
1967   else
1968     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1969
1970   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1971
1972   /* ??? We'd like to use queue_reg_save, but we need to come up with
1973      a different flushing heuristic for epilogues.  */
1974   reg_save (label, sregno, dregno, 0);
1975 }
1976
1977 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1978
1979 static void
1980 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
1981 {
1982   rtx src, dest, span;
1983   dw_cfi_ref cfi = new_cfi ();
1984
1985   dest = SET_DEST (set);
1986   src = SET_SRC (set);
1987
1988   gcc_assert (REG_P (src));
1989   gcc_assert (MEM_P (dest));
1990
1991   span = targetm.dwarf_register_span (src);
1992   gcc_assert (!span);
1993
1994   cfi->dw_cfi_opc = DW_CFA_expression;
1995   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
1996   cfi->dw_cfi_oprnd2.dw_cfi_loc
1997     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
1998                           VAR_INIT_STATUS_INITIALIZED);
1999
2000   /* ??? We'd like to use queue_reg_save, were the interface different,
2001      and, as above, we could manage flushing for epilogues.  */
2002   add_fde_cfi (label, cfi);
2003 }
2004
2005 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2006
2007 static void
2008 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2009 {
2010   dw_cfi_ref cfi = new_cfi ();
2011   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2012
2013   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2014   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2015
2016   add_fde_cfi (label, cfi);
2017 }
2018
2019 /* Record call frame debugging information for an expression EXPR,
2020    which either sets SP or FP (adjusting how we calculate the frame
2021    address) or saves a register to the stack or another register.
2022    LABEL indicates the address of EXPR.
2023
2024    This function encodes a state machine mapping rtxes to actions on
2025    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2026    users need not read the source code.
2027
2028   The High-Level Picture
2029
2030   Changes in the register we use to calculate the CFA: Currently we
2031   assume that if you copy the CFA register into another register, we
2032   should take the other one as the new CFA register; this seems to
2033   work pretty well.  If it's wrong for some target, it's simple
2034   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2035
2036   Changes in the register we use for saving registers to the stack:
2037   This is usually SP, but not always.  Again, we deduce that if you
2038   copy SP into another register (and SP is not the CFA register),
2039   then the new register is the one we will be using for register
2040   saves.  This also seems to work.
2041
2042   Register saves: There's not much guesswork about this one; if
2043   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2044   register save, and the register used to calculate the destination
2045   had better be the one we think we're using for this purpose.
2046   It's also assumed that a copy from a call-saved register to another
2047   register is saving that register if RTX_FRAME_RELATED_P is set on
2048   that instruction.  If the copy is from a call-saved register to
2049   the *same* register, that means that the register is now the same
2050   value as in the caller.
2051
2052   Except: If the register being saved is the CFA register, and the
2053   offset is nonzero, we are saving the CFA, so we assume we have to
2054   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2055   the intent is to save the value of SP from the previous frame.
2056
2057   In addition, if a register has previously been saved to a different
2058   register,
2059
2060   Invariants / Summaries of Rules
2061
2062   cfa          current rule for calculating the CFA.  It usually
2063                consists of a register and an offset.
2064   cfa_store    register used by prologue code to save things to the stack
2065                cfa_store.offset is the offset from the value of
2066                cfa_store.reg to the actual CFA
2067   cfa_temp     register holding an integral value.  cfa_temp.offset
2068                stores the value, which will be used to adjust the
2069                stack pointer.  cfa_temp is also used like cfa_store,
2070                to track stores to the stack via fp or a temp reg.
2071
2072   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2073                with cfa.reg as the first operand changes the cfa.reg and its
2074                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2075                cfa_temp.offset.
2076
2077   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2078                expression yielding a constant.  This sets cfa_temp.reg
2079                and cfa_temp.offset.
2080
2081   Rule 5:      Create a new register cfa_store used to save items to the
2082                stack.
2083
2084   Rules 10-14: Save a register to the stack.  Define offset as the
2085                difference of the original location and cfa_store's
2086                location (or cfa_temp's location if cfa_temp is used).
2087
2088   Rules 16-20: If AND operation happens on sp in prologue, we assume
2089                stack is realigned.  We will use a group of DW_OP_XXX
2090                expressions to represent the location of the stored
2091                register instead of CFA+offset.
2092
2093   The Rules
2094
2095   "{a,b}" indicates a choice of a xor b.
2096   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2097
2098   Rule 1:
2099   (set <reg1> <reg2>:cfa.reg)
2100   effects: cfa.reg = <reg1>
2101            cfa.offset unchanged
2102            cfa_temp.reg = <reg1>
2103            cfa_temp.offset = cfa.offset
2104
2105   Rule 2:
2106   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2107                               {<const_int>,<reg>:cfa_temp.reg}))
2108   effects: cfa.reg = sp if fp used
2109            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2110            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2111              if cfa_store.reg==sp
2112
2113   Rule 3:
2114   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2115   effects: cfa.reg = fp
2116            cfa_offset += +/- <const_int>
2117
2118   Rule 4:
2119   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2120   constraints: <reg1> != fp
2121                <reg1> != sp
2122   effects: cfa.reg = <reg1>
2123            cfa_temp.reg = <reg1>
2124            cfa_temp.offset = cfa.offset
2125
2126   Rule 5:
2127   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2128   constraints: <reg1> != fp
2129                <reg1> != sp
2130   effects: cfa_store.reg = <reg1>
2131            cfa_store.offset = cfa.offset - cfa_temp.offset
2132
2133   Rule 6:
2134   (set <reg> <const_int>)
2135   effects: cfa_temp.reg = <reg>
2136            cfa_temp.offset = <const_int>
2137
2138   Rule 7:
2139   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2140   effects: cfa_temp.reg = <reg1>
2141            cfa_temp.offset |= <const_int>
2142
2143   Rule 8:
2144   (set <reg> (high <exp>))
2145   effects: none
2146
2147   Rule 9:
2148   (set <reg> (lo_sum <exp> <const_int>))
2149   effects: cfa_temp.reg = <reg>
2150            cfa_temp.offset = <const_int>
2151
2152   Rule 10:
2153   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2154   effects: cfa_store.offset -= <const_int>
2155            cfa.offset = cfa_store.offset if cfa.reg == sp
2156            cfa.reg = sp
2157            cfa.base_offset = -cfa_store.offset
2158
2159   Rule 11:
2160   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2161   effects: cfa_store.offset += -/+ mode_size(mem)
2162            cfa.offset = cfa_store.offset if cfa.reg == sp
2163            cfa.reg = sp
2164            cfa.base_offset = -cfa_store.offset
2165
2166   Rule 12:
2167   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2168
2169        <reg2>)
2170   effects: cfa.reg = <reg1>
2171            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2172
2173   Rule 13:
2174   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2175   effects: cfa.reg = <reg1>
2176            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2177
2178   Rule 14:
2179   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2180   effects: cfa.reg = <reg1>
2181            cfa.base_offset = -cfa_temp.offset
2182            cfa_temp.offset -= mode_size(mem)
2183
2184   Rule 15:
2185   (set <reg> {unspec, unspec_volatile})
2186   effects: target-dependent
2187
2188   Rule 16:
2189   (set sp (and: sp <const_int>))
2190   constraints: cfa_store.reg == sp
2191   effects: current_fde.stack_realign = 1
2192            cfa_store.offset = 0
2193            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2194
2195   Rule 17:
2196   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2197   effects: cfa_store.offset += -/+ mode_size(mem)
2198
2199   Rule 18:
2200   (set (mem ({pre_inc, pre_dec} sp)) fp)
2201   constraints: fde->stack_realign == 1
2202   effects: cfa_store.offset = 0
2203            cfa.reg != HARD_FRAME_POINTER_REGNUM
2204
2205   Rule 19:
2206   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2207   constraints: fde->stack_realign == 1
2208                && cfa.offset == 0
2209                && cfa.indirect == 0
2210                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2211   effects: Use DW_CFA_def_cfa_expression to define cfa
2212            cfa.reg == fde->drap_reg  */
2213
2214 static void
2215 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2216 {
2217   rtx src, dest, span;
2218   HOST_WIDE_INT offset;
2219   dw_fde_ref fde;
2220
2221   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2222      the PARALLEL independently. The first element is always processed if
2223      it is a SET. This is for backward compatibility.   Other elements
2224      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2225      flag is set in them.  */
2226   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2227     {
2228       int par_index;
2229       int limit = XVECLEN (expr, 0);
2230       rtx elem;
2231
2232       /* PARALLELs have strict read-modify-write semantics, so we
2233          ought to evaluate every rvalue before changing any lvalue.
2234          It's cumbersome to do that in general, but there's an
2235          easy approximation that is enough for all current users:
2236          handle register saves before register assignments.  */
2237       if (GET_CODE (expr) == PARALLEL)
2238         for (par_index = 0; par_index < limit; par_index++)
2239           {
2240             elem = XVECEXP (expr, 0, par_index);
2241             if (GET_CODE (elem) == SET
2242                 && MEM_P (SET_DEST (elem))
2243                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2244               dwarf2out_frame_debug_expr (elem, label);
2245           }
2246
2247       for (par_index = 0; par_index < limit; par_index++)
2248         {
2249           elem = XVECEXP (expr, 0, par_index);
2250           if (GET_CODE (elem) == SET
2251               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2252               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2253             dwarf2out_frame_debug_expr (elem, label);
2254           else if (GET_CODE (elem) == SET
2255                    && par_index != 0
2256                    && !RTX_FRAME_RELATED_P (elem))
2257             {
2258               /* Stack adjustment combining might combine some post-prologue
2259                  stack adjustment into a prologue stack adjustment.  */
2260               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2261
2262               if (offset != 0)
2263                 dwarf2out_stack_adjust (offset, label);
2264             }
2265         }
2266       return;
2267     }
2268
2269   gcc_assert (GET_CODE (expr) == SET);
2270
2271   src = SET_SRC (expr);
2272   dest = SET_DEST (expr);
2273
2274   if (REG_P (src))
2275     {
2276       rtx rsi = reg_saved_in (src);
2277       if (rsi)
2278         src = rsi;
2279     }
2280
2281   fde = current_fde ();
2282
2283   switch (GET_CODE (dest))
2284     {
2285     case REG:
2286       switch (GET_CODE (src))
2287         {
2288           /* Setting FP from SP.  */
2289         case REG:
2290           if (cfa.reg == (unsigned) REGNO (src))
2291             {
2292               /* Rule 1 */
2293               /* Update the CFA rule wrt SP or FP.  Make sure src is
2294                  relative to the current CFA register.
2295
2296                  We used to require that dest be either SP or FP, but the
2297                  ARM copies SP to a temporary register, and from there to
2298                  FP.  So we just rely on the backends to only set
2299                  RTX_FRAME_RELATED_P on appropriate insns.  */
2300               cfa.reg = REGNO (dest);
2301               cfa_temp.reg = cfa.reg;
2302               cfa_temp.offset = cfa.offset;
2303             }
2304           else
2305             {
2306               /* Saving a register in a register.  */
2307               gcc_assert (!fixed_regs [REGNO (dest)]
2308                           /* For the SPARC and its register window.  */
2309                           || (DWARF_FRAME_REGNUM (REGNO (src))
2310                               == DWARF_FRAME_RETURN_COLUMN));
2311
2312               /* After stack is aligned, we can only save SP in FP
2313                  if drap register is used.  In this case, we have
2314                  to restore stack pointer with the CFA value and we
2315                  don't generate this DWARF information.  */
2316               if (fde
2317                   && fde->stack_realign
2318                   && REGNO (src) == STACK_POINTER_REGNUM)
2319                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2320                             && fde->drap_reg != INVALID_REGNUM
2321                             && cfa.reg != REGNO (src));
2322               else
2323                 queue_reg_save (label, src, dest, 0);
2324             }
2325           break;
2326
2327         case PLUS:
2328         case MINUS:
2329         case LO_SUM:
2330           if (dest == stack_pointer_rtx)
2331             {
2332               /* Rule 2 */
2333               /* Adjusting SP.  */
2334               switch (GET_CODE (XEXP (src, 1)))
2335                 {
2336                 case CONST_INT:
2337                   offset = INTVAL (XEXP (src, 1));
2338                   break;
2339                 case REG:
2340                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2341                               == cfa_temp.reg);
2342                   offset = cfa_temp.offset;
2343                   break;
2344                 default:
2345                   gcc_unreachable ();
2346                 }
2347
2348               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2349                 {
2350                   /* Restoring SP from FP in the epilogue.  */
2351                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2352                   cfa.reg = STACK_POINTER_REGNUM;
2353                 }
2354               else if (GET_CODE (src) == LO_SUM)
2355                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2356                 ;
2357               else
2358                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2359
2360               if (GET_CODE (src) != MINUS)
2361                 offset = -offset;
2362               if (cfa.reg == STACK_POINTER_REGNUM)
2363                 cfa.offset += offset;
2364               if (cfa_store.reg == STACK_POINTER_REGNUM)
2365                 cfa_store.offset += offset;
2366             }
2367           else if (dest == hard_frame_pointer_rtx)
2368             {
2369               /* Rule 3 */
2370               /* Either setting the FP from an offset of the SP,
2371                  or adjusting the FP */
2372               gcc_assert (frame_pointer_needed);
2373
2374               gcc_assert (REG_P (XEXP (src, 0))
2375                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2376                           && CONST_INT_P (XEXP (src, 1)));
2377               offset = INTVAL (XEXP (src, 1));
2378               if (GET_CODE (src) != MINUS)
2379                 offset = -offset;
2380               cfa.offset += offset;
2381               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2382             }
2383           else
2384             {
2385               gcc_assert (GET_CODE (src) != MINUS);
2386
2387               /* Rule 4 */
2388               if (REG_P (XEXP (src, 0))
2389                   && REGNO (XEXP (src, 0)) == cfa.reg
2390                   && CONST_INT_P (XEXP (src, 1)))
2391                 {
2392                   /* Setting a temporary CFA register that will be copied
2393                      into the FP later on.  */
2394                   offset = - INTVAL (XEXP (src, 1));
2395                   cfa.offset += offset;
2396                   cfa.reg = REGNO (dest);
2397                   /* Or used to save regs to the stack.  */
2398                   cfa_temp.reg = cfa.reg;
2399                   cfa_temp.offset = cfa.offset;
2400                 }
2401
2402               /* Rule 5 */
2403               else if (REG_P (XEXP (src, 0))
2404                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2405                        && XEXP (src, 1) == stack_pointer_rtx)
2406                 {
2407                   /* Setting a scratch register that we will use instead
2408                      of SP for saving registers to the stack.  */
2409                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2410                   cfa_store.reg = REGNO (dest);
2411                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2412                 }
2413
2414               /* Rule 9 */
2415               else if (GET_CODE (src) == LO_SUM
2416                        && CONST_INT_P (XEXP (src, 1)))
2417                 {
2418                   cfa_temp.reg = REGNO (dest);
2419                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2420                 }
2421               else
2422                 gcc_unreachable ();
2423             }
2424           break;
2425
2426           /* Rule 6 */
2427         case CONST_INT:
2428           cfa_temp.reg = REGNO (dest);
2429           cfa_temp.offset = INTVAL (src);
2430           break;
2431
2432           /* Rule 7 */
2433         case IOR:
2434           gcc_assert (REG_P (XEXP (src, 0))
2435                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2436                       && CONST_INT_P (XEXP (src, 1)));
2437
2438           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2439             cfa_temp.reg = REGNO (dest);
2440           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2441           break;
2442
2443           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2444              which will fill in all of the bits.  */
2445           /* Rule 8 */
2446         case HIGH:
2447           break;
2448
2449           /* Rule 15 */
2450         case UNSPEC:
2451         case UNSPEC_VOLATILE:
2452           gcc_assert (targetm.dwarf_handle_frame_unspec);
2453           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2454           return;
2455
2456           /* Rule 16 */
2457         case AND:
2458           /* If this AND operation happens on stack pointer in prologue,
2459              we assume the stack is realigned and we extract the
2460              alignment.  */
2461           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2462             {
2463               /* We interpret reg_save differently with stack_realign set.
2464                  Thus we must flush whatever we have queued first.  */
2465               dwarf2out_flush_queued_reg_saves ();
2466
2467               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2468               fde->stack_realign = 1;
2469               fde->stack_realignment = INTVAL (XEXP (src, 1));
2470               cfa_store.offset = 0;
2471
2472               if (cfa.reg != STACK_POINTER_REGNUM
2473                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2474                 fde->drap_reg = cfa.reg;
2475             }
2476           return;
2477
2478         default:
2479           gcc_unreachable ();
2480         }
2481
2482       def_cfa_1 (label, &cfa);
2483       break;
2484
2485     case MEM:
2486
2487       /* Saving a register to the stack.  Make sure dest is relative to the
2488          CFA register.  */
2489       switch (GET_CODE (XEXP (dest, 0)))
2490         {
2491           /* Rule 10 */
2492           /* With a push.  */
2493         case PRE_MODIFY:
2494           /* We can't handle variable size modifications.  */
2495           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2496                       == CONST_INT);
2497           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2498
2499           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2500                       && cfa_store.reg == STACK_POINTER_REGNUM);
2501
2502           cfa_store.offset += offset;
2503           if (cfa.reg == STACK_POINTER_REGNUM)
2504             cfa.offset = cfa_store.offset;
2505
2506           offset = -cfa_store.offset;
2507           break;
2508
2509           /* Rule 11 */
2510         case PRE_INC:
2511         case PRE_DEC:
2512           offset = GET_MODE_SIZE (GET_MODE (dest));
2513           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2514             offset = -offset;
2515
2516           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2517                        == STACK_POINTER_REGNUM)
2518                       && cfa_store.reg == STACK_POINTER_REGNUM);
2519
2520           cfa_store.offset += offset;
2521
2522           /* Rule 18: If stack is aligned, we will use FP as a
2523              reference to represent the address of the stored
2524              regiser.  */
2525           if (fde
2526               && fde->stack_realign
2527               && src == hard_frame_pointer_rtx)
2528             {
2529               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2530               cfa_store.offset = 0;
2531             }
2532
2533           if (cfa.reg == STACK_POINTER_REGNUM)
2534             cfa.offset = cfa_store.offset;
2535
2536           offset = -cfa_store.offset;
2537           break;
2538
2539           /* Rule 12 */
2540           /* With an offset.  */
2541         case PLUS:
2542         case MINUS:
2543         case LO_SUM:
2544           {
2545             int regno;
2546
2547             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2548                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2549             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2550             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2551               offset = -offset;
2552
2553             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2554
2555             if (cfa.reg == (unsigned) regno)
2556               offset -= cfa.offset;
2557             else if (cfa_store.reg == (unsigned) regno)
2558               offset -= cfa_store.offset;
2559             else
2560               {
2561                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2562                 offset -= cfa_temp.offset;
2563               }
2564           }
2565           break;
2566
2567           /* Rule 13 */
2568           /* Without an offset.  */
2569         case REG:
2570           {
2571             int regno = REGNO (XEXP (dest, 0));
2572
2573             if (cfa.reg == (unsigned) regno)
2574               offset = -cfa.offset;
2575             else if (cfa_store.reg == (unsigned) regno)
2576               offset = -cfa_store.offset;
2577             else
2578               {
2579                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2580                 offset = -cfa_temp.offset;
2581               }
2582           }
2583           break;
2584
2585           /* Rule 14 */
2586         case POST_INC:
2587           gcc_assert (cfa_temp.reg
2588                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2589           offset = -cfa_temp.offset;
2590           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2591           break;
2592
2593         default:
2594           gcc_unreachable ();
2595         }
2596
2597         /* Rule 17 */
2598         /* If the source operand of this MEM operation is not a
2599            register, basically the source is return address.  Here
2600            we only care how much stack grew and we don't save it.  */
2601       if (!REG_P (src))
2602         break;
2603
2604       if (REGNO (src) != STACK_POINTER_REGNUM
2605           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2606           && (unsigned) REGNO (src) == cfa.reg)
2607         {
2608           /* We're storing the current CFA reg into the stack.  */
2609
2610           if (cfa.offset == 0)
2611             {
2612               /* Rule 19 */
2613               /* If stack is aligned, putting CFA reg into stack means
2614                  we can no longer use reg + offset to represent CFA.
2615                  Here we use DW_CFA_def_cfa_expression instead.  The
2616                  result of this expression equals to the original CFA
2617                  value.  */
2618               if (fde
2619                   && fde->stack_realign
2620                   && cfa.indirect == 0
2621                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2622                 {
2623                   dw_cfa_location cfa_exp;
2624
2625                   gcc_assert (fde->drap_reg == cfa.reg);
2626
2627                   cfa_exp.indirect = 1;
2628                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2629                   cfa_exp.base_offset = offset;
2630                   cfa_exp.offset = 0;
2631
2632                   fde->drap_reg_saved = 1;
2633
2634                   def_cfa_1 (label, &cfa_exp);
2635                   break;
2636                 }
2637
2638               /* If the source register is exactly the CFA, assume
2639                  we're saving SP like any other register; this happens
2640                  on the ARM.  */
2641               def_cfa_1 (label, &cfa);
2642               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2643               break;
2644             }
2645           else
2646             {
2647               /* Otherwise, we'll need to look in the stack to
2648                  calculate the CFA.  */
2649               rtx x = XEXP (dest, 0);
2650
2651               if (!REG_P (x))
2652                 x = XEXP (x, 0);
2653               gcc_assert (REG_P (x));
2654
2655               cfa.reg = REGNO (x);
2656               cfa.base_offset = offset;
2657               cfa.indirect = 1;
2658               def_cfa_1 (label, &cfa);
2659               break;
2660             }
2661         }
2662
2663       def_cfa_1 (label, &cfa);
2664       {
2665         span = targetm.dwarf_register_span (src);
2666
2667         if (!span)
2668           queue_reg_save (label, src, NULL_RTX, offset);
2669         else
2670           {
2671             /* We have a PARALLEL describing where the contents of SRC
2672                live.  Queue register saves for each piece of the
2673                PARALLEL.  */
2674             int par_index;
2675             int limit;
2676             HOST_WIDE_INT span_offset = offset;
2677
2678             gcc_assert (GET_CODE (span) == PARALLEL);
2679
2680             limit = XVECLEN (span, 0);
2681             for (par_index = 0; par_index < limit; par_index++)
2682               {
2683                 rtx elem = XVECEXP (span, 0, par_index);
2684
2685                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2686                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2687               }
2688           }
2689       }
2690       break;
2691
2692     default:
2693       gcc_unreachable ();
2694     }
2695 }
2696
2697 /* Record call frame debugging information for INSN, which either
2698    sets SP or FP (adjusting how we calculate the frame address) or saves a
2699    register to the stack.  If INSN is NULL_RTX, initialize our state.
2700
2701    If AFTER_P is false, we're being called before the insn is emitted,
2702    otherwise after.  Call instructions get invoked twice.  */
2703
2704 void
2705 dwarf2out_frame_debug (rtx insn, bool after_p)
2706 {
2707   const char *label;
2708   rtx note, n;
2709   bool handled_one = false;
2710
2711   if (insn == NULL_RTX)
2712     {
2713       size_t i;
2714
2715       /* Flush any queued register saves.  */
2716       dwarf2out_flush_queued_reg_saves ();
2717
2718       /* Set up state for generating call frame debug info.  */
2719       lookup_cfa (&cfa);
2720       gcc_assert (cfa.reg
2721                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2722
2723       cfa.reg = STACK_POINTER_REGNUM;
2724       cfa_store = cfa;
2725       cfa_temp.reg = -1;
2726       cfa_temp.offset = 0;
2727
2728       for (i = 0; i < num_regs_saved_in_regs; i++)
2729         {
2730           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2731           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2732         }
2733       num_regs_saved_in_regs = 0;
2734
2735       if (barrier_args_size)
2736         {
2737           XDELETEVEC (barrier_args_size);
2738           barrier_args_size = NULL;
2739         }
2740       return;
2741     }
2742
2743   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2744     dwarf2out_flush_queued_reg_saves ();
2745
2746   if (!RTX_FRAME_RELATED_P (insn))
2747     {
2748       /* ??? This should be done unconditionally since stack adjustments
2749          matter if the stack pointer is not the CFA register anymore but
2750          is still used to save registers.  */
2751       if (!ACCUMULATE_OUTGOING_ARGS)
2752         dwarf2out_notice_stack_adjust (insn, after_p);
2753       return;
2754     }
2755
2756   label = dwarf2out_cfi_label (false);
2757   any_cfis_emitted = false;
2758
2759   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2760     switch (REG_NOTE_KIND (note))
2761       {
2762       case REG_FRAME_RELATED_EXPR:
2763         insn = XEXP (note, 0);
2764         goto found;
2765
2766       case REG_CFA_DEF_CFA:
2767         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2768         handled_one = true;
2769         break;
2770
2771       case REG_CFA_ADJUST_CFA:
2772         n = XEXP (note, 0);
2773         if (n == NULL)
2774           {
2775             n = PATTERN (insn);
2776             if (GET_CODE (n) == PARALLEL)
2777               n = XVECEXP (n, 0, 0);
2778           }
2779         dwarf2out_frame_debug_adjust_cfa (n, label);
2780         handled_one = true;
2781         break;
2782
2783       case REG_CFA_OFFSET:
2784         n = XEXP (note, 0);
2785         if (n == NULL)
2786           n = single_set (insn);
2787         dwarf2out_frame_debug_cfa_offset (n, label);
2788         handled_one = true;
2789         break;
2790
2791       case REG_CFA_REGISTER:
2792         n = XEXP (note, 0);
2793         if (n == NULL)
2794           {
2795             n = PATTERN (insn);
2796             if (GET_CODE (n) == PARALLEL)
2797               n = XVECEXP (n, 0, 0);
2798           }
2799         dwarf2out_frame_debug_cfa_register (n, label);
2800         handled_one = true;
2801         break;
2802
2803       case REG_CFA_EXPRESSION:
2804         n = XEXP (note, 0);
2805         if (n == NULL)
2806           n = single_set (insn);
2807         dwarf2out_frame_debug_cfa_expression (n, label);
2808         handled_one = true;
2809         break;
2810
2811       case REG_CFA_RESTORE:
2812         n = XEXP (note, 0);
2813         if (n == NULL)
2814           {
2815             n = PATTERN (insn);
2816             if (GET_CODE (n) == PARALLEL)
2817               n = XVECEXP (n, 0, 0);
2818             n = XEXP (n, 0);
2819           }
2820         dwarf2out_frame_debug_cfa_restore (n, label);
2821         handled_one = true;
2822         break;
2823
2824       case REG_CFA_SET_VDRAP:
2825         n = XEXP (note, 0);
2826         if (REG_P (n))
2827           {
2828             dw_fde_ref fde = current_fde ();
2829             if (fde)
2830               {
2831                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2832                 if (REG_P (n))
2833                   fde->vdrap_reg = REGNO (n);
2834               }
2835           }
2836         handled_one = true;
2837         break;
2838
2839       default:
2840         break;
2841       }
2842   if (handled_one)
2843     {
2844       if (any_cfis_emitted)
2845         dwarf2out_flush_queued_reg_saves ();
2846       return;
2847     }
2848
2849   insn = PATTERN (insn);
2850  found:
2851   dwarf2out_frame_debug_expr (insn, label);
2852
2853   /* Check again.  A parallel can save and update the same register.
2854      We could probably check just once, here, but this is safer than
2855      removing the check above.  */
2856   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2857     dwarf2out_flush_queued_reg_saves ();
2858 }
2859
2860 /* Determine if we need to save and restore CFI information around this
2861    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2862    we do need to save/restore, then emit the save now, and insert a
2863    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2864
2865 void
2866 dwarf2out_cfi_begin_epilogue (rtx insn)
2867 {
2868   bool saw_frp = false;
2869   rtx i;
2870
2871   /* Scan forward to the return insn, noticing if there are possible
2872      frame related insns.  */
2873   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2874     {
2875       if (!INSN_P (i))
2876         continue;
2877
2878       /* Look for both regular and sibcalls to end the block.  */
2879       if (returnjump_p (i))
2880         break;
2881       if (CALL_P (i) && SIBLING_CALL_P (i))
2882         break;
2883
2884       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2885         {
2886           int idx;
2887           rtx seq = PATTERN (i);
2888
2889           if (returnjump_p (XVECEXP (seq, 0, 0)))
2890             break;
2891           if (CALL_P (XVECEXP (seq, 0, 0))
2892               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2893             break;
2894
2895           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2896             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2897               saw_frp = true;
2898         }
2899
2900       if (RTX_FRAME_RELATED_P (i))
2901         saw_frp = true;
2902     }
2903
2904   /* If the port doesn't emit epilogue unwind info, we don't need a
2905      save/restore pair.  */
2906   if (!saw_frp)
2907     return;
2908
2909   /* Otherwise, search forward to see if the return insn was the last
2910      basic block of the function.  If so, we don't need save/restore.  */
2911   gcc_assert (i != NULL);
2912   i = next_real_insn (i);
2913   if (i == NULL)
2914     return;
2915
2916   /* Insert the restore before that next real insn in the stream, and before
2917      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2918      properly nested.  This should be after any label or alignment.  This
2919      will be pushed into the CFI stream by the function below.  */
2920   while (1)
2921     {
2922       rtx p = PREV_INSN (i);
2923       if (!NOTE_P (p))
2924         break;
2925       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2926         break;
2927       i = p;
2928     }
2929   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2930
2931   emit_cfa_remember = true;
2932
2933   /* And emulate the state save.  */
2934   gcc_assert (!cfa_remember.in_use);
2935   cfa_remember = cfa;
2936   cfa_remember.in_use = 1;
2937 }
2938
2939 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2940    required.  */
2941
2942 void
2943 dwarf2out_frame_debug_restore_state (void)
2944 {
2945   dw_cfi_ref cfi = new_cfi ();
2946   const char *label = dwarf2out_cfi_label (false);
2947
2948   cfi->dw_cfi_opc = DW_CFA_restore_state;
2949   add_fde_cfi (label, cfi);
2950
2951   gcc_assert (cfa_remember.in_use);
2952   cfa = cfa_remember;
2953   cfa_remember.in_use = 0;
2954 }
2955
2956 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2957 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2958  (enum dwarf_call_frame_info cfi);
2959
2960 static enum dw_cfi_oprnd_type
2961 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2962 {
2963   switch (cfi)
2964     {
2965     case DW_CFA_nop:
2966     case DW_CFA_GNU_window_save:
2967     case DW_CFA_remember_state:
2968     case DW_CFA_restore_state:
2969       return dw_cfi_oprnd_unused;
2970
2971     case DW_CFA_set_loc:
2972     case DW_CFA_advance_loc1:
2973     case DW_CFA_advance_loc2:
2974     case DW_CFA_advance_loc4:
2975     case DW_CFA_MIPS_advance_loc8:
2976       return dw_cfi_oprnd_addr;
2977
2978     case DW_CFA_offset:
2979     case DW_CFA_offset_extended:
2980     case DW_CFA_def_cfa:
2981     case DW_CFA_offset_extended_sf:
2982     case DW_CFA_def_cfa_sf:
2983     case DW_CFA_restore:
2984     case DW_CFA_restore_extended:
2985     case DW_CFA_undefined:
2986     case DW_CFA_same_value:
2987     case DW_CFA_def_cfa_register:
2988     case DW_CFA_register:
2989     case DW_CFA_expression:
2990       return dw_cfi_oprnd_reg_num;
2991
2992     case DW_CFA_def_cfa_offset:
2993     case DW_CFA_GNU_args_size:
2994     case DW_CFA_def_cfa_offset_sf:
2995       return dw_cfi_oprnd_offset;
2996
2997     case DW_CFA_def_cfa_expression:
2998       return dw_cfi_oprnd_loc;
2999
3000     default:
3001       gcc_unreachable ();
3002     }
3003 }
3004
3005 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3006 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3007  (enum dwarf_call_frame_info cfi);
3008
3009 static enum dw_cfi_oprnd_type
3010 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3011 {
3012   switch (cfi)
3013     {
3014     case DW_CFA_def_cfa:
3015     case DW_CFA_def_cfa_sf:
3016     case DW_CFA_offset:
3017     case DW_CFA_offset_extended_sf:
3018     case DW_CFA_offset_extended:
3019       return dw_cfi_oprnd_offset;
3020
3021     case DW_CFA_register:
3022       return dw_cfi_oprnd_reg_num;
3023
3024     case DW_CFA_expression:
3025       return dw_cfi_oprnd_loc;
3026
3027     default:
3028       return dw_cfi_oprnd_unused;
3029     }
3030 }
3031
3032 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3033    switch to the data section instead, and write out a synthetic start label
3034    for collect2 the first time around.  */
3035
3036 static void
3037 switch_to_eh_frame_section (bool back)
3038 {
3039   tree label;
3040
3041 #ifdef EH_FRAME_SECTION_NAME
3042   if (eh_frame_section == 0)
3043     {
3044       int flags;
3045
3046       if (EH_TABLES_CAN_BE_READ_ONLY)
3047         {
3048           int fde_encoding;
3049           int per_encoding;
3050           int lsda_encoding;
3051
3052           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3053                                                        /*global=*/0);
3054           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3055                                                        /*global=*/1);
3056           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3057                                                         /*global=*/0);
3058           flags = ((! flag_pic
3059                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3060                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3061                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3062                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3063                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3064                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3065                    ? 0 : SECTION_WRITE);
3066         }
3067       else
3068         flags = SECTION_WRITE;
3069       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3070     }
3071 #endif /* EH_FRAME_SECTION_NAME */
3072
3073   if (eh_frame_section)
3074     switch_to_section (eh_frame_section);
3075   else
3076     {
3077       /* We have no special eh_frame section.  Put the information in
3078          the data section and emit special labels to guide collect2.  */
3079       switch_to_section (data_section);
3080
3081       if (!back)
3082         {
3083           label = get_file_function_name ("F");
3084           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3085           targetm.asm_out.globalize_label (asm_out_file,
3086                                            IDENTIFIER_POINTER (label));
3087           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3088         }
3089     }
3090 }
3091
3092 /* Switch [BACK] to the eh or debug frame table section, depending on
3093    FOR_EH.  */
3094
3095 static void
3096 switch_to_frame_table_section (int for_eh, bool back)
3097 {
3098   if (for_eh)
3099     switch_to_eh_frame_section (back);
3100   else
3101     {
3102       if (!debug_frame_section)
3103         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3104                                            SECTION_DEBUG, NULL);
3105       switch_to_section (debug_frame_section);
3106     }
3107 }
3108
3109 /* Output a Call Frame Information opcode and its operand(s).  */
3110
3111 static void
3112 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3113 {
3114   unsigned long r;
3115   HOST_WIDE_INT off;
3116
3117   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3118     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3119                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3120                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3121                          ((unsigned HOST_WIDE_INT)
3122                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3123   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3124     {
3125       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3126       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3127                            "DW_CFA_offset, column %#lx", r);
3128       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3129       dw2_asm_output_data_uleb128 (off, NULL);
3130     }
3131   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3132     {
3133       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3134       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3135                            "DW_CFA_restore, column %#lx", r);
3136     }
3137   else
3138     {
3139       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3140                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3141
3142       switch (cfi->dw_cfi_opc)
3143         {
3144         case DW_CFA_set_loc:
3145           if (for_eh)
3146             dw2_asm_output_encoded_addr_rtx (
3147                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3148                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3149                 false, NULL);
3150           else
3151             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3152                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3153           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3154           break;
3155
3156         case DW_CFA_advance_loc1:
3157           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3158                                 fde->dw_fde_current_label, NULL);
3159           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3160           break;
3161
3162         case DW_CFA_advance_loc2:
3163           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3164                                 fde->dw_fde_current_label, NULL);
3165           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3166           break;
3167
3168         case DW_CFA_advance_loc4:
3169           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3170                                 fde->dw_fde_current_label, NULL);
3171           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3172           break;
3173
3174         case DW_CFA_MIPS_advance_loc8:
3175           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3176                                 fde->dw_fde_current_label, NULL);
3177           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3178           break;
3179
3180         case DW_CFA_offset_extended:
3181           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3182           dw2_asm_output_data_uleb128 (r, NULL);
3183           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3184           dw2_asm_output_data_uleb128 (off, NULL);
3185           break;
3186
3187         case DW_CFA_def_cfa:
3188           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3189           dw2_asm_output_data_uleb128 (r, NULL);
3190           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3191           break;
3192
3193         case DW_CFA_offset_extended_sf:
3194           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3195           dw2_asm_output_data_uleb128 (r, NULL);
3196           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3197           dw2_asm_output_data_sleb128 (off, NULL);
3198           break;
3199
3200         case DW_CFA_def_cfa_sf:
3201           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3202           dw2_asm_output_data_uleb128 (r, NULL);
3203           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3204           dw2_asm_output_data_sleb128 (off, NULL);
3205           break;
3206
3207         case DW_CFA_restore_extended:
3208         case DW_CFA_undefined:
3209         case DW_CFA_same_value:
3210         case DW_CFA_def_cfa_register:
3211           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3212           dw2_asm_output_data_uleb128 (r, NULL);
3213           break;
3214
3215         case DW_CFA_register:
3216           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3217           dw2_asm_output_data_uleb128 (r, NULL);
3218           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3219           dw2_asm_output_data_uleb128 (r, NULL);
3220           break;
3221
3222         case DW_CFA_def_cfa_offset:
3223         case DW_CFA_GNU_args_size:
3224           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3225           break;
3226
3227         case DW_CFA_def_cfa_offset_sf:
3228           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3229           dw2_asm_output_data_sleb128 (off, NULL);
3230           break;
3231
3232         case DW_CFA_GNU_window_save:
3233           break;
3234
3235         case DW_CFA_def_cfa_expression:
3236         case DW_CFA_expression:
3237           output_cfa_loc (cfi);
3238           break;
3239
3240         case DW_CFA_GNU_negative_offset_extended:
3241           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3242           gcc_unreachable ();
3243
3244         default:
3245           break;
3246         }
3247     }
3248 }
3249
3250 /* Similar, but do it via assembler directives instead.  */
3251
3252 static void
3253 output_cfi_directive (dw_cfi_ref cfi)
3254 {
3255   unsigned long r, r2;
3256
3257   switch (cfi->dw_cfi_opc)
3258     {
3259     case DW_CFA_advance_loc:
3260     case DW_CFA_advance_loc1:
3261     case DW_CFA_advance_loc2:
3262     case DW_CFA_advance_loc4:
3263     case DW_CFA_MIPS_advance_loc8:
3264     case DW_CFA_set_loc:
3265       /* Should only be created by add_fde_cfi in a code path not
3266          followed when emitting via directives.  The assembler is
3267          going to take care of this for us.  */
3268       gcc_unreachable ();
3269
3270     case DW_CFA_offset:
3271     case DW_CFA_offset_extended:
3272     case DW_CFA_offset_extended_sf:
3273       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3274       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3275                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3276       break;
3277
3278     case DW_CFA_restore:
3279     case DW_CFA_restore_extended:
3280       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3281       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3282       break;
3283
3284     case DW_CFA_undefined:
3285       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3286       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3287       break;
3288
3289     case DW_CFA_same_value:
3290       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3291       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3292       break;
3293
3294     case DW_CFA_def_cfa:
3295     case DW_CFA_def_cfa_sf:
3296       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3297       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3298                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3299       break;
3300
3301     case DW_CFA_def_cfa_register:
3302       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3303       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3304       break;
3305
3306     case DW_CFA_register:
3307       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3308       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3309       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3310       break;
3311
3312     case DW_CFA_def_cfa_offset:
3313     case DW_CFA_def_cfa_offset_sf:
3314       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3315                HOST_WIDE_INT_PRINT_DEC"\n",
3316                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3317       break;
3318
3319     case DW_CFA_remember_state:
3320       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3321       break;
3322     case DW_CFA_restore_state:
3323       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3324       break;
3325
3326     case DW_CFA_GNU_args_size:
3327       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3328       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3329       if (flag_debug_asm)
3330         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3331                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3332       fputc ('\n', asm_out_file);
3333       break;
3334
3335     case DW_CFA_GNU_window_save:
3336       fprintf (asm_out_file, "\t.cfi_window_save\n");
3337       break;
3338
3339     case DW_CFA_def_cfa_expression:
3340     case DW_CFA_expression:
3341       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3342       output_cfa_loc_raw (cfi);
3343       fputc ('\n', asm_out_file);
3344       break;
3345
3346     default:
3347       gcc_unreachable ();
3348     }
3349 }
3350
3351 DEF_VEC_P (dw_cfi_ref);
3352 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3353
3354 /* Output CFIs to bring current FDE to the same state as after executing
3355    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3356    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3357    other arguments to pass to output_cfi.  */
3358
3359 static void
3360 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3361 {
3362   struct dw_cfi_struct cfi_buf;
3363   dw_cfi_ref cfi2;
3364   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3365   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3366   unsigned int len, idx;
3367
3368   for (;; cfi = cfi->dw_cfi_next)
3369     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3370       {
3371       case DW_CFA_advance_loc:
3372       case DW_CFA_advance_loc1:
3373       case DW_CFA_advance_loc2:
3374       case DW_CFA_advance_loc4:
3375       case DW_CFA_MIPS_advance_loc8:
3376       case DW_CFA_set_loc:
3377         /* All advances should be ignored.  */
3378         break;
3379       case DW_CFA_remember_state:
3380         {
3381           dw_cfi_ref args_size = cfi_args_size;
3382
3383           /* Skip everything between .cfi_remember_state and
3384              .cfi_restore_state.  */
3385           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3386             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3387               break;
3388             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3389               args_size = cfi2;
3390             else
3391               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3392
3393           if (cfi2 == NULL)
3394             goto flush_all;
3395           else
3396             {
3397               cfi = cfi2;
3398               cfi_args_size = args_size;
3399             }
3400           break;
3401         }
3402       case DW_CFA_GNU_args_size:
3403         cfi_args_size = cfi;
3404         break;
3405       case DW_CFA_GNU_window_save:
3406         goto flush_all;
3407       case DW_CFA_offset:
3408       case DW_CFA_offset_extended:
3409       case DW_CFA_offset_extended_sf:
3410       case DW_CFA_restore:
3411       case DW_CFA_restore_extended:
3412       case DW_CFA_undefined:
3413       case DW_CFA_same_value:
3414       case DW_CFA_register:
3415       case DW_CFA_val_offset:
3416       case DW_CFA_val_offset_sf:
3417       case DW_CFA_expression:
3418       case DW_CFA_val_expression:
3419       case DW_CFA_GNU_negative_offset_extended:
3420         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3421           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3422                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3423         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3424         break;
3425       case DW_CFA_def_cfa:
3426       case DW_CFA_def_cfa_sf:
3427       case DW_CFA_def_cfa_expression:
3428         cfi_cfa = cfi;
3429         cfi_cfa_offset = cfi;
3430         break;
3431       case DW_CFA_def_cfa_register:
3432         cfi_cfa = cfi;
3433         break;
3434       case DW_CFA_def_cfa_offset:
3435       case DW_CFA_def_cfa_offset_sf:
3436         cfi_cfa_offset = cfi;
3437         break;
3438       case DW_CFA_nop:
3439         gcc_assert (cfi == NULL);
3440       flush_all:
3441         len = VEC_length (dw_cfi_ref, regs);
3442         for (idx = 0; idx < len; idx++)
3443           {
3444             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3445             if (cfi2 != NULL
3446                 && cfi2->dw_cfi_opc != DW_CFA_restore
3447                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3448               {
3449                 if (do_cfi_asm)
3450                   output_cfi_directive (cfi2);
3451                 else
3452                   output_cfi (cfi2, fde, for_eh);
3453               }
3454           }
3455         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3456           {
3457             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3458             cfi_buf = *cfi_cfa;
3459             switch (cfi_cfa_offset->dw_cfi_opc)
3460               {
3461               case DW_CFA_def_cfa_offset:
3462                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3463                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3464                 break;
3465               case DW_CFA_def_cfa_offset_sf:
3466                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3467                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3468                 break;
3469               case DW_CFA_def_cfa:
3470               case DW_CFA_def_cfa_sf:
3471                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3472                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3473                 break;
3474               default:
3475                 gcc_unreachable ();
3476               }
3477             cfi_cfa = &cfi_buf;
3478           }
3479         else if (cfi_cfa_offset)
3480           cfi_cfa = cfi_cfa_offset;
3481         if (cfi_cfa)
3482           {
3483             if (do_cfi_asm)
3484               output_cfi_directive (cfi_cfa);
3485             else
3486               output_cfi (cfi_cfa, fde, for_eh);
3487           }
3488         cfi_cfa = NULL;
3489         cfi_cfa_offset = NULL;
3490         if (cfi_args_size
3491             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3492           {
3493             if (do_cfi_asm)
3494               output_cfi_directive (cfi_args_size);
3495             else
3496               output_cfi (cfi_args_size, fde, for_eh);
3497           }
3498         cfi_args_size = NULL;
3499         if (cfi == NULL)
3500           {
3501             VEC_free (dw_cfi_ref, heap, regs);
3502             return;
3503           }
3504         else if (do_cfi_asm)
3505           output_cfi_directive (cfi);
3506         else
3507           output_cfi (cfi, fde, for_eh);
3508         break;
3509       default:
3510         gcc_unreachable ();
3511     }
3512 }
3513
3514 /* Output one FDE.  */
3515
3516 static void
3517 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3518             char *section_start_label, int fde_encoding, char *augmentation,
3519             bool any_lsda_needed, int lsda_encoding)
3520 {
3521   const char *begin, *end;
3522   static unsigned int j;
3523   char l1[20], l2[20];
3524   dw_cfi_ref cfi;
3525
3526   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3527                                      /* empty */ 0);
3528   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3529                                   for_eh + j);
3530   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3531   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3532   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3533     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3534                          " indicating 64-bit DWARF extension");
3535   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3536                         "FDE Length");
3537   ASM_OUTPUT_LABEL (asm_out_file, l1);
3538
3539   if (for_eh)
3540     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3541   else
3542     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3543                            debug_frame_section, "FDE CIE offset");
3544
3545   if (!fde->dw_fde_switched_sections)
3546     {
3547       begin = fde->dw_fde_begin;
3548       end = fde->dw_fde_end;
3549     }
3550   else
3551     {
3552       /* For the first section, prefer dw_fde_begin over
3553          dw_fde_{hot,cold}_section_label, as the latter
3554          might be separated from the real start of the
3555          function by alignment padding.  */
3556       if (!second)
3557         begin = fde->dw_fde_begin;
3558       else if (fde->dw_fde_switched_cold_to_hot)
3559         begin = fde->dw_fde_hot_section_label;
3560       else
3561         begin = fde->dw_fde_unlikely_section_label;
3562       if (second ^ fde->dw_fde_switched_cold_to_hot)
3563         end = fde->dw_fde_unlikely_section_end_label;
3564       else
3565         end = fde->dw_fde_hot_section_end_label;
3566     }
3567
3568   if (for_eh)
3569     {
3570       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3571       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3572       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3573                                        "FDE initial location");
3574       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3575                             end, begin, "FDE address range");
3576     }
3577   else
3578     {
3579       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3580       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3581     }
3582
3583   if (augmentation[0])
3584     {
3585       if (any_lsda_needed)
3586         {
3587           int size = size_of_encoded_value (lsda_encoding);
3588
3589           if (lsda_encoding == DW_EH_PE_aligned)
3590             {
3591               int offset = (  4         /* Length */
3592                             + 4         /* CIE offset */
3593                             + 2 * size_of_encoded_value (fde_encoding)
3594                             + 1         /* Augmentation size */ );
3595               int pad = -offset & (PTR_SIZE - 1);
3596
3597               size += pad;
3598               gcc_assert (size_of_uleb128 (size) == 1);
3599             }
3600
3601           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3602
3603           if (fde->uses_eh_lsda)
3604             {
3605               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3606                                            fde->funcdef_number);
3607               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3608                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3609                                                false,
3610                                                "Language Specific Data Area");
3611             }
3612           else
3613             {
3614               if (lsda_encoding == DW_EH_PE_aligned)
3615                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3616               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3617                                    "Language Specific Data Area (none)");
3618             }
3619         }
3620       else
3621         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3622     }
3623
3624   /* Loop through the Call Frame Instructions associated with
3625      this FDE.  */
3626   fde->dw_fde_current_label = begin;
3627   if (!fde->dw_fde_switched_sections)
3628     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3629       output_cfi (cfi, fde, for_eh);
3630   else if (!second)
3631     {
3632       if (fde->dw_fde_switch_cfi)
3633         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3634           {
3635             output_cfi (cfi, fde, for_eh);
3636             if (cfi == fde->dw_fde_switch_cfi)
3637               break;
3638           }
3639     }
3640   else
3641     {
3642       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3643
3644       if (fde->dw_fde_switch_cfi)
3645         {
3646           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3647           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3648           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3649           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3650         }
3651       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3652         output_cfi (cfi, fde, for_eh);
3653     }
3654
3655   /* If we are to emit a ref/link from function bodies to their frame tables,
3656      do it now.  This is typically performed to make sure that tables
3657      associated with functions are dragged with them and not discarded in
3658      garbage collecting links. We need to do this on a per function basis to
3659      cope with -ffunction-sections.  */
3660
3661 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3662   /* Switch to the function section, emit the ref to the tables, and
3663      switch *back* into the table section.  */
3664   switch_to_section (function_section (fde->decl));
3665   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3666   switch_to_frame_table_section (for_eh, true);
3667 #endif
3668
3669   /* Pad the FDE out to an address sized boundary.  */
3670   ASM_OUTPUT_ALIGN (asm_out_file,
3671                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3672   ASM_OUTPUT_LABEL (asm_out_file, l2);
3673
3674   j += 2;
3675 }
3676
3677 /* Return true if frame description entry FDE is needed for EH.  */
3678
3679 static bool
3680 fde_needed_for_eh_p (dw_fde_ref fde)
3681 {
3682   if (flag_asynchronous_unwind_tables)
3683     return true;
3684
3685   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3686     return true;
3687
3688   if (fde->uses_eh_lsda)
3689     return true;
3690
3691   /* If exceptions are enabled, we have collected nothrow info.  */
3692   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3693     return false;
3694
3695   return true;
3696 }
3697
3698 /* Output the call frame information used to record information
3699    that relates to calculating the frame pointer, and records the
3700    location of saved registers.  */
3701
3702 static void
3703 output_call_frame_info (int for_eh)
3704 {
3705   unsigned int i;
3706   dw_fde_ref fde;
3707   dw_cfi_ref cfi;
3708   char l1[20], l2[20], section_start_label[20];
3709   bool any_lsda_needed = false;
3710   char augmentation[6];
3711   int augmentation_size;
3712   int fde_encoding = DW_EH_PE_absptr;
3713   int per_encoding = DW_EH_PE_absptr;
3714   int lsda_encoding = DW_EH_PE_absptr;
3715   int return_reg;
3716   rtx personality = NULL;
3717   int dw_cie_version;
3718
3719   /* Don't emit a CIE if there won't be any FDEs.  */
3720   if (fde_table_in_use == 0)
3721     return;
3722
3723   /* Nothing to do if the assembler's doing it all.  */
3724   if (dwarf2out_do_cfi_asm ())
3725     return;
3726
3727   /* If we don't have any functions we'll want to unwind out of, don't emit
3728      any EH unwind information.  If we make FDEs linkonce, we may have to
3729      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3730      want to avoid having an FDE kept around when the function it refers to
3731      is discarded.  Example where this matters: a primary function template
3732      in C++ requires EH information, an explicit specialization doesn't.  */
3733   if (for_eh)
3734     {
3735       bool any_eh_needed = false;
3736
3737       for (i = 0; i < fde_table_in_use; i++)
3738         if (fde_table[i].uses_eh_lsda)
3739           any_eh_needed = any_lsda_needed = true;
3740         else if (fde_needed_for_eh_p (&fde_table[i]))
3741           any_eh_needed = true;
3742         else if (TARGET_USES_WEAK_UNWIND_INFO)
3743           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3744                                              1, 1);
3745
3746       if (!any_eh_needed)
3747         return;
3748     }
3749
3750   /* We're going to be generating comments, so turn on app.  */
3751   if (flag_debug_asm)
3752     app_enable ();
3753
3754   /* Switch to the proper frame section, first time.  */
3755   switch_to_frame_table_section (for_eh, false);
3756
3757   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3758   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3759
3760   /* Output the CIE.  */
3761   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3762   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3763   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3764     dw2_asm_output_data (4, 0xffffffff,
3765       "Initial length escape value indicating 64-bit DWARF extension");
3766   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3767                         "Length of Common Information Entry");
3768   ASM_OUTPUT_LABEL (asm_out_file, l1);
3769
3770   /* Now that the CIE pointer is PC-relative for EH,
3771      use 0 to identify the CIE.  */
3772   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3773                        (for_eh ? 0 : DWARF_CIE_ID),
3774                        "CIE Identifier Tag");
3775
3776   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3777      use CIE version 1, unless that would produce incorrect results
3778      due to overflowing the return register column.  */
3779   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3780   dw_cie_version = 1;
3781   if (return_reg >= 256 || dwarf_version > 2)
3782     dw_cie_version = 3;
3783   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3784
3785   augmentation[0] = 0;
3786   augmentation_size = 0;
3787
3788   personality = current_unit_personality;
3789   if (for_eh)
3790     {
3791       char *p;
3792
3793       /* Augmentation:
3794          z      Indicates that a uleb128 is present to size the
3795                 augmentation section.
3796          L      Indicates the encoding (and thus presence) of
3797                 an LSDA pointer in the FDE augmentation.
3798          R      Indicates a non-default pointer encoding for
3799                 FDE code pointers.
3800          P      Indicates the presence of an encoding + language
3801                 personality routine in the CIE augmentation.  */
3802
3803       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3804       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3805       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3806
3807       p = augmentation + 1;
3808       if (personality)
3809         {
3810           *p++ = 'P';
3811           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3812           assemble_external_libcall (personality);
3813         }
3814       if (any_lsda_needed)
3815         {
3816           *p++ = 'L';
3817           augmentation_size += 1;
3818         }
3819       if (fde_encoding != DW_EH_PE_absptr)
3820         {
3821           *p++ = 'R';
3822           augmentation_size += 1;
3823         }
3824       if (p > augmentation + 1)
3825         {
3826           augmentation[0] = 'z';
3827           *p = '\0';
3828         }
3829
3830       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3831       if (personality && per_encoding == DW_EH_PE_aligned)
3832         {
3833           int offset = (  4             /* Length */
3834                         + 4             /* CIE Id */
3835                         + 1             /* CIE version */
3836                         + strlen (augmentation) + 1     /* Augmentation */
3837                         + size_of_uleb128 (1)           /* Code alignment */
3838                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3839                         + 1             /* RA column */
3840                         + 1             /* Augmentation size */
3841                         + 1             /* Personality encoding */ );
3842           int pad = -offset & (PTR_SIZE - 1);
3843
3844           augmentation_size += pad;
3845
3846           /* Augmentations should be small, so there's scarce need to
3847              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3848           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3849         }
3850     }
3851
3852   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3853   if (dw_cie_version >= 4)
3854     {
3855       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3856       dw2_asm_output_data (1, 0, "CIE Segment Size");
3857     }
3858   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3859   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3860                                "CIE Data Alignment Factor");
3861
3862   if (dw_cie_version == 1)
3863     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3864   else
3865     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3866
3867   if (augmentation[0])
3868     {
3869       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3870       if (personality)
3871         {
3872           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3873                                eh_data_format_name (per_encoding));
3874           dw2_asm_output_encoded_addr_rtx (per_encoding,
3875                                            personality,
3876                                            true, NULL);
3877         }
3878
3879       if (any_lsda_needed)
3880         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3881                              eh_data_format_name (lsda_encoding));
3882
3883       if (fde_encoding != DW_EH_PE_absptr)
3884         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3885                              eh_data_format_name (fde_encoding));
3886     }
3887
3888   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3889     output_cfi (cfi, NULL, for_eh);
3890
3891   /* Pad the CIE out to an address sized boundary.  */
3892   ASM_OUTPUT_ALIGN (asm_out_file,
3893                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3894   ASM_OUTPUT_LABEL (asm_out_file, l2);
3895
3896   /* Loop through all of the FDE's.  */
3897   for (i = 0; i < fde_table_in_use; i++)
3898     {
3899       unsigned int k;
3900       fde = &fde_table[i];
3901
3902       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3903       if (for_eh && !fde_needed_for_eh_p (fde))
3904         continue;
3905
3906       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3907         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3908                     augmentation, any_lsda_needed, lsda_encoding);
3909     }
3910
3911   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3912     dw2_asm_output_data (4, 0, "End of Table");
3913 #ifdef MIPS_DEBUGGING_INFO
3914   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3915      get a value of 0.  Putting .align 0 after the label fixes it.  */
3916   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3917 #endif
3918
3919   /* Turn off app to make assembly quicker.  */
3920   if (flag_debug_asm)
3921     app_disable ();
3922 }
3923
3924 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3925
3926 static void
3927 dwarf2out_do_cfi_startproc (bool second)
3928 {
3929   int enc;
3930   rtx ref;
3931   rtx personality = get_personality_function (current_function_decl);
3932
3933   fprintf (asm_out_file, "\t.cfi_startproc\n");
3934
3935   if (personality)
3936     {
3937       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3938       ref = personality;
3939
3940       /* ??? The GAS support isn't entirely consistent.  We have to
3941          handle indirect support ourselves, but PC-relative is done
3942          in the assembler.  Further, the assembler can't handle any
3943          of the weirder relocation types.  */
3944       if (enc & DW_EH_PE_indirect)
3945         ref = dw2_force_const_mem (ref, true);
3946
3947       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3948       output_addr_const (asm_out_file, ref);
3949       fputc ('\n', asm_out_file);
3950     }
3951
3952   if (crtl->uses_eh_lsda)
3953     {
3954       char lab[20];
3955
3956       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3957       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3958                                    current_function_funcdef_no);
3959       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3960       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3961
3962       if (enc & DW_EH_PE_indirect)
3963         ref = dw2_force_const_mem (ref, true);
3964
3965       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3966       output_addr_const (asm_out_file, ref);
3967       fputc ('\n', asm_out_file);
3968     }
3969 }
3970
3971 /* Output a marker (i.e. a label) for the beginning of a function, before
3972    the prologue.  */
3973
3974 void
3975 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3976                           const char *file ATTRIBUTE_UNUSED)
3977 {
3978   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3979   char * dup_label;
3980   dw_fde_ref fde;
3981   section *fnsec;
3982   bool do_frame;
3983
3984   current_function_func_begin_label = NULL;
3985
3986   do_frame = dwarf2out_do_frame ();
3987
3988   /* ??? current_function_func_begin_label is also used by except.c for
3989      call-site information.  We must emit this label if it might be used.  */
3990   if (!do_frame
3991       && (!flag_exceptions
3992           || targetm.except_unwind_info () != UI_TARGET))
3993     return;
3994
3995   fnsec = function_section (current_function_decl);
3996   switch_to_section (fnsec);
3997   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3998                                current_function_funcdef_no);
3999   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4000                           current_function_funcdef_no);
4001   dup_label = xstrdup (label);
4002   current_function_func_begin_label = dup_label;
4003
4004   /* We can elide the fde allocation if we're not emitting debug info.  */
4005   if (!do_frame)
4006     return;
4007
4008   /* Expand the fde table if necessary.  */
4009   if (fde_table_in_use == fde_table_allocated)
4010     {
4011       fde_table_allocated += FDE_TABLE_INCREMENT;
4012       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4013       memset (fde_table + fde_table_in_use, 0,
4014               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4015     }
4016
4017   /* Record the FDE associated with this function.  */
4018   current_funcdef_fde = fde_table_in_use;
4019
4020   /* Add the new FDE at the end of the fde_table.  */
4021   fde = &fde_table[fde_table_in_use++];
4022   fde->decl = current_function_decl;
4023   fde->dw_fde_begin = dup_label;
4024   fde->dw_fde_current_label = dup_label;
4025   fde->dw_fde_hot_section_label = NULL;
4026   fde->dw_fde_hot_section_end_label = NULL;
4027   fde->dw_fde_unlikely_section_label = NULL;
4028   fde->dw_fde_unlikely_section_end_label = NULL;
4029   fde->dw_fde_switched_sections = 0;
4030   fde->dw_fde_switched_cold_to_hot = 0;
4031   fde->dw_fde_end = NULL;
4032   fde->dw_fde_vms_end_prologue = NULL;
4033   fde->dw_fde_vms_begin_epilogue = NULL;
4034   fde->dw_fde_cfi = NULL;
4035   fde->dw_fde_switch_cfi = NULL;
4036   fde->funcdef_number = current_function_funcdef_no;
4037   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4038   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4039   fde->nothrow = crtl->nothrow;
4040   fde->drap_reg = INVALID_REGNUM;
4041   fde->vdrap_reg = INVALID_REGNUM;
4042   if (flag_reorder_blocks_and_partition)
4043     {
4044       section *unlikelysec;
4045       if (first_function_block_is_cold)
4046         fde->in_std_section = 1;
4047       else
4048         fde->in_std_section
4049           = (fnsec == text_section
4050              || (cold_text_section && fnsec == cold_text_section));
4051       unlikelysec = unlikely_text_section ();
4052       fde->cold_in_std_section
4053         = (unlikelysec == text_section
4054            || (cold_text_section && unlikelysec == cold_text_section));
4055     }
4056   else
4057     {
4058       fde->in_std_section
4059         = (fnsec == text_section
4060            || (cold_text_section && fnsec == cold_text_section));
4061       fde->cold_in_std_section = 0;
4062     }
4063
4064   args_size = old_args_size = 0;
4065
4066   /* We only want to output line number information for the genuine dwarf2
4067      prologue case, not the eh frame case.  */
4068 #ifdef DWARF2_DEBUGGING_INFO
4069   if (file)
4070     dwarf2out_source_line (line, file, 0, true);
4071 #endif
4072
4073   if (dwarf2out_do_cfi_asm ())
4074     dwarf2out_do_cfi_startproc (false);
4075   else
4076     {
4077       rtx personality = get_personality_function (current_function_decl);
4078       if (!current_unit_personality)
4079         current_unit_personality = personality;
4080
4081       /* We cannot keep a current personality per function as without CFI
4082          asm, at the point where we emit the CFI data, there is no current
4083          function anymore.  */
4084       if (personality && current_unit_personality != personality)
4085         sorry ("multiple EH personalities are supported only with assemblers "
4086                "supporting .cfi_personality directive");
4087     }
4088 }
4089
4090 /* Output a marker (i.e. a label) for the end of the generated code
4091    for a function prologue.  This gets called *after* the prologue code has
4092    been generated.  */
4093
4094 void
4095 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4096                         const char *file ATTRIBUTE_UNUSED)
4097 {
4098   dw_fde_ref fde;
4099   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4100
4101   /* Output a label to mark the endpoint of the code generated for this
4102      function.  */
4103   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4104                                current_function_funcdef_no);
4105   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4106                           current_function_funcdef_no);
4107   fde = &fde_table[fde_table_in_use - 1];
4108   fde->dw_fde_vms_end_prologue = xstrdup (label);
4109 }
4110
4111 /* Output a marker (i.e. a label) for the beginning of the generated code
4112    for a function epilogue.  This gets called *before* the prologue code has
4113    been generated.  */
4114
4115 void
4116 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4117                           const char *file ATTRIBUTE_UNUSED)
4118 {
4119   dw_fde_ref fde;
4120   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4121
4122   fde = &fde_table[fde_table_in_use - 1];
4123   if (fde->dw_fde_vms_begin_epilogue)
4124     return;
4125
4126   /* Output a label to mark the endpoint of the code generated for this
4127      function.  */
4128   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4129                                current_function_funcdef_no);
4130   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4131                           current_function_funcdef_no);
4132   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4133 }
4134
4135 /* Output a marker (i.e. a label) for the absolute end of the generated code
4136    for a function definition.  This gets called *after* the epilogue code has
4137    been generated.  */
4138
4139 void
4140 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4141                         const char *file ATTRIBUTE_UNUSED)
4142 {
4143   dw_fde_ref fde;
4144   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4145
4146   last_var_location_insn = NULL_RTX;
4147
4148   if (dwarf2out_do_cfi_asm ())
4149     fprintf (asm_out_file, "\t.cfi_endproc\n");
4150
4151   /* Output a label to mark the endpoint of the code generated for this
4152      function.  */
4153   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4154                                current_function_funcdef_no);
4155   ASM_OUTPUT_LABEL (asm_out_file, label);
4156   fde = current_fde ();
4157   gcc_assert (fde != NULL);
4158   fde->dw_fde_end = xstrdup (label);
4159 }
4160
4161 void
4162 dwarf2out_frame_init (void)
4163 {
4164   /* Allocate the initial hunk of the fde_table.  */
4165   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4166   fde_table_allocated = FDE_TABLE_INCREMENT;
4167   fde_table_in_use = 0;
4168
4169   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4170      sake of lookup_cfa.  */
4171
4172   /* On entry, the Canonical Frame Address is at SP.  */
4173   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4174
4175   if (targetm.debug_unwind_info () == UI_DWARF2
4176       || targetm.except_unwind_info () == UI_DWARF2)
4177     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4178 }
4179
4180 void
4181 dwarf2out_frame_finish (void)
4182 {
4183   /* Output call frame information.  */
4184   if (targetm.debug_unwind_info () == UI_DWARF2)
4185     output_call_frame_info (0);
4186
4187   /* Output another copy for the unwinder.  */
4188   if ((flag_unwind_tables || flag_exceptions)
4189       && targetm.except_unwind_info () == UI_DWARF2)
4190     output_call_frame_info (1);
4191 }
4192
4193 /* Note that the current function section is being used for code.  */
4194
4195 static void
4196 dwarf2out_note_section_used (void)
4197 {
4198   section *sec = current_function_section ();
4199   if (sec == text_section)
4200     text_section_used = true;
4201   else if (sec == cold_text_section)
4202     cold_text_section_used = true;
4203 }
4204
4205 void
4206 dwarf2out_switch_text_section (void)
4207 {
4208   dw_fde_ref fde = current_fde ();
4209
4210   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4211
4212   fde->dw_fde_switched_sections = 1;
4213   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4214
4215   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4216   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4217   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4218   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4219   have_multiple_function_sections = true;
4220
4221   /* Reset the current label on switching text sections, so that we
4222      don't attempt to advance_loc4 between labels in different sections.  */
4223   fde->dw_fde_current_label = NULL;
4224
4225   /* There is no need to mark used sections when not debugging.  */
4226   if (cold_text_section != NULL)
4227     dwarf2out_note_section_used ();
4228
4229   if (dwarf2out_do_cfi_asm ())
4230     fprintf (asm_out_file, "\t.cfi_endproc\n");
4231
4232   /* Now do the real section switch.  */
4233   switch_to_section (current_function_section ());
4234
4235   if (dwarf2out_do_cfi_asm ())
4236     {
4237       dwarf2out_do_cfi_startproc (true);
4238       /* As this is a different FDE, insert all current CFI instructions
4239          again.  */
4240       output_cfis (fde->dw_fde_cfi, true, fde, true);
4241     }
4242   else
4243     {
4244       dw_cfi_ref cfi = fde->dw_fde_cfi;
4245
4246       cfi = fde->dw_fde_cfi;
4247       if (cfi)
4248         while (cfi->dw_cfi_next != NULL)
4249           cfi = cfi->dw_cfi_next;
4250       fde->dw_fde_switch_cfi = cfi;
4251     }
4252 }
4253 \f
4254 /* And now, the subset of the debugging information support code necessary
4255    for emitting location expressions.  */
4256
4257 /* Data about a single source file.  */
4258 struct GTY(()) dwarf_file_data {
4259   const char * filename;
4260   int emitted_number;
4261 };
4262
4263 typedef struct dw_val_struct *dw_val_ref;
4264 typedef struct die_struct *dw_die_ref;
4265 typedef const struct die_struct *const_dw_die_ref;
4266 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4267 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4268
4269 typedef struct GTY(()) deferred_locations_struct
4270 {
4271   tree variable;
4272   dw_die_ref die;
4273 } deferred_locations;
4274
4275 DEF_VEC_O(deferred_locations);
4276 DEF_VEC_ALLOC_O(deferred_locations,gc);
4277
4278 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4279
4280 DEF_VEC_P(dw_die_ref);
4281 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4282
4283 /* Each DIE may have a series of attribute/value pairs.  Values
4284    can take on several forms.  The forms that are used in this
4285    implementation are listed below.  */
4286
4287 enum dw_val_class
4288 {
4289   dw_val_class_addr,
4290   dw_val_class_offset,
4291   dw_val_class_loc,
4292   dw_val_class_loc_list,
4293   dw_val_class_range_list,
4294   dw_val_class_const,
4295   dw_val_class_unsigned_const,
4296   dw_val_class_const_double,
4297   dw_val_class_vec,
4298   dw_val_class_flag,
4299   dw_val_class_die_ref,
4300   dw_val_class_fde_ref,
4301   dw_val_class_lbl_id,
4302   dw_val_class_lineptr,
4303   dw_val_class_str,
4304   dw_val_class_macptr,
4305   dw_val_class_file,
4306   dw_val_class_data8,
4307   dw_val_class_decl_ref,
4308   dw_val_class_vms_delta
4309 };
4310
4311 /* Describe a floating point constant value, or a vector constant value.  */
4312
4313 typedef struct GTY(()) dw_vec_struct {
4314   unsigned char * GTY((length ("%h.length"))) array;
4315   unsigned length;
4316   unsigned elt_size;
4317 }
4318 dw_vec_const;
4319
4320 /* The dw_val_node describes an attribute's value, as it is
4321    represented internally.  */
4322
4323 typedef struct GTY(()) dw_val_struct {
4324   enum dw_val_class val_class;
4325   union dw_val_struct_union
4326     {
4327       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4328       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4329       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4330       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4331       HOST_WIDE_INT GTY ((default)) val_int;
4332       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4333       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4334       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4335       struct dw_val_die_union
4336         {
4337           dw_die_ref die;
4338           int external;
4339         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4340       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4341       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4342       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4343       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4344       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4345       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4346       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4347       struct dw_val_vms_delta_union
4348         {
4349           char * lbl1;
4350           char * lbl2;
4351         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4352     }
4353   GTY ((desc ("%1.val_class"))) v;
4354 }
4355 dw_val_node;
4356
4357 /* Locations in memory are described using a sequence of stack machine
4358    operations.  */
4359
4360 typedef struct GTY(()) dw_loc_descr_struct {
4361   dw_loc_descr_ref dw_loc_next;
4362   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4363   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4364      from DW_OP_addr with a dtp-relative symbol relocation.  */
4365   unsigned int dtprel : 1;
4366   int dw_loc_addr;
4367   dw_val_node dw_loc_oprnd1;
4368   dw_val_node dw_loc_oprnd2;
4369 }
4370 dw_loc_descr_node;
4371
4372 /* Location lists are ranges + location descriptions for that range,
4373    so you can track variables that are in different places over
4374    their entire life.  */
4375 typedef struct GTY(()) dw_loc_list_struct {
4376   dw_loc_list_ref dw_loc_next;
4377   const char *begin; /* Label for begin address of range */
4378   const char *end;  /* Label for end address of range */
4379   char *ll_symbol; /* Label for beginning of location list.
4380                       Only on head of list */
4381   const char *section; /* Section this loclist is relative to */
4382   dw_loc_descr_ref expr;
4383   hashval_t hash;
4384   bool emitted;
4385 } dw_loc_list_node;
4386
4387 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4388
4389 /* Convert a DWARF stack opcode into its string name.  */
4390
4391 static const char *
4392 dwarf_stack_op_name (unsigned int op)
4393 {
4394   switch (op)
4395     {
4396     case DW_OP_addr:
4397       return "DW_OP_addr";
4398     case DW_OP_deref:
4399       return "DW_OP_deref";
4400     case DW_OP_const1u:
4401       return "DW_OP_const1u";
4402     case DW_OP_const1s:
4403       return "DW_OP_const1s";
4404     case DW_OP_const2u:
4405       return "DW_OP_const2u";
4406     case DW_OP_const2s:
4407       return "DW_OP_const2s";
4408     case DW_OP_const4u:
4409       return "DW_OP_const4u";
4410     case DW_OP_const4s:
4411       return "DW_OP_const4s";
4412     case DW_OP_const8u:
4413       return "DW_OP_const8u";
4414     case DW_OP_const8s:
4415       return "DW_OP_const8s";
4416     case DW_OP_constu:
4417       return "DW_OP_constu";
4418     case DW_OP_consts:
4419       return "DW_OP_consts";
4420     case DW_OP_dup:
4421       return "DW_OP_dup";
4422     case DW_OP_drop:
4423       return "DW_OP_drop";
4424     case DW_OP_over:
4425       return "DW_OP_over";
4426     case DW_OP_pick:
4427       return "DW_OP_pick";
4428     case DW_OP_swap:
4429       return "DW_OP_swap";
4430     case DW_OP_rot:
4431       return "DW_OP_rot";
4432     case DW_OP_xderef:
4433       return "DW_OP_xderef";
4434     case DW_OP_abs:
4435       return "DW_OP_abs";
4436     case DW_OP_and:
4437       return "DW_OP_and";
4438     case DW_OP_div:
4439       return "DW_OP_div";
4440     case DW_OP_minus:
4441       return "DW_OP_minus";
4442     case DW_OP_mod:
4443       return "DW_OP_mod";
4444     case DW_OP_mul:
4445       return "DW_OP_mul";
4446     case DW_OP_neg:
4447       return "DW_OP_neg";
4448     case DW_OP_not:
4449       return "DW_OP_not";
4450     case DW_OP_or:
4451       return "DW_OP_or";
4452     case DW_OP_plus:
4453       return "DW_OP_plus";
4454     case DW_OP_plus_uconst:
4455       return "DW_OP_plus_uconst";
4456     case DW_OP_shl:
4457       return "DW_OP_shl";
4458     case DW_OP_shr:
4459       return "DW_OP_shr";
4460     case DW_OP_shra:
4461       return "DW_OP_shra";
4462     case DW_OP_xor:
4463       return "DW_OP_xor";
4464     case DW_OP_bra:
4465       return "DW_OP_bra";
4466     case DW_OP_eq:
4467       return "DW_OP_eq";
4468     case DW_OP_ge:
4469       return "DW_OP_ge";
4470     case DW_OP_gt:
4471       return "DW_OP_gt";
4472     case DW_OP_le:
4473       return "DW_OP_le";
4474     case DW_OP_lt:
4475       return "DW_OP_lt";
4476     case DW_OP_ne:
4477       return "DW_OP_ne";
4478     case DW_OP_skip:
4479       return "DW_OP_skip";
4480     case DW_OP_lit0:
4481       return "DW_OP_lit0";
4482     case DW_OP_lit1:
4483       return "DW_OP_lit1";
4484     case DW_OP_lit2:
4485       return "DW_OP_lit2";
4486     case DW_OP_lit3:
4487       return "DW_OP_lit3";
4488     case DW_OP_lit4:
4489       return "DW_OP_lit4";
4490     case DW_OP_lit5:
4491       return "DW_OP_lit5";
4492     case DW_OP_lit6:
4493       return "DW_OP_lit6";
4494     case DW_OP_lit7:
4495       return "DW_OP_lit7";
4496     case DW_OP_lit8:
4497       return "DW_OP_lit8";
4498     case DW_OP_lit9:
4499       return "DW_OP_lit9";
4500     case DW_OP_lit10:
4501       return "DW_OP_lit10";
4502     case DW_OP_lit11:
4503       return "DW_OP_lit11";
4504     case DW_OP_lit12:
4505       return "DW_OP_lit12";
4506     case DW_OP_lit13:
4507       return "DW_OP_lit13";
4508     case DW_OP_lit14:
4509       return "DW_OP_lit14";
4510     case DW_OP_lit15:
4511       return "DW_OP_lit15";
4512     case DW_OP_lit16:
4513       return "DW_OP_lit16";
4514     case DW_OP_lit17:
4515       return "DW_OP_lit17";
4516     case DW_OP_lit18:
4517       return "DW_OP_lit18";
4518     case DW_OP_lit19:
4519       return "DW_OP_lit19";
4520     case DW_OP_lit20:
4521       return "DW_OP_lit20";
4522     case DW_OP_lit21:
4523       return "DW_OP_lit21";
4524     case DW_OP_lit22:
4525       return "DW_OP_lit22";
4526     case DW_OP_lit23:
4527       return "DW_OP_lit23";
4528     case DW_OP_lit24:
4529       return "DW_OP_lit24";
4530     case DW_OP_lit25:
4531       return "DW_OP_lit25";
4532     case DW_OP_lit26:
4533       return "DW_OP_lit26";
4534     case DW_OP_lit27:
4535       return "DW_OP_lit27";
4536     case DW_OP_lit28:
4537       return "DW_OP_lit28";
4538     case DW_OP_lit29:
4539       return "DW_OP_lit29";
4540     case DW_OP_lit30:
4541       return "DW_OP_lit30";
4542     case DW_OP_lit31:
4543       return "DW_OP_lit31";
4544     case DW_OP_reg0:
4545       return "DW_OP_reg0";
4546     case DW_OP_reg1:
4547       return "DW_OP_reg1";
4548     case DW_OP_reg2:
4549       return "DW_OP_reg2";
4550     case DW_OP_reg3:
4551       return "DW_OP_reg3";
4552     case DW_OP_reg4:
4553       return "DW_OP_reg4";
4554     case DW_OP_reg5:
4555       return "DW_OP_reg5";
4556     case DW_OP_reg6:
4557       return "DW_OP_reg6";
4558     case DW_OP_reg7:
4559       return "DW_OP_reg7";
4560     case DW_OP_reg8:
4561       return "DW_OP_reg8";
4562     case DW_OP_reg9:
4563       return "DW_OP_reg9";
4564     case DW_OP_reg10:
4565       return "DW_OP_reg10";
4566     case DW_OP_reg11:
4567       return "DW_OP_reg11";
4568     case DW_OP_reg12:
4569       return "DW_OP_reg12";
4570     case DW_OP_reg13:
4571       return "DW_OP_reg13";
4572     case DW_OP_reg14:
4573       return "DW_OP_reg14";
4574     case DW_OP_reg15:
4575       return "DW_OP_reg15";
4576     case DW_OP_reg16:
4577       return "DW_OP_reg16";
4578     case DW_OP_reg17:
4579       return "DW_OP_reg17";
4580     case DW_OP_reg18:
4581       return "DW_OP_reg18";
4582     case DW_OP_reg19:
4583       return "DW_OP_reg19";
4584     case DW_OP_reg20:
4585       return "DW_OP_reg20";
4586     case DW_OP_reg21:
4587       return "DW_OP_reg21";
4588     case DW_OP_reg22:
4589       return "DW_OP_reg22";
4590     case DW_OP_reg23:
4591       return "DW_OP_reg23";
4592     case DW_OP_reg24:
4593       return "DW_OP_reg24";
4594     case DW_OP_reg25:
4595       return "DW_OP_reg25";
4596     case DW_OP_reg26:
4597       return "DW_OP_reg26";
4598     case DW_OP_reg27:
4599       return "DW_OP_reg27";
4600     case DW_OP_reg28:
4601       return "DW_OP_reg28";
4602     case DW_OP_reg29:
4603       return "DW_OP_reg29";
4604     case DW_OP_reg30:
4605       return "DW_OP_reg30";
4606     case DW_OP_reg31:
4607       return "DW_OP_reg31";
4608     case DW_OP_breg0:
4609       return "DW_OP_breg0";
4610     case DW_OP_breg1:
4611       return "DW_OP_breg1";
4612     case DW_OP_breg2:
4613       return "DW_OP_breg2";
4614     case DW_OP_breg3:
4615       return "DW_OP_breg3";
4616     case DW_OP_breg4:
4617       return "DW_OP_breg4";
4618     case DW_OP_breg5:
4619       return "DW_OP_breg5";
4620     case DW_OP_breg6:
4621       return "DW_OP_breg6";
4622     case DW_OP_breg7:
4623       return "DW_OP_breg7";
4624     case DW_OP_breg8:
4625       return "DW_OP_breg8";
4626     case DW_OP_breg9:
4627       return "DW_OP_breg9";
4628     case DW_OP_breg10:
4629       return "DW_OP_breg10";
4630     case DW_OP_breg11:
4631       return "DW_OP_breg11";
4632     case DW_OP_breg12:
4633       return "DW_OP_breg12";
4634     case DW_OP_breg13:
4635       return "DW_OP_breg13";
4636     case DW_OP_breg14:
4637       return "DW_OP_breg14";
4638     case DW_OP_breg15:
4639       return "DW_OP_breg15";
4640     case DW_OP_breg16:
4641       return "DW_OP_breg16";
4642     case DW_OP_breg17:
4643       return "DW_OP_breg17";
4644     case DW_OP_breg18:
4645       return "DW_OP_breg18";
4646     case DW_OP_breg19:
4647       return "DW_OP_breg19";
4648     case DW_OP_breg20:
4649       return "DW_OP_breg20";
4650     case DW_OP_breg21:
4651       return "DW_OP_breg21";
4652     case DW_OP_breg22:
4653       return "DW_OP_breg22";
4654     case DW_OP_breg23:
4655       return "DW_OP_breg23";
4656     case DW_OP_breg24:
4657       return "DW_OP_breg24";
4658     case DW_OP_breg25:
4659       return "DW_OP_breg25";
4660     case DW_OP_breg26:
4661       return "DW_OP_breg26";
4662     case DW_OP_breg27:
4663       return "DW_OP_breg27";
4664     case DW_OP_breg28:
4665       return "DW_OP_breg28";
4666     case DW_OP_breg29:
4667       return "DW_OP_breg29";
4668     case DW_OP_breg30:
4669       return "DW_OP_breg30";
4670     case DW_OP_breg31:
4671       return "DW_OP_breg31";
4672     case DW_OP_regx:
4673       return "DW_OP_regx";
4674     case DW_OP_fbreg:
4675       return "DW_OP_fbreg";
4676     case DW_OP_bregx:
4677       return "DW_OP_bregx";
4678     case DW_OP_piece:
4679       return "DW_OP_piece";
4680     case DW_OP_deref_size:
4681       return "DW_OP_deref_size";
4682     case DW_OP_xderef_size:
4683       return "DW_OP_xderef_size";
4684     case DW_OP_nop:
4685       return "DW_OP_nop";
4686
4687     case DW_OP_push_object_address:
4688       return "DW_OP_push_object_address";
4689     case DW_OP_call2:
4690       return "DW_OP_call2";
4691     case DW_OP_call4:
4692       return "DW_OP_call4";
4693     case DW_OP_call_ref:
4694       return "DW_OP_call_ref";
4695     case DW_OP_implicit_value:
4696       return "DW_OP_implicit_value";
4697     case DW_OP_stack_value:
4698       return "DW_OP_stack_value";
4699     case DW_OP_form_tls_address:
4700       return "DW_OP_form_tls_address";
4701     case DW_OP_call_frame_cfa:
4702       return "DW_OP_call_frame_cfa";
4703     case DW_OP_bit_piece:
4704       return "DW_OP_bit_piece";
4705
4706     case DW_OP_GNU_push_tls_address:
4707       return "DW_OP_GNU_push_tls_address";
4708     case DW_OP_GNU_uninit:
4709       return "DW_OP_GNU_uninit";
4710     case DW_OP_GNU_encoded_addr:
4711       return "DW_OP_GNU_encoded_addr";
4712     case DW_OP_GNU_implicit_pointer:
4713       return "DW_OP_GNU_implicit_pointer";
4714
4715     default:
4716       return "OP_<unknown>";
4717     }
4718 }
4719
4720 /* Return a pointer to a newly allocated location description.  Location
4721    descriptions are simple expression terms that can be strung
4722    together to form more complicated location (address) descriptions.  */
4723
4724 static inline dw_loc_descr_ref
4725 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4726                unsigned HOST_WIDE_INT oprnd2)
4727 {
4728   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4729
4730   descr->dw_loc_opc = op;
4731   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4732   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4733   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4734   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4735
4736   return descr;
4737 }
4738
4739 /* Return a pointer to a newly allocated location description for
4740    REG and OFFSET.  */
4741
4742 static inline dw_loc_descr_ref
4743 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4744 {
4745   if (reg <= 31)
4746     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4747                           offset, 0);
4748   else
4749     return new_loc_descr (DW_OP_bregx, reg, offset);
4750 }
4751
4752 /* Add a location description term to a location description expression.  */
4753
4754 static inline void
4755 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4756 {
4757   dw_loc_descr_ref *d;
4758
4759   /* Find the end of the chain.  */
4760   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4761     ;
4762
4763   *d = descr;
4764 }
4765
4766 /* Add a constant OFFSET to a location expression.  */
4767
4768 static void
4769 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4770 {
4771   dw_loc_descr_ref loc;
4772   HOST_WIDE_INT *p;
4773
4774   gcc_assert (*list_head != NULL);
4775
4776   if (!offset)
4777     return;
4778
4779   /* Find the end of the chain.  */
4780   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4781     ;
4782
4783   p = NULL;
4784   if (loc->dw_loc_opc == DW_OP_fbreg
4785       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4786     p = &loc->dw_loc_oprnd1.v.val_int;
4787   else if (loc->dw_loc_opc == DW_OP_bregx)
4788     p = &loc->dw_loc_oprnd2.v.val_int;
4789
4790   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4791      offset.  Don't optimize if an signed integer overflow would happen.  */
4792   if (p != NULL
4793       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4794           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4795     *p += offset;
4796
4797   else if (offset > 0)
4798     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4799
4800   else
4801     {
4802       loc->dw_loc_next = int_loc_descriptor (-offset);
4803       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4804     }
4805 }
4806
4807 /* Add a constant OFFSET to a location list.  */
4808
4809 static void
4810 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4811 {
4812   dw_loc_list_ref d;
4813   for (d = list_head; d != NULL; d = d->dw_loc_next)
4814     loc_descr_plus_const (&d->expr, offset);
4815 }
4816
4817 #define DWARF_REF_SIZE  \
4818   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4819
4820 /* Return the size of a location descriptor.  */
4821
4822 static unsigned long
4823 size_of_loc_descr (dw_loc_descr_ref loc)
4824 {
4825   unsigned long size = 1;
4826
4827   switch (loc->dw_loc_opc)
4828     {
4829     case DW_OP_addr:
4830       size += DWARF2_ADDR_SIZE;
4831       break;
4832     case DW_OP_const1u:
4833     case DW_OP_const1s:
4834       size += 1;
4835       break;
4836     case DW_OP_const2u:
4837     case DW_OP_const2s:
4838       size += 2;
4839       break;
4840     case DW_OP_const4u:
4841     case DW_OP_const4s:
4842       size += 4;
4843       break;
4844     case DW_OP_const8u:
4845     case DW_OP_const8s:
4846       size += 8;
4847       break;
4848     case DW_OP_constu:
4849       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4850       break;
4851     case DW_OP_consts:
4852       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4853       break;
4854     case DW_OP_pick:
4855       size += 1;
4856       break;
4857     case DW_OP_plus_uconst:
4858       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4859       break;
4860     case DW_OP_skip:
4861     case DW_OP_bra:
4862       size += 2;
4863       break;
4864     case DW_OP_breg0:
4865     case DW_OP_breg1:
4866     case DW_OP_breg2:
4867     case DW_OP_breg3:
4868     case DW_OP_breg4:
4869     case DW_OP_breg5:
4870     case DW_OP_breg6:
4871     case DW_OP_breg7:
4872     case DW_OP_breg8:
4873     case DW_OP_breg9:
4874     case DW_OP_breg10:
4875     case DW_OP_breg11:
4876     case DW_OP_breg12:
4877     case DW_OP_breg13:
4878     case DW_OP_breg14:
4879     case DW_OP_breg15:
4880     case DW_OP_breg16:
4881     case DW_OP_breg17:
4882     case DW_OP_breg18:
4883     case DW_OP_breg19:
4884     case DW_OP_breg20:
4885     case DW_OP_breg21:
4886     case DW_OP_breg22:
4887     case DW_OP_breg23:
4888     case DW_OP_breg24:
4889     case DW_OP_breg25:
4890     case DW_OP_breg26:
4891     case DW_OP_breg27:
4892     case DW_OP_breg28:
4893     case DW_OP_breg29:
4894     case DW_OP_breg30:
4895     case DW_OP_breg31:
4896       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4897       break;
4898     case DW_OP_regx:
4899       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4900       break;
4901     case DW_OP_fbreg:
4902       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4903       break;
4904     case DW_OP_bregx:
4905       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4906       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4907       break;
4908     case DW_OP_piece:
4909       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4910       break;
4911     case DW_OP_bit_piece:
4912       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4913       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4914       break;
4915     case DW_OP_deref_size:
4916     case DW_OP_xderef_size:
4917       size += 1;
4918       break;
4919     case DW_OP_call2:
4920       size += 2;
4921       break;
4922     case DW_OP_call4:
4923       size += 4;
4924       break;
4925     case DW_OP_call_ref:
4926       size += DWARF_REF_SIZE;
4927       break;
4928     case DW_OP_implicit_value:
4929       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4930               + loc->dw_loc_oprnd1.v.val_unsigned;
4931       break;
4932     case DW_OP_GNU_implicit_pointer:
4933       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4934       break;
4935     default:
4936       break;
4937     }
4938
4939   return size;
4940 }
4941
4942 /* Return the size of a series of location descriptors.  */
4943
4944 static unsigned long
4945 size_of_locs (dw_loc_descr_ref loc)
4946 {
4947   dw_loc_descr_ref l;
4948   unsigned long size;
4949
4950   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4951      field, to avoid writing to a PCH file.  */
4952   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4953     {
4954       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4955         break;
4956       size += size_of_loc_descr (l);
4957     }
4958   if (! l)
4959     return size;
4960
4961   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4962     {
4963       l->dw_loc_addr = size;
4964       size += size_of_loc_descr (l);
4965     }
4966
4967   return size;
4968 }
4969
4970 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4971 static void get_ref_die_offset_label (char *, dw_die_ref);
4972
4973 /* Output location description stack opcode's operands (if any).  */
4974
4975 static void
4976 output_loc_operands (dw_loc_descr_ref loc)
4977 {
4978   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4979   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4980
4981   switch (loc->dw_loc_opc)
4982     {
4983 #ifdef DWARF2_DEBUGGING_INFO
4984     case DW_OP_const2u:
4985     case DW_OP_const2s:
4986       dw2_asm_output_data (2, val1->v.val_int, NULL);
4987       break;
4988     case DW_OP_const4u:
4989       if (loc->dtprel)
4990         {
4991           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4992           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4993                                                val1->v.val_addr);
4994           fputc ('\n', asm_out_file);
4995           break;
4996         }
4997       /* FALLTHRU */
4998     case DW_OP_const4s:
4999       dw2_asm_output_data (4, val1->v.val_int, NULL);
5000       break;
5001     case DW_OP_const8u:
5002       if (loc->dtprel)
5003         {
5004           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5005           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5006                                                val1->v.val_addr);
5007           fputc ('\n', asm_out_file);
5008           break;
5009         }
5010       /* FALLTHRU */
5011     case DW_OP_const8s:
5012       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5013       dw2_asm_output_data (8, val1->v.val_int, NULL);
5014       break;
5015     case DW_OP_skip:
5016     case DW_OP_bra:
5017       {
5018         int offset;
5019
5020         gcc_assert (val1->val_class == dw_val_class_loc);
5021         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5022
5023         dw2_asm_output_data (2, offset, NULL);
5024       }
5025       break;
5026     case DW_OP_implicit_value:
5027       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5028       switch (val2->val_class)
5029         {
5030         case dw_val_class_const:
5031           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5032           break;
5033         case dw_val_class_vec:
5034           {
5035             unsigned int elt_size = val2->v.val_vec.elt_size;
5036             unsigned int len = val2->v.val_vec.length;
5037             unsigned int i;
5038             unsigned char *p;
5039
5040             if (elt_size > sizeof (HOST_WIDE_INT))
5041               {
5042                 elt_size /= 2;
5043                 len *= 2;
5044               }
5045             for (i = 0, p = val2->v.val_vec.array;
5046                  i < len;
5047                  i++, p += elt_size)
5048               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5049                                    "fp or vector constant word %u", i);
5050           }
5051           break;
5052         case dw_val_class_const_double:
5053           {
5054             unsigned HOST_WIDE_INT first, second;
5055
5056             if (WORDS_BIG_ENDIAN)
5057               {
5058                 first = val2->v.val_double.high;
5059                 second = val2->v.val_double.low;
5060               }
5061             else
5062               {
5063                 first = val2->v.val_double.low;
5064                 second = val2->v.val_double.high;
5065               }
5066             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5067                                  first, NULL);
5068             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5069                                  second, NULL);
5070           }
5071           break;
5072         case dw_val_class_addr:
5073           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5074           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5075           break;
5076         default:
5077           gcc_unreachable ();
5078         }
5079       break;
5080 #else
5081     case DW_OP_const2u:
5082     case DW_OP_const2s:
5083     case DW_OP_const4u:
5084     case DW_OP_const4s:
5085     case DW_OP_const8u:
5086     case DW_OP_const8s:
5087     case DW_OP_skip:
5088     case DW_OP_bra:
5089     case DW_OP_implicit_value:
5090       /* We currently don't make any attempt to make sure these are
5091          aligned properly like we do for the main unwind info, so
5092          don't support emitting things larger than a byte if we're
5093          only doing unwinding.  */
5094       gcc_unreachable ();
5095 #endif
5096     case DW_OP_const1u:
5097     case DW_OP_const1s:
5098       dw2_asm_output_data (1, val1->v.val_int, NULL);
5099       break;
5100     case DW_OP_constu:
5101       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5102       break;
5103     case DW_OP_consts:
5104       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5105       break;
5106     case DW_OP_pick:
5107       dw2_asm_output_data (1, val1->v.val_int, NULL);
5108       break;
5109     case DW_OP_plus_uconst:
5110       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5111       break;
5112     case DW_OP_breg0:
5113     case DW_OP_breg1:
5114     case DW_OP_breg2:
5115     case DW_OP_breg3:
5116     case DW_OP_breg4:
5117     case DW_OP_breg5:
5118     case DW_OP_breg6:
5119     case DW_OP_breg7:
5120     case DW_OP_breg8:
5121     case DW_OP_breg9:
5122     case DW_OP_breg10:
5123     case DW_OP_breg11:
5124     case DW_OP_breg12:
5125     case DW_OP_breg13:
5126     case DW_OP_breg14:
5127     case DW_OP_breg15:
5128     case DW_OP_breg16:
5129     case DW_OP_breg17:
5130     case DW_OP_breg18:
5131     case DW_OP_breg19:
5132     case DW_OP_breg20:
5133     case DW_OP_breg21:
5134     case DW_OP_breg22:
5135     case DW_OP_breg23:
5136     case DW_OP_breg24:
5137     case DW_OP_breg25:
5138     case DW_OP_breg26:
5139     case DW_OP_breg27:
5140     case DW_OP_breg28:
5141     case DW_OP_breg29:
5142     case DW_OP_breg30:
5143     case DW_OP_breg31:
5144       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5145       break;
5146     case DW_OP_regx:
5147       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5148       break;
5149     case DW_OP_fbreg:
5150       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5151       break;
5152     case DW_OP_bregx:
5153       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5154       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5155       break;
5156     case DW_OP_piece:
5157       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5158       break;
5159     case DW_OP_bit_piece:
5160       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5161       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5162       break;
5163     case DW_OP_deref_size:
5164     case DW_OP_xderef_size:
5165       dw2_asm_output_data (1, val1->v.val_int, NULL);
5166       break;
5167
5168     case DW_OP_addr:
5169       if (loc->dtprel)
5170         {
5171           if (targetm.asm_out.output_dwarf_dtprel)
5172             {
5173               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5174                                                    DWARF2_ADDR_SIZE,
5175                                                    val1->v.val_addr);
5176               fputc ('\n', asm_out_file);
5177             }
5178           else
5179             gcc_unreachable ();
5180         }
5181       else
5182         {
5183 #ifdef DWARF2_DEBUGGING_INFO
5184           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5185 #else
5186           gcc_unreachable ();
5187 #endif
5188         }
5189       break;
5190
5191     case DW_OP_GNU_implicit_pointer:
5192       {
5193         char label[MAX_ARTIFICIAL_LABEL_BYTES
5194                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5195         gcc_assert (val1->val_class == dw_val_class_die_ref);
5196         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5197         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5198         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5199       }
5200       break;
5201
5202     default:
5203       /* Other codes have no operands.  */
5204       break;
5205     }
5206 }
5207
5208 /* Output a sequence of location operations.  */
5209
5210 static void
5211 output_loc_sequence (dw_loc_descr_ref loc)
5212 {
5213   for (; loc != NULL; loc = loc->dw_loc_next)
5214     {
5215       /* Output the opcode.  */
5216       dw2_asm_output_data (1, loc->dw_loc_opc,
5217                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5218
5219       /* Output the operand(s) (if any).  */
5220       output_loc_operands (loc);
5221     }
5222 }
5223
5224 /* Output location description stack opcode's operands (if any).
5225    The output is single bytes on a line, suitable for .cfi_escape.  */
5226
5227 static void
5228 output_loc_operands_raw (dw_loc_descr_ref loc)
5229 {
5230   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5231   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5232
5233   switch (loc->dw_loc_opc)
5234     {
5235     case DW_OP_addr:
5236     case DW_OP_implicit_value:
5237       /* We cannot output addresses in .cfi_escape, only bytes.  */
5238       gcc_unreachable ();
5239
5240     case DW_OP_const1u:
5241     case DW_OP_const1s:
5242     case DW_OP_pick:
5243     case DW_OP_deref_size:
5244     case DW_OP_xderef_size:
5245       fputc (',', asm_out_file);
5246       dw2_asm_output_data_raw (1, val1->v.val_int);
5247       break;
5248
5249     case DW_OP_const2u:
5250     case DW_OP_const2s:
5251       fputc (',', asm_out_file);
5252       dw2_asm_output_data_raw (2, val1->v.val_int);
5253       break;
5254
5255     case DW_OP_const4u:
5256     case DW_OP_const4s:
5257       fputc (',', asm_out_file);
5258       dw2_asm_output_data_raw (4, val1->v.val_int);
5259       break;
5260
5261     case DW_OP_const8u:
5262     case DW_OP_const8s:
5263       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5264       fputc (',', asm_out_file);
5265       dw2_asm_output_data_raw (8, val1->v.val_int);
5266       break;
5267
5268     case DW_OP_skip:
5269     case DW_OP_bra:
5270       {
5271         int offset;
5272
5273         gcc_assert (val1->val_class == dw_val_class_loc);
5274         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5275
5276         fputc (',', asm_out_file);
5277         dw2_asm_output_data_raw (2, offset);
5278       }
5279       break;
5280
5281     case DW_OP_constu:
5282     case DW_OP_plus_uconst:
5283     case DW_OP_regx:
5284     case DW_OP_piece:
5285       fputc (',', asm_out_file);
5286       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5287       break;
5288
5289     case DW_OP_bit_piece:
5290       fputc (',', asm_out_file);
5291       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5292       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5293       break;
5294
5295     case DW_OP_consts:
5296     case DW_OP_breg0:
5297     case DW_OP_breg1:
5298     case DW_OP_breg2:
5299     case DW_OP_breg3:
5300     case DW_OP_breg4:
5301     case DW_OP_breg5:
5302     case DW_OP_breg6:
5303     case DW_OP_breg7:
5304     case DW_OP_breg8:
5305     case DW_OP_breg9:
5306     case DW_OP_breg10:
5307     case DW_OP_breg11:
5308     case DW_OP_breg12:
5309     case DW_OP_breg13:
5310     case DW_OP_breg14:
5311     case DW_OP_breg15:
5312     case DW_OP_breg16:
5313     case DW_OP_breg17:
5314     case DW_OP_breg18:
5315     case DW_OP_breg19:
5316     case DW_OP_breg20:
5317     case DW_OP_breg21:
5318     case DW_OP_breg22:
5319     case DW_OP_breg23:
5320     case DW_OP_breg24:
5321     case DW_OP_breg25:
5322     case DW_OP_breg26:
5323     case DW_OP_breg27:
5324     case DW_OP_breg28:
5325     case DW_OP_breg29:
5326     case DW_OP_breg30:
5327     case DW_OP_breg31:
5328     case DW_OP_fbreg:
5329       fputc (',', asm_out_file);
5330       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5331       break;
5332
5333     case DW_OP_bregx:
5334       fputc (',', asm_out_file);
5335       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5336       fputc (',', asm_out_file);
5337       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5338       break;
5339
5340     case DW_OP_GNU_implicit_pointer:
5341       gcc_unreachable ();
5342       break;
5343
5344     default:
5345       /* Other codes have no operands.  */
5346       break;
5347     }
5348 }
5349
5350 static void
5351 output_loc_sequence_raw (dw_loc_descr_ref loc)
5352 {
5353   while (1)
5354     {
5355       /* Output the opcode.  */
5356       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5357       output_loc_operands_raw (loc);
5358
5359       if (!loc->dw_loc_next)
5360         break;
5361       loc = loc->dw_loc_next;
5362
5363       fputc (',', asm_out_file);
5364     }
5365 }
5366
5367 /* This routine will generate the correct assembly data for a location
5368    description based on a cfi entry with a complex address.  */
5369
5370 static void
5371 output_cfa_loc (dw_cfi_ref cfi)
5372 {
5373   dw_loc_descr_ref loc;
5374   unsigned long size;
5375
5376   if (cfi->dw_cfi_opc == DW_CFA_expression)
5377     {
5378       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5379       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5380     }
5381   else
5382     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5383
5384   /* Output the size of the block.  */
5385   size = size_of_locs (loc);
5386   dw2_asm_output_data_uleb128 (size, NULL);
5387
5388   /* Now output the operations themselves.  */
5389   output_loc_sequence (loc);
5390 }
5391
5392 /* Similar, but used for .cfi_escape.  */
5393
5394 static void
5395 output_cfa_loc_raw (dw_cfi_ref cfi)
5396 {
5397   dw_loc_descr_ref loc;
5398   unsigned long size;
5399
5400   if (cfi->dw_cfi_opc == DW_CFA_expression)
5401     {
5402       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5403       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5404     }
5405   else
5406     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5407
5408   /* Output the size of the block.  */
5409   size = size_of_locs (loc);
5410   dw2_asm_output_data_uleb128_raw (size);
5411   fputc (',', asm_out_file);
5412
5413   /* Now output the operations themselves.  */
5414   output_loc_sequence_raw (loc);
5415 }
5416
5417 /* This function builds a dwarf location descriptor sequence from a
5418    dw_cfa_location, adding the given OFFSET to the result of the
5419    expression.  */
5420
5421 static struct dw_loc_descr_struct *
5422 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5423 {
5424   struct dw_loc_descr_struct *head, *tmp;
5425
5426   offset += cfa->offset;
5427
5428   if (cfa->indirect)
5429     {
5430       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5431       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5432       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5433       add_loc_descr (&head, tmp);
5434       if (offset != 0)
5435         {
5436           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5437           add_loc_descr (&head, tmp);
5438         }
5439     }
5440   else
5441     head = new_reg_loc_descr (cfa->reg, offset);
5442
5443   return head;
5444 }
5445
5446 /* This function builds a dwarf location descriptor sequence for
5447    the address at OFFSET from the CFA when stack is aligned to
5448    ALIGNMENT byte.  */
5449
5450 static struct dw_loc_descr_struct *
5451 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5452 {
5453   struct dw_loc_descr_struct *head;
5454   unsigned int dwarf_fp
5455     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5456
5457  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5458   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5459     {
5460       head = new_reg_loc_descr (dwarf_fp, 0);
5461       add_loc_descr (&head, int_loc_descriptor (alignment));
5462       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5463       loc_descr_plus_const (&head, offset);
5464     }
5465   else
5466     head = new_reg_loc_descr (dwarf_fp, offset);
5467   return head;
5468 }
5469
5470 /* This function fills in aa dw_cfa_location structure from a dwarf location
5471    descriptor sequence.  */
5472
5473 static void
5474 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5475 {
5476   struct dw_loc_descr_struct *ptr;
5477   cfa->offset = 0;
5478   cfa->base_offset = 0;
5479   cfa->indirect = 0;
5480   cfa->reg = -1;
5481
5482   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5483     {
5484       enum dwarf_location_atom op = ptr->dw_loc_opc;
5485
5486       switch (op)
5487         {
5488         case DW_OP_reg0:
5489         case DW_OP_reg1:
5490         case DW_OP_reg2:
5491         case DW_OP_reg3:
5492         case DW_OP_reg4:
5493         case DW_OP_reg5:
5494         case DW_OP_reg6:
5495         case DW_OP_reg7:
5496         case DW_OP_reg8:
5497         case DW_OP_reg9:
5498         case DW_OP_reg10:
5499         case DW_OP_reg11:
5500         case DW_OP_reg12:
5501         case DW_OP_reg13:
5502         case DW_OP_reg14:
5503         case DW_OP_reg15:
5504         case DW_OP_reg16:
5505         case DW_OP_reg17:
5506         case DW_OP_reg18:
5507         case DW_OP_reg19:
5508         case DW_OP_reg20:
5509         case DW_OP_reg21:
5510         case DW_OP_reg22:
5511         case DW_OP_reg23:
5512         case DW_OP_reg24:
5513         case DW_OP_reg25:
5514         case DW_OP_reg26:
5515         case DW_OP_reg27:
5516         case DW_OP_reg28:
5517         case DW_OP_reg29:
5518         case DW_OP_reg30:
5519         case DW_OP_reg31:
5520           cfa->reg = op - DW_OP_reg0;
5521           break;
5522         case DW_OP_regx:
5523           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5524           break;
5525         case DW_OP_breg0:
5526         case DW_OP_breg1:
5527         case DW_OP_breg2:
5528         case DW_OP_breg3:
5529         case DW_OP_breg4:
5530         case DW_OP_breg5:
5531         case DW_OP_breg6:
5532         case DW_OP_breg7:
5533         case DW_OP_breg8:
5534         case DW_OP_breg9:
5535         case DW_OP_breg10:
5536         case DW_OP_breg11:
5537         case DW_OP_breg12:
5538         case DW_OP_breg13:
5539         case DW_OP_breg14:
5540         case DW_OP_breg15:
5541         case DW_OP_breg16:
5542         case DW_OP_breg17:
5543         case DW_OP_breg18:
5544         case DW_OP_breg19:
5545         case DW_OP_breg20:
5546         case DW_OP_breg21:
5547         case DW_OP_breg22:
5548         case DW_OP_breg23:
5549         case DW_OP_breg24:
5550         case DW_OP_breg25:
5551         case DW_OP_breg26:
5552         case DW_OP_breg27:
5553         case DW_OP_breg28:
5554         case DW_OP_breg29:
5555         case DW_OP_breg30:
5556         case DW_OP_breg31:
5557           cfa->reg = op - DW_OP_breg0;
5558           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5559           break;
5560         case DW_OP_bregx:
5561           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5562           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5563           break;
5564         case DW_OP_deref:
5565           cfa->indirect = 1;
5566           break;
5567         case DW_OP_plus_uconst:
5568           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5569           break;
5570         default:
5571           internal_error ("DW_LOC_OP %s not implemented",
5572                           dwarf_stack_op_name (ptr->dw_loc_opc));
5573         }
5574     }
5575 }
5576 \f
5577 /* And now, the support for symbolic debugging information.  */
5578
5579 /* .debug_str support.  */
5580 static int output_indirect_string (void **, void *);
5581
5582 static void dwarf2out_init (const char *);
5583 static void dwarf2out_finish (const char *);
5584 static void dwarf2out_assembly_start (void);
5585 static void dwarf2out_define (unsigned int, const char *);
5586 static void dwarf2out_undef (unsigned int, const char *);
5587 static void dwarf2out_start_source_file (unsigned, const char *);
5588 static void dwarf2out_end_source_file (unsigned);
5589 static void dwarf2out_function_decl (tree);
5590 static void dwarf2out_begin_block (unsigned, unsigned);
5591 static void dwarf2out_end_block (unsigned, unsigned);
5592 static bool dwarf2out_ignore_block (const_tree);
5593 static void dwarf2out_global_decl (tree);
5594 static void dwarf2out_type_decl (tree, int);
5595 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5596 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5597                                                  dw_die_ref);
5598 static void dwarf2out_abstract_function (tree);
5599 static void dwarf2out_var_location (rtx);
5600 static void dwarf2out_direct_call (tree);
5601 static void dwarf2out_virtual_call_token (tree, int);
5602 static void dwarf2out_copy_call_info (rtx, rtx);
5603 static void dwarf2out_virtual_call (int);
5604 static void dwarf2out_begin_function (tree);
5605 static void dwarf2out_set_name (tree, tree);
5606
5607 /* The debug hooks structure.  */
5608
5609 const struct gcc_debug_hooks dwarf2_debug_hooks =
5610 {
5611   dwarf2out_init,
5612   dwarf2out_finish,
5613   dwarf2out_assembly_start,
5614   dwarf2out_define,
5615   dwarf2out_undef,
5616   dwarf2out_start_source_file,
5617   dwarf2out_end_source_file,
5618   dwarf2out_begin_block,
5619   dwarf2out_end_block,
5620   dwarf2out_ignore_block,
5621   dwarf2out_source_line,
5622   dwarf2out_begin_prologue,
5623 #if VMS_DEBUGGING_INFO
5624   dwarf2out_vms_end_prologue,
5625   dwarf2out_vms_begin_epilogue,
5626 #else
5627   debug_nothing_int_charstar,
5628   debug_nothing_int_charstar,
5629 #endif
5630   dwarf2out_end_epilogue,
5631   dwarf2out_begin_function,
5632   debug_nothing_int,            /* end_function */
5633   dwarf2out_function_decl,      /* function_decl */
5634   dwarf2out_global_decl,
5635   dwarf2out_type_decl,          /* type_decl */
5636   dwarf2out_imported_module_or_decl,
5637   debug_nothing_tree,           /* deferred_inline_function */
5638   /* The DWARF 2 backend tries to reduce debugging bloat by not
5639      emitting the abstract description of inline functions until
5640      something tries to reference them.  */
5641   dwarf2out_abstract_function,  /* outlining_inline_function */
5642   debug_nothing_rtx,            /* label */
5643   debug_nothing_int,            /* handle_pch */
5644   dwarf2out_var_location,
5645   dwarf2out_switch_text_section,
5646   dwarf2out_direct_call,
5647   dwarf2out_virtual_call_token,
5648   dwarf2out_copy_call_info,
5649   dwarf2out_virtual_call,
5650   dwarf2out_set_name,
5651   1,                            /* start_end_main_source_file */
5652   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5653 };
5654 \f
5655 /* NOTE: In the comments in this file, many references are made to
5656    "Debugging Information Entries".  This term is abbreviated as `DIE'
5657    throughout the remainder of this file.  */
5658
5659 /* An internal representation of the DWARF output is built, and then
5660    walked to generate the DWARF debugging info.  The walk of the internal
5661    representation is done after the entire program has been compiled.
5662    The types below are used to describe the internal representation.  */
5663
5664 /* Various DIE's use offsets relative to the beginning of the
5665    .debug_info section to refer to each other.  */
5666
5667 typedef long int dw_offset;
5668
5669 /* Define typedefs here to avoid circular dependencies.  */
5670
5671 typedef struct dw_attr_struct *dw_attr_ref;
5672 typedef struct dw_line_info_struct *dw_line_info_ref;
5673 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5674 typedef struct pubname_struct *pubname_ref;
5675 typedef struct dw_ranges_struct *dw_ranges_ref;
5676 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5677 typedef struct comdat_type_struct *comdat_type_node_ref;
5678
5679 /* Each entry in the line_info_table maintains the file and
5680    line number associated with the label generated for that
5681    entry.  The label gives the PC value associated with
5682    the line number entry.  */
5683
5684 typedef struct GTY(()) dw_line_info_struct {
5685   unsigned long dw_file_num;
5686   unsigned long dw_line_num;
5687 }
5688 dw_line_info_entry;
5689
5690 /* Line information for functions in separate sections; each one gets its
5691    own sequence.  */
5692 typedef struct GTY(()) dw_separate_line_info_struct {
5693   unsigned long dw_file_num;
5694   unsigned long dw_line_num;
5695   unsigned long function;
5696 }
5697 dw_separate_line_info_entry;
5698
5699 /* Each DIE attribute has a field specifying the attribute kind,
5700    a link to the next attribute in the chain, and an attribute value.
5701    Attributes are typically linked below the DIE they modify.  */
5702
5703 typedef struct GTY(()) dw_attr_struct {
5704   enum dwarf_attribute dw_attr;
5705   dw_val_node dw_attr_val;
5706 }
5707 dw_attr_node;
5708
5709 DEF_VEC_O(dw_attr_node);
5710 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5711
5712 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5713    The children of each node form a circular list linked by
5714    die_sib.  die_child points to the node *before* the "first" child node.  */
5715
5716 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5717   union die_symbol_or_type_node
5718     {
5719       char * GTY ((tag ("0"))) die_symbol;
5720       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5721     }
5722   GTY ((desc ("dwarf_version >= 4"))) die_id;
5723   VEC(dw_attr_node,gc) * die_attr;
5724   dw_die_ref die_parent;
5725   dw_die_ref die_child;
5726   dw_die_ref die_sib;
5727   dw_die_ref die_definition; /* ref from a specification to its definition */
5728   dw_offset die_offset;
5729   unsigned long die_abbrev;
5730   int die_mark;
5731   /* Die is used and must not be pruned as unused.  */
5732   int die_perennial_p;
5733   unsigned int decl_id;
5734   enum dwarf_tag die_tag;
5735 }
5736 die_node;
5737
5738 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5739 #define FOR_EACH_CHILD(die, c, expr) do {       \
5740   c = die->die_child;                           \
5741   if (c) do {                                   \
5742     c = c->die_sib;                             \
5743     expr;                                       \
5744   } while (c != die->die_child);                \
5745 } while (0)
5746
5747 /* The pubname structure */
5748
5749 typedef struct GTY(()) pubname_struct {
5750   dw_die_ref die;
5751   const char *name;
5752 }
5753 pubname_entry;
5754
5755 DEF_VEC_O(pubname_entry);
5756 DEF_VEC_ALLOC_O(pubname_entry, gc);
5757
5758 struct GTY(()) dw_ranges_struct {
5759   /* If this is positive, it's a block number, otherwise it's a
5760      bitwise-negated index into dw_ranges_by_label.  */
5761   int num;
5762 };
5763
5764 /* A structure to hold a macinfo entry.  */
5765
5766 typedef struct GTY(()) macinfo_struct {
5767   unsigned HOST_WIDE_INT code;
5768   unsigned HOST_WIDE_INT lineno;
5769   const char *info;
5770 }
5771 macinfo_entry;
5772
5773 DEF_VEC_O(macinfo_entry);
5774 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5775
5776 struct GTY(()) dw_ranges_by_label_struct {
5777   const char *begin;
5778   const char *end;
5779 };
5780
5781 /* The comdat type node structure.  */
5782 typedef struct GTY(()) comdat_type_struct
5783 {
5784   dw_die_ref root_die;
5785   dw_die_ref type_die;
5786   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5787   struct comdat_type_struct *next;
5788 }
5789 comdat_type_node;
5790
5791 /* The limbo die list structure.  */
5792 typedef struct GTY(()) limbo_die_struct {
5793   dw_die_ref die;
5794   tree created_for;
5795   struct limbo_die_struct *next;
5796 }
5797 limbo_die_node;
5798
5799 typedef struct GTY(()) skeleton_chain_struct
5800 {
5801   dw_die_ref old_die;
5802   dw_die_ref new_die;
5803   struct skeleton_chain_struct *parent;
5804 }
5805 skeleton_chain_node;
5806
5807 /* How to start an assembler comment.  */
5808 #ifndef ASM_COMMENT_START
5809 #define ASM_COMMENT_START ";#"
5810 #endif
5811
5812 /* Define a macro which returns nonzero for a TYPE_DECL which was
5813    implicitly generated for a tagged type.
5814
5815    Note that unlike the gcc front end (which generates a NULL named
5816    TYPE_DECL node for each complete tagged type, each array type, and
5817    each function type node created) the g++ front end generates a
5818    _named_ TYPE_DECL node for each tagged type node created.
5819    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5820    generate a DW_TAG_typedef DIE for them.  */
5821
5822 #define TYPE_DECL_IS_STUB(decl)                         \
5823   (DECL_NAME (decl) == NULL_TREE                        \
5824    || (DECL_ARTIFICIAL (decl)                           \
5825        && is_tagged_type (TREE_TYPE (decl))             \
5826        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5827            /* This is necessary for stub decls that     \
5828               appear in nested inline functions.  */    \
5829            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5830                && (decl_ultimate_origin (decl)          \
5831                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5832
5833 /* Information concerning the compilation unit's programming
5834    language, and compiler version.  */
5835
5836 /* Fixed size portion of the DWARF compilation unit header.  */
5837 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5838   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5839
5840 /* Fixed size portion of the DWARF comdat type unit header.  */
5841 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5842   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5843    + DWARF_OFFSET_SIZE)
5844
5845 /* Fixed size portion of public names info.  */
5846 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5847
5848 /* Fixed size portion of the address range info.  */
5849 #define DWARF_ARANGES_HEADER_SIZE                                       \
5850   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5851                 DWARF2_ADDR_SIZE * 2)                                   \
5852    - DWARF_INITIAL_LENGTH_SIZE)
5853
5854 /* Size of padding portion in the address range info.  It must be
5855    aligned to twice the pointer size.  */
5856 #define DWARF_ARANGES_PAD_SIZE \
5857   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5858                 DWARF2_ADDR_SIZE * 2)                              \
5859    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5860
5861 /* Use assembler line directives if available.  */
5862 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5863 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5864 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5865 #else
5866 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5867 #endif
5868 #endif
5869
5870 /* Minimum line offset in a special line info. opcode.
5871    This value was chosen to give a reasonable range of values.  */
5872 #define DWARF_LINE_BASE  -10
5873
5874 /* First special line opcode - leave room for the standard opcodes.  */
5875 #define DWARF_LINE_OPCODE_BASE  10
5876
5877 /* Range of line offsets in a special line info. opcode.  */
5878 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5879
5880 /* Flag that indicates the initial value of the is_stmt_start flag.
5881    In the present implementation, we do not mark any lines as
5882    the beginning of a source statement, because that information
5883    is not made available by the GCC front-end.  */
5884 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5885
5886 /* Maximum number of operations per instruction bundle.  */
5887 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5888 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5889 #endif
5890
5891 /* This location is used by calc_die_sizes() to keep track
5892    the offset of each DIE within the .debug_info section.  */
5893 static unsigned long next_die_offset;
5894
5895 /* Record the root of the DIE's built for the current compilation unit.  */
5896 static GTY(()) dw_die_ref single_comp_unit_die;
5897
5898 /* A list of type DIEs that have been separated into comdat sections.  */
5899 static GTY(()) comdat_type_node *comdat_type_list;
5900
5901 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5902 static GTY(()) limbo_die_node *limbo_die_list;
5903
5904 /* A list of DIEs for which we may have to generate
5905    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5906 static GTY(()) limbo_die_node *deferred_asm_name;
5907
5908 /* Filenames referenced by this compilation unit.  */
5909 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5910
5911 /* A hash table of references to DIE's that describe declarations.
5912    The key is a DECL_UID() which is a unique number identifying each decl.  */
5913 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5914
5915 /* A hash table of references to DIE's that describe COMMON blocks.
5916    The key is DECL_UID() ^ die_parent.  */
5917 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5918
5919 typedef struct GTY(()) die_arg_entry_struct {
5920     dw_die_ref die;
5921     tree arg;
5922 } die_arg_entry;
5923
5924 DEF_VEC_O(die_arg_entry);
5925 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5926
5927 /* Node of the variable location list.  */
5928 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5929   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5930      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5931      in mode of the EXPR_LIST node and first EXPR_LIST operand
5932      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5933      location or NULL for padding.  For larger bitsizes,
5934      mode is 0 and first operand is a CONCAT with bitsize
5935      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5936      NULL as second operand.  */
5937   rtx GTY (()) loc;
5938   const char * GTY (()) label;
5939   struct var_loc_node * GTY (()) next;
5940 };
5941
5942 /* Variable location list.  */
5943 struct GTY (()) var_loc_list_def {
5944   struct var_loc_node * GTY (()) first;
5945
5946   /* Pointer to the last but one or last element of the
5947      chained list.  If the list is empty, both first and
5948      last are NULL, if the list contains just one node
5949      or the last node certainly is not redundant, it points
5950      to the last node, otherwise points to the last but one.
5951      Do not mark it for GC because it is marked through the chain.  */
5952   struct var_loc_node * GTY ((skip ("%h"))) last;
5953
5954   /* DECL_UID of the variable decl.  */
5955   unsigned int decl_id;
5956 };
5957 typedef struct var_loc_list_def var_loc_list;
5958
5959
5960 /* Table of decl location linked lists.  */
5961 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5962
5963 /* A pointer to the base of a list of references to DIE's that
5964    are uniquely identified by their tag, presence/absence of
5965    children DIE's, and list of attribute/value pairs.  */
5966 static GTY((length ("abbrev_die_table_allocated")))
5967   dw_die_ref *abbrev_die_table;
5968
5969 /* Number of elements currently allocated for abbrev_die_table.  */
5970 static GTY(()) unsigned abbrev_die_table_allocated;
5971
5972 /* Number of elements in type_die_table currently in use.  */
5973 static GTY(()) unsigned abbrev_die_table_in_use;
5974
5975 /* Size (in elements) of increments by which we may expand the
5976    abbrev_die_table.  */
5977 #define ABBREV_DIE_TABLE_INCREMENT 256
5978
5979 /* A pointer to the base of a table that contains line information
5980    for each source code line in .text in the compilation unit.  */
5981 static GTY((length ("line_info_table_allocated")))
5982      dw_line_info_ref line_info_table;
5983
5984 /* Number of elements currently allocated for line_info_table.  */
5985 static GTY(()) unsigned line_info_table_allocated;
5986
5987 /* Number of elements in line_info_table currently in use.  */
5988 static GTY(()) unsigned line_info_table_in_use;
5989
5990 /* A pointer to the base of a table that contains line information
5991    for each source code line outside of .text in the compilation unit.  */
5992 static GTY ((length ("separate_line_info_table_allocated")))
5993      dw_separate_line_info_ref separate_line_info_table;
5994
5995 /* Number of elements currently allocated for separate_line_info_table.  */
5996 static GTY(()) unsigned separate_line_info_table_allocated;
5997
5998 /* Number of elements in separate_line_info_table currently in use.  */
5999 static GTY(()) unsigned separate_line_info_table_in_use;
6000
6001 /* Size (in elements) of increments by which we may expand the
6002    line_info_table.  */
6003 #define LINE_INFO_TABLE_INCREMENT 1024
6004
6005 /* A flag to tell pubnames/types export if there is an info section to
6006    refer to.  */
6007 static bool info_section_emitted;
6008
6009 /* A pointer to the base of a table that contains a list of publicly
6010    accessible names.  */
6011 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6012
6013 /* A pointer to the base of a table that contains a list of publicly
6014    accessible types.  */
6015 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6016
6017 /* A pointer to the base of a table that contains a list of macro
6018    defines/undefines (and file start/end markers).  */
6019 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6020
6021 /* Array of dies for which we should generate .debug_arange info.  */
6022 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6023
6024 /* Number of elements currently allocated for arange_table.  */
6025 static GTY(()) unsigned arange_table_allocated;
6026
6027 /* Number of elements in arange_table currently in use.  */
6028 static GTY(()) unsigned arange_table_in_use;
6029
6030 /* Size (in elements) of increments by which we may expand the
6031    arange_table.  */
6032 #define ARANGE_TABLE_INCREMENT 64
6033
6034 /* Array of dies for which we should generate .debug_ranges info.  */
6035 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6036
6037 /* Number of elements currently allocated for ranges_table.  */
6038 static GTY(()) unsigned ranges_table_allocated;
6039
6040 /* Number of elements in ranges_table currently in use.  */
6041 static GTY(()) unsigned ranges_table_in_use;
6042
6043 /* Array of pairs of labels referenced in ranges_table.  */
6044 static GTY ((length ("ranges_by_label_allocated")))
6045      dw_ranges_by_label_ref ranges_by_label;
6046
6047 /* Number of elements currently allocated for ranges_by_label.  */
6048 static GTY(()) unsigned ranges_by_label_allocated;
6049
6050 /* Number of elements in ranges_by_label currently in use.  */
6051 static GTY(()) unsigned ranges_by_label_in_use;
6052
6053 /* Size (in elements) of increments by which we may expand the
6054    ranges_table.  */
6055 #define RANGES_TABLE_INCREMENT 64
6056
6057 /* Whether we have location lists that need outputting */
6058 static GTY(()) bool have_location_lists;
6059
6060 /* Unique label counter.  */
6061 static GTY(()) unsigned int loclabel_num;
6062
6063 /* Unique label counter for point-of-call tables.  */
6064 static GTY(()) unsigned int poc_label_num;
6065
6066 /* The direct call table structure.  */
6067
6068 typedef struct GTY(()) dcall_struct {
6069   unsigned int poc_label_num;
6070   tree poc_decl;
6071   dw_die_ref targ_die;
6072 }
6073 dcall_entry;
6074
6075 DEF_VEC_O(dcall_entry);
6076 DEF_VEC_ALLOC_O(dcall_entry, gc);
6077
6078 /* The virtual call table structure.  */
6079
6080 typedef struct GTY(()) vcall_struct {
6081   unsigned int poc_label_num;
6082   unsigned int vtable_slot;
6083 }
6084 vcall_entry;
6085
6086 DEF_VEC_O(vcall_entry);
6087 DEF_VEC_ALLOC_O(vcall_entry, gc);
6088
6089 /* Pointers to the direct and virtual call tables.  */
6090 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6091 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6092
6093 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6094
6095 struct GTY (()) vcall_insn {
6096   int insn_uid;
6097   unsigned int vtable_slot;
6098 };
6099
6100 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6101
6102 /* Record whether the function being analyzed contains inlined functions.  */
6103 static int current_function_has_inlines;
6104
6105 /* The last file entry emitted by maybe_emit_file().  */
6106 static GTY(()) struct dwarf_file_data * last_emitted_file;
6107
6108 /* Number of internal labels generated by gen_internal_sym().  */
6109 static GTY(()) int label_num;
6110
6111 /* Cached result of previous call to lookup_filename.  */
6112 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6113
6114 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6115
6116 /* Offset from the "steady-state frame pointer" to the frame base,
6117    within the current function.  */
6118 static HOST_WIDE_INT frame_pointer_fb_offset;
6119
6120 /* Forward declarations for functions defined in this file.  */
6121
6122 static int is_pseudo_reg (const_rtx);
6123 static tree type_main_variant (tree);
6124 static int is_tagged_type (const_tree);
6125 static const char *dwarf_tag_name (unsigned);
6126 static const char *dwarf_attr_name (unsigned);
6127 static const char *dwarf_form_name (unsigned);
6128 static tree decl_ultimate_origin (const_tree);
6129 static tree decl_class_context (tree);
6130 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6131 static inline enum dw_val_class AT_class (dw_attr_ref);
6132 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6133 static inline unsigned AT_flag (dw_attr_ref);
6134 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6135 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6136 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6137 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6138 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6139                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6140 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6141                                unsigned int, unsigned char *);
6142 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6143 static hashval_t debug_str_do_hash (const void *);
6144 static int debug_str_eq (const void *, const void *);
6145 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6146 static inline const char *AT_string (dw_attr_ref);
6147 static enum dwarf_form AT_string_form (dw_attr_ref);
6148 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6149 static void add_AT_specification (dw_die_ref, dw_die_ref);
6150 static inline dw_die_ref AT_ref (dw_attr_ref);
6151 static inline int AT_ref_external (dw_attr_ref);
6152 static inline void set_AT_ref_external (dw_attr_ref, int);
6153 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6154 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6155 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6156 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6157                              dw_loc_list_ref);
6158 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6159 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6160 static inline rtx AT_addr (dw_attr_ref);
6161 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6162 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6163 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6164 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6165                            unsigned HOST_WIDE_INT);
6166 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6167                                unsigned long);
6168 static inline const char *AT_lbl (dw_attr_ref);
6169 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6170 static const char *get_AT_low_pc (dw_die_ref);
6171 static const char *get_AT_hi_pc (dw_die_ref);
6172 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6173 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6174 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6175 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6176 static bool is_cxx (void);
6177 static bool is_fortran (void);
6178 static bool is_ada (void);
6179 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6180 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6181 static void add_child_die (dw_die_ref, dw_die_ref);
6182 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6183 static dw_die_ref lookup_type_die (tree);
6184 static void equate_type_number_to_die (tree, dw_die_ref);
6185 static hashval_t decl_die_table_hash (const void *);
6186 static int decl_die_table_eq (const void *, const void *);
6187 static dw_die_ref lookup_decl_die (tree);
6188 static hashval_t common_block_die_table_hash (const void *);
6189 static int common_block_die_table_eq (const void *, const void *);
6190 static hashval_t decl_loc_table_hash (const void *);
6191 static int decl_loc_table_eq (const void *, const void *);
6192 static var_loc_list *lookup_decl_loc (const_tree);
6193 static void equate_decl_number_to_die (tree, dw_die_ref);
6194 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6195 static void print_spaces (FILE *);
6196 static void print_die (dw_die_ref, FILE *);
6197 static void print_dwarf_line_table (FILE *);
6198 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6199 static dw_die_ref pop_compile_unit (dw_die_ref);
6200 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6201 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6202 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6203 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6204 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6205 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6206 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6207                                    struct md5_ctx *, int *);
6208 struct checksum_attributes;
6209 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6210 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6211 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6212 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6213 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6214 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6215 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6216 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6217 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6218 static void compute_section_prefix (dw_die_ref);
6219 static int is_type_die (dw_die_ref);
6220 static int is_comdat_die (dw_die_ref);
6221 static int is_symbol_die (dw_die_ref);
6222 static void assign_symbol_names (dw_die_ref);
6223 static void break_out_includes (dw_die_ref);
6224 static int is_declaration_die (dw_die_ref);
6225 static int should_move_die_to_comdat (dw_die_ref);
6226 static dw_die_ref clone_as_declaration (dw_die_ref);
6227 static dw_die_ref clone_die (dw_die_ref);
6228 static dw_die_ref clone_tree (dw_die_ref);
6229 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6230 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6231 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6232 static dw_die_ref generate_skeleton (dw_die_ref);
6233 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6234                                                          dw_die_ref);
6235 static void break_out_comdat_types (dw_die_ref);
6236 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6237 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6238 static void copy_decls_for_unworthy_types (dw_die_ref);
6239
6240 static hashval_t htab_cu_hash (const void *);
6241 static int htab_cu_eq (const void *, const void *);
6242 static void htab_cu_del (void *);
6243 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6244 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6245 static void add_sibling_attributes (dw_die_ref);
6246 static void build_abbrev_table (dw_die_ref);
6247 static void output_location_lists (dw_die_ref);
6248 static int constant_size (unsigned HOST_WIDE_INT);
6249 static unsigned long size_of_die (dw_die_ref);
6250 static void calc_die_sizes (dw_die_ref);
6251 static void mark_dies (dw_die_ref);
6252 static void unmark_dies (dw_die_ref);
6253 static void unmark_all_dies (dw_die_ref);
6254 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6255 static unsigned long size_of_aranges (void);
6256 static enum dwarf_form value_format (dw_attr_ref);
6257 static void output_value_format (dw_attr_ref);
6258 static void output_abbrev_section (void);
6259 static void output_die_symbol (dw_die_ref);
6260 static void output_die (dw_die_ref);
6261 static void output_compilation_unit_header (void);
6262 static void output_comp_unit (dw_die_ref, int);
6263 static void output_comdat_type_unit (comdat_type_node *);
6264 static const char *dwarf2_name (tree, int);
6265 static void add_pubname (tree, dw_die_ref);
6266 static void add_pubname_string (const char *, dw_die_ref);
6267 static void add_pubtype (tree, dw_die_ref);
6268 static void output_pubnames (VEC (pubname_entry,gc) *);
6269 static void add_arange (tree, dw_die_ref);
6270 static void output_aranges (void);
6271 static unsigned int add_ranges_num (int);
6272 static unsigned int add_ranges (const_tree);
6273 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6274                                   bool *);
6275 static void output_ranges (void);
6276 static void output_line_info (void);
6277 static void output_file_names (void);
6278 static dw_die_ref base_type_die (tree);
6279 static int is_base_type (tree);
6280 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6281 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6282 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6283 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6284 static int type_is_enum (const_tree);
6285 static unsigned int dbx_reg_number (const_rtx);
6286 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6287 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6288 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6289                                                 enum var_init_status);
6290 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6291                                                      enum var_init_status);
6292 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6293                                          enum var_init_status);
6294 static int is_based_loc (const_rtx);
6295 static int resolve_one_addr (rtx *, void *);
6296 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6297                                                enum var_init_status);
6298 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6299                                         enum var_init_status);
6300 static dw_loc_list_ref loc_list_from_tree (tree, int);
6301 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6302 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6303 static tree field_type (const_tree);
6304 static unsigned int simple_type_align_in_bits (const_tree);
6305 static unsigned int simple_decl_align_in_bits (const_tree);
6306 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6307 static HOST_WIDE_INT field_byte_offset (const_tree);
6308 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6309                                          dw_loc_list_ref);
6310 static void add_data_member_location_attribute (dw_die_ref, tree);
6311 static bool add_const_value_attribute (dw_die_ref, rtx);
6312 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6313 static void insert_double (double_int, unsigned char *);
6314 static void insert_float (const_rtx, unsigned char *);
6315 static rtx rtl_for_decl_location (tree);
6316 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6317                                                    enum dwarf_attribute);
6318 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6319 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6320 static void add_name_attribute (dw_die_ref, const char *);
6321 static void add_comp_dir_attribute (dw_die_ref);
6322 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6323 static void add_subscript_info (dw_die_ref, tree, bool);
6324 static void add_byte_size_attribute (dw_die_ref, tree);
6325 static void add_bit_offset_attribute (dw_die_ref, tree);
6326 static void add_bit_size_attribute (dw_die_ref, tree);
6327 static void add_prototyped_attribute (dw_die_ref, tree);
6328 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6329 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6330 static void add_src_coords_attributes (dw_die_ref, tree);
6331 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6332 static void push_decl_scope (tree);
6333 static void pop_decl_scope (void);
6334 static dw_die_ref scope_die_for (tree, dw_die_ref);
6335 static inline int local_scope_p (dw_die_ref);
6336 static inline int class_scope_p (dw_die_ref);
6337 static inline int class_or_namespace_scope_p (dw_die_ref);
6338 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6339 static void add_calling_convention_attribute (dw_die_ref, tree);
6340 static const char *type_tag (const_tree);
6341 static tree member_declared_type (const_tree);
6342 #if 0
6343 static const char *decl_start_label (tree);
6344 #endif
6345 static void gen_array_type_die (tree, dw_die_ref);
6346 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6347 #if 0
6348 static void gen_entry_point_die (tree, dw_die_ref);
6349 #endif
6350 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6351 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6352 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6353 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6354 static void gen_formal_types_die (tree, dw_die_ref);
6355 static void gen_subprogram_die (tree, dw_die_ref);
6356 static void gen_variable_die (tree, tree, dw_die_ref);
6357 static void gen_const_die (tree, dw_die_ref);
6358 static void gen_label_die (tree, dw_die_ref);
6359 static void gen_lexical_block_die (tree, dw_die_ref, int);
6360 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6361 static void gen_field_die (tree, dw_die_ref);
6362 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6363 static dw_die_ref gen_compile_unit_die (const char *);
6364 static void gen_inheritance_die (tree, tree, dw_die_ref);
6365 static void gen_member_die (tree, dw_die_ref);
6366 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6367                                                 enum debug_info_usage);
6368 static void gen_subroutine_type_die (tree, dw_die_ref);
6369 static void gen_typedef_die (tree, dw_die_ref);
6370 static void gen_type_die (tree, dw_die_ref);
6371 static void gen_block_die (tree, dw_die_ref, int);
6372 static void decls_for_scope (tree, dw_die_ref, int);
6373 static int is_redundant_typedef (const_tree);
6374 static bool is_naming_typedef_decl (const_tree);
6375 static inline dw_die_ref get_context_die (tree);
6376 static void gen_namespace_die (tree, dw_die_ref);
6377 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6378 static dw_die_ref force_decl_die (tree);
6379 static dw_die_ref force_type_die (tree);
6380 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6381 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6382 static struct dwarf_file_data * lookup_filename (const char *);
6383 static void retry_incomplete_types (void);
6384 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6385 static void gen_generic_params_dies (tree);
6386 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6387 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6388 static void splice_child_die (dw_die_ref, dw_die_ref);
6389 static int file_info_cmp (const void *, const void *);
6390 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6391                                      const char *, const char *);
6392 static void output_loc_list (dw_loc_list_ref);
6393 static char *gen_internal_sym (const char *);
6394
6395 static void prune_unmark_dies (dw_die_ref);
6396 static void prune_unused_types_mark (dw_die_ref, int);
6397 static void prune_unused_types_walk (dw_die_ref);
6398 static void prune_unused_types_walk_attribs (dw_die_ref);
6399 static void prune_unused_types_prune (dw_die_ref);
6400 static void prune_unused_types (void);
6401 static int maybe_emit_file (struct dwarf_file_data *fd);
6402 static inline const char *AT_vms_delta1 (dw_attr_ref);
6403 static inline const char *AT_vms_delta2 (dw_attr_ref);
6404 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6405                                      const char *, const char *);
6406 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6407 static void gen_remaining_tmpl_value_param_die_attribute (void);
6408
6409 /* Section names used to hold DWARF debugging information.  */
6410 #ifndef DEBUG_INFO_SECTION
6411 #define DEBUG_INFO_SECTION      ".debug_info"
6412 #endif
6413 #ifndef DEBUG_ABBREV_SECTION
6414 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6415 #endif
6416 #ifndef DEBUG_ARANGES_SECTION
6417 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6418 #endif
6419 #ifndef DEBUG_MACINFO_SECTION
6420 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6421 #endif
6422 #ifndef DEBUG_LINE_SECTION
6423 #define DEBUG_LINE_SECTION      ".debug_line"
6424 #endif
6425 #ifndef DEBUG_LOC_SECTION
6426 #define DEBUG_LOC_SECTION       ".debug_loc"
6427 #endif
6428 #ifndef DEBUG_PUBNAMES_SECTION
6429 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6430 #endif
6431 #ifndef DEBUG_PUBTYPES_SECTION
6432 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6433 #endif
6434 #ifndef DEBUG_DCALL_SECTION
6435 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6436 #endif
6437 #ifndef DEBUG_VCALL_SECTION
6438 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6439 #endif
6440 #ifndef DEBUG_STR_SECTION
6441 #define DEBUG_STR_SECTION       ".debug_str"
6442 #endif
6443 #ifndef DEBUG_RANGES_SECTION
6444 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6445 #endif
6446
6447 /* Standard ELF section names for compiled code and data.  */
6448 #ifndef TEXT_SECTION_NAME
6449 #define TEXT_SECTION_NAME       ".text"
6450 #endif
6451
6452 /* Section flags for .debug_str section.  */
6453 #define DEBUG_STR_SECTION_FLAGS \
6454   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6455    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6456    : SECTION_DEBUG)
6457
6458 /* Labels we insert at beginning sections we can reference instead of
6459    the section names themselves.  */
6460
6461 #ifndef TEXT_SECTION_LABEL
6462 #define TEXT_SECTION_LABEL              "Ltext"
6463 #endif
6464 #ifndef COLD_TEXT_SECTION_LABEL
6465 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6466 #endif
6467 #ifndef DEBUG_LINE_SECTION_LABEL
6468 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6469 #endif
6470 #ifndef DEBUG_INFO_SECTION_LABEL
6471 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6472 #endif
6473 #ifndef DEBUG_ABBREV_SECTION_LABEL
6474 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6475 #endif
6476 #ifndef DEBUG_LOC_SECTION_LABEL
6477 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6478 #endif
6479 #ifndef DEBUG_RANGES_SECTION_LABEL
6480 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6481 #endif
6482 #ifndef DEBUG_MACINFO_SECTION_LABEL
6483 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6484 #endif
6485
6486
6487 /* Definitions of defaults for formats and names of various special
6488    (artificial) labels which may be generated within this file (when the -g
6489    options is used and DWARF2_DEBUGGING_INFO is in effect.
6490    If necessary, these may be overridden from within the tm.h file, but
6491    typically, overriding these defaults is unnecessary.  */
6492
6493 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6494 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6495 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6496 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6497 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6498 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6499 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6500 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6501 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6502 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6503
6504 #ifndef TEXT_END_LABEL
6505 #define TEXT_END_LABEL          "Letext"
6506 #endif
6507 #ifndef COLD_END_LABEL
6508 #define COLD_END_LABEL          "Letext_cold"
6509 #endif
6510 #ifndef BLOCK_BEGIN_LABEL
6511 #define BLOCK_BEGIN_LABEL       "LBB"
6512 #endif
6513 #ifndef BLOCK_END_LABEL
6514 #define BLOCK_END_LABEL         "LBE"
6515 #endif
6516 #ifndef LINE_CODE_LABEL
6517 #define LINE_CODE_LABEL         "LM"
6518 #endif
6519 #ifndef SEPARATE_LINE_CODE_LABEL
6520 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6521 #endif
6522
6523 \f
6524 /* Return the root of the DIE's built for the current compilation unit.  */
6525 static dw_die_ref
6526 comp_unit_die (void)
6527 {
6528   if (!single_comp_unit_die)
6529     single_comp_unit_die = gen_compile_unit_die (NULL);
6530   return single_comp_unit_die;
6531 }
6532
6533 /* We allow a language front-end to designate a function that is to be
6534    called to "demangle" any name before it is put into a DIE.  */
6535
6536 static const char *(*demangle_name_func) (const char *);
6537
6538 void
6539 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6540 {
6541   demangle_name_func = func;
6542 }
6543
6544 /* Test if rtl node points to a pseudo register.  */
6545
6546 static inline int
6547 is_pseudo_reg (const_rtx rtl)
6548 {
6549   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6550           || (GET_CODE (rtl) == SUBREG
6551               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6552 }
6553
6554 /* Return a reference to a type, with its const and volatile qualifiers
6555    removed.  */
6556
6557 static inline tree
6558 type_main_variant (tree type)
6559 {
6560   type = TYPE_MAIN_VARIANT (type);
6561
6562   /* ??? There really should be only one main variant among any group of
6563      variants of a given type (and all of the MAIN_VARIANT values for all
6564      members of the group should point to that one type) but sometimes the C
6565      front-end messes this up for array types, so we work around that bug
6566      here.  */
6567   if (TREE_CODE (type) == ARRAY_TYPE)
6568     while (type != TYPE_MAIN_VARIANT (type))
6569       type = TYPE_MAIN_VARIANT (type);
6570
6571   return type;
6572 }
6573
6574 /* Return nonzero if the given type node represents a tagged type.  */
6575
6576 static inline int
6577 is_tagged_type (const_tree type)
6578 {
6579   enum tree_code code = TREE_CODE (type);
6580
6581   return (code == RECORD_TYPE || code == UNION_TYPE
6582           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6583 }
6584
6585 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6586
6587 static void
6588 get_ref_die_offset_label (char *label, dw_die_ref ref)
6589 {
6590   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6591 }
6592
6593 /* Convert a DIE tag into its string name.  */
6594
6595 static const char *
6596 dwarf_tag_name (unsigned int tag)
6597 {
6598   switch (tag)
6599     {
6600     case DW_TAG_padding:
6601       return "DW_TAG_padding";
6602     case DW_TAG_array_type:
6603       return "DW_TAG_array_type";
6604     case DW_TAG_class_type:
6605       return "DW_TAG_class_type";
6606     case DW_TAG_entry_point:
6607       return "DW_TAG_entry_point";
6608     case DW_TAG_enumeration_type:
6609       return "DW_TAG_enumeration_type";
6610     case DW_TAG_formal_parameter:
6611       return "DW_TAG_formal_parameter";
6612     case DW_TAG_imported_declaration:
6613       return "DW_TAG_imported_declaration";
6614     case DW_TAG_label:
6615       return "DW_TAG_label";
6616     case DW_TAG_lexical_block:
6617       return "DW_TAG_lexical_block";
6618     case DW_TAG_member:
6619       return "DW_TAG_member";
6620     case DW_TAG_pointer_type:
6621       return "DW_TAG_pointer_type";
6622     case DW_TAG_reference_type:
6623       return "DW_TAG_reference_type";
6624     case DW_TAG_compile_unit:
6625       return "DW_TAG_compile_unit";
6626     case DW_TAG_string_type:
6627       return "DW_TAG_string_type";
6628     case DW_TAG_structure_type:
6629       return "DW_TAG_structure_type";
6630     case DW_TAG_subroutine_type:
6631       return "DW_TAG_subroutine_type";
6632     case DW_TAG_typedef:
6633       return "DW_TAG_typedef";
6634     case DW_TAG_union_type:
6635       return "DW_TAG_union_type";
6636     case DW_TAG_unspecified_parameters:
6637       return "DW_TAG_unspecified_parameters";
6638     case DW_TAG_variant:
6639       return "DW_TAG_variant";
6640     case DW_TAG_common_block:
6641       return "DW_TAG_common_block";
6642     case DW_TAG_common_inclusion:
6643       return "DW_TAG_common_inclusion";
6644     case DW_TAG_inheritance:
6645       return "DW_TAG_inheritance";
6646     case DW_TAG_inlined_subroutine:
6647       return "DW_TAG_inlined_subroutine";
6648     case DW_TAG_module:
6649       return "DW_TAG_module";
6650     case DW_TAG_ptr_to_member_type:
6651       return "DW_TAG_ptr_to_member_type";
6652     case DW_TAG_set_type:
6653       return "DW_TAG_set_type";
6654     case DW_TAG_subrange_type:
6655       return "DW_TAG_subrange_type";
6656     case DW_TAG_with_stmt:
6657       return "DW_TAG_with_stmt";
6658     case DW_TAG_access_declaration:
6659       return "DW_TAG_access_declaration";
6660     case DW_TAG_base_type:
6661       return "DW_TAG_base_type";
6662     case DW_TAG_catch_block:
6663       return "DW_TAG_catch_block";
6664     case DW_TAG_const_type:
6665       return "DW_TAG_const_type";
6666     case DW_TAG_constant:
6667       return "DW_TAG_constant";
6668     case DW_TAG_enumerator:
6669       return "DW_TAG_enumerator";
6670     case DW_TAG_file_type:
6671       return "DW_TAG_file_type";
6672     case DW_TAG_friend:
6673       return "DW_TAG_friend";
6674     case DW_TAG_namelist:
6675       return "DW_TAG_namelist";
6676     case DW_TAG_namelist_item:
6677       return "DW_TAG_namelist_item";
6678     case DW_TAG_packed_type:
6679       return "DW_TAG_packed_type";
6680     case DW_TAG_subprogram:
6681       return "DW_TAG_subprogram";
6682     case DW_TAG_template_type_param:
6683       return "DW_TAG_template_type_param";
6684     case DW_TAG_template_value_param:
6685       return "DW_TAG_template_value_param";
6686     case DW_TAG_thrown_type:
6687       return "DW_TAG_thrown_type";
6688     case DW_TAG_try_block:
6689       return "DW_TAG_try_block";
6690     case DW_TAG_variant_part:
6691       return "DW_TAG_variant_part";
6692     case DW_TAG_variable:
6693       return "DW_TAG_variable";
6694     case DW_TAG_volatile_type:
6695       return "DW_TAG_volatile_type";
6696     case DW_TAG_dwarf_procedure:
6697       return "DW_TAG_dwarf_procedure";
6698     case DW_TAG_restrict_type:
6699       return "DW_TAG_restrict_type";
6700     case DW_TAG_interface_type:
6701       return "DW_TAG_interface_type";
6702     case DW_TAG_namespace:
6703       return "DW_TAG_namespace";
6704     case DW_TAG_imported_module:
6705       return "DW_TAG_imported_module";
6706     case DW_TAG_unspecified_type:
6707       return "DW_TAG_unspecified_type";
6708     case DW_TAG_partial_unit:
6709       return "DW_TAG_partial_unit";
6710     case DW_TAG_imported_unit:
6711       return "DW_TAG_imported_unit";
6712     case DW_TAG_condition:
6713       return "DW_TAG_condition";
6714     case DW_TAG_shared_type:
6715       return "DW_TAG_shared_type";
6716     case DW_TAG_type_unit:
6717       return "DW_TAG_type_unit";
6718     case DW_TAG_rvalue_reference_type:
6719       return "DW_TAG_rvalue_reference_type";
6720     case DW_TAG_template_alias:
6721       return "DW_TAG_template_alias";
6722     case DW_TAG_GNU_template_parameter_pack:
6723       return "DW_TAG_GNU_template_parameter_pack";
6724     case DW_TAG_GNU_formal_parameter_pack:
6725       return "DW_TAG_GNU_formal_parameter_pack";
6726     case DW_TAG_MIPS_loop:
6727       return "DW_TAG_MIPS_loop";
6728     case DW_TAG_format_label:
6729       return "DW_TAG_format_label";
6730     case DW_TAG_function_template:
6731       return "DW_TAG_function_template";
6732     case DW_TAG_class_template:
6733       return "DW_TAG_class_template";
6734     case DW_TAG_GNU_BINCL:
6735       return "DW_TAG_GNU_BINCL";
6736     case DW_TAG_GNU_EINCL:
6737       return "DW_TAG_GNU_EINCL";
6738     case DW_TAG_GNU_template_template_param:
6739       return "DW_TAG_GNU_template_template_param";
6740     default:
6741       return "DW_TAG_<unknown>";
6742     }
6743 }
6744
6745 /* Convert a DWARF attribute code into its string name.  */
6746
6747 static const char *
6748 dwarf_attr_name (unsigned int attr)
6749 {
6750   switch (attr)
6751     {
6752     case DW_AT_sibling:
6753       return "DW_AT_sibling";
6754     case DW_AT_location:
6755       return "DW_AT_location";
6756     case DW_AT_name:
6757       return "DW_AT_name";
6758     case DW_AT_ordering:
6759       return "DW_AT_ordering";
6760     case DW_AT_subscr_data:
6761       return "DW_AT_subscr_data";
6762     case DW_AT_byte_size:
6763       return "DW_AT_byte_size";
6764     case DW_AT_bit_offset:
6765       return "DW_AT_bit_offset";
6766     case DW_AT_bit_size:
6767       return "DW_AT_bit_size";
6768     case DW_AT_element_list:
6769       return "DW_AT_element_list";
6770     case DW_AT_stmt_list:
6771       return "DW_AT_stmt_list";
6772     case DW_AT_low_pc:
6773       return "DW_AT_low_pc";
6774     case DW_AT_high_pc:
6775       return "DW_AT_high_pc";
6776     case DW_AT_language:
6777       return "DW_AT_language";
6778     case DW_AT_member:
6779       return "DW_AT_member";
6780     case DW_AT_discr:
6781       return "DW_AT_discr";
6782     case DW_AT_discr_value:
6783       return "DW_AT_discr_value";
6784     case DW_AT_visibility:
6785       return "DW_AT_visibility";
6786     case DW_AT_import:
6787       return "DW_AT_import";
6788     case DW_AT_string_length:
6789       return "DW_AT_string_length";
6790     case DW_AT_common_reference:
6791       return "DW_AT_common_reference";
6792     case DW_AT_comp_dir:
6793       return "DW_AT_comp_dir";
6794     case DW_AT_const_value:
6795       return "DW_AT_const_value";
6796     case DW_AT_containing_type:
6797       return "DW_AT_containing_type";
6798     case DW_AT_default_value:
6799       return "DW_AT_default_value";
6800     case DW_AT_inline:
6801       return "DW_AT_inline";
6802     case DW_AT_is_optional:
6803       return "DW_AT_is_optional";
6804     case DW_AT_lower_bound:
6805       return "DW_AT_lower_bound";
6806     case DW_AT_producer:
6807       return "DW_AT_producer";
6808     case DW_AT_prototyped:
6809       return "DW_AT_prototyped";
6810     case DW_AT_return_addr:
6811       return "DW_AT_return_addr";
6812     case DW_AT_start_scope:
6813       return "DW_AT_start_scope";
6814     case DW_AT_bit_stride:
6815       return "DW_AT_bit_stride";
6816     case DW_AT_upper_bound:
6817       return "DW_AT_upper_bound";
6818     case DW_AT_abstract_origin:
6819       return "DW_AT_abstract_origin";
6820     case DW_AT_accessibility:
6821       return "DW_AT_accessibility";
6822     case DW_AT_address_class:
6823       return "DW_AT_address_class";
6824     case DW_AT_artificial:
6825       return "DW_AT_artificial";
6826     case DW_AT_base_types:
6827       return "DW_AT_base_types";
6828     case DW_AT_calling_convention:
6829       return "DW_AT_calling_convention";
6830     case DW_AT_count:
6831       return "DW_AT_count";
6832     case DW_AT_data_member_location:
6833       return "DW_AT_data_member_location";
6834     case DW_AT_decl_column:
6835       return "DW_AT_decl_column";
6836     case DW_AT_decl_file:
6837       return "DW_AT_decl_file";
6838     case DW_AT_decl_line:
6839       return "DW_AT_decl_line";
6840     case DW_AT_declaration:
6841       return "DW_AT_declaration";
6842     case DW_AT_discr_list:
6843       return "DW_AT_discr_list";
6844     case DW_AT_encoding:
6845       return "DW_AT_encoding";
6846     case DW_AT_external:
6847       return "DW_AT_external";
6848     case DW_AT_explicit:
6849       return "DW_AT_explicit";
6850     case DW_AT_frame_base:
6851       return "DW_AT_frame_base";
6852     case DW_AT_friend:
6853       return "DW_AT_friend";
6854     case DW_AT_identifier_case:
6855       return "DW_AT_identifier_case";
6856     case DW_AT_macro_info:
6857       return "DW_AT_macro_info";
6858     case DW_AT_namelist_items:
6859       return "DW_AT_namelist_items";
6860     case DW_AT_priority:
6861       return "DW_AT_priority";
6862     case DW_AT_segment:
6863       return "DW_AT_segment";
6864     case DW_AT_specification:
6865       return "DW_AT_specification";
6866     case DW_AT_static_link:
6867       return "DW_AT_static_link";
6868     case DW_AT_type:
6869       return "DW_AT_type";
6870     case DW_AT_use_location:
6871       return "DW_AT_use_location";
6872     case DW_AT_variable_parameter:
6873       return "DW_AT_variable_parameter";
6874     case DW_AT_virtuality:
6875       return "DW_AT_virtuality";
6876     case DW_AT_vtable_elem_location:
6877       return "DW_AT_vtable_elem_location";
6878
6879     case DW_AT_allocated:
6880       return "DW_AT_allocated";
6881     case DW_AT_associated:
6882       return "DW_AT_associated";
6883     case DW_AT_data_location:
6884       return "DW_AT_data_location";
6885     case DW_AT_byte_stride:
6886       return "DW_AT_byte_stride";
6887     case DW_AT_entry_pc:
6888       return "DW_AT_entry_pc";
6889     case DW_AT_use_UTF8:
6890       return "DW_AT_use_UTF8";
6891     case DW_AT_extension:
6892       return "DW_AT_extension";
6893     case DW_AT_ranges:
6894       return "DW_AT_ranges";
6895     case DW_AT_trampoline:
6896       return "DW_AT_trampoline";
6897     case DW_AT_call_column:
6898       return "DW_AT_call_column";
6899     case DW_AT_call_file:
6900       return "DW_AT_call_file";
6901     case DW_AT_call_line:
6902       return "DW_AT_call_line";
6903     case DW_AT_object_pointer:
6904       return "DW_AT_object_pointer";
6905
6906     case DW_AT_signature:
6907       return "DW_AT_signature";
6908     case DW_AT_main_subprogram:
6909       return "DW_AT_main_subprogram";
6910     case DW_AT_data_bit_offset:
6911       return "DW_AT_data_bit_offset";
6912     case DW_AT_const_expr:
6913       return "DW_AT_const_expr";
6914     case DW_AT_enum_class:
6915       return "DW_AT_enum_class";
6916     case DW_AT_linkage_name:
6917       return "DW_AT_linkage_name";
6918
6919     case DW_AT_MIPS_fde:
6920       return "DW_AT_MIPS_fde";
6921     case DW_AT_MIPS_loop_begin:
6922       return "DW_AT_MIPS_loop_begin";
6923     case DW_AT_MIPS_tail_loop_begin:
6924       return "DW_AT_MIPS_tail_loop_begin";
6925     case DW_AT_MIPS_epilog_begin:
6926       return "DW_AT_MIPS_epilog_begin";
6927 #if VMS_DEBUGGING_INFO
6928     case DW_AT_HP_prologue:
6929       return "DW_AT_HP_prologue";
6930 #else
6931     case DW_AT_MIPS_loop_unroll_factor:
6932       return "DW_AT_MIPS_loop_unroll_factor";
6933 #endif
6934     case DW_AT_MIPS_software_pipeline_depth:
6935       return "DW_AT_MIPS_software_pipeline_depth";
6936     case DW_AT_MIPS_linkage_name:
6937       return "DW_AT_MIPS_linkage_name";
6938 #if VMS_DEBUGGING_INFO
6939     case DW_AT_HP_epilogue:
6940       return "DW_AT_HP_epilogue";
6941 #else
6942     case DW_AT_MIPS_stride:
6943       return "DW_AT_MIPS_stride";
6944 #endif
6945     case DW_AT_MIPS_abstract_name:
6946       return "DW_AT_MIPS_abstract_name";
6947     case DW_AT_MIPS_clone_origin:
6948       return "DW_AT_MIPS_clone_origin";
6949     case DW_AT_MIPS_has_inlines:
6950       return "DW_AT_MIPS_has_inlines";
6951
6952     case DW_AT_sf_names:
6953       return "DW_AT_sf_names";
6954     case DW_AT_src_info:
6955       return "DW_AT_src_info";
6956     case DW_AT_mac_info:
6957       return "DW_AT_mac_info";
6958     case DW_AT_src_coords:
6959       return "DW_AT_src_coords";
6960     case DW_AT_body_begin:
6961       return "DW_AT_body_begin";
6962     case DW_AT_body_end:
6963       return "DW_AT_body_end";
6964     case DW_AT_GNU_vector:
6965       return "DW_AT_GNU_vector";
6966     case DW_AT_GNU_guarded_by:
6967       return "DW_AT_GNU_guarded_by";
6968     case DW_AT_GNU_pt_guarded_by:
6969       return "DW_AT_GNU_pt_guarded_by";
6970     case DW_AT_GNU_guarded:
6971       return "DW_AT_GNU_guarded";
6972     case DW_AT_GNU_pt_guarded:
6973       return "DW_AT_GNU_pt_guarded";
6974     case DW_AT_GNU_locks_excluded:
6975       return "DW_AT_GNU_locks_excluded";
6976     case DW_AT_GNU_exclusive_locks_required:
6977       return "DW_AT_GNU_exclusive_locks_required";
6978     case DW_AT_GNU_shared_locks_required:
6979       return "DW_AT_GNU_shared_locks_required";
6980     case DW_AT_GNU_odr_signature:
6981       return "DW_AT_GNU_odr_signature";
6982     case DW_AT_GNU_template_name:
6983       return "DW_AT_GNU_template_name";
6984
6985     case DW_AT_VMS_rtnbeg_pd_address:
6986       return "DW_AT_VMS_rtnbeg_pd_address";
6987
6988     default:
6989       return "DW_AT_<unknown>";
6990     }
6991 }
6992
6993 /* Convert a DWARF value form code into its string name.  */
6994
6995 static const char *
6996 dwarf_form_name (unsigned int form)
6997 {
6998   switch (form)
6999     {
7000     case DW_FORM_addr:
7001       return "DW_FORM_addr";
7002     case DW_FORM_block2:
7003       return "DW_FORM_block2";
7004     case DW_FORM_block4:
7005       return "DW_FORM_block4";
7006     case DW_FORM_data2:
7007       return "DW_FORM_data2";
7008     case DW_FORM_data4:
7009       return "DW_FORM_data4";
7010     case DW_FORM_data8:
7011       return "DW_FORM_data8";
7012     case DW_FORM_string:
7013       return "DW_FORM_string";
7014     case DW_FORM_block:
7015       return "DW_FORM_block";
7016     case DW_FORM_block1:
7017       return "DW_FORM_block1";
7018     case DW_FORM_data1:
7019       return "DW_FORM_data1";
7020     case DW_FORM_flag:
7021       return "DW_FORM_flag";
7022     case DW_FORM_sdata:
7023       return "DW_FORM_sdata";
7024     case DW_FORM_strp:
7025       return "DW_FORM_strp";
7026     case DW_FORM_udata:
7027       return "DW_FORM_udata";
7028     case DW_FORM_ref_addr:
7029       return "DW_FORM_ref_addr";
7030     case DW_FORM_ref1:
7031       return "DW_FORM_ref1";
7032     case DW_FORM_ref2:
7033       return "DW_FORM_ref2";
7034     case DW_FORM_ref4:
7035       return "DW_FORM_ref4";
7036     case DW_FORM_ref8:
7037       return "DW_FORM_ref8";
7038     case DW_FORM_ref_udata:
7039       return "DW_FORM_ref_udata";
7040     case DW_FORM_indirect:
7041       return "DW_FORM_indirect";
7042     case DW_FORM_sec_offset:
7043       return "DW_FORM_sec_offset";
7044     case DW_FORM_exprloc:
7045       return "DW_FORM_exprloc";
7046     case DW_FORM_flag_present:
7047       return "DW_FORM_flag_present";
7048     case DW_FORM_ref_sig8:
7049       return "DW_FORM_ref_sig8";
7050     default:
7051       return "DW_FORM_<unknown>";
7052     }
7053 }
7054 \f
7055 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7056    instance of an inlined instance of a decl which is local to an inline
7057    function, so we have to trace all of the way back through the origin chain
7058    to find out what sort of node actually served as the original seed for the
7059    given block.  */
7060
7061 static tree
7062 decl_ultimate_origin (const_tree decl)
7063 {
7064   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7065     return NULL_TREE;
7066
7067   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7068      nodes in the function to point to themselves; ignore that if
7069      we're trying to output the abstract instance of this function.  */
7070   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7071     return NULL_TREE;
7072
7073   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7074      most distant ancestor, this should never happen.  */
7075   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7076
7077   return DECL_ABSTRACT_ORIGIN (decl);
7078 }
7079
7080 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7081    of a virtual function may refer to a base class, so we check the 'this'
7082    parameter.  */
7083
7084 static tree
7085 decl_class_context (tree decl)
7086 {
7087   tree context = NULL_TREE;
7088
7089   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7090     context = DECL_CONTEXT (decl);
7091   else
7092     context = TYPE_MAIN_VARIANT
7093       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7094
7095   if (context && !TYPE_P (context))
7096     context = NULL_TREE;
7097
7098   return context;
7099 }
7100 \f
7101 /* Add an attribute/value pair to a DIE.  */
7102
7103 static inline void
7104 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7105 {
7106   /* Maybe this should be an assert?  */
7107   if (die == NULL)
7108     return;
7109
7110   if (die->die_attr == NULL)
7111     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7112   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7113 }
7114
7115 static inline enum dw_val_class
7116 AT_class (dw_attr_ref a)
7117 {
7118   return a->dw_attr_val.val_class;
7119 }
7120
7121 /* Add a flag value attribute to a DIE.  */
7122
7123 static inline void
7124 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7125 {
7126   dw_attr_node attr;
7127
7128   attr.dw_attr = attr_kind;
7129   attr.dw_attr_val.val_class = dw_val_class_flag;
7130   attr.dw_attr_val.v.val_flag = flag;
7131   add_dwarf_attr (die, &attr);
7132 }
7133
7134 static inline unsigned
7135 AT_flag (dw_attr_ref a)
7136 {
7137   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7138   return a->dw_attr_val.v.val_flag;
7139 }
7140
7141 /* Add a signed integer attribute value to a DIE.  */
7142
7143 static inline void
7144 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7145 {
7146   dw_attr_node attr;
7147
7148   attr.dw_attr = attr_kind;
7149   attr.dw_attr_val.val_class = dw_val_class_const;
7150   attr.dw_attr_val.v.val_int = int_val;
7151   add_dwarf_attr (die, &attr);
7152 }
7153
7154 static inline HOST_WIDE_INT
7155 AT_int (dw_attr_ref a)
7156 {
7157   gcc_assert (a && AT_class (a) == dw_val_class_const);
7158   return a->dw_attr_val.v.val_int;
7159 }
7160
7161 /* Add an unsigned integer attribute value to a DIE.  */
7162
7163 static inline void
7164 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7165                  unsigned HOST_WIDE_INT unsigned_val)
7166 {
7167   dw_attr_node attr;
7168
7169   attr.dw_attr = attr_kind;
7170   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7171   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7172   add_dwarf_attr (die, &attr);
7173 }
7174
7175 static inline unsigned HOST_WIDE_INT
7176 AT_unsigned (dw_attr_ref a)
7177 {
7178   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7179   return a->dw_attr_val.v.val_unsigned;
7180 }
7181
7182 /* Add an unsigned double integer attribute value to a DIE.  */
7183
7184 static inline void
7185 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7186                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7187 {
7188   dw_attr_node attr;
7189
7190   attr.dw_attr = attr_kind;
7191   attr.dw_attr_val.val_class = dw_val_class_const_double;
7192   attr.dw_attr_val.v.val_double.high = high;
7193   attr.dw_attr_val.v.val_double.low = low;
7194   add_dwarf_attr (die, &attr);
7195 }
7196
7197 /* Add a floating point attribute value to a DIE and return it.  */
7198
7199 static inline void
7200 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7201             unsigned int length, unsigned int elt_size, unsigned char *array)
7202 {
7203   dw_attr_node attr;
7204
7205   attr.dw_attr = attr_kind;
7206   attr.dw_attr_val.val_class = dw_val_class_vec;
7207   attr.dw_attr_val.v.val_vec.length = length;
7208   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7209   attr.dw_attr_val.v.val_vec.array = array;
7210   add_dwarf_attr (die, &attr);
7211 }
7212
7213 /* Add an 8-byte data attribute value to a DIE.  */
7214
7215 static inline void
7216 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7217               unsigned char data8[8])
7218 {
7219   dw_attr_node attr;
7220
7221   attr.dw_attr = attr_kind;
7222   attr.dw_attr_val.val_class = dw_val_class_data8;
7223   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7224   add_dwarf_attr (die, &attr);
7225 }
7226
7227 /* Hash and equality functions for debug_str_hash.  */
7228
7229 static hashval_t
7230 debug_str_do_hash (const void *x)
7231 {
7232   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7233 }
7234
7235 static int
7236 debug_str_eq (const void *x1, const void *x2)
7237 {
7238   return strcmp ((((const struct indirect_string_node *)x1)->str),
7239                  (const char *)x2) == 0;
7240 }
7241
7242 /* Add STR to the indirect string hash table.  */
7243
7244 static struct indirect_string_node *
7245 find_AT_string (const char *str)
7246 {
7247   struct indirect_string_node *node;
7248   void **slot;
7249
7250   if (! debug_str_hash)
7251     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7252                                       debug_str_eq, NULL);
7253
7254   slot = htab_find_slot_with_hash (debug_str_hash, str,
7255                                    htab_hash_string (str), INSERT);
7256   if (*slot == NULL)
7257     {
7258       node = ggc_alloc_cleared_indirect_string_node ();
7259       node->str = ggc_strdup (str);
7260       *slot = node;
7261     }
7262   else
7263     node = (struct indirect_string_node *) *slot;
7264
7265   node->refcount++;
7266   return node;
7267 }
7268
7269 /* Add a string attribute value to a DIE.  */
7270
7271 static inline void
7272 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7273 {
7274   dw_attr_node attr;
7275   struct indirect_string_node *node;
7276
7277   node = find_AT_string (str);
7278
7279   attr.dw_attr = attr_kind;
7280   attr.dw_attr_val.val_class = dw_val_class_str;
7281   attr.dw_attr_val.v.val_str = node;
7282   add_dwarf_attr (die, &attr);
7283 }
7284
7285 /* Create a label for an indirect string node, ensuring it is going to
7286    be output, unless its reference count goes down to zero.  */
7287
7288 static inline void
7289 gen_label_for_indirect_string (struct indirect_string_node *node)
7290 {
7291   char label[32];
7292
7293   if (node->label)
7294     return;
7295
7296   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7297   ++dw2_string_counter;
7298   node->label = xstrdup (label);
7299 }
7300
7301 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7302    debug string STR.  */
7303
7304 static inline rtx
7305 get_debug_string_label (const char *str)
7306 {
7307   struct indirect_string_node *node = find_AT_string (str);
7308
7309   debug_str_hash_forced = true;
7310
7311   gen_label_for_indirect_string (node);
7312
7313   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7314 }
7315
7316 static inline const char *
7317 AT_string (dw_attr_ref a)
7318 {
7319   gcc_assert (a && AT_class (a) == dw_val_class_str);
7320   return a->dw_attr_val.v.val_str->str;
7321 }
7322
7323 /* Find out whether a string should be output inline in DIE
7324    or out-of-line in .debug_str section.  */
7325
7326 static enum dwarf_form
7327 AT_string_form (dw_attr_ref a)
7328 {
7329   struct indirect_string_node *node;
7330   unsigned int len;
7331
7332   gcc_assert (a && AT_class (a) == dw_val_class_str);
7333
7334   node = a->dw_attr_val.v.val_str;
7335   if (node->form)
7336     return node->form;
7337
7338   len = strlen (node->str) + 1;
7339
7340   /* If the string is shorter or equal to the size of the reference, it is
7341      always better to put it inline.  */
7342   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7343     return node->form = DW_FORM_string;
7344
7345   /* If we cannot expect the linker to merge strings in .debug_str
7346      section, only put it into .debug_str if it is worth even in this
7347      single module.  */
7348   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7349       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7350       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7351     return node->form = DW_FORM_string;
7352
7353   gen_label_for_indirect_string (node);
7354
7355   return node->form = DW_FORM_strp;
7356 }
7357
7358 /* Add a DIE reference attribute value to a DIE.  */
7359
7360 static inline void
7361 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7362 {
7363   dw_attr_node attr;
7364
7365 #ifdef ENABLE_CHECKING
7366   gcc_assert (targ_die != NULL);
7367 #else
7368   /* With LTO we can end up trying to reference something we didn't create
7369      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7370   if (targ_die == NULL)
7371     return;
7372 #endif
7373
7374   attr.dw_attr = attr_kind;
7375   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7376   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7377   attr.dw_attr_val.v.val_die_ref.external = 0;
7378   add_dwarf_attr (die, &attr);
7379 }
7380
7381 /* Add an AT_specification attribute to a DIE, and also make the back
7382    pointer from the specification to the definition.  */
7383
7384 static inline void
7385 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7386 {
7387   add_AT_die_ref (die, DW_AT_specification, targ_die);
7388   gcc_assert (!targ_die->die_definition);
7389   targ_die->die_definition = die;
7390 }
7391
7392 static inline dw_die_ref
7393 AT_ref (dw_attr_ref a)
7394 {
7395   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7396   return a->dw_attr_val.v.val_die_ref.die;
7397 }
7398
7399 static inline int
7400 AT_ref_external (dw_attr_ref a)
7401 {
7402   if (a && AT_class (a) == dw_val_class_die_ref)
7403     return a->dw_attr_val.v.val_die_ref.external;
7404
7405   return 0;
7406 }
7407
7408 static inline void
7409 set_AT_ref_external (dw_attr_ref a, int i)
7410 {
7411   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7412   a->dw_attr_val.v.val_die_ref.external = i;
7413 }
7414
7415 /* Add an FDE reference attribute value to a DIE.  */
7416
7417 static inline void
7418 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7419 {
7420   dw_attr_node attr;
7421
7422   attr.dw_attr = attr_kind;
7423   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7424   attr.dw_attr_val.v.val_fde_index = targ_fde;
7425   add_dwarf_attr (die, &attr);
7426 }
7427
7428 /* Add a location description attribute value to a DIE.  */
7429
7430 static inline void
7431 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7432 {
7433   dw_attr_node attr;
7434
7435   attr.dw_attr = attr_kind;
7436   attr.dw_attr_val.val_class = dw_val_class_loc;
7437   attr.dw_attr_val.v.val_loc = loc;
7438   add_dwarf_attr (die, &attr);
7439 }
7440
7441 static inline dw_loc_descr_ref
7442 AT_loc (dw_attr_ref a)
7443 {
7444   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7445   return a->dw_attr_val.v.val_loc;
7446 }
7447
7448 static inline void
7449 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7450 {
7451   dw_attr_node attr;
7452
7453   attr.dw_attr = attr_kind;
7454   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7455   attr.dw_attr_val.v.val_loc_list = loc_list;
7456   add_dwarf_attr (die, &attr);
7457   have_location_lists = true;
7458 }
7459
7460 static inline dw_loc_list_ref
7461 AT_loc_list (dw_attr_ref a)
7462 {
7463   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7464   return a->dw_attr_val.v.val_loc_list;
7465 }
7466
7467 static inline dw_loc_list_ref *
7468 AT_loc_list_ptr (dw_attr_ref a)
7469 {
7470   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7471   return &a->dw_attr_val.v.val_loc_list;
7472 }
7473
7474 /* Add an address constant attribute value to a DIE.  */
7475
7476 static inline void
7477 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7478 {
7479   dw_attr_node attr;
7480
7481   attr.dw_attr = attr_kind;
7482   attr.dw_attr_val.val_class = dw_val_class_addr;
7483   attr.dw_attr_val.v.val_addr = addr;
7484   add_dwarf_attr (die, &attr);
7485 }
7486
7487 /* Get the RTX from to an address DIE attribute.  */
7488
7489 static inline rtx
7490 AT_addr (dw_attr_ref a)
7491 {
7492   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7493   return a->dw_attr_val.v.val_addr;
7494 }
7495
7496 /* Add a file attribute value to a DIE.  */
7497
7498 static inline void
7499 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7500              struct dwarf_file_data *fd)
7501 {
7502   dw_attr_node attr;
7503
7504   attr.dw_attr = attr_kind;
7505   attr.dw_attr_val.val_class = dw_val_class_file;
7506   attr.dw_attr_val.v.val_file = fd;
7507   add_dwarf_attr (die, &attr);
7508 }
7509
7510 /* Get the dwarf_file_data from a file DIE attribute.  */
7511
7512 static inline struct dwarf_file_data *
7513 AT_file (dw_attr_ref a)
7514 {
7515   gcc_assert (a && AT_class (a) == dw_val_class_file);
7516   return a->dw_attr_val.v.val_file;
7517 }
7518
7519 /* Add a vms delta attribute value to a DIE.  */
7520
7521 static inline void
7522 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7523                   const char *lbl1, const char *lbl2)
7524 {
7525   dw_attr_node attr;
7526
7527   attr.dw_attr = attr_kind;
7528   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7529   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7530   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7531   add_dwarf_attr (die, &attr);
7532 }
7533
7534 /* Add a label identifier attribute value to a DIE.  */
7535
7536 static inline void
7537 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7538 {
7539   dw_attr_node attr;
7540
7541   attr.dw_attr = attr_kind;
7542   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7543   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7544   add_dwarf_attr (die, &attr);
7545 }
7546
7547 /* Add a section offset attribute value to a DIE, an offset into the
7548    debug_line section.  */
7549
7550 static inline void
7551 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7552                 const char *label)
7553 {
7554   dw_attr_node attr;
7555
7556   attr.dw_attr = attr_kind;
7557   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7558   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7559   add_dwarf_attr (die, &attr);
7560 }
7561
7562 /* Add a section offset attribute value to a DIE, an offset into the
7563    debug_macinfo section.  */
7564
7565 static inline void
7566 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7567                const char *label)
7568 {
7569   dw_attr_node attr;
7570
7571   attr.dw_attr = attr_kind;
7572   attr.dw_attr_val.val_class = dw_val_class_macptr;
7573   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7574   add_dwarf_attr (die, &attr);
7575 }
7576
7577 /* Add an offset attribute value to a DIE.  */
7578
7579 static inline void
7580 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7581                unsigned HOST_WIDE_INT offset)
7582 {
7583   dw_attr_node attr;
7584
7585   attr.dw_attr = attr_kind;
7586   attr.dw_attr_val.val_class = dw_val_class_offset;
7587   attr.dw_attr_val.v.val_offset = offset;
7588   add_dwarf_attr (die, &attr);
7589 }
7590
7591 /* Add an range_list attribute value to a DIE.  */
7592
7593 static void
7594 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7595                    long unsigned int offset)
7596 {
7597   dw_attr_node attr;
7598
7599   attr.dw_attr = attr_kind;
7600   attr.dw_attr_val.val_class = dw_val_class_range_list;
7601   attr.dw_attr_val.v.val_offset = offset;
7602   add_dwarf_attr (die, &attr);
7603 }
7604
7605 /* Return the start label of a delta attribute.  */
7606
7607 static inline const char *
7608 AT_vms_delta1 (dw_attr_ref a)
7609 {
7610   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7611   return a->dw_attr_val.v.val_vms_delta.lbl1;
7612 }
7613
7614 /* Return the end label of a delta attribute.  */
7615
7616 static inline const char *
7617 AT_vms_delta2 (dw_attr_ref a)
7618 {
7619   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7620   return a->dw_attr_val.v.val_vms_delta.lbl2;
7621 }
7622
7623 static inline const char *
7624 AT_lbl (dw_attr_ref a)
7625 {
7626   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7627                     || AT_class (a) == dw_val_class_lineptr
7628                     || AT_class (a) == dw_val_class_macptr));
7629   return a->dw_attr_val.v.val_lbl_id;
7630 }
7631
7632 /* Get the attribute of type attr_kind.  */
7633
7634 static dw_attr_ref
7635 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7636 {
7637   dw_attr_ref a;
7638   unsigned ix;
7639   dw_die_ref spec = NULL;
7640
7641   if (! die)
7642     return NULL;
7643
7644   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7645     if (a->dw_attr == attr_kind)
7646       return a;
7647     else if (a->dw_attr == DW_AT_specification
7648              || a->dw_attr == DW_AT_abstract_origin)
7649       spec = AT_ref (a);
7650
7651   if (spec)
7652     return get_AT (spec, attr_kind);
7653
7654   return NULL;
7655 }
7656
7657 /* Return the "low pc" attribute value, typically associated with a subprogram
7658    DIE.  Return null if the "low pc" attribute is either not present, or if it
7659    cannot be represented as an assembler label identifier.  */
7660
7661 static inline const char *
7662 get_AT_low_pc (dw_die_ref die)
7663 {
7664   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7665
7666   return a ? AT_lbl (a) : NULL;
7667 }
7668
7669 /* Return the "high pc" attribute value, typically associated with a subprogram
7670    DIE.  Return null if the "high pc" attribute is either not present, or if it
7671    cannot be represented as an assembler label identifier.  */
7672
7673 static inline const char *
7674 get_AT_hi_pc (dw_die_ref die)
7675 {
7676   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7677
7678   return a ? AT_lbl (a) : NULL;
7679 }
7680
7681 /* Return the value of the string attribute designated by ATTR_KIND, or
7682    NULL if it is not present.  */
7683
7684 static inline const char *
7685 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7686 {
7687   dw_attr_ref a = get_AT (die, attr_kind);
7688
7689   return a ? AT_string (a) : NULL;
7690 }
7691
7692 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7693    if it is not present.  */
7694
7695 static inline int
7696 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7697 {
7698   dw_attr_ref a = get_AT (die, attr_kind);
7699
7700   return a ? AT_flag (a) : 0;
7701 }
7702
7703 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7704    if it is not present.  */
7705
7706 static inline unsigned
7707 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7708 {
7709   dw_attr_ref a = get_AT (die, attr_kind);
7710
7711   return a ? AT_unsigned (a) : 0;
7712 }
7713
7714 static inline dw_die_ref
7715 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7716 {
7717   dw_attr_ref a = get_AT (die, attr_kind);
7718
7719   return a ? AT_ref (a) : NULL;
7720 }
7721
7722 static inline struct dwarf_file_data *
7723 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7724 {
7725   dw_attr_ref a = get_AT (die, attr_kind);
7726
7727   return a ? AT_file (a) : NULL;
7728 }
7729
7730 /* Return TRUE if the language is C++.  */
7731
7732 static inline bool
7733 is_cxx (void)
7734 {
7735   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7736
7737   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7738 }
7739
7740 /* Return TRUE if the language is Fortran.  */
7741
7742 static inline bool
7743 is_fortran (void)
7744 {
7745   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7746
7747   return (lang == DW_LANG_Fortran77
7748           || lang == DW_LANG_Fortran90
7749           || lang == DW_LANG_Fortran95);
7750 }
7751
7752 /* Return TRUE if the language is Ada.  */
7753
7754 static inline bool
7755 is_ada (void)
7756 {
7757   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7758
7759   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7760 }
7761
7762 /* Remove the specified attribute if present.  */
7763
7764 static void
7765 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7766 {
7767   dw_attr_ref a;
7768   unsigned ix;
7769
7770   if (! die)
7771     return;
7772
7773   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7774     if (a->dw_attr == attr_kind)
7775       {
7776         if (AT_class (a) == dw_val_class_str)
7777           if (a->dw_attr_val.v.val_str->refcount)
7778             a->dw_attr_val.v.val_str->refcount--;
7779
7780         /* VEC_ordered_remove should help reduce the number of abbrevs
7781            that are needed.  */
7782         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7783         return;
7784       }
7785 }
7786
7787 /* Remove CHILD from its parent.  PREV must have the property that
7788    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7789
7790 static void
7791 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7792 {
7793   gcc_assert (child->die_parent == prev->die_parent);
7794   gcc_assert (prev->die_sib == child);
7795   if (prev == child)
7796     {
7797       gcc_assert (child->die_parent->die_child == child);
7798       prev = NULL;
7799     }
7800   else
7801     prev->die_sib = child->die_sib;
7802   if (child->die_parent->die_child == child)
7803     child->die_parent->die_child = prev;
7804 }
7805
7806 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7807    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7808
7809 static void
7810 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7811 {
7812   dw_die_ref parent = old_child->die_parent;
7813
7814   gcc_assert (parent == prev->die_parent);
7815   gcc_assert (prev->die_sib == old_child);
7816
7817   new_child->die_parent = parent;
7818   if (prev == old_child)
7819     {
7820       gcc_assert (parent->die_child == old_child);
7821       new_child->die_sib = new_child;
7822     }
7823   else
7824     {
7825       prev->die_sib = new_child;
7826       new_child->die_sib = old_child->die_sib;
7827     }
7828   if (old_child->die_parent->die_child == old_child)
7829     old_child->die_parent->die_child = new_child;
7830 }
7831
7832 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7833
7834 static void
7835 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7836 {
7837   dw_die_ref c;
7838   new_parent->die_child = old_parent->die_child;
7839   old_parent->die_child = NULL;
7840   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7841 }
7842
7843 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7844    matches TAG.  */
7845
7846 static void
7847 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7848 {
7849   dw_die_ref c;
7850
7851   c = die->die_child;
7852   if (c) do {
7853     dw_die_ref prev = c;
7854     c = c->die_sib;
7855     while (c->die_tag == tag)
7856       {
7857         remove_child_with_prev (c, prev);
7858         /* Might have removed every child.  */
7859         if (c == c->die_sib)
7860           return;
7861         c = c->die_sib;
7862       }
7863   } while (c != die->die_child);
7864 }
7865
7866 /* Add a CHILD_DIE as the last child of DIE.  */
7867
7868 static void
7869 add_child_die (dw_die_ref die, dw_die_ref child_die)
7870 {
7871   /* FIXME this should probably be an assert.  */
7872   if (! die || ! child_die)
7873     return;
7874   gcc_assert (die != child_die);
7875
7876   child_die->die_parent = die;
7877   if (die->die_child)
7878     {
7879       child_die->die_sib = die->die_child->die_sib;
7880       die->die_child->die_sib = child_die;
7881     }
7882   else
7883     child_die->die_sib = child_die;
7884   die->die_child = child_die;
7885 }
7886
7887 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7888    is the specification, to the end of PARENT's list of children.
7889    This is done by removing and re-adding it.  */
7890
7891 static void
7892 splice_child_die (dw_die_ref parent, dw_die_ref child)
7893 {
7894   dw_die_ref p;
7895
7896   /* We want the declaration DIE from inside the class, not the
7897      specification DIE at toplevel.  */
7898   if (child->die_parent != parent)
7899     {
7900       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7901
7902       if (tmp)
7903         child = tmp;
7904     }
7905
7906   gcc_assert (child->die_parent == parent
7907               || (child->die_parent
7908                   == get_AT_ref (parent, DW_AT_specification)));
7909
7910   for (p = child->die_parent->die_child; ; p = p->die_sib)
7911     if (p->die_sib == child)
7912       {
7913         remove_child_with_prev (child, p);
7914         break;
7915       }
7916
7917   add_child_die (parent, child);
7918 }
7919
7920 /* Return a pointer to a newly created DIE node.  */
7921
7922 static inline dw_die_ref
7923 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7924 {
7925   dw_die_ref die = ggc_alloc_cleared_die_node ();
7926
7927   die->die_tag = tag_value;
7928
7929   if (parent_die != NULL)
7930     add_child_die (parent_die, die);
7931   else
7932     {
7933       limbo_die_node *limbo_node;
7934
7935       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7936       limbo_node->die = die;
7937       limbo_node->created_for = t;
7938       limbo_node->next = limbo_die_list;
7939       limbo_die_list = limbo_node;
7940     }
7941
7942   return die;
7943 }
7944
7945 /* Return the DIE associated with the given type specifier.  */
7946
7947 static inline dw_die_ref
7948 lookup_type_die (tree type)
7949 {
7950   return TYPE_SYMTAB_DIE (type);
7951 }
7952
7953 /* Equate a DIE to a given type specifier.  */
7954
7955 static inline void
7956 equate_type_number_to_die (tree type, dw_die_ref type_die)
7957 {
7958   TYPE_SYMTAB_DIE (type) = type_die;
7959 }
7960
7961 /* Returns a hash value for X (which really is a die_struct).  */
7962
7963 static hashval_t
7964 decl_die_table_hash (const void *x)
7965 {
7966   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7967 }
7968
7969 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7970
7971 static int
7972 decl_die_table_eq (const void *x, const void *y)
7973 {
7974   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7975 }
7976
7977 /* Return the DIE associated with a given declaration.  */
7978
7979 static inline dw_die_ref
7980 lookup_decl_die (tree decl)
7981 {
7982   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7983 }
7984
7985 /* Returns a hash value for X (which really is a var_loc_list).  */
7986
7987 static hashval_t
7988 decl_loc_table_hash (const void *x)
7989 {
7990   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7991 }
7992
7993 /* Return nonzero if decl_id of var_loc_list X is the same as
7994    UID of decl *Y.  */
7995
7996 static int
7997 decl_loc_table_eq (const void *x, const void *y)
7998 {
7999   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8000 }
8001
8002 /* Return the var_loc list associated with a given declaration.  */
8003
8004 static inline var_loc_list *
8005 lookup_decl_loc (const_tree decl)
8006 {
8007   if (!decl_loc_table)
8008     return NULL;
8009   return (var_loc_list *)
8010     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8011 }
8012
8013 /* Equate a DIE to a particular declaration.  */
8014
8015 static void
8016 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8017 {
8018   unsigned int decl_id = DECL_UID (decl);
8019   void **slot;
8020
8021   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8022   *slot = decl_die;
8023   decl_die->decl_id = decl_id;
8024 }
8025
8026 /* Return how many bits covers PIECE EXPR_LIST.  */
8027
8028 static int
8029 decl_piece_bitsize (rtx piece)
8030 {
8031   int ret = (int) GET_MODE (piece);
8032   if (ret)
8033     return ret;
8034   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8035               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8036   return INTVAL (XEXP (XEXP (piece, 0), 0));
8037 }
8038
8039 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8040
8041 static rtx *
8042 decl_piece_varloc_ptr (rtx piece)
8043 {
8044   if ((int) GET_MODE (piece))
8045     return &XEXP (piece, 0);
8046   else
8047     return &XEXP (XEXP (piece, 0), 1);
8048 }
8049
8050 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8051    Next is the chain of following piece nodes.  */
8052
8053 static rtx
8054 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8055 {
8056   if (bitsize <= (int) MAX_MACHINE_MODE)
8057     return alloc_EXPR_LIST (bitsize, loc_note, next);
8058   else
8059     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8060                                                GEN_INT (bitsize),
8061                                                loc_note), next);
8062 }
8063
8064 /* Return rtx that should be stored into loc field for
8065    LOC_NOTE and BITPOS/BITSIZE.  */
8066
8067 static rtx
8068 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8069                       HOST_WIDE_INT bitsize)
8070 {
8071   if (bitsize != -1)
8072     {
8073       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8074       if (bitpos != 0)
8075         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8076     }
8077   return loc_note;
8078 }
8079
8080 /* This function either modifies location piece list *DEST in
8081    place (if SRC and INNER is NULL), or copies location piece list
8082    *SRC to *DEST while modifying it.  Location BITPOS is modified
8083    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8084    not copied and if needed some padding around it is added.
8085    When modifying in place, DEST should point to EXPR_LIST where
8086    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8087    to the start of the whole list and INNER points to the EXPR_LIST
8088    where earlier pieces cover PIECE_BITPOS bits.  */
8089
8090 static void
8091 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8092                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8093                    HOST_WIDE_INT bitsize, rtx loc_note)
8094 {
8095   int diff;
8096   bool copy = inner != NULL;
8097
8098   if (copy)
8099     {
8100       /* First copy all nodes preceeding the current bitpos.  */
8101       while (src != inner)
8102         {
8103           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8104                                    decl_piece_bitsize (*src), NULL_RTX);
8105           dest = &XEXP (*dest, 1);
8106           src = &XEXP (*src, 1);
8107         }
8108     }
8109   /* Add padding if needed.  */
8110   if (bitpos != piece_bitpos)
8111     {
8112       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8113                                copy ? NULL_RTX : *dest);
8114       dest = &XEXP (*dest, 1);
8115     }
8116   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8117     {
8118       gcc_assert (!copy);
8119       /* A piece with correct bitpos and bitsize already exist,
8120          just update the location for it and return.  */
8121       *decl_piece_varloc_ptr (*dest) = loc_note;
8122       return;
8123     }
8124   /* Add the piece that changed.  */
8125   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8126   dest = &XEXP (*dest, 1);
8127   /* Skip over pieces that overlap it.  */
8128   diff = bitpos - piece_bitpos + bitsize;
8129   if (!copy)
8130     src = dest;
8131   while (diff > 0 && *src)
8132     {
8133       rtx piece = *src;
8134       diff -= decl_piece_bitsize (piece);
8135       if (copy)
8136         src = &XEXP (piece, 1);
8137       else
8138         {
8139           *src = XEXP (piece, 1);
8140           free_EXPR_LIST_node (piece);
8141         }
8142     }
8143   /* Add padding if needed.  */
8144   if (diff < 0 && *src)
8145     {
8146       if (!copy)
8147         dest = src;
8148       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8149       dest = &XEXP (*dest, 1);
8150     }
8151   if (!copy)
8152     return;
8153   /* Finally copy all nodes following it.  */
8154   while (*src)
8155     {
8156       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8157                                decl_piece_bitsize (*src), NULL_RTX);
8158       dest = &XEXP (*dest, 1);
8159       src = &XEXP (*src, 1);
8160     }
8161 }
8162
8163 /* Add a variable location node to the linked list for DECL.  */
8164
8165 static struct var_loc_node *
8166 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8167 {
8168   unsigned int decl_id;
8169   var_loc_list *temp;
8170   void **slot;
8171   struct var_loc_node *loc = NULL;
8172   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8173
8174   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8175     {
8176       tree realdecl = DECL_DEBUG_EXPR (decl);
8177       if (realdecl && handled_component_p (realdecl))
8178         {
8179           HOST_WIDE_INT maxsize;
8180           tree innerdecl;
8181           innerdecl
8182             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8183           if (!DECL_P (innerdecl)
8184               || DECL_IGNORED_P (innerdecl)
8185               || TREE_STATIC (innerdecl)
8186               || bitsize <= 0
8187               || bitpos + bitsize > 256
8188               || bitsize != maxsize)
8189             return NULL;
8190           decl = innerdecl;
8191         }
8192     }
8193
8194   decl_id = DECL_UID (decl);
8195   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8196   if (*slot == NULL)
8197     {
8198       temp = ggc_alloc_cleared_var_loc_list ();
8199       temp->decl_id = decl_id;
8200       *slot = temp;
8201     }
8202   else
8203     temp = (var_loc_list *) *slot;
8204
8205   if (temp->last)
8206     {
8207       struct var_loc_node *last = temp->last, *unused = NULL;
8208       rtx *piece_loc = NULL, last_loc_note;
8209       int piece_bitpos = 0;
8210       if (last->next)
8211         {
8212           last = last->next;
8213           gcc_assert (last->next == NULL);
8214         }
8215       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8216         {
8217           piece_loc = &last->loc;
8218           do
8219             {
8220               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8221               if (piece_bitpos + cur_bitsize > bitpos)
8222                 break;
8223               piece_bitpos += cur_bitsize;
8224               piece_loc = &XEXP (*piece_loc, 1);
8225             }
8226           while (*piece_loc);
8227         }
8228       /* TEMP->LAST here is either pointer to the last but one or
8229          last element in the chained list, LAST is pointer to the
8230          last element.  */
8231       if (label && strcmp (last->label, label) == 0)
8232         {
8233           /* For SRA optimized variables if there weren't any real
8234              insns since last note, just modify the last node.  */
8235           if (piece_loc != NULL)
8236             {
8237               adjust_piece_list (piece_loc, NULL, NULL,
8238                                  bitpos, piece_bitpos, bitsize, loc_note);
8239               return NULL;
8240             }
8241           /* If the last note doesn't cover any instructions, remove it.  */
8242           if (temp->last != last)
8243             {
8244               temp->last->next = NULL;
8245               unused = last;
8246               last = temp->last;
8247               gcc_assert (strcmp (last->label, label) != 0);
8248             }
8249           else
8250             {
8251               gcc_assert (temp->first == temp->last);
8252               memset (temp->last, '\0', sizeof (*temp->last));
8253               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8254               return temp->last;
8255             }
8256         }
8257       if (bitsize == -1 && NOTE_P (last->loc))
8258         last_loc_note = last->loc;
8259       else if (piece_loc != NULL
8260                && *piece_loc != NULL_RTX
8261                && piece_bitpos == bitpos
8262                && decl_piece_bitsize (*piece_loc) == bitsize)
8263         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8264       else
8265         last_loc_note = NULL_RTX;
8266       /* If the current location is the same as the end of the list,
8267          and either both or neither of the locations is uninitialized,
8268          we have nothing to do.  */
8269       if (last_loc_note == NULL_RTX
8270           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8271                             NOTE_VAR_LOCATION_LOC (loc_note)))
8272           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8273                != NOTE_VAR_LOCATION_STATUS (loc_note))
8274               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8275                    == VAR_INIT_STATUS_UNINITIALIZED)
8276                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8277                       == VAR_INIT_STATUS_UNINITIALIZED))))
8278         {
8279           /* Add LOC to the end of list and update LAST.  If the last
8280              element of the list has been removed above, reuse its
8281              memory for the new node, otherwise allocate a new one.  */
8282           if (unused)
8283             {
8284               loc = unused;
8285               memset (loc, '\0', sizeof (*loc));
8286             }
8287           else
8288             loc = ggc_alloc_cleared_var_loc_node ();
8289           if (bitsize == -1 || piece_loc == NULL)
8290             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8291           else
8292             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8293                                bitpos, piece_bitpos, bitsize, loc_note);
8294           last->next = loc;
8295           /* Ensure TEMP->LAST will point either to the new last but one
8296              element of the chain, or to the last element in it.  */
8297           if (last != temp->last)
8298             temp->last = last;
8299         }
8300       else if (unused)
8301         ggc_free (unused);
8302     }
8303   else
8304     {
8305       loc = ggc_alloc_cleared_var_loc_node ();
8306       temp->first = loc;
8307       temp->last = loc;
8308       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8309     }
8310   return loc;
8311 }
8312 \f
8313 /* Keep track of the number of spaces used to indent the
8314    output of the debugging routines that print the structure of
8315    the DIE internal representation.  */
8316 static int print_indent;
8317
8318 /* Indent the line the number of spaces given by print_indent.  */
8319
8320 static inline void
8321 print_spaces (FILE *outfile)
8322 {
8323   fprintf (outfile, "%*s", print_indent, "");
8324 }
8325
8326 /* Print a type signature in hex.  */
8327
8328 static inline void
8329 print_signature (FILE *outfile, char *sig)
8330 {
8331   int i;
8332
8333   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8334     fprintf (outfile, "%02x", sig[i] & 0xff);
8335 }
8336
8337 /* Print the information associated with a given DIE, and its children.
8338    This routine is a debugging aid only.  */
8339
8340 static void
8341 print_die (dw_die_ref die, FILE *outfile)
8342 {
8343   dw_attr_ref a;
8344   dw_die_ref c;
8345   unsigned ix;
8346
8347   print_spaces (outfile);
8348   fprintf (outfile, "DIE %4ld: %s\n",
8349            die->die_offset, dwarf_tag_name (die->die_tag));
8350   print_spaces (outfile);
8351   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8352   fprintf (outfile, " offset: %ld\n", die->die_offset);
8353   if (dwarf_version >= 4 && die->die_id.die_type_node)
8354     {
8355       print_spaces (outfile);
8356       fprintf (outfile, "  signature: ");
8357       print_signature (outfile, die->die_id.die_type_node->signature);
8358       fprintf (outfile, "\n");
8359     }
8360
8361   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8362     {
8363       print_spaces (outfile);
8364       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8365
8366       switch (AT_class (a))
8367         {
8368         case dw_val_class_addr:
8369           fprintf (outfile, "address");
8370           break;
8371         case dw_val_class_offset:
8372           fprintf (outfile, "offset");
8373           break;
8374         case dw_val_class_loc:
8375           fprintf (outfile, "location descriptor");
8376           break;
8377         case dw_val_class_loc_list:
8378           fprintf (outfile, "location list -> label:%s",
8379                    AT_loc_list (a)->ll_symbol);
8380           break;
8381         case dw_val_class_range_list:
8382           fprintf (outfile, "range list");
8383           break;
8384         case dw_val_class_const:
8385           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8386           break;
8387         case dw_val_class_unsigned_const:
8388           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8389           break;
8390         case dw_val_class_const_double:
8391           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8392                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8393                    a->dw_attr_val.v.val_double.high,
8394                    a->dw_attr_val.v.val_double.low);
8395           break;
8396         case dw_val_class_vec:
8397           fprintf (outfile, "floating-point or vector constant");
8398           break;
8399         case dw_val_class_flag:
8400           fprintf (outfile, "%u", AT_flag (a));
8401           break;
8402         case dw_val_class_die_ref:
8403           if (AT_ref (a) != NULL)
8404             {
8405               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8406                 {
8407                   fprintf (outfile, "die -> signature: ");
8408                   print_signature (outfile,
8409                                    AT_ref (a)->die_id.die_type_node->signature);
8410                 }
8411               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8412                 fprintf (outfile, "die -> label: %s",
8413                          AT_ref (a)->die_id.die_symbol);
8414               else
8415                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8416             }
8417           else
8418             fprintf (outfile, "die -> <null>");
8419           break;
8420         case dw_val_class_vms_delta:
8421           fprintf (outfile, "delta: @slotcount(%s-%s)",
8422                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8423           break;
8424         case dw_val_class_lbl_id:
8425         case dw_val_class_lineptr:
8426         case dw_val_class_macptr:
8427           fprintf (outfile, "label: %s", AT_lbl (a));
8428           break;
8429         case dw_val_class_str:
8430           if (AT_string (a) != NULL)
8431             fprintf (outfile, "\"%s\"", AT_string (a));
8432           else
8433             fprintf (outfile, "<null>");
8434           break;
8435         case dw_val_class_file:
8436           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8437                    AT_file (a)->emitted_number);
8438           break;
8439         case dw_val_class_data8:
8440           {
8441             int i;
8442
8443             for (i = 0; i < 8; i++)
8444               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8445             break;
8446           }
8447         default:
8448           break;
8449         }
8450
8451       fprintf (outfile, "\n");
8452     }
8453
8454   if (die->die_child != NULL)
8455     {
8456       print_indent += 4;
8457       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8458       print_indent -= 4;
8459     }
8460   if (print_indent == 0)
8461     fprintf (outfile, "\n");
8462 }
8463
8464 /* Print the contents of the source code line number correspondence table.
8465    This routine is a debugging aid only.  */
8466
8467 static void
8468 print_dwarf_line_table (FILE *outfile)
8469 {
8470   unsigned i;
8471   dw_line_info_ref line_info;
8472
8473   fprintf (outfile, "\n\nDWARF source line information\n");
8474   for (i = 1; i < line_info_table_in_use; i++)
8475     {
8476       line_info = &line_info_table[i];
8477       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8478                line_info->dw_file_num,
8479                line_info->dw_line_num);
8480     }
8481
8482   fprintf (outfile, "\n\n");
8483 }
8484
8485 /* Print the information collected for a given DIE.  */
8486
8487 DEBUG_FUNCTION void
8488 debug_dwarf_die (dw_die_ref die)
8489 {
8490   print_die (die, stderr);
8491 }
8492
8493 /* Print all DWARF information collected for the compilation unit.
8494    This routine is a debugging aid only.  */
8495
8496 DEBUG_FUNCTION void
8497 debug_dwarf (void)
8498 {
8499   print_indent = 0;
8500   print_die (comp_unit_die (), stderr);
8501   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8502     print_dwarf_line_table (stderr);
8503 }
8504 \f
8505 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8506    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8507    DIE that marks the start of the DIEs for this include file.  */
8508
8509 static dw_die_ref
8510 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8511 {
8512   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8513   dw_die_ref new_unit = gen_compile_unit_die (filename);
8514
8515   new_unit->die_sib = old_unit;
8516   return new_unit;
8517 }
8518
8519 /* Close an include-file CU and reopen the enclosing one.  */
8520
8521 static dw_die_ref
8522 pop_compile_unit (dw_die_ref old_unit)
8523 {
8524   dw_die_ref new_unit = old_unit->die_sib;
8525
8526   old_unit->die_sib = NULL;
8527   return new_unit;
8528 }
8529
8530 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8531 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8532
8533 /* Calculate the checksum of a location expression.  */
8534
8535 static inline void
8536 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8537 {
8538   int tem;
8539
8540   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8541   CHECKSUM (tem);
8542   CHECKSUM (loc->dw_loc_oprnd1);
8543   CHECKSUM (loc->dw_loc_oprnd2);
8544 }
8545
8546 /* Calculate the checksum of an attribute.  */
8547
8548 static void
8549 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8550 {
8551   dw_loc_descr_ref loc;
8552   rtx r;
8553
8554   CHECKSUM (at->dw_attr);
8555
8556   /* We don't care that this was compiled with a different compiler
8557      snapshot; if the output is the same, that's what matters.  */
8558   if (at->dw_attr == DW_AT_producer)
8559     return;
8560
8561   switch (AT_class (at))
8562     {
8563     case dw_val_class_const:
8564       CHECKSUM (at->dw_attr_val.v.val_int);
8565       break;
8566     case dw_val_class_unsigned_const:
8567       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8568       break;
8569     case dw_val_class_const_double:
8570       CHECKSUM (at->dw_attr_val.v.val_double);
8571       break;
8572     case dw_val_class_vec:
8573       CHECKSUM (at->dw_attr_val.v.val_vec);
8574       break;
8575     case dw_val_class_flag:
8576       CHECKSUM (at->dw_attr_val.v.val_flag);
8577       break;
8578     case dw_val_class_str:
8579       CHECKSUM_STRING (AT_string (at));
8580       break;
8581
8582     case dw_val_class_addr:
8583       r = AT_addr (at);
8584       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8585       CHECKSUM_STRING (XSTR (r, 0));
8586       break;
8587
8588     case dw_val_class_offset:
8589       CHECKSUM (at->dw_attr_val.v.val_offset);
8590       break;
8591
8592     case dw_val_class_loc:
8593       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8594         loc_checksum (loc, ctx);
8595       break;
8596
8597     case dw_val_class_die_ref:
8598       die_checksum (AT_ref (at), ctx, mark);
8599       break;
8600
8601     case dw_val_class_fde_ref:
8602     case dw_val_class_vms_delta:
8603     case dw_val_class_lbl_id:
8604     case dw_val_class_lineptr:
8605     case dw_val_class_macptr:
8606       break;
8607
8608     case dw_val_class_file:
8609       CHECKSUM_STRING (AT_file (at)->filename);
8610       break;
8611
8612     case dw_val_class_data8:
8613       CHECKSUM (at->dw_attr_val.v.val_data8);
8614       break;
8615
8616     default:
8617       break;
8618     }
8619 }
8620
8621 /* Calculate the checksum of a DIE.  */
8622
8623 static void
8624 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8625 {
8626   dw_die_ref c;
8627   dw_attr_ref a;
8628   unsigned ix;
8629
8630   /* To avoid infinite recursion.  */
8631   if (die->die_mark)
8632     {
8633       CHECKSUM (die->die_mark);
8634       return;
8635     }
8636   die->die_mark = ++(*mark);
8637
8638   CHECKSUM (die->die_tag);
8639
8640   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8641     attr_checksum (a, ctx, mark);
8642
8643   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8644 }
8645
8646 #undef CHECKSUM
8647 #undef CHECKSUM_STRING
8648
8649 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8650 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8651 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8652 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8653 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8654 #define CHECKSUM_ATTR(FOO) \
8655   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8656
8657 /* Calculate the checksum of a number in signed LEB128 format.  */
8658
8659 static void
8660 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8661 {
8662   unsigned char byte;
8663   bool more;
8664
8665   while (1)
8666     {
8667       byte = (value & 0x7f);
8668       value >>= 7;
8669       more = !((value == 0 && (byte & 0x40) == 0)
8670                 || (value == -1 && (byte & 0x40) != 0));
8671       if (more)
8672         byte |= 0x80;
8673       CHECKSUM (byte);
8674       if (!more)
8675         break;
8676     }
8677 }
8678
8679 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8680
8681 static void
8682 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8683 {
8684   while (1)
8685     {
8686       unsigned char byte = (value & 0x7f);
8687       value >>= 7;
8688       if (value != 0)
8689         /* More bytes to follow.  */
8690         byte |= 0x80;
8691       CHECKSUM (byte);
8692       if (value == 0)
8693         break;
8694     }
8695 }
8696
8697 /* Checksum the context of the DIE.  This adds the names of any
8698    surrounding namespaces or structures to the checksum.  */
8699
8700 static void
8701 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8702 {
8703   const char *name;
8704   dw_die_ref spec;
8705   int tag = die->die_tag;
8706
8707   if (tag != DW_TAG_namespace
8708       && tag != DW_TAG_structure_type
8709       && tag != DW_TAG_class_type)
8710     return;
8711
8712   name = get_AT_string (die, DW_AT_name);
8713
8714   spec = get_AT_ref (die, DW_AT_specification);
8715   if (spec != NULL)
8716     die = spec;
8717
8718   if (die->die_parent != NULL)
8719     checksum_die_context (die->die_parent, ctx);
8720
8721   CHECKSUM_ULEB128 ('C');
8722   CHECKSUM_ULEB128 (tag);
8723   if (name != NULL)
8724     CHECKSUM_STRING (name);
8725 }
8726
8727 /* Calculate the checksum of a location expression.  */
8728
8729 static inline void
8730 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8731 {
8732   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8733      were emitted as a DW_FORM_sdata instead of a location expression.  */
8734   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8735     {
8736       CHECKSUM_ULEB128 (DW_FORM_sdata);
8737       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8738       return;
8739     }
8740
8741   /* Otherwise, just checksum the raw location expression.  */
8742   while (loc != NULL)
8743     {
8744       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8745       CHECKSUM (loc->dw_loc_oprnd1);
8746       CHECKSUM (loc->dw_loc_oprnd2);
8747       loc = loc->dw_loc_next;
8748     }
8749 }
8750
8751 /* Calculate the checksum of an attribute.  */
8752
8753 static void
8754 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8755                        struct md5_ctx *ctx, int *mark)
8756 {
8757   dw_loc_descr_ref loc;
8758   rtx r;
8759
8760   if (AT_class (at) == dw_val_class_die_ref)
8761     {
8762       dw_die_ref target_die = AT_ref (at);
8763
8764       /* For pointer and reference types, we checksum only the (qualified)
8765          name of the target type (if there is a name).  For friend entries,
8766          we checksum only the (qualified) name of the target type or function.
8767          This allows the checksum to remain the same whether the target type
8768          is complete or not.  */
8769       if ((at->dw_attr == DW_AT_type
8770            && (tag == DW_TAG_pointer_type
8771                || tag == DW_TAG_reference_type
8772                || tag == DW_TAG_rvalue_reference_type
8773                || tag == DW_TAG_ptr_to_member_type))
8774           || (at->dw_attr == DW_AT_friend
8775               && tag == DW_TAG_friend))
8776         {
8777           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8778
8779           if (name_attr != NULL)
8780             {
8781               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8782
8783               if (decl == NULL)
8784                 decl = target_die;
8785               CHECKSUM_ULEB128 ('N');
8786               CHECKSUM_ULEB128 (at->dw_attr);
8787               if (decl->die_parent != NULL)
8788                 checksum_die_context (decl->die_parent, ctx);
8789               CHECKSUM_ULEB128 ('E');
8790               CHECKSUM_STRING (AT_string (name_attr));
8791               return;
8792             }
8793         }
8794
8795       /* For all other references to another DIE, we check to see if the
8796          target DIE has already been visited.  If it has, we emit a
8797          backward reference; if not, we descend recursively.  */
8798       if (target_die->die_mark > 0)
8799         {
8800           CHECKSUM_ULEB128 ('R');
8801           CHECKSUM_ULEB128 (at->dw_attr);
8802           CHECKSUM_ULEB128 (target_die->die_mark);
8803         }
8804       else
8805         {
8806           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8807
8808           if (decl == NULL)
8809             decl = target_die;
8810           target_die->die_mark = ++(*mark);
8811           CHECKSUM_ULEB128 ('T');
8812           CHECKSUM_ULEB128 (at->dw_attr);
8813           if (decl->die_parent != NULL)
8814             checksum_die_context (decl->die_parent, ctx);
8815           die_checksum_ordered (target_die, ctx, mark);
8816         }
8817       return;
8818     }
8819
8820   CHECKSUM_ULEB128 ('A');
8821   CHECKSUM_ULEB128 (at->dw_attr);
8822
8823   switch (AT_class (at))
8824     {
8825     case dw_val_class_const:
8826       CHECKSUM_ULEB128 (DW_FORM_sdata);
8827       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8828       break;
8829
8830     case dw_val_class_unsigned_const:
8831       CHECKSUM_ULEB128 (DW_FORM_sdata);
8832       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8833       break;
8834
8835     case dw_val_class_const_double:
8836       CHECKSUM_ULEB128 (DW_FORM_block);
8837       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8838       CHECKSUM (at->dw_attr_val.v.val_double);
8839       break;
8840
8841     case dw_val_class_vec:
8842       CHECKSUM_ULEB128 (DW_FORM_block);
8843       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8844       CHECKSUM (at->dw_attr_val.v.val_vec);
8845       break;
8846
8847     case dw_val_class_flag:
8848       CHECKSUM_ULEB128 (DW_FORM_flag);
8849       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8850       break;
8851
8852     case dw_val_class_str:
8853       CHECKSUM_ULEB128 (DW_FORM_string);
8854       CHECKSUM_STRING (AT_string (at));
8855       break;
8856
8857     case dw_val_class_addr:
8858       r = AT_addr (at);
8859       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8860       CHECKSUM_ULEB128 (DW_FORM_string);
8861       CHECKSUM_STRING (XSTR (r, 0));
8862       break;
8863
8864     case dw_val_class_offset:
8865       CHECKSUM_ULEB128 (DW_FORM_sdata);
8866       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8867       break;
8868
8869     case dw_val_class_loc:
8870       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8871         loc_checksum_ordered (loc, ctx);
8872       break;
8873
8874     case dw_val_class_fde_ref:
8875     case dw_val_class_lbl_id:
8876     case dw_val_class_lineptr:
8877     case dw_val_class_macptr:
8878       break;
8879
8880     case dw_val_class_file:
8881       CHECKSUM_ULEB128 (DW_FORM_string);
8882       CHECKSUM_STRING (AT_file (at)->filename);
8883       break;
8884
8885     case dw_val_class_data8:
8886       CHECKSUM (at->dw_attr_val.v.val_data8);
8887       break;
8888
8889     default:
8890       break;
8891     }
8892 }
8893
8894 struct checksum_attributes
8895 {
8896   dw_attr_ref at_name;
8897   dw_attr_ref at_type;
8898   dw_attr_ref at_friend;
8899   dw_attr_ref at_accessibility;
8900   dw_attr_ref at_address_class;
8901   dw_attr_ref at_allocated;
8902   dw_attr_ref at_artificial;
8903   dw_attr_ref at_associated;
8904   dw_attr_ref at_binary_scale;
8905   dw_attr_ref at_bit_offset;
8906   dw_attr_ref at_bit_size;
8907   dw_attr_ref at_bit_stride;
8908   dw_attr_ref at_byte_size;
8909   dw_attr_ref at_byte_stride;
8910   dw_attr_ref at_const_value;
8911   dw_attr_ref at_containing_type;
8912   dw_attr_ref at_count;
8913   dw_attr_ref at_data_location;
8914   dw_attr_ref at_data_member_location;
8915   dw_attr_ref at_decimal_scale;
8916   dw_attr_ref at_decimal_sign;
8917   dw_attr_ref at_default_value;
8918   dw_attr_ref at_digit_count;
8919   dw_attr_ref at_discr;
8920   dw_attr_ref at_discr_list;
8921   dw_attr_ref at_discr_value;
8922   dw_attr_ref at_encoding;
8923   dw_attr_ref at_endianity;
8924   dw_attr_ref at_explicit;
8925   dw_attr_ref at_is_optional;
8926   dw_attr_ref at_location;
8927   dw_attr_ref at_lower_bound;
8928   dw_attr_ref at_mutable;
8929   dw_attr_ref at_ordering;
8930   dw_attr_ref at_picture_string;
8931   dw_attr_ref at_prototyped;
8932   dw_attr_ref at_small;
8933   dw_attr_ref at_segment;
8934   dw_attr_ref at_string_length;
8935   dw_attr_ref at_threads_scaled;
8936   dw_attr_ref at_upper_bound;
8937   dw_attr_ref at_use_location;
8938   dw_attr_ref at_use_UTF8;
8939   dw_attr_ref at_variable_parameter;
8940   dw_attr_ref at_virtuality;
8941   dw_attr_ref at_visibility;
8942   dw_attr_ref at_vtable_elem_location;
8943 };
8944
8945 /* Collect the attributes that we will want to use for the checksum.  */
8946
8947 static void
8948 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8949 {
8950   dw_attr_ref a;
8951   unsigned ix;
8952
8953   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8954     {
8955       switch (a->dw_attr)
8956         {
8957         case DW_AT_name:
8958           attrs->at_name = a;
8959           break;
8960         case DW_AT_type:
8961           attrs->at_type = a;
8962           break;
8963         case DW_AT_friend:
8964           attrs->at_friend = a;
8965           break;
8966         case DW_AT_accessibility:
8967           attrs->at_accessibility = a;
8968           break;
8969         case DW_AT_address_class:
8970           attrs->at_address_class = a;
8971           break;
8972         case DW_AT_allocated:
8973           attrs->at_allocated = a;
8974           break;
8975         case DW_AT_artificial:
8976           attrs->at_artificial = a;
8977           break;
8978         case DW_AT_associated:
8979           attrs->at_associated = a;
8980           break;
8981         case DW_AT_binary_scale:
8982           attrs->at_binary_scale = a;
8983           break;
8984         case DW_AT_bit_offset:
8985           attrs->at_bit_offset = a;
8986           break;
8987         case DW_AT_bit_size:
8988           attrs->at_bit_size = a;
8989           break;
8990         case DW_AT_bit_stride:
8991           attrs->at_bit_stride = a;
8992           break;
8993         case DW_AT_byte_size:
8994           attrs->at_byte_size = a;
8995           break;
8996         case DW_AT_byte_stride:
8997           attrs->at_byte_stride = a;
8998           break;
8999         case DW_AT_const_value:
9000           attrs->at_const_value = a;
9001           break;
9002         case DW_AT_containing_type:
9003           attrs->at_containing_type = a;
9004           break;
9005         case DW_AT_count:
9006           attrs->at_count = a;
9007           break;
9008         case DW_AT_data_location:
9009           attrs->at_data_location = a;
9010           break;
9011         case DW_AT_data_member_location:
9012           attrs->at_data_member_location = a;
9013           break;
9014         case DW_AT_decimal_scale:
9015           attrs->at_decimal_scale = a;
9016           break;
9017         case DW_AT_decimal_sign:
9018           attrs->at_decimal_sign = a;
9019           break;
9020         case DW_AT_default_value:
9021           attrs->at_default_value = a;
9022           break;
9023         case DW_AT_digit_count:
9024           attrs->at_digit_count = a;
9025           break;
9026         case DW_AT_discr:
9027           attrs->at_discr = a;
9028           break;
9029         case DW_AT_discr_list:
9030           attrs->at_discr_list = a;
9031           break;
9032         case DW_AT_discr_value:
9033           attrs->at_discr_value = a;
9034           break;
9035         case DW_AT_encoding:
9036           attrs->at_encoding = a;
9037           break;
9038         case DW_AT_endianity:
9039           attrs->at_endianity = a;
9040           break;
9041         case DW_AT_explicit:
9042           attrs->at_explicit = a;
9043           break;
9044         case DW_AT_is_optional:
9045           attrs->at_is_optional = a;
9046           break;
9047         case DW_AT_location:
9048           attrs->at_location = a;
9049           break;
9050         case DW_AT_lower_bound:
9051           attrs->at_lower_bound = a;
9052           break;
9053         case DW_AT_mutable:
9054           attrs->at_mutable = a;
9055           break;
9056         case DW_AT_ordering:
9057           attrs->at_ordering = a;
9058           break;
9059         case DW_AT_picture_string:
9060           attrs->at_picture_string = a;
9061           break;
9062         case DW_AT_prototyped:
9063           attrs->at_prototyped = a;
9064           break;
9065         case DW_AT_small:
9066           attrs->at_small = a;
9067           break;
9068         case DW_AT_segment:
9069           attrs->at_segment = a;
9070           break;
9071         case DW_AT_string_length:
9072           attrs->at_string_length = a;
9073           break;
9074         case DW_AT_threads_scaled:
9075           attrs->at_threads_scaled = a;
9076           break;
9077         case DW_AT_upper_bound:
9078           attrs->at_upper_bound = a;
9079           break;
9080         case DW_AT_use_location:
9081           attrs->at_use_location = a;
9082           break;
9083         case DW_AT_use_UTF8:
9084           attrs->at_use_UTF8 = a;
9085           break;
9086         case DW_AT_variable_parameter:
9087           attrs->at_variable_parameter = a;
9088           break;
9089         case DW_AT_virtuality:
9090           attrs->at_virtuality = a;
9091           break;
9092         case DW_AT_visibility:
9093           attrs->at_visibility = a;
9094           break;
9095         case DW_AT_vtable_elem_location:
9096           attrs->at_vtable_elem_location = a;
9097           break;
9098         default:
9099           break;
9100         }
9101     }
9102 }
9103
9104 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9105
9106 static void
9107 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9108 {
9109   dw_die_ref c;
9110   dw_die_ref decl;
9111   struct checksum_attributes attrs;
9112
9113   CHECKSUM_ULEB128 ('D');
9114   CHECKSUM_ULEB128 (die->die_tag);
9115
9116   memset (&attrs, 0, sizeof (attrs));
9117
9118   decl = get_AT_ref (die, DW_AT_specification);
9119   if (decl != NULL)
9120     collect_checksum_attributes (&attrs, decl);
9121   collect_checksum_attributes (&attrs, die);
9122
9123   CHECKSUM_ATTR (attrs.at_name);
9124   CHECKSUM_ATTR (attrs.at_accessibility);
9125   CHECKSUM_ATTR (attrs.at_address_class);
9126   CHECKSUM_ATTR (attrs.at_allocated);
9127   CHECKSUM_ATTR (attrs.at_artificial);
9128   CHECKSUM_ATTR (attrs.at_associated);
9129   CHECKSUM_ATTR (attrs.at_binary_scale);
9130   CHECKSUM_ATTR (attrs.at_bit_offset);
9131   CHECKSUM_ATTR (attrs.at_bit_size);
9132   CHECKSUM_ATTR (attrs.at_bit_stride);
9133   CHECKSUM_ATTR (attrs.at_byte_size);
9134   CHECKSUM_ATTR (attrs.at_byte_stride);
9135   CHECKSUM_ATTR (attrs.at_const_value);
9136   CHECKSUM_ATTR (attrs.at_containing_type);
9137   CHECKSUM_ATTR (attrs.at_count);
9138   CHECKSUM_ATTR (attrs.at_data_location);
9139   CHECKSUM_ATTR (attrs.at_data_member_location);
9140   CHECKSUM_ATTR (attrs.at_decimal_scale);
9141   CHECKSUM_ATTR (attrs.at_decimal_sign);
9142   CHECKSUM_ATTR (attrs.at_default_value);
9143   CHECKSUM_ATTR (attrs.at_digit_count);
9144   CHECKSUM_ATTR (attrs.at_discr);
9145   CHECKSUM_ATTR (attrs.at_discr_list);
9146   CHECKSUM_ATTR (attrs.at_discr_value);
9147   CHECKSUM_ATTR (attrs.at_encoding);
9148   CHECKSUM_ATTR (attrs.at_endianity);
9149   CHECKSUM_ATTR (attrs.at_explicit);
9150   CHECKSUM_ATTR (attrs.at_is_optional);
9151   CHECKSUM_ATTR (attrs.at_location);
9152   CHECKSUM_ATTR (attrs.at_lower_bound);
9153   CHECKSUM_ATTR (attrs.at_mutable);
9154   CHECKSUM_ATTR (attrs.at_ordering);
9155   CHECKSUM_ATTR (attrs.at_picture_string);
9156   CHECKSUM_ATTR (attrs.at_prototyped);
9157   CHECKSUM_ATTR (attrs.at_small);
9158   CHECKSUM_ATTR (attrs.at_segment);
9159   CHECKSUM_ATTR (attrs.at_string_length);
9160   CHECKSUM_ATTR (attrs.at_threads_scaled);
9161   CHECKSUM_ATTR (attrs.at_upper_bound);
9162   CHECKSUM_ATTR (attrs.at_use_location);
9163   CHECKSUM_ATTR (attrs.at_use_UTF8);
9164   CHECKSUM_ATTR (attrs.at_variable_parameter);
9165   CHECKSUM_ATTR (attrs.at_virtuality);
9166   CHECKSUM_ATTR (attrs.at_visibility);
9167   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9168   CHECKSUM_ATTR (attrs.at_type);
9169   CHECKSUM_ATTR (attrs.at_friend);
9170
9171   /* Checksum the child DIEs, except for nested types and member functions.  */
9172   c = die->die_child;
9173   if (c) do {
9174     dw_attr_ref name_attr;
9175
9176     c = c->die_sib;
9177     name_attr = get_AT (c, DW_AT_name);
9178     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9179         && name_attr != NULL)
9180       {
9181         CHECKSUM_ULEB128 ('S');
9182         CHECKSUM_ULEB128 (c->die_tag);
9183         CHECKSUM_STRING (AT_string (name_attr));
9184       }
9185     else
9186       {
9187         /* Mark this DIE so it gets processed when unmarking.  */
9188         if (c->die_mark == 0)
9189           c->die_mark = -1;
9190         die_checksum_ordered (c, ctx, mark);
9191       }
9192   } while (c != die->die_child);
9193
9194   CHECKSUM_ULEB128 (0);
9195 }
9196
9197 #undef CHECKSUM
9198 #undef CHECKSUM_STRING
9199 #undef CHECKSUM_ATTR
9200 #undef CHECKSUM_LEB128
9201 #undef CHECKSUM_ULEB128
9202
9203 /* Generate the type signature for DIE.  This is computed by generating an
9204    MD5 checksum over the DIE's tag, its relevant attributes, and its
9205    children.  Attributes that are references to other DIEs are processed
9206    by recursion, using the MARK field to prevent infinite recursion.
9207    If the DIE is nested inside a namespace or another type, we also
9208    need to include that context in the signature.  The lower 64 bits
9209    of the resulting MD5 checksum comprise the signature.  */
9210
9211 static void
9212 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9213 {
9214   int mark;
9215   const char *name;
9216   unsigned char checksum[16];
9217   struct md5_ctx ctx;
9218   dw_die_ref decl;
9219
9220   name = get_AT_string (die, DW_AT_name);
9221   decl = get_AT_ref (die, DW_AT_specification);
9222
9223   /* First, compute a signature for just the type name (and its surrounding
9224      context, if any.  This is stored in the type unit DIE for link-time
9225      ODR (one-definition rule) checking.  */
9226
9227   if (is_cxx() && name != NULL)
9228     {
9229       md5_init_ctx (&ctx);
9230
9231       /* Checksum the names of surrounding namespaces and structures.  */
9232       if (decl != NULL && decl->die_parent != NULL)
9233         checksum_die_context (decl->die_parent, &ctx);
9234
9235       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9236       md5_process_bytes (name, strlen (name) + 1, &ctx);
9237       md5_finish_ctx (&ctx, checksum);
9238
9239       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9240     }
9241
9242   /* Next, compute the complete type signature.  */
9243
9244   md5_init_ctx (&ctx);
9245   mark = 1;
9246   die->die_mark = mark;
9247
9248   /* Checksum the names of surrounding namespaces and structures.  */
9249   if (decl != NULL && decl->die_parent != NULL)
9250     checksum_die_context (decl->die_parent, &ctx);
9251
9252   /* Checksum the DIE and its children.  */
9253   die_checksum_ordered (die, &ctx, &mark);
9254   unmark_all_dies (die);
9255   md5_finish_ctx (&ctx, checksum);
9256
9257   /* Store the signature in the type node and link the type DIE and the
9258      type node together.  */
9259   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9260           DWARF_TYPE_SIGNATURE_SIZE);
9261   die->die_id.die_type_node = type_node;
9262   type_node->type_die = die;
9263
9264   /* If the DIE is a specification, link its declaration to the type node
9265      as well.  */
9266   if (decl != NULL)
9267     decl->die_id.die_type_node = type_node;
9268 }
9269
9270 /* Do the location expressions look same?  */
9271 static inline int
9272 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9273 {
9274   return loc1->dw_loc_opc == loc2->dw_loc_opc
9275          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9276          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9277 }
9278
9279 /* Do the values look the same?  */
9280 static int
9281 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9282 {
9283   dw_loc_descr_ref loc1, loc2;
9284   rtx r1, r2;
9285
9286   if (v1->val_class != v2->val_class)
9287     return 0;
9288
9289   switch (v1->val_class)
9290     {
9291     case dw_val_class_const:
9292       return v1->v.val_int == v2->v.val_int;
9293     case dw_val_class_unsigned_const:
9294       return v1->v.val_unsigned == v2->v.val_unsigned;
9295     case dw_val_class_const_double:
9296       return v1->v.val_double.high == v2->v.val_double.high
9297              && v1->v.val_double.low == v2->v.val_double.low;
9298     case dw_val_class_vec:
9299       if (v1->v.val_vec.length != v2->v.val_vec.length
9300           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9301         return 0;
9302       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9303                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9304         return 0;
9305       return 1;
9306     case dw_val_class_flag:
9307       return v1->v.val_flag == v2->v.val_flag;
9308     case dw_val_class_str:
9309       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9310
9311     case dw_val_class_addr:
9312       r1 = v1->v.val_addr;
9313       r2 = v2->v.val_addr;
9314       if (GET_CODE (r1) != GET_CODE (r2))
9315         return 0;
9316       return !rtx_equal_p (r1, r2);
9317
9318     case dw_val_class_offset:
9319       return v1->v.val_offset == v2->v.val_offset;
9320
9321     case dw_val_class_loc:
9322       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9323            loc1 && loc2;
9324            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9325         if (!same_loc_p (loc1, loc2, mark))
9326           return 0;
9327       return !loc1 && !loc2;
9328
9329     case dw_val_class_die_ref:
9330       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9331
9332     case dw_val_class_fde_ref:
9333     case dw_val_class_vms_delta:
9334     case dw_val_class_lbl_id:
9335     case dw_val_class_lineptr:
9336     case dw_val_class_macptr:
9337       return 1;
9338
9339     case dw_val_class_file:
9340       return v1->v.val_file == v2->v.val_file;
9341
9342     case dw_val_class_data8:
9343       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9344
9345     default:
9346       return 1;
9347     }
9348 }
9349
9350 /* Do the attributes look the same?  */
9351
9352 static int
9353 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9354 {
9355   if (at1->dw_attr != at2->dw_attr)
9356     return 0;
9357
9358   /* We don't care that this was compiled with a different compiler
9359      snapshot; if the output is the same, that's what matters. */
9360   if (at1->dw_attr == DW_AT_producer)
9361     return 1;
9362
9363   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9364 }
9365
9366 /* Do the dies look the same?  */
9367
9368 static int
9369 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9370 {
9371   dw_die_ref c1, c2;
9372   dw_attr_ref a1;
9373   unsigned ix;
9374
9375   /* To avoid infinite recursion.  */
9376   if (die1->die_mark)
9377     return die1->die_mark == die2->die_mark;
9378   die1->die_mark = die2->die_mark = ++(*mark);
9379
9380   if (die1->die_tag != die2->die_tag)
9381     return 0;
9382
9383   if (VEC_length (dw_attr_node, die1->die_attr)
9384       != VEC_length (dw_attr_node, die2->die_attr))
9385     return 0;
9386
9387   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9388     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9389       return 0;
9390
9391   c1 = die1->die_child;
9392   c2 = die2->die_child;
9393   if (! c1)
9394     {
9395       if (c2)
9396         return 0;
9397     }
9398   else
9399     for (;;)
9400       {
9401         if (!same_die_p (c1, c2, mark))
9402           return 0;
9403         c1 = c1->die_sib;
9404         c2 = c2->die_sib;
9405         if (c1 == die1->die_child)
9406           {
9407             if (c2 == die2->die_child)
9408               break;
9409             else
9410               return 0;
9411           }
9412     }
9413
9414   return 1;
9415 }
9416
9417 /* Do the dies look the same?  Wrapper around same_die_p.  */
9418
9419 static int
9420 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9421 {
9422   int mark = 0;
9423   int ret = same_die_p (die1, die2, &mark);
9424
9425   unmark_all_dies (die1);
9426   unmark_all_dies (die2);
9427
9428   return ret;
9429 }
9430
9431 /* The prefix to attach to symbols on DIEs in the current comdat debug
9432    info section.  */
9433 static char *comdat_symbol_id;
9434
9435 /* The index of the current symbol within the current comdat CU.  */
9436 static unsigned int comdat_symbol_number;
9437
9438 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9439    children, and set comdat_symbol_id accordingly.  */
9440
9441 static void
9442 compute_section_prefix (dw_die_ref unit_die)
9443 {
9444   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9445   const char *base = die_name ? lbasename (die_name) : "anonymous";
9446   char *name = XALLOCAVEC (char, strlen (base) + 64);
9447   char *p;
9448   int i, mark;
9449   unsigned char checksum[16];
9450   struct md5_ctx ctx;
9451
9452   /* Compute the checksum of the DIE, then append part of it as hex digits to
9453      the name filename of the unit.  */
9454
9455   md5_init_ctx (&ctx);
9456   mark = 0;
9457   die_checksum (unit_die, &ctx, &mark);
9458   unmark_all_dies (unit_die);
9459   md5_finish_ctx (&ctx, checksum);
9460
9461   sprintf (name, "%s.", base);
9462   clean_symbol_name (name);
9463
9464   p = name + strlen (name);
9465   for (i = 0; i < 4; i++)
9466     {
9467       sprintf (p, "%.2x", checksum[i]);
9468       p += 2;
9469     }
9470
9471   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9472   comdat_symbol_number = 0;
9473 }
9474
9475 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9476
9477 static int
9478 is_type_die (dw_die_ref die)
9479 {
9480   switch (die->die_tag)
9481     {
9482     case DW_TAG_array_type:
9483     case DW_TAG_class_type:
9484     case DW_TAG_interface_type:
9485     case DW_TAG_enumeration_type:
9486     case DW_TAG_pointer_type:
9487     case DW_TAG_reference_type:
9488     case DW_TAG_rvalue_reference_type:
9489     case DW_TAG_string_type:
9490     case DW_TAG_structure_type:
9491     case DW_TAG_subroutine_type:
9492     case DW_TAG_union_type:
9493     case DW_TAG_ptr_to_member_type:
9494     case DW_TAG_set_type:
9495     case DW_TAG_subrange_type:
9496     case DW_TAG_base_type:
9497     case DW_TAG_const_type:
9498     case DW_TAG_file_type:
9499     case DW_TAG_packed_type:
9500     case DW_TAG_volatile_type:
9501     case DW_TAG_typedef:
9502       return 1;
9503     default:
9504       return 0;
9505     }
9506 }
9507
9508 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9509    Basically, we want to choose the bits that are likely to be shared between
9510    compilations (types) and leave out the bits that are specific to individual
9511    compilations (functions).  */
9512
9513 static int
9514 is_comdat_die (dw_die_ref c)
9515 {
9516   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9517      we do for stabs.  The advantage is a greater likelihood of sharing between
9518      objects that don't include headers in the same order (and therefore would
9519      put the base types in a different comdat).  jason 8/28/00 */
9520
9521   if (c->die_tag == DW_TAG_base_type)
9522     return 0;
9523
9524   if (c->die_tag == DW_TAG_pointer_type
9525       || c->die_tag == DW_TAG_reference_type
9526       || c->die_tag == DW_TAG_rvalue_reference_type
9527       || c->die_tag == DW_TAG_const_type
9528       || c->die_tag == DW_TAG_volatile_type)
9529     {
9530       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9531
9532       return t ? is_comdat_die (t) : 0;
9533     }
9534
9535   return is_type_die (c);
9536 }
9537
9538 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9539    compilation unit.  */
9540
9541 static int
9542 is_symbol_die (dw_die_ref c)
9543 {
9544   return (is_type_die (c)
9545           || is_declaration_die (c)
9546           || c->die_tag == DW_TAG_namespace
9547           || c->die_tag == DW_TAG_module);
9548 }
9549
9550 /* Returns true iff C is a compile-unit DIE.  */
9551
9552 static inline bool
9553 is_cu_die (dw_die_ref c)
9554 {
9555   return c && c->die_tag == DW_TAG_compile_unit;
9556 }
9557
9558 static char *
9559 gen_internal_sym (const char *prefix)
9560 {
9561   char buf[256];
9562
9563   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9564   return xstrdup (buf);
9565 }
9566
9567 /* Assign symbols to all worthy DIEs under DIE.  */
9568
9569 static void
9570 assign_symbol_names (dw_die_ref die)
9571 {
9572   dw_die_ref c;
9573
9574   if (is_symbol_die (die))
9575     {
9576       if (comdat_symbol_id)
9577         {
9578           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9579
9580           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9581                    comdat_symbol_id, comdat_symbol_number++);
9582           die->die_id.die_symbol = xstrdup (p);
9583         }
9584       else
9585         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9586     }
9587
9588   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9589 }
9590
9591 struct cu_hash_table_entry
9592 {
9593   dw_die_ref cu;
9594   unsigned min_comdat_num, max_comdat_num;
9595   struct cu_hash_table_entry *next;
9596 };
9597
9598 /* Routines to manipulate hash table of CUs.  */
9599 static hashval_t
9600 htab_cu_hash (const void *of)
9601 {
9602   const struct cu_hash_table_entry *const entry =
9603     (const struct cu_hash_table_entry *) of;
9604
9605   return htab_hash_string (entry->cu->die_id.die_symbol);
9606 }
9607
9608 static int
9609 htab_cu_eq (const void *of1, const void *of2)
9610 {
9611   const struct cu_hash_table_entry *const entry1 =
9612     (const struct cu_hash_table_entry *) of1;
9613   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9614
9615   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9616 }
9617
9618 static void
9619 htab_cu_del (void *what)
9620 {
9621   struct cu_hash_table_entry *next,
9622     *entry = (struct cu_hash_table_entry *) what;
9623
9624   while (entry)
9625     {
9626       next = entry->next;
9627       free (entry);
9628       entry = next;
9629     }
9630 }
9631
9632 /* Check whether we have already seen this CU and set up SYM_NUM
9633    accordingly.  */
9634 static int
9635 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9636 {
9637   struct cu_hash_table_entry dummy;
9638   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9639
9640   dummy.max_comdat_num = 0;
9641
9642   slot = (struct cu_hash_table_entry **)
9643     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9644         INSERT);
9645   entry = *slot;
9646
9647   for (; entry; last = entry, entry = entry->next)
9648     {
9649       if (same_die_p_wrap (cu, entry->cu))
9650         break;
9651     }
9652
9653   if (entry)
9654     {
9655       *sym_num = entry->min_comdat_num;
9656       return 1;
9657     }
9658
9659   entry = XCNEW (struct cu_hash_table_entry);
9660   entry->cu = cu;
9661   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9662   entry->next = *slot;
9663   *slot = entry;
9664
9665   return 0;
9666 }
9667
9668 /* Record SYM_NUM to record of CU in HTABLE.  */
9669 static void
9670 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9671 {
9672   struct cu_hash_table_entry **slot, *entry;
9673
9674   slot = (struct cu_hash_table_entry **)
9675     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9676         NO_INSERT);
9677   entry = *slot;
9678
9679   entry->max_comdat_num = sym_num;
9680 }
9681
9682 /* Traverse the DIE (which is always comp_unit_die), and set up
9683    additional compilation units for each of the include files we see
9684    bracketed by BINCL/EINCL.  */
9685
9686 static void
9687 break_out_includes (dw_die_ref die)
9688 {
9689   dw_die_ref c;
9690   dw_die_ref unit = NULL;
9691   limbo_die_node *node, **pnode;
9692   htab_t cu_hash_table;
9693
9694   c = die->die_child;
9695   if (c) do {
9696     dw_die_ref prev = c;
9697     c = c->die_sib;
9698     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9699            || (unit && is_comdat_die (c)))
9700       {
9701         dw_die_ref next = c->die_sib;
9702
9703         /* This DIE is for a secondary CU; remove it from the main one.  */
9704         remove_child_with_prev (c, prev);
9705
9706         if (c->die_tag == DW_TAG_GNU_BINCL)
9707           unit = push_new_compile_unit (unit, c);
9708         else if (c->die_tag == DW_TAG_GNU_EINCL)
9709           unit = pop_compile_unit (unit);
9710         else
9711           add_child_die (unit, c);
9712         c = next;
9713         if (c == die->die_child)
9714           break;
9715       }
9716   } while (c != die->die_child);
9717
9718 #if 0
9719   /* We can only use this in debugging, since the frontend doesn't check
9720      to make sure that we leave every include file we enter.  */
9721   gcc_assert (!unit);
9722 #endif
9723
9724   assign_symbol_names (die);
9725   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9726   for (node = limbo_die_list, pnode = &limbo_die_list;
9727        node;
9728        node = node->next)
9729     {
9730       int is_dupl;
9731
9732       compute_section_prefix (node->die);
9733       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9734                         &comdat_symbol_number);
9735       assign_symbol_names (node->die);
9736       if (is_dupl)
9737         *pnode = node->next;
9738       else
9739         {
9740           pnode = &node->next;
9741           record_comdat_symbol_number (node->die, cu_hash_table,
9742                 comdat_symbol_number);
9743         }
9744     }
9745   htab_delete (cu_hash_table);
9746 }
9747
9748 /* Return non-zero if this DIE is a declaration.  */
9749
9750 static int
9751 is_declaration_die (dw_die_ref die)
9752 {
9753   dw_attr_ref a;
9754   unsigned ix;
9755
9756   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9757     if (a->dw_attr == DW_AT_declaration)
9758       return 1;
9759
9760   return 0;
9761 }
9762
9763 /* Return non-zero if this DIE is nested inside a subprogram.  */
9764
9765 static int
9766 is_nested_in_subprogram (dw_die_ref die)
9767 {
9768   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9769
9770   if (decl == NULL)
9771     decl = die;
9772   return local_scope_p (decl);
9773 }
9774
9775 /* Return non-zero if this is a type DIE that should be moved to a
9776    COMDAT .debug_types section.  */
9777
9778 static int
9779 should_move_die_to_comdat (dw_die_ref die)
9780 {
9781   switch (die->die_tag)
9782     {
9783     case DW_TAG_class_type:
9784     case DW_TAG_structure_type:
9785     case DW_TAG_enumeration_type:
9786     case DW_TAG_union_type:
9787       /* Don't move declarations, inlined instances, or types nested in a
9788          subprogram.  */
9789       if (is_declaration_die (die)
9790           || get_AT (die, DW_AT_abstract_origin)
9791           || is_nested_in_subprogram (die))
9792         return 0;
9793       return 1;
9794     case DW_TAG_array_type:
9795     case DW_TAG_interface_type:
9796     case DW_TAG_pointer_type:
9797     case DW_TAG_reference_type:
9798     case DW_TAG_rvalue_reference_type:
9799     case DW_TAG_string_type:
9800     case DW_TAG_subroutine_type:
9801     case DW_TAG_ptr_to_member_type:
9802     case DW_TAG_set_type:
9803     case DW_TAG_subrange_type:
9804     case DW_TAG_base_type:
9805     case DW_TAG_const_type:
9806     case DW_TAG_file_type:
9807     case DW_TAG_packed_type:
9808     case DW_TAG_volatile_type:
9809     case DW_TAG_typedef:
9810     default:
9811       return 0;
9812     }
9813 }
9814
9815 /* Make a clone of DIE.  */
9816
9817 static dw_die_ref
9818 clone_die (dw_die_ref die)
9819 {
9820   dw_die_ref clone;
9821   dw_attr_ref a;
9822   unsigned ix;
9823
9824   clone = ggc_alloc_cleared_die_node ();
9825   clone->die_tag = die->die_tag;
9826
9827   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9828     add_dwarf_attr (clone, a);
9829
9830   return clone;
9831 }
9832
9833 /* Make a clone of the tree rooted at DIE.  */
9834
9835 static dw_die_ref
9836 clone_tree (dw_die_ref die)
9837 {
9838   dw_die_ref c;
9839   dw_die_ref clone = clone_die (die);
9840
9841   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9842
9843   return clone;
9844 }
9845
9846 /* Make a clone of DIE as a declaration.  */
9847
9848 static dw_die_ref
9849 clone_as_declaration (dw_die_ref die)
9850 {
9851   dw_die_ref clone;
9852   dw_die_ref decl;
9853   dw_attr_ref a;
9854   unsigned ix;
9855
9856   /* If the DIE is already a declaration, just clone it.  */
9857   if (is_declaration_die (die))
9858     return clone_die (die);
9859
9860   /* If the DIE is a specification, just clone its declaration DIE.  */
9861   decl = get_AT_ref (die, DW_AT_specification);
9862   if (decl != NULL)
9863     return clone_die (decl);
9864
9865   clone = ggc_alloc_cleared_die_node ();
9866   clone->die_tag = die->die_tag;
9867
9868   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9869     {
9870       /* We don't want to copy over all attributes.
9871          For example we don't want DW_AT_byte_size because otherwise we will no
9872          longer have a declaration and GDB will treat it as a definition.  */
9873
9874       switch (a->dw_attr)
9875         {
9876         case DW_AT_artificial:
9877         case DW_AT_containing_type:
9878         case DW_AT_external:
9879         case DW_AT_name:
9880         case DW_AT_type:
9881         case DW_AT_virtuality:
9882         case DW_AT_linkage_name:
9883         case DW_AT_MIPS_linkage_name:
9884           add_dwarf_attr (clone, a);
9885           break;
9886         case DW_AT_byte_size:
9887         default:
9888           break;
9889         }
9890     }
9891
9892   if (die->die_id.die_type_node)
9893     add_AT_die_ref (clone, DW_AT_signature, die);
9894
9895   add_AT_flag (clone, DW_AT_declaration, 1);
9896   return clone;
9897 }
9898
9899 /* Copy the declaration context to the new compile unit DIE.  This includes
9900    any surrounding namespace or type declarations.  If the DIE has an
9901    AT_specification attribute, it also includes attributes and children
9902    attached to the specification.  */
9903
9904 static void
9905 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9906 {
9907   dw_die_ref decl;
9908   dw_die_ref new_decl;
9909
9910   decl = get_AT_ref (die, DW_AT_specification);
9911   if (decl == NULL)
9912     decl = die;
9913   else
9914     {
9915       unsigned ix;
9916       dw_die_ref c;
9917       dw_attr_ref a;
9918
9919       /* Copy the type node pointer from the new DIE to the original
9920          declaration DIE so we can forward references later.  */
9921       decl->die_id.die_type_node = die->die_id.die_type_node;
9922
9923       remove_AT (die, DW_AT_specification);
9924
9925       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
9926         {
9927           if (a->dw_attr != DW_AT_name
9928               && a->dw_attr != DW_AT_declaration
9929               && a->dw_attr != DW_AT_external)
9930             add_dwarf_attr (die, a);
9931         }
9932
9933       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9934     }
9935
9936   if (decl->die_parent != NULL
9937       && decl->die_parent->die_tag != DW_TAG_compile_unit
9938       && decl->die_parent->die_tag != DW_TAG_type_unit)
9939     {
9940       new_decl = copy_ancestor_tree (unit, decl, NULL);
9941       if (new_decl != NULL)
9942         {
9943           remove_AT (new_decl, DW_AT_signature);
9944           add_AT_specification (die, new_decl);
9945         }
9946     }
9947 }
9948
9949 /* Generate the skeleton ancestor tree for the given NODE, then clone
9950    the DIE and add the clone into the tree.  */
9951
9952 static void
9953 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9954 {
9955   if (node->new_die != NULL)
9956     return;
9957
9958   node->new_die = clone_as_declaration (node->old_die);
9959
9960   if (node->parent != NULL)
9961     {
9962       generate_skeleton_ancestor_tree (node->parent);
9963       add_child_die (node->parent->new_die, node->new_die);
9964     }
9965 }
9966
9967 /* Generate a skeleton tree of DIEs containing any declarations that are
9968    found in the original tree.  We traverse the tree looking for declaration
9969    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9970
9971 static void
9972 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9973 {
9974   skeleton_chain_node node;
9975   dw_die_ref c;
9976   dw_die_ref first;
9977   dw_die_ref prev = NULL;
9978   dw_die_ref next = NULL;
9979
9980   node.parent = parent;
9981
9982   first = c = parent->old_die->die_child;
9983   if (c)
9984     next = c->die_sib;
9985   if (c) do {
9986     if (prev == NULL || prev->die_sib == c)
9987       prev = c;
9988     c = next;
9989     next = (c == first ? NULL : c->die_sib);
9990     node.old_die = c;
9991     node.new_die = NULL;
9992     if (is_declaration_die (c))
9993       {
9994         /* Clone the existing DIE, move the original to the skeleton
9995            tree (which is in the main CU), and put the clone, with
9996            all the original's children, where the original came from.  */
9997         dw_die_ref clone = clone_die (c);
9998         move_all_children (c, clone);
9999
10000         replace_child (c, clone, prev);
10001         generate_skeleton_ancestor_tree (parent);
10002         add_child_die (parent->new_die, c);
10003         node.new_die = c;
10004         c = clone;
10005       }
10006     generate_skeleton_bottom_up (&node);
10007   } while (next != NULL);
10008 }
10009
10010 /* Wrapper function for generate_skeleton_bottom_up.  */
10011
10012 static dw_die_ref
10013 generate_skeleton (dw_die_ref die)
10014 {
10015   skeleton_chain_node node;
10016
10017   node.old_die = die;
10018   node.new_die = NULL;
10019   node.parent = NULL;
10020
10021   /* If this type definition is nested inside another type,
10022      always leave at least a declaration in its place.  */
10023   if (die->die_parent != NULL && is_type_die (die->die_parent))
10024     node.new_die = clone_as_declaration (die);
10025
10026   generate_skeleton_bottom_up (&node);
10027   return node.new_die;
10028 }
10029
10030 /* Remove the DIE from its parent, possibly replacing it with a cloned
10031    declaration.  The original DIE will be moved to a new compile unit
10032    so that existing references to it follow it to the new location.  If
10033    any of the original DIE's descendants is a declaration, we need to
10034    replace the original DIE with a skeleton tree and move the
10035    declarations back into the skeleton tree.  */
10036
10037 static dw_die_ref
10038 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10039 {
10040   dw_die_ref skeleton;
10041
10042   skeleton = generate_skeleton (child);
10043   if (skeleton == NULL)
10044     remove_child_with_prev (child, prev);
10045   else
10046     {
10047       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10048       replace_child (child, skeleton, prev);
10049     }
10050
10051   return skeleton;
10052 }
10053
10054 /* Traverse the DIE and set up additional .debug_types sections for each
10055    type worthy of being placed in a COMDAT section.  */
10056
10057 static void
10058 break_out_comdat_types (dw_die_ref die)
10059 {
10060   dw_die_ref c;
10061   dw_die_ref first;
10062   dw_die_ref prev = NULL;
10063   dw_die_ref next = NULL;
10064   dw_die_ref unit = NULL;
10065
10066   first = c = die->die_child;
10067   if (c)
10068     next = c->die_sib;
10069   if (c) do {
10070     if (prev == NULL || prev->die_sib == c)
10071       prev = c;
10072     c = next;
10073     next = (c == first ? NULL : c->die_sib);
10074     if (should_move_die_to_comdat (c))
10075       {
10076         dw_die_ref replacement;
10077         comdat_type_node_ref type_node;
10078
10079         /* Create a new type unit DIE as the root for the new tree, and
10080            add it to the list of comdat types.  */
10081         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10082         add_AT_unsigned (unit, DW_AT_language,
10083                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10084         type_node = ggc_alloc_cleared_comdat_type_node ();
10085         type_node->root_die = unit;
10086         type_node->next = comdat_type_list;
10087         comdat_type_list = type_node;
10088
10089         /* Generate the type signature.  */
10090         generate_type_signature (c, type_node);
10091
10092         /* Copy the declaration context, attributes, and children of the
10093            declaration into the new compile unit DIE.  */
10094         copy_declaration_context (unit, c);
10095
10096         /* Remove this DIE from the main CU.  */
10097         replacement = remove_child_or_replace_with_skeleton (c, prev);
10098
10099         /* Break out nested types into their own type units.  */
10100         break_out_comdat_types (c);
10101
10102         /* Add the DIE to the new compunit.  */
10103         add_child_die (unit, c);
10104
10105         if (replacement != NULL)
10106           c = replacement;
10107       }
10108     else if (c->die_tag == DW_TAG_namespace
10109              || c->die_tag == DW_TAG_class_type
10110              || c->die_tag == DW_TAG_structure_type
10111              || c->die_tag == DW_TAG_union_type)
10112       {
10113         /* Look for nested types that can be broken out.  */
10114         break_out_comdat_types (c);
10115       }
10116   } while (next != NULL);
10117 }
10118
10119 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10120
10121 struct decl_table_entry
10122 {
10123   dw_die_ref orig;
10124   dw_die_ref copy;
10125 };
10126
10127 /* Routines to manipulate hash table of copied declarations.  */
10128
10129 static hashval_t
10130 htab_decl_hash (const void *of)
10131 {
10132   const struct decl_table_entry *const entry =
10133     (const struct decl_table_entry *) of;
10134
10135   return htab_hash_pointer (entry->orig);
10136 }
10137
10138 static int
10139 htab_decl_eq (const void *of1, const void *of2)
10140 {
10141   const struct decl_table_entry *const entry1 =
10142     (const struct decl_table_entry *) of1;
10143   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10144
10145   return entry1->orig == entry2;
10146 }
10147
10148 static void
10149 htab_decl_del (void *what)
10150 {
10151   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10152
10153   free (entry);
10154 }
10155
10156 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10157    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10158    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10159    to check if the ancestor has already been copied into UNIT.  */
10160
10161 static dw_die_ref
10162 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10163 {
10164   dw_die_ref parent = die->die_parent;
10165   dw_die_ref new_parent = unit;
10166   dw_die_ref copy;
10167   void **slot = NULL;
10168   struct decl_table_entry *entry = NULL;
10169
10170   if (decl_table)
10171     {
10172       /* Check if the entry has already been copied to UNIT.  */
10173       slot = htab_find_slot_with_hash (decl_table, die,
10174                                        htab_hash_pointer (die), INSERT);
10175       if (*slot != HTAB_EMPTY_ENTRY)
10176         {
10177           entry = (struct decl_table_entry *) *slot;
10178           return entry->copy;
10179         }
10180
10181       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10182       entry = XCNEW (struct decl_table_entry);
10183       entry->orig = die;
10184       entry->copy = NULL;
10185       *slot = entry;
10186     }
10187
10188   if (parent != NULL)
10189     {
10190       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10191       if (spec != NULL)
10192         parent = spec;
10193       if (parent->die_tag != DW_TAG_compile_unit
10194           && parent->die_tag != DW_TAG_type_unit)
10195         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10196     }
10197
10198   copy = clone_as_declaration (die);
10199   add_child_die (new_parent, copy);
10200
10201   if (decl_table != NULL)
10202     {
10203       /* Record the pointer to the copy.  */
10204       entry->copy = copy;
10205     }
10206
10207   return copy;
10208 }
10209
10210 /* Walk the DIE and its children, looking for references to incomplete
10211    or trivial types that are unmarked (i.e., that are not in the current
10212    type_unit).  */
10213
10214 static void
10215 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10216 {
10217   dw_die_ref c;
10218   dw_attr_ref a;
10219   unsigned ix;
10220
10221   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10222     {
10223       if (AT_class (a) == dw_val_class_die_ref)
10224         {
10225           dw_die_ref targ = AT_ref (a);
10226           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10227           void **slot;
10228           struct decl_table_entry *entry;
10229
10230           if (targ->die_mark != 0 || type_node != NULL)
10231             continue;
10232
10233           slot = htab_find_slot_with_hash (decl_table, targ,
10234                                            htab_hash_pointer (targ), INSERT);
10235
10236           if (*slot != HTAB_EMPTY_ENTRY)
10237             {
10238               /* TARG has already been copied, so we just need to
10239                  modify the reference to point to the copy.  */
10240               entry = (struct decl_table_entry *) *slot;
10241               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10242             }
10243           else
10244             {
10245               dw_die_ref parent = unit;
10246               dw_die_ref copy = clone_tree (targ);
10247
10248               /* Make sure the cloned tree is marked as part of the
10249                  type unit.  */
10250               mark_dies (copy);
10251
10252               /* Record in DECL_TABLE that TARG has been copied.
10253                  Need to do this now, before the recursive call,
10254                  because DECL_TABLE may be expanded and SLOT
10255                  would no longer be a valid pointer.  */
10256               entry = XCNEW (struct decl_table_entry);
10257               entry->orig = targ;
10258               entry->copy = copy;
10259               *slot = entry;
10260
10261               /* If TARG has surrounding context, copy its ancestor tree
10262                  into the new type unit.  */
10263               if (targ->die_parent != NULL
10264                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10265                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10266                 parent = copy_ancestor_tree (unit, targ->die_parent,
10267                                              decl_table);
10268
10269               add_child_die (parent, copy);
10270               a->dw_attr_val.v.val_die_ref.die = copy;
10271
10272               /* Make sure the newly-copied DIE is walked.  If it was
10273                  installed in a previously-added context, it won't
10274                  get visited otherwise.  */
10275               if (parent != unit)
10276                 {
10277                   /* Find the highest point of the newly-added tree,
10278                      mark each node along the way, and walk from there.  */
10279                   parent->die_mark = 1;
10280                   while (parent->die_parent
10281                          && parent->die_parent->die_mark == 0)
10282                     {
10283                       parent = parent->die_parent;
10284                       parent->die_mark = 1;
10285                     }
10286                   copy_decls_walk (unit, parent, decl_table);
10287                 }
10288             }
10289         }
10290     }
10291
10292   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10293 }
10294
10295 /* Copy declarations for "unworthy" types into the new comdat section.
10296    Incomplete types, modified types, and certain other types aren't broken
10297    out into comdat sections of their own, so they don't have a signature,
10298    and we need to copy the declaration into the same section so that we
10299    don't have an external reference.  */
10300
10301 static void
10302 copy_decls_for_unworthy_types (dw_die_ref unit)
10303 {
10304   htab_t decl_table;
10305
10306   mark_dies (unit);
10307   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10308   copy_decls_walk (unit, unit, decl_table);
10309   htab_delete (decl_table);
10310   unmark_dies (unit);
10311 }
10312
10313 /* Traverse the DIE and add a sibling attribute if it may have the
10314    effect of speeding up access to siblings.  To save some space,
10315    avoid generating sibling attributes for DIE's without children.  */
10316
10317 static void
10318 add_sibling_attributes (dw_die_ref die)
10319 {
10320   dw_die_ref c;
10321
10322   if (! die->die_child)
10323     return;
10324
10325   if (die->die_parent && die != die->die_parent->die_child)
10326     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10327
10328   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10329 }
10330
10331 /* Output all location lists for the DIE and its children.  */
10332
10333 static void
10334 output_location_lists (dw_die_ref die)
10335 {
10336   dw_die_ref c;
10337   dw_attr_ref a;
10338   unsigned ix;
10339
10340   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10341     if (AT_class (a) == dw_val_class_loc_list)
10342       output_loc_list (AT_loc_list (a));
10343
10344   FOR_EACH_CHILD (die, c, output_location_lists (c));
10345 }
10346
10347 /* The format of each DIE (and its attribute value pairs) is encoded in an
10348    abbreviation table.  This routine builds the abbreviation table and assigns
10349    a unique abbreviation id for each abbreviation entry.  The children of each
10350    die are visited recursively.  */
10351
10352 static void
10353 build_abbrev_table (dw_die_ref die)
10354 {
10355   unsigned long abbrev_id;
10356   unsigned int n_alloc;
10357   dw_die_ref c;
10358   dw_attr_ref a;
10359   unsigned ix;
10360
10361   /* Scan the DIE references, and mark as external any that refer to
10362      DIEs from other CUs (i.e. those which are not marked).  */
10363   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10364     if (AT_class (a) == dw_val_class_die_ref
10365         && AT_ref (a)->die_mark == 0)
10366       {
10367         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10368         set_AT_ref_external (a, 1);
10369       }
10370
10371   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10372     {
10373       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10374       dw_attr_ref die_a, abbrev_a;
10375       unsigned ix;
10376       bool ok = true;
10377
10378       if (abbrev->die_tag != die->die_tag)
10379         continue;
10380       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10381         continue;
10382
10383       if (VEC_length (dw_attr_node, abbrev->die_attr)
10384           != VEC_length (dw_attr_node, die->die_attr))
10385         continue;
10386
10387       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10388         {
10389           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10390           if ((abbrev_a->dw_attr != die_a->dw_attr)
10391               || (value_format (abbrev_a) != value_format (die_a)))
10392             {
10393               ok = false;
10394               break;
10395             }
10396         }
10397       if (ok)
10398         break;
10399     }
10400
10401   if (abbrev_id >= abbrev_die_table_in_use)
10402     {
10403       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10404         {
10405           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10406           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10407                                             n_alloc);
10408
10409           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10410                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10411           abbrev_die_table_allocated = n_alloc;
10412         }
10413
10414       ++abbrev_die_table_in_use;
10415       abbrev_die_table[abbrev_id] = die;
10416     }
10417
10418   die->die_abbrev = abbrev_id;
10419   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10420 }
10421 \f
10422 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10423
10424 static int
10425 constant_size (unsigned HOST_WIDE_INT value)
10426 {
10427   int log;
10428
10429   if (value == 0)
10430     log = 0;
10431   else
10432     log = floor_log2 (value);
10433
10434   log = log / 8;
10435   log = 1 << (floor_log2 (log) + 1);
10436
10437   return log;
10438 }
10439
10440 /* Return the size of a DIE as it is represented in the
10441    .debug_info section.  */
10442
10443 static unsigned long
10444 size_of_die (dw_die_ref die)
10445 {
10446   unsigned long size = 0;
10447   dw_attr_ref a;
10448   unsigned ix;
10449
10450   size += size_of_uleb128 (die->die_abbrev);
10451   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10452     {
10453       switch (AT_class (a))
10454         {
10455         case dw_val_class_addr:
10456           size += DWARF2_ADDR_SIZE;
10457           break;
10458         case dw_val_class_offset:
10459           size += DWARF_OFFSET_SIZE;
10460           break;
10461         case dw_val_class_loc:
10462           {
10463             unsigned long lsize = size_of_locs (AT_loc (a));
10464
10465             /* Block length.  */
10466             if (dwarf_version >= 4)
10467               size += size_of_uleb128 (lsize);
10468             else
10469               size += constant_size (lsize);
10470             size += lsize;
10471           }
10472           break;
10473         case dw_val_class_loc_list:
10474           size += DWARF_OFFSET_SIZE;
10475           break;
10476         case dw_val_class_range_list:
10477           size += DWARF_OFFSET_SIZE;
10478           break;
10479         case dw_val_class_const:
10480           size += size_of_sleb128 (AT_int (a));
10481           break;
10482         case dw_val_class_unsigned_const:
10483           size += constant_size (AT_unsigned (a));
10484           break;
10485         case dw_val_class_const_double:
10486           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10487           if (HOST_BITS_PER_WIDE_INT >= 64)
10488             size++; /* block */
10489           break;
10490         case dw_val_class_vec:
10491           size += constant_size (a->dw_attr_val.v.val_vec.length
10492                                  * a->dw_attr_val.v.val_vec.elt_size)
10493                   + a->dw_attr_val.v.val_vec.length
10494                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10495           break;
10496         case dw_val_class_flag:
10497           if (dwarf_version >= 4)
10498             /* Currently all add_AT_flag calls pass in 1 as last argument,
10499                so DW_FORM_flag_present can be used.  If that ever changes,
10500                we'll need to use DW_FORM_flag and have some optimization
10501                in build_abbrev_table that will change those to
10502                DW_FORM_flag_present if it is set to 1 in all DIEs using
10503                the same abbrev entry.  */
10504             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10505           else
10506             size += 1;
10507           break;
10508         case dw_val_class_die_ref:
10509           if (AT_ref_external (a))
10510             {
10511               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10512                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10513                  is sized by target address length, whereas in DWARF3
10514                  it's always sized as an offset.  */
10515               if (dwarf_version >= 4)
10516                 size += DWARF_TYPE_SIGNATURE_SIZE;
10517               else if (dwarf_version == 2)
10518                 size += DWARF2_ADDR_SIZE;
10519               else
10520                 size += DWARF_OFFSET_SIZE;
10521             }
10522           else
10523             size += DWARF_OFFSET_SIZE;
10524           break;
10525         case dw_val_class_fde_ref:
10526           size += DWARF_OFFSET_SIZE;
10527           break;
10528         case dw_val_class_lbl_id:
10529           size += DWARF2_ADDR_SIZE;
10530           break;
10531         case dw_val_class_lineptr:
10532         case dw_val_class_macptr:
10533           size += DWARF_OFFSET_SIZE;
10534           break;
10535         case dw_val_class_str:
10536           if (AT_string_form (a) == DW_FORM_strp)
10537             size += DWARF_OFFSET_SIZE;
10538           else
10539             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10540           break;
10541         case dw_val_class_file:
10542           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10543           break;
10544         case dw_val_class_data8:
10545           size += 8;
10546           break;
10547         case dw_val_class_vms_delta:
10548           size += DWARF_OFFSET_SIZE;
10549           break;
10550         default:
10551           gcc_unreachable ();
10552         }
10553     }
10554
10555   return size;
10556 }
10557
10558 /* Size the debugging information associated with a given DIE.  Visits the
10559    DIE's children recursively.  Updates the global variable next_die_offset, on
10560    each time through.  Uses the current value of next_die_offset to update the
10561    die_offset field in each DIE.  */
10562
10563 static void
10564 calc_die_sizes (dw_die_ref die)
10565 {
10566   dw_die_ref c;
10567
10568   die->die_offset = next_die_offset;
10569   next_die_offset += size_of_die (die);
10570
10571   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10572
10573   if (die->die_child != NULL)
10574     /* Count the null byte used to terminate sibling lists.  */
10575     next_die_offset += 1;
10576 }
10577
10578 /* Set the marks for a die and its children.  We do this so
10579    that we know whether or not a reference needs to use FORM_ref_addr; only
10580    DIEs in the same CU will be marked.  We used to clear out the offset
10581    and use that as the flag, but ran into ordering problems.  */
10582
10583 static void
10584 mark_dies (dw_die_ref die)
10585 {
10586   dw_die_ref c;
10587
10588   gcc_assert (!die->die_mark);
10589
10590   die->die_mark = 1;
10591   FOR_EACH_CHILD (die, c, mark_dies (c));
10592 }
10593
10594 /* Clear the marks for a die and its children.  */
10595
10596 static void
10597 unmark_dies (dw_die_ref die)
10598 {
10599   dw_die_ref c;
10600
10601   if (dwarf_version < 4)
10602     gcc_assert (die->die_mark);
10603
10604   die->die_mark = 0;
10605   FOR_EACH_CHILD (die, c, unmark_dies (c));
10606 }
10607
10608 /* Clear the marks for a die, its children and referred dies.  */
10609
10610 static void
10611 unmark_all_dies (dw_die_ref die)
10612 {
10613   dw_die_ref c;
10614   dw_attr_ref a;
10615   unsigned ix;
10616
10617   if (!die->die_mark)
10618     return;
10619   die->die_mark = 0;
10620
10621   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10622
10623   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10624     if (AT_class (a) == dw_val_class_die_ref)
10625       unmark_all_dies (AT_ref (a));
10626 }
10627
10628 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10629    generated for the compilation unit.  */
10630
10631 static unsigned long
10632 size_of_pubnames (VEC (pubname_entry, gc) * names)
10633 {
10634   unsigned long size;
10635   unsigned i;
10636   pubname_ref p;
10637
10638   size = DWARF_PUBNAMES_HEADER_SIZE;
10639   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10640     if (names != pubtype_table
10641         || p->die->die_offset != 0
10642         || !flag_eliminate_unused_debug_types)
10643       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10644
10645   size += DWARF_OFFSET_SIZE;
10646   return size;
10647 }
10648
10649 /* Return the size of the information in the .debug_aranges section.  */
10650
10651 static unsigned long
10652 size_of_aranges (void)
10653 {
10654   unsigned long size;
10655
10656   size = DWARF_ARANGES_HEADER_SIZE;
10657
10658   /* Count the address/length pair for this compilation unit.  */
10659   if (text_section_used)
10660     size += 2 * DWARF2_ADDR_SIZE;
10661   if (cold_text_section_used)
10662     size += 2 * DWARF2_ADDR_SIZE;
10663   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10664
10665   /* Count the two zero words used to terminated the address range table.  */
10666   size += 2 * DWARF2_ADDR_SIZE;
10667   return size;
10668 }
10669 \f
10670 /* Select the encoding of an attribute value.  */
10671
10672 static enum dwarf_form
10673 value_format (dw_attr_ref a)
10674 {
10675   switch (a->dw_attr_val.val_class)
10676     {
10677     case dw_val_class_addr:
10678       /* Only very few attributes allow DW_FORM_addr.  */
10679       switch (a->dw_attr)
10680         {
10681         case DW_AT_low_pc:
10682         case DW_AT_high_pc:
10683         case DW_AT_entry_pc:
10684         case DW_AT_trampoline:
10685           return DW_FORM_addr;
10686         default:
10687           break;
10688         }
10689       switch (DWARF2_ADDR_SIZE)
10690         {
10691         case 1:
10692           return DW_FORM_data1;
10693         case 2:
10694           return DW_FORM_data2;
10695         case 4:
10696           return DW_FORM_data4;
10697         case 8:
10698           return DW_FORM_data8;
10699         default:
10700           gcc_unreachable ();
10701         }
10702     case dw_val_class_range_list:
10703     case dw_val_class_loc_list:
10704       if (dwarf_version >= 4)
10705         return DW_FORM_sec_offset;
10706       /* FALLTHRU */
10707     case dw_val_class_vms_delta:
10708     case dw_val_class_offset:
10709       switch (DWARF_OFFSET_SIZE)
10710         {
10711         case 4:
10712           return DW_FORM_data4;
10713         case 8:
10714           return DW_FORM_data8;
10715         default:
10716           gcc_unreachable ();
10717         }
10718     case dw_val_class_loc:
10719       if (dwarf_version >= 4)
10720         return DW_FORM_exprloc;
10721       switch (constant_size (size_of_locs (AT_loc (a))))
10722         {
10723         case 1:
10724           return DW_FORM_block1;
10725         case 2:
10726           return DW_FORM_block2;
10727         default:
10728           gcc_unreachable ();
10729         }
10730     case dw_val_class_const:
10731       return DW_FORM_sdata;
10732     case dw_val_class_unsigned_const:
10733       switch (constant_size (AT_unsigned (a)))
10734         {
10735         case 1:
10736           return DW_FORM_data1;
10737         case 2:
10738           return DW_FORM_data2;
10739         case 4:
10740           return DW_FORM_data4;
10741         case 8:
10742           return DW_FORM_data8;
10743         default:
10744           gcc_unreachable ();
10745         }
10746     case dw_val_class_const_double:
10747       switch (HOST_BITS_PER_WIDE_INT)
10748         {
10749         case 8:
10750           return DW_FORM_data2;
10751         case 16:
10752           return DW_FORM_data4;
10753         case 32:
10754           return DW_FORM_data8;
10755         case 64:
10756         default:
10757           return DW_FORM_block1;
10758         }
10759     case dw_val_class_vec:
10760       switch (constant_size (a->dw_attr_val.v.val_vec.length
10761                              * a->dw_attr_val.v.val_vec.elt_size))
10762         {
10763         case 1:
10764           return DW_FORM_block1;
10765         case 2:
10766           return DW_FORM_block2;
10767         case 4:
10768           return DW_FORM_block4;
10769         default:
10770           gcc_unreachable ();
10771         }
10772     case dw_val_class_flag:
10773       if (dwarf_version >= 4)
10774         {
10775           /* Currently all add_AT_flag calls pass in 1 as last argument,
10776              so DW_FORM_flag_present can be used.  If that ever changes,
10777              we'll need to use DW_FORM_flag and have some optimization
10778              in build_abbrev_table that will change those to
10779              DW_FORM_flag_present if it is set to 1 in all DIEs using
10780              the same abbrev entry.  */
10781           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10782           return DW_FORM_flag_present;
10783         }
10784       return DW_FORM_flag;
10785     case dw_val_class_die_ref:
10786       if (AT_ref_external (a))
10787         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10788       else
10789         return DW_FORM_ref;
10790     case dw_val_class_fde_ref:
10791       return DW_FORM_data;
10792     case dw_val_class_lbl_id:
10793       return DW_FORM_addr;
10794     case dw_val_class_lineptr:
10795     case dw_val_class_macptr:
10796       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10797     case dw_val_class_str:
10798       return AT_string_form (a);
10799     case dw_val_class_file:
10800       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10801         {
10802         case 1:
10803           return DW_FORM_data1;
10804         case 2:
10805           return DW_FORM_data2;
10806         case 4:
10807           return DW_FORM_data4;
10808         default:
10809           gcc_unreachable ();
10810         }
10811
10812     case dw_val_class_data8:
10813       return DW_FORM_data8;
10814
10815     default:
10816       gcc_unreachable ();
10817     }
10818 }
10819
10820 /* Output the encoding of an attribute value.  */
10821
10822 static void
10823 output_value_format (dw_attr_ref a)
10824 {
10825   enum dwarf_form form = value_format (a);
10826
10827   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10828 }
10829
10830 /* Output the .debug_abbrev section which defines the DIE abbreviation
10831    table.  */
10832
10833 static void
10834 output_abbrev_section (void)
10835 {
10836   unsigned long abbrev_id;
10837
10838   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10839     {
10840       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10841       unsigned ix;
10842       dw_attr_ref a_attr;
10843
10844       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10845       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10846                                    dwarf_tag_name (abbrev->die_tag));
10847
10848       if (abbrev->die_child != NULL)
10849         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10850       else
10851         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10852
10853       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10854            ix++)
10855         {
10856           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10857                                        dwarf_attr_name (a_attr->dw_attr));
10858           output_value_format (a_attr);
10859         }
10860
10861       dw2_asm_output_data (1, 0, NULL);
10862       dw2_asm_output_data (1, 0, NULL);
10863     }
10864
10865   /* Terminate the table.  */
10866   dw2_asm_output_data (1, 0, NULL);
10867 }
10868
10869 /* Output a symbol we can use to refer to this DIE from another CU.  */
10870
10871 static inline void
10872 output_die_symbol (dw_die_ref die)
10873 {
10874   char *sym = die->die_id.die_symbol;
10875
10876   if (sym == 0)
10877     return;
10878
10879   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10880     /* We make these global, not weak; if the target doesn't support
10881        .linkonce, it doesn't support combining the sections, so debugging
10882        will break.  */
10883     targetm.asm_out.globalize_label (asm_out_file, sym);
10884
10885   ASM_OUTPUT_LABEL (asm_out_file, sym);
10886 }
10887
10888 /* Return a new location list, given the begin and end range, and the
10889    expression.  */
10890
10891 static inline dw_loc_list_ref
10892 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10893               const char *section)
10894 {
10895   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10896
10897   retlist->begin = begin;
10898   retlist->end = end;
10899   retlist->expr = expr;
10900   retlist->section = section;
10901
10902   return retlist;
10903 }
10904
10905 /* Generate a new internal symbol for this location list node, if it
10906    hasn't got one yet.  */
10907
10908 static inline void
10909 gen_llsym (dw_loc_list_ref list)
10910 {
10911   gcc_assert (!list->ll_symbol);
10912   list->ll_symbol = gen_internal_sym ("LLST");
10913 }
10914
10915 /* Output the location list given to us.  */
10916
10917 static void
10918 output_loc_list (dw_loc_list_ref list_head)
10919 {
10920   dw_loc_list_ref curr = list_head;
10921
10922   if (list_head->emitted)
10923     return;
10924   list_head->emitted = true;
10925
10926   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10927
10928   /* Walk the location list, and output each range + expression.  */
10929   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10930     {
10931       unsigned long size;
10932       /* Don't output an entry that starts and ends at the same address.  */
10933       if (strcmp (curr->begin, curr->end) == 0)
10934         continue;
10935       if (!have_multiple_function_sections)
10936         {
10937           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10938                                 "Location list begin address (%s)",
10939                                 list_head->ll_symbol);
10940           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10941                                 "Location list end address (%s)",
10942                                 list_head->ll_symbol);
10943         }
10944       else
10945         {
10946           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10947                                "Location list begin address (%s)",
10948                                list_head->ll_symbol);
10949           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10950                                "Location list end address (%s)",
10951                                list_head->ll_symbol);
10952         }
10953       size = size_of_locs (curr->expr);
10954
10955       /* Output the block length for this list of location operations.  */
10956       gcc_assert (size <= 0xffff);
10957       dw2_asm_output_data (2, size, "%s", "Location expression size");
10958
10959       output_loc_sequence (curr->expr);
10960     }
10961
10962   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10963                        "Location list terminator begin (%s)",
10964                        list_head->ll_symbol);
10965   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10966                        "Location list terminator end (%s)",
10967                        list_head->ll_symbol);
10968 }
10969
10970 /* Output a type signature.  */
10971
10972 static inline void
10973 output_signature (const char *sig, const char *name)
10974 {
10975   int i;
10976
10977   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10978     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10979 }
10980
10981 /* Output the DIE and its attributes.  Called recursively to generate
10982    the definitions of each child DIE.  */
10983
10984 static void
10985 output_die (dw_die_ref die)
10986 {
10987   dw_attr_ref a;
10988   dw_die_ref c;
10989   unsigned long size;
10990   unsigned ix;
10991
10992   /* If someone in another CU might refer to us, set up a symbol for
10993      them to point to.  */
10994   if (dwarf_version < 4 && die->die_id.die_symbol)
10995     output_die_symbol (die);
10996
10997   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10998                                (unsigned long)die->die_offset,
10999                                dwarf_tag_name (die->die_tag));
11000
11001   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11002     {
11003       const char *name = dwarf_attr_name (a->dw_attr);
11004
11005       switch (AT_class (a))
11006         {
11007         case dw_val_class_addr:
11008           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11009           break;
11010
11011         case dw_val_class_offset:
11012           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11013                                "%s", name);
11014           break;
11015
11016         case dw_val_class_range_list:
11017           {
11018             char *p = strchr (ranges_section_label, '\0');
11019
11020             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11021                      a->dw_attr_val.v.val_offset);
11022             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11023                                    debug_ranges_section, "%s", name);
11024             *p = '\0';
11025           }
11026           break;
11027
11028         case dw_val_class_loc:
11029           size = size_of_locs (AT_loc (a));
11030
11031           /* Output the block length for this list of location operations.  */
11032           if (dwarf_version >= 4)
11033             dw2_asm_output_data_uleb128 (size, "%s", name);
11034           else
11035             dw2_asm_output_data (constant_size (size), size, "%s", name);
11036
11037           output_loc_sequence (AT_loc (a));
11038           break;
11039
11040         case dw_val_class_const:
11041           /* ??? It would be slightly more efficient to use a scheme like is
11042              used for unsigned constants below, but gdb 4.x does not sign
11043              extend.  Gdb 5.x does sign extend.  */
11044           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11045           break;
11046
11047         case dw_val_class_unsigned_const:
11048           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11049                                AT_unsigned (a), "%s", name);
11050           break;
11051
11052         case dw_val_class_const_double:
11053           {
11054             unsigned HOST_WIDE_INT first, second;
11055
11056             if (HOST_BITS_PER_WIDE_INT >= 64)
11057               dw2_asm_output_data (1,
11058                                    2 * HOST_BITS_PER_WIDE_INT
11059                                    / HOST_BITS_PER_CHAR,
11060                                    NULL);
11061
11062             if (WORDS_BIG_ENDIAN)
11063               {
11064                 first = a->dw_attr_val.v.val_double.high;
11065                 second = a->dw_attr_val.v.val_double.low;
11066               }
11067             else
11068               {
11069                 first = a->dw_attr_val.v.val_double.low;
11070                 second = a->dw_attr_val.v.val_double.high;
11071               }
11072
11073             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11074                                  first, name);
11075             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11076                                  second, NULL);
11077           }
11078           break;
11079
11080         case dw_val_class_vec:
11081           {
11082             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11083             unsigned int len = a->dw_attr_val.v.val_vec.length;
11084             unsigned int i;
11085             unsigned char *p;
11086
11087             dw2_asm_output_data (constant_size (len * elt_size),
11088                                  len * elt_size, "%s", name);
11089             if (elt_size > sizeof (HOST_WIDE_INT))
11090               {
11091                 elt_size /= 2;
11092                 len *= 2;
11093               }
11094             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11095                  i < len;
11096                  i++, p += elt_size)
11097               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11098                                    "fp or vector constant word %u", i);
11099             break;
11100           }
11101
11102         case dw_val_class_flag:
11103           if (dwarf_version >= 4)
11104             {
11105               /* Currently all add_AT_flag calls pass in 1 as last argument,
11106                  so DW_FORM_flag_present can be used.  If that ever changes,
11107                  we'll need to use DW_FORM_flag and have some optimization
11108                  in build_abbrev_table that will change those to
11109                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11110                  the same abbrev entry.  */
11111               gcc_assert (AT_flag (a) == 1);
11112               if (flag_debug_asm)
11113                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11114                          ASM_COMMENT_START, name);
11115               break;
11116             }
11117           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11118           break;
11119
11120         case dw_val_class_loc_list:
11121           {
11122             char *sym = AT_loc_list (a)->ll_symbol;
11123
11124             gcc_assert (sym);
11125             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11126                                    "%s", name);
11127           }
11128           break;
11129
11130         case dw_val_class_die_ref:
11131           if (AT_ref_external (a))
11132             {
11133               if (dwarf_version >= 4)
11134                 {
11135                   comdat_type_node_ref type_node =
11136                     AT_ref (a)->die_id.die_type_node;
11137
11138                   gcc_assert (type_node);
11139                   output_signature (type_node->signature, name);
11140                 }
11141               else
11142                 {
11143                   char *sym = AT_ref (a)->die_id.die_symbol;
11144                   int size;
11145
11146                   gcc_assert (sym);
11147                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11148                      length, whereas in DWARF3 it's always sized as an
11149                      offset.  */
11150                   if (dwarf_version == 2)
11151                     size = DWARF2_ADDR_SIZE;
11152                   else
11153                     size = DWARF_OFFSET_SIZE;
11154                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11155                                          name);
11156                 }
11157             }
11158           else
11159             {
11160               gcc_assert (AT_ref (a)->die_offset);
11161               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11162                                    "%s", name);
11163             }
11164           break;
11165
11166         case dw_val_class_fde_ref:
11167           {
11168             char l1[20];
11169
11170             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11171                                          a->dw_attr_val.v.val_fde_index * 2);
11172             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11173                                    "%s", name);
11174           }
11175           break;
11176
11177         case dw_val_class_vms_delta:
11178           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11179                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11180                                     "%s", name);
11181           break;
11182
11183         case dw_val_class_lbl_id:
11184           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11185           break;
11186
11187         case dw_val_class_lineptr:
11188           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11189                                  debug_line_section, "%s", name);
11190           break;
11191
11192         case dw_val_class_macptr:
11193           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11194                                  debug_macinfo_section, "%s", name);
11195           break;
11196
11197         case dw_val_class_str:
11198           if (AT_string_form (a) == DW_FORM_strp)
11199             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11200                                    a->dw_attr_val.v.val_str->label,
11201                                    debug_str_section,
11202                                    "%s: \"%s\"", name, AT_string (a));
11203           else
11204             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11205           break;
11206
11207         case dw_val_class_file:
11208           {
11209             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11210
11211             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11212                                  a->dw_attr_val.v.val_file->filename);
11213             break;
11214           }
11215
11216         case dw_val_class_data8:
11217           {
11218             int i;
11219
11220             for (i = 0; i < 8; i++)
11221               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11222                                    i == 0 ? "%s" : NULL, name);
11223             break;
11224           }
11225
11226         default:
11227           gcc_unreachable ();
11228         }
11229     }
11230
11231   FOR_EACH_CHILD (die, c, output_die (c));
11232
11233   /* Add null byte to terminate sibling list.  */
11234   if (die->die_child != NULL)
11235     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11236                          (unsigned long) die->die_offset);
11237 }
11238
11239 /* Output the compilation unit that appears at the beginning of the
11240    .debug_info section, and precedes the DIE descriptions.  */
11241
11242 static void
11243 output_compilation_unit_header (void)
11244 {
11245   int ver = dwarf_version;
11246
11247   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11248     dw2_asm_output_data (4, 0xffffffff,
11249       "Initial length escape value indicating 64-bit DWARF extension");
11250   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11251                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11252                        "Length of Compilation Unit Info");
11253   dw2_asm_output_data (2, ver, "DWARF version number");
11254   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11255                          debug_abbrev_section,
11256                          "Offset Into Abbrev. Section");
11257   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11258 }
11259
11260 /* Output the compilation unit DIE and its children.  */
11261
11262 static void
11263 output_comp_unit (dw_die_ref die, int output_if_empty)
11264 {
11265   const char *secname;
11266   char *oldsym, *tmp;
11267
11268   /* Unless we are outputting main CU, we may throw away empty ones.  */
11269   if (!output_if_empty && die->die_child == NULL)
11270     return;
11271
11272   /* Even if there are no children of this DIE, we must output the information
11273      about the compilation unit.  Otherwise, on an empty translation unit, we
11274      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11275      will then complain when examining the file.  First mark all the DIEs in
11276      this CU so we know which get local refs.  */
11277   mark_dies (die);
11278
11279   build_abbrev_table (die);
11280
11281   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11282   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11283   calc_die_sizes (die);
11284
11285   oldsym = die->die_id.die_symbol;
11286   if (oldsym)
11287     {
11288       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11289
11290       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11291       secname = tmp;
11292       die->die_id.die_symbol = NULL;
11293       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11294     }
11295   else
11296     {
11297       switch_to_section (debug_info_section);
11298       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11299       info_section_emitted = true;
11300     }
11301
11302   /* Output debugging information.  */
11303   output_compilation_unit_header ();
11304   output_die (die);
11305
11306   /* Leave the marks on the main CU, so we can check them in
11307      output_pubnames.  */
11308   if (oldsym)
11309     {
11310       unmark_dies (die);
11311       die->die_id.die_symbol = oldsym;
11312     }
11313 }
11314
11315 /* Output a comdat type unit DIE and its children.  */
11316
11317 static void
11318 output_comdat_type_unit (comdat_type_node *node)
11319 {
11320   const char *secname;
11321   char *tmp;
11322   int i;
11323 #if defined (OBJECT_FORMAT_ELF)
11324   tree comdat_key;
11325 #endif
11326
11327   /* First mark all the DIEs in this CU so we know which get local refs.  */
11328   mark_dies (node->root_die);
11329
11330   build_abbrev_table (node->root_die);
11331
11332   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11333   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11334   calc_die_sizes (node->root_die);
11335
11336 #if defined (OBJECT_FORMAT_ELF)
11337   secname = ".debug_types";
11338   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11339   sprintf (tmp, "wt.");
11340   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11341     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11342   comdat_key = get_identifier (tmp);
11343   targetm.asm_out.named_section (secname,
11344                                  SECTION_DEBUG | SECTION_LINKONCE,
11345                                  comdat_key);
11346 #else
11347   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11348   sprintf (tmp, ".gnu.linkonce.wt.");
11349   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11350     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11351   secname = tmp;
11352   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11353 #endif
11354
11355   /* Output debugging information.  */
11356   output_compilation_unit_header ();
11357   output_signature (node->signature, "Type Signature");
11358   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11359                        "Offset to Type DIE");
11360   output_die (node->root_die);
11361
11362   unmark_dies (node->root_die);
11363 }
11364
11365 /* Return the DWARF2/3 pubname associated with a decl.  */
11366
11367 static const char *
11368 dwarf2_name (tree decl, int scope)
11369 {
11370   if (DECL_NAMELESS (decl))
11371     return NULL;
11372   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11373 }
11374
11375 /* Add a new entry to .debug_pubnames if appropriate.  */
11376
11377 static void
11378 add_pubname_string (const char *str, dw_die_ref die)
11379 {
11380   if (targetm.want_debug_pub_sections)
11381     {
11382       pubname_entry e;
11383
11384       e.die = die;
11385       e.name = xstrdup (str);
11386       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11387     }
11388 }
11389
11390 static void
11391 add_pubname (tree decl, dw_die_ref die)
11392 {
11393   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11394     {
11395       const char *name = dwarf2_name (decl, 1);
11396       if (name)
11397         add_pubname_string (name, die);
11398     }
11399 }
11400
11401 /* Add a new entry to .debug_pubtypes if appropriate.  */
11402
11403 static void
11404 add_pubtype (tree decl, dw_die_ref die)
11405 {
11406   pubname_entry e;
11407
11408   if (!targetm.want_debug_pub_sections)
11409     return;
11410
11411   e.name = NULL;
11412   if ((TREE_PUBLIC (decl)
11413        || is_cu_die (die->die_parent))
11414       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11415     {
11416       e.die = die;
11417       if (TYPE_P (decl))
11418         {
11419           if (TYPE_NAME (decl))
11420             {
11421               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11422                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11423               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11424                        && DECL_NAME (TYPE_NAME (decl)))
11425                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11426               else
11427                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11428             }
11429         }
11430       else
11431         {
11432           e.name = dwarf2_name (decl, 1);
11433           if (e.name)
11434             e.name = xstrdup (e.name);
11435         }
11436
11437       /* If we don't have a name for the type, there's no point in adding
11438          it to the table.  */
11439       if (e.name && e.name[0] != '\0')
11440         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11441     }
11442 }
11443
11444 /* Output the public names table used to speed up access to externally
11445    visible names; or the public types table used to find type definitions.  */
11446
11447 static void
11448 output_pubnames (VEC (pubname_entry, gc) * names)
11449 {
11450   unsigned i;
11451   unsigned long pubnames_length = size_of_pubnames (names);
11452   pubname_ref pub;
11453
11454   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11455     dw2_asm_output_data (4, 0xffffffff,
11456       "Initial length escape value indicating 64-bit DWARF extension");
11457   if (names == pubname_table)
11458     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11459                          "Length of Public Names Info");
11460   else
11461     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11462                          "Length of Public Type Names Info");
11463   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11464   dw2_asm_output_data (2, 2, "DWARF Version");
11465   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11466                          debug_info_section,
11467                          "Offset of Compilation Unit Info");
11468   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11469                        "Compilation Unit Length");
11470
11471   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11472     {
11473       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11474       if (names == pubname_table)
11475         gcc_assert (pub->die->die_mark);
11476
11477       if (names != pubtype_table
11478           || pub->die->die_offset != 0
11479           || !flag_eliminate_unused_debug_types)
11480         {
11481           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11482                                "DIE offset");
11483
11484           dw2_asm_output_nstring (pub->name, -1, "external name");
11485         }
11486     }
11487
11488   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11489 }
11490
11491 /* Add a new entry to .debug_aranges if appropriate.  */
11492
11493 static void
11494 add_arange (tree decl, dw_die_ref die)
11495 {
11496   if (! DECL_SECTION_NAME (decl))
11497     return;
11498
11499   if (arange_table_in_use == arange_table_allocated)
11500     {
11501       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11502       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11503                                     arange_table_allocated);
11504       memset (arange_table + arange_table_in_use, 0,
11505               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11506     }
11507
11508   arange_table[arange_table_in_use++] = die;
11509 }
11510
11511 /* Output the information that goes into the .debug_aranges table.
11512    Namely, define the beginning and ending address range of the
11513    text section generated for this compilation unit.  */
11514
11515 static void
11516 output_aranges (void)
11517 {
11518   unsigned i;
11519   unsigned long aranges_length = size_of_aranges ();
11520
11521   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11522     dw2_asm_output_data (4, 0xffffffff,
11523       "Initial length escape value indicating 64-bit DWARF extension");
11524   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11525                        "Length of Address Ranges Info");
11526   /* Version number for aranges is still 2, even in DWARF3.  */
11527   dw2_asm_output_data (2, 2, "DWARF Version");
11528   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11529                          debug_info_section,
11530                          "Offset of Compilation Unit Info");
11531   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11532   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11533
11534   /* We need to align to twice the pointer size here.  */
11535   if (DWARF_ARANGES_PAD_SIZE)
11536     {
11537       /* Pad using a 2 byte words so that padding is correct for any
11538          pointer size.  */
11539       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11540                            2 * DWARF2_ADDR_SIZE);
11541       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11542         dw2_asm_output_data (2, 0, NULL);
11543     }
11544
11545   /* It is necessary not to output these entries if the sections were
11546      not used; if the sections were not used, the length will be 0 and
11547      the address may end up as 0 if the section is discarded by ld
11548      --gc-sections, leaving an invalid (0, 0) entry that can be
11549      confused with the terminator.  */
11550   if (text_section_used)
11551     {
11552       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11553       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11554                             text_section_label, "Length");
11555     }
11556   if (cold_text_section_used)
11557     {
11558       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11559                            "Address");
11560       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11561                             cold_text_section_label, "Length");
11562     }
11563
11564   for (i = 0; i < arange_table_in_use; i++)
11565     {
11566       dw_die_ref die = arange_table[i];
11567
11568       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11569       gcc_assert (die->die_mark);
11570
11571       if (die->die_tag == DW_TAG_subprogram)
11572         {
11573           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11574                                "Address");
11575           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11576                                 get_AT_low_pc (die), "Length");
11577         }
11578       else
11579         {
11580           /* A static variable; extract the symbol from DW_AT_location.
11581              Note that this code isn't currently hit, as we only emit
11582              aranges for functions (jason 9/23/99).  */
11583           dw_attr_ref a = get_AT (die, DW_AT_location);
11584           dw_loc_descr_ref loc;
11585
11586           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11587
11588           loc = AT_loc (a);
11589           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11590
11591           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11592                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11593           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11594                                get_AT_unsigned (die, DW_AT_byte_size),
11595                                "Length");
11596         }
11597     }
11598
11599   /* Output the terminator words.  */
11600   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11601   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11602 }
11603
11604 /* Add a new entry to .debug_ranges.  Return the offset at which it
11605    was placed.  */
11606
11607 static unsigned int
11608 add_ranges_num (int num)
11609 {
11610   unsigned int in_use = ranges_table_in_use;
11611
11612   if (in_use == ranges_table_allocated)
11613     {
11614       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11615       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11616                                     ranges_table_allocated);
11617       memset (ranges_table + ranges_table_in_use, 0,
11618               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11619     }
11620
11621   ranges_table[in_use].num = num;
11622   ranges_table_in_use = in_use + 1;
11623
11624   return in_use * 2 * DWARF2_ADDR_SIZE;
11625 }
11626
11627 /* Add a new entry to .debug_ranges corresponding to a block, or a
11628    range terminator if BLOCK is NULL.  */
11629
11630 static unsigned int
11631 add_ranges (const_tree block)
11632 {
11633   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11634 }
11635
11636 /* Add a new entry to .debug_ranges corresponding to a pair of
11637    labels.  */
11638
11639 static void
11640 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11641                       bool *added)
11642 {
11643   unsigned int in_use = ranges_by_label_in_use;
11644   unsigned int offset;
11645
11646   if (in_use == ranges_by_label_allocated)
11647     {
11648       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11649       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11650                                        ranges_by_label,
11651                                        ranges_by_label_allocated);
11652       memset (ranges_by_label + ranges_by_label_in_use, 0,
11653               RANGES_TABLE_INCREMENT
11654               * sizeof (struct dw_ranges_by_label_struct));
11655     }
11656
11657   ranges_by_label[in_use].begin = begin;
11658   ranges_by_label[in_use].end = end;
11659   ranges_by_label_in_use = in_use + 1;
11660
11661   offset = add_ranges_num (-(int)in_use - 1);
11662   if (!*added)
11663     {
11664       add_AT_range_list (die, DW_AT_ranges, offset);
11665       *added = true;
11666     }
11667 }
11668
11669 static void
11670 output_ranges (void)
11671 {
11672   unsigned i;
11673   static const char *const start_fmt = "Offset %#x";
11674   const char *fmt = start_fmt;
11675
11676   for (i = 0; i < ranges_table_in_use; i++)
11677     {
11678       int block_num = ranges_table[i].num;
11679
11680       if (block_num > 0)
11681         {
11682           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11683           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11684
11685           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11686           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11687
11688           /* If all code is in the text section, then the compilation
11689              unit base address defaults to DW_AT_low_pc, which is the
11690              base of the text section.  */
11691           if (!have_multiple_function_sections)
11692             {
11693               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11694                                     text_section_label,
11695                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11696               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11697                                     text_section_label, NULL);
11698             }
11699
11700           /* Otherwise, the compilation unit base address is zero,
11701              which allows us to use absolute addresses, and not worry
11702              about whether the target supports cross-section
11703              arithmetic.  */
11704           else
11705             {
11706               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11707                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11708               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11709             }
11710
11711           fmt = NULL;
11712         }
11713
11714       /* Negative block_num stands for an index into ranges_by_label.  */
11715       else if (block_num < 0)
11716         {
11717           int lab_idx = - block_num - 1;
11718
11719           if (!have_multiple_function_sections)
11720             {
11721               gcc_unreachable ();
11722 #if 0
11723               /* If we ever use add_ranges_by_labels () for a single
11724                  function section, all we have to do is to take out
11725                  the #if 0 above.  */
11726               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11727                                     ranges_by_label[lab_idx].begin,
11728                                     text_section_label,
11729                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11730               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11731                                     ranges_by_label[lab_idx].end,
11732                                     text_section_label, NULL);
11733 #endif
11734             }
11735           else
11736             {
11737               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11738                                    ranges_by_label[lab_idx].begin,
11739                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11740               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11741                                    ranges_by_label[lab_idx].end,
11742                                    NULL);
11743             }
11744         }
11745       else
11746         {
11747           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11748           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11749           fmt = start_fmt;
11750         }
11751     }
11752 }
11753
11754 /* Data structure containing information about input files.  */
11755 struct file_info
11756 {
11757   const char *path;     /* Complete file name.  */
11758   const char *fname;    /* File name part.  */
11759   int length;           /* Length of entire string.  */
11760   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11761   int dir_idx;          /* Index in directory table.  */
11762 };
11763
11764 /* Data structure containing information about directories with source
11765    files.  */
11766 struct dir_info
11767 {
11768   const char *path;     /* Path including directory name.  */
11769   int length;           /* Path length.  */
11770   int prefix;           /* Index of directory entry which is a prefix.  */
11771   int count;            /* Number of files in this directory.  */
11772   int dir_idx;          /* Index of directory used as base.  */
11773 };
11774
11775 /* Callback function for file_info comparison.  We sort by looking at
11776    the directories in the path.  */
11777
11778 static int
11779 file_info_cmp (const void *p1, const void *p2)
11780 {
11781   const struct file_info *const s1 = (const struct file_info *) p1;
11782   const struct file_info *const s2 = (const struct file_info *) p2;
11783   const unsigned char *cp1;
11784   const unsigned char *cp2;
11785
11786   /* Take care of file names without directories.  We need to make sure that
11787      we return consistent values to qsort since some will get confused if
11788      we return the same value when identical operands are passed in opposite
11789      orders.  So if neither has a directory, return 0 and otherwise return
11790      1 or -1 depending on which one has the directory.  */
11791   if ((s1->path == s1->fname || s2->path == s2->fname))
11792     return (s2->path == s2->fname) - (s1->path == s1->fname);
11793
11794   cp1 = (const unsigned char *) s1->path;
11795   cp2 = (const unsigned char *) s2->path;
11796
11797   while (1)
11798     {
11799       ++cp1;
11800       ++cp2;
11801       /* Reached the end of the first path?  If so, handle like above.  */
11802       if ((cp1 == (const unsigned char *) s1->fname)
11803           || (cp2 == (const unsigned char *) s2->fname))
11804         return ((cp2 == (const unsigned char *) s2->fname)
11805                 - (cp1 == (const unsigned char *) s1->fname));
11806
11807       /* Character of current path component the same?  */
11808       else if (*cp1 != *cp2)
11809         return *cp1 - *cp2;
11810     }
11811 }
11812
11813 struct file_name_acquire_data
11814 {
11815   struct file_info *files;
11816   int used_files;
11817   int max_files;
11818 };
11819
11820 /* Traversal function for the hash table.  */
11821
11822 static int
11823 file_name_acquire (void ** slot, void *data)
11824 {
11825   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11826   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11827   struct file_info *fi;
11828   const char *f;
11829
11830   gcc_assert (fnad->max_files >= d->emitted_number);
11831
11832   if (! d->emitted_number)
11833     return 1;
11834
11835   gcc_assert (fnad->max_files != fnad->used_files);
11836
11837   fi = fnad->files + fnad->used_files++;
11838
11839   /* Skip all leading "./".  */
11840   f = d->filename;
11841   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11842     f += 2;
11843
11844   /* Create a new array entry.  */
11845   fi->path = f;
11846   fi->length = strlen (f);
11847   fi->file_idx = d;
11848
11849   /* Search for the file name part.  */
11850   f = strrchr (f, DIR_SEPARATOR);
11851 #if defined (DIR_SEPARATOR_2)
11852   {
11853     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11854
11855     if (g != NULL)
11856       {
11857         if (f == NULL || f < g)
11858           f = g;
11859       }
11860   }
11861 #endif
11862
11863   fi->fname = f == NULL ? fi->path : f + 1;
11864   return 1;
11865 }
11866
11867 /* Output the directory table and the file name table.  We try to minimize
11868    the total amount of memory needed.  A heuristic is used to avoid large
11869    slowdowns with many input files.  */
11870
11871 static void
11872 output_file_names (void)
11873 {
11874   struct file_name_acquire_data fnad;
11875   int numfiles;
11876   struct file_info *files;
11877   struct dir_info *dirs;
11878   int *saved;
11879   int *savehere;
11880   int *backmap;
11881   int ndirs;
11882   int idx_offset;
11883   int i;
11884
11885   if (!last_emitted_file)
11886     {
11887       dw2_asm_output_data (1, 0, "End directory table");
11888       dw2_asm_output_data (1, 0, "End file name table");
11889       return;
11890     }
11891
11892   numfiles = last_emitted_file->emitted_number;
11893
11894   /* Allocate the various arrays we need.  */
11895   files = XALLOCAVEC (struct file_info, numfiles);
11896   dirs = XALLOCAVEC (struct dir_info, numfiles);
11897
11898   fnad.files = files;
11899   fnad.used_files = 0;
11900   fnad.max_files = numfiles;
11901   htab_traverse (file_table, file_name_acquire, &fnad);
11902   gcc_assert (fnad.used_files == fnad.max_files);
11903
11904   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11905
11906   /* Find all the different directories used.  */
11907   dirs[0].path = files[0].path;
11908   dirs[0].length = files[0].fname - files[0].path;
11909   dirs[0].prefix = -1;
11910   dirs[0].count = 1;
11911   dirs[0].dir_idx = 0;
11912   files[0].dir_idx = 0;
11913   ndirs = 1;
11914
11915   for (i = 1; i < numfiles; i++)
11916     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11917         && memcmp (dirs[ndirs - 1].path, files[i].path,
11918                    dirs[ndirs - 1].length) == 0)
11919       {
11920         /* Same directory as last entry.  */
11921         files[i].dir_idx = ndirs - 1;
11922         ++dirs[ndirs - 1].count;
11923       }
11924     else
11925       {
11926         int j;
11927
11928         /* This is a new directory.  */
11929         dirs[ndirs].path = files[i].path;
11930         dirs[ndirs].length = files[i].fname - files[i].path;
11931         dirs[ndirs].count = 1;
11932         dirs[ndirs].dir_idx = ndirs;
11933         files[i].dir_idx = ndirs;
11934
11935         /* Search for a prefix.  */
11936         dirs[ndirs].prefix = -1;
11937         for (j = 0; j < ndirs; j++)
11938           if (dirs[j].length < dirs[ndirs].length
11939               && dirs[j].length > 1
11940               && (dirs[ndirs].prefix == -1
11941                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11942               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11943             dirs[ndirs].prefix = j;
11944
11945         ++ndirs;
11946       }
11947
11948   /* Now to the actual work.  We have to find a subset of the directories which
11949      allow expressing the file name using references to the directory table
11950      with the least amount of characters.  We do not do an exhaustive search
11951      where we would have to check out every combination of every single
11952      possible prefix.  Instead we use a heuristic which provides nearly optimal
11953      results in most cases and never is much off.  */
11954   saved = XALLOCAVEC (int, ndirs);
11955   savehere = XALLOCAVEC (int, ndirs);
11956
11957   memset (saved, '\0', ndirs * sizeof (saved[0]));
11958   for (i = 0; i < ndirs; i++)
11959     {
11960       int j;
11961       int total;
11962
11963       /* We can always save some space for the current directory.  But this
11964          does not mean it will be enough to justify adding the directory.  */
11965       savehere[i] = dirs[i].length;
11966       total = (savehere[i] - saved[i]) * dirs[i].count;
11967
11968       for (j = i + 1; j < ndirs; j++)
11969         {
11970           savehere[j] = 0;
11971           if (saved[j] < dirs[i].length)
11972             {
11973               /* Determine whether the dirs[i] path is a prefix of the
11974                  dirs[j] path.  */
11975               int k;
11976
11977               k = dirs[j].prefix;
11978               while (k != -1 && k != (int) i)
11979                 k = dirs[k].prefix;
11980
11981               if (k == (int) i)
11982                 {
11983                   /* Yes it is.  We can possibly save some memory by
11984                      writing the filenames in dirs[j] relative to
11985                      dirs[i].  */
11986                   savehere[j] = dirs[i].length;
11987                   total += (savehere[j] - saved[j]) * dirs[j].count;
11988                 }
11989             }
11990         }
11991
11992       /* Check whether we can save enough to justify adding the dirs[i]
11993          directory.  */
11994       if (total > dirs[i].length + 1)
11995         {
11996           /* It's worthwhile adding.  */
11997           for (j = i; j < ndirs; j++)
11998             if (savehere[j] > 0)
11999               {
12000                 /* Remember how much we saved for this directory so far.  */
12001                 saved[j] = savehere[j];
12002
12003                 /* Remember the prefix directory.  */
12004                 dirs[j].dir_idx = i;
12005               }
12006         }
12007     }
12008
12009   /* Emit the directory name table.  */
12010   idx_offset = dirs[0].length > 0 ? 1 : 0;
12011   for (i = 1 - idx_offset; i < ndirs; i++)
12012     dw2_asm_output_nstring (dirs[i].path,
12013                             dirs[i].length
12014                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12015                             "Directory Entry: %#x", i + idx_offset);
12016
12017   dw2_asm_output_data (1, 0, "End directory table");
12018
12019   /* We have to emit them in the order of emitted_number since that's
12020      used in the debug info generation.  To do this efficiently we
12021      generate a back-mapping of the indices first.  */
12022   backmap = XALLOCAVEC (int, numfiles);
12023   for (i = 0; i < numfiles; i++)
12024     backmap[files[i].file_idx->emitted_number - 1] = i;
12025
12026   /* Now write all the file names.  */
12027   for (i = 0; i < numfiles; i++)
12028     {
12029       int file_idx = backmap[i];
12030       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12031
12032 #ifdef VMS_DEBUGGING_INFO
12033 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12034
12035       /* Setting these fields can lead to debugger miscomparisons,
12036          but VMS Debug requires them to be set correctly.  */
12037
12038       int ver;
12039       long long cdt;
12040       long siz;
12041       int maxfilelen = strlen (files[file_idx].path)
12042                                + dirs[dir_idx].length
12043                                + MAX_VMS_VERSION_LEN + 1;
12044       char *filebuf = XALLOCAVEC (char, maxfilelen);
12045
12046       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12047       snprintf (filebuf, maxfilelen, "%s;%d",
12048                 files[file_idx].path + dirs[dir_idx].length, ver);
12049
12050       dw2_asm_output_nstring
12051         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12052
12053       /* Include directory index.  */
12054       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12055
12056       /* Modification time.  */
12057       dw2_asm_output_data_uleb128
12058         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12059           ? cdt : 0,
12060          NULL);
12061
12062       /* File length in bytes.  */
12063       dw2_asm_output_data_uleb128
12064         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12065           ? siz : 0,
12066          NULL);
12067 #else
12068       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12069                               "File Entry: %#x", (unsigned) i + 1);
12070
12071       /* Include directory index.  */
12072       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12073
12074       /* Modification time.  */
12075       dw2_asm_output_data_uleb128 (0, NULL);
12076
12077       /* File length in bytes.  */
12078       dw2_asm_output_data_uleb128 (0, NULL);
12079 #endif /* VMS_DEBUGGING_INFO */
12080     }
12081
12082   dw2_asm_output_data (1, 0, "End file name table");
12083 }
12084
12085
12086 /* Output the source line number correspondence information.  This
12087    information goes into the .debug_line section.  */
12088
12089 static void
12090 output_line_info (void)
12091 {
12092   char l1[20], l2[20], p1[20], p2[20];
12093   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12094   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12095   unsigned opc;
12096   unsigned n_op_args;
12097   unsigned long lt_index;
12098   unsigned long current_line;
12099   long line_offset;
12100   long line_delta;
12101   unsigned long current_file;
12102   unsigned long function;
12103   int ver = dwarf_version;
12104
12105   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12106   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12107   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12108   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12109
12110   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12111     dw2_asm_output_data (4, 0xffffffff,
12112       "Initial length escape value indicating 64-bit DWARF extension");
12113   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12114                         "Length of Source Line Info");
12115   ASM_OUTPUT_LABEL (asm_out_file, l1);
12116
12117   dw2_asm_output_data (2, ver, "DWARF Version");
12118   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12119   ASM_OUTPUT_LABEL (asm_out_file, p1);
12120
12121   /* Define the architecture-dependent minimum instruction length (in
12122    bytes).  In this implementation of DWARF, this field is used for
12123    information purposes only.  Since GCC generates assembly language,
12124    we have no a priori knowledge of how many instruction bytes are
12125    generated for each source line, and therefore can use only the
12126    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12127    commands.  Accordingly, we fix this as `1', which is "correct
12128    enough" for all architectures, and don't let the target override.  */
12129   dw2_asm_output_data (1, 1,
12130                        "Minimum Instruction Length");
12131
12132   if (ver >= 4)
12133     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12134                          "Maximum Operations Per Instruction");
12135   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12136                        "Default is_stmt_start flag");
12137   dw2_asm_output_data (1, DWARF_LINE_BASE,
12138                        "Line Base Value (Special Opcodes)");
12139   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12140                        "Line Range Value (Special Opcodes)");
12141   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12142                        "Special Opcode Base");
12143
12144   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12145     {
12146       switch (opc)
12147         {
12148         case DW_LNS_advance_pc:
12149         case DW_LNS_advance_line:
12150         case DW_LNS_set_file:
12151         case DW_LNS_set_column:
12152         case DW_LNS_fixed_advance_pc:
12153           n_op_args = 1;
12154           break;
12155         default:
12156           n_op_args = 0;
12157           break;
12158         }
12159
12160       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12161                            opc, n_op_args);
12162     }
12163
12164   /* Write out the information about the files we use.  */
12165   output_file_names ();
12166   ASM_OUTPUT_LABEL (asm_out_file, p2);
12167
12168   /* We used to set the address register to the first location in the text
12169      section here, but that didn't accomplish anything since we already
12170      have a line note for the opening brace of the first function.  */
12171
12172   /* Generate the line number to PC correspondence table, encoded as
12173      a series of state machine operations.  */
12174   current_file = 1;
12175   current_line = 1;
12176
12177   if (cfun && in_cold_section_p)
12178     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12179   else
12180     strcpy (prev_line_label, text_section_label);
12181   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12182     {
12183       dw_line_info_ref line_info = &line_info_table[lt_index];
12184
12185 #if 0
12186       /* Disable this optimization for now; GDB wants to see two line notes
12187          at the beginning of a function so it can find the end of the
12188          prologue.  */
12189
12190       /* Don't emit anything for redundant notes.  Just updating the
12191          address doesn't accomplish anything, because we already assume
12192          that anything after the last address is this line.  */
12193       if (line_info->dw_line_num == current_line
12194           && line_info->dw_file_num == current_file)
12195         continue;
12196 #endif
12197
12198       /* Emit debug info for the address of the current line.
12199
12200          Unfortunately, we have little choice here currently, and must always
12201          use the most general form.  GCC does not know the address delta
12202          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12203          attributes which will give an upper bound on the address range.  We
12204          could perhaps use length attributes to determine when it is safe to
12205          use DW_LNS_fixed_advance_pc.  */
12206
12207       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12208       if (0)
12209         {
12210           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12211           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12212                                "DW_LNS_fixed_advance_pc");
12213           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12214         }
12215       else
12216         {
12217           /* This can handle any delta.  This takes
12218              4+DWARF2_ADDR_SIZE bytes.  */
12219           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12220           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12221           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12222           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12223         }
12224
12225       strcpy (prev_line_label, line_label);
12226
12227       /* Emit debug info for the source file of the current line, if
12228          different from the previous line.  */
12229       if (line_info->dw_file_num != current_file)
12230         {
12231           current_file = line_info->dw_file_num;
12232           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12233           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12234         }
12235
12236       /* Emit debug info for the current line number, choosing the encoding
12237          that uses the least amount of space.  */
12238       if (line_info->dw_line_num != current_line)
12239         {
12240           line_offset = line_info->dw_line_num - current_line;
12241           line_delta = line_offset - DWARF_LINE_BASE;
12242           current_line = line_info->dw_line_num;
12243           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12244             /* This can handle deltas from -10 to 234, using the current
12245                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12246                takes 1 byte.  */
12247             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12248                                  "line %lu", current_line);
12249           else
12250             {
12251               /* This can handle any delta.  This takes at least 4 bytes,
12252                  depending on the value being encoded.  */
12253               dw2_asm_output_data (1, DW_LNS_advance_line,
12254                                    "advance to line %lu", current_line);
12255               dw2_asm_output_data_sleb128 (line_offset, NULL);
12256               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12257             }
12258         }
12259       else
12260         /* We still need to start a new row, so output a copy insn.  */
12261         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12262     }
12263
12264   /* Emit debug info for the address of the end of the function.  */
12265   if (0)
12266     {
12267       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12268                            "DW_LNS_fixed_advance_pc");
12269       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12270     }
12271   else
12272     {
12273       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12274       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12275       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12276       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12277     }
12278
12279   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12280   dw2_asm_output_data_uleb128 (1, NULL);
12281   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12282
12283   function = 0;
12284   current_file = 1;
12285   current_line = 1;
12286   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12287     {
12288       dw_separate_line_info_ref line_info
12289         = &separate_line_info_table[lt_index];
12290
12291 #if 0
12292       /* Don't emit anything for redundant notes.  */
12293       if (line_info->dw_line_num == current_line
12294           && line_info->dw_file_num == current_file
12295           && line_info->function == function)
12296         goto cont;
12297 #endif
12298
12299       /* Emit debug info for the address of the current line.  If this is
12300          a new function, or the first line of a function, then we need
12301          to handle it differently.  */
12302       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12303                                    lt_index);
12304       if (function != line_info->function)
12305         {
12306           function = line_info->function;
12307
12308           /* Set the address register to the first line in the function.  */
12309           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12310           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12311           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12312           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12313         }
12314       else
12315         {
12316           /* ??? See the DW_LNS_advance_pc comment above.  */
12317           if (0)
12318             {
12319               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12320                                    "DW_LNS_fixed_advance_pc");
12321               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12322             }
12323           else
12324             {
12325               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12326               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12327               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12328               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12329             }
12330         }
12331
12332       strcpy (prev_line_label, line_label);
12333
12334       /* Emit debug info for the source file of the current line, if
12335          different from the previous line.  */
12336       if (line_info->dw_file_num != current_file)
12337         {
12338           current_file = line_info->dw_file_num;
12339           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12340           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12341         }
12342
12343       /* Emit debug info for the current line number, choosing the encoding
12344          that uses the least amount of space.  */
12345       if (line_info->dw_line_num != current_line)
12346         {
12347           line_offset = line_info->dw_line_num - current_line;
12348           line_delta = line_offset - DWARF_LINE_BASE;
12349           current_line = line_info->dw_line_num;
12350           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12351             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12352                                  "line %lu", current_line);
12353           else
12354             {
12355               dw2_asm_output_data (1, DW_LNS_advance_line,
12356                                    "advance to line %lu", current_line);
12357               dw2_asm_output_data_sleb128 (line_offset, NULL);
12358               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12359             }
12360         }
12361       else
12362         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12363
12364 #if 0
12365     cont:
12366 #endif
12367
12368       lt_index++;
12369
12370       /* If we're done with a function, end its sequence.  */
12371       if (lt_index == separate_line_info_table_in_use
12372           || separate_line_info_table[lt_index].function != function)
12373         {
12374           current_file = 1;
12375           current_line = 1;
12376
12377           /* Emit debug info for the address of the end of the function.  */
12378           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12379           if (0)
12380             {
12381               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12382                                    "DW_LNS_fixed_advance_pc");
12383               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12384             }
12385           else
12386             {
12387               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12388               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12389               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12390               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12391             }
12392
12393           /* Output the marker for the end of this sequence.  */
12394           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12395           dw2_asm_output_data_uleb128 (1, NULL);
12396           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12397         }
12398     }
12399
12400   /* Output the marker for the end of the line number info.  */
12401   ASM_OUTPUT_LABEL (asm_out_file, l2);
12402 }
12403
12404 /* Return the size of the .debug_dcall table for the compilation unit.  */
12405
12406 static unsigned long
12407 size_of_dcall_table (void)
12408 {
12409   unsigned long size;
12410   unsigned int i;
12411   dcall_entry *p;
12412   tree last_poc_decl = NULL;
12413
12414   /* Header:  version + debug info section pointer + pointer size.  */
12415   size = 2 + DWARF_OFFSET_SIZE + 1;
12416
12417   /* Each entry:  code label + DIE offset.  */
12418   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12419     {
12420       gcc_assert (p->targ_die != NULL);
12421       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12422       if (p->poc_decl != last_poc_decl)
12423         {
12424           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12425           gcc_assert (poc_die);
12426           last_poc_decl = p->poc_decl;
12427           if (poc_die)
12428             size += (DWARF_OFFSET_SIZE
12429                      + size_of_uleb128 (poc_die->die_offset));
12430         }
12431       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12432     }
12433
12434   return size;
12435 }
12436
12437 /* Output the direct call table used to disambiguate PC values when
12438    identical function have been merged.  */
12439
12440 static void
12441 output_dcall_table (void)
12442 {
12443   unsigned i;
12444   unsigned long dcall_length = size_of_dcall_table ();
12445   dcall_entry *p;
12446   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12447   tree last_poc_decl = NULL;
12448
12449   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12450     dw2_asm_output_data (4, 0xffffffff,
12451       "Initial length escape value indicating 64-bit DWARF extension");
12452   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12453                        "Length of Direct Call Table");
12454   dw2_asm_output_data (2, 4, "Version number");
12455   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12456                          debug_info_section,
12457                          "Offset of Compilation Unit Info");
12458   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12459
12460   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12461     {
12462       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12463       if (p->poc_decl != last_poc_decl)
12464         {
12465           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12466           last_poc_decl = p->poc_decl;
12467           if (poc_die)
12468             {
12469               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12470               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12471                                            "Caller DIE offset");
12472             }
12473         }
12474       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12475       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12476       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12477                                    "Callee DIE offset");
12478     }
12479 }
12480 \f
12481 /* Return the size of the .debug_vcall table for the compilation unit.  */
12482
12483 static unsigned long
12484 size_of_vcall_table (void)
12485 {
12486   unsigned long size;
12487   unsigned int i;
12488   vcall_entry *p;
12489
12490   /* Header:  version + pointer size.  */
12491   size = 2 + 1;
12492
12493   /* Each entry:  code label + vtable slot index.  */
12494   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12495     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12496
12497   return size;
12498 }
12499
12500 /* Output the virtual call table used to disambiguate PC values when
12501    identical function have been merged.  */
12502
12503 static void
12504 output_vcall_table (void)
12505 {
12506   unsigned i;
12507   unsigned long vcall_length = size_of_vcall_table ();
12508   vcall_entry *p;
12509   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12510
12511   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12512     dw2_asm_output_data (4, 0xffffffff,
12513       "Initial length escape value indicating 64-bit DWARF extension");
12514   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12515                        "Length of Virtual Call Table");
12516   dw2_asm_output_data (2, 4, "Version number");
12517   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12518
12519   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12520     {
12521       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12522       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12523       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12524     }
12525 }
12526 \f
12527 /* Given a pointer to a tree node for some base type, return a pointer to
12528    a DIE that describes the given type.
12529
12530    This routine must only be called for GCC type nodes that correspond to
12531    Dwarf base (fundamental) types.  */
12532
12533 static dw_die_ref
12534 base_type_die (tree type)
12535 {
12536   dw_die_ref base_type_result;
12537   enum dwarf_type encoding;
12538
12539   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12540     return 0;
12541
12542   /* If this is a subtype that should not be emitted as a subrange type,
12543      use the base type.  See subrange_type_for_debug_p.  */
12544   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12545     type = TREE_TYPE (type);
12546
12547   switch (TREE_CODE (type))
12548     {
12549     case INTEGER_TYPE:
12550       if ((dwarf_version >= 4 || !dwarf_strict)
12551           && TYPE_NAME (type)
12552           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12553           && DECL_IS_BUILTIN (TYPE_NAME (type))
12554           && DECL_NAME (TYPE_NAME (type)))
12555         {
12556           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12557           if (strcmp (name, "char16_t") == 0
12558               || strcmp (name, "char32_t") == 0)
12559             {
12560               encoding = DW_ATE_UTF;
12561               break;
12562             }
12563         }
12564       if (TYPE_STRING_FLAG (type))
12565         {
12566           if (TYPE_UNSIGNED (type))
12567             encoding = DW_ATE_unsigned_char;
12568           else
12569             encoding = DW_ATE_signed_char;
12570         }
12571       else if (TYPE_UNSIGNED (type))
12572         encoding = DW_ATE_unsigned;
12573       else
12574         encoding = DW_ATE_signed;
12575       break;
12576
12577     case REAL_TYPE:
12578       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12579         {
12580           if (dwarf_version >= 3 || !dwarf_strict)
12581             encoding = DW_ATE_decimal_float;
12582           else
12583             encoding = DW_ATE_lo_user;
12584         }
12585       else
12586         encoding = DW_ATE_float;
12587       break;
12588
12589     case FIXED_POINT_TYPE:
12590       if (!(dwarf_version >= 3 || !dwarf_strict))
12591         encoding = DW_ATE_lo_user;
12592       else if (TYPE_UNSIGNED (type))
12593         encoding = DW_ATE_unsigned_fixed;
12594       else
12595         encoding = DW_ATE_signed_fixed;
12596       break;
12597
12598       /* Dwarf2 doesn't know anything about complex ints, so use
12599          a user defined type for it.  */
12600     case COMPLEX_TYPE:
12601       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12602         encoding = DW_ATE_complex_float;
12603       else
12604         encoding = DW_ATE_lo_user;
12605       break;
12606
12607     case BOOLEAN_TYPE:
12608       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12609       encoding = DW_ATE_boolean;
12610       break;
12611
12612     default:
12613       /* No other TREE_CODEs are Dwarf fundamental types.  */
12614       gcc_unreachable ();
12615     }
12616
12617   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12618
12619   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12620                    int_size_in_bytes (type));
12621   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12622
12623   return base_type_result;
12624 }
12625
12626 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12627    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12628
12629 static inline int
12630 is_base_type (tree type)
12631 {
12632   switch (TREE_CODE (type))
12633     {
12634     case ERROR_MARK:
12635     case VOID_TYPE:
12636     case INTEGER_TYPE:
12637     case REAL_TYPE:
12638     case FIXED_POINT_TYPE:
12639     case COMPLEX_TYPE:
12640     case BOOLEAN_TYPE:
12641       return 1;
12642
12643     case ARRAY_TYPE:
12644     case RECORD_TYPE:
12645     case UNION_TYPE:
12646     case QUAL_UNION_TYPE:
12647     case ENUMERAL_TYPE:
12648     case FUNCTION_TYPE:
12649     case METHOD_TYPE:
12650     case POINTER_TYPE:
12651     case REFERENCE_TYPE:
12652     case NULLPTR_TYPE:
12653     case OFFSET_TYPE:
12654     case LANG_TYPE:
12655     case VECTOR_TYPE:
12656       return 0;
12657
12658     default:
12659       gcc_unreachable ();
12660     }
12661
12662   return 0;
12663 }
12664
12665 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12666    node, return the size in bits for the type if it is a constant, or else
12667    return the alignment for the type if the type's size is not constant, or
12668    else return BITS_PER_WORD if the type actually turns out to be an
12669    ERROR_MARK node.  */
12670
12671 static inline unsigned HOST_WIDE_INT
12672 simple_type_size_in_bits (const_tree type)
12673 {
12674   if (TREE_CODE (type) == ERROR_MARK)
12675     return BITS_PER_WORD;
12676   else if (TYPE_SIZE (type) == NULL_TREE)
12677     return 0;
12678   else if (host_integerp (TYPE_SIZE (type), 1))
12679     return tree_low_cst (TYPE_SIZE (type), 1);
12680   else
12681     return TYPE_ALIGN (type);
12682 }
12683
12684 /* Similarly, but return a double_int instead of UHWI.  */
12685
12686 static inline double_int
12687 double_int_type_size_in_bits (const_tree type)
12688 {
12689   if (TREE_CODE (type) == ERROR_MARK)
12690     return uhwi_to_double_int (BITS_PER_WORD);
12691   else if (TYPE_SIZE (type) == NULL_TREE)
12692     return double_int_zero;
12693   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12694     return tree_to_double_int (TYPE_SIZE (type));
12695   else
12696     return uhwi_to_double_int (TYPE_ALIGN (type));
12697 }
12698
12699 /*  Given a pointer to a tree node for a subrange type, return a pointer
12700     to a DIE that describes the given type.  */
12701
12702 static dw_die_ref
12703 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12704 {
12705   dw_die_ref subrange_die;
12706   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12707
12708   if (context_die == NULL)
12709     context_die = comp_unit_die ();
12710
12711   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12712
12713   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12714     {
12715       /* The size of the subrange type and its base type do not match,
12716          so we need to generate a size attribute for the subrange type.  */
12717       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12718     }
12719
12720   if (low)
12721     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12722   if (high)
12723     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12724
12725   return subrange_die;
12726 }
12727
12728 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12729    entry that chains various modifiers in front of the given type.  */
12730
12731 static dw_die_ref
12732 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12733                    dw_die_ref context_die)
12734 {
12735   enum tree_code code = TREE_CODE (type);
12736   dw_die_ref mod_type_die;
12737   dw_die_ref sub_die = NULL;
12738   tree item_type = NULL;
12739   tree qualified_type;
12740   tree name, low, high;
12741
12742   if (code == ERROR_MARK)
12743     return NULL;
12744
12745   /* See if we already have the appropriately qualified variant of
12746      this type.  */
12747   qualified_type
12748     = get_qualified_type (type,
12749                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12750                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12751
12752   if (qualified_type == sizetype
12753       && TYPE_NAME (qualified_type)
12754       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12755     {
12756       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12757
12758       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12759                            && TYPE_PRECISION (t)
12760                            == TYPE_PRECISION (qualified_type)
12761                            && TYPE_UNSIGNED (t)
12762                            == TYPE_UNSIGNED (qualified_type));
12763       qualified_type = t;
12764     }
12765
12766   /* If we do, then we can just use its DIE, if it exists.  */
12767   if (qualified_type)
12768     {
12769       mod_type_die = lookup_type_die (qualified_type);
12770       if (mod_type_die)
12771         return mod_type_die;
12772     }
12773
12774   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12775
12776   /* Handle C typedef types.  */
12777   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12778       && !DECL_ARTIFICIAL (name))
12779     {
12780       tree dtype = TREE_TYPE (name);
12781
12782       if (qualified_type == dtype)
12783         {
12784           /* For a named type, use the typedef.  */
12785           gen_type_die (qualified_type, context_die);
12786           return lookup_type_die (qualified_type);
12787         }
12788       else if (is_const_type < TYPE_READONLY (dtype)
12789                || is_volatile_type < TYPE_VOLATILE (dtype)
12790                || (is_const_type <= TYPE_READONLY (dtype)
12791                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12792                    && DECL_ORIGINAL_TYPE (name) != type))
12793         /* cv-unqualified version of named type.  Just use the unnamed
12794            type to which it refers.  */
12795         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12796                                   is_const_type, is_volatile_type,
12797                                   context_die);
12798       /* Else cv-qualified version of named type; fall through.  */
12799     }
12800
12801   if (is_const_type)
12802     {
12803       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12804       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12805     }
12806   else if (is_volatile_type)
12807     {
12808       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12809       sub_die = modified_type_die (type, 0, 0, context_die);
12810     }
12811   else if (code == POINTER_TYPE)
12812     {
12813       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12814       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12815                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12816       item_type = TREE_TYPE (type);
12817       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12818         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12819                          TYPE_ADDR_SPACE (item_type));
12820     }
12821   else if (code == REFERENCE_TYPE)
12822     {
12823       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12824         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12825                                 type);
12826       else
12827         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12828       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12829                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12830       item_type = TREE_TYPE (type);
12831       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12832         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12833                          TYPE_ADDR_SPACE (item_type));
12834     }
12835   else if (code == INTEGER_TYPE
12836            && TREE_TYPE (type) != NULL_TREE
12837            && subrange_type_for_debug_p (type, &low, &high))
12838     {
12839       mod_type_die = subrange_type_die (type, low, high, context_die);
12840       item_type = TREE_TYPE (type);
12841     }
12842   else if (is_base_type (type))
12843     mod_type_die = base_type_die (type);
12844   else
12845     {
12846       gen_type_die (type, context_die);
12847
12848       /* We have to get the type_main_variant here (and pass that to the
12849          `lookup_type_die' routine) because the ..._TYPE node we have
12850          might simply be a *copy* of some original type node (where the
12851          copy was created to help us keep track of typedef names) and
12852          that copy might have a different TYPE_UID from the original
12853          ..._TYPE node.  */
12854       if (TREE_CODE (type) != VECTOR_TYPE)
12855         return lookup_type_die (type_main_variant (type));
12856       else
12857         /* Vectors have the debugging information in the type,
12858            not the main variant.  */
12859         return lookup_type_die (type);
12860     }
12861
12862   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12863      don't output a DW_TAG_typedef, since there isn't one in the
12864      user's program; just attach a DW_AT_name to the type.
12865      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12866      if the base type already has the same name.  */
12867   if (name
12868       && ((TREE_CODE (name) != TYPE_DECL
12869            && (qualified_type == TYPE_MAIN_VARIANT (type)
12870                || (!is_const_type && !is_volatile_type)))
12871           || (TREE_CODE (name) == TYPE_DECL
12872               && TREE_TYPE (name) == qualified_type
12873               && DECL_NAME (name))))
12874     {
12875       if (TREE_CODE (name) == TYPE_DECL)
12876         /* Could just call add_name_and_src_coords_attributes here,
12877            but since this is a builtin type it doesn't have any
12878            useful source coordinates anyway.  */
12879         name = DECL_NAME (name);
12880       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12881     }
12882   /* This probably indicates a bug.  */
12883   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12884     add_name_attribute (mod_type_die, "__unknown__");
12885
12886   if (qualified_type)
12887     equate_type_number_to_die (qualified_type, mod_type_die);
12888
12889   if (item_type)
12890     /* We must do this after the equate_type_number_to_die call, in case
12891        this is a recursive type.  This ensures that the modified_type_die
12892        recursion will terminate even if the type is recursive.  Recursive
12893        types are possible in Ada.  */
12894     sub_die = modified_type_die (item_type,
12895                                  TYPE_READONLY (item_type),
12896                                  TYPE_VOLATILE (item_type),
12897                                  context_die);
12898
12899   if (sub_die != NULL)
12900     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12901
12902   return mod_type_die;
12903 }
12904
12905 /* Generate DIEs for the generic parameters of T.
12906    T must be either a generic type or a generic function.
12907    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12908
12909 static void
12910 gen_generic_params_dies (tree t)
12911 {
12912   tree parms, args;
12913   int parms_num, i;
12914   dw_die_ref die = NULL;
12915
12916   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12917     return;
12918
12919   if (TYPE_P (t))
12920     die = lookup_type_die (t);
12921   else if (DECL_P (t))
12922     die = lookup_decl_die (t);
12923
12924   gcc_assert (die);
12925
12926   parms = lang_hooks.get_innermost_generic_parms (t);
12927   if (!parms)
12928     /* T has no generic parameter. It means T is neither a generic type
12929        or function. End of story.  */
12930     return;
12931
12932   parms_num = TREE_VEC_LENGTH (parms);
12933   args = lang_hooks.get_innermost_generic_args (t);
12934   for (i = 0; i < parms_num; i++)
12935     {
12936       tree parm, arg, arg_pack_elems;
12937
12938       parm = TREE_VEC_ELT (parms, i);
12939       arg = TREE_VEC_ELT (args, i);
12940       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12941       gcc_assert (parm && TREE_VALUE (parm) && arg);
12942
12943       if (parm && TREE_VALUE (parm) && arg)
12944         {
12945           /* If PARM represents a template parameter pack,
12946              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12947              by DW_TAG_template_*_parameter DIEs for the argument
12948              pack elements of ARG. Note that ARG would then be
12949              an argument pack.  */
12950           if (arg_pack_elems)
12951             template_parameter_pack_die (TREE_VALUE (parm),
12952                                          arg_pack_elems,
12953                                          die);
12954           else
12955             generic_parameter_die (TREE_VALUE (parm), arg,
12956                                    true /* Emit DW_AT_name */, die);
12957         }
12958     }
12959 }
12960
12961 /* Create and return a DIE for PARM which should be
12962    the representation of a generic type parameter.
12963    For instance, in the C++ front end, PARM would be a template parameter.
12964    ARG is the argument to PARM.
12965    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12966    name of the PARM.
12967    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12968    as a child node.  */
12969
12970 static dw_die_ref
12971 generic_parameter_die (tree parm, tree arg,
12972                        bool emit_name_p,
12973                        dw_die_ref parent_die)
12974 {
12975   dw_die_ref tmpl_die = NULL;
12976   const char *name = NULL;
12977
12978   if (!parm || !DECL_NAME (parm) || !arg)
12979     return NULL;
12980
12981   /* We support non-type generic parameters and arguments,
12982      type generic parameters and arguments, as well as
12983      generic generic parameters (a.k.a. template template parameters in C++)
12984      and arguments.  */
12985   if (TREE_CODE (parm) == PARM_DECL)
12986     /* PARM is a nontype generic parameter  */
12987     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12988   else if (TREE_CODE (parm) == TYPE_DECL)
12989     /* PARM is a type generic parameter.  */
12990     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12991   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12992     /* PARM is a generic generic parameter.
12993        Its DIE is a GNU extension. It shall have a
12994        DW_AT_name attribute to represent the name of the template template
12995        parameter, and a DW_AT_GNU_template_name attribute to represent the
12996        name of the template template argument.  */
12997     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12998                         parent_die, parm);
12999   else
13000     gcc_unreachable ();
13001
13002   if (tmpl_die)
13003     {
13004       tree tmpl_type;
13005
13006       /* If PARM is a generic parameter pack, it means we are
13007          emitting debug info for a template argument pack element.
13008          In other terms, ARG is a template argument pack element.
13009          In that case, we don't emit any DW_AT_name attribute for
13010          the die.  */
13011       if (emit_name_p)
13012         {
13013           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13014           gcc_assert (name);
13015           add_AT_string (tmpl_die, DW_AT_name, name);
13016         }
13017
13018       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13019         {
13020           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13021              TMPL_DIE should have a child DW_AT_type attribute that is set
13022              to the type of the argument to PARM, which is ARG.
13023              If PARM is a type generic parameter, TMPL_DIE should have a
13024              child DW_AT_type that is set to ARG.  */
13025           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13026           add_type_attribute (tmpl_die, tmpl_type, 0,
13027                               TREE_THIS_VOLATILE (tmpl_type),
13028                               parent_die);
13029         }
13030       else
13031         {
13032           /* So TMPL_DIE is a DIE representing a
13033              a generic generic template parameter, a.k.a template template
13034              parameter in C++ and arg is a template.  */
13035
13036           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13037              to the name of the argument.  */
13038           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13039           if (name)
13040             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13041         }
13042
13043       if (TREE_CODE (parm) == PARM_DECL)
13044         /* So PARM is a non-type generic parameter.
13045            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13046            attribute of TMPL_DIE which value represents the value
13047            of ARG.
13048            We must be careful here:
13049            The value of ARG might reference some function decls.
13050            We might currently be emitting debug info for a generic
13051            type and types are emitted before function decls, we don't
13052            know if the function decls referenced by ARG will actually be
13053            emitted after cgraph computations.
13054            So must defer the generation of the DW_AT_const_value to
13055            after cgraph is ready.  */
13056         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13057     }
13058
13059   return tmpl_die;
13060 }
13061
13062 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13063    PARM_PACK must be a template parameter pack. The returned DIE
13064    will be child DIE of PARENT_DIE.  */
13065
13066 static dw_die_ref
13067 template_parameter_pack_die (tree parm_pack,
13068                              tree parm_pack_args,
13069                              dw_die_ref parent_die)
13070 {
13071   dw_die_ref die;
13072   int j;
13073
13074   gcc_assert (parent_die && parm_pack);
13075
13076   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13077   add_name_and_src_coords_attributes (die, parm_pack);
13078   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13079     generic_parameter_die (parm_pack,
13080                            TREE_VEC_ELT (parm_pack_args, j),
13081                            false /* Don't emit DW_AT_name */,
13082                            die);
13083   return die;
13084 }
13085
13086 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13087    an enumerated type.  */
13088
13089 static inline int
13090 type_is_enum (const_tree type)
13091 {
13092   return TREE_CODE (type) == ENUMERAL_TYPE;
13093 }
13094
13095 /* Return the DBX register number described by a given RTL node.  */
13096
13097 static unsigned int
13098 dbx_reg_number (const_rtx rtl)
13099 {
13100   unsigned regno = REGNO (rtl);
13101
13102   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13103
13104 #ifdef LEAF_REG_REMAP
13105   if (current_function_uses_only_leaf_regs)
13106     {
13107       int leaf_reg = LEAF_REG_REMAP (regno);
13108       if (leaf_reg != -1)
13109         regno = (unsigned) leaf_reg;
13110     }
13111 #endif
13112
13113   return DBX_REGISTER_NUMBER (regno);
13114 }
13115
13116 /* Optionally add a DW_OP_piece term to a location description expression.
13117    DW_OP_piece is only added if the location description expression already
13118    doesn't end with DW_OP_piece.  */
13119
13120 static void
13121 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13122 {
13123   dw_loc_descr_ref loc;
13124
13125   if (*list_head != NULL)
13126     {
13127       /* Find the end of the chain.  */
13128       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13129         ;
13130
13131       if (loc->dw_loc_opc != DW_OP_piece)
13132         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13133     }
13134 }
13135
13136 /* Return a location descriptor that designates a machine register or
13137    zero if there is none.  */
13138
13139 static dw_loc_descr_ref
13140 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13141 {
13142   rtx regs;
13143
13144   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13145     return 0;
13146
13147   /* We only use "frame base" when we're sure we're talking about the
13148      post-prologue local stack frame.  We do this by *not* running
13149      register elimination until this point, and recognizing the special
13150      argument pointer and soft frame pointer rtx's.
13151      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13152   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13153       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13154     {
13155       dw_loc_descr_ref result = NULL;
13156
13157       if (dwarf_version >= 4 || !dwarf_strict)
13158         {
13159           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13160           if (result)
13161             add_loc_descr (&result,
13162                            new_loc_descr (DW_OP_stack_value, 0, 0));
13163         }
13164       return result;
13165     }
13166
13167   regs = targetm.dwarf_register_span (rtl);
13168
13169   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13170     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13171   else
13172     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13173 }
13174
13175 /* Return a location descriptor that designates a machine register for
13176    a given hard register number.  */
13177
13178 static dw_loc_descr_ref
13179 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13180 {
13181   dw_loc_descr_ref reg_loc_descr;
13182
13183   if (regno <= 31)
13184     reg_loc_descr
13185       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13186   else
13187     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13188
13189   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13190     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13191
13192   return reg_loc_descr;
13193 }
13194
13195 /* Given an RTL of a register, return a location descriptor that
13196    designates a value that spans more than one register.  */
13197
13198 static dw_loc_descr_ref
13199 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13200                              enum var_init_status initialized)
13201 {
13202   int nregs, size, i;
13203   unsigned reg;
13204   dw_loc_descr_ref loc_result = NULL;
13205
13206   reg = REGNO (rtl);
13207 #ifdef LEAF_REG_REMAP
13208   if (current_function_uses_only_leaf_regs)
13209     {
13210       int leaf_reg = LEAF_REG_REMAP (reg);
13211       if (leaf_reg != -1)
13212         reg = (unsigned) leaf_reg;
13213     }
13214 #endif
13215   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13216   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13217
13218   /* Simple, contiguous registers.  */
13219   if (regs == NULL_RTX)
13220     {
13221       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13222
13223       loc_result = NULL;
13224       while (nregs--)
13225         {
13226           dw_loc_descr_ref t;
13227
13228           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13229                                       VAR_INIT_STATUS_INITIALIZED);
13230           add_loc_descr (&loc_result, t);
13231           add_loc_descr_op_piece (&loc_result, size);
13232           ++reg;
13233         }
13234       return loc_result;
13235     }
13236
13237   /* Now onto stupid register sets in non contiguous locations.  */
13238
13239   gcc_assert (GET_CODE (regs) == PARALLEL);
13240
13241   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13242   loc_result = NULL;
13243
13244   for (i = 0; i < XVECLEN (regs, 0); ++i)
13245     {
13246       dw_loc_descr_ref t;
13247
13248       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13249                                   VAR_INIT_STATUS_INITIALIZED);
13250       add_loc_descr (&loc_result, t);
13251       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13252       add_loc_descr_op_piece (&loc_result, size);
13253     }
13254
13255   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13256     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13257   return loc_result;
13258 }
13259
13260 /* Return a location descriptor that designates a constant.  */
13261
13262 static dw_loc_descr_ref
13263 int_loc_descriptor (HOST_WIDE_INT i)
13264 {
13265   enum dwarf_location_atom op;
13266
13267   /* Pick the smallest representation of a constant, rather than just
13268      defaulting to the LEB encoding.  */
13269   if (i >= 0)
13270     {
13271       if (i <= 31)
13272         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13273       else if (i <= 0xff)
13274         op = DW_OP_const1u;
13275       else if (i <= 0xffff)
13276         op = DW_OP_const2u;
13277       else if (HOST_BITS_PER_WIDE_INT == 32
13278                || i <= 0xffffffff)
13279         op = DW_OP_const4u;
13280       else
13281         op = DW_OP_constu;
13282     }
13283   else
13284     {
13285       if (i >= -0x80)
13286         op = DW_OP_const1s;
13287       else if (i >= -0x8000)
13288         op = DW_OP_const2s;
13289       else if (HOST_BITS_PER_WIDE_INT == 32
13290                || i >= -0x80000000)
13291         op = DW_OP_const4s;
13292       else
13293         op = DW_OP_consts;
13294     }
13295
13296   return new_loc_descr (op, i, 0);
13297 }
13298
13299 /* Return loc description representing "address" of integer value.
13300    This can appear only as toplevel expression.  */
13301
13302 static dw_loc_descr_ref
13303 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13304 {
13305   int litsize;
13306   dw_loc_descr_ref loc_result = NULL;
13307
13308   if (!(dwarf_version >= 4 || !dwarf_strict))
13309     return NULL;
13310
13311   if (i >= 0)
13312     {
13313       if (i <= 31)
13314         litsize = 1;
13315       else if (i <= 0xff)
13316         litsize = 2;
13317       else if (i <= 0xffff)
13318         litsize = 3;
13319       else if (HOST_BITS_PER_WIDE_INT == 32
13320                || i <= 0xffffffff)
13321         litsize = 5;
13322       else
13323         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13324     }
13325   else
13326     {
13327       if (i >= -0x80)
13328         litsize = 2;
13329       else if (i >= -0x8000)
13330         litsize = 3;
13331       else if (HOST_BITS_PER_WIDE_INT == 32
13332                || i >= -0x80000000)
13333         litsize = 5;
13334       else
13335         litsize = 1 + size_of_sleb128 (i);
13336     }
13337   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13338      is more compact.  For DW_OP_stack_value we need:
13339      litsize + 1 (DW_OP_stack_value)
13340      and for DW_OP_implicit_value:
13341      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13342   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13343     {
13344       loc_result = int_loc_descriptor (i);
13345       add_loc_descr (&loc_result,
13346                      new_loc_descr (DW_OP_stack_value, 0, 0));
13347       return loc_result;
13348     }
13349
13350   loc_result = new_loc_descr (DW_OP_implicit_value,
13351                               size, 0);
13352   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13353   loc_result->dw_loc_oprnd2.v.val_int = i;
13354   return loc_result;
13355 }
13356
13357 /* Return a location descriptor that designates a base+offset location.  */
13358
13359 static dw_loc_descr_ref
13360 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13361                  enum var_init_status initialized)
13362 {
13363   unsigned int regno;
13364   dw_loc_descr_ref result;
13365   dw_fde_ref fde = current_fde ();
13366
13367   /* We only use "frame base" when we're sure we're talking about the
13368      post-prologue local stack frame.  We do this by *not* running
13369      register elimination until this point, and recognizing the special
13370      argument pointer and soft frame pointer rtx's.  */
13371   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13372     {
13373       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13374
13375       if (elim != reg)
13376         {
13377           if (GET_CODE (elim) == PLUS)
13378             {
13379               offset += INTVAL (XEXP (elim, 1));
13380               elim = XEXP (elim, 0);
13381             }
13382           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13383                        && (elim == hard_frame_pointer_rtx
13384                            || elim == stack_pointer_rtx))
13385                       || elim == (frame_pointer_needed
13386                                   ? hard_frame_pointer_rtx
13387                                   : stack_pointer_rtx));
13388
13389           /* If drap register is used to align stack, use frame
13390              pointer + offset to access stack variables.  If stack
13391              is aligned without drap, use stack pointer + offset to
13392              access stack variables.  */
13393           if (crtl->stack_realign_tried
13394               && reg == frame_pointer_rtx)
13395             {
13396               int base_reg
13397                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13398                                       ? HARD_FRAME_POINTER_REGNUM
13399                                       : STACK_POINTER_REGNUM);
13400               return new_reg_loc_descr (base_reg, offset);
13401             }
13402
13403           offset += frame_pointer_fb_offset;
13404           return new_loc_descr (DW_OP_fbreg, offset, 0);
13405         }
13406     }
13407   else if (!optimize
13408            && fde
13409            && (fde->drap_reg == REGNO (reg)
13410                || fde->vdrap_reg == REGNO (reg)))
13411     {
13412       /* Use cfa+offset to represent the location of arguments passed
13413          on the stack when drap is used to align stack.
13414          Only do this when not optimizing, for optimized code var-tracking
13415          is supposed to track where the arguments live and the register
13416          used as vdrap or drap in some spot might be used for something
13417          else in other part of the routine.  */
13418       return new_loc_descr (DW_OP_fbreg, offset, 0);
13419     }
13420
13421   regno = dbx_reg_number (reg);
13422   if (regno <= 31)
13423     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13424                             offset, 0);
13425   else
13426     result = new_loc_descr (DW_OP_bregx, regno, offset);
13427
13428   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13429     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13430
13431   return result;
13432 }
13433
13434 /* Return true if this RTL expression describes a base+offset calculation.  */
13435
13436 static inline int
13437 is_based_loc (const_rtx rtl)
13438 {
13439   return (GET_CODE (rtl) == PLUS
13440           && ((REG_P (XEXP (rtl, 0))
13441                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13442                && CONST_INT_P (XEXP (rtl, 1)))));
13443 }
13444
13445 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13446    failed.  */
13447
13448 static dw_loc_descr_ref
13449 tls_mem_loc_descriptor (rtx mem)
13450 {
13451   tree base;
13452   dw_loc_descr_ref loc_result;
13453
13454   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13455     return NULL;
13456
13457   base = get_base_address (MEM_EXPR (mem));
13458   if (base == NULL
13459       || TREE_CODE (base) != VAR_DECL
13460       || !DECL_THREAD_LOCAL_P (base))
13461     return NULL;
13462
13463   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13464   if (loc_result == NULL)
13465     return NULL;
13466
13467   if (INTVAL (MEM_OFFSET (mem)))
13468     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13469
13470   return loc_result;
13471 }
13472
13473 /* Output debug info about reason why we failed to expand expression as dwarf
13474    expression.  */
13475
13476 static void
13477 expansion_failed (tree expr, rtx rtl, char const *reason)
13478 {
13479   if (dump_file && (dump_flags & TDF_DETAILS))
13480     {
13481       fprintf (dump_file, "Failed to expand as dwarf: ");
13482       if (expr)
13483         print_generic_expr (dump_file, expr, dump_flags);
13484       if (rtl)
13485         {
13486           fprintf (dump_file, "\n");
13487           print_rtl (dump_file, rtl);
13488         }
13489       fprintf (dump_file, "\nReason: %s\n", reason);
13490     }
13491 }
13492
13493 /* Helper function for const_ok_for_output, called either directly
13494    or via for_each_rtx.  */
13495
13496 static int
13497 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13498 {
13499   rtx rtl = *rtlp;
13500
13501   if (GET_CODE (rtl) == UNSPEC)
13502     {
13503       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13504          we can't express it in the debug info.  */
13505 #ifdef ENABLE_CHECKING
13506       /* Don't complain about TLS UNSPECs, those are just too hard to
13507          delegitimize.  */
13508       if (XVECLEN (rtl, 0) != 1
13509           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13510           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13511           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13512           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13513         inform (current_function_decl
13514                 ? DECL_SOURCE_LOCATION (current_function_decl)
13515                 : UNKNOWN_LOCATION,
13516                 "non-delegitimized UNSPEC %d found in variable location",
13517                 XINT (rtl, 1));
13518 #endif
13519       expansion_failed (NULL_TREE, rtl,
13520                         "UNSPEC hasn't been delegitimized.\n");
13521       return 1;
13522     }
13523
13524   if (GET_CODE (rtl) != SYMBOL_REF)
13525     return 0;
13526
13527   if (CONSTANT_POOL_ADDRESS_P (rtl))
13528     {
13529       bool marked;
13530       get_pool_constant_mark (rtl, &marked);
13531       /* If all references to this pool constant were optimized away,
13532          it was not output and thus we can't represent it.  */
13533       if (!marked)
13534         {
13535           expansion_failed (NULL_TREE, rtl,
13536                             "Constant was removed from constant pool.\n");
13537           return 1;
13538         }
13539     }
13540
13541   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13542     return 1;
13543
13544   /* Avoid references to external symbols in debug info, on several targets
13545      the linker might even refuse to link when linking a shared library,
13546      and in many other cases the relocations for .debug_info/.debug_loc are
13547      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13548      to be defined within the same shared library or executable are fine.  */
13549   if (SYMBOL_REF_EXTERNAL_P (rtl))
13550     {
13551       tree decl = SYMBOL_REF_DECL (rtl);
13552
13553       if (decl == NULL || !targetm.binds_local_p (decl))
13554         {
13555           expansion_failed (NULL_TREE, rtl,
13556                             "Symbol not defined in current TU.\n");
13557           return 1;
13558         }
13559     }
13560
13561   return 0;
13562 }
13563
13564 /* Return true if constant RTL can be emitted in DW_OP_addr or
13565    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13566    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13567
13568 static bool
13569 const_ok_for_output (rtx rtl)
13570 {
13571   if (GET_CODE (rtl) == SYMBOL_REF)
13572     return const_ok_for_output_1 (&rtl, NULL) == 0;
13573
13574   if (GET_CODE (rtl) == CONST)
13575     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13576
13577   return true;
13578 }
13579
13580 /* The following routine converts the RTL for a variable or parameter
13581    (resident in memory) into an equivalent Dwarf representation of a
13582    mechanism for getting the address of that same variable onto the top of a
13583    hypothetical "address evaluation" stack.
13584
13585    When creating memory location descriptors, we are effectively transforming
13586    the RTL for a memory-resident object into its Dwarf postfix expression
13587    equivalent.  This routine recursively descends an RTL tree, turning
13588    it into Dwarf postfix code as it goes.
13589
13590    MODE is the mode of the memory reference, needed to handle some
13591    autoincrement addressing modes.
13592
13593    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13594    location list for RTL.
13595
13596    Return 0 if we can't represent the location.  */
13597
13598 static dw_loc_descr_ref
13599 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13600                     enum var_init_status initialized)
13601 {
13602   dw_loc_descr_ref mem_loc_result = NULL;
13603   enum dwarf_location_atom op;
13604   dw_loc_descr_ref op0, op1;
13605
13606   /* Note that for a dynamically sized array, the location we will generate a
13607      description of here will be the lowest numbered location which is
13608      actually within the array.  That's *not* necessarily the same as the
13609      zeroth element of the array.  */
13610
13611   rtl = targetm.delegitimize_address (rtl);
13612
13613   switch (GET_CODE (rtl))
13614     {
13615     case POST_INC:
13616     case POST_DEC:
13617     case POST_MODIFY:
13618       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13619
13620     case SUBREG:
13621       /* The case of a subreg may arise when we have a local (register)
13622          variable or a formal (register) parameter which doesn't quite fill
13623          up an entire register.  For now, just assume that it is
13624          legitimate to make the Dwarf info refer to the whole register which
13625          contains the given subreg.  */
13626       if (!subreg_lowpart_p (rtl))
13627         break;
13628       rtl = SUBREG_REG (rtl);
13629       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13630         break;
13631       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13632         break;
13633       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13634       break;
13635
13636     case REG:
13637       /* Whenever a register number forms a part of the description of the
13638          method for calculating the (dynamic) address of a memory resident
13639          object, DWARF rules require the register number be referred to as
13640          a "base register".  This distinction is not based in any way upon
13641          what category of register the hardware believes the given register
13642          belongs to.  This is strictly DWARF terminology we're dealing with
13643          here. Note that in cases where the location of a memory-resident
13644          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13645          OP_CONST (0)) the actual DWARF location descriptor that we generate
13646          may just be OP_BASEREG (basereg).  This may look deceptively like
13647          the object in question was allocated to a register (rather than in
13648          memory) so DWARF consumers need to be aware of the subtle
13649          distinction between OP_REG and OP_BASEREG.  */
13650       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13651         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13652       else if (stack_realign_drap
13653                && crtl->drap_reg
13654                && crtl->args.internal_arg_pointer == rtl
13655                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13656         {
13657           /* If RTL is internal_arg_pointer, which has been optimized
13658              out, use DRAP instead.  */
13659           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13660                                             VAR_INIT_STATUS_INITIALIZED);
13661         }
13662       break;
13663
13664     case SIGN_EXTEND:
13665     case ZERO_EXTEND:
13666       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13667                                 VAR_INIT_STATUS_INITIALIZED);
13668       if (op0 == 0)
13669         break;
13670       else
13671         {
13672           int shift = DWARF2_ADDR_SIZE
13673                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13674           shift *= BITS_PER_UNIT;
13675           if (GET_CODE (rtl) == SIGN_EXTEND)
13676             op = DW_OP_shra;
13677           else
13678             op = DW_OP_shr;
13679           mem_loc_result = op0;
13680           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13681           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13682           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13683           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13684         }
13685       break;
13686
13687     case MEM:
13688       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13689                                            VAR_INIT_STATUS_INITIALIZED);
13690       if (mem_loc_result == NULL)
13691         mem_loc_result = tls_mem_loc_descriptor (rtl);
13692       if (mem_loc_result != 0)
13693         {
13694           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13695             {
13696               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13697               return 0;
13698             }
13699           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13700             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13701           else
13702             add_loc_descr (&mem_loc_result,
13703                            new_loc_descr (DW_OP_deref_size,
13704                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13705         }
13706       else
13707         {
13708           rtx new_rtl = avoid_constant_pool_reference (rtl);
13709           if (new_rtl != rtl)
13710             return mem_loc_descriptor (new_rtl, mode, initialized);
13711         }
13712       break;
13713
13714     case LO_SUM:
13715          rtl = XEXP (rtl, 1);
13716
13717       /* ... fall through ...  */
13718
13719     case LABEL_REF:
13720       /* Some ports can transform a symbol ref into a label ref, because
13721          the symbol ref is too far away and has to be dumped into a constant
13722          pool.  */
13723     case CONST:
13724     case SYMBOL_REF:
13725       if (GET_CODE (rtl) == SYMBOL_REF
13726           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13727         {
13728           dw_loc_descr_ref temp;
13729
13730           /* If this is not defined, we have no way to emit the data.  */
13731           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13732             break;
13733
13734           /* We used to emit DW_OP_addr here, but that's wrong, since
13735              DW_OP_addr should be relocated by the debug info consumer,
13736              while DW_OP_GNU_push_tls_address operand should not.  */
13737           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13738                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13739           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13740           temp->dw_loc_oprnd1.v.val_addr = rtl;
13741           temp->dtprel = true;
13742
13743           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13744           add_loc_descr (&mem_loc_result, temp);
13745
13746           break;
13747         }
13748
13749       if (!const_ok_for_output (rtl))
13750         break;
13751
13752     symref:
13753       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13754       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13755       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13756       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13757       break;
13758
13759     case CONCAT:
13760     case CONCATN:
13761     case VAR_LOCATION:
13762     case DEBUG_IMPLICIT_PTR:
13763       expansion_failed (NULL_TREE, rtl,
13764                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13765       return 0;
13766
13767     case PRE_MODIFY:
13768       /* Extract the PLUS expression nested inside and fall into
13769          PLUS code below.  */
13770       rtl = XEXP (rtl, 1);
13771       goto plus;
13772
13773     case PRE_INC:
13774     case PRE_DEC:
13775       /* Turn these into a PLUS expression and fall into the PLUS code
13776          below.  */
13777       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13778                           GEN_INT (GET_CODE (rtl) == PRE_INC
13779                                    ? GET_MODE_UNIT_SIZE (mode)
13780                                    : -GET_MODE_UNIT_SIZE (mode)));
13781
13782       /* ... fall through ...  */
13783
13784     case PLUS:
13785     plus:
13786       if (is_based_loc (rtl))
13787         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13788                                           INTVAL (XEXP (rtl, 1)),
13789                                           VAR_INIT_STATUS_INITIALIZED);
13790       else
13791         {
13792           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13793                                                VAR_INIT_STATUS_INITIALIZED);
13794           if (mem_loc_result == 0)
13795             break;
13796
13797           if (CONST_INT_P (XEXP (rtl, 1)))
13798             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13799           else
13800             {
13801               dw_loc_descr_ref mem_loc_result2
13802                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13803                                       VAR_INIT_STATUS_INITIALIZED);
13804               if (mem_loc_result2 == 0)
13805                 break;
13806               add_loc_descr (&mem_loc_result, mem_loc_result2);
13807               add_loc_descr (&mem_loc_result,
13808                              new_loc_descr (DW_OP_plus, 0, 0));
13809             }
13810         }
13811       break;
13812
13813     /* If a pseudo-reg is optimized away, it is possible for it to
13814        be replaced with a MEM containing a multiply or shift.  */
13815     case MINUS:
13816       op = DW_OP_minus;
13817       goto do_binop;
13818
13819     case MULT:
13820       op = DW_OP_mul;
13821       goto do_binop;
13822
13823     case DIV:
13824       op = DW_OP_div;
13825       goto do_binop;
13826
13827     case UMOD:
13828       op = DW_OP_mod;
13829       goto do_binop;
13830
13831     case ASHIFT:
13832       op = DW_OP_shl;
13833       goto do_binop;
13834
13835     case ASHIFTRT:
13836       op = DW_OP_shra;
13837       goto do_binop;
13838
13839     case LSHIFTRT:
13840       op = DW_OP_shr;
13841       goto do_binop;
13842
13843     case AND:
13844       op = DW_OP_and;
13845       goto do_binop;
13846
13847     case IOR:
13848       op = DW_OP_or;
13849       goto do_binop;
13850
13851     case XOR:
13852       op = DW_OP_xor;
13853       goto do_binop;
13854
13855     do_binop:
13856       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13857                                 VAR_INIT_STATUS_INITIALIZED);
13858       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13859                                 VAR_INIT_STATUS_INITIALIZED);
13860
13861       if (op0 == 0 || op1 == 0)
13862         break;
13863
13864       mem_loc_result = op0;
13865       add_loc_descr (&mem_loc_result, op1);
13866       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13867       break;
13868
13869     case MOD:
13870       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13871                                 VAR_INIT_STATUS_INITIALIZED);
13872       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13873                                 VAR_INIT_STATUS_INITIALIZED);
13874
13875       if (op0 == 0 || op1 == 0)
13876         break;
13877
13878       mem_loc_result = op0;
13879       add_loc_descr (&mem_loc_result, op1);
13880       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13881       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13882       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13883       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13884       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13885       break;
13886
13887     case NOT:
13888       op = DW_OP_not;
13889       goto do_unop;
13890
13891     case ABS:
13892       op = DW_OP_abs;
13893       goto do_unop;
13894
13895     case NEG:
13896       op = DW_OP_neg;
13897       goto do_unop;
13898
13899     do_unop:
13900       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13901                                 VAR_INIT_STATUS_INITIALIZED);
13902
13903       if (op0 == 0)
13904         break;
13905
13906       mem_loc_result = op0;
13907       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13908       break;
13909
13910     case CONST_INT:
13911       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13912       break;
13913
13914     case EQ:
13915       op = DW_OP_eq;
13916       goto do_scompare;
13917
13918     case GE:
13919       op = DW_OP_ge;
13920       goto do_scompare;
13921
13922     case GT:
13923       op = DW_OP_gt;
13924       goto do_scompare;
13925
13926     case LE:
13927       op = DW_OP_le;
13928       goto do_scompare;
13929
13930     case LT:
13931       op = DW_OP_lt;
13932       goto do_scompare;
13933
13934     case NE:
13935       op = DW_OP_ne;
13936       goto do_scompare;
13937
13938     do_scompare:
13939       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13940           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13941         break;
13942       else
13943         {
13944           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13945
13946           if (op_mode == VOIDmode)
13947             op_mode = GET_MODE (XEXP (rtl, 1));
13948           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13949             break;
13950
13951           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13952                                     VAR_INIT_STATUS_INITIALIZED);
13953           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13954                                     VAR_INIT_STATUS_INITIALIZED);
13955
13956           if (op0 == 0 || op1 == 0)
13957             break;
13958
13959           if (op_mode != VOIDmode
13960               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13961             {
13962               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13963               shift *= BITS_PER_UNIT;
13964               /* For eq/ne, if the operands are known to be zero-extended,
13965                  there is no need to do the fancy shifting up.  */
13966               if (op == DW_OP_eq || op == DW_OP_ne)
13967                 {
13968                   dw_loc_descr_ref last0, last1;
13969                   for (last0 = op0;
13970                        last0->dw_loc_next != NULL;
13971                        last0 = last0->dw_loc_next)
13972                     ;
13973                   for (last1 = op1;
13974                        last1->dw_loc_next != NULL;
13975                        last1 = last1->dw_loc_next)
13976                     ;
13977                   /* deref_size zero extends, and for constants we can check
13978                      whether they are zero extended or not.  */
13979                   if (((last0->dw_loc_opc == DW_OP_deref_size
13980                         && last0->dw_loc_oprnd1.v.val_int
13981                            <= GET_MODE_SIZE (op_mode))
13982                        || (CONST_INT_P (XEXP (rtl, 0))
13983                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13984                                == (INTVAL (XEXP (rtl, 0))
13985                                    & GET_MODE_MASK (op_mode))))
13986                       && ((last1->dw_loc_opc == DW_OP_deref_size
13987                            && last1->dw_loc_oprnd1.v.val_int
13988                               <= GET_MODE_SIZE (op_mode))
13989                           || (CONST_INT_P (XEXP (rtl, 1))
13990                               && (unsigned HOST_WIDE_INT)
13991                                  INTVAL (XEXP (rtl, 1))
13992                                  == (INTVAL (XEXP (rtl, 1))
13993                                      & GET_MODE_MASK (op_mode)))))
13994                     goto do_compare;
13995                 }
13996               add_loc_descr (&op0, int_loc_descriptor (shift));
13997               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13998               if (CONST_INT_P (XEXP (rtl, 1)))
13999                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14000               else
14001                 {
14002                   add_loc_descr (&op1, int_loc_descriptor (shift));
14003                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14004                 }
14005             }
14006         }
14007
14008     do_compare:
14009       mem_loc_result = op0;
14010       add_loc_descr (&mem_loc_result, op1);
14011       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14012       if (STORE_FLAG_VALUE != 1)
14013         {
14014           add_loc_descr (&mem_loc_result,
14015                          int_loc_descriptor (STORE_FLAG_VALUE));
14016           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14017         }
14018       break;
14019
14020     case GEU:
14021       op = DW_OP_ge;
14022       goto do_ucompare;
14023
14024     case GTU:
14025       op = DW_OP_gt;
14026       goto do_ucompare;
14027
14028     case LEU:
14029       op = DW_OP_le;
14030       goto do_ucompare;
14031
14032     case LTU:
14033       op = DW_OP_lt;
14034       goto do_ucompare;
14035
14036     do_ucompare:
14037       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14038           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14039         break;
14040       else
14041         {
14042           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14043
14044           if (op_mode == VOIDmode)
14045             op_mode = GET_MODE (XEXP (rtl, 1));
14046           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14047             break;
14048
14049           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14050                                     VAR_INIT_STATUS_INITIALIZED);
14051           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14052                                     VAR_INIT_STATUS_INITIALIZED);
14053
14054           if (op0 == 0 || op1 == 0)
14055             break;
14056
14057           if (op_mode != VOIDmode
14058               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14059             {
14060               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14061               dw_loc_descr_ref last0, last1;
14062               for (last0 = op0;
14063                    last0->dw_loc_next != NULL;
14064                    last0 = last0->dw_loc_next)
14065                 ;
14066               for (last1 = op1;
14067                    last1->dw_loc_next != NULL;
14068                    last1 = last1->dw_loc_next)
14069                 ;
14070               if (CONST_INT_P (XEXP (rtl, 0)))
14071                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14072               /* deref_size zero extends, so no need to mask it again.  */
14073               else if (last0->dw_loc_opc != DW_OP_deref_size
14074                        || last0->dw_loc_oprnd1.v.val_int
14075                           > GET_MODE_SIZE (op_mode))
14076                 {
14077                   add_loc_descr (&op0, int_loc_descriptor (mask));
14078                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14079                 }
14080               if (CONST_INT_P (XEXP (rtl, 1)))
14081                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14082               /* deref_size zero extends, so no need to mask it again.  */
14083               else if (last1->dw_loc_opc != DW_OP_deref_size
14084                        || last1->dw_loc_oprnd1.v.val_int
14085                           > GET_MODE_SIZE (op_mode))
14086                 {
14087                   add_loc_descr (&op1, int_loc_descriptor (mask));
14088                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14089                 }
14090             }
14091           else
14092             {
14093               HOST_WIDE_INT bias = 1;
14094               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14095               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14096               if (CONST_INT_P (XEXP (rtl, 1)))
14097                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14098                                           + INTVAL (XEXP (rtl, 1)));
14099               else
14100                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14101                                                     bias, 0));
14102             }
14103         }
14104       goto do_compare;
14105
14106     case SMIN:
14107     case SMAX:
14108     case UMIN:
14109     case UMAX:
14110       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14111           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14112           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14113         break;
14114
14115       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14116                                 VAR_INIT_STATUS_INITIALIZED);
14117       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14118                                 VAR_INIT_STATUS_INITIALIZED);
14119
14120       if (op0 == 0 || op1 == 0)
14121         break;
14122
14123       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14124       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14125       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14126       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14127         {
14128           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14129             {
14130               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14131               add_loc_descr (&op0, int_loc_descriptor (mask));
14132               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14133               add_loc_descr (&op1, int_loc_descriptor (mask));
14134               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14135             }
14136           else
14137             {
14138               HOST_WIDE_INT bias = 1;
14139               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14140               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14141               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14142             }
14143         }
14144       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14145         {
14146           int shift = DWARF2_ADDR_SIZE
14147                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14148           shift *= BITS_PER_UNIT;
14149           add_loc_descr (&op0, int_loc_descriptor (shift));
14150           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14151           add_loc_descr (&op1, int_loc_descriptor (shift));
14152           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14153         }
14154
14155       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14156         op = DW_OP_lt;
14157       else
14158         op = DW_OP_gt;
14159       mem_loc_result = op0;
14160       add_loc_descr (&mem_loc_result, op1);
14161       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14162       {
14163         dw_loc_descr_ref bra_node, drop_node;
14164
14165         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14166         add_loc_descr (&mem_loc_result, bra_node);
14167         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14168         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14169         add_loc_descr (&mem_loc_result, drop_node);
14170         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14171         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14172       }
14173       break;
14174
14175     case ZERO_EXTRACT:
14176     case SIGN_EXTRACT:
14177       if (CONST_INT_P (XEXP (rtl, 1))
14178           && CONST_INT_P (XEXP (rtl, 2))
14179           && ((unsigned) INTVAL (XEXP (rtl, 1))
14180               + (unsigned) INTVAL (XEXP (rtl, 2))
14181               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14182           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14183           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14184         {
14185           int shift, size;
14186           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14187                                     VAR_INIT_STATUS_INITIALIZED);
14188           if (op0 == 0)
14189             break;
14190           if (GET_CODE (rtl) == SIGN_EXTRACT)
14191             op = DW_OP_shra;
14192           else
14193             op = DW_OP_shr;
14194           mem_loc_result = op0;
14195           size = INTVAL (XEXP (rtl, 1));
14196           shift = INTVAL (XEXP (rtl, 2));
14197           if (BITS_BIG_ENDIAN)
14198             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14199                     - shift - size;
14200           if (shift + size != (int) DWARF2_ADDR_SIZE)
14201             {
14202               add_loc_descr (&mem_loc_result,
14203                              int_loc_descriptor (DWARF2_ADDR_SIZE
14204                                                  - shift - size));
14205               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14206             }
14207           if (size != (int) DWARF2_ADDR_SIZE)
14208             {
14209               add_loc_descr (&mem_loc_result,
14210                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14211               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14212             }
14213         }
14214       break;
14215
14216     case IF_THEN_ELSE:
14217       {
14218         dw_loc_descr_ref op2, bra_node, drop_node;
14219         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14220                                   VAR_INIT_STATUS_INITIALIZED);
14221         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14222                                   VAR_INIT_STATUS_INITIALIZED);
14223         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14224                                   VAR_INIT_STATUS_INITIALIZED);
14225         if (op0 == NULL || op1 == NULL || op2 == NULL)
14226           break;
14227
14228         mem_loc_result = op1;
14229         add_loc_descr (&mem_loc_result, op2);
14230         add_loc_descr (&mem_loc_result, op0);
14231         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14232         add_loc_descr (&mem_loc_result, bra_node);
14233         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14234         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14235         add_loc_descr (&mem_loc_result, drop_node);
14236         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14237         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14238       }
14239       break;
14240
14241     case COMPARE:
14242     case ROTATE:
14243     case ROTATERT:
14244     case TRUNCATE:
14245       /* In theory, we could implement the above.  */
14246       /* DWARF cannot represent the unsigned compare operations
14247          natively.  */
14248     case SS_MULT:
14249     case US_MULT:
14250     case SS_DIV:
14251     case US_DIV:
14252     case SS_PLUS:
14253     case US_PLUS:
14254     case SS_MINUS:
14255     case US_MINUS:
14256     case SS_NEG:
14257     case US_NEG:
14258     case SS_ABS:
14259     case SS_ASHIFT:
14260     case US_ASHIFT:
14261     case SS_TRUNCATE:
14262     case US_TRUNCATE:
14263     case UDIV:
14264     case UNORDERED:
14265     case ORDERED:
14266     case UNEQ:
14267     case UNGE:
14268     case UNGT:
14269     case UNLE:
14270     case UNLT:
14271     case LTGT:
14272     case FLOAT_EXTEND:
14273     case FLOAT_TRUNCATE:
14274     case FLOAT:
14275     case UNSIGNED_FLOAT:
14276     case FIX:
14277     case UNSIGNED_FIX:
14278     case FRACT_CONVERT:
14279     case UNSIGNED_FRACT_CONVERT:
14280     case SAT_FRACT:
14281     case UNSIGNED_SAT_FRACT:
14282     case SQRT:
14283     case BSWAP:
14284     case FFS:
14285     case CLZ:
14286     case CTZ:
14287     case POPCOUNT:
14288     case PARITY:
14289     case ASM_OPERANDS:
14290     case VEC_MERGE:
14291     case VEC_SELECT:
14292     case VEC_CONCAT:
14293     case VEC_DUPLICATE:
14294     case UNSPEC:
14295     case HIGH:
14296       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14297          can't express it in the debug info.  This can happen e.g. with some
14298          TLS UNSPECs.  */
14299       break;
14300
14301     case CONST_STRING:
14302       resolve_one_addr (&rtl, NULL);
14303       goto symref;
14304
14305     default:
14306 #ifdef ENABLE_CHECKING
14307       print_rtl (stderr, rtl);
14308       gcc_unreachable ();
14309 #else
14310       break;
14311 #endif
14312     }
14313
14314   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14315     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14316
14317   return mem_loc_result;
14318 }
14319
14320 /* Return a descriptor that describes the concatenation of two locations.
14321    This is typically a complex variable.  */
14322
14323 static dw_loc_descr_ref
14324 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14325 {
14326   dw_loc_descr_ref cc_loc_result = NULL;
14327   dw_loc_descr_ref x0_ref
14328     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14329   dw_loc_descr_ref x1_ref
14330     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14331
14332   if (x0_ref == 0 || x1_ref == 0)
14333     return 0;
14334
14335   cc_loc_result = x0_ref;
14336   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14337
14338   add_loc_descr (&cc_loc_result, x1_ref);
14339   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14340
14341   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14342     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14343
14344   return cc_loc_result;
14345 }
14346
14347 /* Return a descriptor that describes the concatenation of N
14348    locations.  */
14349
14350 static dw_loc_descr_ref
14351 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14352 {
14353   unsigned int i;
14354   dw_loc_descr_ref cc_loc_result = NULL;
14355   unsigned int n = XVECLEN (concatn, 0);
14356
14357   for (i = 0; i < n; ++i)
14358     {
14359       dw_loc_descr_ref ref;
14360       rtx x = XVECEXP (concatn, 0, i);
14361
14362       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14363       if (ref == NULL)
14364         return NULL;
14365
14366       add_loc_descr (&cc_loc_result, ref);
14367       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14368     }
14369
14370   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14371     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14372
14373   return cc_loc_result;
14374 }
14375
14376 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14377    for DEBUG_IMPLICIT_PTR RTL.  */
14378
14379 static dw_loc_descr_ref
14380 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14381 {
14382   dw_loc_descr_ref ret;
14383   dw_die_ref ref;
14384
14385   if (dwarf_strict)
14386     return NULL;
14387   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14388               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14389               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14390   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14391   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14392   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14393   if (ref)
14394     {
14395       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14396       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14397       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14398     }
14399   else
14400     {
14401       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14402       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14403     }
14404   return ret;
14405 }
14406
14407 /* Output a proper Dwarf location descriptor for a variable or parameter
14408    which is either allocated in a register or in a memory location.  For a
14409    register, we just generate an OP_REG and the register number.  For a
14410    memory location we provide a Dwarf postfix expression describing how to
14411    generate the (dynamic) address of the object onto the address stack.
14412
14413    MODE is mode of the decl if this loc_descriptor is going to be used in
14414    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14415    allowed, VOIDmode otherwise.
14416
14417    If we don't know how to describe it, return 0.  */
14418
14419 static dw_loc_descr_ref
14420 loc_descriptor (rtx rtl, enum machine_mode mode,
14421                 enum var_init_status initialized)
14422 {
14423   dw_loc_descr_ref loc_result = NULL;
14424
14425   switch (GET_CODE (rtl))
14426     {
14427     case SUBREG:
14428       /* The case of a subreg may arise when we have a local (register)
14429          variable or a formal (register) parameter which doesn't quite fill
14430          up an entire register.  For now, just assume that it is
14431          legitimate to make the Dwarf info refer to the whole register which
14432          contains the given subreg.  */
14433       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14434       break;
14435
14436     case REG:
14437       loc_result = reg_loc_descriptor (rtl, initialized);
14438       break;
14439
14440     case MEM:
14441       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14442                                        initialized);
14443       if (loc_result == NULL)
14444         loc_result = tls_mem_loc_descriptor (rtl);
14445       if (loc_result == NULL)
14446         {
14447           rtx new_rtl = avoid_constant_pool_reference (rtl);
14448           if (new_rtl != rtl)
14449             loc_result = loc_descriptor (new_rtl, mode, initialized);
14450         }
14451       break;
14452
14453     case CONCAT:
14454       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14455                                           initialized);
14456       break;
14457
14458     case CONCATN:
14459       loc_result = concatn_loc_descriptor (rtl, initialized);
14460       break;
14461
14462     case VAR_LOCATION:
14463       /* Single part.  */
14464       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14465         {
14466           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14467           if (GET_CODE (loc) == EXPR_LIST)
14468             loc = XEXP (loc, 0);
14469           loc_result = loc_descriptor (loc, mode, initialized);
14470           break;
14471         }
14472
14473       rtl = XEXP (rtl, 1);
14474       /* FALLTHRU */
14475
14476     case PARALLEL:
14477       {
14478         rtvec par_elems = XVEC (rtl, 0);
14479         int num_elem = GET_NUM_ELEM (par_elems);
14480         enum machine_mode mode;
14481         int i;
14482
14483         /* Create the first one, so we have something to add to.  */
14484         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14485                                      VOIDmode, initialized);
14486         if (loc_result == NULL)
14487           return NULL;
14488         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14489         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14490         for (i = 1; i < num_elem; i++)
14491           {
14492             dw_loc_descr_ref temp;
14493
14494             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14495                                    VOIDmode, initialized);
14496             if (temp == NULL)
14497               return NULL;
14498             add_loc_descr (&loc_result, temp);
14499             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14500             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14501           }
14502       }
14503       break;
14504
14505     case CONST_INT:
14506       if (mode != VOIDmode && mode != BLKmode)
14507         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14508                                                     INTVAL (rtl));
14509       break;
14510
14511     case CONST_DOUBLE:
14512       if (mode == VOIDmode)
14513         mode = GET_MODE (rtl);
14514
14515       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14516         {
14517           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14518
14519           /* Note that a CONST_DOUBLE rtx could represent either an integer
14520              or a floating-point constant.  A CONST_DOUBLE is used whenever
14521              the constant requires more than one word in order to be
14522              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14523           loc_result = new_loc_descr (DW_OP_implicit_value,
14524                                       GET_MODE_SIZE (mode), 0);
14525           if (SCALAR_FLOAT_MODE_P (mode))
14526             {
14527               unsigned int length = GET_MODE_SIZE (mode);
14528               unsigned char *array
14529                   = (unsigned char*) ggc_alloc_atomic (length);
14530
14531               insert_float (rtl, array);
14532               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14533               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14534               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14535               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14536             }
14537           else
14538             {
14539               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14540               loc_result->dw_loc_oprnd2.v.val_double
14541                 = rtx_to_double_int (rtl);
14542             }
14543         }
14544       break;
14545
14546     case CONST_VECTOR:
14547       if (mode == VOIDmode)
14548         mode = GET_MODE (rtl);
14549
14550       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14551         {
14552           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14553           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14554           unsigned char *array = (unsigned char *)
14555             ggc_alloc_atomic (length * elt_size);
14556           unsigned int i;
14557           unsigned char *p;
14558
14559           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14560           switch (GET_MODE_CLASS (mode))
14561             {
14562             case MODE_VECTOR_INT:
14563               for (i = 0, p = array; i < length; i++, p += elt_size)
14564                 {
14565                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14566                   double_int val = rtx_to_double_int (elt);
14567
14568                   if (elt_size <= sizeof (HOST_WIDE_INT))
14569                     insert_int (double_int_to_shwi (val), elt_size, p);
14570                   else
14571                     {
14572                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14573                       insert_double (val, p);
14574                     }
14575                 }
14576               break;
14577
14578             case MODE_VECTOR_FLOAT:
14579               for (i = 0, p = array; i < length; i++, p += elt_size)
14580                 {
14581                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14582                   insert_float (elt, p);
14583                 }
14584               break;
14585
14586             default:
14587               gcc_unreachable ();
14588             }
14589
14590           loc_result = new_loc_descr (DW_OP_implicit_value,
14591                                       length * elt_size, 0);
14592           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14593           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14594           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14595           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14596         }
14597       break;
14598
14599     case CONST:
14600       if (mode == VOIDmode
14601           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14602           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14603           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14604         {
14605           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14606           break;
14607         }
14608       /* FALLTHROUGH */
14609     case SYMBOL_REF:
14610       if (!const_ok_for_output (rtl))
14611         break;
14612     case LABEL_REF:
14613       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14614           && (dwarf_version >= 4 || !dwarf_strict))
14615         {
14616           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14617           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14618           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14619           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14620           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14621         }
14622       break;
14623
14624     case DEBUG_IMPLICIT_PTR:
14625       loc_result = implicit_ptr_descriptor (rtl, 0);
14626       break;
14627
14628     case PLUS:
14629       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14630           && CONST_INT_P (XEXP (rtl, 1)))
14631         {
14632           loc_result
14633             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14634           break;
14635         }
14636       /* FALLTHRU */
14637     default:
14638       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14639           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14640           && (dwarf_version >= 4 || !dwarf_strict))
14641         {
14642           /* Value expression.  */
14643           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14644           if (loc_result)
14645             add_loc_descr (&loc_result,
14646                            new_loc_descr (DW_OP_stack_value, 0, 0));
14647         }
14648       break;
14649     }
14650
14651   return loc_result;
14652 }
14653
14654 /* We need to figure out what section we should use as the base for the
14655    address ranges where a given location is valid.
14656    1. If this particular DECL has a section associated with it, use that.
14657    2. If this function has a section associated with it, use that.
14658    3. Otherwise, use the text section.
14659    XXX: If you split a variable across multiple sections, we won't notice.  */
14660
14661 static const char *
14662 secname_for_decl (const_tree decl)
14663 {
14664   const char *secname;
14665
14666   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14667     {
14668       tree sectree = DECL_SECTION_NAME (decl);
14669       secname = TREE_STRING_POINTER (sectree);
14670     }
14671   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14672     {
14673       tree sectree = DECL_SECTION_NAME (current_function_decl);
14674       secname = TREE_STRING_POINTER (sectree);
14675     }
14676   else if (cfun && in_cold_section_p)
14677     secname = crtl->subsections.cold_section_label;
14678   else
14679     secname = text_section_label;
14680
14681   return secname;
14682 }
14683
14684 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14685
14686 static bool
14687 decl_by_reference_p (tree decl)
14688 {
14689   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14690            || TREE_CODE (decl) == VAR_DECL)
14691           && DECL_BY_REFERENCE (decl));
14692 }
14693
14694 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14695    for VARLOC.  */
14696
14697 static dw_loc_descr_ref
14698 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14699                enum var_init_status initialized)
14700 {
14701   int have_address = 0;
14702   dw_loc_descr_ref descr;
14703   enum machine_mode mode;
14704
14705   if (want_address != 2)
14706     {
14707       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14708       /* Single part.  */
14709       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14710         {
14711           varloc = PAT_VAR_LOCATION_LOC (varloc);
14712           if (GET_CODE (varloc) == EXPR_LIST)
14713             varloc = XEXP (varloc, 0);
14714           mode = GET_MODE (varloc);
14715           if (MEM_P (varloc))
14716             {
14717               rtx addr = XEXP (varloc, 0);
14718               descr = mem_loc_descriptor (addr, mode, initialized);
14719               if (descr)
14720                 have_address = 1;
14721               else
14722                 {
14723                   rtx x = avoid_constant_pool_reference (varloc);
14724                   if (x != varloc)
14725                     descr = mem_loc_descriptor (x, mode, initialized);
14726                 }
14727             }
14728           else
14729             descr = mem_loc_descriptor (varloc, mode, initialized);
14730         }
14731       else
14732         return 0;
14733     }
14734   else
14735     {
14736       if (GET_CODE (varloc) == VAR_LOCATION)
14737         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14738       else
14739         mode = DECL_MODE (loc);
14740       descr = loc_descriptor (varloc, mode, initialized);
14741       have_address = 1;
14742     }
14743
14744   if (!descr)
14745     return 0;
14746
14747   if (want_address == 2 && !have_address
14748       && (dwarf_version >= 4 || !dwarf_strict))
14749     {
14750       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14751         {
14752           expansion_failed (loc, NULL_RTX,
14753                             "DWARF address size mismatch");
14754           return 0;
14755         }
14756       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14757       have_address = 1;
14758     }
14759   /* Show if we can't fill the request for an address.  */
14760   if (want_address && !have_address)
14761     {
14762       expansion_failed (loc, NULL_RTX,
14763                         "Want address and only have value");
14764       return 0;
14765     }
14766
14767   /* If we've got an address and don't want one, dereference.  */
14768   if (!want_address && have_address)
14769     {
14770       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14771       enum dwarf_location_atom op;
14772
14773       if (size > DWARF2_ADDR_SIZE || size == -1)
14774         {
14775           expansion_failed (loc, NULL_RTX,
14776                             "DWARF address size mismatch");
14777           return 0;
14778         }
14779       else if (size == DWARF2_ADDR_SIZE)
14780         op = DW_OP_deref;
14781       else
14782         op = DW_OP_deref_size;
14783
14784       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14785     }
14786
14787   return descr;
14788 }
14789
14790 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14791    if it is not possible.  */
14792
14793 static dw_loc_descr_ref
14794 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14795 {
14796   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14797     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14798   else if (dwarf_version >= 3 || !dwarf_strict)
14799     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14800   else
14801     return NULL;
14802 }
14803
14804 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14805    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14806
14807 static dw_loc_descr_ref
14808 dw_sra_loc_expr (tree decl, rtx loc)
14809 {
14810   rtx p;
14811   unsigned int padsize = 0;
14812   dw_loc_descr_ref descr, *descr_tail;
14813   unsigned HOST_WIDE_INT decl_size;
14814   rtx varloc;
14815   enum var_init_status initialized;
14816
14817   if (DECL_SIZE (decl) == NULL
14818       || !host_integerp (DECL_SIZE (decl), 1))
14819     return NULL;
14820
14821   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14822   descr = NULL;
14823   descr_tail = &descr;
14824
14825   for (p = loc; p; p = XEXP (p, 1))
14826     {
14827       unsigned int bitsize = decl_piece_bitsize (p);
14828       rtx loc_note = *decl_piece_varloc_ptr (p);
14829       dw_loc_descr_ref cur_descr;
14830       dw_loc_descr_ref *tail, last = NULL;
14831       unsigned int opsize = 0;
14832
14833       if (loc_note == NULL_RTX
14834           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14835         {
14836           padsize += bitsize;
14837           continue;
14838         }
14839       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14840       varloc = NOTE_VAR_LOCATION (loc_note);
14841       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14842       if (cur_descr == NULL)
14843         {
14844           padsize += bitsize;
14845           continue;
14846         }
14847
14848       /* Check that cur_descr either doesn't use
14849          DW_OP_*piece operations, or their sum is equal
14850          to bitsize.  Otherwise we can't embed it.  */
14851       for (tail = &cur_descr; *tail != NULL;
14852            tail = &(*tail)->dw_loc_next)
14853         if ((*tail)->dw_loc_opc == DW_OP_piece)
14854           {
14855             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14856                       * BITS_PER_UNIT;
14857             last = *tail;
14858           }
14859         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14860           {
14861             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14862             last = *tail;
14863           }
14864
14865       if (last != NULL && opsize != bitsize)
14866         {
14867           padsize += bitsize;
14868           continue;
14869         }
14870
14871       /* If there is a hole, add DW_OP_*piece after empty DWARF
14872          expression, which means that those bits are optimized out.  */
14873       if (padsize)
14874         {
14875           if (padsize > decl_size)
14876             return NULL;
14877           decl_size -= padsize;
14878           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14879           if (*descr_tail == NULL)
14880             return NULL;
14881           descr_tail = &(*descr_tail)->dw_loc_next;
14882           padsize = 0;
14883         }
14884       *descr_tail = cur_descr;
14885       descr_tail = tail;
14886       if (bitsize > decl_size)
14887         return NULL;
14888       decl_size -= bitsize;
14889       if (last == NULL)
14890         {
14891           HOST_WIDE_INT offset = 0;
14892           if (GET_CODE (varloc) == VAR_LOCATION
14893               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14894             {
14895               varloc = PAT_VAR_LOCATION_LOC (varloc);
14896               if (GET_CODE (varloc) == EXPR_LIST)
14897                 varloc = XEXP (varloc, 0);
14898             }
14899           do 
14900             {
14901               if (GET_CODE (varloc) == CONST
14902                   || GET_CODE (varloc) == SIGN_EXTEND
14903                   || GET_CODE (varloc) == ZERO_EXTEND)
14904                 varloc = XEXP (varloc, 0);
14905               else if (GET_CODE (varloc) == SUBREG)
14906                 varloc = SUBREG_REG (varloc);
14907               else
14908                 break;
14909             }
14910           while (1);
14911           /* DW_OP_bit_size offset should be zero for register
14912              or implicit location descriptions and empty location
14913              descriptions, but for memory addresses needs big endian
14914              adjustment.  */
14915           if (MEM_P (varloc))
14916             {
14917               unsigned HOST_WIDE_INT memsize
14918                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14919               if (memsize != bitsize)
14920                 {
14921                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14922                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14923                     return NULL;
14924                   if (memsize < bitsize)
14925                     return NULL;
14926                   if (BITS_BIG_ENDIAN)
14927                     offset = memsize - bitsize;
14928                 }
14929             }
14930
14931           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14932           if (*descr_tail == NULL)
14933             return NULL;
14934           descr_tail = &(*descr_tail)->dw_loc_next;
14935         }
14936     }
14937
14938   /* If there were any non-empty expressions, add padding till the end of
14939      the decl.  */
14940   if (descr != NULL && decl_size != 0)
14941     {
14942       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14943       if (*descr_tail == NULL)
14944         return NULL;
14945     }
14946   return descr;
14947 }
14948
14949 /* Return the dwarf representation of the location list LOC_LIST of
14950    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14951    function.  */
14952
14953 static dw_loc_list_ref
14954 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14955 {
14956   const char *endname, *secname;
14957   rtx varloc;
14958   enum var_init_status initialized;
14959   struct var_loc_node *node;
14960   dw_loc_descr_ref descr;
14961   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14962   dw_loc_list_ref list = NULL;
14963   dw_loc_list_ref *listp = &list;
14964
14965   /* Now that we know what section we are using for a base,
14966      actually construct the list of locations.
14967      The first location information is what is passed to the
14968      function that creates the location list, and the remaining
14969      locations just get added on to that list.
14970      Note that we only know the start address for a location
14971      (IE location changes), so to build the range, we use
14972      the range [current location start, next location start].
14973      This means we have to special case the last node, and generate
14974      a range of [last location start, end of function label].  */
14975
14976   secname = secname_for_decl (decl);
14977
14978   for (node = loc_list->first; node; node = node->next)
14979     if (GET_CODE (node->loc) == EXPR_LIST
14980         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14981       {
14982         if (GET_CODE (node->loc) == EXPR_LIST)
14983           {
14984             /* This requires DW_OP_{,bit_}piece, which is not usable
14985                inside DWARF expressions.  */
14986             if (want_address != 2)
14987               continue;
14988             descr = dw_sra_loc_expr (decl, node->loc);
14989             if (descr == NULL)
14990               continue;
14991           }
14992         else
14993           {
14994             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14995             varloc = NOTE_VAR_LOCATION (node->loc);
14996             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14997           }
14998         if (descr)
14999           {
15000             /* The variable has a location between NODE->LABEL and
15001                NODE->NEXT->LABEL.  */
15002             if (node->next)
15003               endname = node->next->label;
15004             /* If the variable has a location at the last label
15005                it keeps its location until the end of function.  */
15006             else if (!current_function_decl)
15007               endname = text_end_label;
15008             else
15009               {
15010                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15011                                              current_function_funcdef_no);
15012                 endname = ggc_strdup (label_id);
15013               }
15014
15015             *listp = new_loc_list (descr, node->label, endname, secname);
15016             listp = &(*listp)->dw_loc_next;
15017           }
15018       }
15019
15020   /* Try to avoid the overhead of a location list emitting a location
15021      expression instead, but only if we didn't have more than one
15022      location entry in the first place.  If some entries were not
15023      representable, we don't want to pretend a single entry that was
15024      applies to the entire scope in which the variable is
15025      available.  */
15026   if (list && loc_list->first->next)
15027     gen_llsym (list);
15028
15029   return list;
15030 }
15031
15032 /* Return if the loc_list has only single element and thus can be represented
15033    as location description.   */
15034
15035 static bool
15036 single_element_loc_list_p (dw_loc_list_ref list)
15037 {
15038   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15039   return !list->ll_symbol;
15040 }
15041
15042 /* To each location in list LIST add loc descr REF.  */
15043
15044 static void
15045 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15046 {
15047   dw_loc_descr_ref copy;
15048   add_loc_descr (&list->expr, ref);
15049   list = list->dw_loc_next;
15050   while (list)
15051     {
15052       copy = ggc_alloc_dw_loc_descr_node ();
15053       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15054       add_loc_descr (&list->expr, copy);
15055       while (copy->dw_loc_next)
15056         {
15057           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15058           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15059           copy->dw_loc_next = new_copy;
15060           copy = new_copy;
15061         }
15062       list = list->dw_loc_next;
15063     }
15064 }
15065
15066 /* Given two lists RET and LIST
15067    produce location list that is result of adding expression in LIST
15068    to expression in RET on each possition in program.
15069    Might be destructive on both RET and LIST.
15070
15071    TODO: We handle only simple cases of RET or LIST having at most one
15072    element. General case would inolve sorting the lists in program order
15073    and merging them that will need some additional work.
15074    Adding that will improve quality of debug info especially for SRA-ed
15075    structures.  */
15076
15077 static void
15078 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15079 {
15080   if (!list)
15081     return;
15082   if (!*ret)
15083     {
15084       *ret = list;
15085       return;
15086     }
15087   if (!list->dw_loc_next)
15088     {
15089       add_loc_descr_to_each (*ret, list->expr);
15090       return;
15091     }
15092   if (!(*ret)->dw_loc_next)
15093     {
15094       add_loc_descr_to_each (list, (*ret)->expr);
15095       *ret = list;
15096       return;
15097     }
15098   expansion_failed (NULL_TREE, NULL_RTX,
15099                     "Don't know how to merge two non-trivial"
15100                     " location lists.\n");
15101   *ret = NULL;
15102   return;
15103 }
15104
15105 /* LOC is constant expression.  Try a luck, look it up in constant
15106    pool and return its loc_descr of its address.  */
15107
15108 static dw_loc_descr_ref
15109 cst_pool_loc_descr (tree loc)
15110 {
15111   /* Get an RTL for this, if something has been emitted.  */
15112   rtx rtl = lookup_constant_def (loc);
15113   enum machine_mode mode;
15114
15115   if (!rtl || !MEM_P (rtl))
15116     {
15117       gcc_assert (!rtl);
15118       return 0;
15119     }
15120   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15121
15122   /* TODO: We might get more coverage if we was actually delaying expansion
15123      of all expressions till end of compilation when constant pools are fully
15124      populated.  */
15125   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15126     {
15127       expansion_failed (loc, NULL_RTX,
15128                         "CST value in contant pool but not marked.");
15129       return 0;
15130     }
15131   mode = GET_MODE (rtl);
15132   rtl = XEXP (rtl, 0);
15133   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15134 }
15135
15136 /* Return dw_loc_list representing address of addr_expr LOC
15137    by looking for innder INDIRECT_REF expression and turing it
15138    into simple arithmetics.  */
15139
15140 static dw_loc_list_ref
15141 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15142 {
15143   tree obj, offset;
15144   HOST_WIDE_INT bitsize, bitpos, bytepos;
15145   enum machine_mode mode;
15146   int volatilep;
15147   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15148   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15149
15150   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15151                              &bitsize, &bitpos, &offset, &mode,
15152                              &unsignedp, &volatilep, false);
15153   STRIP_NOPS (obj);
15154   if (bitpos % BITS_PER_UNIT)
15155     {
15156       expansion_failed (loc, NULL_RTX, "bitfield access");
15157       return 0;
15158     }
15159   if (!INDIRECT_REF_P (obj))
15160     {
15161       expansion_failed (obj,
15162                         NULL_RTX, "no indirect ref in inner refrence");
15163       return 0;
15164     }
15165   if (!offset && !bitpos)
15166     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15167   else if (toplev
15168            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15169            && (dwarf_version >= 4 || !dwarf_strict))
15170     {
15171       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15172       if (!list_ret)
15173         return 0;
15174       if (offset)
15175         {
15176           /* Variable offset.  */
15177           list_ret1 = loc_list_from_tree (offset, 0);
15178           if (list_ret1 == 0)
15179             return 0;
15180           add_loc_list (&list_ret, list_ret1);
15181           if (!list_ret)
15182             return 0;
15183           add_loc_descr_to_each (list_ret,
15184                                  new_loc_descr (DW_OP_plus, 0, 0));
15185         }
15186       bytepos = bitpos / BITS_PER_UNIT;
15187       if (bytepos > 0)
15188         add_loc_descr_to_each (list_ret,
15189                                new_loc_descr (DW_OP_plus_uconst,
15190                                               bytepos, 0));
15191       else if (bytepos < 0)
15192         loc_list_plus_const (list_ret, bytepos);
15193       add_loc_descr_to_each (list_ret,
15194                              new_loc_descr (DW_OP_stack_value, 0, 0));
15195     }
15196   return list_ret;
15197 }
15198
15199
15200 /* Generate Dwarf location list representing LOC.
15201    If WANT_ADDRESS is false, expression computing LOC will be computed
15202    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15203    if WANT_ADDRESS is 2, expression computing address useable in location
15204      will be returned (i.e. DW_OP_reg can be used
15205      to refer to register values).  */
15206
15207 static dw_loc_list_ref
15208 loc_list_from_tree (tree loc, int want_address)
15209 {
15210   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15211   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15212   int have_address = 0;
15213   enum dwarf_location_atom op;
15214
15215   /* ??? Most of the time we do not take proper care for sign/zero
15216      extending the values properly.  Hopefully this won't be a real
15217      problem...  */
15218
15219   switch (TREE_CODE (loc))
15220     {
15221     case ERROR_MARK:
15222       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15223       return 0;
15224
15225     case PLACEHOLDER_EXPR:
15226       /* This case involves extracting fields from an object to determine the
15227          position of other fields.  We don't try to encode this here.  The
15228          only user of this is Ada, which encodes the needed information using
15229          the names of types.  */
15230       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15231       return 0;
15232
15233     case CALL_EXPR:
15234       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15235       /* There are no opcodes for these operations.  */
15236       return 0;
15237
15238     case PREINCREMENT_EXPR:
15239     case PREDECREMENT_EXPR:
15240     case POSTINCREMENT_EXPR:
15241     case POSTDECREMENT_EXPR:
15242       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15243       /* There are no opcodes for these operations.  */
15244       return 0;
15245
15246     case ADDR_EXPR:
15247       /* If we already want an address, see if there is INDIRECT_REF inside
15248          e.g. for &this->field.  */
15249       if (want_address)
15250         {
15251           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15252                        (loc, want_address == 2);
15253           if (list_ret)
15254             have_address = 1;
15255           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15256                    && (ret = cst_pool_loc_descr (loc)))
15257             have_address = 1;
15258         }
15259         /* Otherwise, process the argument and look for the address.  */
15260       if (!list_ret && !ret)
15261         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15262       else
15263         {
15264           if (want_address)
15265             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15266           return NULL;
15267         }
15268       break;
15269
15270     case VAR_DECL:
15271       if (DECL_THREAD_LOCAL_P (loc))
15272         {
15273           rtx rtl;
15274           enum dwarf_location_atom first_op;
15275           enum dwarf_location_atom second_op;
15276           bool dtprel = false;
15277
15278           if (targetm.have_tls)
15279             {
15280               /* If this is not defined, we have no way to emit the
15281                  data.  */
15282               if (!targetm.asm_out.output_dwarf_dtprel)
15283                 return 0;
15284
15285                /* The way DW_OP_GNU_push_tls_address is specified, we
15286                   can only look up addresses of objects in the current
15287                   module.  We used DW_OP_addr as first op, but that's
15288                   wrong, because DW_OP_addr is relocated by the debug
15289                   info consumer, while DW_OP_GNU_push_tls_address
15290                   operand shouldn't be.  */
15291               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15292                 return 0;
15293               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15294               dtprel = true;
15295               second_op = DW_OP_GNU_push_tls_address;
15296             }
15297           else
15298             {
15299               if (!targetm.emutls.debug_form_tls_address
15300                   || !(dwarf_version >= 3 || !dwarf_strict))
15301                 return 0;
15302               /* We stuffed the control variable into the DECL_VALUE_EXPR
15303                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15304                  no longer appear in gimple code.  We used the control
15305                  variable in specific so that we could pick it up here.  */
15306               loc = DECL_VALUE_EXPR (loc);
15307               first_op = DW_OP_addr;
15308               second_op = DW_OP_form_tls_address;
15309             }
15310
15311           rtl = rtl_for_decl_location (loc);
15312           if (rtl == NULL_RTX)
15313             return 0;
15314
15315           if (!MEM_P (rtl))
15316             return 0;
15317           rtl = XEXP (rtl, 0);
15318           if (! CONSTANT_P (rtl))
15319             return 0;
15320
15321           ret = new_loc_descr (first_op, 0, 0);
15322           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15323           ret->dw_loc_oprnd1.v.val_addr = rtl;
15324           ret->dtprel = dtprel;
15325
15326           ret1 = new_loc_descr (second_op, 0, 0);
15327           add_loc_descr (&ret, ret1);
15328
15329           have_address = 1;
15330           break;
15331         }
15332       /* FALLTHRU */
15333
15334     case PARM_DECL:
15335       if (DECL_HAS_VALUE_EXPR_P (loc))
15336         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15337                                    want_address);
15338       /* FALLTHRU */
15339
15340     case RESULT_DECL:
15341     case FUNCTION_DECL:
15342       {
15343         rtx rtl;
15344         var_loc_list *loc_list = lookup_decl_loc (loc);
15345
15346         if (loc_list && loc_list->first)
15347           {
15348             list_ret = dw_loc_list (loc_list, loc, want_address);
15349             have_address = want_address != 0;
15350             break;
15351           }
15352         rtl = rtl_for_decl_location (loc);
15353         if (rtl == NULL_RTX)
15354           {
15355             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15356             return 0;
15357           }
15358         else if (CONST_INT_P (rtl))
15359           {
15360             HOST_WIDE_INT val = INTVAL (rtl);
15361             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15362               val &= GET_MODE_MASK (DECL_MODE (loc));
15363             ret = int_loc_descriptor (val);
15364           }
15365         else if (GET_CODE (rtl) == CONST_STRING)
15366           {
15367             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15368             return 0;
15369           }
15370         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15371           {
15372             ret = new_loc_descr (DW_OP_addr, 0, 0);
15373             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15374             ret->dw_loc_oprnd1.v.val_addr = rtl;
15375           }
15376         else
15377           {
15378             enum machine_mode mode;
15379
15380             /* Certain constructs can only be represented at top-level.  */
15381             if (want_address == 2)
15382               {
15383                 ret = loc_descriptor (rtl, VOIDmode,
15384                                       VAR_INIT_STATUS_INITIALIZED);
15385                 have_address = 1;
15386               }
15387             else
15388               {
15389                 mode = GET_MODE (rtl);
15390                 if (MEM_P (rtl))
15391                   {
15392                     rtl = XEXP (rtl, 0);
15393                     have_address = 1;
15394                   }
15395                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15396               }
15397             if (!ret)
15398               expansion_failed (loc, rtl,
15399                                 "failed to produce loc descriptor for rtl");
15400           }
15401       }
15402       break;
15403
15404     case MEM_REF:
15405       /* ??? FIXME.  */
15406       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15407         return 0;
15408       /* Fallthru.  */
15409     case INDIRECT_REF:
15410       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15411       have_address = 1;
15412       break;
15413
15414     case COMPOUND_EXPR:
15415       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15416
15417     CASE_CONVERT:
15418     case VIEW_CONVERT_EXPR:
15419     case SAVE_EXPR:
15420     case MODIFY_EXPR:
15421       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15422
15423     case COMPONENT_REF:
15424     case BIT_FIELD_REF:
15425     case ARRAY_REF:
15426     case ARRAY_RANGE_REF:
15427     case REALPART_EXPR:
15428     case IMAGPART_EXPR:
15429       {
15430         tree obj, offset;
15431         HOST_WIDE_INT bitsize, bitpos, bytepos;
15432         enum machine_mode mode;
15433         int volatilep;
15434         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15435
15436         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15437                                    &unsignedp, &volatilep, false);
15438
15439         gcc_assert (obj != loc);
15440
15441         list_ret = loc_list_from_tree (obj,
15442                                        want_address == 2
15443                                        && !bitpos && !offset ? 2 : 1);
15444         /* TODO: We can extract value of the small expression via shifting even
15445            for nonzero bitpos.  */
15446         if (list_ret == 0)
15447           return 0;
15448         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15449           {
15450             expansion_failed (loc, NULL_RTX,
15451                               "bitfield access");
15452             return 0;
15453           }
15454
15455         if (offset != NULL_TREE)
15456           {
15457             /* Variable offset.  */
15458             list_ret1 = loc_list_from_tree (offset, 0);
15459             if (list_ret1 == 0)
15460               return 0;
15461             add_loc_list (&list_ret, list_ret1);
15462             if (!list_ret)
15463               return 0;
15464             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15465           }
15466
15467         bytepos = bitpos / BITS_PER_UNIT;
15468         if (bytepos > 0)
15469           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15470         else if (bytepos < 0)
15471           loc_list_plus_const (list_ret, bytepos);
15472
15473         have_address = 1;
15474         break;
15475       }
15476
15477     case INTEGER_CST:
15478       if ((want_address || !host_integerp (loc, 0))
15479           && (ret = cst_pool_loc_descr (loc)))
15480         have_address = 1;
15481       else if (want_address == 2
15482                && host_integerp (loc, 0)
15483                && (ret = address_of_int_loc_descriptor
15484                            (int_size_in_bytes (TREE_TYPE (loc)),
15485                             tree_low_cst (loc, 0))))
15486         have_address = 1;
15487       else if (host_integerp (loc, 0))
15488         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15489       else
15490         {
15491           expansion_failed (loc, NULL_RTX,
15492                             "Integer operand is not host integer");
15493           return 0;
15494         }
15495       break;
15496
15497     case CONSTRUCTOR:
15498     case REAL_CST:
15499     case STRING_CST:
15500     case COMPLEX_CST:
15501       if ((ret = cst_pool_loc_descr (loc)))
15502         have_address = 1;
15503       else
15504       /* We can construct small constants here using int_loc_descriptor.  */
15505         expansion_failed (loc, NULL_RTX,
15506                           "constructor or constant not in constant pool");
15507       break;
15508
15509     case TRUTH_AND_EXPR:
15510     case TRUTH_ANDIF_EXPR:
15511     case BIT_AND_EXPR:
15512       op = DW_OP_and;
15513       goto do_binop;
15514
15515     case TRUTH_XOR_EXPR:
15516     case BIT_XOR_EXPR:
15517       op = DW_OP_xor;
15518       goto do_binop;
15519
15520     case TRUTH_OR_EXPR:
15521     case TRUTH_ORIF_EXPR:
15522     case BIT_IOR_EXPR:
15523       op = DW_OP_or;
15524       goto do_binop;
15525
15526     case FLOOR_DIV_EXPR:
15527     case CEIL_DIV_EXPR:
15528     case ROUND_DIV_EXPR:
15529     case TRUNC_DIV_EXPR:
15530       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15531         return 0;
15532       op = DW_OP_div;
15533       goto do_binop;
15534
15535     case MINUS_EXPR:
15536       op = DW_OP_minus;
15537       goto do_binop;
15538
15539     case FLOOR_MOD_EXPR:
15540     case CEIL_MOD_EXPR:
15541     case ROUND_MOD_EXPR:
15542     case TRUNC_MOD_EXPR:
15543       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15544         {
15545           op = DW_OP_mod;
15546           goto do_binop;
15547         }
15548       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15549       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15550       if (list_ret == 0 || list_ret1 == 0)
15551         return 0;
15552
15553       add_loc_list (&list_ret, list_ret1);
15554       if (list_ret == 0)
15555         return 0;
15556       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15557       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15558       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15559       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15560       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15561       break;
15562
15563     case MULT_EXPR:
15564       op = DW_OP_mul;
15565       goto do_binop;
15566
15567     case LSHIFT_EXPR:
15568       op = DW_OP_shl;
15569       goto do_binop;
15570
15571     case RSHIFT_EXPR:
15572       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15573       goto do_binop;
15574
15575     case POINTER_PLUS_EXPR:
15576     case PLUS_EXPR:
15577       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15578         {
15579           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15580           if (list_ret == 0)
15581             return 0;
15582
15583           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15584           break;
15585         }
15586
15587       op = DW_OP_plus;
15588       goto do_binop;
15589
15590     case LE_EXPR:
15591       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15592         return 0;
15593
15594       op = DW_OP_le;
15595       goto do_binop;
15596
15597     case GE_EXPR:
15598       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15599         return 0;
15600
15601       op = DW_OP_ge;
15602       goto do_binop;
15603
15604     case LT_EXPR:
15605       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15606         return 0;
15607
15608       op = DW_OP_lt;
15609       goto do_binop;
15610
15611     case GT_EXPR:
15612       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15613         return 0;
15614
15615       op = DW_OP_gt;
15616       goto do_binop;
15617
15618     case EQ_EXPR:
15619       op = DW_OP_eq;
15620       goto do_binop;
15621
15622     case NE_EXPR:
15623       op = DW_OP_ne;
15624       goto do_binop;
15625
15626     do_binop:
15627       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15628       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15629       if (list_ret == 0 || list_ret1 == 0)
15630         return 0;
15631
15632       add_loc_list (&list_ret, list_ret1);
15633       if (list_ret == 0)
15634         return 0;
15635       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15636       break;
15637
15638     case TRUTH_NOT_EXPR:
15639     case BIT_NOT_EXPR:
15640       op = DW_OP_not;
15641       goto do_unop;
15642
15643     case ABS_EXPR:
15644       op = DW_OP_abs;
15645       goto do_unop;
15646
15647     case NEGATE_EXPR:
15648       op = DW_OP_neg;
15649       goto do_unop;
15650
15651     do_unop:
15652       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15653       if (list_ret == 0)
15654         return 0;
15655
15656       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15657       break;
15658
15659     case MIN_EXPR:
15660     case MAX_EXPR:
15661       {
15662         const enum tree_code code =
15663           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15664
15665         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15666                       build2 (code, integer_type_node,
15667                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15668                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15669       }
15670
15671       /* ... fall through ...  */
15672
15673     case COND_EXPR:
15674       {
15675         dw_loc_descr_ref lhs
15676           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15677         dw_loc_list_ref rhs
15678           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15679         dw_loc_descr_ref bra_node, jump_node, tmp;
15680
15681         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15682         if (list_ret == 0 || lhs == 0 || rhs == 0)
15683           return 0;
15684
15685         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15686         add_loc_descr_to_each (list_ret, bra_node);
15687
15688         add_loc_list (&list_ret, rhs);
15689         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15690         add_loc_descr_to_each (list_ret, jump_node);
15691
15692         add_loc_descr_to_each (list_ret, lhs);
15693         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15694         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15695
15696         /* ??? Need a node to point the skip at.  Use a nop.  */
15697         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15698         add_loc_descr_to_each (list_ret, tmp);
15699         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15700         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15701       }
15702       break;
15703
15704     case FIX_TRUNC_EXPR:
15705       return 0;
15706
15707     default:
15708       /* Leave front-end specific codes as simply unknown.  This comes
15709          up, for instance, with the C STMT_EXPR.  */
15710       if ((unsigned int) TREE_CODE (loc)
15711           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15712         {
15713           expansion_failed (loc, NULL_RTX,
15714                             "language specific tree node");
15715           return 0;
15716         }
15717
15718 #ifdef ENABLE_CHECKING
15719       /* Otherwise this is a generic code; we should just lists all of
15720          these explicitly.  We forgot one.  */
15721       gcc_unreachable ();
15722 #else
15723       /* In a release build, we want to degrade gracefully: better to
15724          generate incomplete debugging information than to crash.  */
15725       return NULL;
15726 #endif
15727     }
15728
15729   if (!ret && !list_ret)
15730     return 0;
15731
15732   if (want_address == 2 && !have_address
15733       && (dwarf_version >= 4 || !dwarf_strict))
15734     {
15735       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15736         {
15737           expansion_failed (loc, NULL_RTX,
15738                             "DWARF address size mismatch");
15739           return 0;
15740         }
15741       if (ret)
15742         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15743       else
15744         add_loc_descr_to_each (list_ret,
15745                                new_loc_descr (DW_OP_stack_value, 0, 0));
15746       have_address = 1;
15747     }
15748   /* Show if we can't fill the request for an address.  */
15749   if (want_address && !have_address)
15750     {
15751       expansion_failed (loc, NULL_RTX,
15752                         "Want address and only have value");
15753       return 0;
15754     }
15755
15756   gcc_assert (!ret || !list_ret);
15757
15758   /* If we've got an address and don't want one, dereference.  */
15759   if (!want_address && have_address)
15760     {
15761       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15762
15763       if (size > DWARF2_ADDR_SIZE || size == -1)
15764         {
15765           expansion_failed (loc, NULL_RTX,
15766                             "DWARF address size mismatch");
15767           return 0;
15768         }
15769       else if (size == DWARF2_ADDR_SIZE)
15770         op = DW_OP_deref;
15771       else
15772         op = DW_OP_deref_size;
15773
15774       if (ret)
15775         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15776       else
15777         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15778     }
15779   if (ret)
15780     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15781
15782   return list_ret;
15783 }
15784
15785 /* Same as above but return only single location expression.  */
15786 static dw_loc_descr_ref
15787 loc_descriptor_from_tree (tree loc, int want_address)
15788 {
15789   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15790   if (!ret)
15791     return NULL;
15792   if (ret->dw_loc_next)
15793     {
15794       expansion_failed (loc, NULL_RTX,
15795                         "Location list where only loc descriptor needed");
15796       return NULL;
15797     }
15798   return ret->expr;
15799 }
15800
15801 /* Given a value, round it up to the lowest multiple of `boundary'
15802    which is not less than the value itself.  */
15803
15804 static inline HOST_WIDE_INT
15805 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15806 {
15807   return (((value + boundary - 1) / boundary) * boundary);
15808 }
15809
15810 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15811    pointer to the declared type for the relevant field variable, or return
15812    `integer_type_node' if the given node turns out to be an
15813    ERROR_MARK node.  */
15814
15815 static inline tree
15816 field_type (const_tree decl)
15817 {
15818   tree type;
15819
15820   if (TREE_CODE (decl) == ERROR_MARK)
15821     return integer_type_node;
15822
15823   type = DECL_BIT_FIELD_TYPE (decl);
15824   if (type == NULL_TREE)
15825     type = TREE_TYPE (decl);
15826
15827   return type;
15828 }
15829
15830 /* Given a pointer to a tree node, return the alignment in bits for
15831    it, or else return BITS_PER_WORD if the node actually turns out to
15832    be an ERROR_MARK node.  */
15833
15834 static inline unsigned
15835 simple_type_align_in_bits (const_tree type)
15836 {
15837   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15838 }
15839
15840 static inline unsigned
15841 simple_decl_align_in_bits (const_tree decl)
15842 {
15843   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15844 }
15845
15846 /* Return the result of rounding T up to ALIGN.  */
15847
15848 static inline double_int
15849 round_up_to_align (double_int t, unsigned int align)
15850 {
15851   double_int alignd = uhwi_to_double_int (align);
15852   t = double_int_add (t, alignd);
15853   t = double_int_add (t, double_int_minus_one);
15854   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15855   t = double_int_mul (t, alignd);
15856   return t;
15857 }
15858
15859 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15860    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15861    or return 0 if we are unable to determine what that offset is, either
15862    because the argument turns out to be a pointer to an ERROR_MARK node, or
15863    because the offset is actually variable.  (We can't handle the latter case
15864    just yet).  */
15865
15866 static HOST_WIDE_INT
15867 field_byte_offset (const_tree decl)
15868 {
15869   double_int object_offset_in_bits;
15870   double_int object_offset_in_bytes;
15871   double_int bitpos_int;
15872
15873   if (TREE_CODE (decl) == ERROR_MARK)
15874     return 0;
15875
15876   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15877
15878   /* We cannot yet cope with fields whose positions are variable, so
15879      for now, when we see such things, we simply return 0.  Someday, we may
15880      be able to handle such cases, but it will be damn difficult.  */
15881   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15882     return 0;
15883
15884   bitpos_int = tree_to_double_int (bit_position (decl));
15885
15886 #ifdef PCC_BITFIELD_TYPE_MATTERS
15887   if (PCC_BITFIELD_TYPE_MATTERS)
15888     {
15889       tree type;
15890       tree field_size_tree;
15891       double_int deepest_bitpos;
15892       double_int field_size_in_bits;
15893       unsigned int type_align_in_bits;
15894       unsigned int decl_align_in_bits;
15895       double_int type_size_in_bits;
15896
15897       type = field_type (decl);
15898       type_size_in_bits = double_int_type_size_in_bits (type);
15899       type_align_in_bits = simple_type_align_in_bits (type);
15900
15901       field_size_tree = DECL_SIZE (decl);
15902
15903       /* The size could be unspecified if there was an error, or for
15904          a flexible array member.  */
15905       if (!field_size_tree)
15906         field_size_tree = bitsize_zero_node;
15907
15908       /* If the size of the field is not constant, use the type size.  */
15909       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15910         field_size_in_bits = tree_to_double_int (field_size_tree);
15911       else
15912         field_size_in_bits = type_size_in_bits;
15913
15914       decl_align_in_bits = simple_decl_align_in_bits (decl);
15915
15916       /* The GCC front-end doesn't make any attempt to keep track of the
15917          starting bit offset (relative to the start of the containing
15918          structure type) of the hypothetical "containing object" for a
15919          bit-field.  Thus, when computing the byte offset value for the
15920          start of the "containing object" of a bit-field, we must deduce
15921          this information on our own. This can be rather tricky to do in
15922          some cases.  For example, handling the following structure type
15923          definition when compiling for an i386/i486 target (which only
15924          aligns long long's to 32-bit boundaries) can be very tricky:
15925
15926          struct S { int field1; long long field2:31; };
15927
15928          Fortunately, there is a simple rule-of-thumb which can be used
15929          in such cases.  When compiling for an i386/i486, GCC will
15930          allocate 8 bytes for the structure shown above.  It decides to
15931          do this based upon one simple rule for bit-field allocation.
15932          GCC allocates each "containing object" for each bit-field at
15933          the first (i.e. lowest addressed) legitimate alignment boundary
15934          (based upon the required minimum alignment for the declared
15935          type of the field) which it can possibly use, subject to the
15936          condition that there is still enough available space remaining
15937          in the containing object (when allocated at the selected point)
15938          to fully accommodate all of the bits of the bit-field itself.
15939
15940          This simple rule makes it obvious why GCC allocates 8 bytes for
15941          each object of the structure type shown above.  When looking
15942          for a place to allocate the "containing object" for `field2',
15943          the compiler simply tries to allocate a 64-bit "containing
15944          object" at each successive 32-bit boundary (starting at zero)
15945          until it finds a place to allocate that 64- bit field such that
15946          at least 31 contiguous (and previously unallocated) bits remain
15947          within that selected 64 bit field.  (As it turns out, for the
15948          example above, the compiler finds it is OK to allocate the
15949          "containing object" 64-bit field at bit-offset zero within the
15950          structure type.)
15951
15952          Here we attempt to work backwards from the limited set of facts
15953          we're given, and we try to deduce from those facts, where GCC
15954          must have believed that the containing object started (within
15955          the structure type). The value we deduce is then used (by the
15956          callers of this routine) to generate DW_AT_location and
15957          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15958          the case of DW_AT_location, regular fields as well).  */
15959
15960       /* Figure out the bit-distance from the start of the structure to
15961          the "deepest" bit of the bit-field.  */
15962       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15963
15964       /* This is the tricky part.  Use some fancy footwork to deduce
15965          where the lowest addressed bit of the containing object must
15966          be.  */
15967       object_offset_in_bits
15968         = double_int_sub (deepest_bitpos, type_size_in_bits);
15969
15970       /* Round up to type_align by default.  This works best for
15971          bitfields.  */
15972       object_offset_in_bits
15973         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15974
15975       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15976         {
15977           object_offset_in_bits
15978             = double_int_sub (deepest_bitpos, type_size_in_bits);
15979
15980           /* Round up to decl_align instead.  */
15981           object_offset_in_bits
15982             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15983         }
15984     }
15985   else
15986 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15987     object_offset_in_bits = bitpos_int;
15988
15989   object_offset_in_bytes
15990     = double_int_div (object_offset_in_bits,
15991                       uhwi_to_double_int (BITS_PER_UNIT), true,
15992                       TRUNC_DIV_EXPR);
15993   return double_int_to_shwi (object_offset_in_bytes);
15994 }
15995 \f
15996 /* The following routines define various Dwarf attributes and any data
15997    associated with them.  */
15998
15999 /* Add a location description attribute value to a DIE.
16000
16001    This emits location attributes suitable for whole variables and
16002    whole parameters.  Note that the location attributes for struct fields are
16003    generated by the routine `data_member_location_attribute' below.  */
16004
16005 static inline void
16006 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16007                              dw_loc_list_ref descr)
16008 {
16009   if (descr == 0)
16010     return;
16011   if (single_element_loc_list_p (descr))
16012     add_AT_loc (die, attr_kind, descr->expr);
16013   else
16014     add_AT_loc_list (die, attr_kind, descr);
16015 }
16016
16017 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16018
16019 static void
16020 add_accessibility_attribute (dw_die_ref die, tree decl)
16021 {
16022   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16023      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16024      the default has always been DW_ACCESS_public.  */
16025   if (TREE_PROTECTED (decl))
16026     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16027   else if (TREE_PRIVATE (decl))
16028     {
16029       if (dwarf_version == 2
16030           || die->die_parent == NULL
16031           || die->die_parent->die_tag != DW_TAG_class_type)
16032         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16033     }
16034   else if (dwarf_version > 2
16035            && die->die_parent
16036            && die->die_parent->die_tag == DW_TAG_class_type)
16037     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16038 }
16039
16040 /* Attach the specialized form of location attribute used for data members of
16041    struct and union types.  In the special case of a FIELD_DECL node which
16042    represents a bit-field, the "offset" part of this special location
16043    descriptor must indicate the distance in bytes from the lowest-addressed
16044    byte of the containing struct or union type to the lowest-addressed byte of
16045    the "containing object" for the bit-field.  (See the `field_byte_offset'
16046    function above).
16047
16048    For any given bit-field, the "containing object" is a hypothetical object
16049    (of some integral or enum type) within which the given bit-field lives.  The
16050    type of this hypothetical "containing object" is always the same as the
16051    declared type of the individual bit-field itself (for GCC anyway... the
16052    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16053    bytes) of the hypothetical "containing object" which will be given in the
16054    DW_AT_byte_size attribute for this bit-field.  (See the
16055    `byte_size_attribute' function below.)  It is also used when calculating the
16056    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16057    function below.)  */
16058
16059 static void
16060 add_data_member_location_attribute (dw_die_ref die, tree decl)
16061 {
16062   HOST_WIDE_INT offset;
16063   dw_loc_descr_ref loc_descr = 0;
16064
16065   if (TREE_CODE (decl) == TREE_BINFO)
16066     {
16067       /* We're working on the TAG_inheritance for a base class.  */
16068       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16069         {
16070           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16071              aren't at a fixed offset from all (sub)objects of the same
16072              type.  We need to extract the appropriate offset from our
16073              vtable.  The following dwarf expression means
16074
16075                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16076
16077              This is specific to the V3 ABI, of course.  */
16078
16079           dw_loc_descr_ref tmp;
16080
16081           /* Make a copy of the object address.  */
16082           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16083           add_loc_descr (&loc_descr, tmp);
16084
16085           /* Extract the vtable address.  */
16086           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16087           add_loc_descr (&loc_descr, tmp);
16088
16089           /* Calculate the address of the offset.  */
16090           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16091           gcc_assert (offset < 0);
16092
16093           tmp = int_loc_descriptor (-offset);
16094           add_loc_descr (&loc_descr, tmp);
16095           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16096           add_loc_descr (&loc_descr, tmp);
16097
16098           /* Extract the offset.  */
16099           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16100           add_loc_descr (&loc_descr, tmp);
16101
16102           /* Add it to the object address.  */
16103           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16104           add_loc_descr (&loc_descr, tmp);
16105         }
16106       else
16107         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16108     }
16109   else
16110     offset = field_byte_offset (decl);
16111
16112   if (! loc_descr)
16113     {
16114       if (dwarf_version > 2)
16115         {
16116           /* Don't need to output a location expression, just the constant. */
16117           if (offset < 0)
16118             add_AT_int (die, DW_AT_data_member_location, offset);
16119           else
16120             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16121           return;
16122         }
16123       else
16124         {
16125           enum dwarf_location_atom op;
16126
16127           /* The DWARF2 standard says that we should assume that the structure
16128              address is already on the stack, so we can specify a structure
16129              field address by using DW_OP_plus_uconst.  */
16130
16131 #ifdef MIPS_DEBUGGING_INFO
16132           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16133              operator correctly.  It works only if we leave the offset on the
16134              stack.  */
16135           op = DW_OP_constu;
16136 #else
16137           op = DW_OP_plus_uconst;
16138 #endif
16139
16140           loc_descr = new_loc_descr (op, offset, 0);
16141         }
16142     }
16143
16144   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16145 }
16146
16147 /* Writes integer values to dw_vec_const array.  */
16148
16149 static void
16150 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16151 {
16152   while (size != 0)
16153     {
16154       *dest++ = val & 0xff;
16155       val >>= 8;
16156       --size;
16157     }
16158 }
16159
16160 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16161
16162 static HOST_WIDE_INT
16163 extract_int (const unsigned char *src, unsigned int size)
16164 {
16165   HOST_WIDE_INT val = 0;
16166
16167   src += size;
16168   while (size != 0)
16169     {
16170       val <<= 8;
16171       val |= *--src & 0xff;
16172       --size;
16173     }
16174   return val;
16175 }
16176
16177 /* Writes double_int values to dw_vec_const array.  */
16178
16179 static void
16180 insert_double (double_int val, unsigned char *dest)
16181 {
16182   unsigned char *p0 = dest;
16183   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16184
16185   if (WORDS_BIG_ENDIAN)
16186     {
16187       p0 = p1;
16188       p1 = dest;
16189     }
16190
16191   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16192   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16193 }
16194
16195 /* Writes floating point values to dw_vec_const array.  */
16196
16197 static void
16198 insert_float (const_rtx rtl, unsigned char *array)
16199 {
16200   REAL_VALUE_TYPE rv;
16201   long val[4];
16202   int i;
16203
16204   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16205   real_to_target (val, &rv, GET_MODE (rtl));
16206
16207   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16208   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16209     {
16210       insert_int (val[i], 4, array);
16211       array += 4;
16212     }
16213 }
16214
16215 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16216    does not have a "location" either in memory or in a register.  These
16217    things can arise in GNU C when a constant is passed as an actual parameter
16218    to an inlined function.  They can also arise in C++ where declared
16219    constants do not necessarily get memory "homes".  */
16220
16221 static bool
16222 add_const_value_attribute (dw_die_ref die, rtx rtl)
16223 {
16224   switch (GET_CODE (rtl))
16225     {
16226     case CONST_INT:
16227       {
16228         HOST_WIDE_INT val = INTVAL (rtl);
16229
16230         if (val < 0)
16231           add_AT_int (die, DW_AT_const_value, val);
16232         else
16233           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16234       }
16235       return true;
16236
16237     case CONST_DOUBLE:
16238       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16239          floating-point constant.  A CONST_DOUBLE is used whenever the
16240          constant requires more than one word in order to be adequately
16241          represented.  */
16242       {
16243         enum machine_mode mode = GET_MODE (rtl);
16244
16245         if (SCALAR_FLOAT_MODE_P (mode))
16246           {
16247             unsigned int length = GET_MODE_SIZE (mode);
16248             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16249
16250             insert_float (rtl, array);
16251             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16252           }
16253         else
16254           add_AT_double (die, DW_AT_const_value,
16255                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16256       }
16257       return true;
16258
16259     case CONST_VECTOR:
16260       {
16261         enum machine_mode mode = GET_MODE (rtl);
16262         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16263         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16264         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16265           (length * elt_size);
16266         unsigned int i;
16267         unsigned char *p;
16268
16269         switch (GET_MODE_CLASS (mode))
16270           {
16271           case MODE_VECTOR_INT:
16272             for (i = 0, p = array; i < length; i++, p += elt_size)
16273               {
16274                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16275                 double_int val = rtx_to_double_int (elt);
16276
16277                 if (elt_size <= sizeof (HOST_WIDE_INT))
16278                   insert_int (double_int_to_shwi (val), elt_size, p);
16279                 else
16280                   {
16281                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16282                     insert_double (val, p);
16283                   }
16284               }
16285             break;
16286
16287           case MODE_VECTOR_FLOAT:
16288             for (i = 0, p = array; i < length; i++, p += elt_size)
16289               {
16290                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16291                 insert_float (elt, p);
16292               }
16293             break;
16294
16295           default:
16296             gcc_unreachable ();
16297           }
16298
16299         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16300       }
16301       return true;
16302
16303     case CONST_STRING:
16304       if (dwarf_version >= 4 || !dwarf_strict)
16305         {
16306           dw_loc_descr_ref loc_result;
16307           resolve_one_addr (&rtl, NULL);
16308         rtl_addr:
16309           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16310           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16311           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16312           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16313           add_AT_loc (die, DW_AT_location, loc_result);
16314           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16315           return true;
16316         }
16317       return false;
16318
16319     case CONST:
16320       if (CONSTANT_P (XEXP (rtl, 0)))
16321         return add_const_value_attribute (die, XEXP (rtl, 0));
16322       /* FALLTHROUGH */
16323     case SYMBOL_REF:
16324       if (!const_ok_for_output (rtl))
16325         return false;
16326     case LABEL_REF:
16327       if (dwarf_version >= 4 || !dwarf_strict)
16328         goto rtl_addr;
16329       return false;
16330
16331     case PLUS:
16332       /* In cases where an inlined instance of an inline function is passed
16333          the address of an `auto' variable (which is local to the caller) we
16334          can get a situation where the DECL_RTL of the artificial local
16335          variable (for the inlining) which acts as a stand-in for the
16336          corresponding formal parameter (of the inline function) will look
16337          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16338          exactly a compile-time constant expression, but it isn't the address
16339          of the (artificial) local variable either.  Rather, it represents the
16340          *value* which the artificial local variable always has during its
16341          lifetime.  We currently have no way to represent such quasi-constant
16342          values in Dwarf, so for now we just punt and generate nothing.  */
16343       return false;
16344
16345     case HIGH:
16346     case CONST_FIXED:
16347       return false;
16348
16349     case MEM:
16350       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16351           && MEM_READONLY_P (rtl)
16352           && GET_MODE (rtl) == BLKmode)
16353         {
16354           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16355           return true;
16356         }
16357       return false;
16358
16359     default:
16360       /* No other kinds of rtx should be possible here.  */
16361       gcc_unreachable ();
16362     }
16363   return false;
16364 }
16365
16366 /* Determine whether the evaluation of EXPR references any variables
16367    or functions which aren't otherwise used (and therefore may not be
16368    output).  */
16369 static tree
16370 reference_to_unused (tree * tp, int * walk_subtrees,
16371                      void * data ATTRIBUTE_UNUSED)
16372 {
16373   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16374     *walk_subtrees = 0;
16375
16376   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16377       && ! TREE_ASM_WRITTEN (*tp))
16378     return *tp;
16379   /* ???  The C++ FE emits debug information for using decls, so
16380      putting gcc_unreachable here falls over.  See PR31899.  For now
16381      be conservative.  */
16382   else if (!cgraph_global_info_ready
16383            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16384     return *tp;
16385   else if (TREE_CODE (*tp) == VAR_DECL)
16386     {
16387       struct varpool_node *node = varpool_get_node (*tp);
16388       if (!node || !node->needed)
16389         return *tp;
16390     }
16391   else if (TREE_CODE (*tp) == FUNCTION_DECL
16392            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16393     {
16394       /* The call graph machinery must have finished analyzing,
16395          optimizing and gimplifying the CU by now.
16396          So if *TP has no call graph node associated
16397          to it, it means *TP will not be emitted.  */
16398       if (!cgraph_get_node (*tp))
16399         return *tp;
16400     }
16401   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16402     return *tp;
16403
16404   return NULL_TREE;
16405 }
16406
16407 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16408    for use in a later add_const_value_attribute call.  */
16409
16410 static rtx
16411 rtl_for_decl_init (tree init, tree type)
16412 {
16413   rtx rtl = NULL_RTX;
16414
16415   /* If a variable is initialized with a string constant without embedded
16416      zeros, build CONST_STRING.  */
16417   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16418     {
16419       tree enttype = TREE_TYPE (type);
16420       tree domain = TYPE_DOMAIN (type);
16421       enum machine_mode mode = TYPE_MODE (enttype);
16422
16423       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16424           && domain
16425           && integer_zerop (TYPE_MIN_VALUE (domain))
16426           && compare_tree_int (TYPE_MAX_VALUE (domain),
16427                                TREE_STRING_LENGTH (init) - 1) == 0
16428           && ((size_t) TREE_STRING_LENGTH (init)
16429               == strlen (TREE_STRING_POINTER (init)) + 1))
16430         {
16431           rtl = gen_rtx_CONST_STRING (VOIDmode,
16432                                       ggc_strdup (TREE_STRING_POINTER (init)));
16433           rtl = gen_rtx_MEM (BLKmode, rtl);
16434           MEM_READONLY_P (rtl) = 1;
16435         }
16436     }
16437   /* Other aggregates, and complex values, could be represented using
16438      CONCAT: FIXME!  */
16439   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16440     ;
16441   /* Vectors only work if their mode is supported by the target.
16442      FIXME: generic vectors ought to work too.  */
16443   else if (TREE_CODE (type) == VECTOR_TYPE
16444            && !VECTOR_MODE_P (TYPE_MODE (type)))
16445     ;
16446   /* If the initializer is something that we know will expand into an
16447      immediate RTL constant, expand it now.  We must be careful not to
16448      reference variables which won't be output.  */
16449   else if (initializer_constant_valid_p (init, type)
16450            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16451     {
16452       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16453          possible.  */
16454       if (TREE_CODE (type) == VECTOR_TYPE)
16455         switch (TREE_CODE (init))
16456           {
16457           case VECTOR_CST:
16458             break;
16459           case CONSTRUCTOR:
16460             if (TREE_CONSTANT (init))
16461               {
16462                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16463                 bool constant_p = true;
16464                 tree value;
16465                 unsigned HOST_WIDE_INT ix;
16466
16467                 /* Even when ctor is constant, it might contain non-*_CST
16468                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16469                    belong into VECTOR_CST nodes.  */
16470                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16471                   if (!CONSTANT_CLASS_P (value))
16472                     {
16473                       constant_p = false;
16474                       break;
16475                     }
16476
16477                 if (constant_p)
16478                   {
16479                     init = build_vector_from_ctor (type, elts);
16480                     break;
16481                   }
16482               }
16483             /* FALLTHRU */
16484
16485           default:
16486             return NULL;
16487           }
16488
16489       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16490
16491       /* If expand_expr returns a MEM, it wasn't immediate.  */
16492       gcc_assert (!rtl || !MEM_P (rtl));
16493     }
16494
16495   return rtl;
16496 }
16497
16498 /* Generate RTL for the variable DECL to represent its location.  */
16499
16500 static rtx
16501 rtl_for_decl_location (tree decl)
16502 {
16503   rtx rtl;
16504
16505   /* Here we have to decide where we are going to say the parameter "lives"
16506      (as far as the debugger is concerned).  We only have a couple of
16507      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16508
16509      DECL_RTL normally indicates where the parameter lives during most of the
16510      activation of the function.  If optimization is enabled however, this
16511      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16512      that the parameter doesn't really live anywhere (as far as the code
16513      generation parts of GCC are concerned) during most of the function's
16514      activation.  That will happen (for example) if the parameter is never
16515      referenced within the function.
16516
16517      We could just generate a location descriptor here for all non-NULL
16518      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16519      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16520      where DECL_RTL is NULL or is a pseudo-reg.
16521
16522      Note however that we can only get away with using DECL_INCOMING_RTL as
16523      a backup substitute for DECL_RTL in certain limited cases.  In cases
16524      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16525      we can be sure that the parameter was passed using the same type as it is
16526      declared to have within the function, and that its DECL_INCOMING_RTL
16527      points us to a place where a value of that type is passed.
16528
16529      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16530      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16531      because in these cases DECL_INCOMING_RTL points us to a value of some
16532      type which is *different* from the type of the parameter itself.  Thus,
16533      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16534      such cases, the debugger would end up (for example) trying to fetch a
16535      `float' from a place which actually contains the first part of a
16536      `double'.  That would lead to really incorrect and confusing
16537      output at debug-time.
16538
16539      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16540      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16541      are a couple of exceptions however.  On little-endian machines we can
16542      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16543      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16544      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16545      when (on a little-endian machine) a non-prototyped function has a
16546      parameter declared to be of type `short' or `char'.  In such cases,
16547      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16548      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16549      passed `int' value.  If the debugger then uses that address to fetch
16550      a `short' or a `char' (on a little-endian machine) the result will be
16551      the correct data, so we allow for such exceptional cases below.
16552
16553      Note that our goal here is to describe the place where the given formal
16554      parameter lives during most of the function's activation (i.e. between the
16555      end of the prologue and the start of the epilogue).  We'll do that as best
16556      as we can. Note however that if the given formal parameter is modified
16557      sometime during the execution of the function, then a stack backtrace (at
16558      debug-time) will show the function as having been called with the *new*
16559      value rather than the value which was originally passed in.  This happens
16560      rarely enough that it is not a major problem, but it *is* a problem, and
16561      I'd like to fix it.
16562
16563      A future version of dwarf2out.c may generate two additional attributes for
16564      any given DW_TAG_formal_parameter DIE which will describe the "passed
16565      type" and the "passed location" for the given formal parameter in addition
16566      to the attributes we now generate to indicate the "declared type" and the
16567      "active location" for each parameter.  This additional set of attributes
16568      could be used by debuggers for stack backtraces. Separately, note that
16569      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16570      This happens (for example) for inlined-instances of inline function formal
16571      parameters which are never referenced.  This really shouldn't be
16572      happening.  All PARM_DECL nodes should get valid non-NULL
16573      DECL_INCOMING_RTL values.  FIXME.  */
16574
16575   /* Use DECL_RTL as the "location" unless we find something better.  */
16576   rtl = DECL_RTL_IF_SET (decl);
16577
16578   /* When generating abstract instances, ignore everything except
16579      constants, symbols living in memory, and symbols living in
16580      fixed registers.  */
16581   if (! reload_completed)
16582     {
16583       if (rtl
16584           && (CONSTANT_P (rtl)
16585               || (MEM_P (rtl)
16586                   && CONSTANT_P (XEXP (rtl, 0)))
16587               || (REG_P (rtl)
16588                   && TREE_CODE (decl) == VAR_DECL
16589                   && TREE_STATIC (decl))))
16590         {
16591           rtl = targetm.delegitimize_address (rtl);
16592           return rtl;
16593         }
16594       rtl = NULL_RTX;
16595     }
16596   else if (TREE_CODE (decl) == PARM_DECL)
16597     {
16598       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16599         {
16600           tree declared_type = TREE_TYPE (decl);
16601           tree passed_type = DECL_ARG_TYPE (decl);
16602           enum machine_mode dmode = TYPE_MODE (declared_type);
16603           enum machine_mode pmode = TYPE_MODE (passed_type);
16604
16605           /* This decl represents a formal parameter which was optimized out.
16606              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16607              all cases where (rtl == NULL_RTX) just below.  */
16608           if (dmode == pmode)
16609             rtl = DECL_INCOMING_RTL (decl);
16610           else if (SCALAR_INT_MODE_P (dmode)
16611                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16612                    && DECL_INCOMING_RTL (decl))
16613             {
16614               rtx inc = DECL_INCOMING_RTL (decl);
16615               if (REG_P (inc))
16616                 rtl = inc;
16617               else if (MEM_P (inc))
16618                 {
16619                   if (BYTES_BIG_ENDIAN)
16620                     rtl = adjust_address_nv (inc, dmode,
16621                                              GET_MODE_SIZE (pmode)
16622                                              - GET_MODE_SIZE (dmode));
16623                   else
16624                     rtl = inc;
16625                 }
16626             }
16627         }
16628
16629       /* If the parm was passed in registers, but lives on the stack, then
16630          make a big endian correction if the mode of the type of the
16631          parameter is not the same as the mode of the rtl.  */
16632       /* ??? This is the same series of checks that are made in dbxout.c before
16633          we reach the big endian correction code there.  It isn't clear if all
16634          of these checks are necessary here, but keeping them all is the safe
16635          thing to do.  */
16636       else if (MEM_P (rtl)
16637                && XEXP (rtl, 0) != const0_rtx
16638                && ! CONSTANT_P (XEXP (rtl, 0))
16639                /* Not passed in memory.  */
16640                && !MEM_P (DECL_INCOMING_RTL (decl))
16641                /* Not passed by invisible reference.  */
16642                && (!REG_P (XEXP (rtl, 0))
16643                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16644                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16645 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16646                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16647 #endif
16648                      )
16649                /* Big endian correction check.  */
16650                && BYTES_BIG_ENDIAN
16651                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16652                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16653                    < UNITS_PER_WORD))
16654         {
16655           int offset = (UNITS_PER_WORD
16656                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16657
16658           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16659                              plus_constant (XEXP (rtl, 0), offset));
16660         }
16661     }
16662   else if (TREE_CODE (decl) == VAR_DECL
16663            && rtl
16664            && MEM_P (rtl)
16665            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16666            && BYTES_BIG_ENDIAN)
16667     {
16668       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16669       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16670
16671       /* If a variable is declared "register" yet is smaller than
16672          a register, then if we store the variable to memory, it
16673          looks like we're storing a register-sized value, when in
16674          fact we are not.  We need to adjust the offset of the
16675          storage location to reflect the actual value's bytes,
16676          else gdb will not be able to display it.  */
16677       if (rsize > dsize)
16678         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16679                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16680     }
16681
16682   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16683      and will have been substituted directly into all expressions that use it.
16684      C does not have such a concept, but C++ and other languages do.  */
16685   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16686     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16687
16688   if (rtl)
16689     rtl = targetm.delegitimize_address (rtl);
16690
16691   /* If we don't look past the constant pool, we risk emitting a
16692      reference to a constant pool entry that isn't referenced from
16693      code, and thus is not emitted.  */
16694   if (rtl)
16695     rtl = avoid_constant_pool_reference (rtl);
16696
16697   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16698      in the current CU, resolve_addr will remove the expression referencing
16699      it.  */
16700   if (rtl == NULL_RTX
16701       && TREE_CODE (decl) == VAR_DECL
16702       && !DECL_EXTERNAL (decl)
16703       && TREE_STATIC (decl)
16704       && DECL_NAME (decl)
16705       && !DECL_HARD_REGISTER (decl)
16706       && DECL_MODE (decl) != VOIDmode)
16707     {
16708       rtl = make_decl_rtl_for_debug (decl);
16709       if (!MEM_P (rtl)
16710           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16711           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16712         rtl = NULL_RTX;
16713     }
16714
16715   return rtl;
16716 }
16717
16718 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16719    returned.  If so, the decl for the COMMON block is returned, and the
16720    value is the offset into the common block for the symbol.  */
16721
16722 static tree
16723 fortran_common (tree decl, HOST_WIDE_INT *value)
16724 {
16725   tree val_expr, cvar;
16726   enum machine_mode mode;
16727   HOST_WIDE_INT bitsize, bitpos;
16728   tree offset;
16729   int volatilep = 0, unsignedp = 0;
16730
16731   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16732      it does not have a value (the offset into the common area), or if it
16733      is thread local (as opposed to global) then it isn't common, and shouldn't
16734      be handled as such.  */
16735   if (TREE_CODE (decl) != VAR_DECL
16736       || !TREE_STATIC (decl)
16737       || !DECL_HAS_VALUE_EXPR_P (decl)
16738       || !is_fortran ())
16739     return NULL_TREE;
16740
16741   val_expr = DECL_VALUE_EXPR (decl);
16742   if (TREE_CODE (val_expr) != COMPONENT_REF)
16743     return NULL_TREE;
16744
16745   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16746                               &mode, &unsignedp, &volatilep, true);
16747
16748   if (cvar == NULL_TREE
16749       || TREE_CODE (cvar) != VAR_DECL
16750       || DECL_ARTIFICIAL (cvar)
16751       || !TREE_PUBLIC (cvar))
16752     return NULL_TREE;
16753
16754   *value = 0;
16755   if (offset != NULL)
16756     {
16757       if (!host_integerp (offset, 0))
16758         return NULL_TREE;
16759       *value = tree_low_cst (offset, 0);
16760     }
16761   if (bitpos != 0)
16762     *value += bitpos / BITS_PER_UNIT;
16763
16764   return cvar;
16765 }
16766
16767 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16768    data attribute for a variable or a parameter.  We generate the
16769    DW_AT_const_value attribute only in those cases where the given variable
16770    or parameter does not have a true "location" either in memory or in a
16771    register.  This can happen (for example) when a constant is passed as an
16772    actual argument in a call to an inline function.  (It's possible that
16773    these things can crop up in other ways also.)  Note that one type of
16774    constant value which can be passed into an inlined function is a constant
16775    pointer.  This can happen for example if an actual argument in an inlined
16776    function call evaluates to a compile-time constant address.  */
16777
16778 static bool
16779 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16780                                        enum dwarf_attribute attr)
16781 {
16782   rtx rtl;
16783   dw_loc_list_ref list;
16784   var_loc_list *loc_list;
16785
16786   if (TREE_CODE (decl) == ERROR_MARK)
16787     return false;
16788
16789   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16790               || TREE_CODE (decl) == RESULT_DECL);
16791
16792   /* Try to get some constant RTL for this decl, and use that as the value of
16793      the location.  */
16794
16795   rtl = rtl_for_decl_location (decl);
16796   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16797       && add_const_value_attribute (die, rtl))
16798     return true;
16799
16800   /* See if we have single element location list that is equivalent to
16801      a constant value.  That way we are better to use add_const_value_attribute
16802      rather than expanding constant value equivalent.  */
16803   loc_list = lookup_decl_loc (decl);
16804   if (loc_list
16805       && loc_list->first
16806       && loc_list->first->next == NULL
16807       && NOTE_P (loc_list->first->loc)
16808       && NOTE_VAR_LOCATION (loc_list->first->loc)
16809       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16810     {
16811       struct var_loc_node *node;
16812
16813       node = loc_list->first;
16814       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16815       if (GET_CODE (rtl) == EXPR_LIST)
16816         rtl = XEXP (rtl, 0);
16817       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16818           && add_const_value_attribute (die, rtl))
16819          return true;
16820     }
16821   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16822   if (list)
16823     {
16824       add_AT_location_description (die, attr, list);
16825       return true;
16826     }
16827   /* None of that worked, so it must not really have a location;
16828      try adding a constant value attribute from the DECL_INITIAL.  */
16829   return tree_add_const_value_attribute_for_decl (die, decl);
16830 }
16831
16832 /* Add VARIABLE and DIE into deferred locations list.  */
16833
16834 static void
16835 defer_location (tree variable, dw_die_ref die)
16836 {
16837   deferred_locations entry;
16838   entry.variable = variable;
16839   entry.die = die;
16840   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16841 }
16842
16843 /* Helper function for tree_add_const_value_attribute.  Natively encode
16844    initializer INIT into an array.  Return true if successful.  */
16845
16846 static bool
16847 native_encode_initializer (tree init, unsigned char *array, int size)
16848 {
16849   tree type;
16850
16851   if (init == NULL_TREE)
16852     return false;
16853
16854   STRIP_NOPS (init);
16855   switch (TREE_CODE (init))
16856     {
16857     case STRING_CST:
16858       type = TREE_TYPE (init);
16859       if (TREE_CODE (type) == ARRAY_TYPE)
16860         {
16861           tree enttype = TREE_TYPE (type);
16862           enum machine_mode mode = TYPE_MODE (enttype);
16863
16864           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16865             return false;
16866           if (int_size_in_bytes (type) != size)
16867             return false;
16868           if (size > TREE_STRING_LENGTH (init))
16869             {
16870               memcpy (array, TREE_STRING_POINTER (init),
16871                       TREE_STRING_LENGTH (init));
16872               memset (array + TREE_STRING_LENGTH (init),
16873                       '\0', size - TREE_STRING_LENGTH (init));
16874             }
16875           else
16876             memcpy (array, TREE_STRING_POINTER (init), size);
16877           return true;
16878         }
16879       return false;
16880     case CONSTRUCTOR:
16881       type = TREE_TYPE (init);
16882       if (int_size_in_bytes (type) != size)
16883         return false;
16884       if (TREE_CODE (type) == ARRAY_TYPE)
16885         {
16886           HOST_WIDE_INT min_index;
16887           unsigned HOST_WIDE_INT cnt;
16888           int curpos = 0, fieldsize;
16889           constructor_elt *ce;
16890
16891           if (TYPE_DOMAIN (type) == NULL_TREE
16892               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16893             return false;
16894
16895           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16896           if (fieldsize <= 0)
16897             return false;
16898
16899           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16900           memset (array, '\0', size);
16901           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16902             {
16903               tree val = ce->value;
16904               tree index = ce->index;
16905               int pos = curpos;
16906               if (index && TREE_CODE (index) == RANGE_EXPR)
16907                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16908                       * fieldsize;
16909               else if (index)
16910                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16911
16912               if (val)
16913                 {
16914                   STRIP_NOPS (val);
16915                   if (!native_encode_initializer (val, array + pos, fieldsize))
16916                     return false;
16917                 }
16918               curpos = pos + fieldsize;
16919               if (index && TREE_CODE (index) == RANGE_EXPR)
16920                 {
16921                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16922                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16923                   while (count > 0)
16924                     {
16925                       if (val)
16926                         memcpy (array + curpos, array + pos, fieldsize);
16927                       curpos += fieldsize;
16928                     }
16929                 }
16930               gcc_assert (curpos <= size);
16931             }
16932           return true;
16933         }
16934       else if (TREE_CODE (type) == RECORD_TYPE
16935                || TREE_CODE (type) == UNION_TYPE)
16936         {
16937           tree field = NULL_TREE;
16938           unsigned HOST_WIDE_INT cnt;
16939           constructor_elt *ce;
16940
16941           if (int_size_in_bytes (type) != size)
16942             return false;
16943
16944           if (TREE_CODE (type) == RECORD_TYPE)
16945             field = TYPE_FIELDS (type);
16946
16947           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16948             {
16949               tree val = ce->value;
16950               int pos, fieldsize;
16951
16952               if (ce->index != 0)
16953                 field = ce->index;
16954
16955               if (val)
16956                 STRIP_NOPS (val);
16957
16958               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16959                 return false;
16960
16961               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16962                   && TYPE_DOMAIN (TREE_TYPE (field))
16963                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16964                 return false;
16965               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16966                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16967                 return false;
16968               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16969               pos = int_byte_position (field);
16970               gcc_assert (pos + fieldsize <= size);
16971               if (val
16972                   && !native_encode_initializer (val, array + pos, fieldsize))
16973                 return false;
16974             }
16975           return true;
16976         }
16977       return false;
16978     case VIEW_CONVERT_EXPR:
16979     case NON_LVALUE_EXPR:
16980       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16981     default:
16982       return native_encode_expr (init, array, size) == size;
16983     }
16984 }
16985
16986 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16987    attribute is the const value T.  */
16988
16989 static bool
16990 tree_add_const_value_attribute (dw_die_ref die, tree t)
16991 {
16992   tree init;
16993   tree type = TREE_TYPE (t);
16994   rtx rtl;
16995
16996   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16997     return false;
16998
16999   init = t;
17000   gcc_assert (!DECL_P (init));
17001
17002   rtl = rtl_for_decl_init (init, type);
17003   if (rtl)
17004     return add_const_value_attribute (die, rtl);
17005   /* If the host and target are sane, try harder.  */
17006   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17007            && initializer_constant_valid_p (init, type))
17008     {
17009       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17010       if (size > 0 && (int) size == size)
17011         {
17012           unsigned char *array = (unsigned char *)
17013             ggc_alloc_cleared_atomic (size);
17014
17015           if (native_encode_initializer (init, array, size))
17016             {
17017               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17018               return true;
17019             }
17020         }
17021     }
17022   return false;
17023 }
17024
17025 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17026    attribute is the const value of T, where T is an integral constant
17027    variable with static storage duration
17028    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17029
17030 static bool
17031 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17032 {
17033
17034   if (!decl
17035       || (TREE_CODE (decl) != VAR_DECL
17036           && TREE_CODE (decl) != CONST_DECL))
17037     return false;
17038
17039     if (TREE_READONLY (decl)
17040         && ! TREE_THIS_VOLATILE (decl)
17041         && DECL_INITIAL (decl))
17042       /* OK */;
17043     else
17044       return false;
17045
17046   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17047   if (get_AT (var_die, DW_AT_const_value))
17048     return false;
17049
17050   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17051 }
17052
17053 /* Convert the CFI instructions for the current function into a
17054    location list.  This is used for DW_AT_frame_base when we targeting
17055    a dwarf2 consumer that does not support the dwarf3
17056    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17057    expressions.  */
17058
17059 static dw_loc_list_ref
17060 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17061 {
17062   dw_fde_ref fde;
17063   dw_loc_list_ref list, *list_tail;
17064   dw_cfi_ref cfi;
17065   dw_cfa_location last_cfa, next_cfa;
17066   const char *start_label, *last_label, *section;
17067   dw_cfa_location remember;
17068
17069   fde = current_fde ();
17070   gcc_assert (fde != NULL);
17071
17072   section = secname_for_decl (current_function_decl);
17073   list_tail = &list;
17074   list = NULL;
17075
17076   memset (&next_cfa, 0, sizeof (next_cfa));
17077   next_cfa.reg = INVALID_REGNUM;
17078   remember = next_cfa;
17079
17080   start_label = fde->dw_fde_begin;
17081
17082   /* ??? Bald assumption that the CIE opcode list does not contain
17083      advance opcodes.  */
17084   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17085     lookup_cfa_1 (cfi, &next_cfa, &remember);
17086
17087   last_cfa = next_cfa;
17088   last_label = start_label;
17089
17090   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17091     switch (cfi->dw_cfi_opc)
17092       {
17093       case DW_CFA_set_loc:
17094       case DW_CFA_advance_loc1:
17095       case DW_CFA_advance_loc2:
17096       case DW_CFA_advance_loc4:
17097         if (!cfa_equal_p (&last_cfa, &next_cfa))
17098           {
17099             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17100                                        start_label, last_label, section);
17101
17102             list_tail = &(*list_tail)->dw_loc_next;
17103             last_cfa = next_cfa;
17104             start_label = last_label;
17105           }
17106         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17107         break;
17108
17109       case DW_CFA_advance_loc:
17110         /* The encoding is complex enough that we should never emit this.  */
17111         gcc_unreachable ();
17112
17113       default:
17114         lookup_cfa_1 (cfi, &next_cfa, &remember);
17115         break;
17116       }
17117
17118   if (!cfa_equal_p (&last_cfa, &next_cfa))
17119     {
17120       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17121                                  start_label, last_label, section);
17122       list_tail = &(*list_tail)->dw_loc_next;
17123       start_label = last_label;
17124     }
17125
17126   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17127                              start_label, fde->dw_fde_end, section);
17128
17129   if (list && list->dw_loc_next)
17130     gen_llsym (list);
17131
17132   return list;
17133 }
17134
17135 /* Compute a displacement from the "steady-state frame pointer" to the
17136    frame base (often the same as the CFA), and store it in
17137    frame_pointer_fb_offset.  OFFSET is added to the displacement
17138    before the latter is negated.  */
17139
17140 static void
17141 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17142 {
17143   rtx reg, elim;
17144
17145 #ifdef FRAME_POINTER_CFA_OFFSET
17146   reg = frame_pointer_rtx;
17147   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17148 #else
17149   reg = arg_pointer_rtx;
17150   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17151 #endif
17152
17153   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17154   if (GET_CODE (elim) == PLUS)
17155     {
17156       offset += INTVAL (XEXP (elim, 1));
17157       elim = XEXP (elim, 0);
17158     }
17159
17160   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17161                && (elim == hard_frame_pointer_rtx
17162                    || elim == stack_pointer_rtx))
17163               || elim == (frame_pointer_needed
17164                           ? hard_frame_pointer_rtx
17165                           : stack_pointer_rtx));
17166
17167   frame_pointer_fb_offset = -offset;
17168 }
17169
17170 /* Generate a DW_AT_name attribute given some string value to be included as
17171    the value of the attribute.  */
17172
17173 static void
17174 add_name_attribute (dw_die_ref die, const char *name_string)
17175 {
17176   if (name_string != NULL && *name_string != 0)
17177     {
17178       if (demangle_name_func)
17179         name_string = (*demangle_name_func) (name_string);
17180
17181       add_AT_string (die, DW_AT_name, name_string);
17182     }
17183 }
17184
17185 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17186
17187 static void
17188 add_comp_dir_attribute (dw_die_ref die)
17189 {
17190   const char *wd = get_src_pwd ();
17191   char *wd1;
17192
17193   if (wd == NULL)
17194     return;
17195
17196   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17197     {
17198       int wdlen;
17199
17200       wdlen = strlen (wd);
17201       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17202       strcpy (wd1, wd);
17203       wd1 [wdlen] = DIR_SEPARATOR;
17204       wd1 [wdlen + 1] = 0;
17205       wd = wd1;
17206     }
17207
17208     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17209 }
17210
17211 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17212    default.  */
17213
17214 static int
17215 lower_bound_default (void)
17216 {
17217   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17218     {
17219     case DW_LANG_C:
17220     case DW_LANG_C89:
17221     case DW_LANG_C99:
17222     case DW_LANG_C_plus_plus:
17223     case DW_LANG_ObjC:
17224     case DW_LANG_ObjC_plus_plus:
17225     case DW_LANG_Java:
17226       return 0;
17227     case DW_LANG_Fortran77:
17228     case DW_LANG_Fortran90:
17229     case DW_LANG_Fortran95:
17230       return 1;
17231     case DW_LANG_UPC:
17232     case DW_LANG_D:
17233     case DW_LANG_Python:
17234       return dwarf_version >= 4 ? 0 : -1;
17235     case DW_LANG_Ada95:
17236     case DW_LANG_Ada83:
17237     case DW_LANG_Cobol74:
17238     case DW_LANG_Cobol85:
17239     case DW_LANG_Pascal83:
17240     case DW_LANG_Modula2:
17241     case DW_LANG_PLI:
17242       return dwarf_version >= 4 ? 1 : -1;
17243     default:
17244       return -1;
17245     }
17246 }
17247
17248 /* Given a tree node describing an array bound (either lower or upper) output
17249    a representation for that bound.  */
17250
17251 static void
17252 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17253 {
17254   switch (TREE_CODE (bound))
17255     {
17256     case ERROR_MARK:
17257       return;
17258
17259     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17260     case INTEGER_CST:
17261       {
17262         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17263         int dflt;
17264
17265         /* Use the default if possible.  */
17266         if (bound_attr == DW_AT_lower_bound
17267             && host_integerp (bound, 0)
17268             && (dflt = lower_bound_default ()) != -1
17269             && tree_low_cst (bound, 0) == dflt)
17270           ;
17271
17272         /* Otherwise represent the bound as an unsigned value with the
17273            precision of its type.  The precision and signedness of the
17274            type will be necessary to re-interpret it unambiguously.  */
17275         else if (prec < HOST_BITS_PER_WIDE_INT)
17276           {
17277             unsigned HOST_WIDE_INT mask
17278               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17279             add_AT_unsigned (subrange_die, bound_attr,
17280                              TREE_INT_CST_LOW (bound) & mask);
17281           }
17282         else if (prec == HOST_BITS_PER_WIDE_INT
17283                  || TREE_INT_CST_HIGH (bound) == 0)
17284           add_AT_unsigned (subrange_die, bound_attr,
17285                            TREE_INT_CST_LOW (bound));
17286         else
17287           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17288                          TREE_INT_CST_LOW (bound));
17289       }
17290       break;
17291
17292     CASE_CONVERT:
17293     case VIEW_CONVERT_EXPR:
17294       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17295       break;
17296
17297     case SAVE_EXPR:
17298       break;
17299
17300     case VAR_DECL:
17301     case PARM_DECL:
17302     case RESULT_DECL:
17303       {
17304         dw_die_ref decl_die = lookup_decl_die (bound);
17305
17306         /* ??? Can this happen, or should the variable have been bound
17307            first?  Probably it can, since I imagine that we try to create
17308            the types of parameters in the order in which they exist in
17309            the list, and won't have created a forward reference to a
17310            later parameter.  */
17311         if (decl_die != NULL)
17312           {
17313             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17314             break;
17315           }
17316       }
17317       /* FALLTHRU */
17318
17319     default:
17320       {
17321         /* Otherwise try to create a stack operation procedure to
17322            evaluate the value of the array bound.  */
17323
17324         dw_die_ref ctx, decl_die;
17325         dw_loc_list_ref list;
17326
17327         list = loc_list_from_tree (bound, 2);
17328         if (list == NULL || single_element_loc_list_p (list))
17329           {
17330             /* If DW_AT_*bound is not a reference nor constant, it is
17331                a DWARF expression rather than location description.
17332                For that loc_list_from_tree (bound, 0) is needed.
17333                If that fails to give a single element list,
17334                fall back to outputting this as a reference anyway.  */
17335             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17336             if (list2 && single_element_loc_list_p (list2))
17337               {
17338                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17339                 break;
17340               }
17341           }
17342         if (list == NULL)
17343           break;
17344
17345         if (current_function_decl == 0)
17346           ctx = comp_unit_die ();
17347         else
17348           ctx = lookup_decl_die (current_function_decl);
17349
17350         decl_die = new_die (DW_TAG_variable, ctx, bound);
17351         add_AT_flag (decl_die, DW_AT_artificial, 1);
17352         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17353         add_AT_location_description (decl_die, DW_AT_location, list);
17354         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17355         break;
17356       }
17357     }
17358 }
17359
17360 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17361    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17362    Note that the block of subscript information for an array type also
17363    includes information about the element type of the given array type.  */
17364
17365 static void
17366 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17367 {
17368   unsigned dimension_number;
17369   tree lower, upper;
17370   dw_die_ref subrange_die;
17371
17372   for (dimension_number = 0;
17373        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17374        type = TREE_TYPE (type), dimension_number++)
17375     {
17376       tree domain = TYPE_DOMAIN (type);
17377
17378       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17379         break;
17380
17381       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17382          and (in GNU C only) variable bounds.  Handle all three forms
17383          here.  */
17384       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17385       if (domain)
17386         {
17387           /* We have an array type with specified bounds.  */
17388           lower = TYPE_MIN_VALUE (domain);
17389           upper = TYPE_MAX_VALUE (domain);
17390
17391           /* Define the index type.  */
17392           if (TREE_TYPE (domain))
17393             {
17394               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17395                  TREE_TYPE field.  We can't emit debug info for this
17396                  because it is an unnamed integral type.  */
17397               if (TREE_CODE (domain) == INTEGER_TYPE
17398                   && TYPE_NAME (domain) == NULL_TREE
17399                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17400                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17401                 ;
17402               else
17403                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17404                                     type_die);
17405             }
17406
17407           /* ??? If upper is NULL, the array has unspecified length,
17408              but it does have a lower bound.  This happens with Fortran
17409                dimension arr(N:*)
17410              Since the debugger is definitely going to need to know N
17411              to produce useful results, go ahead and output the lower
17412              bound solo, and hope the debugger can cope.  */
17413
17414           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17415           if (upper)
17416             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17417         }
17418
17419       /* Otherwise we have an array type with an unspecified length.  The
17420          DWARF-2 spec does not say how to handle this; let's just leave out the
17421          bounds.  */
17422     }
17423 }
17424
17425 static void
17426 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17427 {
17428   unsigned size;
17429
17430   switch (TREE_CODE (tree_node))
17431     {
17432     case ERROR_MARK:
17433       size = 0;
17434       break;
17435     case ENUMERAL_TYPE:
17436     case RECORD_TYPE:
17437     case UNION_TYPE:
17438     case QUAL_UNION_TYPE:
17439       size = int_size_in_bytes (tree_node);
17440       break;
17441     case FIELD_DECL:
17442       /* For a data member of a struct or union, the DW_AT_byte_size is
17443          generally given as the number of bytes normally allocated for an
17444          object of the *declared* type of the member itself.  This is true
17445          even for bit-fields.  */
17446       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17447       break;
17448     default:
17449       gcc_unreachable ();
17450     }
17451
17452   /* Note that `size' might be -1 when we get to this point.  If it is, that
17453      indicates that the byte size of the entity in question is variable.  We
17454      have no good way of expressing this fact in Dwarf at the present time,
17455      so just let the -1 pass on through.  */
17456   add_AT_unsigned (die, DW_AT_byte_size, size);
17457 }
17458
17459 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17460    which specifies the distance in bits from the highest order bit of the
17461    "containing object" for the bit-field to the highest order bit of the
17462    bit-field itself.
17463
17464    For any given bit-field, the "containing object" is a hypothetical object
17465    (of some integral or enum type) within which the given bit-field lives.  The
17466    type of this hypothetical "containing object" is always the same as the
17467    declared type of the individual bit-field itself.  The determination of the
17468    exact location of the "containing object" for a bit-field is rather
17469    complicated.  It's handled by the `field_byte_offset' function (above).
17470
17471    Note that it is the size (in bytes) of the hypothetical "containing object"
17472    which will be given in the DW_AT_byte_size attribute for this bit-field.
17473    (See `byte_size_attribute' above).  */
17474
17475 static inline void
17476 add_bit_offset_attribute (dw_die_ref die, tree decl)
17477 {
17478   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17479   tree type = DECL_BIT_FIELD_TYPE (decl);
17480   HOST_WIDE_INT bitpos_int;
17481   HOST_WIDE_INT highest_order_object_bit_offset;
17482   HOST_WIDE_INT highest_order_field_bit_offset;
17483   HOST_WIDE_INT unsigned bit_offset;
17484
17485   /* Must be a field and a bit field.  */
17486   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17487
17488   /* We can't yet handle bit-fields whose offsets are variable, so if we
17489      encounter such things, just return without generating any attribute
17490      whatsoever.  Likewise for variable or too large size.  */
17491   if (! host_integerp (bit_position (decl), 0)
17492       || ! host_integerp (DECL_SIZE (decl), 1))
17493     return;
17494
17495   bitpos_int = int_bit_position (decl);
17496
17497   /* Note that the bit offset is always the distance (in bits) from the
17498      highest-order bit of the "containing object" to the highest-order bit of
17499      the bit-field itself.  Since the "high-order end" of any object or field
17500      is different on big-endian and little-endian machines, the computation
17501      below must take account of these differences.  */
17502   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17503   highest_order_field_bit_offset = bitpos_int;
17504
17505   if (! BYTES_BIG_ENDIAN)
17506     {
17507       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17508       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17509     }
17510
17511   bit_offset
17512     = (! BYTES_BIG_ENDIAN
17513        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17514        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17515
17516   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17517 }
17518
17519 /* For a FIELD_DECL node which represents a bit field, output an attribute
17520    which specifies the length in bits of the given field.  */
17521
17522 static inline void
17523 add_bit_size_attribute (dw_die_ref die, tree decl)
17524 {
17525   /* Must be a field and a bit field.  */
17526   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17527               && DECL_BIT_FIELD_TYPE (decl));
17528
17529   if (host_integerp (DECL_SIZE (decl), 1))
17530     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17531 }
17532
17533 /* If the compiled language is ANSI C, then add a 'prototyped'
17534    attribute, if arg types are given for the parameters of a function.  */
17535
17536 static inline void
17537 add_prototyped_attribute (dw_die_ref die, tree func_type)
17538 {
17539   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17540       && TYPE_ARG_TYPES (func_type) != NULL)
17541     add_AT_flag (die, DW_AT_prototyped, 1);
17542 }
17543
17544 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17545    by looking in either the type declaration or object declaration
17546    equate table.  */
17547
17548 static inline dw_die_ref
17549 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17550 {
17551   dw_die_ref origin_die = NULL;
17552
17553   if (TREE_CODE (origin) != FUNCTION_DECL)
17554     {
17555       /* We may have gotten separated from the block for the inlined
17556          function, if we're in an exception handler or some such; make
17557          sure that the abstract function has been written out.
17558
17559          Doing this for nested functions is wrong, however; functions are
17560          distinct units, and our context might not even be inline.  */
17561       tree fn = origin;
17562
17563       if (TYPE_P (fn))
17564         fn = TYPE_STUB_DECL (fn);
17565
17566       fn = decl_function_context (fn);
17567       if (fn)
17568         dwarf2out_abstract_function (fn);
17569     }
17570
17571   if (DECL_P (origin))
17572     origin_die = lookup_decl_die (origin);
17573   else if (TYPE_P (origin))
17574     origin_die = lookup_type_die (origin);
17575
17576   /* XXX: Functions that are never lowered don't always have correct block
17577      trees (in the case of java, they simply have no block tree, in some other
17578      languages).  For these functions, there is nothing we can really do to
17579      output correct debug info for inlined functions in all cases.  Rather
17580      than die, we'll just produce deficient debug info now, in that we will
17581      have variables without a proper abstract origin.  In the future, when all
17582      functions are lowered, we should re-add a gcc_assert (origin_die)
17583      here.  */
17584
17585   if (origin_die)
17586     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17587   return origin_die;
17588 }
17589
17590 /* We do not currently support the pure_virtual attribute.  */
17591
17592 static inline void
17593 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17594 {
17595   if (DECL_VINDEX (func_decl))
17596     {
17597       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17598
17599       if (host_integerp (DECL_VINDEX (func_decl), 0))
17600         add_AT_loc (die, DW_AT_vtable_elem_location,
17601                     new_loc_descr (DW_OP_constu,
17602                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17603                                    0));
17604
17605       /* GNU extension: Record what type this method came from originally.  */
17606       if (debug_info_level > DINFO_LEVEL_TERSE
17607           && DECL_CONTEXT (func_decl))
17608         add_AT_die_ref (die, DW_AT_containing_type,
17609                         lookup_type_die (DECL_CONTEXT (func_decl)));
17610     }
17611 }
17612 \f
17613 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17614    given decl.  This used to be a vendor extension until after DWARF 4
17615    standardized it.  */
17616
17617 static void
17618 add_linkage_attr (dw_die_ref die, tree decl)
17619 {
17620   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17621
17622   /* Mimic what assemble_name_raw does with a leading '*'.  */
17623   if (name[0] == '*')
17624     name = &name[1];
17625
17626   if (dwarf_version >= 4)
17627     add_AT_string (die, DW_AT_linkage_name, name);
17628   else
17629     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17630 }
17631
17632 /* Add source coordinate attributes for the given decl.  */
17633
17634 static void
17635 add_src_coords_attributes (dw_die_ref die, tree decl)
17636 {
17637   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17638
17639   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17640   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17641 }
17642
17643 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17644
17645 static void
17646 add_linkage_name (dw_die_ref die, tree decl)
17647 {
17648   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17649        && TREE_PUBLIC (decl)
17650        && !DECL_ABSTRACT (decl)
17651        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17652        && die->die_tag != DW_TAG_member)
17653     {
17654       /* Defer until we have an assembler name set.  */
17655       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17656         {
17657           limbo_die_node *asm_name;
17658
17659           asm_name = ggc_alloc_cleared_limbo_die_node ();
17660           asm_name->die = die;
17661           asm_name->created_for = decl;
17662           asm_name->next = deferred_asm_name;
17663           deferred_asm_name = asm_name;
17664         }
17665       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17666         add_linkage_attr (die, decl);
17667     }
17668 }
17669
17670 /* Add a DW_AT_name attribute and source coordinate attribute for the
17671    given decl, but only if it actually has a name.  */
17672
17673 static void
17674 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17675 {
17676   tree decl_name;
17677
17678   decl_name = DECL_NAME (decl);
17679   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17680     {
17681       const char *name = dwarf2_name (decl, 0);
17682       if (name)
17683         add_name_attribute (die, name);
17684       if (! DECL_ARTIFICIAL (decl))
17685         add_src_coords_attributes (die, decl);
17686
17687       add_linkage_name (die, decl);
17688     }
17689
17690 #ifdef VMS_DEBUGGING_INFO
17691   /* Get the function's name, as described by its RTL.  This may be different
17692      from the DECL_NAME name used in the source file.  */
17693   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17694     {
17695       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17696                    XEXP (DECL_RTL (decl), 0));
17697       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17698     }
17699 #endif /* VMS_DEBUGGING_INFO */
17700 }
17701
17702 #ifdef VMS_DEBUGGING_INFO
17703 /* Output the debug main pointer die for VMS */
17704
17705 void
17706 dwarf2out_vms_debug_main_pointer (void)
17707 {
17708   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17709   dw_die_ref die;
17710
17711   /* Allocate the VMS debug main subprogram die.  */
17712   die = ggc_alloc_cleared_die_node ();
17713   die->die_tag = DW_TAG_subprogram;
17714   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17715   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17716                                current_function_funcdef_no);
17717   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17718
17719   /* Make it the first child of comp_unit_die ().  */
17720   die->die_parent = comp_unit_die ();
17721   if (comp_unit_die ()->die_child)
17722     {
17723       die->die_sib = comp_unit_die ()->die_child->die_sib;
17724       comp_unit_die ()->die_child->die_sib = die;
17725     }
17726   else
17727     {
17728       die->die_sib = die;
17729       comp_unit_die ()->die_child = die;
17730     }
17731 }
17732 #endif /* VMS_DEBUGGING_INFO */
17733
17734 /* Push a new declaration scope.  */
17735
17736 static void
17737 push_decl_scope (tree scope)
17738 {
17739   VEC_safe_push (tree, gc, decl_scope_table, scope);
17740 }
17741
17742 /* Pop a declaration scope.  */
17743
17744 static inline void
17745 pop_decl_scope (void)
17746 {
17747   VEC_pop (tree, decl_scope_table);
17748 }
17749
17750 /* Return the DIE for the scope that immediately contains this type.
17751    Non-named types get global scope.  Named types nested in other
17752    types get their containing scope if it's open, or global scope
17753    otherwise.  All other types (i.e. function-local named types) get
17754    the current active scope.  */
17755
17756 static dw_die_ref
17757 scope_die_for (tree t, dw_die_ref context_die)
17758 {
17759   dw_die_ref scope_die = NULL;
17760   tree containing_scope;
17761   int i;
17762
17763   /* Non-types always go in the current scope.  */
17764   gcc_assert (TYPE_P (t));
17765
17766   containing_scope = TYPE_CONTEXT (t);
17767
17768   /* Use the containing namespace if it was passed in (for a declaration).  */
17769   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17770     {
17771       if (context_die == lookup_decl_die (containing_scope))
17772         /* OK */;
17773       else
17774         containing_scope = NULL_TREE;
17775     }
17776
17777   /* Ignore function type "scopes" from the C frontend.  They mean that
17778      a tagged type is local to a parmlist of a function declarator, but
17779      that isn't useful to DWARF.  */
17780   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17781     containing_scope = NULL_TREE;
17782
17783   if (SCOPE_FILE_SCOPE_P (containing_scope))
17784     scope_die = comp_unit_die ();
17785   else if (TYPE_P (containing_scope))
17786     {
17787       /* For types, we can just look up the appropriate DIE.  But
17788          first we check to see if we're in the middle of emitting it
17789          so we know where the new DIE should go.  */
17790       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17791         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17792           break;
17793
17794       if (i < 0)
17795         {
17796           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17797                       || TREE_ASM_WRITTEN (containing_scope));
17798           /*We are not in the middle of emitting the type
17799             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17800           scope_die = lookup_type_die (containing_scope);
17801
17802           /* If none of the current dies are suitable, we get file scope.  */
17803           if (scope_die == NULL)
17804             scope_die = comp_unit_die ();
17805         }
17806       else
17807         scope_die = lookup_type_die (containing_scope);
17808     }
17809   else
17810     scope_die = context_die;
17811
17812   return scope_die;
17813 }
17814
17815 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17816
17817 static inline int
17818 local_scope_p (dw_die_ref context_die)
17819 {
17820   for (; context_die; context_die = context_die->die_parent)
17821     if (context_die->die_tag == DW_TAG_inlined_subroutine
17822         || context_die->die_tag == DW_TAG_subprogram)
17823       return 1;
17824
17825   return 0;
17826 }
17827
17828 /* Returns nonzero if CONTEXT_DIE is a class.  */
17829
17830 static inline int
17831 class_scope_p (dw_die_ref context_die)
17832 {
17833   return (context_die
17834           && (context_die->die_tag == DW_TAG_structure_type
17835               || context_die->die_tag == DW_TAG_class_type
17836               || context_die->die_tag == DW_TAG_interface_type
17837               || context_die->die_tag == DW_TAG_union_type));
17838 }
17839
17840 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17841    whether or not to treat a DIE in this context as a declaration.  */
17842
17843 static inline int
17844 class_or_namespace_scope_p (dw_die_ref context_die)
17845 {
17846   return (class_scope_p (context_die)
17847           || (context_die && context_die->die_tag == DW_TAG_namespace));
17848 }
17849
17850 /* Many forms of DIEs require a "type description" attribute.  This
17851    routine locates the proper "type descriptor" die for the type given
17852    by 'type', and adds a DW_AT_type attribute below the given die.  */
17853
17854 static void
17855 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17856                     int decl_volatile, dw_die_ref context_die)
17857 {
17858   enum tree_code code  = TREE_CODE (type);
17859   dw_die_ref type_die  = NULL;
17860
17861   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17862      or fixed-point type, use the inner type.  This is because we have no
17863      support for unnamed types in base_type_die.  This can happen if this is
17864      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17865   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17866       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17867     type = TREE_TYPE (type), code = TREE_CODE (type);
17868
17869   if (code == ERROR_MARK
17870       /* Handle a special case.  For functions whose return type is void, we
17871          generate *no* type attribute.  (Note that no object may have type
17872          `void', so this only applies to function return types).  */
17873       || code == VOID_TYPE)
17874     return;
17875
17876   type_die = modified_type_die (type,
17877                                 decl_const || TYPE_READONLY (type),
17878                                 decl_volatile || TYPE_VOLATILE (type),
17879                                 context_die);
17880
17881   if (type_die != NULL)
17882     add_AT_die_ref (object_die, DW_AT_type, type_die);
17883 }
17884
17885 /* Given an object die, add the calling convention attribute for the
17886    function call type.  */
17887 static void
17888 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17889 {
17890   enum dwarf_calling_convention value = DW_CC_normal;
17891
17892   value = ((enum dwarf_calling_convention)
17893            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17894
17895   if (is_fortran ()
17896       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17897     {
17898       /* DWARF 2 doesn't provide a way to identify a program's source-level
17899         entry point.  DW_AT_calling_convention attributes are only meant
17900         to describe functions' calling conventions.  However, lacking a
17901         better way to signal the Fortran main program, we used this for 
17902         a long time, following existing custom.  Now, DWARF 4 has 
17903         DW_AT_main_subprogram, which we add below, but some tools still
17904         rely on the old way, which we thus keep.  */
17905       value = DW_CC_program;
17906
17907       if (dwarf_version >= 4 || !dwarf_strict)
17908         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17909     }
17910
17911   /* Only add the attribute if the backend requests it, and
17912      is not DW_CC_normal.  */
17913   if (value && (value != DW_CC_normal))
17914     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17915 }
17916
17917 /* Given a tree pointer to a struct, class, union, or enum type node, return
17918    a pointer to the (string) tag name for the given type, or zero if the type
17919    was declared without a tag.  */
17920
17921 static const char *
17922 type_tag (const_tree type)
17923 {
17924   const char *name = 0;
17925
17926   if (TYPE_NAME (type) != 0)
17927     {
17928       tree t = 0;
17929
17930       /* Find the IDENTIFIER_NODE for the type name.  */
17931       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17932           && !TYPE_NAMELESS (type))
17933         t = TYPE_NAME (type);
17934
17935       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17936          a TYPE_DECL node, regardless of whether or not a `typedef' was
17937          involved.  */
17938       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17939                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17940         {
17941           /* We want to be extra verbose.  Don't call dwarf_name if
17942              DECL_NAME isn't set.  The default hook for decl_printable_name
17943              doesn't like that, and in this context it's correct to return
17944              0, instead of "<anonymous>" or the like.  */
17945           if (DECL_NAME (TYPE_NAME (type))
17946               && !DECL_NAMELESS (TYPE_NAME (type)))
17947             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17948         }
17949
17950       /* Now get the name as a string, or invent one.  */
17951       if (!name && t != 0)
17952         name = IDENTIFIER_POINTER (t);
17953     }
17954
17955   return (name == 0 || *name == '\0') ? 0 : name;
17956 }
17957
17958 /* Return the type associated with a data member, make a special check
17959    for bit field types.  */
17960
17961 static inline tree
17962 member_declared_type (const_tree member)
17963 {
17964   return (DECL_BIT_FIELD_TYPE (member)
17965           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17966 }
17967
17968 /* Get the decl's label, as described by its RTL. This may be different
17969    from the DECL_NAME name used in the source file.  */
17970
17971 #if 0
17972 static const char *
17973 decl_start_label (tree decl)
17974 {
17975   rtx x;
17976   const char *fnname;
17977
17978   x = DECL_RTL (decl);
17979   gcc_assert (MEM_P (x));
17980
17981   x = XEXP (x, 0);
17982   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17983
17984   fnname = XSTR (x, 0);
17985   return fnname;
17986 }
17987 #endif
17988 \f
17989 /* These routines generate the internal representation of the DIE's for
17990    the compilation unit.  Debugging information is collected by walking
17991    the declaration trees passed in from dwarf2out_decl().  */
17992
17993 static void
17994 gen_array_type_die (tree type, dw_die_ref context_die)
17995 {
17996   dw_die_ref scope_die = scope_die_for (type, context_die);
17997   dw_die_ref array_die;
17998
17999   /* GNU compilers represent multidimensional array types as sequences of one
18000      dimensional array types whose element types are themselves array types.
18001      We sometimes squish that down to a single array_type DIE with multiple
18002      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18003      say that we are allowed to do this kind of compression in C, because
18004      there is no difference between an array of arrays and a multidimensional
18005      array.  We don't do this for Ada to remain as close as possible to the
18006      actual representation, which is especially important against the language
18007      flexibilty wrt arrays of variable size.  */
18008
18009   bool collapse_nested_arrays = !is_ada ();
18010   tree element_type;
18011
18012   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18013      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18014   if (TYPE_STRING_FLAG (type)
18015       && TREE_CODE (type) == ARRAY_TYPE
18016       && is_fortran ()
18017       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18018     {
18019       HOST_WIDE_INT size;
18020
18021       array_die = new_die (DW_TAG_string_type, scope_die, type);
18022       add_name_attribute (array_die, type_tag (type));
18023       equate_type_number_to_die (type, array_die);
18024       size = int_size_in_bytes (type);
18025       if (size >= 0)
18026         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18027       else if (TYPE_DOMAIN (type) != NULL_TREE
18028                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18029                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18030         {
18031           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18032           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18033
18034           size = int_size_in_bytes (TREE_TYPE (szdecl));
18035           if (loc && size > 0)
18036             {
18037               add_AT_location_description (array_die, DW_AT_string_length, loc);
18038               if (size != DWARF2_ADDR_SIZE)
18039                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18040             }
18041         }
18042       return;
18043     }
18044
18045   /* ??? The SGI dwarf reader fails for array of array of enum types
18046      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18047      array type comes before the outer array type.  We thus call gen_type_die
18048      before we new_die and must prevent nested array types collapsing for this
18049      target.  */
18050
18051 #ifdef MIPS_DEBUGGING_INFO
18052   gen_type_die (TREE_TYPE (type), context_die);
18053   collapse_nested_arrays = false;
18054 #endif
18055
18056   array_die = new_die (DW_TAG_array_type, scope_die, type);
18057   add_name_attribute (array_die, type_tag (type));
18058   equate_type_number_to_die (type, array_die);
18059
18060   if (TREE_CODE (type) == VECTOR_TYPE)
18061     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18062
18063   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18064   if (is_fortran ()
18065       && TREE_CODE (type) == ARRAY_TYPE
18066       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18067       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18068     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18069
18070 #if 0
18071   /* We default the array ordering.  SDB will probably do
18072      the right things even if DW_AT_ordering is not present.  It's not even
18073      an issue until we start to get into multidimensional arrays anyway.  If
18074      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18075      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18076      and when we find out that we need to put these in, we will only do so
18077      for multidimensional arrays.  */
18078   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18079 #endif
18080
18081 #ifdef MIPS_DEBUGGING_INFO
18082   /* The SGI compilers handle arrays of unknown bound by setting
18083      AT_declaration and not emitting any subrange DIEs.  */
18084   if (TREE_CODE (type) == ARRAY_TYPE
18085       && ! TYPE_DOMAIN (type))
18086     add_AT_flag (array_die, DW_AT_declaration, 1);
18087   else
18088 #endif
18089   if (TREE_CODE (type) == VECTOR_TYPE)
18090     {
18091       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18092       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18093       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18094       add_bound_info (subrange_die, DW_AT_upper_bound,
18095                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18096     }
18097   else
18098     add_subscript_info (array_die, type, collapse_nested_arrays);
18099
18100   /* Add representation of the type of the elements of this array type and
18101      emit the corresponding DIE if we haven't done it already.  */
18102   element_type = TREE_TYPE (type);
18103   if (collapse_nested_arrays)
18104     while (TREE_CODE (element_type) == ARRAY_TYPE)
18105       {
18106         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18107           break;
18108         element_type = TREE_TYPE (element_type);
18109       }
18110
18111 #ifndef MIPS_DEBUGGING_INFO
18112   gen_type_die (element_type, context_die);
18113 #endif
18114
18115   add_type_attribute (array_die, element_type, 0, 0, context_die);
18116
18117   if (get_AT (array_die, DW_AT_name))
18118     add_pubtype (type, array_die);
18119 }
18120
18121 static dw_loc_descr_ref
18122 descr_info_loc (tree val, tree base_decl)
18123 {
18124   HOST_WIDE_INT size;
18125   dw_loc_descr_ref loc, loc2;
18126   enum dwarf_location_atom op;
18127
18128   if (val == base_decl)
18129     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18130
18131   switch (TREE_CODE (val))
18132     {
18133     CASE_CONVERT:
18134       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18135     case VAR_DECL:
18136       return loc_descriptor_from_tree (val, 0);
18137     case INTEGER_CST:
18138       if (host_integerp (val, 0))
18139         return int_loc_descriptor (tree_low_cst (val, 0));
18140       break;
18141     case INDIRECT_REF:
18142       size = int_size_in_bytes (TREE_TYPE (val));
18143       if (size < 0)
18144         break;
18145       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18146       if (!loc)
18147         break;
18148       if (size == DWARF2_ADDR_SIZE)
18149         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18150       else
18151         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18152       return loc;
18153     case POINTER_PLUS_EXPR:
18154     case PLUS_EXPR:
18155       if (host_integerp (TREE_OPERAND (val, 1), 1)
18156           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18157              < 16384)
18158         {
18159           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18160           if (!loc)
18161             break;
18162           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18163         }
18164       else
18165         {
18166           op = DW_OP_plus;
18167         do_binop:
18168           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18169           if (!loc)
18170             break;
18171           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18172           if (!loc2)
18173             break;
18174           add_loc_descr (&loc, loc2);
18175           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18176         }
18177       return loc;
18178     case MINUS_EXPR:
18179       op = DW_OP_minus;
18180       goto do_binop;
18181     case MULT_EXPR:
18182       op = DW_OP_mul;
18183       goto do_binop;
18184     case EQ_EXPR:
18185       op = DW_OP_eq;
18186       goto do_binop;
18187     case NE_EXPR:
18188       op = DW_OP_ne;
18189       goto do_binop;
18190     default:
18191       break;
18192     }
18193   return NULL;
18194 }
18195
18196 static void
18197 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18198                       tree val, tree base_decl)
18199 {
18200   dw_loc_descr_ref loc;
18201
18202   if (host_integerp (val, 0))
18203     {
18204       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18205       return;
18206     }
18207
18208   loc = descr_info_loc (val, base_decl);
18209   if (!loc)
18210     return;
18211
18212   add_AT_loc (die, attr, loc);
18213 }
18214
18215 /* This routine generates DIE for array with hidden descriptor, details
18216    are filled into *info by a langhook.  */
18217
18218 static void
18219 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18220                           dw_die_ref context_die)
18221 {
18222   dw_die_ref scope_die = scope_die_for (type, context_die);
18223   dw_die_ref array_die;
18224   int dim;
18225
18226   array_die = new_die (DW_TAG_array_type, scope_die, type);
18227   add_name_attribute (array_die, type_tag (type));
18228   equate_type_number_to_die (type, array_die);
18229
18230   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18231   if (is_fortran ()
18232       && info->ndimensions >= 2)
18233     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18234
18235   if (info->data_location)
18236     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18237                           info->base_decl);
18238   if (info->associated)
18239     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18240                           info->base_decl);
18241   if (info->allocated)
18242     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18243                           info->base_decl);
18244
18245   for (dim = 0; dim < info->ndimensions; dim++)
18246     {
18247       dw_die_ref subrange_die
18248         = new_die (DW_TAG_subrange_type, array_die, NULL);
18249
18250       if (info->dimen[dim].lower_bound)
18251         {
18252           /* If it is the default value, omit it.  */
18253           int dflt;
18254
18255           if (host_integerp (info->dimen[dim].lower_bound, 0)
18256               && (dflt = lower_bound_default ()) != -1
18257               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18258             ;
18259           else
18260             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18261                                   info->dimen[dim].lower_bound,
18262                                   info->base_decl);
18263         }
18264       if (info->dimen[dim].upper_bound)
18265         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18266                               info->dimen[dim].upper_bound,
18267                               info->base_decl);
18268       if (info->dimen[dim].stride)
18269         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18270                               info->dimen[dim].stride,
18271                               info->base_decl);
18272     }
18273
18274   gen_type_die (info->element_type, context_die);
18275   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18276
18277   if (get_AT (array_die, DW_AT_name))
18278     add_pubtype (type, array_die);
18279 }
18280
18281 #if 0
18282 static void
18283 gen_entry_point_die (tree decl, dw_die_ref context_die)
18284 {
18285   tree origin = decl_ultimate_origin (decl);
18286   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18287
18288   if (origin != NULL)
18289     add_abstract_origin_attribute (decl_die, origin);
18290   else
18291     {
18292       add_name_and_src_coords_attributes (decl_die, decl);
18293       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18294                           0, 0, context_die);
18295     }
18296
18297   if (DECL_ABSTRACT (decl))
18298     equate_decl_number_to_die (decl, decl_die);
18299   else
18300     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18301 }
18302 #endif
18303
18304 /* Walk through the list of incomplete types again, trying once more to
18305    emit full debugging info for them.  */
18306
18307 static void
18308 retry_incomplete_types (void)
18309 {
18310   int i;
18311
18312   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18313     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18314                                   DINFO_USAGE_DIR_USE))
18315       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18316 }
18317
18318 /* Determine what tag to use for a record type.  */
18319
18320 static enum dwarf_tag
18321 record_type_tag (tree type)
18322 {
18323   if (! lang_hooks.types.classify_record)
18324     return DW_TAG_structure_type;
18325
18326   switch (lang_hooks.types.classify_record (type))
18327     {
18328     case RECORD_IS_STRUCT:
18329       return DW_TAG_structure_type;
18330
18331     case RECORD_IS_CLASS:
18332       return DW_TAG_class_type;
18333
18334     case RECORD_IS_INTERFACE:
18335       if (dwarf_version >= 3 || !dwarf_strict)
18336         return DW_TAG_interface_type;
18337       return DW_TAG_structure_type;
18338
18339     default:
18340       gcc_unreachable ();
18341     }
18342 }
18343
18344 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18345    include all of the information about the enumeration values also. Each
18346    enumerated type name/value is listed as a child of the enumerated type
18347    DIE.  */
18348
18349 static dw_die_ref
18350 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18351 {
18352   dw_die_ref type_die = lookup_type_die (type);
18353
18354   if (type_die == NULL)
18355     {
18356       type_die = new_die (DW_TAG_enumeration_type,
18357                           scope_die_for (type, context_die), type);
18358       equate_type_number_to_die (type, type_die);
18359       add_name_attribute (type_die, type_tag (type));
18360       if (dwarf_version >= 4 || !dwarf_strict)
18361         {
18362           if (ENUM_IS_SCOPED (type))
18363             add_AT_flag (type_die, DW_AT_enum_class, 1);
18364           if (ENUM_IS_OPAQUE (type))
18365             add_AT_flag (type_die, DW_AT_declaration, 1);
18366         }
18367     }
18368   else if (! TYPE_SIZE (type))
18369     return type_die;
18370   else
18371     remove_AT (type_die, DW_AT_declaration);
18372
18373   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18374      given enum type is incomplete, do not generate the DW_AT_byte_size
18375      attribute or the DW_AT_element_list attribute.  */
18376   if (TYPE_SIZE (type))
18377     {
18378       tree link;
18379
18380       TREE_ASM_WRITTEN (type) = 1;
18381       add_byte_size_attribute (type_die, type);
18382       if (TYPE_STUB_DECL (type) != NULL_TREE)
18383         {
18384           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18385           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18386         }
18387
18388       /* If the first reference to this type was as the return type of an
18389          inline function, then it may not have a parent.  Fix this now.  */
18390       if (type_die->die_parent == NULL)
18391         add_child_die (scope_die_for (type, context_die), type_die);
18392
18393       for (link = TYPE_VALUES (type);
18394            link != NULL; link = TREE_CHAIN (link))
18395         {
18396           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18397           tree value = TREE_VALUE (link);
18398
18399           add_name_attribute (enum_die,
18400                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18401
18402           if (TREE_CODE (value) == CONST_DECL)
18403             value = DECL_INITIAL (value);
18404
18405           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18406             /* DWARF2 does not provide a way of indicating whether or
18407                not enumeration constants are signed or unsigned.  GDB
18408                always assumes the values are signed, so we output all
18409                values as if they were signed.  That means that
18410                enumeration constants with very large unsigned values
18411                will appear to have negative values in the debugger.  */
18412             add_AT_int (enum_die, DW_AT_const_value,
18413                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18414         }
18415     }
18416   else
18417     add_AT_flag (type_die, DW_AT_declaration, 1);
18418
18419   if (get_AT (type_die, DW_AT_name))
18420     add_pubtype (type, type_die);
18421
18422   return type_die;
18423 }
18424
18425 /* Generate a DIE to represent either a real live formal parameter decl or to
18426    represent just the type of some formal parameter position in some function
18427    type.
18428
18429    Note that this routine is a bit unusual because its argument may be a
18430    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18431    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18432    node.  If it's the former then this function is being called to output a
18433    DIE to represent a formal parameter object (or some inlining thereof).  If
18434    it's the latter, then this function is only being called to output a
18435    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18436    argument type of some subprogram type.
18437    If EMIT_NAME_P is true, name and source coordinate attributes
18438    are emitted.  */
18439
18440 static dw_die_ref
18441 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18442                           dw_die_ref context_die)
18443 {
18444   tree node_or_origin = node ? node : origin;
18445   tree ultimate_origin;
18446   dw_die_ref parm_die
18447     = new_die (DW_TAG_formal_parameter, context_die, node);
18448
18449   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18450     {
18451     case tcc_declaration:
18452       ultimate_origin = decl_ultimate_origin (node_or_origin);
18453       if (node || ultimate_origin)
18454         origin = ultimate_origin;
18455       if (origin != NULL)
18456         add_abstract_origin_attribute (parm_die, origin);
18457       else if (emit_name_p)
18458         add_name_and_src_coords_attributes (parm_die, node);
18459       if (origin == NULL
18460           || (! DECL_ABSTRACT (node_or_origin)
18461               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18462                                            decl_function_context
18463                                                             (node_or_origin))))
18464         {
18465           tree type = TREE_TYPE (node_or_origin);
18466           if (decl_by_reference_p (node_or_origin))
18467             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18468                                 context_die);
18469           else
18470             add_type_attribute (parm_die, type,
18471                                 TREE_READONLY (node_or_origin),
18472                                 TREE_THIS_VOLATILE (node_or_origin),
18473                                 context_die);
18474         }
18475       if (origin == NULL && DECL_ARTIFICIAL (node))
18476         add_AT_flag (parm_die, DW_AT_artificial, 1);
18477
18478       if (node && node != origin)
18479         equate_decl_number_to_die (node, parm_die);
18480       if (! DECL_ABSTRACT (node_or_origin))
18481         add_location_or_const_value_attribute (parm_die, node_or_origin,
18482                                                DW_AT_location);
18483
18484       break;
18485
18486     case tcc_type:
18487       /* We were called with some kind of a ..._TYPE node.  */
18488       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18489       break;
18490
18491     default:
18492       gcc_unreachable ();
18493     }
18494
18495   return parm_die;
18496 }
18497
18498 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18499    children DW_TAG_formal_parameter DIEs representing the arguments of the
18500    parameter pack.
18501
18502    PARM_PACK must be a function parameter pack.
18503    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18504    must point to the subsequent arguments of the function PACK_ARG belongs to.
18505    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18506    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18507    following the last one for which a DIE was generated.  */
18508
18509 static dw_die_ref
18510 gen_formal_parameter_pack_die  (tree parm_pack,
18511                                 tree pack_arg,
18512                                 dw_die_ref subr_die,
18513                                 tree *next_arg)
18514 {
18515   tree arg;
18516   dw_die_ref parm_pack_die;
18517
18518   gcc_assert (parm_pack
18519               && lang_hooks.function_parameter_pack_p (parm_pack)
18520               && subr_die);
18521
18522   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18523   add_src_coords_attributes (parm_pack_die, parm_pack);
18524
18525   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18526     {
18527       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18528                                                                  parm_pack))
18529         break;
18530       gen_formal_parameter_die (arg, NULL,
18531                                 false /* Don't emit name attribute.  */,
18532                                 parm_pack_die);
18533     }
18534   if (next_arg)
18535     *next_arg = arg;
18536   return parm_pack_die;
18537 }
18538
18539 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18540    at the end of an (ANSI prototyped) formal parameters list.  */
18541
18542 static void
18543 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18544 {
18545   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18546 }
18547
18548 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18549    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18550    parameters as specified in some function type specification (except for
18551    those which appear as part of a function *definition*).  */
18552
18553 static void
18554 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18555 {
18556   tree link;
18557   tree formal_type = NULL;
18558   tree first_parm_type;
18559   tree arg;
18560
18561   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18562     {
18563       arg = DECL_ARGUMENTS (function_or_method_type);
18564       function_or_method_type = TREE_TYPE (function_or_method_type);
18565     }
18566   else
18567     arg = NULL_TREE;
18568
18569   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18570
18571   /* Make our first pass over the list of formal parameter types and output a
18572      DW_TAG_formal_parameter DIE for each one.  */
18573   for (link = first_parm_type; link; )
18574     {
18575       dw_die_ref parm_die;
18576
18577       formal_type = TREE_VALUE (link);
18578       if (formal_type == void_type_node)
18579         break;
18580
18581       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18582       parm_die = gen_formal_parameter_die (formal_type, NULL,
18583                                            true /* Emit name attribute.  */,
18584                                            context_die);
18585       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18586           && link == first_parm_type)
18587         {
18588           add_AT_flag (parm_die, DW_AT_artificial, 1);
18589           if (dwarf_version >= 3 || !dwarf_strict)
18590             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18591         }
18592       else if (arg && DECL_ARTIFICIAL (arg))
18593         add_AT_flag (parm_die, DW_AT_artificial, 1);
18594
18595       link = TREE_CHAIN (link);
18596       if (arg)
18597         arg = DECL_CHAIN (arg);
18598     }
18599
18600   /* If this function type has an ellipsis, add a
18601      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18602   if (formal_type != void_type_node)
18603     gen_unspecified_parameters_die (function_or_method_type, context_die);
18604
18605   /* Make our second (and final) pass over the list of formal parameter types
18606      and output DIEs to represent those types (as necessary).  */
18607   for (link = TYPE_ARG_TYPES (function_or_method_type);
18608        link && TREE_VALUE (link);
18609        link = TREE_CHAIN (link))
18610     gen_type_die (TREE_VALUE (link), context_die);
18611 }
18612
18613 /* We want to generate the DIE for TYPE so that we can generate the
18614    die for MEMBER, which has been defined; we will need to refer back
18615    to the member declaration nested within TYPE.  If we're trying to
18616    generate minimal debug info for TYPE, processing TYPE won't do the
18617    trick; we need to attach the member declaration by hand.  */
18618
18619 static void
18620 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18621 {
18622   gen_type_die (type, context_die);
18623
18624   /* If we're trying to avoid duplicate debug info, we may not have
18625      emitted the member decl for this function.  Emit it now.  */
18626   if (TYPE_STUB_DECL (type)
18627       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18628       && ! lookup_decl_die (member))
18629     {
18630       dw_die_ref type_die;
18631       gcc_assert (!decl_ultimate_origin (member));
18632
18633       push_decl_scope (type);
18634       type_die = lookup_type_die (type);
18635       if (TREE_CODE (member) == FUNCTION_DECL)
18636         gen_subprogram_die (member, type_die);
18637       else if (TREE_CODE (member) == FIELD_DECL)
18638         {
18639           /* Ignore the nameless fields that are used to skip bits but handle
18640              C++ anonymous unions and structs.  */
18641           if (DECL_NAME (member) != NULL_TREE
18642               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18643               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18644             {
18645               gen_type_die (member_declared_type (member), type_die);
18646               gen_field_die (member, type_die);
18647             }
18648         }
18649       else
18650         gen_variable_die (member, NULL_TREE, type_die);
18651
18652       pop_decl_scope ();
18653     }
18654 }
18655
18656 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18657    may later generate inlined and/or out-of-line instances of.  */
18658
18659 static void
18660 dwarf2out_abstract_function (tree decl)
18661 {
18662   dw_die_ref old_die;
18663   tree save_fn;
18664   tree context;
18665   int was_abstract;
18666   htab_t old_decl_loc_table;
18667
18668   /* Make sure we have the actual abstract inline, not a clone.  */
18669   decl = DECL_ORIGIN (decl);
18670
18671   old_die = lookup_decl_die (decl);
18672   if (old_die && get_AT (old_die, DW_AT_inline))
18673     /* We've already generated the abstract instance.  */
18674     return;
18675
18676   /* We can be called while recursively when seeing block defining inlined subroutine
18677      DIE.  Be sure to not clobber the outer location table nor use it or we would
18678      get locations in abstract instantces.  */
18679   old_decl_loc_table = decl_loc_table;
18680   decl_loc_table = NULL;
18681
18682   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18683      we don't get confused by DECL_ABSTRACT.  */
18684   if (debug_info_level > DINFO_LEVEL_TERSE)
18685     {
18686       context = decl_class_context (decl);
18687       if (context)
18688         gen_type_die_for_member
18689           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18690     }
18691
18692   /* Pretend we've just finished compiling this function.  */
18693   save_fn = current_function_decl;
18694   current_function_decl = decl;
18695   push_cfun (DECL_STRUCT_FUNCTION (decl));
18696
18697   was_abstract = DECL_ABSTRACT (decl);
18698   set_decl_abstract_flags (decl, 1);
18699   dwarf2out_decl (decl);
18700   if (! was_abstract)
18701     set_decl_abstract_flags (decl, 0);
18702
18703   current_function_decl = save_fn;
18704   decl_loc_table = old_decl_loc_table;
18705   pop_cfun ();
18706 }
18707
18708 /* Helper function of premark_used_types() which gets called through
18709    htab_traverse.
18710
18711    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18712    marked as unused by prune_unused_types.  */
18713
18714 static int
18715 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18716 {
18717   tree type;
18718   dw_die_ref die;
18719
18720   type = (tree) *slot;
18721   die = lookup_type_die (type);
18722   if (die != NULL)
18723     die->die_perennial_p = 1;
18724   return 1;
18725 }
18726
18727 /* Helper function of premark_types_used_by_global_vars which gets called
18728    through htab_traverse.
18729
18730    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18731    marked as unused by prune_unused_types. The DIE of the type is marked
18732    only if the global variable using the type will actually be emitted.  */
18733
18734 static int
18735 premark_types_used_by_global_vars_helper (void **slot,
18736                                           void *data ATTRIBUTE_UNUSED)
18737 {
18738   struct types_used_by_vars_entry *entry;
18739   dw_die_ref die;
18740
18741   entry = (struct types_used_by_vars_entry *) *slot;
18742   gcc_assert (entry->type != NULL
18743               && entry->var_decl != NULL);
18744   die = lookup_type_die (entry->type);
18745   if (die)
18746     {
18747       /* Ask cgraph if the global variable really is to be emitted.
18748          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18749       struct varpool_node *node = varpool_get_node (entry->var_decl);
18750       if (node && node->needed)
18751         {
18752           die->die_perennial_p = 1;
18753           /* Keep the parent DIEs as well.  */
18754           while ((die = die->die_parent) && die->die_perennial_p == 0)
18755             die->die_perennial_p = 1;
18756         }
18757     }
18758   return 1;
18759 }
18760
18761 /* Mark all members of used_types_hash as perennial.  */
18762
18763 static void
18764 premark_used_types (void)
18765 {
18766   if (cfun && cfun->used_types_hash)
18767     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18768 }
18769
18770 /* Mark all members of types_used_by_vars_entry as perennial.  */
18771
18772 static void
18773 premark_types_used_by_global_vars (void)
18774 {
18775   if (types_used_by_vars_hash)
18776     htab_traverse (types_used_by_vars_hash,
18777                    premark_types_used_by_global_vars_helper, NULL);
18778 }
18779
18780 /* Generate a DIE to represent a declared function (either file-scope or
18781    block-local).  */
18782
18783 static void
18784 gen_subprogram_die (tree decl, dw_die_ref context_die)
18785 {
18786   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18787   tree origin = decl_ultimate_origin (decl);
18788   dw_die_ref subr_die;
18789   tree fn_arg_types;
18790   tree outer_scope;
18791   dw_die_ref old_die = lookup_decl_die (decl);
18792   int declaration = (current_function_decl != decl
18793                      || class_or_namespace_scope_p (context_die));
18794
18795   premark_used_types ();
18796
18797   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18798      started to generate the abstract instance of an inline, decided to output
18799      its containing class, and proceeded to emit the declaration of the inline
18800      from the member list for the class.  If so, DECLARATION takes priority;
18801      we'll get back to the abstract instance when done with the class.  */
18802
18803   /* The class-scope declaration DIE must be the primary DIE.  */
18804   if (origin && declaration && class_or_namespace_scope_p (context_die))
18805     {
18806       origin = NULL;
18807       gcc_assert (!old_die);
18808     }
18809
18810   /* Now that the C++ front end lazily declares artificial member fns, we
18811      might need to retrofit the declaration into its class.  */
18812   if (!declaration && !origin && !old_die
18813       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18814       && !class_or_namespace_scope_p (context_die)
18815       && debug_info_level > DINFO_LEVEL_TERSE)
18816     old_die = force_decl_die (decl);
18817
18818   if (origin != NULL)
18819     {
18820       gcc_assert (!declaration || local_scope_p (context_die));
18821
18822       /* Fixup die_parent for the abstract instance of a nested
18823          inline function.  */
18824       if (old_die && old_die->die_parent == NULL)
18825         add_child_die (context_die, old_die);
18826
18827       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18828       add_abstract_origin_attribute (subr_die, origin);
18829     }
18830   else if (old_die)
18831     {
18832       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18833       struct dwarf_file_data * file_index = lookup_filename (s.file);
18834
18835       if (!get_AT_flag (old_die, DW_AT_declaration)
18836           /* We can have a normal definition following an inline one in the
18837              case of redefinition of GNU C extern inlines.
18838              It seems reasonable to use AT_specification in this case.  */
18839           && !get_AT (old_die, DW_AT_inline))
18840         {
18841           /* Detect and ignore this case, where we are trying to output
18842              something we have already output.  */
18843           return;
18844         }
18845
18846       /* If the definition comes from the same place as the declaration,
18847          maybe use the old DIE.  We always want the DIE for this function
18848          that has the *_pc attributes to be under comp_unit_die so the
18849          debugger can find it.  We also need to do this for abstract
18850          instances of inlines, since the spec requires the out-of-line copy
18851          to have the same parent.  For local class methods, this doesn't
18852          apply; we just use the old DIE.  */
18853       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18854           && (DECL_ARTIFICIAL (decl)
18855               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18856                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18857                       == (unsigned) s.line))))
18858         {
18859           subr_die = old_die;
18860
18861           /* Clear out the declaration attribute and the formal parameters.
18862              Do not remove all children, because it is possible that this
18863              declaration die was forced using force_decl_die(). In such
18864              cases die that forced declaration die (e.g. TAG_imported_module)
18865              is one of the children that we do not want to remove.  */
18866           remove_AT (subr_die, DW_AT_declaration);
18867           remove_AT (subr_die, DW_AT_object_pointer);
18868           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18869         }
18870       else
18871         {
18872           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18873           add_AT_specification (subr_die, old_die);
18874           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18875             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18876           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18877             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18878         }
18879     }
18880   else
18881     {
18882       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18883
18884       if (TREE_PUBLIC (decl))
18885         add_AT_flag (subr_die, DW_AT_external, 1);
18886
18887       add_name_and_src_coords_attributes (subr_die, decl);
18888       if (debug_info_level > DINFO_LEVEL_TERSE)
18889         {
18890           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18891           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18892                               0, 0, context_die);
18893         }
18894
18895       add_pure_or_virtual_attribute (subr_die, decl);
18896       if (DECL_ARTIFICIAL (decl))
18897         add_AT_flag (subr_die, DW_AT_artificial, 1);
18898
18899       add_accessibility_attribute (subr_die, decl);
18900     }
18901
18902   if (declaration)
18903     {
18904       if (!old_die || !get_AT (old_die, DW_AT_inline))
18905         {
18906           add_AT_flag (subr_die, DW_AT_declaration, 1);
18907
18908           /* If this is an explicit function declaration then generate
18909              a DW_AT_explicit attribute.  */
18910           if (lang_hooks.decls.function_decl_explicit_p (decl)
18911               && (dwarf_version >= 3 || !dwarf_strict))
18912             add_AT_flag (subr_die, DW_AT_explicit, 1);
18913
18914           /* The first time we see a member function, it is in the context of
18915              the class to which it belongs.  We make sure of this by emitting
18916              the class first.  The next time is the definition, which is
18917              handled above.  The two may come from the same source text.
18918
18919              Note that force_decl_die() forces function declaration die. It is
18920              later reused to represent definition.  */
18921           equate_decl_number_to_die (decl, subr_die);
18922         }
18923     }
18924   else if (DECL_ABSTRACT (decl))
18925     {
18926       if (DECL_DECLARED_INLINE_P (decl))
18927         {
18928           if (cgraph_function_possibly_inlined_p (decl))
18929             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18930           else
18931             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18932         }
18933       else
18934         {
18935           if (cgraph_function_possibly_inlined_p (decl))
18936             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18937           else
18938             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18939         }
18940
18941       if (DECL_DECLARED_INLINE_P (decl)
18942           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18943         add_AT_flag (subr_die, DW_AT_artificial, 1);
18944
18945       equate_decl_number_to_die (decl, subr_die);
18946     }
18947   else if (!DECL_EXTERNAL (decl))
18948     {
18949       HOST_WIDE_INT cfa_fb_offset;
18950
18951       if (!old_die || !get_AT (old_die, DW_AT_inline))
18952         equate_decl_number_to_die (decl, subr_die);
18953
18954       if (!flag_reorder_blocks_and_partition)
18955         {
18956           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18957                                        current_function_funcdef_no);
18958           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18959           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18960                                        current_function_funcdef_no);
18961           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18962
18963 #if VMS_DEBUGGING_INFO
18964       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18965          Section 2.3 Prologue and Epilogue Attributes:
18966          When a breakpoint is set on entry to a function, it is generally
18967          desirable for execution to be suspended, not on the very first
18968          instruction of the function, but rather at a point after the
18969          function's frame has been set up, after any language defined local
18970          declaration processing has been completed, and before execution of
18971          the first statement of the function begins. Debuggers generally
18972          cannot properly determine where this point is.  Similarly for a
18973          breakpoint set on exit from a function. The prologue and epilogue
18974          attributes allow a compiler to communicate the location(s) to use.  */
18975
18976       {
18977         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18978
18979         if (fde->dw_fde_vms_end_prologue)
18980           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18981             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18982
18983         if (fde->dw_fde_vms_begin_epilogue)
18984           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18985             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18986       }
18987 #endif
18988
18989           add_pubname (decl, subr_die);
18990           add_arange (decl, subr_die);
18991         }
18992       else
18993         {  /* Do nothing for now; maybe need to duplicate die, one for
18994               hot section and one for cold section, then use the hot/cold
18995               section begin/end labels to generate the aranges...  */
18996           /*
18997             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18998             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18999             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
19000             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
19001
19002             add_pubname (decl, subr_die);
19003             add_arange (decl, subr_die);
19004             add_arange (decl, subr_die);
19005            */
19006         }
19007
19008 #ifdef MIPS_DEBUGGING_INFO
19009       /* Add a reference to the FDE for this routine.  */
19010       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19011 #endif
19012
19013       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19014
19015       /* We define the "frame base" as the function's CFA.  This is more
19016          convenient for several reasons: (1) It's stable across the prologue
19017          and epilogue, which makes it better than just a frame pointer,
19018          (2) With dwarf3, there exists a one-byte encoding that allows us
19019          to reference the .debug_frame data by proxy, but failing that,
19020          (3) We can at least reuse the code inspection and interpretation
19021          code that determines the CFA position at various points in the
19022          function.  */
19023       if (dwarf_version >= 3)
19024         {
19025           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19026           add_AT_loc (subr_die, DW_AT_frame_base, op);
19027         }
19028       else
19029         {
19030           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19031           if (list->dw_loc_next)
19032             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19033           else
19034             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19035         }
19036
19037       /* Compute a displacement from the "steady-state frame pointer" to
19038          the CFA.  The former is what all stack slots and argument slots
19039          will reference in the rtl; the later is what we've told the
19040          debugger about.  We'll need to adjust all frame_base references
19041          by this displacement.  */
19042       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19043
19044       if (cfun->static_chain_decl)
19045         add_AT_location_description (subr_die, DW_AT_static_link,
19046                  loc_list_from_tree (cfun->static_chain_decl, 2));
19047     }
19048
19049   /* Generate child dies for template paramaters.  */
19050   if (debug_info_level > DINFO_LEVEL_TERSE)
19051     gen_generic_params_dies (decl);
19052
19053   /* Now output descriptions of the arguments for this function. This gets
19054      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19055      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19056      `...' at the end of the formal parameter list.  In order to find out if
19057      there was a trailing ellipsis or not, we must instead look at the type
19058      associated with the FUNCTION_DECL.  This will be a node of type
19059      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19060      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19061      an ellipsis at the end.  */
19062
19063   /* In the case where we are describing a mere function declaration, all we
19064      need to do here (and all we *can* do here) is to describe the *types* of
19065      its formal parameters.  */
19066   if (debug_info_level <= DINFO_LEVEL_TERSE)
19067     ;
19068   else if (declaration)
19069     gen_formal_types_die (decl, subr_die);
19070   else
19071     {
19072       /* Generate DIEs to represent all known formal parameters.  */
19073       tree parm = DECL_ARGUMENTS (decl);
19074       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19075       tree generic_decl_parm = generic_decl
19076                                 ? DECL_ARGUMENTS (generic_decl)
19077                                 : NULL;
19078
19079       /* Now we want to walk the list of parameters of the function and
19080          emit their relevant DIEs.
19081
19082          We consider the case of DECL being an instance of a generic function
19083          as well as it being a normal function.
19084
19085          If DECL is an instance of a generic function we walk the
19086          parameters of the generic function declaration _and_ the parameters of
19087          DECL itself. This is useful because we want to emit specific DIEs for
19088          function parameter packs and those are declared as part of the
19089          generic function declaration. In that particular case,
19090          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19091          That DIE has children DIEs representing the set of arguments
19092          of the pack. Note that the set of pack arguments can be empty.
19093          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19094          children DIE.
19095
19096          Otherwise, we just consider the parameters of DECL.  */
19097       while (generic_decl_parm || parm)
19098         {
19099           if (generic_decl_parm
19100               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19101             gen_formal_parameter_pack_die (generic_decl_parm,
19102                                            parm, subr_die,
19103                                            &parm);
19104           else if (parm)
19105             {
19106               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19107
19108               if (parm == DECL_ARGUMENTS (decl)
19109                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19110                   && parm_die
19111                   && (dwarf_version >= 3 || !dwarf_strict))
19112                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19113
19114               parm = DECL_CHAIN (parm);
19115             }
19116
19117           if (generic_decl_parm)
19118             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19119         }
19120
19121       /* Decide whether we need an unspecified_parameters DIE at the end.
19122          There are 2 more cases to do this for: 1) the ansi ... declaration -
19123          this is detectable when the end of the arg list is not a
19124          void_type_node 2) an unprototyped function declaration (not a
19125          definition).  This just means that we have no info about the
19126          parameters at all.  */
19127       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
19128       if (fn_arg_types != NULL)
19129         {
19130           /* This is the prototyped case, check for....  */
19131           if (stdarg_p (TREE_TYPE (decl)))
19132             gen_unspecified_parameters_die (decl, subr_die);
19133         }
19134       else if (DECL_INITIAL (decl) == NULL_TREE)
19135         gen_unspecified_parameters_die (decl, subr_die);
19136     }
19137
19138   /* Output Dwarf info for all of the stuff within the body of the function
19139      (if it has one - it may be just a declaration).  */
19140   outer_scope = DECL_INITIAL (decl);
19141
19142   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19143      a function.  This BLOCK actually represents the outermost binding contour
19144      for the function, i.e. the contour in which the function's formal
19145      parameters and labels get declared. Curiously, it appears that the front
19146      end doesn't actually put the PARM_DECL nodes for the current function onto
19147      the BLOCK_VARS list for this outer scope, but are strung off of the
19148      DECL_ARGUMENTS list for the function instead.
19149
19150      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19151      the LABEL_DECL nodes for the function however, and we output DWARF info
19152      for those in decls_for_scope.  Just within the `outer_scope' there will be
19153      a BLOCK node representing the function's outermost pair of curly braces,
19154      and any blocks used for the base and member initializers of a C++
19155      constructor function.  */
19156   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19157     {
19158       /* Emit a DW_TAG_variable DIE for a named return value.  */
19159       if (DECL_NAME (DECL_RESULT (decl)))
19160         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19161
19162       current_function_has_inlines = 0;
19163       decls_for_scope (outer_scope, subr_die, 0);
19164     }
19165   /* Add the calling convention attribute if requested.  */
19166   add_calling_convention_attribute (subr_die, decl);
19167
19168 }
19169
19170 /* Returns a hash value for X (which really is a die_struct).  */
19171
19172 static hashval_t
19173 common_block_die_table_hash (const void *x)
19174 {
19175   const_dw_die_ref d = (const_dw_die_ref) x;
19176   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19177 }
19178
19179 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19180    as decl_id and die_parent of die_struct Y.  */
19181
19182 static int
19183 common_block_die_table_eq (const void *x, const void *y)
19184 {
19185   const_dw_die_ref d = (const_dw_die_ref) x;
19186   const_dw_die_ref e = (const_dw_die_ref) y;
19187   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19188 }
19189
19190 /* Generate a DIE to represent a declared data object.
19191    Either DECL or ORIGIN must be non-null.  */
19192
19193 static void
19194 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19195 {
19196   HOST_WIDE_INT off;
19197   tree com_decl;
19198   tree decl_or_origin = decl ? decl : origin;
19199   tree ultimate_origin;
19200   dw_die_ref var_die;
19201   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19202   dw_die_ref origin_die;
19203   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19204                       || class_or_namespace_scope_p (context_die));
19205   bool specialization_p = false;
19206
19207   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19208   if (decl || ultimate_origin)
19209     origin = ultimate_origin;
19210   com_decl = fortran_common (decl_or_origin, &off);
19211
19212   /* Symbol in common gets emitted as a child of the common block, in the form
19213      of a data member.  */
19214   if (com_decl)
19215     {
19216       dw_die_ref com_die;
19217       dw_loc_list_ref loc;
19218       die_node com_die_arg;
19219
19220       var_die = lookup_decl_die (decl_or_origin);
19221       if (var_die)
19222         {
19223           if (get_AT (var_die, DW_AT_location) == NULL)
19224             {
19225               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19226               if (loc)
19227                 {
19228                   if (off)
19229                     {
19230                       /* Optimize the common case.  */
19231                       if (single_element_loc_list_p (loc)
19232                           && loc->expr->dw_loc_opc == DW_OP_addr
19233                           && loc->expr->dw_loc_next == NULL
19234                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19235                              == SYMBOL_REF)
19236                         loc->expr->dw_loc_oprnd1.v.val_addr
19237                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19238                         else
19239                           loc_list_plus_const (loc, off);
19240                     }
19241                   add_AT_location_description (var_die, DW_AT_location, loc);
19242                   remove_AT (var_die, DW_AT_declaration);
19243                 }
19244             }
19245           return;
19246         }
19247
19248       if (common_block_die_table == NULL)
19249         common_block_die_table
19250           = htab_create_ggc (10, common_block_die_table_hash,
19251                              common_block_die_table_eq, NULL);
19252
19253       com_die_arg.decl_id = DECL_UID (com_decl);
19254       com_die_arg.die_parent = context_die;
19255       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19256       loc = loc_list_from_tree (com_decl, 2);
19257       if (com_die == NULL)
19258         {
19259           const char *cnam
19260             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19261           void **slot;
19262
19263           com_die = new_die (DW_TAG_common_block, context_die, decl);
19264           add_name_and_src_coords_attributes (com_die, com_decl);
19265           if (loc)
19266             {
19267               add_AT_location_description (com_die, DW_AT_location, loc);
19268               /* Avoid sharing the same loc descriptor between
19269                  DW_TAG_common_block and DW_TAG_variable.  */
19270               loc = loc_list_from_tree (com_decl, 2);
19271             }
19272           else if (DECL_EXTERNAL (decl))
19273             add_AT_flag (com_die, DW_AT_declaration, 1);
19274           add_pubname_string (cnam, com_die); /* ??? needed? */
19275           com_die->decl_id = DECL_UID (com_decl);
19276           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19277           *slot = (void *) com_die;
19278         }
19279       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19280         {
19281           add_AT_location_description (com_die, DW_AT_location, loc);
19282           loc = loc_list_from_tree (com_decl, 2);
19283           remove_AT (com_die, DW_AT_declaration);
19284         }
19285       var_die = new_die (DW_TAG_variable, com_die, decl);
19286       add_name_and_src_coords_attributes (var_die, decl);
19287       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19288                           TREE_THIS_VOLATILE (decl), context_die);
19289       add_AT_flag (var_die, DW_AT_external, 1);
19290       if (loc)
19291         {
19292           if (off)
19293             {
19294               /* Optimize the common case.  */
19295               if (single_element_loc_list_p (loc)
19296                   && loc->expr->dw_loc_opc == DW_OP_addr
19297                   && loc->expr->dw_loc_next == NULL
19298                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19299                 loc->expr->dw_loc_oprnd1.v.val_addr
19300                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19301               else
19302                 loc_list_plus_const (loc, off);
19303             }
19304           add_AT_location_description (var_die, DW_AT_location, loc);
19305         }
19306       else if (DECL_EXTERNAL (decl))
19307         add_AT_flag (var_die, DW_AT_declaration, 1);
19308       equate_decl_number_to_die (decl, var_die);
19309       return;
19310     }
19311
19312   /* If the compiler emitted a definition for the DECL declaration
19313      and if we already emitted a DIE for it, don't emit a second
19314      DIE for it again. Allow re-declarations of DECLs that are
19315      inside functions, though.  */
19316   if (old_die && declaration && !local_scope_p (context_die))
19317     return;
19318
19319   /* For static data members, the declaration in the class is supposed
19320      to have DW_TAG_member tag; the specification should still be
19321      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19322   if (declaration && class_scope_p (context_die))
19323     var_die = new_die (DW_TAG_member, context_die, decl);
19324   else
19325     var_die = new_die (DW_TAG_variable, context_die, decl);
19326
19327   origin_die = NULL;
19328   if (origin != NULL)
19329     origin_die = add_abstract_origin_attribute (var_die, origin);
19330
19331   /* Loop unrolling can create multiple blocks that refer to the same
19332      static variable, so we must test for the DW_AT_declaration flag.
19333
19334      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19335      copy decls and set the DECL_ABSTRACT flag on them instead of
19336      sharing them.
19337
19338      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19339
19340      ??? The declare_in_namespace support causes us to get two DIEs for one
19341      variable, both of which are declarations.  We want to avoid considering
19342      one to be a specification, so we must test that this DIE is not a
19343      declaration.  */
19344   else if (old_die && TREE_STATIC (decl) && ! declaration
19345            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19346     {
19347       /* This is a definition of a C++ class level static.  */
19348       add_AT_specification (var_die, old_die);
19349       specialization_p = true;
19350       if (DECL_NAME (decl))
19351         {
19352           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19353           struct dwarf_file_data * file_index = lookup_filename (s.file);
19354
19355           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19356             add_AT_file (var_die, DW_AT_decl_file, file_index);
19357
19358           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19359             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19360
19361           if (old_die->die_tag == DW_TAG_member)
19362             add_linkage_name (var_die, decl);
19363         }
19364     }
19365   else
19366     add_name_and_src_coords_attributes (var_die, decl);
19367
19368   if ((origin == NULL && !specialization_p)
19369       || (origin != NULL
19370           && !DECL_ABSTRACT (decl_or_origin)
19371           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19372                                        decl_function_context
19373                                                         (decl_or_origin))))
19374     {
19375       tree type = TREE_TYPE (decl_or_origin);
19376
19377       if (decl_by_reference_p (decl_or_origin))
19378         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19379       else
19380         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19381                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19382     }
19383
19384   if (origin == NULL && !specialization_p)
19385     {
19386       if (TREE_PUBLIC (decl))
19387         add_AT_flag (var_die, DW_AT_external, 1);
19388
19389       if (DECL_ARTIFICIAL (decl))
19390         add_AT_flag (var_die, DW_AT_artificial, 1);
19391
19392       add_accessibility_attribute (var_die, decl);
19393     }
19394
19395   if (declaration)
19396     add_AT_flag (var_die, DW_AT_declaration, 1);
19397
19398   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19399     equate_decl_number_to_die (decl, var_die);
19400
19401   if (! declaration
19402       && (! DECL_ABSTRACT (decl_or_origin)
19403           /* Local static vars are shared between all clones/inlines,
19404              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19405              already set.  */
19406           || (TREE_CODE (decl_or_origin) == VAR_DECL
19407               && TREE_STATIC (decl_or_origin)
19408               && DECL_RTL_SET_P (decl_or_origin)))
19409       /* When abstract origin already has DW_AT_location attribute, no need
19410          to add it again.  */
19411       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19412     {
19413       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19414           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19415         defer_location (decl_or_origin, var_die);
19416       else
19417         add_location_or_const_value_attribute (var_die,
19418                                                decl_or_origin,
19419                                                DW_AT_location);
19420       add_pubname (decl_or_origin, var_die);
19421     }
19422   else
19423     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19424 }
19425
19426 /* Generate a DIE to represent a named constant.  */
19427
19428 static void
19429 gen_const_die (tree decl, dw_die_ref context_die)
19430 {
19431   dw_die_ref const_die;
19432   tree type = TREE_TYPE (decl);
19433
19434   const_die = new_die (DW_TAG_constant, context_die, decl);
19435   add_name_and_src_coords_attributes (const_die, decl);
19436   add_type_attribute (const_die, type, 1, 0, context_die);
19437   if (TREE_PUBLIC (decl))
19438     add_AT_flag (const_die, DW_AT_external, 1);
19439   if (DECL_ARTIFICIAL (decl))
19440     add_AT_flag (const_die, DW_AT_artificial, 1);
19441   tree_add_const_value_attribute_for_decl (const_die, decl);
19442 }
19443
19444 /* Generate a DIE to represent a label identifier.  */
19445
19446 static void
19447 gen_label_die (tree decl, dw_die_ref context_die)
19448 {
19449   tree origin = decl_ultimate_origin (decl);
19450   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19451   rtx insn;
19452   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19453
19454   if (origin != NULL)
19455     add_abstract_origin_attribute (lbl_die, origin);
19456   else
19457     add_name_and_src_coords_attributes (lbl_die, decl);
19458
19459   if (DECL_ABSTRACT (decl))
19460     equate_decl_number_to_die (decl, lbl_die);
19461   else
19462     {
19463       insn = DECL_RTL_IF_SET (decl);
19464
19465       /* Deleted labels are programmer specified labels which have been
19466          eliminated because of various optimizations.  We still emit them
19467          here so that it is possible to put breakpoints on them.  */
19468       if (insn
19469           && (LABEL_P (insn)
19470               || ((NOTE_P (insn)
19471                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19472         {
19473           /* When optimization is enabled (via -O) some parts of the compiler
19474              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19475              represent source-level labels which were explicitly declared by
19476              the user.  This really shouldn't be happening though, so catch
19477              it if it ever does happen.  */
19478           gcc_assert (!INSN_DELETED_P (insn));
19479
19480           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19481           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19482         }
19483     }
19484 }
19485
19486 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19487    attributes to the DIE for a block STMT, to describe where the inlined
19488    function was called from.  This is similar to add_src_coords_attributes.  */
19489
19490 static inline void
19491 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19492 {
19493   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19494
19495   if (dwarf_version >= 3 || !dwarf_strict)
19496     {
19497       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19498       add_AT_unsigned (die, DW_AT_call_line, s.line);
19499     }
19500 }
19501
19502
19503 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19504    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19505
19506 static inline void
19507 add_high_low_attributes (tree stmt, dw_die_ref die)
19508 {
19509   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19510
19511   if (BLOCK_FRAGMENT_CHAIN (stmt)
19512       && (dwarf_version >= 3 || !dwarf_strict))
19513     {
19514       tree chain;
19515
19516       if (inlined_function_outer_scope_p (stmt))
19517         {
19518           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19519                                        BLOCK_NUMBER (stmt));
19520           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19521         }
19522
19523       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19524
19525       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19526       do
19527         {
19528           add_ranges (chain);
19529           chain = BLOCK_FRAGMENT_CHAIN (chain);
19530         }
19531       while (chain);
19532       add_ranges (NULL);
19533     }
19534   else
19535     {
19536       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19537                                    BLOCK_NUMBER (stmt));
19538       add_AT_lbl_id (die, DW_AT_low_pc, label);
19539       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19540                                    BLOCK_NUMBER (stmt));
19541       add_AT_lbl_id (die, DW_AT_high_pc, label);
19542     }
19543 }
19544
19545 /* Generate a DIE for a lexical block.  */
19546
19547 static void
19548 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19549 {
19550   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19551
19552   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19553     add_high_low_attributes (stmt, stmt_die);
19554
19555   decls_for_scope (stmt, stmt_die, depth);
19556 }
19557
19558 /* Generate a DIE for an inlined subprogram.  */
19559
19560 static void
19561 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19562 {
19563   tree decl;
19564
19565   /* The instance of function that is effectively being inlined shall not
19566      be abstract.  */
19567   gcc_assert (! BLOCK_ABSTRACT (stmt));
19568
19569   decl = block_ultimate_origin (stmt);
19570
19571   /* Emit info for the abstract instance first, if we haven't yet.  We
19572      must emit this even if the block is abstract, otherwise when we
19573      emit the block below (or elsewhere), we may end up trying to emit
19574      a die whose origin die hasn't been emitted, and crashing.  */
19575   dwarf2out_abstract_function (decl);
19576
19577   if (! BLOCK_ABSTRACT (stmt))
19578     {
19579       dw_die_ref subr_die
19580         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19581
19582       add_abstract_origin_attribute (subr_die, decl);
19583       if (TREE_ASM_WRITTEN (stmt))
19584         add_high_low_attributes (stmt, subr_die);
19585       add_call_src_coords_attributes (stmt, subr_die);
19586
19587       decls_for_scope (stmt, subr_die, depth);
19588       current_function_has_inlines = 1;
19589     }
19590 }
19591
19592 /* Generate a DIE for a field in a record, or structure.  */
19593
19594 static void
19595 gen_field_die (tree decl, dw_die_ref context_die)
19596 {
19597   dw_die_ref decl_die;
19598
19599   if (TREE_TYPE (decl) == error_mark_node)
19600     return;
19601
19602   decl_die = new_die (DW_TAG_member, context_die, decl);
19603   add_name_and_src_coords_attributes (decl_die, decl);
19604   add_type_attribute (decl_die, member_declared_type (decl),
19605                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19606                       context_die);
19607
19608   if (DECL_BIT_FIELD_TYPE (decl))
19609     {
19610       add_byte_size_attribute (decl_die, decl);
19611       add_bit_size_attribute (decl_die, decl);
19612       add_bit_offset_attribute (decl_die, decl);
19613     }
19614
19615   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19616     add_data_member_location_attribute (decl_die, decl);
19617
19618   if (DECL_ARTIFICIAL (decl))
19619     add_AT_flag (decl_die, DW_AT_artificial, 1);
19620
19621   add_accessibility_attribute (decl_die, decl);
19622
19623   /* Equate decl number to die, so that we can look up this decl later on.  */
19624   equate_decl_number_to_die (decl, decl_die);
19625 }
19626
19627 #if 0
19628 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19629    Use modified_type_die instead.
19630    We keep this code here just in case these types of DIEs may be needed to
19631    represent certain things in other languages (e.g. Pascal) someday.  */
19632
19633 static void
19634 gen_pointer_type_die (tree type, dw_die_ref context_die)
19635 {
19636   dw_die_ref ptr_die
19637     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19638
19639   equate_type_number_to_die (type, ptr_die);
19640   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19641   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19642 }
19643
19644 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19645    Use modified_type_die instead.
19646    We keep this code here just in case these types of DIEs may be needed to
19647    represent certain things in other languages (e.g. Pascal) someday.  */
19648
19649 static void
19650 gen_reference_type_die (tree type, dw_die_ref context_die)
19651 {
19652   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19653
19654   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19655     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19656   else
19657     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19658
19659   equate_type_number_to_die (type, ref_die);
19660   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19661   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19662 }
19663 #endif
19664
19665 /* Generate a DIE for a pointer to a member type.  */
19666
19667 static void
19668 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19669 {
19670   dw_die_ref ptr_die
19671     = new_die (DW_TAG_ptr_to_member_type,
19672                scope_die_for (type, context_die), type);
19673
19674   equate_type_number_to_die (type, ptr_die);
19675   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19676                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19677   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19678 }
19679
19680 /* Generate the DIE for the compilation unit.  */
19681
19682 static dw_die_ref
19683 gen_compile_unit_die (const char *filename)
19684 {
19685   dw_die_ref die;
19686   char producer[250];
19687   const char *language_string = lang_hooks.name;
19688   int language;
19689
19690   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19691
19692   if (filename)
19693     {
19694       add_name_attribute (die, filename);
19695       /* Don't add cwd for <built-in>.  */
19696       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19697         add_comp_dir_attribute (die);
19698     }
19699
19700   sprintf (producer, "%s %s", language_string, version_string);
19701
19702 #ifdef MIPS_DEBUGGING_INFO
19703   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19704      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19705      not appear in the producer string, the debugger reaches the conclusion
19706      that the object file is stripped and has no debugging information.
19707      To get the MIPS/SGI debugger to believe that there is debugging
19708      information in the object file, we add a -g to the producer string.  */
19709   if (debug_info_level > DINFO_LEVEL_TERSE)
19710     strcat (producer, " -g");
19711 #endif
19712
19713   add_AT_string (die, DW_AT_producer, producer);
19714
19715   /* If our producer is LTO try to figure out a common language to use
19716      from the global list of translation units.  */
19717   if (strcmp (language_string, "GNU GIMPLE") == 0)
19718     {
19719       unsigned i;
19720       tree t;
19721       const char *common_lang = NULL;
19722
19723       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
19724         {
19725           if (!TRANSLATION_UNIT_LANGUAGE (t))
19726             continue;
19727           if (!common_lang)
19728             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19729           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19730             ;
19731           else if (strncmp (common_lang, "GNU C", 5) == 0
19732                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19733             /* Mixing C and C++ is ok, use C++ in that case.  */
19734             common_lang = "GNU C++";
19735           else
19736             {
19737               /* Fall back to C.  */
19738               common_lang = NULL;
19739               break;
19740             }
19741         }
19742
19743       if (common_lang)
19744         language_string = common_lang;
19745     }
19746
19747   language = DW_LANG_C89;
19748   if (strcmp (language_string, "GNU C++") == 0)
19749     language = DW_LANG_C_plus_plus;
19750   else if (strcmp (language_string, "GNU F77") == 0)
19751     language = DW_LANG_Fortran77;
19752   else if (strcmp (language_string, "GNU Pascal") == 0)
19753     language = DW_LANG_Pascal83;
19754   else if (dwarf_version >= 3 || !dwarf_strict)
19755     {
19756       if (strcmp (language_string, "GNU Ada") == 0)
19757         language = DW_LANG_Ada95;
19758       else if (strcmp (language_string, "GNU Fortran") == 0)
19759         language = DW_LANG_Fortran95;
19760       else if (strcmp (language_string, "GNU Java") == 0)
19761         language = DW_LANG_Java;
19762       else if (strcmp (language_string, "GNU Objective-C") == 0)
19763         language = DW_LANG_ObjC;
19764       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19765         language = DW_LANG_ObjC_plus_plus;
19766     }
19767
19768   add_AT_unsigned (die, DW_AT_language, language);
19769
19770   switch (language)
19771     {
19772     case DW_LANG_Fortran77:
19773     case DW_LANG_Fortran90:
19774     case DW_LANG_Fortran95:
19775       /* Fortran has case insensitive identifiers and the front-end
19776          lowercases everything.  */
19777       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19778       break;
19779     default:
19780       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19781       break;
19782     }
19783   return die;
19784 }
19785
19786 /* Generate the DIE for a base class.  */
19787
19788 static void
19789 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19790 {
19791   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19792
19793   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19794   add_data_member_location_attribute (die, binfo);
19795
19796   if (BINFO_VIRTUAL_P (binfo))
19797     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19798
19799   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19800      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19801      the default has always been DW_ACCESS_private.  */
19802   if (access == access_public_node)
19803     {
19804       if (dwarf_version == 2
19805           || context_die->die_tag == DW_TAG_class_type)
19806       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19807     }
19808   else if (access == access_protected_node)
19809     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19810   else if (dwarf_version > 2
19811            && context_die->die_tag != DW_TAG_class_type)
19812     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19813 }
19814
19815 /* Generate a DIE for a class member.  */
19816
19817 static void
19818 gen_member_die (tree type, dw_die_ref context_die)
19819 {
19820   tree member;
19821   tree binfo = TYPE_BINFO (type);
19822   dw_die_ref child;
19823
19824   /* If this is not an incomplete type, output descriptions of each of its
19825      members. Note that as we output the DIEs necessary to represent the
19826      members of this record or union type, we will also be trying to output
19827      DIEs to represent the *types* of those members. However the `type'
19828      function (above) will specifically avoid generating type DIEs for member
19829      types *within* the list of member DIEs for this (containing) type except
19830      for those types (of members) which are explicitly marked as also being
19831      members of this (containing) type themselves.  The g++ front- end can
19832      force any given type to be treated as a member of some other (containing)
19833      type by setting the TYPE_CONTEXT of the given (member) type to point to
19834      the TREE node representing the appropriate (containing) type.  */
19835
19836   /* First output info about the base classes.  */
19837   if (binfo)
19838     {
19839       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19840       int i;
19841       tree base;
19842
19843       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19844         gen_inheritance_die (base,
19845                              (accesses ? VEC_index (tree, accesses, i)
19846                               : access_public_node), context_die);
19847     }
19848
19849   /* Now output info about the data members and type members.  */
19850   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19851     {
19852       /* If we thought we were generating minimal debug info for TYPE
19853          and then changed our minds, some of the member declarations
19854          may have already been defined.  Don't define them again, but
19855          do put them in the right order.  */
19856
19857       child = lookup_decl_die (member);
19858       if (child)
19859         splice_child_die (context_die, child);
19860       else
19861         gen_decl_die (member, NULL, context_die);
19862     }
19863
19864   /* Now output info about the function members (if any).  */
19865   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19866     {
19867       /* Don't include clones in the member list.  */
19868       if (DECL_ABSTRACT_ORIGIN (member))
19869         continue;
19870
19871       child = lookup_decl_die (member);
19872       if (child)
19873         splice_child_die (context_die, child);
19874       else
19875         gen_decl_die (member, NULL, context_die);
19876     }
19877 }
19878
19879 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19880    is set, we pretend that the type was never defined, so we only get the
19881    member DIEs needed by later specification DIEs.  */
19882
19883 static void
19884 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19885                                 enum debug_info_usage usage)
19886 {
19887   dw_die_ref type_die = lookup_type_die (type);
19888   dw_die_ref scope_die = 0;
19889   int nested = 0;
19890   int complete = (TYPE_SIZE (type)
19891                   && (! TYPE_STUB_DECL (type)
19892                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19893   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19894   complete = complete && should_emit_struct_debug (type, usage);
19895
19896   if (type_die && ! complete)
19897     return;
19898
19899   if (TYPE_CONTEXT (type) != NULL_TREE
19900       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19901           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19902     nested = 1;
19903
19904   scope_die = scope_die_for (type, context_die);
19905
19906   if (! type_die || (nested && is_cu_die (scope_die)))
19907     /* First occurrence of type or toplevel definition of nested class.  */
19908     {
19909       dw_die_ref old_die = type_die;
19910
19911       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19912                           ? record_type_tag (type) : DW_TAG_union_type,
19913                           scope_die, type);
19914       equate_type_number_to_die (type, type_die);
19915       if (old_die)
19916         add_AT_specification (type_die, old_die);
19917       else
19918         add_name_attribute (type_die, type_tag (type));
19919     }
19920   else
19921     remove_AT (type_die, DW_AT_declaration);
19922
19923   /* Generate child dies for template paramaters.  */
19924   if (debug_info_level > DINFO_LEVEL_TERSE
19925       && COMPLETE_TYPE_P (type))
19926     gen_generic_params_dies (type);
19927
19928   /* If this type has been completed, then give it a byte_size attribute and
19929      then give a list of members.  */
19930   if (complete && !ns_decl)
19931     {
19932       /* Prevent infinite recursion in cases where the type of some member of
19933          this type is expressed in terms of this type itself.  */
19934       TREE_ASM_WRITTEN (type) = 1;
19935       add_byte_size_attribute (type_die, type);
19936       if (TYPE_STUB_DECL (type) != NULL_TREE)
19937         {
19938           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19939           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19940         }
19941
19942       /* If the first reference to this type was as the return type of an
19943          inline function, then it may not have a parent.  Fix this now.  */
19944       if (type_die->die_parent == NULL)
19945         add_child_die (scope_die, type_die);
19946
19947       push_decl_scope (type);
19948       gen_member_die (type, type_die);
19949       pop_decl_scope ();
19950
19951       /* GNU extension: Record what type our vtable lives in.  */
19952       if (TYPE_VFIELD (type))
19953         {
19954           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19955
19956           gen_type_die (vtype, context_die);
19957           add_AT_die_ref (type_die, DW_AT_containing_type,
19958                           lookup_type_die (vtype));
19959         }
19960     }
19961   else
19962     {
19963       add_AT_flag (type_die, DW_AT_declaration, 1);
19964
19965       /* We don't need to do this for function-local types.  */
19966       if (TYPE_STUB_DECL (type)
19967           && ! decl_function_context (TYPE_STUB_DECL (type)))
19968         VEC_safe_push (tree, gc, incomplete_types, type);
19969     }
19970
19971   if (get_AT (type_die, DW_AT_name))
19972     add_pubtype (type, type_die);
19973 }
19974
19975 /* Generate a DIE for a subroutine _type_.  */
19976
19977 static void
19978 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19979 {
19980   tree return_type = TREE_TYPE (type);
19981   dw_die_ref subr_die
19982     = new_die (DW_TAG_subroutine_type,
19983                scope_die_for (type, context_die), type);
19984
19985   equate_type_number_to_die (type, subr_die);
19986   add_prototyped_attribute (subr_die, type);
19987   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19988   gen_formal_types_die (type, subr_die);
19989
19990   if (get_AT (subr_die, DW_AT_name))
19991     add_pubtype (type, subr_die);
19992 }
19993
19994 /* Generate a DIE for a type definition.  */
19995
19996 static void
19997 gen_typedef_die (tree decl, dw_die_ref context_die)
19998 {
19999   dw_die_ref type_die;
20000   tree origin;
20001
20002   if (TREE_ASM_WRITTEN (decl))
20003     return;
20004
20005   TREE_ASM_WRITTEN (decl) = 1;
20006   type_die = new_die (DW_TAG_typedef, context_die, decl);
20007   origin = decl_ultimate_origin (decl);
20008   if (origin != NULL)
20009     add_abstract_origin_attribute (type_die, origin);
20010   else
20011     {
20012       tree type;
20013
20014       add_name_and_src_coords_attributes (type_die, decl);
20015       if (DECL_ORIGINAL_TYPE (decl))
20016         {
20017           type = DECL_ORIGINAL_TYPE (decl);
20018
20019           gcc_assert (type != TREE_TYPE (decl));
20020           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20021         }
20022       else
20023         {
20024           type = TREE_TYPE (decl);
20025
20026           if (is_naming_typedef_decl (TYPE_NAME (type)))
20027             {
20028               /* Here, we are in the case of decl being a typedef naming
20029                  an anonymous type, e.g:
20030                      typedef struct {...} foo;
20031                  In that case TREE_TYPE (decl) is not a typedef variant
20032                  type and TYPE_NAME of the anonymous type is set to the
20033                  TYPE_DECL of the typedef. This construct is emitted by
20034                  the C++ FE.
20035
20036                  TYPE is the anonymous struct named by the typedef
20037                  DECL. As we need the DW_AT_type attribute of the
20038                  DW_TAG_typedef to point to the DIE of TYPE, let's
20039                  generate that DIE right away. add_type_attribute
20040                  called below will then pick (via lookup_type_die) that
20041                  anonymous struct DIE.  */
20042               if (!TREE_ASM_WRITTEN (type))
20043                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20044             }
20045         }
20046
20047       add_type_attribute (type_die, type, TREE_READONLY (decl),
20048                           TREE_THIS_VOLATILE (decl), context_die);
20049
20050       if (is_naming_typedef_decl (decl))
20051         /* We want that all subsequent calls to lookup_type_die with
20052            TYPE in argument yield the DW_TAG_typedef we have just
20053            created.  */
20054         equate_type_number_to_die (type, type_die);
20055
20056       add_accessibility_attribute (type_die, decl);
20057     }
20058
20059   if (DECL_ABSTRACT (decl))
20060     equate_decl_number_to_die (decl, type_die);
20061
20062   if (get_AT (type_die, DW_AT_name))
20063     add_pubtype (decl, type_die);
20064 }
20065
20066 /* Generate a DIE for a struct, class, enum or union type.  */
20067
20068 static void
20069 gen_tagged_type_die (tree type,
20070                      dw_die_ref context_die,
20071                      enum debug_info_usage usage)
20072 {
20073   int need_pop;
20074
20075   if (type == NULL_TREE
20076       || !is_tagged_type (type))
20077     return;
20078
20079   /* If this is a nested type whose containing class hasn't been written
20080      out yet, writing it out will cover this one, too.  This does not apply
20081      to instantiations of member class templates; they need to be added to
20082      the containing class as they are generated.  FIXME: This hurts the
20083      idea of combining type decls from multiple TUs, since we can't predict
20084      what set of template instantiations we'll get.  */
20085   if (TYPE_CONTEXT (type)
20086       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20087       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20088     {
20089       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20090
20091       if (TREE_ASM_WRITTEN (type))
20092         return;
20093
20094       /* If that failed, attach ourselves to the stub.  */
20095       push_decl_scope (TYPE_CONTEXT (type));
20096       context_die = lookup_type_die (TYPE_CONTEXT (type));
20097       need_pop = 1;
20098     }
20099   else if (TYPE_CONTEXT (type) != NULL_TREE
20100            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20101     {
20102       /* If this type is local to a function that hasn't been written
20103          out yet, use a NULL context for now; it will be fixed up in
20104          decls_for_scope.  */
20105       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20106       need_pop = 0;
20107     }
20108   else
20109     {
20110       context_die = declare_in_namespace (type, context_die);
20111       need_pop = 0;
20112     }
20113
20114   if (TREE_CODE (type) == ENUMERAL_TYPE)
20115     {
20116       /* This might have been written out by the call to
20117          declare_in_namespace.  */
20118       if (!TREE_ASM_WRITTEN (type))
20119         gen_enumeration_type_die (type, context_die);
20120     }
20121   else
20122     gen_struct_or_union_type_die (type, context_die, usage);
20123
20124   if (need_pop)
20125     pop_decl_scope ();
20126
20127   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20128      it up if it is ever completed.  gen_*_type_die will set it for us
20129      when appropriate.  */
20130 }
20131
20132 /* Generate a type description DIE.  */
20133
20134 static void
20135 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20136                                 enum debug_info_usage usage)
20137 {
20138   struct array_descr_info info;
20139
20140   if (type == NULL_TREE || type == error_mark_node)
20141     return;
20142
20143   /* If TYPE is a typedef type variant, let's generate debug info
20144      for the parent typedef which TYPE is a type of.  */
20145   if (typedef_variant_p (type))
20146     {
20147       if (TREE_ASM_WRITTEN (type))
20148         return;
20149
20150       /* Prevent broken recursion; we can't hand off to the same type.  */
20151       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20152
20153       /* Use the DIE of the containing namespace as the parent DIE of
20154          the type description DIE we want to generate.  */
20155       if (DECL_CONTEXT (TYPE_NAME (type))
20156           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20157         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20158
20159       TREE_ASM_WRITTEN (type) = 1;
20160
20161       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20162       return;
20163     }
20164
20165   /* If type is an anonymous tagged type named by a typedef, let's
20166      generate debug info for the typedef.  */
20167   if (is_naming_typedef_decl (TYPE_NAME (type)))
20168     {
20169       /* Use the DIE of the containing namespace as the parent DIE of
20170          the type description DIE we want to generate.  */
20171       if (DECL_CONTEXT (TYPE_NAME (type))
20172           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20173         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20174       
20175       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20176       return;
20177     }
20178
20179   /* If this is an array type with hidden descriptor, handle it first.  */
20180   if (!TREE_ASM_WRITTEN (type)
20181       && lang_hooks.types.get_array_descr_info
20182       && lang_hooks.types.get_array_descr_info (type, &info)
20183       && (dwarf_version >= 3 || !dwarf_strict))
20184     {
20185       gen_descr_array_type_die (type, &info, context_die);
20186       TREE_ASM_WRITTEN (type) = 1;
20187       return;
20188     }
20189
20190   /* We are going to output a DIE to represent the unqualified version
20191      of this type (i.e. without any const or volatile qualifiers) so
20192      get the main variant (i.e. the unqualified version) of this type
20193      now.  (Vectors are special because the debugging info is in the
20194      cloned type itself).  */
20195   if (TREE_CODE (type) != VECTOR_TYPE)
20196     type = type_main_variant (type);
20197
20198   if (TREE_ASM_WRITTEN (type))
20199     return;
20200
20201   switch (TREE_CODE (type))
20202     {
20203     case ERROR_MARK:
20204       break;
20205
20206     case POINTER_TYPE:
20207     case REFERENCE_TYPE:
20208       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20209          ensures that the gen_type_die recursion will terminate even if the
20210          type is recursive.  Recursive types are possible in Ada.  */
20211       /* ??? We could perhaps do this for all types before the switch
20212          statement.  */
20213       TREE_ASM_WRITTEN (type) = 1;
20214
20215       /* For these types, all that is required is that we output a DIE (or a
20216          set of DIEs) to represent the "basis" type.  */
20217       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20218                                 DINFO_USAGE_IND_USE);
20219       break;
20220
20221     case OFFSET_TYPE:
20222       /* This code is used for C++ pointer-to-data-member types.
20223          Output a description of the relevant class type.  */
20224       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20225                                         DINFO_USAGE_IND_USE);
20226
20227       /* Output a description of the type of the object pointed to.  */
20228       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20229                                         DINFO_USAGE_IND_USE);
20230
20231       /* Now output a DIE to represent this pointer-to-data-member type
20232          itself.  */
20233       gen_ptr_to_mbr_type_die (type, context_die);
20234       break;
20235
20236     case FUNCTION_TYPE:
20237       /* Force out return type (in case it wasn't forced out already).  */
20238       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20239                                         DINFO_USAGE_DIR_USE);
20240       gen_subroutine_type_die (type, context_die);
20241       break;
20242
20243     case METHOD_TYPE:
20244       /* Force out return type (in case it wasn't forced out already).  */
20245       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20246                                         DINFO_USAGE_DIR_USE);
20247       gen_subroutine_type_die (type, context_die);
20248       break;
20249
20250     case ARRAY_TYPE:
20251       gen_array_type_die (type, context_die);
20252       break;
20253
20254     case VECTOR_TYPE:
20255       gen_array_type_die (type, context_die);
20256       break;
20257
20258     case ENUMERAL_TYPE:
20259     case RECORD_TYPE:
20260     case UNION_TYPE:
20261     case QUAL_UNION_TYPE:
20262       gen_tagged_type_die (type, context_die, usage);
20263       return;
20264
20265     case VOID_TYPE:
20266     case INTEGER_TYPE:
20267     case REAL_TYPE:
20268     case FIXED_POINT_TYPE:
20269     case COMPLEX_TYPE:
20270     case BOOLEAN_TYPE:
20271       /* No DIEs needed for fundamental types.  */
20272       break;
20273
20274     case NULLPTR_TYPE:
20275     case LANG_TYPE:
20276       /* Just use DW_TAG_unspecified_type.  */
20277       {
20278         dw_die_ref type_die = lookup_type_die (type);
20279         if (type_die == NULL)
20280           {
20281             tree name = TYPE_NAME (type);
20282             if (TREE_CODE (name) == TYPE_DECL)
20283               name = DECL_NAME (name);
20284             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20285             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20286             equate_type_number_to_die (type, type_die);
20287           }
20288       }
20289       break;
20290
20291     default:
20292       gcc_unreachable ();
20293     }
20294
20295   TREE_ASM_WRITTEN (type) = 1;
20296 }
20297
20298 static void
20299 gen_type_die (tree type, dw_die_ref context_die)
20300 {
20301   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20302 }
20303
20304 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20305    things which are local to the given block.  */
20306
20307 static void
20308 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20309 {
20310   int must_output_die = 0;
20311   bool inlined_func;
20312
20313   /* Ignore blocks that are NULL.  */
20314   if (stmt == NULL_TREE)
20315     return;
20316
20317   inlined_func = inlined_function_outer_scope_p (stmt);
20318
20319   /* If the block is one fragment of a non-contiguous block, do not
20320      process the variables, since they will have been done by the
20321      origin block.  Do process subblocks.  */
20322   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20323     {
20324       tree sub;
20325
20326       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20327         gen_block_die (sub, context_die, depth + 1);
20328
20329       return;
20330     }
20331
20332   /* Determine if we need to output any Dwarf DIEs at all to represent this
20333      block.  */
20334   if (inlined_func)
20335     /* The outer scopes for inlinings *must* always be represented.  We
20336        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20337     must_output_die = 1;
20338   else
20339     {
20340       /* Determine if this block directly contains any "significant"
20341          local declarations which we will need to output DIEs for.  */
20342       if (debug_info_level > DINFO_LEVEL_TERSE)
20343         /* We are not in terse mode so *any* local declaration counts
20344            as being a "significant" one.  */
20345         must_output_die = ((BLOCK_VARS (stmt) != NULL
20346                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20347                            && (TREE_USED (stmt)
20348                                || TREE_ASM_WRITTEN (stmt)
20349                                || BLOCK_ABSTRACT (stmt)));
20350       else if ((TREE_USED (stmt)
20351                 || TREE_ASM_WRITTEN (stmt)
20352                 || BLOCK_ABSTRACT (stmt))
20353                && !dwarf2out_ignore_block (stmt))
20354         must_output_die = 1;
20355     }
20356
20357   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20358      DIE for any block which contains no significant local declarations at
20359      all.  Rather, in such cases we just call `decls_for_scope' so that any
20360      needed Dwarf info for any sub-blocks will get properly generated. Note
20361      that in terse mode, our definition of what constitutes a "significant"
20362      local declaration gets restricted to include only inlined function
20363      instances and local (nested) function definitions.  */
20364   if (must_output_die)
20365     {
20366       if (inlined_func)
20367         {
20368           /* If STMT block is abstract, that means we have been called
20369              indirectly from dwarf2out_abstract_function.
20370              That function rightfully marks the descendent blocks (of
20371              the abstract function it is dealing with) as being abstract,
20372              precisely to prevent us from emitting any
20373              DW_TAG_inlined_subroutine DIE as a descendent
20374              of an abstract function instance. So in that case, we should
20375              not call gen_inlined_subroutine_die.
20376
20377              Later though, when cgraph asks dwarf2out to emit info
20378              for the concrete instance of the function decl into which
20379              the concrete instance of STMT got inlined, the later will lead
20380              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20381           if (! BLOCK_ABSTRACT (stmt))
20382             gen_inlined_subroutine_die (stmt, context_die, depth);
20383         }
20384       else
20385         gen_lexical_block_die (stmt, context_die, depth);
20386     }
20387   else
20388     decls_for_scope (stmt, context_die, depth);
20389 }
20390
20391 /* Process variable DECL (or variable with origin ORIGIN) within
20392    block STMT and add it to CONTEXT_DIE.  */
20393 static void
20394 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20395 {
20396   dw_die_ref die;
20397   tree decl_or_origin = decl ? decl : origin;
20398
20399   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20400     die = lookup_decl_die (decl_or_origin);
20401   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20402            && TYPE_DECL_IS_STUB (decl_or_origin))
20403     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20404   else
20405     die = NULL;
20406
20407   if (die != NULL && die->die_parent == NULL)
20408     add_child_die (context_die, die);
20409   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20410     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20411                                          stmt, context_die);
20412   else
20413     gen_decl_die (decl, origin, context_die);
20414 }
20415
20416 /* Generate all of the decls declared within a given scope and (recursively)
20417    all of its sub-blocks.  */
20418
20419 static void
20420 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20421 {
20422   tree decl;
20423   unsigned int i;
20424   tree subblocks;
20425
20426   /* Ignore NULL blocks.  */
20427   if (stmt == NULL_TREE)
20428     return;
20429
20430   /* Output the DIEs to represent all of the data objects and typedefs
20431      declared directly within this block but not within any nested
20432      sub-blocks.  Also, nested function and tag DIEs have been
20433      generated with a parent of NULL; fix that up now.  */
20434   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20435     process_scope_var (stmt, decl, NULL_TREE, context_die);
20436   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20437     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20438                        context_die);
20439
20440   /* If we're at -g1, we're not interested in subblocks.  */
20441   if (debug_info_level <= DINFO_LEVEL_TERSE)
20442     return;
20443
20444   /* Output the DIEs to represent all sub-blocks (and the items declared
20445      therein) of this block.  */
20446   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20447        subblocks != NULL;
20448        subblocks = BLOCK_CHAIN (subblocks))
20449     gen_block_die (subblocks, context_die, depth + 1);
20450 }
20451
20452 /* Is this a typedef we can avoid emitting?  */
20453
20454 static inline int
20455 is_redundant_typedef (const_tree decl)
20456 {
20457   if (TYPE_DECL_IS_STUB (decl))
20458     return 1;
20459
20460   if (DECL_ARTIFICIAL (decl)
20461       && DECL_CONTEXT (decl)
20462       && is_tagged_type (DECL_CONTEXT (decl))
20463       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20464       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20465     /* Also ignore the artificial member typedef for the class name.  */
20466     return 1;
20467
20468   return 0;
20469 }
20470
20471 /* Return TRUE if TYPE is a typedef that names a type for linkage
20472    purposes. This kind of typedefs is produced by the C++ FE for
20473    constructs like:
20474
20475    typedef struct {...} foo;
20476
20477    In that case, there is no typedef variant type produced for foo.
20478    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20479    struct type.  */
20480
20481 static bool
20482 is_naming_typedef_decl (const_tree decl)
20483 {
20484   if (decl == NULL_TREE
20485       || TREE_CODE (decl) != TYPE_DECL
20486       || !is_tagged_type (TREE_TYPE (decl))
20487       || DECL_IS_BUILTIN (decl)
20488       || is_redundant_typedef (decl)
20489       /* It looks like Ada produces TYPE_DECLs that are very similar
20490          to C++ naming typedefs but that have different
20491          semantics. Let's be specific to c++ for now.  */
20492       || !is_cxx ())
20493     return FALSE;
20494
20495   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20496           && TYPE_NAME (TREE_TYPE (decl)) == decl
20497           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20498               != TYPE_NAME (TREE_TYPE (decl))));
20499 }
20500
20501 /* Returns the DIE for a context.  */
20502
20503 static inline dw_die_ref
20504 get_context_die (tree context)
20505 {
20506   if (context)
20507     {
20508       /* Find die that represents this context.  */
20509       if (TYPE_P (context))
20510         return force_type_die (TYPE_MAIN_VARIANT (context));
20511       else
20512         return force_decl_die (context);
20513     }
20514   return comp_unit_die ();
20515 }
20516
20517 /* Returns the DIE for decl.  A DIE will always be returned.  */
20518
20519 static dw_die_ref
20520 force_decl_die (tree decl)
20521 {
20522   dw_die_ref decl_die;
20523   unsigned saved_external_flag;
20524   tree save_fn = NULL_TREE;
20525   decl_die = lookup_decl_die (decl);
20526   if (!decl_die)
20527     {
20528       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20529
20530       decl_die = lookup_decl_die (decl);
20531       if (decl_die)
20532         return decl_die;
20533
20534       switch (TREE_CODE (decl))
20535         {
20536         case FUNCTION_DECL:
20537           /* Clear current_function_decl, so that gen_subprogram_die thinks
20538              that this is a declaration. At this point, we just want to force
20539              declaration die.  */
20540           save_fn = current_function_decl;
20541           current_function_decl = NULL_TREE;
20542           gen_subprogram_die (decl, context_die);
20543           current_function_decl = save_fn;
20544           break;
20545
20546         case VAR_DECL:
20547           /* Set external flag to force declaration die. Restore it after
20548            gen_decl_die() call.  */
20549           saved_external_flag = DECL_EXTERNAL (decl);
20550           DECL_EXTERNAL (decl) = 1;
20551           gen_decl_die (decl, NULL, context_die);
20552           DECL_EXTERNAL (decl) = saved_external_flag;
20553           break;
20554
20555         case NAMESPACE_DECL:
20556           if (dwarf_version >= 3 || !dwarf_strict)
20557             dwarf2out_decl (decl);
20558           else
20559             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20560             decl_die = comp_unit_die ();
20561           break;
20562
20563         case TRANSLATION_UNIT_DECL:
20564           decl_die = comp_unit_die ();
20565           break;
20566
20567         default:
20568           gcc_unreachable ();
20569         }
20570
20571       /* We should be able to find the DIE now.  */
20572       if (!decl_die)
20573         decl_die = lookup_decl_die (decl);
20574       gcc_assert (decl_die);
20575     }
20576
20577   return decl_die;
20578 }
20579
20580 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20581    always returned.  */
20582
20583 static dw_die_ref
20584 force_type_die (tree type)
20585 {
20586   dw_die_ref type_die;
20587
20588   type_die = lookup_type_die (type);
20589   if (!type_die)
20590     {
20591       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20592
20593       type_die = modified_type_die (type, TYPE_READONLY (type),
20594                                     TYPE_VOLATILE (type), context_die);
20595       gcc_assert (type_die);
20596     }
20597   return type_die;
20598 }
20599
20600 /* Force out any required namespaces to be able to output DECL,
20601    and return the new context_die for it, if it's changed.  */
20602
20603 static dw_die_ref
20604 setup_namespace_context (tree thing, dw_die_ref context_die)
20605 {
20606   tree context = (DECL_P (thing)
20607                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20608   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20609     /* Force out the namespace.  */
20610     context_die = force_decl_die (context);
20611
20612   return context_die;
20613 }
20614
20615 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20616    type) within its namespace, if appropriate.
20617
20618    For compatibility with older debuggers, namespace DIEs only contain
20619    declarations; all definitions are emitted at CU scope.  */
20620
20621 static dw_die_ref
20622 declare_in_namespace (tree thing, dw_die_ref context_die)
20623 {
20624   dw_die_ref ns_context;
20625
20626   if (debug_info_level <= DINFO_LEVEL_TERSE)
20627     return context_die;
20628
20629   /* If this decl is from an inlined function, then don't try to emit it in its
20630      namespace, as we will get confused.  It would have already been emitted
20631      when the abstract instance of the inline function was emitted anyways.  */
20632   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20633     return context_die;
20634
20635   ns_context = setup_namespace_context (thing, context_die);
20636
20637   if (ns_context != context_die)
20638     {
20639       if (is_fortran ())
20640         return ns_context;
20641       if (DECL_P (thing))
20642         gen_decl_die (thing, NULL, ns_context);
20643       else
20644         gen_type_die (thing, ns_context);
20645     }
20646   return context_die;
20647 }
20648
20649 /* Generate a DIE for a namespace or namespace alias.  */
20650
20651 static void
20652 gen_namespace_die (tree decl, dw_die_ref context_die)
20653 {
20654   dw_die_ref namespace_die;
20655
20656   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20657      they are an alias of.  */
20658   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20659     {
20660       /* Output a real namespace or module.  */
20661       context_die = setup_namespace_context (decl, comp_unit_die ());
20662       namespace_die = new_die (is_fortran ()
20663                                ? DW_TAG_module : DW_TAG_namespace,
20664                                context_die, decl);
20665       /* For Fortran modules defined in different CU don't add src coords.  */
20666       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20667         {
20668           const char *name = dwarf2_name (decl, 0);
20669           if (name)
20670             add_name_attribute (namespace_die, name);
20671         }
20672       else
20673         add_name_and_src_coords_attributes (namespace_die, decl);
20674       if (DECL_EXTERNAL (decl))
20675         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20676       equate_decl_number_to_die (decl, namespace_die);
20677     }
20678   else
20679     {
20680       /* Output a namespace alias.  */
20681
20682       /* Force out the namespace we are an alias of, if necessary.  */
20683       dw_die_ref origin_die
20684         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20685
20686       if (DECL_FILE_SCOPE_P (decl)
20687           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20688         context_die = setup_namespace_context (decl, comp_unit_die ());
20689       /* Now create the namespace alias DIE.  */
20690       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20691       add_name_and_src_coords_attributes (namespace_die, decl);
20692       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20693       equate_decl_number_to_die (decl, namespace_die);
20694     }
20695 }
20696
20697 /* Generate Dwarf debug information for a decl described by DECL.
20698    The return value is currently only meaningful for PARM_DECLs,
20699    for all other decls it returns NULL.  */
20700
20701 static dw_die_ref
20702 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20703 {
20704   tree decl_or_origin = decl ? decl : origin;
20705   tree class_origin = NULL, ultimate_origin;
20706
20707   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20708     return NULL;
20709
20710   switch (TREE_CODE (decl_or_origin))
20711     {
20712     case ERROR_MARK:
20713       break;
20714
20715     case CONST_DECL:
20716       if (!is_fortran () && !is_ada ())
20717         {
20718           /* The individual enumerators of an enum type get output when we output
20719              the Dwarf representation of the relevant enum type itself.  */
20720           break;
20721         }
20722
20723       /* Emit its type.  */
20724       gen_type_die (TREE_TYPE (decl), context_die);
20725
20726       /* And its containing namespace.  */
20727       context_die = declare_in_namespace (decl, context_die);
20728
20729       gen_const_die (decl, context_die);
20730       break;
20731
20732     case FUNCTION_DECL:
20733       /* Don't output any DIEs to represent mere function declarations,
20734          unless they are class members or explicit block externs.  */
20735       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20736           && DECL_FILE_SCOPE_P (decl_or_origin)
20737           && (current_function_decl == NULL_TREE
20738               || DECL_ARTIFICIAL (decl_or_origin)))
20739         break;
20740
20741 #if 0
20742       /* FIXME */
20743       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20744          on local redeclarations of global functions.  That seems broken.  */
20745       if (current_function_decl != decl)
20746         /* This is only a declaration.  */;
20747 #endif
20748
20749       /* If we're emitting a clone, emit info for the abstract instance.  */
20750       if (origin || DECL_ORIGIN (decl) != decl)
20751         dwarf2out_abstract_function (origin
20752                                      ? DECL_ORIGIN (origin)
20753                                      : DECL_ABSTRACT_ORIGIN (decl));
20754
20755       /* If we're emitting an out-of-line copy of an inline function,
20756          emit info for the abstract instance and set up to refer to it.  */
20757       else if (cgraph_function_possibly_inlined_p (decl)
20758                && ! DECL_ABSTRACT (decl)
20759                && ! class_or_namespace_scope_p (context_die)
20760                /* dwarf2out_abstract_function won't emit a die if this is just
20761                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20762                   that case, because that works only if we have a die.  */
20763                && DECL_INITIAL (decl) != NULL_TREE)
20764         {
20765           dwarf2out_abstract_function (decl);
20766           set_decl_origin_self (decl);
20767         }
20768
20769       /* Otherwise we're emitting the primary DIE for this decl.  */
20770       else if (debug_info_level > DINFO_LEVEL_TERSE)
20771         {
20772           /* Before we describe the FUNCTION_DECL itself, make sure that we
20773              have its containing type.  */
20774           if (!origin)
20775             origin = decl_class_context (decl);
20776           if (origin != NULL_TREE)
20777             gen_type_die (origin, context_die);
20778
20779           /* And its return type.  */
20780           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20781
20782           /* And its virtual context.  */
20783           if (DECL_VINDEX (decl) != NULL_TREE)
20784             gen_type_die (DECL_CONTEXT (decl), context_die);
20785
20786           /* Make sure we have a member DIE for decl.  */
20787           if (origin != NULL_TREE)
20788             gen_type_die_for_member (origin, decl, context_die);
20789
20790           /* And its containing namespace.  */
20791           context_die = declare_in_namespace (decl, context_die);
20792         }
20793
20794       /* Now output a DIE to represent the function itself.  */
20795       if (decl)
20796         gen_subprogram_die (decl, context_die);
20797       break;
20798
20799     case TYPE_DECL:
20800       /* If we are in terse mode, don't generate any DIEs to represent any
20801          actual typedefs.  */
20802       if (debug_info_level <= DINFO_LEVEL_TERSE)
20803         break;
20804
20805       /* In the special case of a TYPE_DECL node representing the declaration
20806          of some type tag, if the given TYPE_DECL is marked as having been
20807          instantiated from some other (original) TYPE_DECL node (e.g. one which
20808          was generated within the original definition of an inline function) we
20809          used to generate a special (abbreviated) DW_TAG_structure_type,
20810          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20811          should be actually referencing those DIEs, as variable DIEs with that
20812          type would be emitted already in the abstract origin, so it was always
20813          removed during unused type prunning.  Don't add anything in this
20814          case.  */
20815       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20816         break;
20817
20818       if (is_redundant_typedef (decl))
20819         gen_type_die (TREE_TYPE (decl), context_die);
20820       else
20821         /* Output a DIE to represent the typedef itself.  */
20822         gen_typedef_die (decl, context_die);
20823       break;
20824
20825     case LABEL_DECL:
20826       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20827         gen_label_die (decl, context_die);
20828       break;
20829
20830     case VAR_DECL:
20831     case RESULT_DECL:
20832       /* If we are in terse mode, don't generate any DIEs to represent any
20833          variable declarations or definitions.  */
20834       if (debug_info_level <= DINFO_LEVEL_TERSE)
20835         break;
20836
20837       /* Output any DIEs that are needed to specify the type of this data
20838          object.  */
20839       if (decl_by_reference_p (decl_or_origin))
20840         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20841       else
20842         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20843
20844       /* And its containing type.  */
20845       class_origin = decl_class_context (decl_or_origin);
20846       if (class_origin != NULL_TREE)
20847         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20848
20849       /* And its containing namespace.  */
20850       context_die = declare_in_namespace (decl_or_origin, context_die);
20851
20852       /* Now output the DIE to represent the data object itself.  This gets
20853          complicated because of the possibility that the VAR_DECL really
20854          represents an inlined instance of a formal parameter for an inline
20855          function.  */
20856       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20857       if (ultimate_origin != NULL_TREE
20858           && TREE_CODE (ultimate_origin) == PARM_DECL)
20859         gen_formal_parameter_die (decl, origin,
20860                                   true /* Emit name attribute.  */,
20861                                   context_die);
20862       else
20863         gen_variable_die (decl, origin, context_die);
20864       break;
20865
20866     case FIELD_DECL:
20867       /* Ignore the nameless fields that are used to skip bits but handle C++
20868          anonymous unions and structs.  */
20869       if (DECL_NAME (decl) != NULL_TREE
20870           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20871           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20872         {
20873           gen_type_die (member_declared_type (decl), context_die);
20874           gen_field_die (decl, context_die);
20875         }
20876       break;
20877
20878     case PARM_DECL:
20879       if (DECL_BY_REFERENCE (decl_or_origin))
20880         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20881       else
20882         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20883       return gen_formal_parameter_die (decl, origin,
20884                                        true /* Emit name attribute.  */,
20885                                        context_die);
20886
20887     case NAMESPACE_DECL:
20888     case IMPORTED_DECL:
20889       if (dwarf_version >= 3 || !dwarf_strict)
20890         gen_namespace_die (decl, context_die);
20891       break;
20892
20893     default:
20894       /* Probably some frontend-internal decl.  Assume we don't care.  */
20895       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20896       break;
20897     }
20898
20899   return NULL;
20900 }
20901 \f
20902 /* Output debug information for global decl DECL.  Called from toplev.c after
20903    compilation proper has finished.  */
20904
20905 static void
20906 dwarf2out_global_decl (tree decl)
20907 {
20908   /* Output DWARF2 information for file-scope tentative data object
20909      declarations, file-scope (extern) function declarations (which
20910      had no corresponding body) and file-scope tagged type declarations
20911      and definitions which have not yet been forced out.  */
20912   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20913     dwarf2out_decl (decl);
20914 }
20915
20916 /* Output debug information for type decl DECL.  Called from toplev.c
20917    and from language front ends (to record built-in types).  */
20918 static void
20919 dwarf2out_type_decl (tree decl, int local)
20920 {
20921   if (!local)
20922     dwarf2out_decl (decl);
20923 }
20924
20925 /* Output debug information for imported module or decl DECL.
20926    NAME is non-NULL name in the lexical block if the decl has been renamed.
20927    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20928    that DECL belongs to.
20929    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20930 static void
20931 dwarf2out_imported_module_or_decl_1 (tree decl,
20932                                      tree name,
20933                                      tree lexical_block,
20934                                      dw_die_ref lexical_block_die)
20935 {
20936   expanded_location xloc;
20937   dw_die_ref imported_die = NULL;
20938   dw_die_ref at_import_die;
20939
20940   if (TREE_CODE (decl) == IMPORTED_DECL)
20941     {
20942       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20943       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20944       gcc_assert (decl);
20945     }
20946   else
20947     xloc = expand_location (input_location);
20948
20949   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20950     {
20951       at_import_die = force_type_die (TREE_TYPE (decl));
20952       /* For namespace N { typedef void T; } using N::T; base_type_die
20953          returns NULL, but DW_TAG_imported_declaration requires
20954          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20955       if (!at_import_die)
20956         {
20957           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20958           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20959           at_import_die = lookup_type_die (TREE_TYPE (decl));
20960           gcc_assert (at_import_die);
20961         }
20962     }
20963   else
20964     {
20965       at_import_die = lookup_decl_die (decl);
20966       if (!at_import_die)
20967         {
20968           /* If we're trying to avoid duplicate debug info, we may not have
20969              emitted the member decl for this field.  Emit it now.  */
20970           if (TREE_CODE (decl) == FIELD_DECL)
20971             {
20972               tree type = DECL_CONTEXT (decl);
20973
20974               if (TYPE_CONTEXT (type)
20975                   && TYPE_P (TYPE_CONTEXT (type))
20976                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20977                                                 DINFO_USAGE_DIR_USE))
20978                 return;
20979               gen_type_die_for_member (type, decl,
20980                                        get_context_die (TYPE_CONTEXT (type)));
20981             }
20982           at_import_die = force_decl_die (decl);
20983         }
20984     }
20985
20986   if (TREE_CODE (decl) == NAMESPACE_DECL)
20987     {
20988       if (dwarf_version >= 3 || !dwarf_strict)
20989         imported_die = new_die (DW_TAG_imported_module,
20990                                 lexical_block_die,
20991                                 lexical_block);
20992       else
20993         return;
20994     }
20995   else
20996     imported_die = new_die (DW_TAG_imported_declaration,
20997                             lexical_block_die,
20998                             lexical_block);
20999
21000   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21001   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21002   if (name)
21003     add_AT_string (imported_die, DW_AT_name,
21004                    IDENTIFIER_POINTER (name));
21005   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21006 }
21007
21008 /* Output debug information for imported module or decl DECL.
21009    NAME is non-NULL name in context if the decl has been renamed.
21010    CHILD is true if decl is one of the renamed decls as part of
21011    importing whole module.  */
21012
21013 static void
21014 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21015                                    bool child)
21016 {
21017   /* dw_die_ref at_import_die;  */
21018   dw_die_ref scope_die;
21019
21020   if (debug_info_level <= DINFO_LEVEL_TERSE)
21021     return;
21022
21023   gcc_assert (decl);
21024
21025   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21026      We need decl DIE for reference and scope die. First, get DIE for the decl
21027      itself.  */
21028
21029   /* Get the scope die for decl context. Use comp_unit_die for global module
21030      or decl. If die is not found for non globals, force new die.  */
21031   if (context
21032       && TYPE_P (context)
21033       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21034     return;
21035
21036   if (!(dwarf_version >= 3 || !dwarf_strict))
21037     return;
21038
21039   scope_die = get_context_die (context);
21040
21041   if (child)
21042     {
21043       gcc_assert (scope_die->die_child);
21044       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21045       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21046       scope_die = scope_die->die_child;
21047     }
21048
21049   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21050   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21051
21052 }
21053
21054 /* Write the debugging output for DECL.  */
21055
21056 void
21057 dwarf2out_decl (tree decl)
21058 {
21059   dw_die_ref context_die = comp_unit_die ();
21060
21061   switch (TREE_CODE (decl))
21062     {
21063     case ERROR_MARK:
21064       return;
21065
21066     case FUNCTION_DECL:
21067       /* What we would really like to do here is to filter out all mere
21068          file-scope declarations of file-scope functions which are never
21069          referenced later within this translation unit (and keep all of ones
21070          that *are* referenced later on) but we aren't clairvoyant, so we have
21071          no idea which functions will be referenced in the future (i.e. later
21072          on within the current translation unit). So here we just ignore all
21073          file-scope function declarations which are not also definitions.  If
21074          and when the debugger needs to know something about these functions,
21075          it will have to hunt around and find the DWARF information associated
21076          with the definition of the function.
21077
21078          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21079          nodes represent definitions and which ones represent mere
21080          declarations.  We have to check DECL_INITIAL instead. That's because
21081          the C front-end supports some weird semantics for "extern inline"
21082          function definitions.  These can get inlined within the current
21083          translation unit (and thus, we need to generate Dwarf info for their
21084          abstract instances so that the Dwarf info for the concrete inlined
21085          instances can have something to refer to) but the compiler never
21086          generates any out-of-lines instances of such things (despite the fact
21087          that they *are* definitions).
21088
21089          The important point is that the C front-end marks these "extern
21090          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21091          them anyway. Note that the C++ front-end also plays some similar games
21092          for inline function definitions appearing within include files which
21093          also contain `#pragma interface' pragmas.  */
21094       if (DECL_INITIAL (decl) == NULL_TREE)
21095         return;
21096
21097       /* If we're a nested function, initially use a parent of NULL; if we're
21098          a plain function, this will be fixed up in decls_for_scope.  If
21099          we're a method, it will be ignored, since we already have a DIE.  */
21100       if (decl_function_context (decl)
21101           /* But if we're in terse mode, we don't care about scope.  */
21102           && debug_info_level > DINFO_LEVEL_TERSE)
21103         context_die = NULL;
21104       break;
21105
21106     case VAR_DECL:
21107       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21108          declaration and if the declaration was never even referenced from
21109          within this entire compilation unit.  We suppress these DIEs in
21110          order to save space in the .debug section (by eliminating entries
21111          which are probably useless).  Note that we must not suppress
21112          block-local extern declarations (whether used or not) because that
21113          would screw-up the debugger's name lookup mechanism and cause it to
21114          miss things which really ought to be in scope at a given point.  */
21115       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21116         return;
21117
21118       /* For local statics lookup proper context die.  */
21119       if (TREE_STATIC (decl) && decl_function_context (decl))
21120         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21121
21122       /* If we are in terse mode, don't generate any DIEs to represent any
21123          variable declarations or definitions.  */
21124       if (debug_info_level <= DINFO_LEVEL_TERSE)
21125         return;
21126       break;
21127
21128     case CONST_DECL:
21129       if (debug_info_level <= DINFO_LEVEL_TERSE)
21130         return;
21131       if (!is_fortran () && !is_ada ())
21132         return;
21133       if (TREE_STATIC (decl) && decl_function_context (decl))
21134         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21135       break;
21136
21137     case NAMESPACE_DECL:
21138     case IMPORTED_DECL:
21139       if (debug_info_level <= DINFO_LEVEL_TERSE)
21140         return;
21141       if (lookup_decl_die (decl) != NULL)
21142         return;
21143       break;
21144
21145     case TYPE_DECL:
21146       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21147       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21148         return;
21149
21150       /* Don't bother trying to generate any DIEs to represent any of the
21151          normal built-in types for the language we are compiling.  */
21152       if (DECL_IS_BUILTIN (decl))
21153         return;
21154
21155       /* If we are in terse mode, don't generate any DIEs for types.  */
21156       if (debug_info_level <= DINFO_LEVEL_TERSE)
21157         return;
21158
21159       /* If we're a function-scope tag, initially use a parent of NULL;
21160          this will be fixed up in decls_for_scope.  */
21161       if (decl_function_context (decl))
21162         context_die = NULL;
21163
21164       break;
21165
21166     default:
21167       return;
21168     }
21169
21170   gen_decl_die (decl, NULL, context_die);
21171 }
21172
21173 /* Write the debugging output for DECL.  */
21174
21175 static void
21176 dwarf2out_function_decl (tree decl)
21177 {
21178   dwarf2out_decl (decl);
21179
21180   htab_empty (decl_loc_table);
21181 }
21182
21183 /* Output a marker (i.e. a label) for the beginning of the generated code for
21184    a lexical block.  */
21185
21186 static void
21187 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21188                        unsigned int blocknum)
21189 {
21190   switch_to_section (current_function_section ());
21191   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21192 }
21193
21194 /* Output a marker (i.e. a label) for the end of the generated code for a
21195    lexical block.  */
21196
21197 static void
21198 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21199 {
21200   switch_to_section (current_function_section ());
21201   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21202 }
21203
21204 /* Returns nonzero if it is appropriate not to emit any debugging
21205    information for BLOCK, because it doesn't contain any instructions.
21206
21207    Don't allow this for blocks with nested functions or local classes
21208    as we would end up with orphans, and in the presence of scheduling
21209    we may end up calling them anyway.  */
21210
21211 static bool
21212 dwarf2out_ignore_block (const_tree block)
21213 {
21214   tree decl;
21215   unsigned int i;
21216
21217   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21218     if (TREE_CODE (decl) == FUNCTION_DECL
21219         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21220       return 0;
21221   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21222     {
21223       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21224       if (TREE_CODE (decl) == FUNCTION_DECL
21225           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21226       return 0;
21227     }
21228
21229   return 1;
21230 }
21231
21232 /* Hash table routines for file_hash.  */
21233
21234 static int
21235 file_table_eq (const void *p1_p, const void *p2_p)
21236 {
21237   const struct dwarf_file_data *const p1 =
21238     (const struct dwarf_file_data *) p1_p;
21239   const char *const p2 = (const char *) p2_p;
21240   return strcmp (p1->filename, p2) == 0;
21241 }
21242
21243 static hashval_t
21244 file_table_hash (const void *p_p)
21245 {
21246   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21247   return htab_hash_string (p->filename);
21248 }
21249
21250 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21251    dwarf2out.c) and return its "index".  The index of each (known) filename is
21252    just a unique number which is associated with only that one filename.  We
21253    need such numbers for the sake of generating labels (in the .debug_sfnames
21254    section) and references to those files numbers (in the .debug_srcinfo
21255    and.debug_macinfo sections).  If the filename given as an argument is not
21256    found in our current list, add it to the list and assign it the next
21257    available unique index number.  In order to speed up searches, we remember
21258    the index of the filename was looked up last.  This handles the majority of
21259    all searches.  */
21260
21261 static struct dwarf_file_data *
21262 lookup_filename (const char *file_name)
21263 {
21264   void ** slot;
21265   struct dwarf_file_data * created;
21266
21267   /* Check to see if the file name that was searched on the previous
21268      call matches this file name.  If so, return the index.  */
21269   if (file_table_last_lookup
21270       && (file_name == file_table_last_lookup->filename
21271           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21272     return file_table_last_lookup;
21273
21274   /* Didn't match the previous lookup, search the table.  */
21275   slot = htab_find_slot_with_hash (file_table, file_name,
21276                                    htab_hash_string (file_name), INSERT);
21277   if (*slot)
21278     return (struct dwarf_file_data *) *slot;
21279
21280   created = ggc_alloc_dwarf_file_data ();
21281   created->filename = file_name;
21282   created->emitted_number = 0;
21283   *slot = created;
21284   return created;
21285 }
21286
21287 /* If the assembler will construct the file table, then translate the compiler
21288    internal file table number into the assembler file table number, and emit
21289    a .file directive if we haven't already emitted one yet.  The file table
21290    numbers are different because we prune debug info for unused variables and
21291    types, which may include filenames.  */
21292
21293 static int
21294 maybe_emit_file (struct dwarf_file_data * fd)
21295 {
21296   if (! fd->emitted_number)
21297     {
21298       if (last_emitted_file)
21299         fd->emitted_number = last_emitted_file->emitted_number + 1;
21300       else
21301         fd->emitted_number = 1;
21302       last_emitted_file = fd;
21303
21304       if (DWARF2_ASM_LINE_DEBUG_INFO)
21305         {
21306           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21307           output_quoted_string (asm_out_file,
21308                                 remap_debug_filename (fd->filename));
21309           fputc ('\n', asm_out_file);
21310         }
21311     }
21312
21313   return fd->emitted_number;
21314 }
21315
21316 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21317    That generation should happen after function debug info has been
21318    generated. The value of the attribute is the constant value of ARG.  */
21319
21320 static void
21321 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21322 {
21323   die_arg_entry entry;
21324
21325   if (!die || !arg)
21326     return;
21327
21328   if (!tmpl_value_parm_die_table)
21329     tmpl_value_parm_die_table
21330       = VEC_alloc (die_arg_entry, gc, 32);
21331
21332   entry.die = die;
21333   entry.arg = arg;
21334   VEC_safe_push (die_arg_entry, gc,
21335                  tmpl_value_parm_die_table,
21336                  &entry);
21337 }
21338
21339 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21340    by append_entry_to_tmpl_value_parm_die_table. This function must
21341    be called after function DIEs have been generated.  */
21342
21343 static void
21344 gen_remaining_tmpl_value_param_die_attribute (void)
21345 {
21346   if (tmpl_value_parm_die_table)
21347     {
21348       unsigned i;
21349       die_arg_entry *e;
21350
21351       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21352         tree_add_const_value_attribute (e->die, e->arg);
21353     }
21354 }
21355
21356
21357 /* Replace DW_AT_name for the decl with name.  */
21358
21359 static void
21360 dwarf2out_set_name (tree decl, tree name)
21361 {
21362   dw_die_ref die;
21363   dw_attr_ref attr;
21364   const char *dname;
21365
21366   die = TYPE_SYMTAB_DIE (decl);
21367   if (!die)
21368     return;
21369
21370   dname = dwarf2_name (name, 0);
21371   if (!dname)
21372     return;
21373
21374   attr = get_AT (die, DW_AT_name);
21375   if (attr)
21376     {
21377       struct indirect_string_node *node;
21378
21379       node = find_AT_string (dname);
21380       /* replace the string.  */
21381       attr->dw_attr_val.v.val_str = node;
21382     }
21383
21384   else
21385     add_name_attribute (die, dname);
21386 }
21387
21388 /* Called by the final INSN scan whenever we see a direct function call.
21389    Make an entry into the direct call table, recording the point of call
21390    and a reference to the target function's debug entry.  */
21391
21392 static void
21393 dwarf2out_direct_call (tree targ)
21394 {
21395   dcall_entry e;
21396   tree origin = decl_ultimate_origin (targ);
21397
21398   /* If this is a clone, use the abstract origin as the target.  */
21399   if (origin)
21400     targ = origin;
21401
21402   e.poc_label_num = poc_label_num++;
21403   e.poc_decl = current_function_decl;
21404   e.targ_die = force_decl_die (targ);
21405   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21406
21407   /* Drop a label at the return point to mark the point of call.  */
21408   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21409 }
21410
21411 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21412
21413 static hashval_t
21414 vcall_insn_table_hash (const void *x)
21415 {
21416   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21417 }
21418
21419 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21420    insnd_uid of *Y.  */
21421
21422 static int
21423 vcall_insn_table_eq (const void *x, const void *y)
21424 {
21425   return (((const struct vcall_insn *) x)->insn_uid
21426           == ((const struct vcall_insn *) y)->insn_uid);
21427 }
21428
21429 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21430
21431 static void
21432 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21433 {
21434   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21435   struct vcall_insn **slot;
21436
21437   gcc_assert (item);
21438   item->insn_uid = insn_uid;
21439   item->vtable_slot = vtable_slot;
21440   slot = (struct vcall_insn **)
21441       htab_find_slot_with_hash (vcall_insn_table, &item,
21442                                 (hashval_t) insn_uid, INSERT);
21443   *slot = item;
21444 }
21445
21446 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21447
21448 static unsigned int
21449 lookup_vcall_insn (unsigned int insn_uid)
21450 {
21451   struct vcall_insn item;
21452   struct vcall_insn *p;
21453
21454   item.insn_uid = insn_uid;
21455   item.vtable_slot = 0;
21456   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21457                                                  (void *) &item,
21458                                                  (hashval_t) insn_uid);
21459   if (p == NULL)
21460     return (unsigned int) -1;
21461   return p->vtable_slot;
21462 }
21463
21464
21465 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21466    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21467    is the vtable slot index that we will need to put in the virtual call
21468    table later.  */
21469
21470 static void
21471 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21472 {
21473   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21474     {
21475       tree token = OBJ_TYPE_REF_TOKEN (addr);
21476       if (TREE_CODE (token) == INTEGER_CST)
21477         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21478     }
21479 }
21480
21481 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21482    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21483    with NEW_INSN.  */
21484
21485 static void
21486 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21487 {
21488   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21489
21490   if (vtable_slot != (unsigned int) -1)
21491     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21492 }
21493
21494 /* Called by the final INSN scan whenever we see a virtual function call.
21495    Make an entry into the virtual call table, recording the point of call
21496    and the slot index of the vtable entry used to call the virtual member
21497    function.  The slot index was associated with the INSN_UID during the
21498    lowering to RTL.  */
21499
21500 static void
21501 dwarf2out_virtual_call (int insn_uid)
21502 {
21503   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21504   vcall_entry e;
21505
21506   if (vtable_slot == (unsigned int) -1)
21507     return;
21508
21509   e.poc_label_num = poc_label_num++;
21510   e.vtable_slot = vtable_slot;
21511   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21512
21513   /* Drop a label at the return point to mark the point of call.  */
21514   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21515 }
21516
21517 /* Called by the final INSN scan whenever we see a var location.  We
21518    use it to drop labels in the right places, and throw the location in
21519    our lookup table.  */
21520
21521 static void
21522 dwarf2out_var_location (rtx loc_note)
21523 {
21524   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21525   struct var_loc_node *newloc;
21526   rtx next_real;
21527   static const char *last_label;
21528   static const char *last_postcall_label;
21529   static bool last_in_cold_section_p;
21530   tree decl;
21531
21532   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21533     return;
21534
21535   next_real = next_real_insn (loc_note);
21536   /* If there are no instructions which would be affected by this note,
21537      don't do anything.  */
21538   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21539     return;
21540
21541   /* If there were any real insns between note we processed last time
21542      and this note (or if it is the first note), clear
21543      last_{,postcall_}label so that they are not reused this time.  */
21544   if (last_var_location_insn == NULL_RTX
21545       || last_var_location_insn != next_real
21546       || last_in_cold_section_p != in_cold_section_p)
21547     {
21548       last_label = NULL;
21549       last_postcall_label = NULL;
21550     }
21551
21552   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21553   newloc = add_var_loc_to_decl (decl, loc_note,
21554                                 NOTE_DURING_CALL_P (loc_note)
21555                                 ? last_postcall_label : last_label);
21556   if (newloc == NULL)
21557     return;
21558
21559   /* If there were no real insns between note we processed last time
21560      and this note, use the label we emitted last time.  Otherwise
21561      create a new label and emit it.  */
21562   if (last_label == NULL)
21563     {
21564       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21565       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21566       loclabel_num++;
21567       last_label = ggc_strdup (loclabel);
21568     }
21569
21570   if (!NOTE_DURING_CALL_P (loc_note))
21571     newloc->label = last_label;
21572   else
21573     {
21574       if (!last_postcall_label)
21575         {
21576           sprintf (loclabel, "%s-1", last_label);
21577           last_postcall_label = ggc_strdup (loclabel);
21578         }
21579       newloc->label = last_postcall_label;
21580     }
21581
21582   last_var_location_insn = next_real;
21583   last_in_cold_section_p = in_cold_section_p;
21584 }
21585
21586 /* We need to reset the locations at the beginning of each
21587    function. We can't do this in the end_function hook, because the
21588    declarations that use the locations won't have been output when
21589    that hook is called.  Also compute have_multiple_function_sections here.  */
21590
21591 static void
21592 dwarf2out_begin_function (tree fun)
21593 {
21594   if (function_section (fun) != text_section)
21595     have_multiple_function_sections = true;
21596
21597   dwarf2out_note_section_used ();
21598 }
21599
21600 /* Output a label to mark the beginning of a source code line entry
21601    and record information relating to this source line, in
21602    'line_info_table' for later output of the .debug_line section.  */
21603
21604 static void
21605 dwarf2out_source_line (unsigned int line, const char *filename,
21606                        int discriminator, bool is_stmt)
21607 {
21608   static bool last_is_stmt = true;
21609
21610   if (debug_info_level >= DINFO_LEVEL_NORMAL
21611       && line != 0)
21612     {
21613       int file_num = maybe_emit_file (lookup_filename (filename));
21614
21615       switch_to_section (current_function_section ());
21616
21617       /* If requested, emit something human-readable.  */
21618       if (flag_debug_asm)
21619         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21620                  filename, line);
21621
21622       if (DWARF2_ASM_LINE_DEBUG_INFO)
21623         {
21624           /* Emit the .loc directive understood by GNU as.  */
21625           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21626           if (is_stmt != last_is_stmt)
21627             {
21628               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21629               last_is_stmt = is_stmt;
21630             }
21631           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21632             fprintf (asm_out_file, " discriminator %d", discriminator);
21633           fputc ('\n', asm_out_file);
21634
21635           /* Indicate that line number info exists.  */
21636           line_info_table_in_use++;
21637         }
21638       else if (function_section (current_function_decl) != text_section)
21639         {
21640           dw_separate_line_info_ref line_info;
21641           targetm.asm_out.internal_label (asm_out_file,
21642                                           SEPARATE_LINE_CODE_LABEL,
21643                                           separate_line_info_table_in_use);
21644
21645           /* Expand the line info table if necessary.  */
21646           if (separate_line_info_table_in_use
21647               == separate_line_info_table_allocated)
21648             {
21649               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21650               separate_line_info_table
21651                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21652                                  separate_line_info_table,
21653                                  separate_line_info_table_allocated);
21654               memset (separate_line_info_table
21655                        + separate_line_info_table_in_use,
21656                       0,
21657                       (LINE_INFO_TABLE_INCREMENT
21658                        * sizeof (dw_separate_line_info_entry)));
21659             }
21660
21661           /* Add the new entry at the end of the line_info_table.  */
21662           line_info
21663             = &separate_line_info_table[separate_line_info_table_in_use++];
21664           line_info->dw_file_num = file_num;
21665           line_info->dw_line_num = line;
21666           line_info->function = current_function_funcdef_no;
21667         }
21668       else
21669         {
21670           dw_line_info_ref line_info;
21671
21672           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21673                                      line_info_table_in_use);
21674
21675           /* Expand the line info table if necessary.  */
21676           if (line_info_table_in_use == line_info_table_allocated)
21677             {
21678               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21679               line_info_table
21680                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21681                                  line_info_table_allocated);
21682               memset (line_info_table + line_info_table_in_use, 0,
21683                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21684             }
21685
21686           /* Add the new entry at the end of the line_info_table.  */
21687           line_info = &line_info_table[line_info_table_in_use++];
21688           line_info->dw_file_num = file_num;
21689           line_info->dw_line_num = line;
21690         }
21691     }
21692 }
21693
21694 /* Record the beginning of a new source file.  */
21695
21696 static void
21697 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21698 {
21699   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21700     {
21701       /* Record the beginning of the file for break_out_includes.  */
21702       dw_die_ref bincl_die;
21703
21704       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21705       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21706     }
21707
21708   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21709     {
21710       macinfo_entry e;
21711       e.code = DW_MACINFO_start_file;
21712       e.lineno = lineno;
21713       e.info = xstrdup (filename);
21714       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21715     }
21716 }
21717
21718 /* Record the end of a source file.  */
21719
21720 static void
21721 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21722 {
21723   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21724     /* Record the end of the file for break_out_includes.  */
21725     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21726
21727   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21728     {
21729       macinfo_entry e;
21730       e.code = DW_MACINFO_end_file;
21731       e.lineno = lineno;
21732       e.info = NULL;
21733       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21734     }
21735 }
21736
21737 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21738    the tail part of the directive line, i.e. the part which is past the
21739    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21740
21741 static void
21742 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21743                   const char *buffer ATTRIBUTE_UNUSED)
21744 {
21745   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21746     {
21747       macinfo_entry e;
21748       e.code = DW_MACINFO_define;
21749       e.lineno = lineno;
21750       e.info = xstrdup (buffer);;
21751       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21752     }
21753 }
21754
21755 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21756    the tail part of the directive line, i.e. the part which is past the
21757    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21758
21759 static void
21760 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21761                  const char *buffer ATTRIBUTE_UNUSED)
21762 {
21763   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21764     {
21765       macinfo_entry e;
21766       e.code = DW_MACINFO_undef;
21767       e.lineno = lineno;
21768       e.info = xstrdup (buffer);;
21769       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21770     }
21771 }
21772
21773 static void
21774 output_macinfo (void)
21775 {
21776   unsigned i;
21777   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
21778   macinfo_entry *ref;
21779
21780   if (! length)
21781     return;
21782
21783   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
21784     {
21785       switch (ref->code)
21786         {
21787           case DW_MACINFO_start_file:
21788             {
21789               int file_num = maybe_emit_file (lookup_filename (ref->info));
21790               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21791               dw2_asm_output_data_uleb128 
21792                         (ref->lineno, "Included from line number %lu", 
21793                                                 (unsigned long)ref->lineno);
21794               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21795             }
21796             break;
21797           case DW_MACINFO_end_file:
21798             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21799             break;
21800           case DW_MACINFO_define:
21801             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21802             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
21803                                                 (unsigned long)ref->lineno);
21804             dw2_asm_output_nstring (ref->info, -1, "The macro");
21805             break;
21806           case DW_MACINFO_undef:
21807             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21808             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21809                                                 (unsigned long)ref->lineno);
21810             dw2_asm_output_nstring (ref->info, -1, "The macro");
21811             break;
21812           default:
21813            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21814              ASM_COMMENT_START, (unsigned long)ref->code);
21815           break;
21816         }
21817     }
21818 }
21819
21820 /* Set up for Dwarf output at the start of compilation.  */
21821
21822 static void
21823 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21824 {
21825   /* Allocate the file_table.  */
21826   file_table = htab_create_ggc (50, file_table_hash,
21827                                 file_table_eq, NULL);
21828
21829   /* Allocate the decl_die_table.  */
21830   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21831                                     decl_die_table_eq, NULL);
21832
21833   /* Allocate the decl_loc_table.  */
21834   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21835                                     decl_loc_table_eq, NULL);
21836
21837   /* Allocate the initial hunk of the decl_scope_table.  */
21838   decl_scope_table = VEC_alloc (tree, gc, 256);
21839
21840   /* Allocate the initial hunk of the abbrev_die_table.  */
21841   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21842     (ABBREV_DIE_TABLE_INCREMENT);
21843   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21844   /* Zero-th entry is allocated, but unused.  */
21845   abbrev_die_table_in_use = 1;
21846
21847   /* Allocate the initial hunk of the line_info_table.  */
21848   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21849     (LINE_INFO_TABLE_INCREMENT);
21850   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21851
21852   /* Zero-th entry is allocated, but unused.  */
21853   line_info_table_in_use = 1;
21854
21855   /* Allocate the pubtypes and pubnames vectors.  */
21856   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21857   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21858
21859   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21860   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21861                                       vcall_insn_table_eq, NULL);
21862
21863   incomplete_types = VEC_alloc (tree, gc, 64);
21864
21865   used_rtx_array = VEC_alloc (rtx, gc, 32);
21866
21867   debug_info_section = get_section (DEBUG_INFO_SECTION,
21868                                     SECTION_DEBUG, NULL);
21869   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21870                                       SECTION_DEBUG, NULL);
21871   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21872                                        SECTION_DEBUG, NULL);
21873   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21874                                        SECTION_DEBUG, NULL);
21875   debug_line_section = get_section (DEBUG_LINE_SECTION,
21876                                     SECTION_DEBUG, NULL);
21877   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21878                                    SECTION_DEBUG, NULL);
21879   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21880                                         SECTION_DEBUG, NULL);
21881   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21882                                         SECTION_DEBUG, NULL);
21883   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21884                                      SECTION_DEBUG, NULL);
21885   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21886                                      SECTION_DEBUG, NULL);
21887   debug_str_section = get_section (DEBUG_STR_SECTION,
21888                                    DEBUG_STR_SECTION_FLAGS, NULL);
21889   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21890                                       SECTION_DEBUG, NULL);
21891   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21892                                      SECTION_DEBUG, NULL);
21893
21894   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21895   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21896                                DEBUG_ABBREV_SECTION_LABEL, 0);
21897   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21898   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21899                                COLD_TEXT_SECTION_LABEL, 0);
21900   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21901
21902   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21903                                DEBUG_INFO_SECTION_LABEL, 0);
21904   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21905                                DEBUG_LINE_SECTION_LABEL, 0);
21906   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21907                                DEBUG_RANGES_SECTION_LABEL, 0);
21908   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21909                                DEBUG_MACINFO_SECTION_LABEL, 0);
21910
21911   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21912     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
21913
21914   switch_to_section (text_section);
21915   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21916   if (flag_reorder_blocks_and_partition)
21917     {
21918       cold_text_section = unlikely_text_section ();
21919       switch_to_section (cold_text_section);
21920       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21921     }
21922
21923 }
21924
21925 /* Called before cgraph_optimize starts outputtting functions, variables
21926    and toplevel asms into assembly.  */
21927
21928 static void
21929 dwarf2out_assembly_start (void)
21930 {
21931   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21932       && dwarf2out_do_cfi_asm ()
21933       && (!(flag_unwind_tables || flag_exceptions)
21934           || targetm.except_unwind_info () != UI_DWARF2))
21935     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21936 }
21937
21938 /* A helper function for dwarf2out_finish called through
21939    htab_traverse.  Emit one queued .debug_str string.  */
21940
21941 static int
21942 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21943 {
21944   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21945
21946   if (node->label && node->refcount)
21947     {
21948       switch_to_section (debug_str_section);
21949       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21950       assemble_string (node->str, strlen (node->str) + 1);
21951     }
21952
21953   return 1;
21954 }
21955
21956 #if ENABLE_ASSERT_CHECKING
21957 /* Verify that all marks are clear.  */
21958
21959 static void
21960 verify_marks_clear (dw_die_ref die)
21961 {
21962   dw_die_ref c;
21963
21964   gcc_assert (! die->die_mark);
21965   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21966 }
21967 #endif /* ENABLE_ASSERT_CHECKING */
21968
21969 /* Clear the marks for a die and its children.
21970    Be cool if the mark isn't set.  */
21971
21972 static void
21973 prune_unmark_dies (dw_die_ref die)
21974 {
21975   dw_die_ref c;
21976
21977   if (die->die_mark)
21978     die->die_mark = 0;
21979   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21980 }
21981
21982 /* Given DIE that we're marking as used, find any other dies
21983    it references as attributes and mark them as used.  */
21984
21985 static void
21986 prune_unused_types_walk_attribs (dw_die_ref die)
21987 {
21988   dw_attr_ref a;
21989   unsigned ix;
21990
21991   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21992     {
21993       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21994         {
21995           /* A reference to another DIE.
21996              Make sure that it will get emitted.
21997              If it was broken out into a comdat group, don't follow it.  */
21998           if (dwarf_version < 4
21999               || a->dw_attr == DW_AT_specification
22000               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22001             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22002         }
22003       /* Set the string's refcount to 0 so that prune_unused_types_mark
22004          accounts properly for it.  */
22005       if (AT_class (a) == dw_val_class_str)
22006         a->dw_attr_val.v.val_str->refcount = 0;
22007     }
22008 }
22009
22010
22011 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22012    to DIE's children.  */
22013
22014 static void
22015 prune_unused_types_mark (dw_die_ref die, int dokids)
22016 {
22017   dw_die_ref c;
22018
22019   if (die->die_mark == 0)
22020     {
22021       /* We haven't done this node yet.  Mark it as used.  */
22022       die->die_mark = 1;
22023
22024       /* We also have to mark its parents as used.
22025          (But we don't want to mark our parents' kids due to this.)  */
22026       if (die->die_parent)
22027         prune_unused_types_mark (die->die_parent, 0);
22028
22029       /* Mark any referenced nodes.  */
22030       prune_unused_types_walk_attribs (die);
22031
22032       /* If this node is a specification,
22033          also mark the definition, if it exists.  */
22034       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22035         prune_unused_types_mark (die->die_definition, 1);
22036     }
22037
22038   if (dokids && die->die_mark != 2)
22039     {
22040       /* We need to walk the children, but haven't done so yet.
22041          Remember that we've walked the kids.  */
22042       die->die_mark = 2;
22043
22044       /* If this is an array type, we need to make sure our
22045          kids get marked, even if they're types.  If we're
22046          breaking out types into comdat sections, do this
22047          for all type definitions.  */
22048       if (die->die_tag == DW_TAG_array_type
22049           || (dwarf_version >= 4
22050               && is_type_die (die) && ! is_declaration_die (die)))
22051         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22052       else
22053         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22054     }
22055 }
22056
22057 /* For local classes, look if any static member functions were emitted
22058    and if so, mark them.  */
22059
22060 static void
22061 prune_unused_types_walk_local_classes (dw_die_ref die)
22062 {
22063   dw_die_ref c;
22064
22065   if (die->die_mark == 2)
22066     return;
22067
22068   switch (die->die_tag)
22069     {
22070     case DW_TAG_structure_type:
22071     case DW_TAG_union_type:
22072     case DW_TAG_class_type:
22073       break;
22074
22075     case DW_TAG_subprogram:
22076       if (!get_AT_flag (die, DW_AT_declaration)
22077           || die->die_definition != NULL)
22078         prune_unused_types_mark (die, 1);
22079       return;
22080
22081     default:
22082       return;
22083     }
22084
22085   /* Mark children.  */
22086   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22087 }
22088
22089 /* Walk the tree DIE and mark types that we actually use.  */
22090
22091 static void
22092 prune_unused_types_walk (dw_die_ref die)
22093 {
22094   dw_die_ref c;
22095
22096   /* Don't do anything if this node is already marked and
22097      children have been marked as well.  */
22098   if (die->die_mark == 2)
22099     return;
22100
22101   switch (die->die_tag)
22102     {
22103     case DW_TAG_structure_type:
22104     case DW_TAG_union_type:
22105     case DW_TAG_class_type:
22106       if (die->die_perennial_p)
22107         break;
22108
22109       for (c = die->die_parent; c; c = c->die_parent)
22110         if (c->die_tag == DW_TAG_subprogram)
22111           break;
22112
22113       /* Finding used static member functions inside of classes
22114          is needed just for local classes, because for other classes
22115          static member function DIEs with DW_AT_specification
22116          are emitted outside of the DW_TAG_*_type.  If we ever change
22117          it, we'd need to call this even for non-local classes.  */
22118       if (c)
22119         prune_unused_types_walk_local_classes (die);
22120
22121       /* It's a type node --- don't mark it.  */
22122       return;
22123
22124     case DW_TAG_const_type:
22125     case DW_TAG_packed_type:
22126     case DW_TAG_pointer_type:
22127     case DW_TAG_reference_type:
22128     case DW_TAG_rvalue_reference_type:
22129     case DW_TAG_volatile_type:
22130     case DW_TAG_typedef:
22131     case DW_TAG_array_type:
22132     case DW_TAG_interface_type:
22133     case DW_TAG_friend:
22134     case DW_TAG_variant_part:
22135     case DW_TAG_enumeration_type:
22136     case DW_TAG_subroutine_type:
22137     case DW_TAG_string_type:
22138     case DW_TAG_set_type:
22139     case DW_TAG_subrange_type:
22140     case DW_TAG_ptr_to_member_type:
22141     case DW_TAG_file_type:
22142       if (die->die_perennial_p)
22143         break;
22144
22145       /* It's a type node --- don't mark it.  */
22146       return;
22147
22148     default:
22149       /* Mark everything else.  */
22150       break;
22151   }
22152
22153   if (die->die_mark == 0)
22154     {
22155       die->die_mark = 1;
22156
22157       /* Now, mark any dies referenced from here.  */
22158       prune_unused_types_walk_attribs (die);
22159     }
22160
22161   die->die_mark = 2;
22162
22163   /* Mark children.  */
22164   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22165 }
22166
22167 /* Increment the string counts on strings referred to from DIE's
22168    attributes.  */
22169
22170 static void
22171 prune_unused_types_update_strings (dw_die_ref die)
22172 {
22173   dw_attr_ref a;
22174   unsigned ix;
22175
22176   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22177     if (AT_class (a) == dw_val_class_str)
22178       {
22179         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22180         s->refcount++;
22181         /* Avoid unnecessarily putting strings that are used less than
22182            twice in the hash table.  */
22183         if (s->refcount
22184             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22185           {
22186             void ** slot;
22187             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22188                                              htab_hash_string (s->str),
22189                                              INSERT);
22190             gcc_assert (*slot == NULL);
22191             *slot = s;
22192           }
22193       }
22194 }
22195
22196 /* Remove from the tree DIE any dies that aren't marked.  */
22197
22198 static void
22199 prune_unused_types_prune (dw_die_ref die)
22200 {
22201   dw_die_ref c;
22202
22203   gcc_assert (die->die_mark);
22204   prune_unused_types_update_strings (die);
22205
22206   if (! die->die_child)
22207     return;
22208
22209   c = die->die_child;
22210   do {
22211     dw_die_ref prev = c;
22212     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22213       if (c == die->die_child)
22214         {
22215           /* No marked children between 'prev' and the end of the list.  */
22216           if (prev == c)
22217             /* No marked children at all.  */
22218             die->die_child = NULL;
22219           else
22220             {
22221               prev->die_sib = c->die_sib;
22222               die->die_child = prev;
22223             }
22224           return;
22225         }
22226
22227     if (c != prev->die_sib)
22228       prev->die_sib = c;
22229     prune_unused_types_prune (c);
22230   } while (c != die->die_child);
22231 }
22232
22233 /* A helper function for dwarf2out_finish called through
22234    htab_traverse.  Clear .debug_str strings that we haven't already
22235    decided to emit.  */
22236
22237 static int
22238 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22239 {
22240   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22241
22242   if (!node->label || !node->refcount)
22243     htab_clear_slot (debug_str_hash, h);
22244
22245   return 1;
22246 }
22247
22248 /* Remove dies representing declarations that we never use.  */
22249
22250 static void
22251 prune_unused_types (void)
22252 {
22253   unsigned int i;
22254   limbo_die_node *node;
22255   comdat_type_node *ctnode;
22256   pubname_ref pub;
22257   dcall_entry *dcall;
22258
22259 #if ENABLE_ASSERT_CHECKING
22260   /* All the marks should already be clear.  */
22261   verify_marks_clear (comp_unit_die ());
22262   for (node = limbo_die_list; node; node = node->next)
22263     verify_marks_clear (node->die);
22264   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22265     verify_marks_clear (ctnode->root_die);
22266 #endif /* ENABLE_ASSERT_CHECKING */
22267
22268   /* Mark types that are used in global variables.  */
22269   premark_types_used_by_global_vars ();
22270
22271   /* Set the mark on nodes that are actually used.  */
22272   prune_unused_types_walk (comp_unit_die ());
22273   for (node = limbo_die_list; node; node = node->next)
22274     prune_unused_types_walk (node->die);
22275   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22276     {
22277       prune_unused_types_walk (ctnode->root_die);
22278       prune_unused_types_mark (ctnode->type_die, 1);
22279     }
22280
22281   /* Also set the mark on nodes referenced from the
22282      pubname_table or arange_table.  */
22283   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22284     prune_unused_types_mark (pub->die, 1);
22285   for (i = 0; i < arange_table_in_use; i++)
22286     prune_unused_types_mark (arange_table[i], 1);
22287
22288   /* Mark nodes referenced from the direct call table.  */
22289   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22290     prune_unused_types_mark (dcall->targ_die, 1);
22291
22292   /* Get rid of nodes that aren't marked; and update the string counts.  */
22293   if (debug_str_hash && debug_str_hash_forced)
22294     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22295   else if (debug_str_hash)
22296     htab_empty (debug_str_hash);
22297   prune_unused_types_prune (comp_unit_die ());
22298   for (node = limbo_die_list; node; node = node->next)
22299     prune_unused_types_prune (node->die);
22300   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22301     prune_unused_types_prune (ctnode->root_die);
22302
22303   /* Leave the marks clear.  */
22304   prune_unmark_dies (comp_unit_die ());
22305   for (node = limbo_die_list; node; node = node->next)
22306     prune_unmark_dies (node->die);
22307   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22308     prune_unmark_dies (ctnode->root_die);
22309 }
22310
22311 /* Set the parameter to true if there are any relative pathnames in
22312    the file table.  */
22313 static int
22314 file_table_relative_p (void ** slot, void *param)
22315 {
22316   bool *p = (bool *) param;
22317   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22318   if (!IS_ABSOLUTE_PATH (d->filename))
22319     {
22320       *p = true;
22321       return 0;
22322     }
22323   return 1;
22324 }
22325
22326 /* Routines to manipulate hash table of comdat type units.  */
22327
22328 static hashval_t
22329 htab_ct_hash (const void *of)
22330 {
22331   hashval_t h;
22332   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22333
22334   memcpy (&h, type_node->signature, sizeof (h));
22335   return h;
22336 }
22337
22338 static int
22339 htab_ct_eq (const void *of1, const void *of2)
22340 {
22341   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22342   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22343
22344   return (! memcmp (type_node_1->signature, type_node_2->signature,
22345                     DWARF_TYPE_SIGNATURE_SIZE));
22346 }
22347
22348 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22349    to the location it would have been added, should we know its
22350    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22351    probably improve compactness of debug info, removing equivalent
22352    abbrevs, and hide any differences caused by deferring the
22353    computation of the assembler name, triggered by e.g. PCH.  */
22354
22355 static inline void
22356 move_linkage_attr (dw_die_ref die)
22357 {
22358   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22359   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22360
22361   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22362               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22363
22364   while (--ix > 0)
22365     {
22366       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22367
22368       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22369         break;
22370     }
22371
22372   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22373     {
22374       VEC_pop (dw_attr_node, die->die_attr);
22375       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22376     }
22377 }
22378
22379 /* Helper function for resolve_addr, attempt to resolve
22380    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22381    SYMBOL_REFs refer to variables emitted in the current CU.  */
22382
22383 static int
22384 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22385 {
22386   rtx rtl = *addr;
22387
22388   if (GET_CODE (rtl) == CONST_STRING)
22389     {
22390       size_t len = strlen (XSTR (rtl, 0)) + 1;
22391       tree t = build_string (len, XSTR (rtl, 0));
22392       tree tlen = build_int_cst (NULL_TREE, len - 1);
22393       TREE_TYPE (t)
22394         = build_array_type (char_type_node, build_index_type (tlen));
22395       rtl = lookup_constant_def (t);
22396       if (!rtl || !MEM_P (rtl))
22397         return 1;
22398       rtl = XEXP (rtl, 0);
22399       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22400       *addr = rtl;
22401       return 0;
22402     }
22403
22404   if (GET_CODE (rtl) == SYMBOL_REF
22405       && SYMBOL_REF_DECL (rtl)
22406       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22407     return 1;
22408
22409   if (GET_CODE (rtl) == CONST
22410       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22411     return 1;
22412
22413   return 0;
22414 }
22415
22416 /* Helper function for resolve_addr, handle one location
22417    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22418    the location list couldn't be resolved.  */
22419
22420 static bool
22421 resolve_addr_in_expr (dw_loc_descr_ref loc)
22422 {
22423   for (; loc; loc = loc->dw_loc_next)
22424     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22425          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22426         || (loc->dw_loc_opc == DW_OP_implicit_value
22427             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22428             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22429       return false;
22430     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22431              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22432       {
22433         dw_die_ref ref
22434           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22435         if (ref == NULL)
22436           return false;
22437         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22438         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22439         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22440       }
22441   return true;
22442 }
22443
22444 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22445    an address in .rodata section if the string literal is emitted there,
22446    or remove the containing location list or replace DW_AT_const_value
22447    with DW_AT_location and empty location expression, if it isn't found
22448    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22449    to something that has been emitted in the current CU.  */
22450
22451 static void
22452 resolve_addr (dw_die_ref die)
22453 {
22454   dw_die_ref c;
22455   dw_attr_ref a;
22456   dw_loc_list_ref *curr;
22457   unsigned ix;
22458
22459   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22460     switch (AT_class (a))
22461       {
22462       case dw_val_class_loc_list:
22463         curr = AT_loc_list_ptr (a);
22464         while (*curr)
22465           {
22466             if (!resolve_addr_in_expr ((*curr)->expr))
22467               {
22468                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22469                 if (next && (*curr)->ll_symbol)
22470                   {
22471                     gcc_assert (!next->ll_symbol);
22472                     next->ll_symbol = (*curr)->ll_symbol;
22473                   }
22474                 *curr = next;
22475               }
22476             else
22477               curr = &(*curr)->dw_loc_next;
22478           }
22479         if (!AT_loc_list (a))
22480           {
22481             remove_AT (die, a->dw_attr);
22482             ix--;
22483           }
22484         break;
22485       case dw_val_class_loc:
22486         if (!resolve_addr_in_expr (AT_loc (a)))
22487           {
22488             remove_AT (die, a->dw_attr);
22489             ix--;
22490           }
22491         break;
22492       case dw_val_class_addr:
22493         if (a->dw_attr == DW_AT_const_value
22494             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22495           {
22496             remove_AT (die, a->dw_attr);
22497             ix--;
22498           }
22499         break;
22500       default:
22501         break;
22502       }
22503
22504   FOR_EACH_CHILD (die, c, resolve_addr (c));
22505 }
22506 \f
22507 /* Helper routines for optimize_location_lists.
22508    This pass tries to share identical local lists in .debug_loc
22509    section.  */
22510
22511 /* Iteratively hash operands of LOC opcode.  */
22512
22513 static inline hashval_t
22514 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22515 {
22516   dw_val_ref val1 = &loc->dw_loc_oprnd1;
22517   dw_val_ref val2 = &loc->dw_loc_oprnd2;
22518
22519   switch (loc->dw_loc_opc)
22520     {
22521     case DW_OP_const4u:
22522     case DW_OP_const8u:
22523       if (loc->dtprel)
22524         goto hash_addr;
22525       /* FALLTHRU */
22526     case DW_OP_const1u:
22527     case DW_OP_const1s:
22528     case DW_OP_const2u:
22529     case DW_OP_const2s:
22530     case DW_OP_const4s:
22531     case DW_OP_const8s:
22532     case DW_OP_constu:
22533     case DW_OP_consts:
22534     case DW_OP_pick:
22535     case DW_OP_plus_uconst:
22536     case DW_OP_breg0:
22537     case DW_OP_breg1:
22538     case DW_OP_breg2:
22539     case DW_OP_breg3:
22540     case DW_OP_breg4:
22541     case DW_OP_breg5:
22542     case DW_OP_breg6:
22543     case DW_OP_breg7:
22544     case DW_OP_breg8:
22545     case DW_OP_breg9:
22546     case DW_OP_breg10:
22547     case DW_OP_breg11:
22548     case DW_OP_breg12:
22549     case DW_OP_breg13:
22550     case DW_OP_breg14:
22551     case DW_OP_breg15:
22552     case DW_OP_breg16:
22553     case DW_OP_breg17:
22554     case DW_OP_breg18:
22555     case DW_OP_breg19:
22556     case DW_OP_breg20:
22557     case DW_OP_breg21:
22558     case DW_OP_breg22:
22559     case DW_OP_breg23:
22560     case DW_OP_breg24:
22561     case DW_OP_breg25:
22562     case DW_OP_breg26:
22563     case DW_OP_breg27:
22564     case DW_OP_breg28:
22565     case DW_OP_breg29:
22566     case DW_OP_breg30:
22567     case DW_OP_breg31:
22568     case DW_OP_regx:
22569     case DW_OP_fbreg:
22570     case DW_OP_piece:
22571     case DW_OP_deref_size:
22572     case DW_OP_xderef_size:
22573       hash = iterative_hash_object (val1->v.val_int, hash);
22574       break;
22575     case DW_OP_skip:
22576     case DW_OP_bra:
22577       {
22578         int offset;
22579
22580         gcc_assert (val1->val_class == dw_val_class_loc);
22581         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22582         hash = iterative_hash_object (offset, hash);
22583       }
22584       break;
22585     case DW_OP_implicit_value:
22586       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22587       switch (val2->val_class)
22588         {
22589         case dw_val_class_const:
22590           hash = iterative_hash_object (val2->v.val_int, hash);
22591           break;
22592         case dw_val_class_vec:
22593           {
22594             unsigned int elt_size = val2->v.val_vec.elt_size;
22595             unsigned int len = val2->v.val_vec.length;
22596
22597             hash = iterative_hash_object (elt_size, hash);
22598             hash = iterative_hash_object (len, hash);
22599             hash = iterative_hash (val2->v.val_vec.array,
22600                                    len * elt_size, hash);
22601           }
22602           break;
22603         case dw_val_class_const_double:
22604           hash = iterative_hash_object (val2->v.val_double.low, hash);
22605           hash = iterative_hash_object (val2->v.val_double.high, hash);
22606           break;
22607         case dw_val_class_addr:
22608           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22609           break;
22610         default:
22611           gcc_unreachable ();
22612         }
22613       break;
22614     case DW_OP_bregx:
22615     case DW_OP_bit_piece:
22616       hash = iterative_hash_object (val1->v.val_int, hash);
22617       hash = iterative_hash_object (val2->v.val_int, hash);
22618       break;
22619     case DW_OP_addr:
22620     hash_addr:
22621       if (loc->dtprel)
22622         {
22623           unsigned char dtprel = 0xd1;
22624           hash = iterative_hash_object (dtprel, hash);
22625         }
22626       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22627       break;
22628     case DW_OP_GNU_implicit_pointer:
22629       hash = iterative_hash_object (val2->v.val_int, hash);
22630       break;
22631
22632     default:
22633       /* Other codes have no operands.  */
22634       break;
22635     }
22636   return hash;
22637 }
22638
22639 /* Iteratively hash the whole DWARF location expression LOC.  */
22640
22641 static inline hashval_t
22642 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22643 {
22644   dw_loc_descr_ref l;
22645   bool sizes_computed = false;
22646   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22647   size_of_locs (loc);
22648
22649   for (l = loc; l != NULL; l = l->dw_loc_next)
22650     {
22651       enum dwarf_location_atom opc = l->dw_loc_opc;
22652       hash = iterative_hash_object (opc, hash);
22653       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22654         {
22655           size_of_locs (loc);
22656           sizes_computed = true;
22657         }
22658       hash = hash_loc_operands (l, hash);
22659     }
22660   return hash;
22661 }
22662
22663 /* Compute hash of the whole location list LIST_HEAD.  */
22664
22665 static inline void
22666 hash_loc_list (dw_loc_list_ref list_head)
22667 {
22668   dw_loc_list_ref curr = list_head;
22669   hashval_t hash = 0;
22670
22671   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22672     {
22673       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22674       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22675       if (curr->section)
22676         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22677                                hash);
22678       hash = hash_locs (curr->expr, hash);
22679     }
22680   list_head->hash = hash;
22681 }
22682
22683 /* Return true if X and Y opcodes have the same operands.  */
22684
22685 static inline bool
22686 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22687 {
22688   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22689   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22690   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22691   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22692
22693   switch (x->dw_loc_opc)
22694     {
22695     case DW_OP_const4u:
22696     case DW_OP_const8u:
22697       if (x->dtprel)
22698         goto hash_addr;
22699       /* FALLTHRU */
22700     case DW_OP_const1u:
22701     case DW_OP_const1s:
22702     case DW_OP_const2u:
22703     case DW_OP_const2s:
22704     case DW_OP_const4s:
22705     case DW_OP_const8s:
22706     case DW_OP_constu:
22707     case DW_OP_consts:
22708     case DW_OP_pick:
22709     case DW_OP_plus_uconst:
22710     case DW_OP_breg0:
22711     case DW_OP_breg1:
22712     case DW_OP_breg2:
22713     case DW_OP_breg3:
22714     case DW_OP_breg4:
22715     case DW_OP_breg5:
22716     case DW_OP_breg6:
22717     case DW_OP_breg7:
22718     case DW_OP_breg8:
22719     case DW_OP_breg9:
22720     case DW_OP_breg10:
22721     case DW_OP_breg11:
22722     case DW_OP_breg12:
22723     case DW_OP_breg13:
22724     case DW_OP_breg14:
22725     case DW_OP_breg15:
22726     case DW_OP_breg16:
22727     case DW_OP_breg17:
22728     case DW_OP_breg18:
22729     case DW_OP_breg19:
22730     case DW_OP_breg20:
22731     case DW_OP_breg21:
22732     case DW_OP_breg22:
22733     case DW_OP_breg23:
22734     case DW_OP_breg24:
22735     case DW_OP_breg25:
22736     case DW_OP_breg26:
22737     case DW_OP_breg27:
22738     case DW_OP_breg28:
22739     case DW_OP_breg29:
22740     case DW_OP_breg30:
22741     case DW_OP_breg31:
22742     case DW_OP_regx:
22743     case DW_OP_fbreg:
22744     case DW_OP_piece:
22745     case DW_OP_deref_size:
22746     case DW_OP_xderef_size:
22747       return valx1->v.val_int == valy1->v.val_int;
22748     case DW_OP_skip:
22749     case DW_OP_bra:
22750       gcc_assert (valx1->val_class == dw_val_class_loc
22751                   && valy1->val_class == dw_val_class_loc
22752                   && x->dw_loc_addr == y->dw_loc_addr);
22753       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22754     case DW_OP_implicit_value:
22755       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22756           || valx2->val_class != valy2->val_class)
22757         return false;
22758       switch (valx2->val_class)
22759         {
22760         case dw_val_class_const:
22761           return valx2->v.val_int == valy2->v.val_int;
22762         case dw_val_class_vec:
22763           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22764                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22765                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22766                             valx2->v.val_vec.elt_size
22767                             * valx2->v.val_vec.length) == 0;
22768         case dw_val_class_const_double:
22769           return valx2->v.val_double.low == valy2->v.val_double.low
22770                  && valx2->v.val_double.high == valy2->v.val_double.high;
22771         case dw_val_class_addr:
22772           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22773         default:
22774           gcc_unreachable ();
22775         }
22776     case DW_OP_bregx:
22777     case DW_OP_bit_piece:
22778       return valx1->v.val_int == valy1->v.val_int
22779              && valx2->v.val_int == valy2->v.val_int;
22780     case DW_OP_addr:
22781     hash_addr:
22782       return rtx_equal_p (valx1->v.val_addr, valx2->v.val_addr);
22783     case DW_OP_GNU_implicit_pointer:
22784       return valx1->val_class == dw_val_class_die_ref
22785              && valx1->val_class == valy1->val_class
22786              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22787              && valx2->v.val_int == valy2->v.val_int;
22788     default:
22789       /* Other codes have no operands.  */
22790       return true;
22791     }
22792 }
22793
22794 /* Return true if DWARF location expressions X and Y are the same.  */
22795
22796 static inline bool
22797 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22798 {
22799   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22800     if (x->dw_loc_opc != y->dw_loc_opc
22801         || x->dtprel != y->dtprel
22802         || !compare_loc_operands (x, y))
22803       break;
22804   return x == NULL && y == NULL;
22805 }
22806
22807 /* Return precomputed hash of location list X.  */
22808
22809 static hashval_t
22810 loc_list_hash (const void *x)
22811 {
22812   return ((const struct dw_loc_list_struct *) x)->hash;
22813 }
22814
22815 /* Return 1 if location lists X and Y are the same.  */
22816
22817 static int
22818 loc_list_eq (const void *x, const void *y)
22819 {
22820   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22821   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22822   if (a == b)
22823     return 1;
22824   if (a->hash != b->hash)
22825     return 0;
22826   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22827     if (strcmp (a->begin, b->begin) != 0
22828         || strcmp (a->end, b->end) != 0
22829         || (a->section == NULL) != (b->section == NULL)
22830         || (a->section && strcmp (a->section, b->section) != 0)
22831         || !compare_locs (a->expr, b->expr))
22832       break;
22833   return a == NULL && b == NULL;
22834 }
22835
22836 /* Recursively optimize location lists referenced from DIE
22837    children and share them whenever possible.  */
22838
22839 static void
22840 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22841 {
22842   dw_die_ref c;
22843   dw_attr_ref a;
22844   unsigned ix;
22845   void **slot;
22846
22847   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22848     if (AT_class (a) == dw_val_class_loc_list)
22849       {
22850         dw_loc_list_ref list = AT_loc_list (a);
22851         /* TODO: perform some optimizations here, before hashing
22852            it and storing into the hash table.  */
22853         hash_loc_list (list);
22854         slot = htab_find_slot_with_hash (htab, list, list->hash,
22855                                          INSERT);
22856         if (*slot == NULL)
22857           *slot = (void *) list;
22858         else
22859           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22860       }
22861
22862   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22863 }
22864
22865 /* Optimize location lists referenced from DIE
22866    children and share them whenever possible.  */
22867
22868 static void
22869 optimize_location_lists (dw_die_ref die)
22870 {
22871   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22872   optimize_location_lists_1 (die, htab);
22873   htab_delete (htab);
22874 }
22875 \f
22876 /* Output stuff that dwarf requires at the end of every file,
22877    and generate the DWARF-2 debugging info.  */
22878
22879 static void
22880 dwarf2out_finish (const char *filename)
22881 {
22882   limbo_die_node *node, *next_node;
22883   comdat_type_node *ctnode;
22884   htab_t comdat_type_table;
22885   dw_die_ref die = 0;
22886   unsigned int i;
22887
22888   gen_remaining_tmpl_value_param_die_attribute ();
22889
22890   /* Add the name for the main input file now.  We delayed this from
22891      dwarf2out_init to avoid complications with PCH.  */
22892   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22893   if (!IS_ABSOLUTE_PATH (filename))
22894     add_comp_dir_attribute (comp_unit_die ());
22895   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22896     {
22897       bool p = false;
22898       htab_traverse (file_table, file_table_relative_p, &p);
22899       if (p)
22900         add_comp_dir_attribute (comp_unit_die ());
22901     }
22902
22903   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22904     {
22905       add_location_or_const_value_attribute (
22906         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22907         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22908         DW_AT_location);
22909     }
22910
22911   /* Traverse the limbo die list, and add parent/child links.  The only
22912      dies without parents that should be here are concrete instances of
22913      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22914      For concrete instances, we can get the parent die from the abstract
22915      instance.  */
22916   for (node = limbo_die_list; node; node = next_node)
22917     {
22918       next_node = node->next;
22919       die = node->die;
22920
22921       if (die->die_parent == NULL)
22922         {
22923           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22924
22925           if (origin)
22926             add_child_die (origin->die_parent, die);
22927           else if (is_cu_die (die))
22928             ;
22929           else if (seen_error ())
22930             /* It's OK to be confused by errors in the input.  */
22931             add_child_die (comp_unit_die (), die);
22932           else
22933             {
22934               /* In certain situations, the lexical block containing a
22935                  nested function can be optimized away, which results
22936                  in the nested function die being orphaned.  Likewise
22937                  with the return type of that nested function.  Force
22938                  this to be a child of the containing function.
22939
22940                  It may happen that even the containing function got fully
22941                  inlined and optimized out.  In that case we are lost and
22942                  assign the empty child.  This should not be big issue as
22943                  the function is likely unreachable too.  */
22944               tree context = NULL_TREE;
22945
22946               gcc_assert (node->created_for);
22947
22948               if (DECL_P (node->created_for))
22949                 context = DECL_CONTEXT (node->created_for);
22950               else if (TYPE_P (node->created_for))
22951                 context = TYPE_CONTEXT (node->created_for);
22952
22953               gcc_assert (context
22954                           && (TREE_CODE (context) == FUNCTION_DECL
22955                               || TREE_CODE (context) == NAMESPACE_DECL));
22956
22957               origin = lookup_decl_die (context);
22958               if (origin)
22959                 add_child_die (origin, die);
22960               else
22961                 add_child_die (comp_unit_die (), die);
22962             }
22963         }
22964     }
22965
22966   limbo_die_list = NULL;
22967
22968   resolve_addr (comp_unit_die ());
22969
22970   for (node = deferred_asm_name; node; node = node->next)
22971     {
22972       tree decl = node->created_for;
22973       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22974         {
22975           add_linkage_attr (node->die, decl);
22976           move_linkage_attr (node->die);
22977         }
22978     }
22979
22980   deferred_asm_name = NULL;
22981
22982   /* Walk through the list of incomplete types again, trying once more to
22983      emit full debugging info for them.  */
22984   retry_incomplete_types ();
22985
22986   if (flag_eliminate_unused_debug_types)
22987     prune_unused_types ();
22988
22989   /* Generate separate CUs for each of the include files we've seen.
22990      They will go into limbo_die_list.  */
22991   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22992     break_out_includes (comp_unit_die ());
22993
22994   /* Generate separate COMDAT sections for type DIEs. */
22995   if (dwarf_version >= 4)
22996     {
22997       break_out_comdat_types (comp_unit_die ());
22998
22999       /* Each new type_unit DIE was added to the limbo die list when created.
23000          Since these have all been added to comdat_type_list, clear the
23001          limbo die list.  */
23002       limbo_die_list = NULL;
23003
23004       /* For each new comdat type unit, copy declarations for incomplete
23005          types to make the new unit self-contained (i.e., no direct
23006          references to the main compile unit).  */
23007       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23008         copy_decls_for_unworthy_types (ctnode->root_die);
23009       copy_decls_for_unworthy_types (comp_unit_die ());
23010
23011       /* In the process of copying declarations from one unit to another,
23012          we may have left some declarations behind that are no longer
23013          referenced.  Prune them.  */
23014       prune_unused_types ();
23015     }
23016
23017   /* Traverse the DIE's and add add sibling attributes to those DIE's
23018      that have children.  */
23019   add_sibling_attributes (comp_unit_die ());
23020   for (node = limbo_die_list; node; node = node->next)
23021     add_sibling_attributes (node->die);
23022   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23023     add_sibling_attributes (ctnode->root_die);
23024
23025   /* Output a terminator label for the .text section.  */
23026   switch_to_section (text_section);
23027   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23028   if (flag_reorder_blocks_and_partition)
23029     {
23030       switch_to_section (unlikely_text_section ());
23031       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23032     }
23033
23034   /* We can only use the low/high_pc attributes if all of the code was
23035      in .text.  */
23036   if (!have_multiple_function_sections
23037       || !(dwarf_version >= 3 || !dwarf_strict))
23038     {
23039       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23040       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23041     }
23042
23043   else
23044     {
23045       unsigned fde_idx = 0;
23046       bool range_list_added = false;
23047
23048       /* We need to give .debug_loc and .debug_ranges an appropriate
23049          "base address".  Use zero so that these addresses become
23050          absolute.  Historically, we've emitted the unexpected
23051          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23052          Emit both to give time for other tools to adapt.  */
23053       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23054       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23055
23056       if (text_section_used)
23057         add_ranges_by_labels (comp_unit_die (), text_section_label,
23058                               text_end_label, &range_list_added);
23059       if (flag_reorder_blocks_and_partition && cold_text_section_used)
23060         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23061                               cold_end_label, &range_list_added);
23062
23063       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23064         {
23065           dw_fde_ref fde = &fde_table[fde_idx];
23066
23067           if (fde->dw_fde_switched_sections)
23068             {
23069               if (!fde->in_std_section)
23070                 add_ranges_by_labels (comp_unit_die (),
23071                                       fde->dw_fde_hot_section_label,
23072                                       fde->dw_fde_hot_section_end_label,
23073                                       &range_list_added);
23074               if (!fde->cold_in_std_section)
23075                 add_ranges_by_labels (comp_unit_die (),
23076                                       fde->dw_fde_unlikely_section_label,
23077                                       fde->dw_fde_unlikely_section_end_label,
23078                                       &range_list_added);
23079             }
23080           else if (!fde->in_std_section)
23081             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23082                                   fde->dw_fde_end, &range_list_added);
23083         }
23084
23085       if (range_list_added)
23086         add_ranges (NULL);
23087     }
23088
23089   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23090     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23091                     debug_line_section_label);
23092
23093   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23094     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23095
23096   if (have_location_lists)
23097     optimize_location_lists (die);
23098
23099   /* Output all of the compilation units.  We put the main one last so that
23100      the offsets are available to output_pubnames.  */
23101   for (node = limbo_die_list; node; node = node->next)
23102     output_comp_unit (node->die, 0);
23103
23104   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23105   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23106     {
23107       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23108
23109       /* Don't output duplicate types.  */
23110       if (*slot != HTAB_EMPTY_ENTRY)
23111         continue;
23112
23113       /* Add a pointer to the line table for the main compilation unit
23114          so that the debugger can make sense of DW_AT_decl_file
23115          attributes.  */
23116       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23117         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23118                         debug_line_section_label);
23119
23120       output_comdat_type_unit (ctnode);
23121       *slot = ctnode;
23122     }
23123   htab_delete (comdat_type_table);
23124
23125   /* Output the main compilation unit if non-empty or if .debug_macinfo
23126      will be emitted.  */
23127   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23128
23129   /* Output the abbreviation table.  */
23130   switch_to_section (debug_abbrev_section);
23131   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23132   output_abbrev_section ();
23133
23134   /* Output location list section if necessary.  */
23135   if (have_location_lists)
23136     {
23137       /* Output the location lists info.  */
23138       switch_to_section (debug_loc_section);
23139       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23140                                    DEBUG_LOC_SECTION_LABEL, 0);
23141       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23142       output_location_lists (die);
23143     }
23144
23145   /* Output public names table if necessary.  */
23146   if (!VEC_empty (pubname_entry, pubname_table))
23147     {
23148       gcc_assert (info_section_emitted);
23149       switch_to_section (debug_pubnames_section);
23150       output_pubnames (pubname_table);
23151     }
23152
23153   /* Output public types table if necessary.  */
23154   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23155      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23156      simply won't look for the section.  */
23157   if (!VEC_empty (pubname_entry, pubtype_table))
23158     {
23159       bool empty = false;
23160       
23161       if (flag_eliminate_unused_debug_types)
23162         {
23163           /* The pubtypes table might be emptied by pruning unused items.  */
23164           unsigned i;
23165           pubname_ref p;
23166           empty = true;
23167           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23168             if (p->die->die_offset != 0)
23169               {
23170                 empty = false;
23171                 break;
23172               }
23173         }
23174       if (!empty)
23175         {
23176           gcc_assert (info_section_emitted);
23177           switch_to_section (debug_pubtypes_section);
23178           output_pubnames (pubtype_table);
23179         }
23180     }
23181
23182   /* Output direct and virtual call tables if necessary.  */
23183   if (!VEC_empty (dcall_entry, dcall_table))
23184     {
23185       switch_to_section (debug_dcall_section);
23186       output_dcall_table ();
23187     }
23188   if (!VEC_empty (vcall_entry, vcall_table))
23189     {
23190       switch_to_section (debug_vcall_section);
23191       output_vcall_table ();
23192     }
23193
23194   /* Output the address range information.  We only put functions in the arange
23195      table, so don't write it out if we don't have any.  */
23196   if (fde_table_in_use)
23197     {
23198       switch_to_section (debug_aranges_section);
23199       output_aranges ();
23200     }
23201
23202   /* Output ranges section if necessary.  */
23203   if (ranges_table_in_use)
23204     {
23205       switch_to_section (debug_ranges_section);
23206       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23207       output_ranges ();
23208     }
23209
23210   /* Output the source line correspondence table.  We must do this
23211      even if there is no line information.  Otherwise, on an empty
23212      translation unit, we will generate a present, but empty,
23213      .debug_info section.  IRIX 6.5 `nm' will then complain when
23214      examining the file.  This is done late so that any filenames
23215      used by the debug_info section are marked as 'used'.  */
23216   switch_to_section (debug_line_section);
23217   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23218   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23219     output_line_info ();
23220
23221   /* Have to end the macro section.  */
23222   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23223     {
23224       switch_to_section (debug_macinfo_section);
23225       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23226       if (!VEC_empty (macinfo_entry, macinfo_table))
23227         output_macinfo ();
23228       dw2_asm_output_data (1, 0, "End compilation unit");
23229     }
23230
23231   /* If we emitted any DW_FORM_strp form attribute, output the string
23232      table too.  */
23233   if (debug_str_hash)
23234     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23235 }
23236
23237 #include "gt-dwarf2out.h"