OSDN Git Service

Implement opaque-enum-specifiers for C++0x.
[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_store.reg == (unsigned) regno)
2556               offset -= cfa_store.offset;
2557             else
2558               {
2559                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2560                 offset -= cfa_temp.offset;
2561               }
2562           }
2563           break;
2564
2565           /* Rule 13 */
2566           /* Without an offset.  */
2567         case REG:
2568           {
2569             int regno = REGNO (XEXP (dest, 0));
2570
2571             if (cfa_store.reg == (unsigned) regno)
2572               offset = -cfa_store.offset;
2573             else
2574               {
2575                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2576                 offset = -cfa_temp.offset;
2577               }
2578           }
2579           break;
2580
2581           /* Rule 14 */
2582         case POST_INC:
2583           gcc_assert (cfa_temp.reg
2584                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2585           offset = -cfa_temp.offset;
2586           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2587           break;
2588
2589         default:
2590           gcc_unreachable ();
2591         }
2592
2593         /* Rule 17 */
2594         /* If the source operand of this MEM operation is not a
2595            register, basically the source is return address.  Here
2596            we only care how much stack grew and we don't save it.  */
2597       if (!REG_P (src))
2598         break;
2599
2600       if (REGNO (src) != STACK_POINTER_REGNUM
2601           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2602           && (unsigned) REGNO (src) == cfa.reg)
2603         {
2604           /* We're storing the current CFA reg into the stack.  */
2605
2606           if (cfa.offset == 0)
2607             {
2608               /* Rule 19 */
2609               /* If stack is aligned, putting CFA reg into stack means
2610                  we can no longer use reg + offset to represent CFA.
2611                  Here we use DW_CFA_def_cfa_expression instead.  The
2612                  result of this expression equals to the original CFA
2613                  value.  */
2614               if (fde
2615                   && fde->stack_realign
2616                   && cfa.indirect == 0
2617                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2618                 {
2619                   dw_cfa_location cfa_exp;
2620
2621                   gcc_assert (fde->drap_reg == cfa.reg);
2622
2623                   cfa_exp.indirect = 1;
2624                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2625                   cfa_exp.base_offset = offset;
2626                   cfa_exp.offset = 0;
2627
2628                   fde->drap_reg_saved = 1;
2629
2630                   def_cfa_1 (label, &cfa_exp);
2631                   break;
2632                 }
2633
2634               /* If the source register is exactly the CFA, assume
2635                  we're saving SP like any other register; this happens
2636                  on the ARM.  */
2637               def_cfa_1 (label, &cfa);
2638               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2639               break;
2640             }
2641           else
2642             {
2643               /* Otherwise, we'll need to look in the stack to
2644                  calculate the CFA.  */
2645               rtx x = XEXP (dest, 0);
2646
2647               if (!REG_P (x))
2648                 x = XEXP (x, 0);
2649               gcc_assert (REG_P (x));
2650
2651               cfa.reg = REGNO (x);
2652               cfa.base_offset = offset;
2653               cfa.indirect = 1;
2654               def_cfa_1 (label, &cfa);
2655               break;
2656             }
2657         }
2658
2659       def_cfa_1 (label, &cfa);
2660       {
2661         span = targetm.dwarf_register_span (src);
2662
2663         if (!span)
2664           queue_reg_save (label, src, NULL_RTX, offset);
2665         else
2666           {
2667             /* We have a PARALLEL describing where the contents of SRC
2668                live.  Queue register saves for each piece of the
2669                PARALLEL.  */
2670             int par_index;
2671             int limit;
2672             HOST_WIDE_INT span_offset = offset;
2673
2674             gcc_assert (GET_CODE (span) == PARALLEL);
2675
2676             limit = XVECLEN (span, 0);
2677             for (par_index = 0; par_index < limit; par_index++)
2678               {
2679                 rtx elem = XVECEXP (span, 0, par_index);
2680
2681                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2682                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2683               }
2684           }
2685       }
2686       break;
2687
2688     default:
2689       gcc_unreachable ();
2690     }
2691 }
2692
2693 /* Record call frame debugging information for INSN, which either
2694    sets SP or FP (adjusting how we calculate the frame address) or saves a
2695    register to the stack.  If INSN is NULL_RTX, initialize our state.
2696
2697    If AFTER_P is false, we're being called before the insn is emitted,
2698    otherwise after.  Call instructions get invoked twice.  */
2699
2700 void
2701 dwarf2out_frame_debug (rtx insn, bool after_p)
2702 {
2703   const char *label;
2704   rtx note, n;
2705   bool handled_one = false;
2706
2707   if (insn == NULL_RTX)
2708     {
2709       size_t i;
2710
2711       /* Flush any queued register saves.  */
2712       dwarf2out_flush_queued_reg_saves ();
2713
2714       /* Set up state for generating call frame debug info.  */
2715       lookup_cfa (&cfa);
2716       gcc_assert (cfa.reg
2717                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2718
2719       cfa.reg = STACK_POINTER_REGNUM;
2720       cfa_store = cfa;
2721       cfa_temp.reg = -1;
2722       cfa_temp.offset = 0;
2723
2724       for (i = 0; i < num_regs_saved_in_regs; i++)
2725         {
2726           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2727           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2728         }
2729       num_regs_saved_in_regs = 0;
2730
2731       if (barrier_args_size)
2732         {
2733           XDELETEVEC (barrier_args_size);
2734           barrier_args_size = NULL;
2735         }
2736       return;
2737     }
2738
2739   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2740     dwarf2out_flush_queued_reg_saves ();
2741
2742   if (!RTX_FRAME_RELATED_P (insn))
2743     {
2744       /* ??? This should be done unconditionally since stack adjustments
2745          matter if the stack pointer is not the CFA register anymore but
2746          is still used to save registers.  */
2747       if (!ACCUMULATE_OUTGOING_ARGS)
2748         dwarf2out_notice_stack_adjust (insn, after_p);
2749       return;
2750     }
2751
2752   label = dwarf2out_cfi_label (false);
2753   any_cfis_emitted = false;
2754
2755   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2756     switch (REG_NOTE_KIND (note))
2757       {
2758       case REG_FRAME_RELATED_EXPR:
2759         insn = XEXP (note, 0);
2760         goto found;
2761
2762       case REG_CFA_DEF_CFA:
2763         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2764         handled_one = true;
2765         break;
2766
2767       case REG_CFA_ADJUST_CFA:
2768         n = XEXP (note, 0);
2769         if (n == NULL)
2770           {
2771             n = PATTERN (insn);
2772             if (GET_CODE (n) == PARALLEL)
2773               n = XVECEXP (n, 0, 0);
2774           }
2775         dwarf2out_frame_debug_adjust_cfa (n, label);
2776         handled_one = true;
2777         break;
2778
2779       case REG_CFA_OFFSET:
2780         n = XEXP (note, 0);
2781         if (n == NULL)
2782           n = single_set (insn);
2783         dwarf2out_frame_debug_cfa_offset (n, label);
2784         handled_one = true;
2785         break;
2786
2787       case REG_CFA_REGISTER:
2788         n = XEXP (note, 0);
2789         if (n == NULL)
2790           {
2791             n = PATTERN (insn);
2792             if (GET_CODE (n) == PARALLEL)
2793               n = XVECEXP (n, 0, 0);
2794           }
2795         dwarf2out_frame_debug_cfa_register (n, label);
2796         handled_one = true;
2797         break;
2798
2799       case REG_CFA_EXPRESSION:
2800         n = XEXP (note, 0);
2801         if (n == NULL)
2802           n = single_set (insn);
2803         dwarf2out_frame_debug_cfa_expression (n, label);
2804         handled_one = true;
2805         break;
2806
2807       case REG_CFA_RESTORE:
2808         n = XEXP (note, 0);
2809         if (n == NULL)
2810           {
2811             n = PATTERN (insn);
2812             if (GET_CODE (n) == PARALLEL)
2813               n = XVECEXP (n, 0, 0);
2814             n = XEXP (n, 0);
2815           }
2816         dwarf2out_frame_debug_cfa_restore (n, label);
2817         handled_one = true;
2818         break;
2819
2820       case REG_CFA_SET_VDRAP:
2821         n = XEXP (note, 0);
2822         if (REG_P (n))
2823           {
2824             dw_fde_ref fde = current_fde ();
2825             if (fde)
2826               {
2827                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2828                 if (REG_P (n))
2829                   fde->vdrap_reg = REGNO (n);
2830               }
2831           }
2832         handled_one = true;
2833         break;
2834
2835       default:
2836         break;
2837       }
2838   if (handled_one)
2839     {
2840       if (any_cfis_emitted)
2841         dwarf2out_flush_queued_reg_saves ();
2842       return;
2843     }
2844
2845   insn = PATTERN (insn);
2846  found:
2847   dwarf2out_frame_debug_expr (insn, label);
2848
2849   /* Check again.  A parallel can save and update the same register.
2850      We could probably check just once, here, but this is safer than
2851      removing the check above.  */
2852   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2853     dwarf2out_flush_queued_reg_saves ();
2854 }
2855
2856 /* Determine if we need to save and restore CFI information around this
2857    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2858    we do need to save/restore, then emit the save now, and insert a
2859    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2860
2861 void
2862 dwarf2out_cfi_begin_epilogue (rtx insn)
2863 {
2864   bool saw_frp = false;
2865   rtx i;
2866
2867   /* Scan forward to the return insn, noticing if there are possible
2868      frame related insns.  */
2869   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2870     {
2871       if (!INSN_P (i))
2872         continue;
2873
2874       /* Look for both regular and sibcalls to end the block.  */
2875       if (returnjump_p (i))
2876         break;
2877       if (CALL_P (i) && SIBLING_CALL_P (i))
2878         break;
2879
2880       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2881         {
2882           int idx;
2883           rtx seq = PATTERN (i);
2884
2885           if (returnjump_p (XVECEXP (seq, 0, 0)))
2886             break;
2887           if (CALL_P (XVECEXP (seq, 0, 0))
2888               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2889             break;
2890
2891           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2892             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2893               saw_frp = true;
2894         }
2895
2896       if (RTX_FRAME_RELATED_P (i))
2897         saw_frp = true;
2898     }
2899
2900   /* If the port doesn't emit epilogue unwind info, we don't need a
2901      save/restore pair.  */
2902   if (!saw_frp)
2903     return;
2904
2905   /* Otherwise, search forward to see if the return insn was the last
2906      basic block of the function.  If so, we don't need save/restore.  */
2907   gcc_assert (i != NULL);
2908   i = next_real_insn (i);
2909   if (i == NULL)
2910     return;
2911
2912   /* Insert the restore before that next real insn in the stream, and before
2913      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2914      properly nested.  This should be after any label or alignment.  This
2915      will be pushed into the CFI stream by the function below.  */
2916   while (1)
2917     {
2918       rtx p = PREV_INSN (i);
2919       if (!NOTE_P (p))
2920         break;
2921       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2922         break;
2923       i = p;
2924     }
2925   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2926
2927   emit_cfa_remember = true;
2928
2929   /* And emulate the state save.  */
2930   gcc_assert (!cfa_remember.in_use);
2931   cfa_remember = cfa;
2932   cfa_remember.in_use = 1;
2933 }
2934
2935 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2936    required.  */
2937
2938 void
2939 dwarf2out_frame_debug_restore_state (void)
2940 {
2941   dw_cfi_ref cfi = new_cfi ();
2942   const char *label = dwarf2out_cfi_label (false);
2943
2944   cfi->dw_cfi_opc = DW_CFA_restore_state;
2945   add_fde_cfi (label, cfi);
2946
2947   gcc_assert (cfa_remember.in_use);
2948   cfa = cfa_remember;
2949   cfa_remember.in_use = 0;
2950 }
2951
2952 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2953 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2954  (enum dwarf_call_frame_info cfi);
2955
2956 static enum dw_cfi_oprnd_type
2957 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2958 {
2959   switch (cfi)
2960     {
2961     case DW_CFA_nop:
2962     case DW_CFA_GNU_window_save:
2963     case DW_CFA_remember_state:
2964     case DW_CFA_restore_state:
2965       return dw_cfi_oprnd_unused;
2966
2967     case DW_CFA_set_loc:
2968     case DW_CFA_advance_loc1:
2969     case DW_CFA_advance_loc2:
2970     case DW_CFA_advance_loc4:
2971     case DW_CFA_MIPS_advance_loc8:
2972       return dw_cfi_oprnd_addr;
2973
2974     case DW_CFA_offset:
2975     case DW_CFA_offset_extended:
2976     case DW_CFA_def_cfa:
2977     case DW_CFA_offset_extended_sf:
2978     case DW_CFA_def_cfa_sf:
2979     case DW_CFA_restore:
2980     case DW_CFA_restore_extended:
2981     case DW_CFA_undefined:
2982     case DW_CFA_same_value:
2983     case DW_CFA_def_cfa_register:
2984     case DW_CFA_register:
2985     case DW_CFA_expression:
2986       return dw_cfi_oprnd_reg_num;
2987
2988     case DW_CFA_def_cfa_offset:
2989     case DW_CFA_GNU_args_size:
2990     case DW_CFA_def_cfa_offset_sf:
2991       return dw_cfi_oprnd_offset;
2992
2993     case DW_CFA_def_cfa_expression:
2994       return dw_cfi_oprnd_loc;
2995
2996     default:
2997       gcc_unreachable ();
2998     }
2999 }
3000
3001 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3002 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3003  (enum dwarf_call_frame_info cfi);
3004
3005 static enum dw_cfi_oprnd_type
3006 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3007 {
3008   switch (cfi)
3009     {
3010     case DW_CFA_def_cfa:
3011     case DW_CFA_def_cfa_sf:
3012     case DW_CFA_offset:
3013     case DW_CFA_offset_extended_sf:
3014     case DW_CFA_offset_extended:
3015       return dw_cfi_oprnd_offset;
3016
3017     case DW_CFA_register:
3018       return dw_cfi_oprnd_reg_num;
3019
3020     case DW_CFA_expression:
3021       return dw_cfi_oprnd_loc;
3022
3023     default:
3024       return dw_cfi_oprnd_unused;
3025     }
3026 }
3027
3028 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3029    switch to the data section instead, and write out a synthetic start label
3030    for collect2 the first time around.  */
3031
3032 static void
3033 switch_to_eh_frame_section (bool back)
3034 {
3035   tree label;
3036
3037 #ifdef EH_FRAME_SECTION_NAME
3038   if (eh_frame_section == 0)
3039     {
3040       int flags;
3041
3042       if (EH_TABLES_CAN_BE_READ_ONLY)
3043         {
3044           int fde_encoding;
3045           int per_encoding;
3046           int lsda_encoding;
3047
3048           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3049                                                        /*global=*/0);
3050           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3051                                                        /*global=*/1);
3052           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3053                                                         /*global=*/0);
3054           flags = ((! flag_pic
3055                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3056                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3057                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3058                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3059                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3060                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3061                    ? 0 : SECTION_WRITE);
3062         }
3063       else
3064         flags = SECTION_WRITE;
3065       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3066     }
3067 #endif /* EH_FRAME_SECTION_NAME */
3068
3069   if (eh_frame_section)
3070     switch_to_section (eh_frame_section);
3071   else
3072     {
3073       /* We have no special eh_frame section.  Put the information in
3074          the data section and emit special labels to guide collect2.  */
3075       switch_to_section (data_section);
3076
3077       if (!back)
3078         {
3079           label = get_file_function_name ("F");
3080           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3081           targetm.asm_out.globalize_label (asm_out_file,
3082                                            IDENTIFIER_POINTER (label));
3083           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3084         }
3085     }
3086 }
3087
3088 /* Switch [BACK] to the eh or debug frame table section, depending on
3089    FOR_EH.  */
3090
3091 static void
3092 switch_to_frame_table_section (int for_eh, bool back)
3093 {
3094   if (for_eh)
3095     switch_to_eh_frame_section (back);
3096   else
3097     {
3098       if (!debug_frame_section)
3099         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3100                                            SECTION_DEBUG, NULL);
3101       switch_to_section (debug_frame_section);
3102     }
3103 }
3104
3105 /* Output a Call Frame Information opcode and its operand(s).  */
3106
3107 static void
3108 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3109 {
3110   unsigned long r;
3111   HOST_WIDE_INT off;
3112
3113   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3114     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3115                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3116                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3117                          ((unsigned HOST_WIDE_INT)
3118                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3119   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3120     {
3121       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3122       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3123                            "DW_CFA_offset, column %#lx", r);
3124       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3125       dw2_asm_output_data_uleb128 (off, NULL);
3126     }
3127   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3128     {
3129       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3130       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3131                            "DW_CFA_restore, column %#lx", r);
3132     }
3133   else
3134     {
3135       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3136                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3137
3138       switch (cfi->dw_cfi_opc)
3139         {
3140         case DW_CFA_set_loc:
3141           if (for_eh)
3142             dw2_asm_output_encoded_addr_rtx (
3143                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3144                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3145                 false, NULL);
3146           else
3147             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3148                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3149           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3150           break;
3151
3152         case DW_CFA_advance_loc1:
3153           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3154                                 fde->dw_fde_current_label, NULL);
3155           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3156           break;
3157
3158         case DW_CFA_advance_loc2:
3159           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3160                                 fde->dw_fde_current_label, NULL);
3161           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3162           break;
3163
3164         case DW_CFA_advance_loc4:
3165           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3166                                 fde->dw_fde_current_label, NULL);
3167           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3168           break;
3169
3170         case DW_CFA_MIPS_advance_loc8:
3171           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3172                                 fde->dw_fde_current_label, NULL);
3173           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3174           break;
3175
3176         case DW_CFA_offset_extended:
3177           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3178           dw2_asm_output_data_uleb128 (r, NULL);
3179           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3180           dw2_asm_output_data_uleb128 (off, NULL);
3181           break;
3182
3183         case DW_CFA_def_cfa:
3184           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3185           dw2_asm_output_data_uleb128 (r, NULL);
3186           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3187           break;
3188
3189         case DW_CFA_offset_extended_sf:
3190           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3191           dw2_asm_output_data_uleb128 (r, NULL);
3192           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3193           dw2_asm_output_data_sleb128 (off, NULL);
3194           break;
3195
3196         case DW_CFA_def_cfa_sf:
3197           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3198           dw2_asm_output_data_uleb128 (r, NULL);
3199           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3200           dw2_asm_output_data_sleb128 (off, NULL);
3201           break;
3202
3203         case DW_CFA_restore_extended:
3204         case DW_CFA_undefined:
3205         case DW_CFA_same_value:
3206         case DW_CFA_def_cfa_register:
3207           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3208           dw2_asm_output_data_uleb128 (r, NULL);
3209           break;
3210
3211         case DW_CFA_register:
3212           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3213           dw2_asm_output_data_uleb128 (r, NULL);
3214           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3215           dw2_asm_output_data_uleb128 (r, NULL);
3216           break;
3217
3218         case DW_CFA_def_cfa_offset:
3219         case DW_CFA_GNU_args_size:
3220           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3221           break;
3222
3223         case DW_CFA_def_cfa_offset_sf:
3224           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3225           dw2_asm_output_data_sleb128 (off, NULL);
3226           break;
3227
3228         case DW_CFA_GNU_window_save:
3229           break;
3230
3231         case DW_CFA_def_cfa_expression:
3232         case DW_CFA_expression:
3233           output_cfa_loc (cfi);
3234           break;
3235
3236         case DW_CFA_GNU_negative_offset_extended:
3237           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3238           gcc_unreachable ();
3239
3240         default:
3241           break;
3242         }
3243     }
3244 }
3245
3246 /* Similar, but do it via assembler directives instead.  */
3247
3248 static void
3249 output_cfi_directive (dw_cfi_ref cfi)
3250 {
3251   unsigned long r, r2;
3252
3253   switch (cfi->dw_cfi_opc)
3254     {
3255     case DW_CFA_advance_loc:
3256     case DW_CFA_advance_loc1:
3257     case DW_CFA_advance_loc2:
3258     case DW_CFA_advance_loc4:
3259     case DW_CFA_MIPS_advance_loc8:
3260     case DW_CFA_set_loc:
3261       /* Should only be created by add_fde_cfi in a code path not
3262          followed when emitting via directives.  The assembler is
3263          going to take care of this for us.  */
3264       gcc_unreachable ();
3265
3266     case DW_CFA_offset:
3267     case DW_CFA_offset_extended:
3268     case DW_CFA_offset_extended_sf:
3269       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3270       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3271                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3272       break;
3273
3274     case DW_CFA_restore:
3275     case DW_CFA_restore_extended:
3276       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3277       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3278       break;
3279
3280     case DW_CFA_undefined:
3281       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3282       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3283       break;
3284
3285     case DW_CFA_same_value:
3286       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3287       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3288       break;
3289
3290     case DW_CFA_def_cfa:
3291     case DW_CFA_def_cfa_sf:
3292       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3293       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3294                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3295       break;
3296
3297     case DW_CFA_def_cfa_register:
3298       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3299       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3300       break;
3301
3302     case DW_CFA_register:
3303       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3304       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3305       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3306       break;
3307
3308     case DW_CFA_def_cfa_offset:
3309     case DW_CFA_def_cfa_offset_sf:
3310       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3311                HOST_WIDE_INT_PRINT_DEC"\n",
3312                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3313       break;
3314
3315     case DW_CFA_remember_state:
3316       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3317       break;
3318     case DW_CFA_restore_state:
3319       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3320       break;
3321
3322     case DW_CFA_GNU_args_size:
3323       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3324       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3325       if (flag_debug_asm)
3326         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3327                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3328       fputc ('\n', asm_out_file);
3329       break;
3330
3331     case DW_CFA_GNU_window_save:
3332       fprintf (asm_out_file, "\t.cfi_window_save\n");
3333       break;
3334
3335     case DW_CFA_def_cfa_expression:
3336     case DW_CFA_expression:
3337       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3338       output_cfa_loc_raw (cfi);
3339       fputc ('\n', asm_out_file);
3340       break;
3341
3342     default:
3343       gcc_unreachable ();
3344     }
3345 }
3346
3347 DEF_VEC_P (dw_cfi_ref);
3348 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3349
3350 /* Output CFIs to bring current FDE to the same state as after executing
3351    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3352    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3353    other arguments to pass to output_cfi.  */
3354
3355 static void
3356 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3357 {
3358   struct dw_cfi_struct cfi_buf;
3359   dw_cfi_ref cfi2;
3360   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3361   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3362   unsigned int len, idx;
3363
3364   for (;; cfi = cfi->dw_cfi_next)
3365     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3366       {
3367       case DW_CFA_advance_loc:
3368       case DW_CFA_advance_loc1:
3369       case DW_CFA_advance_loc2:
3370       case DW_CFA_advance_loc4:
3371       case DW_CFA_MIPS_advance_loc8:
3372       case DW_CFA_set_loc:
3373         /* All advances should be ignored.  */
3374         break;
3375       case DW_CFA_remember_state:
3376         {
3377           dw_cfi_ref args_size = cfi_args_size;
3378
3379           /* Skip everything between .cfi_remember_state and
3380              .cfi_restore_state.  */
3381           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3382             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3383               break;
3384             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3385               args_size = cfi2;
3386             else
3387               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3388
3389           if (cfi2 == NULL)
3390             goto flush_all;
3391           else
3392             {
3393               cfi = cfi2;
3394               cfi_args_size = args_size;
3395             }
3396           break;
3397         }
3398       case DW_CFA_GNU_args_size:
3399         cfi_args_size = cfi;
3400         break;
3401       case DW_CFA_GNU_window_save:
3402         goto flush_all;
3403       case DW_CFA_offset:
3404       case DW_CFA_offset_extended:
3405       case DW_CFA_offset_extended_sf:
3406       case DW_CFA_restore:
3407       case DW_CFA_restore_extended:
3408       case DW_CFA_undefined:
3409       case DW_CFA_same_value:
3410       case DW_CFA_register:
3411       case DW_CFA_val_offset:
3412       case DW_CFA_val_offset_sf:
3413       case DW_CFA_expression:
3414       case DW_CFA_val_expression:
3415       case DW_CFA_GNU_negative_offset_extended:
3416         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3417           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3418                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3419         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3420         break;
3421       case DW_CFA_def_cfa:
3422       case DW_CFA_def_cfa_sf:
3423       case DW_CFA_def_cfa_expression:
3424         cfi_cfa = cfi;
3425         cfi_cfa_offset = cfi;
3426         break;
3427       case DW_CFA_def_cfa_register:
3428         cfi_cfa = cfi;
3429         break;
3430       case DW_CFA_def_cfa_offset:
3431       case DW_CFA_def_cfa_offset_sf:
3432         cfi_cfa_offset = cfi;
3433         break;
3434       case DW_CFA_nop:
3435         gcc_assert (cfi == NULL);
3436       flush_all:
3437         len = VEC_length (dw_cfi_ref, regs);
3438         for (idx = 0; idx < len; idx++)
3439           {
3440             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3441             if (cfi2 != NULL
3442                 && cfi2->dw_cfi_opc != DW_CFA_restore
3443                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3444               {
3445                 if (do_cfi_asm)
3446                   output_cfi_directive (cfi2);
3447                 else
3448                   output_cfi (cfi2, fde, for_eh);
3449               }
3450           }
3451         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3452           {
3453             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3454             cfi_buf = *cfi_cfa;
3455             switch (cfi_cfa_offset->dw_cfi_opc)
3456               {
3457               case DW_CFA_def_cfa_offset:
3458                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3459                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3460                 break;
3461               case DW_CFA_def_cfa_offset_sf:
3462                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3463                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3464                 break;
3465               case DW_CFA_def_cfa:
3466               case DW_CFA_def_cfa_sf:
3467                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3468                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3469                 break;
3470               default:
3471                 gcc_unreachable ();
3472               }
3473             cfi_cfa = &cfi_buf;
3474           }
3475         else if (cfi_cfa_offset)
3476           cfi_cfa = cfi_cfa_offset;
3477         if (cfi_cfa)
3478           {
3479             if (do_cfi_asm)
3480               output_cfi_directive (cfi_cfa);
3481             else
3482               output_cfi (cfi_cfa, fde, for_eh);
3483           }
3484         cfi_cfa = NULL;
3485         cfi_cfa_offset = NULL;
3486         if (cfi_args_size
3487             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3488           {
3489             if (do_cfi_asm)
3490               output_cfi_directive (cfi_args_size);
3491             else
3492               output_cfi (cfi_args_size, fde, for_eh);
3493           }
3494         cfi_args_size = NULL;
3495         if (cfi == NULL)
3496           {
3497             VEC_free (dw_cfi_ref, heap, regs);
3498             return;
3499           }
3500         else if (do_cfi_asm)
3501           output_cfi_directive (cfi);
3502         else
3503           output_cfi (cfi, fde, for_eh);
3504         break;
3505       default:
3506         gcc_unreachable ();
3507     }
3508 }
3509
3510 /* Output one FDE.  */
3511
3512 static void
3513 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3514             char *section_start_label, int fde_encoding, char *augmentation,
3515             bool any_lsda_needed, int lsda_encoding)
3516 {
3517   const char *begin, *end;
3518   static unsigned int j;
3519   char l1[20], l2[20];
3520   dw_cfi_ref cfi;
3521
3522   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3523                                      /* empty */ 0);
3524   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3525                                   for_eh + j);
3526   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3527   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3528   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3529     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3530                          " indicating 64-bit DWARF extension");
3531   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3532                         "FDE Length");
3533   ASM_OUTPUT_LABEL (asm_out_file, l1);
3534
3535   if (for_eh)
3536     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3537   else
3538     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3539                            debug_frame_section, "FDE CIE offset");
3540
3541   if (!fde->dw_fde_switched_sections)
3542     {
3543       begin = fde->dw_fde_begin;
3544       end = fde->dw_fde_end;
3545     }
3546   else
3547     {
3548       /* For the first section, prefer dw_fde_begin over
3549          dw_fde_{hot,cold}_section_label, as the latter
3550          might be separated from the real start of the
3551          function by alignment padding.  */
3552       if (!second)
3553         begin = fde->dw_fde_begin;
3554       else if (fde->dw_fde_switched_cold_to_hot)
3555         begin = fde->dw_fde_hot_section_label;
3556       else
3557         begin = fde->dw_fde_unlikely_section_label;
3558       if (second ^ fde->dw_fde_switched_cold_to_hot)
3559         end = fde->dw_fde_unlikely_section_end_label;
3560       else
3561         end = fde->dw_fde_hot_section_end_label;
3562     }
3563
3564   if (for_eh)
3565     {
3566       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3567       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3568       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3569                                        "FDE initial location");
3570       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3571                             end, begin, "FDE address range");
3572     }
3573   else
3574     {
3575       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3576       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3577     }
3578
3579   if (augmentation[0])
3580     {
3581       if (any_lsda_needed)
3582         {
3583           int size = size_of_encoded_value (lsda_encoding);
3584
3585           if (lsda_encoding == DW_EH_PE_aligned)
3586             {
3587               int offset = (  4         /* Length */
3588                             + 4         /* CIE offset */
3589                             + 2 * size_of_encoded_value (fde_encoding)
3590                             + 1         /* Augmentation size */ );
3591               int pad = -offset & (PTR_SIZE - 1);
3592
3593               size += pad;
3594               gcc_assert (size_of_uleb128 (size) == 1);
3595             }
3596
3597           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3598
3599           if (fde->uses_eh_lsda)
3600             {
3601               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3602                                            fde->funcdef_number);
3603               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3604                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3605                                                false,
3606                                                "Language Specific Data Area");
3607             }
3608           else
3609             {
3610               if (lsda_encoding == DW_EH_PE_aligned)
3611                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3612               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3613                                    "Language Specific Data Area (none)");
3614             }
3615         }
3616       else
3617         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3618     }
3619
3620   /* Loop through the Call Frame Instructions associated with
3621      this FDE.  */
3622   fde->dw_fde_current_label = begin;
3623   if (!fde->dw_fde_switched_sections)
3624     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3625       output_cfi (cfi, fde, for_eh);
3626   else if (!second)
3627     {
3628       if (fde->dw_fde_switch_cfi)
3629         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3630           {
3631             output_cfi (cfi, fde, for_eh);
3632             if (cfi == fde->dw_fde_switch_cfi)
3633               break;
3634           }
3635     }
3636   else
3637     {
3638       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3639
3640       if (fde->dw_fde_switch_cfi)
3641         {
3642           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3643           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3644           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3645           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3646         }
3647       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3648         output_cfi (cfi, fde, for_eh);
3649     }
3650
3651   /* If we are to emit a ref/link from function bodies to their frame tables,
3652      do it now.  This is typically performed to make sure that tables
3653      associated with functions are dragged with them and not discarded in
3654      garbage collecting links. We need to do this on a per function basis to
3655      cope with -ffunction-sections.  */
3656
3657 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3658   /* Switch to the function section, emit the ref to the tables, and
3659      switch *back* into the table section.  */
3660   switch_to_section (function_section (fde->decl));
3661   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3662   switch_to_frame_table_section (for_eh, true);
3663 #endif
3664
3665   /* Pad the FDE out to an address sized boundary.  */
3666   ASM_OUTPUT_ALIGN (asm_out_file,
3667                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3668   ASM_OUTPUT_LABEL (asm_out_file, l2);
3669
3670   j += 2;
3671 }
3672
3673 /* Return true if frame description entry FDE is needed for EH.  */
3674
3675 static bool
3676 fde_needed_for_eh_p (dw_fde_ref fde)
3677 {
3678   if (flag_asynchronous_unwind_tables)
3679     return true;
3680
3681   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3682     return true;
3683
3684   if (fde->uses_eh_lsda)
3685     return true;
3686
3687   /* If exceptions are enabled, we have collected nothrow info.  */
3688   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3689     return false;
3690
3691   return true;
3692 }
3693
3694 /* Output the call frame information used to record information
3695    that relates to calculating the frame pointer, and records the
3696    location of saved registers.  */
3697
3698 static void
3699 output_call_frame_info (int for_eh)
3700 {
3701   unsigned int i;
3702   dw_fde_ref fde;
3703   dw_cfi_ref cfi;
3704   char l1[20], l2[20], section_start_label[20];
3705   bool any_lsda_needed = false;
3706   char augmentation[6];
3707   int augmentation_size;
3708   int fde_encoding = DW_EH_PE_absptr;
3709   int per_encoding = DW_EH_PE_absptr;
3710   int lsda_encoding = DW_EH_PE_absptr;
3711   int return_reg;
3712   rtx personality = NULL;
3713   int dw_cie_version;
3714
3715   /* Don't emit a CIE if there won't be any FDEs.  */
3716   if (fde_table_in_use == 0)
3717     return;
3718
3719   /* Nothing to do if the assembler's doing it all.  */
3720   if (dwarf2out_do_cfi_asm ())
3721     return;
3722
3723   /* If we don't have any functions we'll want to unwind out of, don't emit
3724      any EH unwind information.  If we make FDEs linkonce, we may have to
3725      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3726      want to avoid having an FDE kept around when the function it refers to
3727      is discarded.  Example where this matters: a primary function template
3728      in C++ requires EH information, an explicit specialization doesn't.  */
3729   if (for_eh)
3730     {
3731       bool any_eh_needed = false;
3732
3733       for (i = 0; i < fde_table_in_use; i++)
3734         if (fde_table[i].uses_eh_lsda)
3735           any_eh_needed = any_lsda_needed = true;
3736         else if (fde_needed_for_eh_p (&fde_table[i]))
3737           any_eh_needed = true;
3738         else if (TARGET_USES_WEAK_UNWIND_INFO)
3739           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3740                                              1, 1);
3741
3742       if (!any_eh_needed)
3743         return;
3744     }
3745
3746   /* We're going to be generating comments, so turn on app.  */
3747   if (flag_debug_asm)
3748     app_enable ();
3749
3750   /* Switch to the proper frame section, first time.  */
3751   switch_to_frame_table_section (for_eh, false);
3752
3753   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3754   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3755
3756   /* Output the CIE.  */
3757   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3758   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3759   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3760     dw2_asm_output_data (4, 0xffffffff,
3761       "Initial length escape value indicating 64-bit DWARF extension");
3762   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3763                         "Length of Common Information Entry");
3764   ASM_OUTPUT_LABEL (asm_out_file, l1);
3765
3766   /* Now that the CIE pointer is PC-relative for EH,
3767      use 0 to identify the CIE.  */
3768   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3769                        (for_eh ? 0 : DWARF_CIE_ID),
3770                        "CIE Identifier Tag");
3771
3772   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3773      use CIE version 1, unless that would produce incorrect results
3774      due to overflowing the return register column.  */
3775   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3776   dw_cie_version = 1;
3777   if (return_reg >= 256 || dwarf_version > 2)
3778     dw_cie_version = 3;
3779   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3780
3781   augmentation[0] = 0;
3782   augmentation_size = 0;
3783
3784   personality = current_unit_personality;
3785   if (for_eh)
3786     {
3787       char *p;
3788
3789       /* Augmentation:
3790          z      Indicates that a uleb128 is present to size the
3791                 augmentation section.
3792          L      Indicates the encoding (and thus presence) of
3793                 an LSDA pointer in the FDE augmentation.
3794          R      Indicates a non-default pointer encoding for
3795                 FDE code pointers.
3796          P      Indicates the presence of an encoding + language
3797                 personality routine in the CIE augmentation.  */
3798
3799       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3800       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3801       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3802
3803       p = augmentation + 1;
3804       if (personality)
3805         {
3806           *p++ = 'P';
3807           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3808           assemble_external_libcall (personality);
3809         }
3810       if (any_lsda_needed)
3811         {
3812           *p++ = 'L';
3813           augmentation_size += 1;
3814         }
3815       if (fde_encoding != DW_EH_PE_absptr)
3816         {
3817           *p++ = 'R';
3818           augmentation_size += 1;
3819         }
3820       if (p > augmentation + 1)
3821         {
3822           augmentation[0] = 'z';
3823           *p = '\0';
3824         }
3825
3826       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3827       if (personality && per_encoding == DW_EH_PE_aligned)
3828         {
3829           int offset = (  4             /* Length */
3830                         + 4             /* CIE Id */
3831                         + 1             /* CIE version */
3832                         + strlen (augmentation) + 1     /* Augmentation */
3833                         + size_of_uleb128 (1)           /* Code alignment */
3834                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3835                         + 1             /* RA column */
3836                         + 1             /* Augmentation size */
3837                         + 1             /* Personality encoding */ );
3838           int pad = -offset & (PTR_SIZE - 1);
3839
3840           augmentation_size += pad;
3841
3842           /* Augmentations should be small, so there's scarce need to
3843              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3844           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3845         }
3846     }
3847
3848   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3849   if (dw_cie_version >= 4)
3850     {
3851       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3852       dw2_asm_output_data (1, 0, "CIE Segment Size");
3853     }
3854   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3855   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3856                                "CIE Data Alignment Factor");
3857
3858   if (dw_cie_version == 1)
3859     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3860   else
3861     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3862
3863   if (augmentation[0])
3864     {
3865       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3866       if (personality)
3867         {
3868           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3869                                eh_data_format_name (per_encoding));
3870           dw2_asm_output_encoded_addr_rtx (per_encoding,
3871                                            personality,
3872                                            true, NULL);
3873         }
3874
3875       if (any_lsda_needed)
3876         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3877                              eh_data_format_name (lsda_encoding));
3878
3879       if (fde_encoding != DW_EH_PE_absptr)
3880         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3881                              eh_data_format_name (fde_encoding));
3882     }
3883
3884   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3885     output_cfi (cfi, NULL, for_eh);
3886
3887   /* Pad the CIE out to an address sized boundary.  */
3888   ASM_OUTPUT_ALIGN (asm_out_file,
3889                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3890   ASM_OUTPUT_LABEL (asm_out_file, l2);
3891
3892   /* Loop through all of the FDE's.  */
3893   for (i = 0; i < fde_table_in_use; i++)
3894     {
3895       unsigned int k;
3896       fde = &fde_table[i];
3897
3898       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3899       if (for_eh && !fde_needed_for_eh_p (fde))
3900         continue;
3901
3902       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3903         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3904                     augmentation, any_lsda_needed, lsda_encoding);
3905     }
3906
3907   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3908     dw2_asm_output_data (4, 0, "End of Table");
3909 #ifdef MIPS_DEBUGGING_INFO
3910   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3911      get a value of 0.  Putting .align 0 after the label fixes it.  */
3912   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3913 #endif
3914
3915   /* Turn off app to make assembly quicker.  */
3916   if (flag_debug_asm)
3917     app_disable ();
3918 }
3919
3920 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3921
3922 static void
3923 dwarf2out_do_cfi_startproc (bool second)
3924 {
3925   int enc;
3926   rtx ref;
3927   rtx personality = get_personality_function (current_function_decl);
3928
3929   fprintf (asm_out_file, "\t.cfi_startproc\n");
3930
3931   if (personality)
3932     {
3933       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3934       ref = personality;
3935
3936       /* ??? The GAS support isn't entirely consistent.  We have to
3937          handle indirect support ourselves, but PC-relative is done
3938          in the assembler.  Further, the assembler can't handle any
3939          of the weirder relocation types.  */
3940       if (enc & DW_EH_PE_indirect)
3941         ref = dw2_force_const_mem (ref, true);
3942
3943       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3944       output_addr_const (asm_out_file, ref);
3945       fputc ('\n', asm_out_file);
3946     }
3947
3948   if (crtl->uses_eh_lsda)
3949     {
3950       char lab[20];
3951
3952       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3953       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3954                                    current_function_funcdef_no);
3955       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3956       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3957
3958       if (enc & DW_EH_PE_indirect)
3959         ref = dw2_force_const_mem (ref, true);
3960
3961       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3962       output_addr_const (asm_out_file, ref);
3963       fputc ('\n', asm_out_file);
3964     }
3965 }
3966
3967 /* Output a marker (i.e. a label) for the beginning of a function, before
3968    the prologue.  */
3969
3970 void
3971 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3972                           const char *file ATTRIBUTE_UNUSED)
3973 {
3974   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3975   char * dup_label;
3976   dw_fde_ref fde;
3977   section *fnsec;
3978   bool do_frame;
3979
3980   current_function_func_begin_label = NULL;
3981
3982   do_frame = dwarf2out_do_frame ();
3983
3984   /* ??? current_function_func_begin_label is also used by except.c for
3985      call-site information.  We must emit this label if it might be used.  */
3986   if (!do_frame
3987       && (!flag_exceptions
3988           || targetm.except_unwind_info () != UI_TARGET))
3989     return;
3990
3991   fnsec = function_section (current_function_decl);
3992   switch_to_section (fnsec);
3993   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3994                                current_function_funcdef_no);
3995   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3996                           current_function_funcdef_no);
3997   dup_label = xstrdup (label);
3998   current_function_func_begin_label = dup_label;
3999
4000   /* We can elide the fde allocation if we're not emitting debug info.  */
4001   if (!do_frame)
4002     return;
4003
4004   /* Expand the fde table if necessary.  */
4005   if (fde_table_in_use == fde_table_allocated)
4006     {
4007       fde_table_allocated += FDE_TABLE_INCREMENT;
4008       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4009       memset (fde_table + fde_table_in_use, 0,
4010               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4011     }
4012
4013   /* Record the FDE associated with this function.  */
4014   current_funcdef_fde = fde_table_in_use;
4015
4016   /* Add the new FDE at the end of the fde_table.  */
4017   fde = &fde_table[fde_table_in_use++];
4018   fde->decl = current_function_decl;
4019   fde->dw_fde_begin = dup_label;
4020   fde->dw_fde_current_label = dup_label;
4021   fde->dw_fde_hot_section_label = NULL;
4022   fde->dw_fde_hot_section_end_label = NULL;
4023   fde->dw_fde_unlikely_section_label = NULL;
4024   fde->dw_fde_unlikely_section_end_label = NULL;
4025   fde->dw_fde_switched_sections = 0;
4026   fde->dw_fde_switched_cold_to_hot = 0;
4027   fde->dw_fde_end = NULL;
4028   fde->dw_fde_vms_end_prologue = NULL;
4029   fde->dw_fde_vms_begin_epilogue = NULL;
4030   fde->dw_fde_cfi = NULL;
4031   fde->dw_fde_switch_cfi = NULL;
4032   fde->funcdef_number = current_function_funcdef_no;
4033   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4034   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4035   fde->nothrow = crtl->nothrow;
4036   fde->drap_reg = INVALID_REGNUM;
4037   fde->vdrap_reg = INVALID_REGNUM;
4038   if (flag_reorder_blocks_and_partition)
4039     {
4040       section *unlikelysec;
4041       if (first_function_block_is_cold)
4042         fde->in_std_section = 1;
4043       else
4044         fde->in_std_section
4045           = (fnsec == text_section
4046              || (cold_text_section && fnsec == cold_text_section));
4047       unlikelysec = unlikely_text_section ();
4048       fde->cold_in_std_section
4049         = (unlikelysec == text_section
4050            || (cold_text_section && unlikelysec == cold_text_section));
4051     }
4052   else
4053     {
4054       fde->in_std_section
4055         = (fnsec == text_section
4056            || (cold_text_section && fnsec == cold_text_section));
4057       fde->cold_in_std_section = 0;
4058     }
4059
4060   args_size = old_args_size = 0;
4061
4062   /* We only want to output line number information for the genuine dwarf2
4063      prologue case, not the eh frame case.  */
4064 #ifdef DWARF2_DEBUGGING_INFO
4065   if (file)
4066     dwarf2out_source_line (line, file, 0, true);
4067 #endif
4068
4069   if (dwarf2out_do_cfi_asm ())
4070     dwarf2out_do_cfi_startproc (false);
4071   else
4072     {
4073       rtx personality = get_personality_function (current_function_decl);
4074       if (!current_unit_personality)
4075         current_unit_personality = personality;
4076
4077       /* We cannot keep a current personality per function as without CFI
4078          asm, at the point where we emit the CFI data, there is no current
4079          function anymore.  */
4080       if (personality && current_unit_personality != personality)
4081         sorry ("multiple EH personalities are supported only with assemblers "
4082                "supporting .cfi_personality directive");
4083     }
4084 }
4085
4086 /* Output a marker (i.e. a label) for the end of the generated code
4087    for a function prologue.  This gets called *after* the prologue code has
4088    been generated.  */
4089
4090 void
4091 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4092                         const char *file ATTRIBUTE_UNUSED)
4093 {
4094   dw_fde_ref fde;
4095   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4096
4097   /* Output a label to mark the endpoint of the code generated for this
4098      function.  */
4099   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4100                                current_function_funcdef_no);
4101   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4102                           current_function_funcdef_no);
4103   fde = &fde_table[fde_table_in_use - 1];
4104   fde->dw_fde_vms_end_prologue = xstrdup (label);
4105 }
4106
4107 /* Output a marker (i.e. a label) for the beginning of the generated code
4108    for a function epilogue.  This gets called *before* the prologue code has
4109    been generated.  */
4110
4111 void
4112 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4113                           const char *file ATTRIBUTE_UNUSED)
4114 {
4115   dw_fde_ref fde;
4116   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4117
4118   fde = &fde_table[fde_table_in_use - 1];
4119   if (fde->dw_fde_vms_begin_epilogue)
4120     return;
4121
4122   /* Output a label to mark the endpoint of the code generated for this
4123      function.  */
4124   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4125                                current_function_funcdef_no);
4126   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4127                           current_function_funcdef_no);
4128   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4129 }
4130
4131 /* Output a marker (i.e. a label) for the absolute end of the generated code
4132    for a function definition.  This gets called *after* the epilogue code has
4133    been generated.  */
4134
4135 void
4136 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4137                         const char *file ATTRIBUTE_UNUSED)
4138 {
4139   dw_fde_ref fde;
4140   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4141
4142   last_var_location_insn = NULL_RTX;
4143
4144   if (dwarf2out_do_cfi_asm ())
4145     fprintf (asm_out_file, "\t.cfi_endproc\n");
4146
4147   /* Output a label to mark the endpoint of the code generated for this
4148      function.  */
4149   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4150                                current_function_funcdef_no);
4151   ASM_OUTPUT_LABEL (asm_out_file, label);
4152   fde = current_fde ();
4153   gcc_assert (fde != NULL);
4154   fde->dw_fde_end = xstrdup (label);
4155 }
4156
4157 void
4158 dwarf2out_frame_init (void)
4159 {
4160   /* Allocate the initial hunk of the fde_table.  */
4161   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4162   fde_table_allocated = FDE_TABLE_INCREMENT;
4163   fde_table_in_use = 0;
4164
4165   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4166      sake of lookup_cfa.  */
4167
4168   /* On entry, the Canonical Frame Address is at SP.  */
4169   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4170
4171   if (targetm.debug_unwind_info () == UI_DWARF2
4172       || targetm.except_unwind_info () == UI_DWARF2)
4173     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4174 }
4175
4176 void
4177 dwarf2out_frame_finish (void)
4178 {
4179   /* Output call frame information.  */
4180   if (targetm.debug_unwind_info () == UI_DWARF2)
4181     output_call_frame_info (0);
4182
4183   /* Output another copy for the unwinder.  */
4184   if ((flag_unwind_tables || flag_exceptions)
4185       && targetm.except_unwind_info () == UI_DWARF2)
4186     output_call_frame_info (1);
4187 }
4188
4189 /* Note that the current function section is being used for code.  */
4190
4191 static void
4192 dwarf2out_note_section_used (void)
4193 {
4194   section *sec = current_function_section ();
4195   if (sec == text_section)
4196     text_section_used = true;
4197   else if (sec == cold_text_section)
4198     cold_text_section_used = true;
4199 }
4200
4201 void
4202 dwarf2out_switch_text_section (void)
4203 {
4204   dw_fde_ref fde = current_fde ();
4205
4206   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4207
4208   fde->dw_fde_switched_sections = 1;
4209   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4210
4211   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4212   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4213   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4214   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4215   have_multiple_function_sections = true;
4216
4217   /* Reset the current label on switching text sections, so that we
4218      don't attempt to advance_loc4 between labels in different sections.  */
4219   fde->dw_fde_current_label = NULL;
4220
4221   /* There is no need to mark used sections when not debugging.  */
4222   if (cold_text_section != NULL)
4223     dwarf2out_note_section_used ();
4224
4225   if (dwarf2out_do_cfi_asm ())
4226     fprintf (asm_out_file, "\t.cfi_endproc\n");
4227
4228   /* Now do the real section switch.  */
4229   switch_to_section (current_function_section ());
4230
4231   if (dwarf2out_do_cfi_asm ())
4232     {
4233       dwarf2out_do_cfi_startproc (true);
4234       /* As this is a different FDE, insert all current CFI instructions
4235          again.  */
4236       output_cfis (fde->dw_fde_cfi, true, fde, true);
4237     }
4238   else
4239     {
4240       dw_cfi_ref cfi = fde->dw_fde_cfi;
4241
4242       cfi = fde->dw_fde_cfi;
4243       if (cfi)
4244         while (cfi->dw_cfi_next != NULL)
4245           cfi = cfi->dw_cfi_next;
4246       fde->dw_fde_switch_cfi = cfi;
4247     }
4248 }
4249 \f
4250 /* And now, the subset of the debugging information support code necessary
4251    for emitting location expressions.  */
4252
4253 /* Data about a single source file.  */
4254 struct GTY(()) dwarf_file_data {
4255   const char * filename;
4256   int emitted_number;
4257 };
4258
4259 typedef struct dw_val_struct *dw_val_ref;
4260 typedef struct die_struct *dw_die_ref;
4261 typedef const struct die_struct *const_dw_die_ref;
4262 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4263 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4264
4265 typedef struct GTY(()) deferred_locations_struct
4266 {
4267   tree variable;
4268   dw_die_ref die;
4269 } deferred_locations;
4270
4271 DEF_VEC_O(deferred_locations);
4272 DEF_VEC_ALLOC_O(deferred_locations,gc);
4273
4274 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4275
4276 DEF_VEC_P(dw_die_ref);
4277 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4278
4279 /* Each DIE may have a series of attribute/value pairs.  Values
4280    can take on several forms.  The forms that are used in this
4281    implementation are listed below.  */
4282
4283 enum dw_val_class
4284 {
4285   dw_val_class_addr,
4286   dw_val_class_offset,
4287   dw_val_class_loc,
4288   dw_val_class_loc_list,
4289   dw_val_class_range_list,
4290   dw_val_class_const,
4291   dw_val_class_unsigned_const,
4292   dw_val_class_const_double,
4293   dw_val_class_vec,
4294   dw_val_class_flag,
4295   dw_val_class_die_ref,
4296   dw_val_class_fde_ref,
4297   dw_val_class_lbl_id,
4298   dw_val_class_lineptr,
4299   dw_val_class_str,
4300   dw_val_class_macptr,
4301   dw_val_class_file,
4302   dw_val_class_data8,
4303   dw_val_class_decl_ref,
4304   dw_val_class_vms_delta
4305 };
4306
4307 /* Describe a floating point constant value, or a vector constant value.  */
4308
4309 typedef struct GTY(()) dw_vec_struct {
4310   unsigned char * GTY((length ("%h.length"))) array;
4311   unsigned length;
4312   unsigned elt_size;
4313 }
4314 dw_vec_const;
4315
4316 /* The dw_val_node describes an attribute's value, as it is
4317    represented internally.  */
4318
4319 typedef struct GTY(()) dw_val_struct {
4320   enum dw_val_class val_class;
4321   union dw_val_struct_union
4322     {
4323       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4324       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4325       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4326       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4327       HOST_WIDE_INT GTY ((default)) val_int;
4328       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4329       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4330       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4331       struct dw_val_die_union
4332         {
4333           dw_die_ref die;
4334           int external;
4335         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4336       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4337       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4338       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4339       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4340       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4341       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4342       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4343       struct dw_val_vms_delta_union
4344         {
4345           char * lbl1;
4346           char * lbl2;
4347         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4348     }
4349   GTY ((desc ("%1.val_class"))) v;
4350 }
4351 dw_val_node;
4352
4353 /* Locations in memory are described using a sequence of stack machine
4354    operations.  */
4355
4356 typedef struct GTY(()) dw_loc_descr_struct {
4357   dw_loc_descr_ref dw_loc_next;
4358   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4359   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4360      from DW_OP_addr with a dtp-relative symbol relocation.  */
4361   unsigned int dtprel : 1;
4362   int dw_loc_addr;
4363   dw_val_node dw_loc_oprnd1;
4364   dw_val_node dw_loc_oprnd2;
4365 }
4366 dw_loc_descr_node;
4367
4368 /* Location lists are ranges + location descriptions for that range,
4369    so you can track variables that are in different places over
4370    their entire life.  */
4371 typedef struct GTY(()) dw_loc_list_struct {
4372   dw_loc_list_ref dw_loc_next;
4373   const char *begin; /* Label for begin address of range */
4374   const char *end;  /* Label for end address of range */
4375   char *ll_symbol; /* Label for beginning of location list.
4376                       Only on head of list */
4377   const char *section; /* Section this loclist is relative to */
4378   dw_loc_descr_ref expr;
4379   hashval_t hash;
4380   bool emitted;
4381 } dw_loc_list_node;
4382
4383 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4384
4385 /* Convert a DWARF stack opcode into its string name.  */
4386
4387 static const char *
4388 dwarf_stack_op_name (unsigned int op)
4389 {
4390   switch (op)
4391     {
4392     case DW_OP_addr:
4393       return "DW_OP_addr";
4394     case DW_OP_deref:
4395       return "DW_OP_deref";
4396     case DW_OP_const1u:
4397       return "DW_OP_const1u";
4398     case DW_OP_const1s:
4399       return "DW_OP_const1s";
4400     case DW_OP_const2u:
4401       return "DW_OP_const2u";
4402     case DW_OP_const2s:
4403       return "DW_OP_const2s";
4404     case DW_OP_const4u:
4405       return "DW_OP_const4u";
4406     case DW_OP_const4s:
4407       return "DW_OP_const4s";
4408     case DW_OP_const8u:
4409       return "DW_OP_const8u";
4410     case DW_OP_const8s:
4411       return "DW_OP_const8s";
4412     case DW_OP_constu:
4413       return "DW_OP_constu";
4414     case DW_OP_consts:
4415       return "DW_OP_consts";
4416     case DW_OP_dup:
4417       return "DW_OP_dup";
4418     case DW_OP_drop:
4419       return "DW_OP_drop";
4420     case DW_OP_over:
4421       return "DW_OP_over";
4422     case DW_OP_pick:
4423       return "DW_OP_pick";
4424     case DW_OP_swap:
4425       return "DW_OP_swap";
4426     case DW_OP_rot:
4427       return "DW_OP_rot";
4428     case DW_OP_xderef:
4429       return "DW_OP_xderef";
4430     case DW_OP_abs:
4431       return "DW_OP_abs";
4432     case DW_OP_and:
4433       return "DW_OP_and";
4434     case DW_OP_div:
4435       return "DW_OP_div";
4436     case DW_OP_minus:
4437       return "DW_OP_minus";
4438     case DW_OP_mod:
4439       return "DW_OP_mod";
4440     case DW_OP_mul:
4441       return "DW_OP_mul";
4442     case DW_OP_neg:
4443       return "DW_OP_neg";
4444     case DW_OP_not:
4445       return "DW_OP_not";
4446     case DW_OP_or:
4447       return "DW_OP_or";
4448     case DW_OP_plus:
4449       return "DW_OP_plus";
4450     case DW_OP_plus_uconst:
4451       return "DW_OP_plus_uconst";
4452     case DW_OP_shl:
4453       return "DW_OP_shl";
4454     case DW_OP_shr:
4455       return "DW_OP_shr";
4456     case DW_OP_shra:
4457       return "DW_OP_shra";
4458     case DW_OP_xor:
4459       return "DW_OP_xor";
4460     case DW_OP_bra:
4461       return "DW_OP_bra";
4462     case DW_OP_eq:
4463       return "DW_OP_eq";
4464     case DW_OP_ge:
4465       return "DW_OP_ge";
4466     case DW_OP_gt:
4467       return "DW_OP_gt";
4468     case DW_OP_le:
4469       return "DW_OP_le";
4470     case DW_OP_lt:
4471       return "DW_OP_lt";
4472     case DW_OP_ne:
4473       return "DW_OP_ne";
4474     case DW_OP_skip:
4475       return "DW_OP_skip";
4476     case DW_OP_lit0:
4477       return "DW_OP_lit0";
4478     case DW_OP_lit1:
4479       return "DW_OP_lit1";
4480     case DW_OP_lit2:
4481       return "DW_OP_lit2";
4482     case DW_OP_lit3:
4483       return "DW_OP_lit3";
4484     case DW_OP_lit4:
4485       return "DW_OP_lit4";
4486     case DW_OP_lit5:
4487       return "DW_OP_lit5";
4488     case DW_OP_lit6:
4489       return "DW_OP_lit6";
4490     case DW_OP_lit7:
4491       return "DW_OP_lit7";
4492     case DW_OP_lit8:
4493       return "DW_OP_lit8";
4494     case DW_OP_lit9:
4495       return "DW_OP_lit9";
4496     case DW_OP_lit10:
4497       return "DW_OP_lit10";
4498     case DW_OP_lit11:
4499       return "DW_OP_lit11";
4500     case DW_OP_lit12:
4501       return "DW_OP_lit12";
4502     case DW_OP_lit13:
4503       return "DW_OP_lit13";
4504     case DW_OP_lit14:
4505       return "DW_OP_lit14";
4506     case DW_OP_lit15:
4507       return "DW_OP_lit15";
4508     case DW_OP_lit16:
4509       return "DW_OP_lit16";
4510     case DW_OP_lit17:
4511       return "DW_OP_lit17";
4512     case DW_OP_lit18:
4513       return "DW_OP_lit18";
4514     case DW_OP_lit19:
4515       return "DW_OP_lit19";
4516     case DW_OP_lit20:
4517       return "DW_OP_lit20";
4518     case DW_OP_lit21:
4519       return "DW_OP_lit21";
4520     case DW_OP_lit22:
4521       return "DW_OP_lit22";
4522     case DW_OP_lit23:
4523       return "DW_OP_lit23";
4524     case DW_OP_lit24:
4525       return "DW_OP_lit24";
4526     case DW_OP_lit25:
4527       return "DW_OP_lit25";
4528     case DW_OP_lit26:
4529       return "DW_OP_lit26";
4530     case DW_OP_lit27:
4531       return "DW_OP_lit27";
4532     case DW_OP_lit28:
4533       return "DW_OP_lit28";
4534     case DW_OP_lit29:
4535       return "DW_OP_lit29";
4536     case DW_OP_lit30:
4537       return "DW_OP_lit30";
4538     case DW_OP_lit31:
4539       return "DW_OP_lit31";
4540     case DW_OP_reg0:
4541       return "DW_OP_reg0";
4542     case DW_OP_reg1:
4543       return "DW_OP_reg1";
4544     case DW_OP_reg2:
4545       return "DW_OP_reg2";
4546     case DW_OP_reg3:
4547       return "DW_OP_reg3";
4548     case DW_OP_reg4:
4549       return "DW_OP_reg4";
4550     case DW_OP_reg5:
4551       return "DW_OP_reg5";
4552     case DW_OP_reg6:
4553       return "DW_OP_reg6";
4554     case DW_OP_reg7:
4555       return "DW_OP_reg7";
4556     case DW_OP_reg8:
4557       return "DW_OP_reg8";
4558     case DW_OP_reg9:
4559       return "DW_OP_reg9";
4560     case DW_OP_reg10:
4561       return "DW_OP_reg10";
4562     case DW_OP_reg11:
4563       return "DW_OP_reg11";
4564     case DW_OP_reg12:
4565       return "DW_OP_reg12";
4566     case DW_OP_reg13:
4567       return "DW_OP_reg13";
4568     case DW_OP_reg14:
4569       return "DW_OP_reg14";
4570     case DW_OP_reg15:
4571       return "DW_OP_reg15";
4572     case DW_OP_reg16:
4573       return "DW_OP_reg16";
4574     case DW_OP_reg17:
4575       return "DW_OP_reg17";
4576     case DW_OP_reg18:
4577       return "DW_OP_reg18";
4578     case DW_OP_reg19:
4579       return "DW_OP_reg19";
4580     case DW_OP_reg20:
4581       return "DW_OP_reg20";
4582     case DW_OP_reg21:
4583       return "DW_OP_reg21";
4584     case DW_OP_reg22:
4585       return "DW_OP_reg22";
4586     case DW_OP_reg23:
4587       return "DW_OP_reg23";
4588     case DW_OP_reg24:
4589       return "DW_OP_reg24";
4590     case DW_OP_reg25:
4591       return "DW_OP_reg25";
4592     case DW_OP_reg26:
4593       return "DW_OP_reg26";
4594     case DW_OP_reg27:
4595       return "DW_OP_reg27";
4596     case DW_OP_reg28:
4597       return "DW_OP_reg28";
4598     case DW_OP_reg29:
4599       return "DW_OP_reg29";
4600     case DW_OP_reg30:
4601       return "DW_OP_reg30";
4602     case DW_OP_reg31:
4603       return "DW_OP_reg31";
4604     case DW_OP_breg0:
4605       return "DW_OP_breg0";
4606     case DW_OP_breg1:
4607       return "DW_OP_breg1";
4608     case DW_OP_breg2:
4609       return "DW_OP_breg2";
4610     case DW_OP_breg3:
4611       return "DW_OP_breg3";
4612     case DW_OP_breg4:
4613       return "DW_OP_breg4";
4614     case DW_OP_breg5:
4615       return "DW_OP_breg5";
4616     case DW_OP_breg6:
4617       return "DW_OP_breg6";
4618     case DW_OP_breg7:
4619       return "DW_OP_breg7";
4620     case DW_OP_breg8:
4621       return "DW_OP_breg8";
4622     case DW_OP_breg9:
4623       return "DW_OP_breg9";
4624     case DW_OP_breg10:
4625       return "DW_OP_breg10";
4626     case DW_OP_breg11:
4627       return "DW_OP_breg11";
4628     case DW_OP_breg12:
4629       return "DW_OP_breg12";
4630     case DW_OP_breg13:
4631       return "DW_OP_breg13";
4632     case DW_OP_breg14:
4633       return "DW_OP_breg14";
4634     case DW_OP_breg15:
4635       return "DW_OP_breg15";
4636     case DW_OP_breg16:
4637       return "DW_OP_breg16";
4638     case DW_OP_breg17:
4639       return "DW_OP_breg17";
4640     case DW_OP_breg18:
4641       return "DW_OP_breg18";
4642     case DW_OP_breg19:
4643       return "DW_OP_breg19";
4644     case DW_OP_breg20:
4645       return "DW_OP_breg20";
4646     case DW_OP_breg21:
4647       return "DW_OP_breg21";
4648     case DW_OP_breg22:
4649       return "DW_OP_breg22";
4650     case DW_OP_breg23:
4651       return "DW_OP_breg23";
4652     case DW_OP_breg24:
4653       return "DW_OP_breg24";
4654     case DW_OP_breg25:
4655       return "DW_OP_breg25";
4656     case DW_OP_breg26:
4657       return "DW_OP_breg26";
4658     case DW_OP_breg27:
4659       return "DW_OP_breg27";
4660     case DW_OP_breg28:
4661       return "DW_OP_breg28";
4662     case DW_OP_breg29:
4663       return "DW_OP_breg29";
4664     case DW_OP_breg30:
4665       return "DW_OP_breg30";
4666     case DW_OP_breg31:
4667       return "DW_OP_breg31";
4668     case DW_OP_regx:
4669       return "DW_OP_regx";
4670     case DW_OP_fbreg:
4671       return "DW_OP_fbreg";
4672     case DW_OP_bregx:
4673       return "DW_OP_bregx";
4674     case DW_OP_piece:
4675       return "DW_OP_piece";
4676     case DW_OP_deref_size:
4677       return "DW_OP_deref_size";
4678     case DW_OP_xderef_size:
4679       return "DW_OP_xderef_size";
4680     case DW_OP_nop:
4681       return "DW_OP_nop";
4682
4683     case DW_OP_push_object_address:
4684       return "DW_OP_push_object_address";
4685     case DW_OP_call2:
4686       return "DW_OP_call2";
4687     case DW_OP_call4:
4688       return "DW_OP_call4";
4689     case DW_OP_call_ref:
4690       return "DW_OP_call_ref";
4691     case DW_OP_implicit_value:
4692       return "DW_OP_implicit_value";
4693     case DW_OP_stack_value:
4694       return "DW_OP_stack_value";
4695     case DW_OP_form_tls_address:
4696       return "DW_OP_form_tls_address";
4697     case DW_OP_call_frame_cfa:
4698       return "DW_OP_call_frame_cfa";
4699     case DW_OP_bit_piece:
4700       return "DW_OP_bit_piece";
4701
4702     case DW_OP_GNU_push_tls_address:
4703       return "DW_OP_GNU_push_tls_address";
4704     case DW_OP_GNU_uninit:
4705       return "DW_OP_GNU_uninit";
4706     case DW_OP_GNU_encoded_addr:
4707       return "DW_OP_GNU_encoded_addr";
4708     case DW_OP_GNU_implicit_pointer:
4709       return "DW_OP_GNU_implicit_pointer";
4710
4711     default:
4712       return "OP_<unknown>";
4713     }
4714 }
4715
4716 /* Return a pointer to a newly allocated location description.  Location
4717    descriptions are simple expression terms that can be strung
4718    together to form more complicated location (address) descriptions.  */
4719
4720 static inline dw_loc_descr_ref
4721 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4722                unsigned HOST_WIDE_INT oprnd2)
4723 {
4724   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4725
4726   descr->dw_loc_opc = op;
4727   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4728   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4729   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4730   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4731
4732   return descr;
4733 }
4734
4735 /* Return a pointer to a newly allocated location description for
4736    REG and OFFSET.  */
4737
4738 static inline dw_loc_descr_ref
4739 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4740 {
4741   if (reg <= 31)
4742     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4743                           offset, 0);
4744   else
4745     return new_loc_descr (DW_OP_bregx, reg, offset);
4746 }
4747
4748 /* Add a location description term to a location description expression.  */
4749
4750 static inline void
4751 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4752 {
4753   dw_loc_descr_ref *d;
4754
4755   /* Find the end of the chain.  */
4756   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4757     ;
4758
4759   *d = descr;
4760 }
4761
4762 /* Add a constant OFFSET to a location expression.  */
4763
4764 static void
4765 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4766 {
4767   dw_loc_descr_ref loc;
4768   HOST_WIDE_INT *p;
4769
4770   gcc_assert (*list_head != NULL);
4771
4772   if (!offset)
4773     return;
4774
4775   /* Find the end of the chain.  */
4776   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4777     ;
4778
4779   p = NULL;
4780   if (loc->dw_loc_opc == DW_OP_fbreg
4781       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4782     p = &loc->dw_loc_oprnd1.v.val_int;
4783   else if (loc->dw_loc_opc == DW_OP_bregx)
4784     p = &loc->dw_loc_oprnd2.v.val_int;
4785
4786   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4787      offset.  Don't optimize if an signed integer overflow would happen.  */
4788   if (p != NULL
4789       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4790           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4791     *p += offset;
4792
4793   else if (offset > 0)
4794     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4795
4796   else
4797     {
4798       loc->dw_loc_next = int_loc_descriptor (-offset);
4799       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4800     }
4801 }
4802
4803 /* Add a constant OFFSET to a location list.  */
4804
4805 static void
4806 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4807 {
4808   dw_loc_list_ref d;
4809   for (d = list_head; d != NULL; d = d->dw_loc_next)
4810     loc_descr_plus_const (&d->expr, offset);
4811 }
4812
4813 #define DWARF_REF_SIZE  \
4814   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4815
4816 /* Return the size of a location descriptor.  */
4817
4818 static unsigned long
4819 size_of_loc_descr (dw_loc_descr_ref loc)
4820 {
4821   unsigned long size = 1;
4822
4823   switch (loc->dw_loc_opc)
4824     {
4825     case DW_OP_addr:
4826       size += DWARF2_ADDR_SIZE;
4827       break;
4828     case DW_OP_const1u:
4829     case DW_OP_const1s:
4830       size += 1;
4831       break;
4832     case DW_OP_const2u:
4833     case DW_OP_const2s:
4834       size += 2;
4835       break;
4836     case DW_OP_const4u:
4837     case DW_OP_const4s:
4838       size += 4;
4839       break;
4840     case DW_OP_const8u:
4841     case DW_OP_const8s:
4842       size += 8;
4843       break;
4844     case DW_OP_constu:
4845       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4846       break;
4847     case DW_OP_consts:
4848       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4849       break;
4850     case DW_OP_pick:
4851       size += 1;
4852       break;
4853     case DW_OP_plus_uconst:
4854       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4855       break;
4856     case DW_OP_skip:
4857     case DW_OP_bra:
4858       size += 2;
4859       break;
4860     case DW_OP_breg0:
4861     case DW_OP_breg1:
4862     case DW_OP_breg2:
4863     case DW_OP_breg3:
4864     case DW_OP_breg4:
4865     case DW_OP_breg5:
4866     case DW_OP_breg6:
4867     case DW_OP_breg7:
4868     case DW_OP_breg8:
4869     case DW_OP_breg9:
4870     case DW_OP_breg10:
4871     case DW_OP_breg11:
4872     case DW_OP_breg12:
4873     case DW_OP_breg13:
4874     case DW_OP_breg14:
4875     case DW_OP_breg15:
4876     case DW_OP_breg16:
4877     case DW_OP_breg17:
4878     case DW_OP_breg18:
4879     case DW_OP_breg19:
4880     case DW_OP_breg20:
4881     case DW_OP_breg21:
4882     case DW_OP_breg22:
4883     case DW_OP_breg23:
4884     case DW_OP_breg24:
4885     case DW_OP_breg25:
4886     case DW_OP_breg26:
4887     case DW_OP_breg27:
4888     case DW_OP_breg28:
4889     case DW_OP_breg29:
4890     case DW_OP_breg30:
4891     case DW_OP_breg31:
4892       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4893       break;
4894     case DW_OP_regx:
4895       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4896       break;
4897     case DW_OP_fbreg:
4898       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4899       break;
4900     case DW_OP_bregx:
4901       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4902       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4903       break;
4904     case DW_OP_piece:
4905       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4906       break;
4907     case DW_OP_bit_piece:
4908       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4909       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4910       break;
4911     case DW_OP_deref_size:
4912     case DW_OP_xderef_size:
4913       size += 1;
4914       break;
4915     case DW_OP_call2:
4916       size += 2;
4917       break;
4918     case DW_OP_call4:
4919       size += 4;
4920       break;
4921     case DW_OP_call_ref:
4922       size += DWARF_REF_SIZE;
4923       break;
4924     case DW_OP_implicit_value:
4925       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4926               + loc->dw_loc_oprnd1.v.val_unsigned;
4927       break;
4928     case DW_OP_GNU_implicit_pointer:
4929       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4930       break;
4931     default:
4932       break;
4933     }
4934
4935   return size;
4936 }
4937
4938 /* Return the size of a series of location descriptors.  */
4939
4940 static unsigned long
4941 size_of_locs (dw_loc_descr_ref loc)
4942 {
4943   dw_loc_descr_ref l;
4944   unsigned long size;
4945
4946   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4947      field, to avoid writing to a PCH file.  */
4948   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4949     {
4950       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4951         break;
4952       size += size_of_loc_descr (l);
4953     }
4954   if (! l)
4955     return size;
4956
4957   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4958     {
4959       l->dw_loc_addr = size;
4960       size += size_of_loc_descr (l);
4961     }
4962
4963   return size;
4964 }
4965
4966 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4967 static void get_ref_die_offset_label (char *, dw_die_ref);
4968
4969 /* Output location description stack opcode's operands (if any).  */
4970
4971 static void
4972 output_loc_operands (dw_loc_descr_ref loc)
4973 {
4974   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4975   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4976
4977   switch (loc->dw_loc_opc)
4978     {
4979 #ifdef DWARF2_DEBUGGING_INFO
4980     case DW_OP_const2u:
4981     case DW_OP_const2s:
4982       dw2_asm_output_data (2, val1->v.val_int, NULL);
4983       break;
4984     case DW_OP_const4u:
4985       if (loc->dtprel)
4986         {
4987           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4988           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4989                                                val1->v.val_addr);
4990           fputc ('\n', asm_out_file);
4991           break;
4992         }
4993       /* FALLTHRU */
4994     case DW_OP_const4s:
4995       dw2_asm_output_data (4, val1->v.val_int, NULL);
4996       break;
4997     case DW_OP_const8u:
4998       if (loc->dtprel)
4999         {
5000           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5001           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5002                                                val1->v.val_addr);
5003           fputc ('\n', asm_out_file);
5004           break;
5005         }
5006       /* FALLTHRU */
5007     case DW_OP_const8s:
5008       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5009       dw2_asm_output_data (8, val1->v.val_int, NULL);
5010       break;
5011     case DW_OP_skip:
5012     case DW_OP_bra:
5013       {
5014         int offset;
5015
5016         gcc_assert (val1->val_class == dw_val_class_loc);
5017         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5018
5019         dw2_asm_output_data (2, offset, NULL);
5020       }
5021       break;
5022     case DW_OP_implicit_value:
5023       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5024       switch (val2->val_class)
5025         {
5026         case dw_val_class_const:
5027           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5028           break;
5029         case dw_val_class_vec:
5030           {
5031             unsigned int elt_size = val2->v.val_vec.elt_size;
5032             unsigned int len = val2->v.val_vec.length;
5033             unsigned int i;
5034             unsigned char *p;
5035
5036             if (elt_size > sizeof (HOST_WIDE_INT))
5037               {
5038                 elt_size /= 2;
5039                 len *= 2;
5040               }
5041             for (i = 0, p = val2->v.val_vec.array;
5042                  i < len;
5043                  i++, p += elt_size)
5044               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5045                                    "fp or vector constant word %u", i);
5046           }
5047           break;
5048         case dw_val_class_const_double:
5049           {
5050             unsigned HOST_WIDE_INT first, second;
5051
5052             if (WORDS_BIG_ENDIAN)
5053               {
5054                 first = val2->v.val_double.high;
5055                 second = val2->v.val_double.low;
5056               }
5057             else
5058               {
5059                 first = val2->v.val_double.low;
5060                 second = val2->v.val_double.high;
5061               }
5062             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5063                                  first, NULL);
5064             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5065                                  second, NULL);
5066           }
5067           break;
5068         case dw_val_class_addr:
5069           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5070           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5071           break;
5072         default:
5073           gcc_unreachable ();
5074         }
5075       break;
5076 #else
5077     case DW_OP_const2u:
5078     case DW_OP_const2s:
5079     case DW_OP_const4u:
5080     case DW_OP_const4s:
5081     case DW_OP_const8u:
5082     case DW_OP_const8s:
5083     case DW_OP_skip:
5084     case DW_OP_bra:
5085     case DW_OP_implicit_value:
5086       /* We currently don't make any attempt to make sure these are
5087          aligned properly like we do for the main unwind info, so
5088          don't support emitting things larger than a byte if we're
5089          only doing unwinding.  */
5090       gcc_unreachable ();
5091 #endif
5092     case DW_OP_const1u:
5093     case DW_OP_const1s:
5094       dw2_asm_output_data (1, val1->v.val_int, NULL);
5095       break;
5096     case DW_OP_constu:
5097       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5098       break;
5099     case DW_OP_consts:
5100       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5101       break;
5102     case DW_OP_pick:
5103       dw2_asm_output_data (1, val1->v.val_int, NULL);
5104       break;
5105     case DW_OP_plus_uconst:
5106       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5107       break;
5108     case DW_OP_breg0:
5109     case DW_OP_breg1:
5110     case DW_OP_breg2:
5111     case DW_OP_breg3:
5112     case DW_OP_breg4:
5113     case DW_OP_breg5:
5114     case DW_OP_breg6:
5115     case DW_OP_breg7:
5116     case DW_OP_breg8:
5117     case DW_OP_breg9:
5118     case DW_OP_breg10:
5119     case DW_OP_breg11:
5120     case DW_OP_breg12:
5121     case DW_OP_breg13:
5122     case DW_OP_breg14:
5123     case DW_OP_breg15:
5124     case DW_OP_breg16:
5125     case DW_OP_breg17:
5126     case DW_OP_breg18:
5127     case DW_OP_breg19:
5128     case DW_OP_breg20:
5129     case DW_OP_breg21:
5130     case DW_OP_breg22:
5131     case DW_OP_breg23:
5132     case DW_OP_breg24:
5133     case DW_OP_breg25:
5134     case DW_OP_breg26:
5135     case DW_OP_breg27:
5136     case DW_OP_breg28:
5137     case DW_OP_breg29:
5138     case DW_OP_breg30:
5139     case DW_OP_breg31:
5140       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5141       break;
5142     case DW_OP_regx:
5143       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5144       break;
5145     case DW_OP_fbreg:
5146       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5147       break;
5148     case DW_OP_bregx:
5149       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5150       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5151       break;
5152     case DW_OP_piece:
5153       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5154       break;
5155     case DW_OP_bit_piece:
5156       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5157       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5158       break;
5159     case DW_OP_deref_size:
5160     case DW_OP_xderef_size:
5161       dw2_asm_output_data (1, val1->v.val_int, NULL);
5162       break;
5163
5164     case DW_OP_addr:
5165       if (loc->dtprel)
5166         {
5167           if (targetm.asm_out.output_dwarf_dtprel)
5168             {
5169               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5170                                                    DWARF2_ADDR_SIZE,
5171                                                    val1->v.val_addr);
5172               fputc ('\n', asm_out_file);
5173             }
5174           else
5175             gcc_unreachable ();
5176         }
5177       else
5178         {
5179 #ifdef DWARF2_DEBUGGING_INFO
5180           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5181 #else
5182           gcc_unreachable ();
5183 #endif
5184         }
5185       break;
5186
5187     case DW_OP_GNU_implicit_pointer:
5188       {
5189         char label[MAX_ARTIFICIAL_LABEL_BYTES
5190                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5191         gcc_assert (val1->val_class == dw_val_class_die_ref);
5192         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5193         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5194         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5195       }
5196       break;
5197
5198     default:
5199       /* Other codes have no operands.  */
5200       break;
5201     }
5202 }
5203
5204 /* Output a sequence of location operations.  */
5205
5206 static void
5207 output_loc_sequence (dw_loc_descr_ref loc)
5208 {
5209   for (; loc != NULL; loc = loc->dw_loc_next)
5210     {
5211       /* Output the opcode.  */
5212       dw2_asm_output_data (1, loc->dw_loc_opc,
5213                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5214
5215       /* Output the operand(s) (if any).  */
5216       output_loc_operands (loc);
5217     }
5218 }
5219
5220 /* Output location description stack opcode's operands (if any).
5221    The output is single bytes on a line, suitable for .cfi_escape.  */
5222
5223 static void
5224 output_loc_operands_raw (dw_loc_descr_ref loc)
5225 {
5226   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5227   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5228
5229   switch (loc->dw_loc_opc)
5230     {
5231     case DW_OP_addr:
5232     case DW_OP_implicit_value:
5233       /* We cannot output addresses in .cfi_escape, only bytes.  */
5234       gcc_unreachable ();
5235
5236     case DW_OP_const1u:
5237     case DW_OP_const1s:
5238     case DW_OP_pick:
5239     case DW_OP_deref_size:
5240     case DW_OP_xderef_size:
5241       fputc (',', asm_out_file);
5242       dw2_asm_output_data_raw (1, val1->v.val_int);
5243       break;
5244
5245     case DW_OP_const2u:
5246     case DW_OP_const2s:
5247       fputc (',', asm_out_file);
5248       dw2_asm_output_data_raw (2, val1->v.val_int);
5249       break;
5250
5251     case DW_OP_const4u:
5252     case DW_OP_const4s:
5253       fputc (',', asm_out_file);
5254       dw2_asm_output_data_raw (4, val1->v.val_int);
5255       break;
5256
5257     case DW_OP_const8u:
5258     case DW_OP_const8s:
5259       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5260       fputc (',', asm_out_file);
5261       dw2_asm_output_data_raw (8, val1->v.val_int);
5262       break;
5263
5264     case DW_OP_skip:
5265     case DW_OP_bra:
5266       {
5267         int offset;
5268
5269         gcc_assert (val1->val_class == dw_val_class_loc);
5270         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5271
5272         fputc (',', asm_out_file);
5273         dw2_asm_output_data_raw (2, offset);
5274       }
5275       break;
5276
5277     case DW_OP_constu:
5278     case DW_OP_plus_uconst:
5279     case DW_OP_regx:
5280     case DW_OP_piece:
5281       fputc (',', asm_out_file);
5282       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5283       break;
5284
5285     case DW_OP_bit_piece:
5286       fputc (',', asm_out_file);
5287       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5288       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5289       break;
5290
5291     case DW_OP_consts:
5292     case DW_OP_breg0:
5293     case DW_OP_breg1:
5294     case DW_OP_breg2:
5295     case DW_OP_breg3:
5296     case DW_OP_breg4:
5297     case DW_OP_breg5:
5298     case DW_OP_breg6:
5299     case DW_OP_breg7:
5300     case DW_OP_breg8:
5301     case DW_OP_breg9:
5302     case DW_OP_breg10:
5303     case DW_OP_breg11:
5304     case DW_OP_breg12:
5305     case DW_OP_breg13:
5306     case DW_OP_breg14:
5307     case DW_OP_breg15:
5308     case DW_OP_breg16:
5309     case DW_OP_breg17:
5310     case DW_OP_breg18:
5311     case DW_OP_breg19:
5312     case DW_OP_breg20:
5313     case DW_OP_breg21:
5314     case DW_OP_breg22:
5315     case DW_OP_breg23:
5316     case DW_OP_breg24:
5317     case DW_OP_breg25:
5318     case DW_OP_breg26:
5319     case DW_OP_breg27:
5320     case DW_OP_breg28:
5321     case DW_OP_breg29:
5322     case DW_OP_breg30:
5323     case DW_OP_breg31:
5324     case DW_OP_fbreg:
5325       fputc (',', asm_out_file);
5326       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5327       break;
5328
5329     case DW_OP_bregx:
5330       fputc (',', asm_out_file);
5331       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5332       fputc (',', asm_out_file);
5333       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5334       break;
5335
5336     case DW_OP_GNU_implicit_pointer:
5337       gcc_unreachable ();
5338       break;
5339
5340     default:
5341       /* Other codes have no operands.  */
5342       break;
5343     }
5344 }
5345
5346 static void
5347 output_loc_sequence_raw (dw_loc_descr_ref loc)
5348 {
5349   while (1)
5350     {
5351       /* Output the opcode.  */
5352       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5353       output_loc_operands_raw (loc);
5354
5355       if (!loc->dw_loc_next)
5356         break;
5357       loc = loc->dw_loc_next;
5358
5359       fputc (',', asm_out_file);
5360     }
5361 }
5362
5363 /* This routine will generate the correct assembly data for a location
5364    description based on a cfi entry with a complex address.  */
5365
5366 static void
5367 output_cfa_loc (dw_cfi_ref cfi)
5368 {
5369   dw_loc_descr_ref loc;
5370   unsigned long size;
5371
5372   if (cfi->dw_cfi_opc == DW_CFA_expression)
5373     {
5374       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5375       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5376     }
5377   else
5378     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5379
5380   /* Output the size of the block.  */
5381   size = size_of_locs (loc);
5382   dw2_asm_output_data_uleb128 (size, NULL);
5383
5384   /* Now output the operations themselves.  */
5385   output_loc_sequence (loc);
5386 }
5387
5388 /* Similar, but used for .cfi_escape.  */
5389
5390 static void
5391 output_cfa_loc_raw (dw_cfi_ref cfi)
5392 {
5393   dw_loc_descr_ref loc;
5394   unsigned long size;
5395
5396   if (cfi->dw_cfi_opc == DW_CFA_expression)
5397     {
5398       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5399       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5400     }
5401   else
5402     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5403
5404   /* Output the size of the block.  */
5405   size = size_of_locs (loc);
5406   dw2_asm_output_data_uleb128_raw (size);
5407   fputc (',', asm_out_file);
5408
5409   /* Now output the operations themselves.  */
5410   output_loc_sequence_raw (loc);
5411 }
5412
5413 /* This function builds a dwarf location descriptor sequence from a
5414    dw_cfa_location, adding the given OFFSET to the result of the
5415    expression.  */
5416
5417 static struct dw_loc_descr_struct *
5418 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5419 {
5420   struct dw_loc_descr_struct *head, *tmp;
5421
5422   offset += cfa->offset;
5423
5424   if (cfa->indirect)
5425     {
5426       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5427       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5428       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5429       add_loc_descr (&head, tmp);
5430       if (offset != 0)
5431         {
5432           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5433           add_loc_descr (&head, tmp);
5434         }
5435     }
5436   else
5437     head = new_reg_loc_descr (cfa->reg, offset);
5438
5439   return head;
5440 }
5441
5442 /* This function builds a dwarf location descriptor sequence for
5443    the address at OFFSET from the CFA when stack is aligned to
5444    ALIGNMENT byte.  */
5445
5446 static struct dw_loc_descr_struct *
5447 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5448 {
5449   struct dw_loc_descr_struct *head;
5450   unsigned int dwarf_fp
5451     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5452
5453  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5454   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5455     {
5456       head = new_reg_loc_descr (dwarf_fp, 0);
5457       add_loc_descr (&head, int_loc_descriptor (alignment));
5458       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5459       loc_descr_plus_const (&head, offset);
5460     }
5461   else
5462     head = new_reg_loc_descr (dwarf_fp, offset);
5463   return head;
5464 }
5465
5466 /* This function fills in aa dw_cfa_location structure from a dwarf location
5467    descriptor sequence.  */
5468
5469 static void
5470 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5471 {
5472   struct dw_loc_descr_struct *ptr;
5473   cfa->offset = 0;
5474   cfa->base_offset = 0;
5475   cfa->indirect = 0;
5476   cfa->reg = -1;
5477
5478   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5479     {
5480       enum dwarf_location_atom op = ptr->dw_loc_opc;
5481
5482       switch (op)
5483         {
5484         case DW_OP_reg0:
5485         case DW_OP_reg1:
5486         case DW_OP_reg2:
5487         case DW_OP_reg3:
5488         case DW_OP_reg4:
5489         case DW_OP_reg5:
5490         case DW_OP_reg6:
5491         case DW_OP_reg7:
5492         case DW_OP_reg8:
5493         case DW_OP_reg9:
5494         case DW_OP_reg10:
5495         case DW_OP_reg11:
5496         case DW_OP_reg12:
5497         case DW_OP_reg13:
5498         case DW_OP_reg14:
5499         case DW_OP_reg15:
5500         case DW_OP_reg16:
5501         case DW_OP_reg17:
5502         case DW_OP_reg18:
5503         case DW_OP_reg19:
5504         case DW_OP_reg20:
5505         case DW_OP_reg21:
5506         case DW_OP_reg22:
5507         case DW_OP_reg23:
5508         case DW_OP_reg24:
5509         case DW_OP_reg25:
5510         case DW_OP_reg26:
5511         case DW_OP_reg27:
5512         case DW_OP_reg28:
5513         case DW_OP_reg29:
5514         case DW_OP_reg30:
5515         case DW_OP_reg31:
5516           cfa->reg = op - DW_OP_reg0;
5517           break;
5518         case DW_OP_regx:
5519           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5520           break;
5521         case DW_OP_breg0:
5522         case DW_OP_breg1:
5523         case DW_OP_breg2:
5524         case DW_OP_breg3:
5525         case DW_OP_breg4:
5526         case DW_OP_breg5:
5527         case DW_OP_breg6:
5528         case DW_OP_breg7:
5529         case DW_OP_breg8:
5530         case DW_OP_breg9:
5531         case DW_OP_breg10:
5532         case DW_OP_breg11:
5533         case DW_OP_breg12:
5534         case DW_OP_breg13:
5535         case DW_OP_breg14:
5536         case DW_OP_breg15:
5537         case DW_OP_breg16:
5538         case DW_OP_breg17:
5539         case DW_OP_breg18:
5540         case DW_OP_breg19:
5541         case DW_OP_breg20:
5542         case DW_OP_breg21:
5543         case DW_OP_breg22:
5544         case DW_OP_breg23:
5545         case DW_OP_breg24:
5546         case DW_OP_breg25:
5547         case DW_OP_breg26:
5548         case DW_OP_breg27:
5549         case DW_OP_breg28:
5550         case DW_OP_breg29:
5551         case DW_OP_breg30:
5552         case DW_OP_breg31:
5553           cfa->reg = op - DW_OP_breg0;
5554           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5555           break;
5556         case DW_OP_bregx:
5557           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5558           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5559           break;
5560         case DW_OP_deref:
5561           cfa->indirect = 1;
5562           break;
5563         case DW_OP_plus_uconst:
5564           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5565           break;
5566         default:
5567           internal_error ("DW_LOC_OP %s not implemented",
5568                           dwarf_stack_op_name (ptr->dw_loc_opc));
5569         }
5570     }
5571 }
5572 \f
5573 /* And now, the support for symbolic debugging information.  */
5574
5575 /* .debug_str support.  */
5576 static int output_indirect_string (void **, void *);
5577
5578 static void dwarf2out_init (const char *);
5579 static void dwarf2out_finish (const char *);
5580 static void dwarf2out_assembly_start (void);
5581 static void dwarf2out_define (unsigned int, const char *);
5582 static void dwarf2out_undef (unsigned int, const char *);
5583 static void dwarf2out_start_source_file (unsigned, const char *);
5584 static void dwarf2out_end_source_file (unsigned);
5585 static void dwarf2out_function_decl (tree);
5586 static void dwarf2out_begin_block (unsigned, unsigned);
5587 static void dwarf2out_end_block (unsigned, unsigned);
5588 static bool dwarf2out_ignore_block (const_tree);
5589 static void dwarf2out_global_decl (tree);
5590 static void dwarf2out_type_decl (tree, int);
5591 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5592 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5593                                                  dw_die_ref);
5594 static void dwarf2out_abstract_function (tree);
5595 static void dwarf2out_var_location (rtx);
5596 static void dwarf2out_direct_call (tree);
5597 static void dwarf2out_virtual_call_token (tree, int);
5598 static void dwarf2out_copy_call_info (rtx, rtx);
5599 static void dwarf2out_virtual_call (int);
5600 static void dwarf2out_begin_function (tree);
5601 static void dwarf2out_set_name (tree, tree);
5602
5603 /* The debug hooks structure.  */
5604
5605 const struct gcc_debug_hooks dwarf2_debug_hooks =
5606 {
5607   dwarf2out_init,
5608   dwarf2out_finish,
5609   dwarf2out_assembly_start,
5610   dwarf2out_define,
5611   dwarf2out_undef,
5612   dwarf2out_start_source_file,
5613   dwarf2out_end_source_file,
5614   dwarf2out_begin_block,
5615   dwarf2out_end_block,
5616   dwarf2out_ignore_block,
5617   dwarf2out_source_line,
5618   dwarf2out_begin_prologue,
5619 #if VMS_DEBUGGING_INFO
5620   dwarf2out_vms_end_prologue,
5621   dwarf2out_vms_begin_epilogue,
5622 #else
5623   debug_nothing_int_charstar,
5624   debug_nothing_int_charstar,
5625 #endif
5626   dwarf2out_end_epilogue,
5627   dwarf2out_begin_function,
5628   debug_nothing_int,            /* end_function */
5629   dwarf2out_function_decl,      /* function_decl */
5630   dwarf2out_global_decl,
5631   dwarf2out_type_decl,          /* type_decl */
5632   dwarf2out_imported_module_or_decl,
5633   debug_nothing_tree,           /* deferred_inline_function */
5634   /* The DWARF 2 backend tries to reduce debugging bloat by not
5635      emitting the abstract description of inline functions until
5636      something tries to reference them.  */
5637   dwarf2out_abstract_function,  /* outlining_inline_function */
5638   debug_nothing_rtx,            /* label */
5639   debug_nothing_int,            /* handle_pch */
5640   dwarf2out_var_location,
5641   dwarf2out_switch_text_section,
5642   dwarf2out_direct_call,
5643   dwarf2out_virtual_call_token,
5644   dwarf2out_copy_call_info,
5645   dwarf2out_virtual_call,
5646   dwarf2out_set_name,
5647   1                             /* start_end_main_source_file */
5648 };
5649 \f
5650 /* NOTE: In the comments in this file, many references are made to
5651    "Debugging Information Entries".  This term is abbreviated as `DIE'
5652    throughout the remainder of this file.  */
5653
5654 /* An internal representation of the DWARF output is built, and then
5655    walked to generate the DWARF debugging info.  The walk of the internal
5656    representation is done after the entire program has been compiled.
5657    The types below are used to describe the internal representation.  */
5658
5659 /* Various DIE's use offsets relative to the beginning of the
5660    .debug_info section to refer to each other.  */
5661
5662 typedef long int dw_offset;
5663
5664 /* Define typedefs here to avoid circular dependencies.  */
5665
5666 typedef struct dw_attr_struct *dw_attr_ref;
5667 typedef struct dw_line_info_struct *dw_line_info_ref;
5668 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5669 typedef struct pubname_struct *pubname_ref;
5670 typedef struct dw_ranges_struct *dw_ranges_ref;
5671 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5672 typedef struct comdat_type_struct *comdat_type_node_ref;
5673
5674 /* Each entry in the line_info_table maintains the file and
5675    line number associated with the label generated for that
5676    entry.  The label gives the PC value associated with
5677    the line number entry.  */
5678
5679 typedef struct GTY(()) dw_line_info_struct {
5680   unsigned long dw_file_num;
5681   unsigned long dw_line_num;
5682 }
5683 dw_line_info_entry;
5684
5685 /* Line information for functions in separate sections; each one gets its
5686    own sequence.  */
5687 typedef struct GTY(()) dw_separate_line_info_struct {
5688   unsigned long dw_file_num;
5689   unsigned long dw_line_num;
5690   unsigned long function;
5691 }
5692 dw_separate_line_info_entry;
5693
5694 /* Each DIE attribute has a field specifying the attribute kind,
5695    a link to the next attribute in the chain, and an attribute value.
5696    Attributes are typically linked below the DIE they modify.  */
5697
5698 typedef struct GTY(()) dw_attr_struct {
5699   enum dwarf_attribute dw_attr;
5700   dw_val_node dw_attr_val;
5701 }
5702 dw_attr_node;
5703
5704 DEF_VEC_O(dw_attr_node);
5705 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5706
5707 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5708    The children of each node form a circular list linked by
5709    die_sib.  die_child points to the node *before* the "first" child node.  */
5710
5711 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5712   union die_symbol_or_type_node
5713     {
5714       char * GTY ((tag ("0"))) die_symbol;
5715       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5716     }
5717   GTY ((desc ("dwarf_version >= 4"))) die_id;
5718   VEC(dw_attr_node,gc) * die_attr;
5719   dw_die_ref die_parent;
5720   dw_die_ref die_child;
5721   dw_die_ref die_sib;
5722   dw_die_ref die_definition; /* ref from a specification to its definition */
5723   dw_offset die_offset;
5724   unsigned long die_abbrev;
5725   int die_mark;
5726   /* Die is used and must not be pruned as unused.  */
5727   int die_perennial_p;
5728   unsigned int decl_id;
5729   enum dwarf_tag die_tag;
5730 }
5731 die_node;
5732
5733 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5734 #define FOR_EACH_CHILD(die, c, expr) do {       \
5735   c = die->die_child;                           \
5736   if (c) do {                                   \
5737     c = c->die_sib;                             \
5738     expr;                                       \
5739   } while (c != die->die_child);                \
5740 } while (0)
5741
5742 /* The pubname structure */
5743
5744 typedef struct GTY(()) pubname_struct {
5745   dw_die_ref die;
5746   const char *name;
5747 }
5748 pubname_entry;
5749
5750 DEF_VEC_O(pubname_entry);
5751 DEF_VEC_ALLOC_O(pubname_entry, gc);
5752
5753 struct GTY(()) dw_ranges_struct {
5754   /* If this is positive, it's a block number, otherwise it's a
5755      bitwise-negated index into dw_ranges_by_label.  */
5756   int num;
5757 };
5758
5759 struct GTY(()) dw_ranges_by_label_struct {
5760   const char *begin;
5761   const char *end;
5762 };
5763
5764 /* The comdat type node structure.  */
5765 typedef struct GTY(()) comdat_type_struct
5766 {
5767   dw_die_ref root_die;
5768   dw_die_ref type_die;
5769   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5770   struct comdat_type_struct *next;
5771 }
5772 comdat_type_node;
5773
5774 /* The limbo die list structure.  */
5775 typedef struct GTY(()) limbo_die_struct {
5776   dw_die_ref die;
5777   tree created_for;
5778   struct limbo_die_struct *next;
5779 }
5780 limbo_die_node;
5781
5782 typedef struct GTY(()) skeleton_chain_struct
5783 {
5784   dw_die_ref old_die;
5785   dw_die_ref new_die;
5786   struct skeleton_chain_struct *parent;
5787 }
5788 skeleton_chain_node;
5789
5790 /* How to start an assembler comment.  */
5791 #ifndef ASM_COMMENT_START
5792 #define ASM_COMMENT_START ";#"
5793 #endif
5794
5795 /* Define a macro which returns nonzero for a TYPE_DECL which was
5796    implicitly generated for a tagged type.
5797
5798    Note that unlike the gcc front end (which generates a NULL named
5799    TYPE_DECL node for each complete tagged type, each array type, and
5800    each function type node created) the g++ front end generates a
5801    _named_ TYPE_DECL node for each tagged type node created.
5802    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5803    generate a DW_TAG_typedef DIE for them.  */
5804
5805 #define TYPE_DECL_IS_STUB(decl)                         \
5806   (DECL_NAME (decl) == NULL_TREE                        \
5807    || (DECL_ARTIFICIAL (decl)                           \
5808        && is_tagged_type (TREE_TYPE (decl))             \
5809        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5810            /* This is necessary for stub decls that     \
5811               appear in nested inline functions.  */    \
5812            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5813                && (decl_ultimate_origin (decl)          \
5814                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5815
5816 /* Information concerning the compilation unit's programming
5817    language, and compiler version.  */
5818
5819 /* Fixed size portion of the DWARF compilation unit header.  */
5820 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5821   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5822
5823 /* Fixed size portion of the DWARF comdat type unit header.  */
5824 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5825   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5826    + DWARF_OFFSET_SIZE)
5827
5828 /* Fixed size portion of public names info.  */
5829 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5830
5831 /* Fixed size portion of the address range info.  */
5832 #define DWARF_ARANGES_HEADER_SIZE                                       \
5833   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5834                 DWARF2_ADDR_SIZE * 2)                                   \
5835    - DWARF_INITIAL_LENGTH_SIZE)
5836
5837 /* Size of padding portion in the address range info.  It must be
5838    aligned to twice the pointer size.  */
5839 #define DWARF_ARANGES_PAD_SIZE \
5840   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5841                 DWARF2_ADDR_SIZE * 2)                              \
5842    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5843
5844 /* Use assembler line directives if available.  */
5845 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5846 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5847 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5848 #else
5849 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5850 #endif
5851 #endif
5852
5853 /* Minimum line offset in a special line info. opcode.
5854    This value was chosen to give a reasonable range of values.  */
5855 #define DWARF_LINE_BASE  -10
5856
5857 /* First special line opcode - leave room for the standard opcodes.  */
5858 #define DWARF_LINE_OPCODE_BASE  10
5859
5860 /* Range of line offsets in a special line info. opcode.  */
5861 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5862
5863 /* Flag that indicates the initial value of the is_stmt_start flag.
5864    In the present implementation, we do not mark any lines as
5865    the beginning of a source statement, because that information
5866    is not made available by the GCC front-end.  */
5867 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5868
5869 /* Maximum number of operations per instruction bundle.  */
5870 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5871 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5872 #endif
5873
5874 /* This location is used by calc_die_sizes() to keep track
5875    the offset of each DIE within the .debug_info section.  */
5876 static unsigned long next_die_offset;
5877
5878 /* Record the root of the DIE's built for the current compilation unit.  */
5879 static GTY(()) dw_die_ref single_comp_unit_die;
5880
5881 /* A list of type DIEs that have been separated into comdat sections.  */
5882 static GTY(()) comdat_type_node *comdat_type_list;
5883
5884 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5885 static GTY(()) limbo_die_node *limbo_die_list;
5886
5887 /* A list of DIEs for which we may have to generate
5888    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5889 static GTY(()) limbo_die_node *deferred_asm_name;
5890
5891 /* Filenames referenced by this compilation unit.  */
5892 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5893
5894 /* A hash table of references to DIE's that describe declarations.
5895    The key is a DECL_UID() which is a unique number identifying each decl.  */
5896 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5897
5898 /* A hash table of references to DIE's that describe COMMON blocks.
5899    The key is DECL_UID() ^ die_parent.  */
5900 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5901
5902 typedef struct GTY(()) die_arg_entry_struct {
5903     dw_die_ref die;
5904     tree arg;
5905 } die_arg_entry;
5906
5907 DEF_VEC_O(die_arg_entry);
5908 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5909
5910 /* Node of the variable location list.  */
5911 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5912   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5913      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5914      in mode of the EXPR_LIST node and first EXPR_LIST operand
5915      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5916      location or NULL for padding.  For larger bitsizes,
5917      mode is 0 and first operand is a CONCAT with bitsize
5918      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5919      NULL as second operand.  */
5920   rtx GTY (()) loc;
5921   const char * GTY (()) label;
5922   struct var_loc_node * GTY (()) next;
5923 };
5924
5925 /* Variable location list.  */
5926 struct GTY (()) var_loc_list_def {
5927   struct var_loc_node * GTY (()) first;
5928
5929   /* Pointer to the last but one or last element of the
5930      chained list.  If the list is empty, both first and
5931      last are NULL, if the list contains just one node
5932      or the last node certainly is not redundant, it points
5933      to the last node, otherwise points to the last but one.
5934      Do not mark it for GC because it is marked through the chain.  */
5935   struct var_loc_node * GTY ((skip ("%h"))) last;
5936
5937   /* DECL_UID of the variable decl.  */
5938   unsigned int decl_id;
5939 };
5940 typedef struct var_loc_list_def var_loc_list;
5941
5942
5943 /* Table of decl location linked lists.  */
5944 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5945
5946 /* A pointer to the base of a list of references to DIE's that
5947    are uniquely identified by their tag, presence/absence of
5948    children DIE's, and list of attribute/value pairs.  */
5949 static GTY((length ("abbrev_die_table_allocated")))
5950   dw_die_ref *abbrev_die_table;
5951
5952 /* Number of elements currently allocated for abbrev_die_table.  */
5953 static GTY(()) unsigned abbrev_die_table_allocated;
5954
5955 /* Number of elements in type_die_table currently in use.  */
5956 static GTY(()) unsigned abbrev_die_table_in_use;
5957
5958 /* Size (in elements) of increments by which we may expand the
5959    abbrev_die_table.  */
5960 #define ABBREV_DIE_TABLE_INCREMENT 256
5961
5962 /* A pointer to the base of a table that contains line information
5963    for each source code line in .text in the compilation unit.  */
5964 static GTY((length ("line_info_table_allocated")))
5965      dw_line_info_ref line_info_table;
5966
5967 /* Number of elements currently allocated for line_info_table.  */
5968 static GTY(()) unsigned line_info_table_allocated;
5969
5970 /* Number of elements in line_info_table currently in use.  */
5971 static GTY(()) unsigned line_info_table_in_use;
5972
5973 /* A pointer to the base of a table that contains line information
5974    for each source code line outside of .text in the compilation unit.  */
5975 static GTY ((length ("separate_line_info_table_allocated")))
5976      dw_separate_line_info_ref separate_line_info_table;
5977
5978 /* Number of elements currently allocated for separate_line_info_table.  */
5979 static GTY(()) unsigned separate_line_info_table_allocated;
5980
5981 /* Number of elements in separate_line_info_table currently in use.  */
5982 static GTY(()) unsigned separate_line_info_table_in_use;
5983
5984 /* Size (in elements) of increments by which we may expand the
5985    line_info_table.  */
5986 #define LINE_INFO_TABLE_INCREMENT 1024
5987
5988 /* A pointer to the base of a table that contains a list of publicly
5989    accessible names.  */
5990 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5991
5992 /* A pointer to the base of a table that contains a list of publicly
5993    accessible types.  */
5994 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5995
5996 /* Array of dies for which we should generate .debug_arange info.  */
5997 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5998
5999 /* Number of elements currently allocated for arange_table.  */
6000 static GTY(()) unsigned arange_table_allocated;
6001
6002 /* Number of elements in arange_table currently in use.  */
6003 static GTY(()) unsigned arange_table_in_use;
6004
6005 /* Size (in elements) of increments by which we may expand the
6006    arange_table.  */
6007 #define ARANGE_TABLE_INCREMENT 64
6008
6009 /* Array of dies for which we should generate .debug_ranges info.  */
6010 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6011
6012 /* Number of elements currently allocated for ranges_table.  */
6013 static GTY(()) unsigned ranges_table_allocated;
6014
6015 /* Number of elements in ranges_table currently in use.  */
6016 static GTY(()) unsigned ranges_table_in_use;
6017
6018 /* Array of pairs of labels referenced in ranges_table.  */
6019 static GTY ((length ("ranges_by_label_allocated")))
6020      dw_ranges_by_label_ref ranges_by_label;
6021
6022 /* Number of elements currently allocated for ranges_by_label.  */
6023 static GTY(()) unsigned ranges_by_label_allocated;
6024
6025 /* Number of elements in ranges_by_label currently in use.  */
6026 static GTY(()) unsigned ranges_by_label_in_use;
6027
6028 /* Size (in elements) of increments by which we may expand the
6029    ranges_table.  */
6030 #define RANGES_TABLE_INCREMENT 64
6031
6032 /* Whether we have location lists that need outputting */
6033 static GTY(()) bool have_location_lists;
6034
6035 /* Unique label counter.  */
6036 static GTY(()) unsigned int loclabel_num;
6037
6038 /* Unique label counter for point-of-call tables.  */
6039 static GTY(()) unsigned int poc_label_num;
6040
6041 /* The direct call table structure.  */
6042
6043 typedef struct GTY(()) dcall_struct {
6044   unsigned int poc_label_num;
6045   tree poc_decl;
6046   dw_die_ref targ_die;
6047 }
6048 dcall_entry;
6049
6050 DEF_VEC_O(dcall_entry);
6051 DEF_VEC_ALLOC_O(dcall_entry, gc);
6052
6053 /* The virtual call table structure.  */
6054
6055 typedef struct GTY(()) vcall_struct {
6056   unsigned int poc_label_num;
6057   unsigned int vtable_slot;
6058 }
6059 vcall_entry;
6060
6061 DEF_VEC_O(vcall_entry);
6062 DEF_VEC_ALLOC_O(vcall_entry, gc);
6063
6064 /* Pointers to the direct and virtual call tables.  */
6065 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6066 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6067
6068 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6069
6070 struct GTY (()) vcall_insn {
6071   int insn_uid;
6072   unsigned int vtable_slot;
6073 };
6074
6075 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6076
6077 /* Record whether the function being analyzed contains inlined functions.  */
6078 static int current_function_has_inlines;
6079
6080 /* The last file entry emitted by maybe_emit_file().  */
6081 static GTY(()) struct dwarf_file_data * last_emitted_file;
6082
6083 /* Number of internal labels generated by gen_internal_sym().  */
6084 static GTY(()) int label_num;
6085
6086 /* Cached result of previous call to lookup_filename.  */
6087 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6088
6089 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6090
6091 /* Offset from the "steady-state frame pointer" to the frame base,
6092    within the current function.  */
6093 static HOST_WIDE_INT frame_pointer_fb_offset;
6094
6095 /* Forward declarations for functions defined in this file.  */
6096
6097 static int is_pseudo_reg (const_rtx);
6098 static tree type_main_variant (tree);
6099 static int is_tagged_type (const_tree);
6100 static const char *dwarf_tag_name (unsigned);
6101 static const char *dwarf_attr_name (unsigned);
6102 static const char *dwarf_form_name (unsigned);
6103 static tree decl_ultimate_origin (const_tree);
6104 static tree decl_class_context (tree);
6105 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6106 static inline enum dw_val_class AT_class (dw_attr_ref);
6107 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6108 static inline unsigned AT_flag (dw_attr_ref);
6109 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6110 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6111 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6112 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6113 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6114                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6115 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6116                                unsigned int, unsigned char *);
6117 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6118 static hashval_t debug_str_do_hash (const void *);
6119 static int debug_str_eq (const void *, const void *);
6120 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6121 static inline const char *AT_string (dw_attr_ref);
6122 static enum dwarf_form AT_string_form (dw_attr_ref);
6123 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6124 static void add_AT_specification (dw_die_ref, dw_die_ref);
6125 static inline dw_die_ref AT_ref (dw_attr_ref);
6126 static inline int AT_ref_external (dw_attr_ref);
6127 static inline void set_AT_ref_external (dw_attr_ref, int);
6128 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6129 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6130 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6131 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6132                              dw_loc_list_ref);
6133 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6134 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6135 static inline rtx AT_addr (dw_attr_ref);
6136 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6137 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6138 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6139 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6140                            unsigned HOST_WIDE_INT);
6141 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6142                                unsigned long);
6143 static inline const char *AT_lbl (dw_attr_ref);
6144 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6145 static const char *get_AT_low_pc (dw_die_ref);
6146 static const char *get_AT_hi_pc (dw_die_ref);
6147 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6148 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6149 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6150 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6151 static bool is_cxx (void);
6152 static bool is_fortran (void);
6153 static bool is_ada (void);
6154 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6155 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6156 static void add_child_die (dw_die_ref, dw_die_ref);
6157 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6158 static dw_die_ref lookup_type_die (tree);
6159 static void equate_type_number_to_die (tree, dw_die_ref);
6160 static hashval_t decl_die_table_hash (const void *);
6161 static int decl_die_table_eq (const void *, const void *);
6162 static dw_die_ref lookup_decl_die (tree);
6163 static hashval_t common_block_die_table_hash (const void *);
6164 static int common_block_die_table_eq (const void *, const void *);
6165 static hashval_t decl_loc_table_hash (const void *);
6166 static int decl_loc_table_eq (const void *, const void *);
6167 static var_loc_list *lookup_decl_loc (const_tree);
6168 static void equate_decl_number_to_die (tree, dw_die_ref);
6169 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6170 static void print_spaces (FILE *);
6171 static void print_die (dw_die_ref, FILE *);
6172 static void print_dwarf_line_table (FILE *);
6173 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6174 static dw_die_ref pop_compile_unit (dw_die_ref);
6175 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6176 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6177 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6178 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6179 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6180 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6181 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6182                                    struct md5_ctx *, int *);
6183 struct checksum_attributes;
6184 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6185 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6186 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6187 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6188 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6189 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6190 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6191 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6192 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6193 static void compute_section_prefix (dw_die_ref);
6194 static int is_type_die (dw_die_ref);
6195 static int is_comdat_die (dw_die_ref);
6196 static int is_symbol_die (dw_die_ref);
6197 static void assign_symbol_names (dw_die_ref);
6198 static void break_out_includes (dw_die_ref);
6199 static int is_declaration_die (dw_die_ref);
6200 static int should_move_die_to_comdat (dw_die_ref);
6201 static dw_die_ref clone_as_declaration (dw_die_ref);
6202 static dw_die_ref clone_die (dw_die_ref);
6203 static dw_die_ref clone_tree (dw_die_ref);
6204 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6205 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6206 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6207 static dw_die_ref generate_skeleton (dw_die_ref);
6208 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6209                                                          dw_die_ref);
6210 static void break_out_comdat_types (dw_die_ref);
6211 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6212 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6213 static void copy_decls_for_unworthy_types (dw_die_ref);
6214
6215 static hashval_t htab_cu_hash (const void *);
6216 static int htab_cu_eq (const void *, const void *);
6217 static void htab_cu_del (void *);
6218 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6219 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6220 static void add_sibling_attributes (dw_die_ref);
6221 static void build_abbrev_table (dw_die_ref);
6222 static void output_location_lists (dw_die_ref);
6223 static int constant_size (unsigned HOST_WIDE_INT);
6224 static unsigned long size_of_die (dw_die_ref);
6225 static void calc_die_sizes (dw_die_ref);
6226 static void mark_dies (dw_die_ref);
6227 static void unmark_dies (dw_die_ref);
6228 static void unmark_all_dies (dw_die_ref);
6229 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6230 static unsigned long size_of_aranges (void);
6231 static enum dwarf_form value_format (dw_attr_ref);
6232 static void output_value_format (dw_attr_ref);
6233 static void output_abbrev_section (void);
6234 static void output_die_symbol (dw_die_ref);
6235 static void output_die (dw_die_ref);
6236 static void output_compilation_unit_header (void);
6237 static void output_comp_unit (dw_die_ref, int);
6238 static void output_comdat_type_unit (comdat_type_node *);
6239 static const char *dwarf2_name (tree, int);
6240 static void add_pubname (tree, dw_die_ref);
6241 static void add_pubname_string (const char *, dw_die_ref);
6242 static void add_pubtype (tree, dw_die_ref);
6243 static void output_pubnames (VEC (pubname_entry,gc) *);
6244 static void add_arange (tree, dw_die_ref);
6245 static void output_aranges (void);
6246 static unsigned int add_ranges_num (int);
6247 static unsigned int add_ranges (const_tree);
6248 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6249                                   bool *);
6250 static void output_ranges (void);
6251 static void output_line_info (void);
6252 static void output_file_names (void);
6253 static dw_die_ref base_type_die (tree);
6254 static int is_base_type (tree);
6255 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6256 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6257 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6258 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6259 static int type_is_enum (const_tree);
6260 static unsigned int dbx_reg_number (const_rtx);
6261 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6262 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6263 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6264                                                 enum var_init_status);
6265 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6266                                                      enum var_init_status);
6267 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6268                                          enum var_init_status);
6269 static int is_based_loc (const_rtx);
6270 static int resolve_one_addr (rtx *, void *);
6271 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6272                                                enum var_init_status);
6273 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6274                                         enum var_init_status);
6275 static dw_loc_list_ref loc_list_from_tree (tree, int);
6276 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6277 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6278 static tree field_type (const_tree);
6279 static unsigned int simple_type_align_in_bits (const_tree);
6280 static unsigned int simple_decl_align_in_bits (const_tree);
6281 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6282 static HOST_WIDE_INT field_byte_offset (const_tree);
6283 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6284                                          dw_loc_list_ref);
6285 static void add_data_member_location_attribute (dw_die_ref, tree);
6286 static bool add_const_value_attribute (dw_die_ref, rtx);
6287 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6288 static void insert_double (double_int, unsigned char *);
6289 static void insert_float (const_rtx, unsigned char *);
6290 static rtx rtl_for_decl_location (tree);
6291 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6292                                                    enum dwarf_attribute);
6293 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6294 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6295 static void add_name_attribute (dw_die_ref, const char *);
6296 static void add_comp_dir_attribute (dw_die_ref);
6297 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6298 static void add_subscript_info (dw_die_ref, tree, bool);
6299 static void add_byte_size_attribute (dw_die_ref, tree);
6300 static void add_bit_offset_attribute (dw_die_ref, tree);
6301 static void add_bit_size_attribute (dw_die_ref, tree);
6302 static void add_prototyped_attribute (dw_die_ref, tree);
6303 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6304 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6305 static void add_src_coords_attributes (dw_die_ref, tree);
6306 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6307 static void push_decl_scope (tree);
6308 static void pop_decl_scope (void);
6309 static dw_die_ref scope_die_for (tree, dw_die_ref);
6310 static inline int local_scope_p (dw_die_ref);
6311 static inline int class_scope_p (dw_die_ref);
6312 static inline int class_or_namespace_scope_p (dw_die_ref);
6313 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6314 static void add_calling_convention_attribute (dw_die_ref, tree);
6315 static const char *type_tag (const_tree);
6316 static tree member_declared_type (const_tree);
6317 #if 0
6318 static const char *decl_start_label (tree);
6319 #endif
6320 static void gen_array_type_die (tree, dw_die_ref);
6321 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6322 #if 0
6323 static void gen_entry_point_die (tree, dw_die_ref);
6324 #endif
6325 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6326 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6327 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6328 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6329 static void gen_formal_types_die (tree, dw_die_ref);
6330 static void gen_subprogram_die (tree, dw_die_ref);
6331 static void gen_variable_die (tree, tree, dw_die_ref);
6332 static void gen_const_die (tree, dw_die_ref);
6333 static void gen_label_die (tree, dw_die_ref);
6334 static void gen_lexical_block_die (tree, dw_die_ref, int);
6335 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6336 static void gen_field_die (tree, dw_die_ref);
6337 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6338 static dw_die_ref gen_compile_unit_die (const char *);
6339 static void gen_inheritance_die (tree, tree, dw_die_ref);
6340 static void gen_member_die (tree, dw_die_ref);
6341 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6342                                                 enum debug_info_usage);
6343 static void gen_subroutine_type_die (tree, dw_die_ref);
6344 static void gen_typedef_die (tree, dw_die_ref);
6345 static void gen_type_die (tree, dw_die_ref);
6346 static void gen_block_die (tree, dw_die_ref, int);
6347 static void decls_for_scope (tree, dw_die_ref, int);
6348 static int is_redundant_typedef (const_tree);
6349 static bool is_naming_typedef_decl (const_tree);
6350 static inline dw_die_ref get_context_die (tree);
6351 static void gen_namespace_die (tree, dw_die_ref);
6352 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6353 static dw_die_ref force_decl_die (tree);
6354 static dw_die_ref force_type_die (tree);
6355 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6356 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6357 static struct dwarf_file_data * lookup_filename (const char *);
6358 static void retry_incomplete_types (void);
6359 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6360 static void gen_generic_params_dies (tree);
6361 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6362 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6363 static void splice_child_die (dw_die_ref, dw_die_ref);
6364 static int file_info_cmp (const void *, const void *);
6365 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6366                                      const char *, const char *);
6367 static void output_loc_list (dw_loc_list_ref);
6368 static char *gen_internal_sym (const char *);
6369
6370 static void prune_unmark_dies (dw_die_ref);
6371 static void prune_unused_types_mark (dw_die_ref, int);
6372 static void prune_unused_types_walk (dw_die_ref);
6373 static void prune_unused_types_walk_attribs (dw_die_ref);
6374 static void prune_unused_types_prune (dw_die_ref);
6375 static void prune_unused_types (void);
6376 static int maybe_emit_file (struct dwarf_file_data *fd);
6377 static inline const char *AT_vms_delta1 (dw_attr_ref);
6378 static inline const char *AT_vms_delta2 (dw_attr_ref);
6379 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6380                                      const char *, const char *);
6381 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6382 static void gen_remaining_tmpl_value_param_die_attribute (void);
6383
6384 /* Section names used to hold DWARF debugging information.  */
6385 #ifndef DEBUG_INFO_SECTION
6386 #define DEBUG_INFO_SECTION      ".debug_info"
6387 #endif
6388 #ifndef DEBUG_ABBREV_SECTION
6389 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6390 #endif
6391 #ifndef DEBUG_ARANGES_SECTION
6392 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6393 #endif
6394 #ifndef DEBUG_MACINFO_SECTION
6395 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6396 #endif
6397 #ifndef DEBUG_LINE_SECTION
6398 #define DEBUG_LINE_SECTION      ".debug_line"
6399 #endif
6400 #ifndef DEBUG_LOC_SECTION
6401 #define DEBUG_LOC_SECTION       ".debug_loc"
6402 #endif
6403 #ifndef DEBUG_PUBNAMES_SECTION
6404 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6405 #endif
6406 #ifndef DEBUG_PUBTYPES_SECTION
6407 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6408 #endif
6409 #ifndef DEBUG_DCALL_SECTION
6410 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6411 #endif
6412 #ifndef DEBUG_VCALL_SECTION
6413 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6414 #endif
6415 #ifndef DEBUG_STR_SECTION
6416 #define DEBUG_STR_SECTION       ".debug_str"
6417 #endif
6418 #ifndef DEBUG_RANGES_SECTION
6419 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6420 #endif
6421
6422 /* Standard ELF section names for compiled code and data.  */
6423 #ifndef TEXT_SECTION_NAME
6424 #define TEXT_SECTION_NAME       ".text"
6425 #endif
6426
6427 /* Section flags for .debug_str section.  */
6428 #define DEBUG_STR_SECTION_FLAGS \
6429   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6430    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6431    : SECTION_DEBUG)
6432
6433 /* Labels we insert at beginning sections we can reference instead of
6434    the section names themselves.  */
6435
6436 #ifndef TEXT_SECTION_LABEL
6437 #define TEXT_SECTION_LABEL              "Ltext"
6438 #endif
6439 #ifndef COLD_TEXT_SECTION_LABEL
6440 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6441 #endif
6442 #ifndef DEBUG_LINE_SECTION_LABEL
6443 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6444 #endif
6445 #ifndef DEBUG_INFO_SECTION_LABEL
6446 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6447 #endif
6448 #ifndef DEBUG_ABBREV_SECTION_LABEL
6449 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6450 #endif
6451 #ifndef DEBUG_LOC_SECTION_LABEL
6452 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6453 #endif
6454 #ifndef DEBUG_RANGES_SECTION_LABEL
6455 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6456 #endif
6457 #ifndef DEBUG_MACINFO_SECTION_LABEL
6458 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6459 #endif
6460
6461
6462 /* Definitions of defaults for formats and names of various special
6463    (artificial) labels which may be generated within this file (when the -g
6464    options is used and DWARF2_DEBUGGING_INFO is in effect.
6465    If necessary, these may be overridden from within the tm.h file, but
6466    typically, overriding these defaults is unnecessary.  */
6467
6468 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6469 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6470 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6471 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6472 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6473 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6474 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6475 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6476 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6477 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6478
6479 #ifndef TEXT_END_LABEL
6480 #define TEXT_END_LABEL          "Letext"
6481 #endif
6482 #ifndef COLD_END_LABEL
6483 #define COLD_END_LABEL          "Letext_cold"
6484 #endif
6485 #ifndef BLOCK_BEGIN_LABEL
6486 #define BLOCK_BEGIN_LABEL       "LBB"
6487 #endif
6488 #ifndef BLOCK_END_LABEL
6489 #define BLOCK_END_LABEL         "LBE"
6490 #endif
6491 #ifndef LINE_CODE_LABEL
6492 #define LINE_CODE_LABEL         "LM"
6493 #endif
6494 #ifndef SEPARATE_LINE_CODE_LABEL
6495 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6496 #endif
6497
6498 \f
6499 /* Return the root of the DIE's built for the current compilation unit.  */
6500 static dw_die_ref
6501 comp_unit_die (void)
6502 {
6503   if (!single_comp_unit_die)
6504     single_comp_unit_die = gen_compile_unit_die (NULL);
6505   return single_comp_unit_die;
6506 }
6507
6508 /* We allow a language front-end to designate a function that is to be
6509    called to "demangle" any name before it is put into a DIE.  */
6510
6511 static const char *(*demangle_name_func) (const char *);
6512
6513 void
6514 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6515 {
6516   demangle_name_func = func;
6517 }
6518
6519 /* Test if rtl node points to a pseudo register.  */
6520
6521 static inline int
6522 is_pseudo_reg (const_rtx rtl)
6523 {
6524   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6525           || (GET_CODE (rtl) == SUBREG
6526               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6527 }
6528
6529 /* Return a reference to a type, with its const and volatile qualifiers
6530    removed.  */
6531
6532 static inline tree
6533 type_main_variant (tree type)
6534 {
6535   type = TYPE_MAIN_VARIANT (type);
6536
6537   /* ??? There really should be only one main variant among any group of
6538      variants of a given type (and all of the MAIN_VARIANT values for all
6539      members of the group should point to that one type) but sometimes the C
6540      front-end messes this up for array types, so we work around that bug
6541      here.  */
6542   if (TREE_CODE (type) == ARRAY_TYPE)
6543     while (type != TYPE_MAIN_VARIANT (type))
6544       type = TYPE_MAIN_VARIANT (type);
6545
6546   return type;
6547 }
6548
6549 /* Return nonzero if the given type node represents a tagged type.  */
6550
6551 static inline int
6552 is_tagged_type (const_tree type)
6553 {
6554   enum tree_code code = TREE_CODE (type);
6555
6556   return (code == RECORD_TYPE || code == UNION_TYPE
6557           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6558 }
6559
6560 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6561
6562 static void
6563 get_ref_die_offset_label (char *label, dw_die_ref ref)
6564 {
6565   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6566 }
6567
6568 /* Convert a DIE tag into its string name.  */
6569
6570 static const char *
6571 dwarf_tag_name (unsigned int tag)
6572 {
6573   switch (tag)
6574     {
6575     case DW_TAG_padding:
6576       return "DW_TAG_padding";
6577     case DW_TAG_array_type:
6578       return "DW_TAG_array_type";
6579     case DW_TAG_class_type:
6580       return "DW_TAG_class_type";
6581     case DW_TAG_entry_point:
6582       return "DW_TAG_entry_point";
6583     case DW_TAG_enumeration_type:
6584       return "DW_TAG_enumeration_type";
6585     case DW_TAG_formal_parameter:
6586       return "DW_TAG_formal_parameter";
6587     case DW_TAG_imported_declaration:
6588       return "DW_TAG_imported_declaration";
6589     case DW_TAG_label:
6590       return "DW_TAG_label";
6591     case DW_TAG_lexical_block:
6592       return "DW_TAG_lexical_block";
6593     case DW_TAG_member:
6594       return "DW_TAG_member";
6595     case DW_TAG_pointer_type:
6596       return "DW_TAG_pointer_type";
6597     case DW_TAG_reference_type:
6598       return "DW_TAG_reference_type";
6599     case DW_TAG_compile_unit:
6600       return "DW_TAG_compile_unit";
6601     case DW_TAG_string_type:
6602       return "DW_TAG_string_type";
6603     case DW_TAG_structure_type:
6604       return "DW_TAG_structure_type";
6605     case DW_TAG_subroutine_type:
6606       return "DW_TAG_subroutine_type";
6607     case DW_TAG_typedef:
6608       return "DW_TAG_typedef";
6609     case DW_TAG_union_type:
6610       return "DW_TAG_union_type";
6611     case DW_TAG_unspecified_parameters:
6612       return "DW_TAG_unspecified_parameters";
6613     case DW_TAG_variant:
6614       return "DW_TAG_variant";
6615     case DW_TAG_common_block:
6616       return "DW_TAG_common_block";
6617     case DW_TAG_common_inclusion:
6618       return "DW_TAG_common_inclusion";
6619     case DW_TAG_inheritance:
6620       return "DW_TAG_inheritance";
6621     case DW_TAG_inlined_subroutine:
6622       return "DW_TAG_inlined_subroutine";
6623     case DW_TAG_module:
6624       return "DW_TAG_module";
6625     case DW_TAG_ptr_to_member_type:
6626       return "DW_TAG_ptr_to_member_type";
6627     case DW_TAG_set_type:
6628       return "DW_TAG_set_type";
6629     case DW_TAG_subrange_type:
6630       return "DW_TAG_subrange_type";
6631     case DW_TAG_with_stmt:
6632       return "DW_TAG_with_stmt";
6633     case DW_TAG_access_declaration:
6634       return "DW_TAG_access_declaration";
6635     case DW_TAG_base_type:
6636       return "DW_TAG_base_type";
6637     case DW_TAG_catch_block:
6638       return "DW_TAG_catch_block";
6639     case DW_TAG_const_type:
6640       return "DW_TAG_const_type";
6641     case DW_TAG_constant:
6642       return "DW_TAG_constant";
6643     case DW_TAG_enumerator:
6644       return "DW_TAG_enumerator";
6645     case DW_TAG_file_type:
6646       return "DW_TAG_file_type";
6647     case DW_TAG_friend:
6648       return "DW_TAG_friend";
6649     case DW_TAG_namelist:
6650       return "DW_TAG_namelist";
6651     case DW_TAG_namelist_item:
6652       return "DW_TAG_namelist_item";
6653     case DW_TAG_packed_type:
6654       return "DW_TAG_packed_type";
6655     case DW_TAG_subprogram:
6656       return "DW_TAG_subprogram";
6657     case DW_TAG_template_type_param:
6658       return "DW_TAG_template_type_param";
6659     case DW_TAG_template_value_param:
6660       return "DW_TAG_template_value_param";
6661     case DW_TAG_thrown_type:
6662       return "DW_TAG_thrown_type";
6663     case DW_TAG_try_block:
6664       return "DW_TAG_try_block";
6665     case DW_TAG_variant_part:
6666       return "DW_TAG_variant_part";
6667     case DW_TAG_variable:
6668       return "DW_TAG_variable";
6669     case DW_TAG_volatile_type:
6670       return "DW_TAG_volatile_type";
6671     case DW_TAG_dwarf_procedure:
6672       return "DW_TAG_dwarf_procedure";
6673     case DW_TAG_restrict_type:
6674       return "DW_TAG_restrict_type";
6675     case DW_TAG_interface_type:
6676       return "DW_TAG_interface_type";
6677     case DW_TAG_namespace:
6678       return "DW_TAG_namespace";
6679     case DW_TAG_imported_module:
6680       return "DW_TAG_imported_module";
6681     case DW_TAG_unspecified_type:
6682       return "DW_TAG_unspecified_type";
6683     case DW_TAG_partial_unit:
6684       return "DW_TAG_partial_unit";
6685     case DW_TAG_imported_unit:
6686       return "DW_TAG_imported_unit";
6687     case DW_TAG_condition:
6688       return "DW_TAG_condition";
6689     case DW_TAG_shared_type:
6690       return "DW_TAG_shared_type";
6691     case DW_TAG_type_unit:
6692       return "DW_TAG_type_unit";
6693     case DW_TAG_rvalue_reference_type:
6694       return "DW_TAG_rvalue_reference_type";
6695     case DW_TAG_template_alias:
6696       return "DW_TAG_template_alias";
6697     case DW_TAG_GNU_template_parameter_pack:
6698       return "DW_TAG_GNU_template_parameter_pack";
6699     case DW_TAG_GNU_formal_parameter_pack:
6700       return "DW_TAG_GNU_formal_parameter_pack";
6701     case DW_TAG_MIPS_loop:
6702       return "DW_TAG_MIPS_loop";
6703     case DW_TAG_format_label:
6704       return "DW_TAG_format_label";
6705     case DW_TAG_function_template:
6706       return "DW_TAG_function_template";
6707     case DW_TAG_class_template:
6708       return "DW_TAG_class_template";
6709     case DW_TAG_GNU_BINCL:
6710       return "DW_TAG_GNU_BINCL";
6711     case DW_TAG_GNU_EINCL:
6712       return "DW_TAG_GNU_EINCL";
6713     case DW_TAG_GNU_template_template_param:
6714       return "DW_TAG_GNU_template_template_param";
6715     default:
6716       return "DW_TAG_<unknown>";
6717     }
6718 }
6719
6720 /* Convert a DWARF attribute code into its string name.  */
6721
6722 static const char *
6723 dwarf_attr_name (unsigned int attr)
6724 {
6725   switch (attr)
6726     {
6727     case DW_AT_sibling:
6728       return "DW_AT_sibling";
6729     case DW_AT_location:
6730       return "DW_AT_location";
6731     case DW_AT_name:
6732       return "DW_AT_name";
6733     case DW_AT_ordering:
6734       return "DW_AT_ordering";
6735     case DW_AT_subscr_data:
6736       return "DW_AT_subscr_data";
6737     case DW_AT_byte_size:
6738       return "DW_AT_byte_size";
6739     case DW_AT_bit_offset:
6740       return "DW_AT_bit_offset";
6741     case DW_AT_bit_size:
6742       return "DW_AT_bit_size";
6743     case DW_AT_element_list:
6744       return "DW_AT_element_list";
6745     case DW_AT_stmt_list:
6746       return "DW_AT_stmt_list";
6747     case DW_AT_low_pc:
6748       return "DW_AT_low_pc";
6749     case DW_AT_high_pc:
6750       return "DW_AT_high_pc";
6751     case DW_AT_language:
6752       return "DW_AT_language";
6753     case DW_AT_member:
6754       return "DW_AT_member";
6755     case DW_AT_discr:
6756       return "DW_AT_discr";
6757     case DW_AT_discr_value:
6758       return "DW_AT_discr_value";
6759     case DW_AT_visibility:
6760       return "DW_AT_visibility";
6761     case DW_AT_import:
6762       return "DW_AT_import";
6763     case DW_AT_string_length:
6764       return "DW_AT_string_length";
6765     case DW_AT_common_reference:
6766       return "DW_AT_common_reference";
6767     case DW_AT_comp_dir:
6768       return "DW_AT_comp_dir";
6769     case DW_AT_const_value:
6770       return "DW_AT_const_value";
6771     case DW_AT_containing_type:
6772       return "DW_AT_containing_type";
6773     case DW_AT_default_value:
6774       return "DW_AT_default_value";
6775     case DW_AT_inline:
6776       return "DW_AT_inline";
6777     case DW_AT_is_optional:
6778       return "DW_AT_is_optional";
6779     case DW_AT_lower_bound:
6780       return "DW_AT_lower_bound";
6781     case DW_AT_producer:
6782       return "DW_AT_producer";
6783     case DW_AT_prototyped:
6784       return "DW_AT_prototyped";
6785     case DW_AT_return_addr:
6786       return "DW_AT_return_addr";
6787     case DW_AT_start_scope:
6788       return "DW_AT_start_scope";
6789     case DW_AT_bit_stride:
6790       return "DW_AT_bit_stride";
6791     case DW_AT_upper_bound:
6792       return "DW_AT_upper_bound";
6793     case DW_AT_abstract_origin:
6794       return "DW_AT_abstract_origin";
6795     case DW_AT_accessibility:
6796       return "DW_AT_accessibility";
6797     case DW_AT_address_class:
6798       return "DW_AT_address_class";
6799     case DW_AT_artificial:
6800       return "DW_AT_artificial";
6801     case DW_AT_base_types:
6802       return "DW_AT_base_types";
6803     case DW_AT_calling_convention:
6804       return "DW_AT_calling_convention";
6805     case DW_AT_count:
6806       return "DW_AT_count";
6807     case DW_AT_data_member_location:
6808       return "DW_AT_data_member_location";
6809     case DW_AT_decl_column:
6810       return "DW_AT_decl_column";
6811     case DW_AT_decl_file:
6812       return "DW_AT_decl_file";
6813     case DW_AT_decl_line:
6814       return "DW_AT_decl_line";
6815     case DW_AT_declaration:
6816       return "DW_AT_declaration";
6817     case DW_AT_discr_list:
6818       return "DW_AT_discr_list";
6819     case DW_AT_encoding:
6820       return "DW_AT_encoding";
6821     case DW_AT_external:
6822       return "DW_AT_external";
6823     case DW_AT_explicit:
6824       return "DW_AT_explicit";
6825     case DW_AT_frame_base:
6826       return "DW_AT_frame_base";
6827     case DW_AT_friend:
6828       return "DW_AT_friend";
6829     case DW_AT_identifier_case:
6830       return "DW_AT_identifier_case";
6831     case DW_AT_macro_info:
6832       return "DW_AT_macro_info";
6833     case DW_AT_namelist_items:
6834       return "DW_AT_namelist_items";
6835     case DW_AT_priority:
6836       return "DW_AT_priority";
6837     case DW_AT_segment:
6838       return "DW_AT_segment";
6839     case DW_AT_specification:
6840       return "DW_AT_specification";
6841     case DW_AT_static_link:
6842       return "DW_AT_static_link";
6843     case DW_AT_type:
6844       return "DW_AT_type";
6845     case DW_AT_use_location:
6846       return "DW_AT_use_location";
6847     case DW_AT_variable_parameter:
6848       return "DW_AT_variable_parameter";
6849     case DW_AT_virtuality:
6850       return "DW_AT_virtuality";
6851     case DW_AT_vtable_elem_location:
6852       return "DW_AT_vtable_elem_location";
6853
6854     case DW_AT_allocated:
6855       return "DW_AT_allocated";
6856     case DW_AT_associated:
6857       return "DW_AT_associated";
6858     case DW_AT_data_location:
6859       return "DW_AT_data_location";
6860     case DW_AT_byte_stride:
6861       return "DW_AT_byte_stride";
6862     case DW_AT_entry_pc:
6863       return "DW_AT_entry_pc";
6864     case DW_AT_use_UTF8:
6865       return "DW_AT_use_UTF8";
6866     case DW_AT_extension:
6867       return "DW_AT_extension";
6868     case DW_AT_ranges:
6869       return "DW_AT_ranges";
6870     case DW_AT_trampoline:
6871       return "DW_AT_trampoline";
6872     case DW_AT_call_column:
6873       return "DW_AT_call_column";
6874     case DW_AT_call_file:
6875       return "DW_AT_call_file";
6876     case DW_AT_call_line:
6877       return "DW_AT_call_line";
6878     case DW_AT_object_pointer:
6879       return "DW_AT_object_pointer";
6880
6881     case DW_AT_signature:
6882       return "DW_AT_signature";
6883     case DW_AT_main_subprogram:
6884       return "DW_AT_main_subprogram";
6885     case DW_AT_data_bit_offset:
6886       return "DW_AT_data_bit_offset";
6887     case DW_AT_const_expr:
6888       return "DW_AT_const_expr";
6889     case DW_AT_enum_class:
6890       return "DW_AT_enum_class";
6891     case DW_AT_linkage_name:
6892       return "DW_AT_linkage_name";
6893
6894     case DW_AT_MIPS_fde:
6895       return "DW_AT_MIPS_fde";
6896     case DW_AT_MIPS_loop_begin:
6897       return "DW_AT_MIPS_loop_begin";
6898     case DW_AT_MIPS_tail_loop_begin:
6899       return "DW_AT_MIPS_tail_loop_begin";
6900     case DW_AT_MIPS_epilog_begin:
6901       return "DW_AT_MIPS_epilog_begin";
6902 #if VMS_DEBUGGING_INFO
6903     case DW_AT_HP_prologue:
6904       return "DW_AT_HP_prologue";
6905 #else
6906     case DW_AT_MIPS_loop_unroll_factor:
6907       return "DW_AT_MIPS_loop_unroll_factor";
6908 #endif
6909     case DW_AT_MIPS_software_pipeline_depth:
6910       return "DW_AT_MIPS_software_pipeline_depth";
6911     case DW_AT_MIPS_linkage_name:
6912       return "DW_AT_MIPS_linkage_name";
6913 #if VMS_DEBUGGING_INFO
6914     case DW_AT_HP_epilogue:
6915       return "DW_AT_HP_epilogue";
6916 #else
6917     case DW_AT_MIPS_stride:
6918       return "DW_AT_MIPS_stride";
6919 #endif
6920     case DW_AT_MIPS_abstract_name:
6921       return "DW_AT_MIPS_abstract_name";
6922     case DW_AT_MIPS_clone_origin:
6923       return "DW_AT_MIPS_clone_origin";
6924     case DW_AT_MIPS_has_inlines:
6925       return "DW_AT_MIPS_has_inlines";
6926
6927     case DW_AT_sf_names:
6928       return "DW_AT_sf_names";
6929     case DW_AT_src_info:
6930       return "DW_AT_src_info";
6931     case DW_AT_mac_info:
6932       return "DW_AT_mac_info";
6933     case DW_AT_src_coords:
6934       return "DW_AT_src_coords";
6935     case DW_AT_body_begin:
6936       return "DW_AT_body_begin";
6937     case DW_AT_body_end:
6938       return "DW_AT_body_end";
6939     case DW_AT_GNU_vector:
6940       return "DW_AT_GNU_vector";
6941     case DW_AT_GNU_guarded_by:
6942       return "DW_AT_GNU_guarded_by";
6943     case DW_AT_GNU_pt_guarded_by:
6944       return "DW_AT_GNU_pt_guarded_by";
6945     case DW_AT_GNU_guarded:
6946       return "DW_AT_GNU_guarded";
6947     case DW_AT_GNU_pt_guarded:
6948       return "DW_AT_GNU_pt_guarded";
6949     case DW_AT_GNU_locks_excluded:
6950       return "DW_AT_GNU_locks_excluded";
6951     case DW_AT_GNU_exclusive_locks_required:
6952       return "DW_AT_GNU_exclusive_locks_required";
6953     case DW_AT_GNU_shared_locks_required:
6954       return "DW_AT_GNU_shared_locks_required";
6955     case DW_AT_GNU_odr_signature:
6956       return "DW_AT_GNU_odr_signature";
6957     case DW_AT_GNU_template_name:
6958       return "DW_AT_GNU_template_name";
6959
6960     case DW_AT_VMS_rtnbeg_pd_address:
6961       return "DW_AT_VMS_rtnbeg_pd_address";
6962
6963     default:
6964       return "DW_AT_<unknown>";
6965     }
6966 }
6967
6968 /* Convert a DWARF value form code into its string name.  */
6969
6970 static const char *
6971 dwarf_form_name (unsigned int form)
6972 {
6973   switch (form)
6974     {
6975     case DW_FORM_addr:
6976       return "DW_FORM_addr";
6977     case DW_FORM_block2:
6978       return "DW_FORM_block2";
6979     case DW_FORM_block4:
6980       return "DW_FORM_block4";
6981     case DW_FORM_data2:
6982       return "DW_FORM_data2";
6983     case DW_FORM_data4:
6984       return "DW_FORM_data4";
6985     case DW_FORM_data8:
6986       return "DW_FORM_data8";
6987     case DW_FORM_string:
6988       return "DW_FORM_string";
6989     case DW_FORM_block:
6990       return "DW_FORM_block";
6991     case DW_FORM_block1:
6992       return "DW_FORM_block1";
6993     case DW_FORM_data1:
6994       return "DW_FORM_data1";
6995     case DW_FORM_flag:
6996       return "DW_FORM_flag";
6997     case DW_FORM_sdata:
6998       return "DW_FORM_sdata";
6999     case DW_FORM_strp:
7000       return "DW_FORM_strp";
7001     case DW_FORM_udata:
7002       return "DW_FORM_udata";
7003     case DW_FORM_ref_addr:
7004       return "DW_FORM_ref_addr";
7005     case DW_FORM_ref1:
7006       return "DW_FORM_ref1";
7007     case DW_FORM_ref2:
7008       return "DW_FORM_ref2";
7009     case DW_FORM_ref4:
7010       return "DW_FORM_ref4";
7011     case DW_FORM_ref8:
7012       return "DW_FORM_ref8";
7013     case DW_FORM_ref_udata:
7014       return "DW_FORM_ref_udata";
7015     case DW_FORM_indirect:
7016       return "DW_FORM_indirect";
7017     case DW_FORM_sec_offset:
7018       return "DW_FORM_sec_offset";
7019     case DW_FORM_exprloc:
7020       return "DW_FORM_exprloc";
7021     case DW_FORM_flag_present:
7022       return "DW_FORM_flag_present";
7023     case DW_FORM_ref_sig8:
7024       return "DW_FORM_ref_sig8";
7025     default:
7026       return "DW_FORM_<unknown>";
7027     }
7028 }
7029 \f
7030 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7031    instance of an inlined instance of a decl which is local to an inline
7032    function, so we have to trace all of the way back through the origin chain
7033    to find out what sort of node actually served as the original seed for the
7034    given block.  */
7035
7036 static tree
7037 decl_ultimate_origin (const_tree decl)
7038 {
7039   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7040     return NULL_TREE;
7041
7042   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7043      nodes in the function to point to themselves; ignore that if
7044      we're trying to output the abstract instance of this function.  */
7045   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7046     return NULL_TREE;
7047
7048   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7049      most distant ancestor, this should never happen.  */
7050   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7051
7052   return DECL_ABSTRACT_ORIGIN (decl);
7053 }
7054
7055 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7056    of a virtual function may refer to a base class, so we check the 'this'
7057    parameter.  */
7058
7059 static tree
7060 decl_class_context (tree decl)
7061 {
7062   tree context = NULL_TREE;
7063
7064   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7065     context = DECL_CONTEXT (decl);
7066   else
7067     context = TYPE_MAIN_VARIANT
7068       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7069
7070   if (context && !TYPE_P (context))
7071     context = NULL_TREE;
7072
7073   return context;
7074 }
7075 \f
7076 /* Add an attribute/value pair to a DIE.  */
7077
7078 static inline void
7079 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7080 {
7081   /* Maybe this should be an assert?  */
7082   if (die == NULL)
7083     return;
7084
7085   if (die->die_attr == NULL)
7086     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7087   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7088 }
7089
7090 static inline enum dw_val_class
7091 AT_class (dw_attr_ref a)
7092 {
7093   return a->dw_attr_val.val_class;
7094 }
7095
7096 /* Add a flag value attribute to a DIE.  */
7097
7098 static inline void
7099 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7100 {
7101   dw_attr_node attr;
7102
7103   attr.dw_attr = attr_kind;
7104   attr.dw_attr_val.val_class = dw_val_class_flag;
7105   attr.dw_attr_val.v.val_flag = flag;
7106   add_dwarf_attr (die, &attr);
7107 }
7108
7109 static inline unsigned
7110 AT_flag (dw_attr_ref a)
7111 {
7112   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7113   return a->dw_attr_val.v.val_flag;
7114 }
7115
7116 /* Add a signed integer attribute value to a DIE.  */
7117
7118 static inline void
7119 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7120 {
7121   dw_attr_node attr;
7122
7123   attr.dw_attr = attr_kind;
7124   attr.dw_attr_val.val_class = dw_val_class_const;
7125   attr.dw_attr_val.v.val_int = int_val;
7126   add_dwarf_attr (die, &attr);
7127 }
7128
7129 static inline HOST_WIDE_INT
7130 AT_int (dw_attr_ref a)
7131 {
7132   gcc_assert (a && AT_class (a) == dw_val_class_const);
7133   return a->dw_attr_val.v.val_int;
7134 }
7135
7136 /* Add an unsigned integer attribute value to a DIE.  */
7137
7138 static inline void
7139 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7140                  unsigned HOST_WIDE_INT unsigned_val)
7141 {
7142   dw_attr_node attr;
7143
7144   attr.dw_attr = attr_kind;
7145   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7146   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7147   add_dwarf_attr (die, &attr);
7148 }
7149
7150 static inline unsigned HOST_WIDE_INT
7151 AT_unsigned (dw_attr_ref a)
7152 {
7153   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7154   return a->dw_attr_val.v.val_unsigned;
7155 }
7156
7157 /* Add an unsigned double integer attribute value to a DIE.  */
7158
7159 static inline void
7160 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7161                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7162 {
7163   dw_attr_node attr;
7164
7165   attr.dw_attr = attr_kind;
7166   attr.dw_attr_val.val_class = dw_val_class_const_double;
7167   attr.dw_attr_val.v.val_double.high = high;
7168   attr.dw_attr_val.v.val_double.low = low;
7169   add_dwarf_attr (die, &attr);
7170 }
7171
7172 /* Add a floating point attribute value to a DIE and return it.  */
7173
7174 static inline void
7175 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7176             unsigned int length, unsigned int elt_size, unsigned char *array)
7177 {
7178   dw_attr_node attr;
7179
7180   attr.dw_attr = attr_kind;
7181   attr.dw_attr_val.val_class = dw_val_class_vec;
7182   attr.dw_attr_val.v.val_vec.length = length;
7183   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7184   attr.dw_attr_val.v.val_vec.array = array;
7185   add_dwarf_attr (die, &attr);
7186 }
7187
7188 /* Add an 8-byte data attribute value to a DIE.  */
7189
7190 static inline void
7191 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7192               unsigned char data8[8])
7193 {
7194   dw_attr_node attr;
7195
7196   attr.dw_attr = attr_kind;
7197   attr.dw_attr_val.val_class = dw_val_class_data8;
7198   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7199   add_dwarf_attr (die, &attr);
7200 }
7201
7202 /* Hash and equality functions for debug_str_hash.  */
7203
7204 static hashval_t
7205 debug_str_do_hash (const void *x)
7206 {
7207   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7208 }
7209
7210 static int
7211 debug_str_eq (const void *x1, const void *x2)
7212 {
7213   return strcmp ((((const struct indirect_string_node *)x1)->str),
7214                  (const char *)x2) == 0;
7215 }
7216
7217 /* Add STR to the indirect string hash table.  */
7218
7219 static struct indirect_string_node *
7220 find_AT_string (const char *str)
7221 {
7222   struct indirect_string_node *node;
7223   void **slot;
7224
7225   if (! debug_str_hash)
7226     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7227                                       debug_str_eq, NULL);
7228
7229   slot = htab_find_slot_with_hash (debug_str_hash, str,
7230                                    htab_hash_string (str), INSERT);
7231   if (*slot == NULL)
7232     {
7233       node = ggc_alloc_cleared_indirect_string_node ();
7234       node->str = ggc_strdup (str);
7235       *slot = node;
7236     }
7237   else
7238     node = (struct indirect_string_node *) *slot;
7239
7240   node->refcount++;
7241   return node;
7242 }
7243
7244 /* Add a string attribute value to a DIE.  */
7245
7246 static inline void
7247 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7248 {
7249   dw_attr_node attr;
7250   struct indirect_string_node *node;
7251
7252   node = find_AT_string (str);
7253
7254   attr.dw_attr = attr_kind;
7255   attr.dw_attr_val.val_class = dw_val_class_str;
7256   attr.dw_attr_val.v.val_str = node;
7257   add_dwarf_attr (die, &attr);
7258 }
7259
7260 /* Create a label for an indirect string node, ensuring it is going to
7261    be output, unless its reference count goes down to zero.  */
7262
7263 static inline void
7264 gen_label_for_indirect_string (struct indirect_string_node *node)
7265 {
7266   char label[32];
7267
7268   if (node->label)
7269     return;
7270
7271   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7272   ++dw2_string_counter;
7273   node->label = xstrdup (label);
7274 }
7275
7276 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7277    debug string STR.  */
7278
7279 static inline rtx
7280 get_debug_string_label (const char *str)
7281 {
7282   struct indirect_string_node *node = find_AT_string (str);
7283
7284   debug_str_hash_forced = true;
7285
7286   gen_label_for_indirect_string (node);
7287
7288   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7289 }
7290
7291 static inline const char *
7292 AT_string (dw_attr_ref a)
7293 {
7294   gcc_assert (a && AT_class (a) == dw_val_class_str);
7295   return a->dw_attr_val.v.val_str->str;
7296 }
7297
7298 /* Find out whether a string should be output inline in DIE
7299    or out-of-line in .debug_str section.  */
7300
7301 static enum dwarf_form
7302 AT_string_form (dw_attr_ref a)
7303 {
7304   struct indirect_string_node *node;
7305   unsigned int len;
7306
7307   gcc_assert (a && AT_class (a) == dw_val_class_str);
7308
7309   node = a->dw_attr_val.v.val_str;
7310   if (node->form)
7311     return node->form;
7312
7313   len = strlen (node->str) + 1;
7314
7315   /* If the string is shorter or equal to the size of the reference, it is
7316      always better to put it inline.  */
7317   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7318     return node->form = DW_FORM_string;
7319
7320   /* If we cannot expect the linker to merge strings in .debug_str
7321      section, only put it into .debug_str if it is worth even in this
7322      single module.  */
7323   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7324       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7325       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7326     return node->form = DW_FORM_string;
7327
7328   gen_label_for_indirect_string (node);
7329
7330   return node->form = DW_FORM_strp;
7331 }
7332
7333 /* Add a DIE reference attribute value to a DIE.  */
7334
7335 static inline void
7336 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7337 {
7338   dw_attr_node attr;
7339
7340   attr.dw_attr = attr_kind;
7341   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7342   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7343   attr.dw_attr_val.v.val_die_ref.external = 0;
7344   add_dwarf_attr (die, &attr);
7345 }
7346
7347 /* Add an AT_specification attribute to a DIE, and also make the back
7348    pointer from the specification to the definition.  */
7349
7350 static inline void
7351 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7352 {
7353   add_AT_die_ref (die, DW_AT_specification, targ_die);
7354   gcc_assert (!targ_die->die_definition);
7355   targ_die->die_definition = die;
7356 }
7357
7358 static inline dw_die_ref
7359 AT_ref (dw_attr_ref a)
7360 {
7361   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7362   return a->dw_attr_val.v.val_die_ref.die;
7363 }
7364
7365 static inline int
7366 AT_ref_external (dw_attr_ref a)
7367 {
7368   if (a && AT_class (a) == dw_val_class_die_ref)
7369     return a->dw_attr_val.v.val_die_ref.external;
7370
7371   return 0;
7372 }
7373
7374 static inline void
7375 set_AT_ref_external (dw_attr_ref a, int i)
7376 {
7377   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7378   a->dw_attr_val.v.val_die_ref.external = i;
7379 }
7380
7381 /* Add an FDE reference attribute value to a DIE.  */
7382
7383 static inline void
7384 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7385 {
7386   dw_attr_node attr;
7387
7388   attr.dw_attr = attr_kind;
7389   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7390   attr.dw_attr_val.v.val_fde_index = targ_fde;
7391   add_dwarf_attr (die, &attr);
7392 }
7393
7394 /* Add a location description attribute value to a DIE.  */
7395
7396 static inline void
7397 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7398 {
7399   dw_attr_node attr;
7400
7401   attr.dw_attr = attr_kind;
7402   attr.dw_attr_val.val_class = dw_val_class_loc;
7403   attr.dw_attr_val.v.val_loc = loc;
7404   add_dwarf_attr (die, &attr);
7405 }
7406
7407 static inline dw_loc_descr_ref
7408 AT_loc (dw_attr_ref a)
7409 {
7410   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7411   return a->dw_attr_val.v.val_loc;
7412 }
7413
7414 static inline void
7415 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7416 {
7417   dw_attr_node attr;
7418
7419   attr.dw_attr = attr_kind;
7420   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7421   attr.dw_attr_val.v.val_loc_list = loc_list;
7422   add_dwarf_attr (die, &attr);
7423   have_location_lists = true;
7424 }
7425
7426 static inline dw_loc_list_ref
7427 AT_loc_list (dw_attr_ref a)
7428 {
7429   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7430   return a->dw_attr_val.v.val_loc_list;
7431 }
7432
7433 static inline dw_loc_list_ref *
7434 AT_loc_list_ptr (dw_attr_ref a)
7435 {
7436   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7437   return &a->dw_attr_val.v.val_loc_list;
7438 }
7439
7440 /* Add an address constant attribute value to a DIE.  */
7441
7442 static inline void
7443 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7444 {
7445   dw_attr_node attr;
7446
7447   attr.dw_attr = attr_kind;
7448   attr.dw_attr_val.val_class = dw_val_class_addr;
7449   attr.dw_attr_val.v.val_addr = addr;
7450   add_dwarf_attr (die, &attr);
7451 }
7452
7453 /* Get the RTX from to an address DIE attribute.  */
7454
7455 static inline rtx
7456 AT_addr (dw_attr_ref a)
7457 {
7458   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7459   return a->dw_attr_val.v.val_addr;
7460 }
7461
7462 /* Add a file attribute value to a DIE.  */
7463
7464 static inline void
7465 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7466              struct dwarf_file_data *fd)
7467 {
7468   dw_attr_node attr;
7469
7470   attr.dw_attr = attr_kind;
7471   attr.dw_attr_val.val_class = dw_val_class_file;
7472   attr.dw_attr_val.v.val_file = fd;
7473   add_dwarf_attr (die, &attr);
7474 }
7475
7476 /* Get the dwarf_file_data from a file DIE attribute.  */
7477
7478 static inline struct dwarf_file_data *
7479 AT_file (dw_attr_ref a)
7480 {
7481   gcc_assert (a && AT_class (a) == dw_val_class_file);
7482   return a->dw_attr_val.v.val_file;
7483 }
7484
7485 /* Add a vms delta attribute value to a DIE.  */
7486
7487 static inline void
7488 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7489                   const char *lbl1, const char *lbl2)
7490 {
7491   dw_attr_node attr;
7492
7493   attr.dw_attr = attr_kind;
7494   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7495   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7496   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7497   add_dwarf_attr (die, &attr);
7498 }
7499
7500 /* Add a label identifier attribute value to a DIE.  */
7501
7502 static inline void
7503 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7504 {
7505   dw_attr_node attr;
7506
7507   attr.dw_attr = attr_kind;
7508   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7509   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7510   add_dwarf_attr (die, &attr);
7511 }
7512
7513 /* Add a section offset attribute value to a DIE, an offset into the
7514    debug_line section.  */
7515
7516 static inline void
7517 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7518                 const char *label)
7519 {
7520   dw_attr_node attr;
7521
7522   attr.dw_attr = attr_kind;
7523   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7524   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7525   add_dwarf_attr (die, &attr);
7526 }
7527
7528 /* Add a section offset attribute value to a DIE, an offset into the
7529    debug_macinfo section.  */
7530
7531 static inline void
7532 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7533                const char *label)
7534 {
7535   dw_attr_node attr;
7536
7537   attr.dw_attr = attr_kind;
7538   attr.dw_attr_val.val_class = dw_val_class_macptr;
7539   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7540   add_dwarf_attr (die, &attr);
7541 }
7542
7543 /* Add an offset attribute value to a DIE.  */
7544
7545 static inline void
7546 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7547                unsigned HOST_WIDE_INT offset)
7548 {
7549   dw_attr_node attr;
7550
7551   attr.dw_attr = attr_kind;
7552   attr.dw_attr_val.val_class = dw_val_class_offset;
7553   attr.dw_attr_val.v.val_offset = offset;
7554   add_dwarf_attr (die, &attr);
7555 }
7556
7557 /* Add an range_list attribute value to a DIE.  */
7558
7559 static void
7560 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7561                    long unsigned int offset)
7562 {
7563   dw_attr_node attr;
7564
7565   attr.dw_attr = attr_kind;
7566   attr.dw_attr_val.val_class = dw_val_class_range_list;
7567   attr.dw_attr_val.v.val_offset = offset;
7568   add_dwarf_attr (die, &attr);
7569 }
7570
7571 /* Return the start label of a delta attribute.  */
7572
7573 static inline const char *
7574 AT_vms_delta1 (dw_attr_ref a)
7575 {
7576   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7577   return a->dw_attr_val.v.val_vms_delta.lbl1;
7578 }
7579
7580 /* Return the end label of a delta attribute.  */
7581
7582 static inline const char *
7583 AT_vms_delta2 (dw_attr_ref a)
7584 {
7585   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7586   return a->dw_attr_val.v.val_vms_delta.lbl2;
7587 }
7588
7589 static inline const char *
7590 AT_lbl (dw_attr_ref a)
7591 {
7592   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7593                     || AT_class (a) == dw_val_class_lineptr
7594                     || AT_class (a) == dw_val_class_macptr));
7595   return a->dw_attr_val.v.val_lbl_id;
7596 }
7597
7598 /* Get the attribute of type attr_kind.  */
7599
7600 static dw_attr_ref
7601 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7602 {
7603   dw_attr_ref a;
7604   unsigned ix;
7605   dw_die_ref spec = NULL;
7606
7607   if (! die)
7608     return NULL;
7609
7610   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7611     if (a->dw_attr == attr_kind)
7612       return a;
7613     else if (a->dw_attr == DW_AT_specification
7614              || a->dw_attr == DW_AT_abstract_origin)
7615       spec = AT_ref (a);
7616
7617   if (spec)
7618     return get_AT (spec, attr_kind);
7619
7620   return NULL;
7621 }
7622
7623 /* Return the "low pc" attribute value, typically associated with a subprogram
7624    DIE.  Return null if the "low pc" attribute is either not present, or if it
7625    cannot be represented as an assembler label identifier.  */
7626
7627 static inline const char *
7628 get_AT_low_pc (dw_die_ref die)
7629 {
7630   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7631
7632   return a ? AT_lbl (a) : NULL;
7633 }
7634
7635 /* Return the "high pc" attribute value, typically associated with a subprogram
7636    DIE.  Return null if the "high pc" attribute is either not present, or if it
7637    cannot be represented as an assembler label identifier.  */
7638
7639 static inline const char *
7640 get_AT_hi_pc (dw_die_ref die)
7641 {
7642   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7643
7644   return a ? AT_lbl (a) : NULL;
7645 }
7646
7647 /* Return the value of the string attribute designated by ATTR_KIND, or
7648    NULL if it is not present.  */
7649
7650 static inline const char *
7651 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7652 {
7653   dw_attr_ref a = get_AT (die, attr_kind);
7654
7655   return a ? AT_string (a) : NULL;
7656 }
7657
7658 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7659    if it is not present.  */
7660
7661 static inline int
7662 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7663 {
7664   dw_attr_ref a = get_AT (die, attr_kind);
7665
7666   return a ? AT_flag (a) : 0;
7667 }
7668
7669 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7670    if it is not present.  */
7671
7672 static inline unsigned
7673 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7674 {
7675   dw_attr_ref a = get_AT (die, attr_kind);
7676
7677   return a ? AT_unsigned (a) : 0;
7678 }
7679
7680 static inline dw_die_ref
7681 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7682 {
7683   dw_attr_ref a = get_AT (die, attr_kind);
7684
7685   return a ? AT_ref (a) : NULL;
7686 }
7687
7688 static inline struct dwarf_file_data *
7689 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7690 {
7691   dw_attr_ref a = get_AT (die, attr_kind);
7692
7693   return a ? AT_file (a) : NULL;
7694 }
7695
7696 /* Return TRUE if the language is C++.  */
7697
7698 static inline bool
7699 is_cxx (void)
7700 {
7701   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7702
7703   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7704 }
7705
7706 /* Return TRUE if the language is Fortran.  */
7707
7708 static inline bool
7709 is_fortran (void)
7710 {
7711   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7712
7713   return (lang == DW_LANG_Fortran77
7714           || lang == DW_LANG_Fortran90
7715           || lang == DW_LANG_Fortran95);
7716 }
7717
7718 /* Return TRUE if the language is Ada.  */
7719
7720 static inline bool
7721 is_ada (void)
7722 {
7723   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7724
7725   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7726 }
7727
7728 /* Remove the specified attribute if present.  */
7729
7730 static void
7731 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7732 {
7733   dw_attr_ref a;
7734   unsigned ix;
7735
7736   if (! die)
7737     return;
7738
7739   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7740     if (a->dw_attr == attr_kind)
7741       {
7742         if (AT_class (a) == dw_val_class_str)
7743           if (a->dw_attr_val.v.val_str->refcount)
7744             a->dw_attr_val.v.val_str->refcount--;
7745
7746         /* VEC_ordered_remove should help reduce the number of abbrevs
7747            that are needed.  */
7748         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7749         return;
7750       }
7751 }
7752
7753 /* Remove CHILD from its parent.  PREV must have the property that
7754    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7755
7756 static void
7757 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7758 {
7759   gcc_assert (child->die_parent == prev->die_parent);
7760   gcc_assert (prev->die_sib == child);
7761   if (prev == child)
7762     {
7763       gcc_assert (child->die_parent->die_child == child);
7764       prev = NULL;
7765     }
7766   else
7767     prev->die_sib = child->die_sib;
7768   if (child->die_parent->die_child == child)
7769     child->die_parent->die_child = prev;
7770 }
7771
7772 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7773    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7774
7775 static void
7776 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7777 {
7778   dw_die_ref parent = old_child->die_parent;
7779
7780   gcc_assert (parent == prev->die_parent);
7781   gcc_assert (prev->die_sib == old_child);
7782
7783   new_child->die_parent = parent;
7784   if (prev == old_child)
7785     {
7786       gcc_assert (parent->die_child == old_child);
7787       new_child->die_sib = new_child;
7788     }
7789   else
7790     {
7791       prev->die_sib = new_child;
7792       new_child->die_sib = old_child->die_sib;
7793     }
7794   if (old_child->die_parent->die_child == old_child)
7795     old_child->die_parent->die_child = new_child;
7796 }
7797
7798 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7799
7800 static void
7801 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7802 {
7803   dw_die_ref c;
7804   new_parent->die_child = old_parent->die_child;
7805   old_parent->die_child = NULL;
7806   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7807 }
7808
7809 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7810    matches TAG.  */
7811
7812 static void
7813 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7814 {
7815   dw_die_ref c;
7816
7817   c = die->die_child;
7818   if (c) do {
7819     dw_die_ref prev = c;
7820     c = c->die_sib;
7821     while (c->die_tag == tag)
7822       {
7823         remove_child_with_prev (c, prev);
7824         /* Might have removed every child.  */
7825         if (c == c->die_sib)
7826           return;
7827         c = c->die_sib;
7828       }
7829   } while (c != die->die_child);
7830 }
7831
7832 /* Add a CHILD_DIE as the last child of DIE.  */
7833
7834 static void
7835 add_child_die (dw_die_ref die, dw_die_ref child_die)
7836 {
7837   /* FIXME this should probably be an assert.  */
7838   if (! die || ! child_die)
7839     return;
7840   gcc_assert (die != child_die);
7841
7842   child_die->die_parent = die;
7843   if (die->die_child)
7844     {
7845       child_die->die_sib = die->die_child->die_sib;
7846       die->die_child->die_sib = child_die;
7847     }
7848   else
7849     child_die->die_sib = child_die;
7850   die->die_child = child_die;
7851 }
7852
7853 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7854    is the specification, to the end of PARENT's list of children.
7855    This is done by removing and re-adding it.  */
7856
7857 static void
7858 splice_child_die (dw_die_ref parent, dw_die_ref child)
7859 {
7860   dw_die_ref p;
7861
7862   /* We want the declaration DIE from inside the class, not the
7863      specification DIE at toplevel.  */
7864   if (child->die_parent != parent)
7865     {
7866       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7867
7868       if (tmp)
7869         child = tmp;
7870     }
7871
7872   gcc_assert (child->die_parent == parent
7873               || (child->die_parent
7874                   == get_AT_ref (parent, DW_AT_specification)));
7875
7876   for (p = child->die_parent->die_child; ; p = p->die_sib)
7877     if (p->die_sib == child)
7878       {
7879         remove_child_with_prev (child, p);
7880         break;
7881       }
7882
7883   add_child_die (parent, child);
7884 }
7885
7886 /* Return a pointer to a newly created DIE node.  */
7887
7888 static inline dw_die_ref
7889 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7890 {
7891   dw_die_ref die = ggc_alloc_cleared_die_node ();
7892
7893   die->die_tag = tag_value;
7894
7895   if (parent_die != NULL)
7896     add_child_die (parent_die, die);
7897   else
7898     {
7899       limbo_die_node *limbo_node;
7900
7901       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7902       limbo_node->die = die;
7903       limbo_node->created_for = t;
7904       limbo_node->next = limbo_die_list;
7905       limbo_die_list = limbo_node;
7906     }
7907
7908   return die;
7909 }
7910
7911 /* Return the DIE associated with the given type specifier.  */
7912
7913 static inline dw_die_ref
7914 lookup_type_die (tree type)
7915 {
7916   return TYPE_SYMTAB_DIE (type);
7917 }
7918
7919 /* Equate a DIE to a given type specifier.  */
7920
7921 static inline void
7922 equate_type_number_to_die (tree type, dw_die_ref type_die)
7923 {
7924   TYPE_SYMTAB_DIE (type) = type_die;
7925 }
7926
7927 /* Returns a hash value for X (which really is a die_struct).  */
7928
7929 static hashval_t
7930 decl_die_table_hash (const void *x)
7931 {
7932   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7933 }
7934
7935 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7936
7937 static int
7938 decl_die_table_eq (const void *x, const void *y)
7939 {
7940   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7941 }
7942
7943 /* Return the DIE associated with a given declaration.  */
7944
7945 static inline dw_die_ref
7946 lookup_decl_die (tree decl)
7947 {
7948   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7949 }
7950
7951 /* Returns a hash value for X (which really is a var_loc_list).  */
7952
7953 static hashval_t
7954 decl_loc_table_hash (const void *x)
7955 {
7956   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7957 }
7958
7959 /* Return nonzero if decl_id of var_loc_list X is the same as
7960    UID of decl *Y.  */
7961
7962 static int
7963 decl_loc_table_eq (const void *x, const void *y)
7964 {
7965   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7966 }
7967
7968 /* Return the var_loc list associated with a given declaration.  */
7969
7970 static inline var_loc_list *
7971 lookup_decl_loc (const_tree decl)
7972 {
7973   if (!decl_loc_table)
7974     return NULL;
7975   return (var_loc_list *)
7976     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7977 }
7978
7979 /* Equate a DIE to a particular declaration.  */
7980
7981 static void
7982 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7983 {
7984   unsigned int decl_id = DECL_UID (decl);
7985   void **slot;
7986
7987   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7988   *slot = decl_die;
7989   decl_die->decl_id = decl_id;
7990 }
7991
7992 /* Return how many bits covers PIECE EXPR_LIST.  */
7993
7994 static int
7995 decl_piece_bitsize (rtx piece)
7996 {
7997   int ret = (int) GET_MODE (piece);
7998   if (ret)
7999     return ret;
8000   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8001               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8002   return INTVAL (XEXP (XEXP (piece, 0), 0));
8003 }
8004
8005 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8006
8007 static rtx *
8008 decl_piece_varloc_ptr (rtx piece)
8009 {
8010   if ((int) GET_MODE (piece))
8011     return &XEXP (piece, 0);
8012   else
8013     return &XEXP (XEXP (piece, 0), 1);
8014 }
8015
8016 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8017    Next is the chain of following piece nodes.  */
8018
8019 static rtx
8020 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8021 {
8022   if (bitsize <= (int) MAX_MACHINE_MODE)
8023     return alloc_EXPR_LIST (bitsize, loc_note, next);
8024   else
8025     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8026                                                GEN_INT (bitsize),
8027                                                loc_note), next);
8028 }
8029
8030 /* Return rtx that should be stored into loc field for
8031    LOC_NOTE and BITPOS/BITSIZE.  */
8032
8033 static rtx
8034 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8035                       HOST_WIDE_INT bitsize)
8036 {
8037   if (bitsize != -1)
8038     {
8039       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8040       if (bitpos != 0)
8041         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8042     }
8043   return loc_note;
8044 }
8045
8046 /* This function either modifies location piece list *DEST in
8047    place (if SRC and INNER is NULL), or copies location piece list
8048    *SRC to *DEST while modifying it.  Location BITPOS is modified
8049    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8050    not copied and if needed some padding around it is added.
8051    When modifying in place, DEST should point to EXPR_LIST where
8052    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8053    to the start of the whole list and INNER points to the EXPR_LIST
8054    where earlier pieces cover PIECE_BITPOS bits.  */
8055
8056 static void
8057 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8058                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8059                    HOST_WIDE_INT bitsize, rtx loc_note)
8060 {
8061   int diff;
8062   bool copy = inner != NULL;
8063
8064   if (copy)
8065     {
8066       /* First copy all nodes preceeding the current bitpos.  */
8067       while (src != inner)
8068         {
8069           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8070                                    decl_piece_bitsize (*src), NULL_RTX);
8071           dest = &XEXP (*dest, 1);
8072           src = &XEXP (*src, 1);
8073         }
8074     }
8075   /* Add padding if needed.  */
8076   if (bitpos != piece_bitpos)
8077     {
8078       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8079                                copy ? NULL_RTX : *dest);
8080       dest = &XEXP (*dest, 1);
8081     }
8082   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8083     {
8084       gcc_assert (!copy);
8085       /* A piece with correct bitpos and bitsize already exist,
8086          just update the location for it and return.  */
8087       *decl_piece_varloc_ptr (*dest) = loc_note;
8088       return;
8089     }
8090   /* Add the piece that changed.  */
8091   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8092   dest = &XEXP (*dest, 1);
8093   /* Skip over pieces that overlap it.  */
8094   diff = bitpos - piece_bitpos + bitsize;
8095   if (!copy)
8096     src = dest;
8097   while (diff > 0 && *src)
8098     {
8099       rtx piece = *src;
8100       diff -= decl_piece_bitsize (piece);
8101       if (copy)
8102         src = &XEXP (piece, 1);
8103       else
8104         {
8105           *src = XEXP (piece, 1);
8106           free_EXPR_LIST_node (piece);
8107         }
8108     }
8109   /* Add padding if needed.  */
8110   if (diff < 0 && *src)
8111     {
8112       if (!copy)
8113         dest = src;
8114       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8115       dest = &XEXP (*dest, 1);
8116     }
8117   if (!copy)
8118     return;
8119   /* Finally copy all nodes following it.  */
8120   while (*src)
8121     {
8122       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8123                                decl_piece_bitsize (*src), NULL_RTX);
8124       dest = &XEXP (*dest, 1);
8125       src = &XEXP (*src, 1);
8126     }
8127 }
8128
8129 /* Add a variable location node to the linked list for DECL.  */
8130
8131 static struct var_loc_node *
8132 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8133 {
8134   unsigned int decl_id;
8135   var_loc_list *temp;
8136   void **slot;
8137   struct var_loc_node *loc = NULL;
8138   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8139
8140   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8141     {
8142       tree realdecl = DECL_DEBUG_EXPR (decl);
8143       if (realdecl && handled_component_p (realdecl))
8144         {
8145           HOST_WIDE_INT maxsize;
8146           tree innerdecl;
8147           innerdecl
8148             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8149           if (!DECL_P (innerdecl)
8150               || DECL_IGNORED_P (innerdecl)
8151               || TREE_STATIC (innerdecl)
8152               || bitsize <= 0
8153               || bitpos + bitsize > 256
8154               || bitsize != maxsize)
8155             return NULL;
8156           decl = innerdecl;
8157         }
8158     }
8159
8160   decl_id = DECL_UID (decl);
8161   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8162   if (*slot == NULL)
8163     {
8164       temp = ggc_alloc_cleared_var_loc_list ();
8165       temp->decl_id = decl_id;
8166       *slot = temp;
8167     }
8168   else
8169     temp = (var_loc_list *) *slot;
8170
8171   if (temp->last)
8172     {
8173       struct var_loc_node *last = temp->last, *unused = NULL;
8174       rtx *piece_loc = NULL, last_loc_note;
8175       int piece_bitpos = 0;
8176       if (last->next)
8177         {
8178           last = last->next;
8179           gcc_assert (last->next == NULL);
8180         }
8181       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8182         {
8183           piece_loc = &last->loc;
8184           do
8185             {
8186               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8187               if (piece_bitpos + cur_bitsize > bitpos)
8188                 break;
8189               piece_bitpos += cur_bitsize;
8190               piece_loc = &XEXP (*piece_loc, 1);
8191             }
8192           while (*piece_loc);
8193         }
8194       /* TEMP->LAST here is either pointer to the last but one or
8195          last element in the chained list, LAST is pointer to the
8196          last element.  */
8197       if (label && strcmp (last->label, label) == 0)
8198         {
8199           /* For SRA optimized variables if there weren't any real
8200              insns since last note, just modify the last node.  */
8201           if (piece_loc != NULL)
8202             {
8203               adjust_piece_list (piece_loc, NULL, NULL,
8204                                  bitpos, piece_bitpos, bitsize, loc_note);
8205               return NULL;
8206             }
8207           /* If the last note doesn't cover any instructions, remove it.  */
8208           if (temp->last != last)
8209             {
8210               temp->last->next = NULL;
8211               unused = last;
8212               last = temp->last;
8213               gcc_assert (strcmp (last->label, label) != 0);
8214             }
8215           else
8216             {
8217               gcc_assert (temp->first == temp->last);
8218               memset (temp->last, '\0', sizeof (*temp->last));
8219               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8220               return temp->last;
8221             }
8222         }
8223       if (bitsize == -1 && NOTE_P (last->loc))
8224         last_loc_note = last->loc;
8225       else if (piece_loc != NULL
8226                && *piece_loc != NULL_RTX
8227                && piece_bitpos == bitpos
8228                && decl_piece_bitsize (*piece_loc) == bitsize)
8229         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8230       else
8231         last_loc_note = NULL_RTX;
8232       /* If the current location is the same as the end of the list,
8233          and either both or neither of the locations is uninitialized,
8234          we have nothing to do.  */
8235       if (last_loc_note == NULL_RTX
8236           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8237                             NOTE_VAR_LOCATION_LOC (loc_note)))
8238           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8239                != NOTE_VAR_LOCATION_STATUS (loc_note))
8240               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8241                    == VAR_INIT_STATUS_UNINITIALIZED)
8242                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8243                       == VAR_INIT_STATUS_UNINITIALIZED))))
8244         {
8245           /* Add LOC to the end of list and update LAST.  If the last
8246              element of the list has been removed above, reuse its
8247              memory for the new node, otherwise allocate a new one.  */
8248           if (unused)
8249             {
8250               loc = unused;
8251               memset (loc, '\0', sizeof (*loc));
8252             }
8253           else
8254             loc = ggc_alloc_cleared_var_loc_node ();
8255           if (bitsize == -1 || piece_loc == NULL)
8256             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8257           else
8258             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8259                                bitpos, piece_bitpos, bitsize, loc_note);
8260           last->next = loc;
8261           /* Ensure TEMP->LAST will point either to the new last but one
8262              element of the chain, or to the last element in it.  */
8263           if (last != temp->last)
8264             temp->last = last;
8265         }
8266       else if (unused)
8267         ggc_free (unused);
8268     }
8269   else
8270     {
8271       loc = ggc_alloc_cleared_var_loc_node ();
8272       temp->first = loc;
8273       temp->last = loc;
8274       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8275     }
8276   return loc;
8277 }
8278 \f
8279 /* Keep track of the number of spaces used to indent the
8280    output of the debugging routines that print the structure of
8281    the DIE internal representation.  */
8282 static int print_indent;
8283
8284 /* Indent the line the number of spaces given by print_indent.  */
8285
8286 static inline void
8287 print_spaces (FILE *outfile)
8288 {
8289   fprintf (outfile, "%*s", print_indent, "");
8290 }
8291
8292 /* Print a type signature in hex.  */
8293
8294 static inline void
8295 print_signature (FILE *outfile, char *sig)
8296 {
8297   int i;
8298
8299   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8300     fprintf (outfile, "%02x", sig[i] & 0xff);
8301 }
8302
8303 /* Print the information associated with a given DIE, and its children.
8304    This routine is a debugging aid only.  */
8305
8306 static void
8307 print_die (dw_die_ref die, FILE *outfile)
8308 {
8309   dw_attr_ref a;
8310   dw_die_ref c;
8311   unsigned ix;
8312
8313   print_spaces (outfile);
8314   fprintf (outfile, "DIE %4ld: %s\n",
8315            die->die_offset, dwarf_tag_name (die->die_tag));
8316   print_spaces (outfile);
8317   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8318   fprintf (outfile, " offset: %ld\n", die->die_offset);
8319   if (dwarf_version >= 4 && die->die_id.die_type_node)
8320     {
8321       print_spaces (outfile);
8322       fprintf (outfile, "  signature: ");
8323       print_signature (outfile, die->die_id.die_type_node->signature);
8324       fprintf (outfile, "\n");
8325     }
8326
8327   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8328     {
8329       print_spaces (outfile);
8330       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8331
8332       switch (AT_class (a))
8333         {
8334         case dw_val_class_addr:
8335           fprintf (outfile, "address");
8336           break;
8337         case dw_val_class_offset:
8338           fprintf (outfile, "offset");
8339           break;
8340         case dw_val_class_loc:
8341           fprintf (outfile, "location descriptor");
8342           break;
8343         case dw_val_class_loc_list:
8344           fprintf (outfile, "location list -> label:%s",
8345                    AT_loc_list (a)->ll_symbol);
8346           break;
8347         case dw_val_class_range_list:
8348           fprintf (outfile, "range list");
8349           break;
8350         case dw_val_class_const:
8351           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8352           break;
8353         case dw_val_class_unsigned_const:
8354           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8355           break;
8356         case dw_val_class_const_double:
8357           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8358                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8359                    a->dw_attr_val.v.val_double.high,
8360                    a->dw_attr_val.v.val_double.low);
8361           break;
8362         case dw_val_class_vec:
8363           fprintf (outfile, "floating-point or vector constant");
8364           break;
8365         case dw_val_class_flag:
8366           fprintf (outfile, "%u", AT_flag (a));
8367           break;
8368         case dw_val_class_die_ref:
8369           if (AT_ref (a) != NULL)
8370             {
8371               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8372                 {
8373                   fprintf (outfile, "die -> signature: ");
8374                   print_signature (outfile,
8375                                    AT_ref (a)->die_id.die_type_node->signature);
8376                 }
8377               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8378                 fprintf (outfile, "die -> label: %s",
8379                          AT_ref (a)->die_id.die_symbol);
8380               else
8381                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8382             }
8383           else
8384             fprintf (outfile, "die -> <null>");
8385           break;
8386         case dw_val_class_vms_delta:
8387           fprintf (outfile, "delta: @slotcount(%s-%s)",
8388                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8389           break;
8390         case dw_val_class_lbl_id:
8391         case dw_val_class_lineptr:
8392         case dw_val_class_macptr:
8393           fprintf (outfile, "label: %s", AT_lbl (a));
8394           break;
8395         case dw_val_class_str:
8396           if (AT_string (a) != NULL)
8397             fprintf (outfile, "\"%s\"", AT_string (a));
8398           else
8399             fprintf (outfile, "<null>");
8400           break;
8401         case dw_val_class_file:
8402           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8403                    AT_file (a)->emitted_number);
8404           break;
8405         case dw_val_class_data8:
8406           {
8407             int i;
8408
8409             for (i = 0; i < 8; i++)
8410               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8411             break;
8412           }
8413         default:
8414           break;
8415         }
8416
8417       fprintf (outfile, "\n");
8418     }
8419
8420   if (die->die_child != NULL)
8421     {
8422       print_indent += 4;
8423       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8424       print_indent -= 4;
8425     }
8426   if (print_indent == 0)
8427     fprintf (outfile, "\n");
8428 }
8429
8430 /* Print the contents of the source code line number correspondence table.
8431    This routine is a debugging aid only.  */
8432
8433 static void
8434 print_dwarf_line_table (FILE *outfile)
8435 {
8436   unsigned i;
8437   dw_line_info_ref line_info;
8438
8439   fprintf (outfile, "\n\nDWARF source line information\n");
8440   for (i = 1; i < line_info_table_in_use; i++)
8441     {
8442       line_info = &line_info_table[i];
8443       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8444                line_info->dw_file_num,
8445                line_info->dw_line_num);
8446     }
8447
8448   fprintf (outfile, "\n\n");
8449 }
8450
8451 /* Print the information collected for a given DIE.  */
8452
8453 DEBUG_FUNCTION void
8454 debug_dwarf_die (dw_die_ref die)
8455 {
8456   print_die (die, stderr);
8457 }
8458
8459 /* Print all DWARF information collected for the compilation unit.
8460    This routine is a debugging aid only.  */
8461
8462 DEBUG_FUNCTION void
8463 debug_dwarf (void)
8464 {
8465   print_indent = 0;
8466   print_die (comp_unit_die (), stderr);
8467   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8468     print_dwarf_line_table (stderr);
8469 }
8470 \f
8471 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8472    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8473    DIE that marks the start of the DIEs for this include file.  */
8474
8475 static dw_die_ref
8476 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8477 {
8478   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8479   dw_die_ref new_unit = gen_compile_unit_die (filename);
8480
8481   new_unit->die_sib = old_unit;
8482   return new_unit;
8483 }
8484
8485 /* Close an include-file CU and reopen the enclosing one.  */
8486
8487 static dw_die_ref
8488 pop_compile_unit (dw_die_ref old_unit)
8489 {
8490   dw_die_ref new_unit = old_unit->die_sib;
8491
8492   old_unit->die_sib = NULL;
8493   return new_unit;
8494 }
8495
8496 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8497 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8498
8499 /* Calculate the checksum of a location expression.  */
8500
8501 static inline void
8502 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8503 {
8504   int tem;
8505
8506   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8507   CHECKSUM (tem);
8508   CHECKSUM (loc->dw_loc_oprnd1);
8509   CHECKSUM (loc->dw_loc_oprnd2);
8510 }
8511
8512 /* Calculate the checksum of an attribute.  */
8513
8514 static void
8515 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8516 {
8517   dw_loc_descr_ref loc;
8518   rtx r;
8519
8520   CHECKSUM (at->dw_attr);
8521
8522   /* We don't care that this was compiled with a different compiler
8523      snapshot; if the output is the same, that's what matters.  */
8524   if (at->dw_attr == DW_AT_producer)
8525     return;
8526
8527   switch (AT_class (at))
8528     {
8529     case dw_val_class_const:
8530       CHECKSUM (at->dw_attr_val.v.val_int);
8531       break;
8532     case dw_val_class_unsigned_const:
8533       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8534       break;
8535     case dw_val_class_const_double:
8536       CHECKSUM (at->dw_attr_val.v.val_double);
8537       break;
8538     case dw_val_class_vec:
8539       CHECKSUM (at->dw_attr_val.v.val_vec);
8540       break;
8541     case dw_val_class_flag:
8542       CHECKSUM (at->dw_attr_val.v.val_flag);
8543       break;
8544     case dw_val_class_str:
8545       CHECKSUM_STRING (AT_string (at));
8546       break;
8547
8548     case dw_val_class_addr:
8549       r = AT_addr (at);
8550       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8551       CHECKSUM_STRING (XSTR (r, 0));
8552       break;
8553
8554     case dw_val_class_offset:
8555       CHECKSUM (at->dw_attr_val.v.val_offset);
8556       break;
8557
8558     case dw_val_class_loc:
8559       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8560         loc_checksum (loc, ctx);
8561       break;
8562
8563     case dw_val_class_die_ref:
8564       die_checksum (AT_ref (at), ctx, mark);
8565       break;
8566
8567     case dw_val_class_fde_ref:
8568     case dw_val_class_vms_delta:
8569     case dw_val_class_lbl_id:
8570     case dw_val_class_lineptr:
8571     case dw_val_class_macptr:
8572       break;
8573
8574     case dw_val_class_file:
8575       CHECKSUM_STRING (AT_file (at)->filename);
8576       break;
8577
8578     case dw_val_class_data8:
8579       CHECKSUM (at->dw_attr_val.v.val_data8);
8580       break;
8581
8582     default:
8583       break;
8584     }
8585 }
8586
8587 /* Calculate the checksum of a DIE.  */
8588
8589 static void
8590 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8591 {
8592   dw_die_ref c;
8593   dw_attr_ref a;
8594   unsigned ix;
8595
8596   /* To avoid infinite recursion.  */
8597   if (die->die_mark)
8598     {
8599       CHECKSUM (die->die_mark);
8600       return;
8601     }
8602   die->die_mark = ++(*mark);
8603
8604   CHECKSUM (die->die_tag);
8605
8606   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8607     attr_checksum (a, ctx, mark);
8608
8609   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8610 }
8611
8612 #undef CHECKSUM
8613 #undef CHECKSUM_STRING
8614
8615 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8616 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8617 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8618 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8619 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8620 #define CHECKSUM_ATTR(FOO) \
8621   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8622
8623 /* Calculate the checksum of a number in signed LEB128 format.  */
8624
8625 static void
8626 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8627 {
8628   unsigned char byte;
8629   bool more;
8630
8631   while (1)
8632     {
8633       byte = (value & 0x7f);
8634       value >>= 7;
8635       more = !((value == 0 && (byte & 0x40) == 0)
8636                 || (value == -1 && (byte & 0x40) != 0));
8637       if (more)
8638         byte |= 0x80;
8639       CHECKSUM (byte);
8640       if (!more)
8641         break;
8642     }
8643 }
8644
8645 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8646
8647 static void
8648 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8649 {
8650   while (1)
8651     {
8652       unsigned char byte = (value & 0x7f);
8653       value >>= 7;
8654       if (value != 0)
8655         /* More bytes to follow.  */
8656         byte |= 0x80;
8657       CHECKSUM (byte);
8658       if (value == 0)
8659         break;
8660     }
8661 }
8662
8663 /* Checksum the context of the DIE.  This adds the names of any
8664    surrounding namespaces or structures to the checksum.  */
8665
8666 static void
8667 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8668 {
8669   const char *name;
8670   dw_die_ref spec;
8671   int tag = die->die_tag;
8672
8673   if (tag != DW_TAG_namespace
8674       && tag != DW_TAG_structure_type
8675       && tag != DW_TAG_class_type)
8676     return;
8677
8678   name = get_AT_string (die, DW_AT_name);
8679
8680   spec = get_AT_ref (die, DW_AT_specification);
8681   if (spec != NULL)
8682     die = spec;
8683
8684   if (die->die_parent != NULL)
8685     checksum_die_context (die->die_parent, ctx);
8686
8687   CHECKSUM_ULEB128 ('C');
8688   CHECKSUM_ULEB128 (tag);
8689   if (name != NULL)
8690     CHECKSUM_STRING (name);
8691 }
8692
8693 /* Calculate the checksum of a location expression.  */
8694
8695 static inline void
8696 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8697 {
8698   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8699      were emitted as a DW_FORM_sdata instead of a location expression.  */
8700   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8701     {
8702       CHECKSUM_ULEB128 (DW_FORM_sdata);
8703       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8704       return;
8705     }
8706
8707   /* Otherwise, just checksum the raw location expression.  */
8708   while (loc != NULL)
8709     {
8710       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8711       CHECKSUM (loc->dw_loc_oprnd1);
8712       CHECKSUM (loc->dw_loc_oprnd2);
8713       loc = loc->dw_loc_next;
8714     }
8715 }
8716
8717 /* Calculate the checksum of an attribute.  */
8718
8719 static void
8720 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8721                        struct md5_ctx *ctx, int *mark)
8722 {
8723   dw_loc_descr_ref loc;
8724   rtx r;
8725
8726   if (AT_class (at) == dw_val_class_die_ref)
8727     {
8728       dw_die_ref target_die = AT_ref (at);
8729
8730       /* For pointer and reference types, we checksum only the (qualified)
8731          name of the target type (if there is a name).  For friend entries,
8732          we checksum only the (qualified) name of the target type or function.
8733          This allows the checksum to remain the same whether the target type
8734          is complete or not.  */
8735       if ((at->dw_attr == DW_AT_type
8736            && (tag == DW_TAG_pointer_type
8737                || tag == DW_TAG_reference_type
8738                || tag == DW_TAG_rvalue_reference_type
8739                || tag == DW_TAG_ptr_to_member_type))
8740           || (at->dw_attr == DW_AT_friend
8741               && tag == DW_TAG_friend))
8742         {
8743           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8744
8745           if (name_attr != NULL)
8746             {
8747               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8748
8749               if (decl == NULL)
8750                 decl = target_die;
8751               CHECKSUM_ULEB128 ('N');
8752               CHECKSUM_ULEB128 (at->dw_attr);
8753               if (decl->die_parent != NULL)
8754                 checksum_die_context (decl->die_parent, ctx);
8755               CHECKSUM_ULEB128 ('E');
8756               CHECKSUM_STRING (AT_string (name_attr));
8757               return;
8758             }
8759         }
8760
8761       /* For all other references to another DIE, we check to see if the
8762          target DIE has already been visited.  If it has, we emit a
8763          backward reference; if not, we descend recursively.  */
8764       if (target_die->die_mark > 0)
8765         {
8766           CHECKSUM_ULEB128 ('R');
8767           CHECKSUM_ULEB128 (at->dw_attr);
8768           CHECKSUM_ULEB128 (target_die->die_mark);
8769         }
8770       else
8771         {
8772           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8773
8774           if (decl == NULL)
8775             decl = target_die;
8776           target_die->die_mark = ++(*mark);
8777           CHECKSUM_ULEB128 ('T');
8778           CHECKSUM_ULEB128 (at->dw_attr);
8779           if (decl->die_parent != NULL)
8780             checksum_die_context (decl->die_parent, ctx);
8781           die_checksum_ordered (target_die, ctx, mark);
8782         }
8783       return;
8784     }
8785
8786   CHECKSUM_ULEB128 ('A');
8787   CHECKSUM_ULEB128 (at->dw_attr);
8788
8789   switch (AT_class (at))
8790     {
8791     case dw_val_class_const:
8792       CHECKSUM_ULEB128 (DW_FORM_sdata);
8793       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8794       break;
8795
8796     case dw_val_class_unsigned_const:
8797       CHECKSUM_ULEB128 (DW_FORM_sdata);
8798       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8799       break;
8800
8801     case dw_val_class_const_double:
8802       CHECKSUM_ULEB128 (DW_FORM_block);
8803       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8804       CHECKSUM (at->dw_attr_val.v.val_double);
8805       break;
8806
8807     case dw_val_class_vec:
8808       CHECKSUM_ULEB128 (DW_FORM_block);
8809       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8810       CHECKSUM (at->dw_attr_val.v.val_vec);
8811       break;
8812
8813     case dw_val_class_flag:
8814       CHECKSUM_ULEB128 (DW_FORM_flag);
8815       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8816       break;
8817
8818     case dw_val_class_str:
8819       CHECKSUM_ULEB128 (DW_FORM_string);
8820       CHECKSUM_STRING (AT_string (at));
8821       break;
8822
8823     case dw_val_class_addr:
8824       r = AT_addr (at);
8825       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8826       CHECKSUM_ULEB128 (DW_FORM_string);
8827       CHECKSUM_STRING (XSTR (r, 0));
8828       break;
8829
8830     case dw_val_class_offset:
8831       CHECKSUM_ULEB128 (DW_FORM_sdata);
8832       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8833       break;
8834
8835     case dw_val_class_loc:
8836       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8837         loc_checksum_ordered (loc, ctx);
8838       break;
8839
8840     case dw_val_class_fde_ref:
8841     case dw_val_class_lbl_id:
8842     case dw_val_class_lineptr:
8843     case dw_val_class_macptr:
8844       break;
8845
8846     case dw_val_class_file:
8847       CHECKSUM_ULEB128 (DW_FORM_string);
8848       CHECKSUM_STRING (AT_file (at)->filename);
8849       break;
8850
8851     case dw_val_class_data8:
8852       CHECKSUM (at->dw_attr_val.v.val_data8);
8853       break;
8854
8855     default:
8856       break;
8857     }
8858 }
8859
8860 struct checksum_attributes
8861 {
8862   dw_attr_ref at_name;
8863   dw_attr_ref at_type;
8864   dw_attr_ref at_friend;
8865   dw_attr_ref at_accessibility;
8866   dw_attr_ref at_address_class;
8867   dw_attr_ref at_allocated;
8868   dw_attr_ref at_artificial;
8869   dw_attr_ref at_associated;
8870   dw_attr_ref at_binary_scale;
8871   dw_attr_ref at_bit_offset;
8872   dw_attr_ref at_bit_size;
8873   dw_attr_ref at_bit_stride;
8874   dw_attr_ref at_byte_size;
8875   dw_attr_ref at_byte_stride;
8876   dw_attr_ref at_const_value;
8877   dw_attr_ref at_containing_type;
8878   dw_attr_ref at_count;
8879   dw_attr_ref at_data_location;
8880   dw_attr_ref at_data_member_location;
8881   dw_attr_ref at_decimal_scale;
8882   dw_attr_ref at_decimal_sign;
8883   dw_attr_ref at_default_value;
8884   dw_attr_ref at_digit_count;
8885   dw_attr_ref at_discr;
8886   dw_attr_ref at_discr_list;
8887   dw_attr_ref at_discr_value;
8888   dw_attr_ref at_encoding;
8889   dw_attr_ref at_endianity;
8890   dw_attr_ref at_explicit;
8891   dw_attr_ref at_is_optional;
8892   dw_attr_ref at_location;
8893   dw_attr_ref at_lower_bound;
8894   dw_attr_ref at_mutable;
8895   dw_attr_ref at_ordering;
8896   dw_attr_ref at_picture_string;
8897   dw_attr_ref at_prototyped;
8898   dw_attr_ref at_small;
8899   dw_attr_ref at_segment;
8900   dw_attr_ref at_string_length;
8901   dw_attr_ref at_threads_scaled;
8902   dw_attr_ref at_upper_bound;
8903   dw_attr_ref at_use_location;
8904   dw_attr_ref at_use_UTF8;
8905   dw_attr_ref at_variable_parameter;
8906   dw_attr_ref at_virtuality;
8907   dw_attr_ref at_visibility;
8908   dw_attr_ref at_vtable_elem_location;
8909 };
8910
8911 /* Collect the attributes that we will want to use for the checksum.  */
8912
8913 static void
8914 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8915 {
8916   dw_attr_ref a;
8917   unsigned ix;
8918
8919   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8920     {
8921       switch (a->dw_attr)
8922         {
8923         case DW_AT_name:
8924           attrs->at_name = a;
8925           break;
8926         case DW_AT_type:
8927           attrs->at_type = a;
8928           break;
8929         case DW_AT_friend:
8930           attrs->at_friend = a;
8931           break;
8932         case DW_AT_accessibility:
8933           attrs->at_accessibility = a;
8934           break;
8935         case DW_AT_address_class:
8936           attrs->at_address_class = a;
8937           break;
8938         case DW_AT_allocated:
8939           attrs->at_allocated = a;
8940           break;
8941         case DW_AT_artificial:
8942           attrs->at_artificial = a;
8943           break;
8944         case DW_AT_associated:
8945           attrs->at_associated = a;
8946           break;
8947         case DW_AT_binary_scale:
8948           attrs->at_binary_scale = a;
8949           break;
8950         case DW_AT_bit_offset:
8951           attrs->at_bit_offset = a;
8952           break;
8953         case DW_AT_bit_size:
8954           attrs->at_bit_size = a;
8955           break;
8956         case DW_AT_bit_stride:
8957           attrs->at_bit_stride = a;
8958           break;
8959         case DW_AT_byte_size:
8960           attrs->at_byte_size = a;
8961           break;
8962         case DW_AT_byte_stride:
8963           attrs->at_byte_stride = a;
8964           break;
8965         case DW_AT_const_value:
8966           attrs->at_const_value = a;
8967           break;
8968         case DW_AT_containing_type:
8969           attrs->at_containing_type = a;
8970           break;
8971         case DW_AT_count:
8972           attrs->at_count = a;
8973           break;
8974         case DW_AT_data_location:
8975           attrs->at_data_location = a;
8976           break;
8977         case DW_AT_data_member_location:
8978           attrs->at_data_member_location = a;
8979           break;
8980         case DW_AT_decimal_scale:
8981           attrs->at_decimal_scale = a;
8982           break;
8983         case DW_AT_decimal_sign:
8984           attrs->at_decimal_sign = a;
8985           break;
8986         case DW_AT_default_value:
8987           attrs->at_default_value = a;
8988           break;
8989         case DW_AT_digit_count:
8990           attrs->at_digit_count = a;
8991           break;
8992         case DW_AT_discr:
8993           attrs->at_discr = a;
8994           break;
8995         case DW_AT_discr_list:
8996           attrs->at_discr_list = a;
8997           break;
8998         case DW_AT_discr_value:
8999           attrs->at_discr_value = a;
9000           break;
9001         case DW_AT_encoding:
9002           attrs->at_encoding = a;
9003           break;
9004         case DW_AT_endianity:
9005           attrs->at_endianity = a;
9006           break;
9007         case DW_AT_explicit:
9008           attrs->at_explicit = a;
9009           break;
9010         case DW_AT_is_optional:
9011           attrs->at_is_optional = a;
9012           break;
9013         case DW_AT_location:
9014           attrs->at_location = a;
9015           break;
9016         case DW_AT_lower_bound:
9017           attrs->at_lower_bound = a;
9018           break;
9019         case DW_AT_mutable:
9020           attrs->at_mutable = a;
9021           break;
9022         case DW_AT_ordering:
9023           attrs->at_ordering = a;
9024           break;
9025         case DW_AT_picture_string:
9026           attrs->at_picture_string = a;
9027           break;
9028         case DW_AT_prototyped:
9029           attrs->at_prototyped = a;
9030           break;
9031         case DW_AT_small:
9032           attrs->at_small = a;
9033           break;
9034         case DW_AT_segment:
9035           attrs->at_segment = a;
9036           break;
9037         case DW_AT_string_length:
9038           attrs->at_string_length = a;
9039           break;
9040         case DW_AT_threads_scaled:
9041           attrs->at_threads_scaled = a;
9042           break;
9043         case DW_AT_upper_bound:
9044           attrs->at_upper_bound = a;
9045           break;
9046         case DW_AT_use_location:
9047           attrs->at_use_location = a;
9048           break;
9049         case DW_AT_use_UTF8:
9050           attrs->at_use_UTF8 = a;
9051           break;
9052         case DW_AT_variable_parameter:
9053           attrs->at_variable_parameter = a;
9054           break;
9055         case DW_AT_virtuality:
9056           attrs->at_virtuality = a;
9057           break;
9058         case DW_AT_visibility:
9059           attrs->at_visibility = a;
9060           break;
9061         case DW_AT_vtable_elem_location:
9062           attrs->at_vtable_elem_location = a;
9063           break;
9064         default:
9065           break;
9066         }
9067     }
9068 }
9069
9070 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9071
9072 static void
9073 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9074 {
9075   dw_die_ref c;
9076   dw_die_ref decl;
9077   struct checksum_attributes attrs;
9078
9079   CHECKSUM_ULEB128 ('D');
9080   CHECKSUM_ULEB128 (die->die_tag);
9081
9082   memset (&attrs, 0, sizeof (attrs));
9083
9084   decl = get_AT_ref (die, DW_AT_specification);
9085   if (decl != NULL)
9086     collect_checksum_attributes (&attrs, decl);
9087   collect_checksum_attributes (&attrs, die);
9088
9089   CHECKSUM_ATTR (attrs.at_name);
9090   CHECKSUM_ATTR (attrs.at_accessibility);
9091   CHECKSUM_ATTR (attrs.at_address_class);
9092   CHECKSUM_ATTR (attrs.at_allocated);
9093   CHECKSUM_ATTR (attrs.at_artificial);
9094   CHECKSUM_ATTR (attrs.at_associated);
9095   CHECKSUM_ATTR (attrs.at_binary_scale);
9096   CHECKSUM_ATTR (attrs.at_bit_offset);
9097   CHECKSUM_ATTR (attrs.at_bit_size);
9098   CHECKSUM_ATTR (attrs.at_bit_stride);
9099   CHECKSUM_ATTR (attrs.at_byte_size);
9100   CHECKSUM_ATTR (attrs.at_byte_stride);
9101   CHECKSUM_ATTR (attrs.at_const_value);
9102   CHECKSUM_ATTR (attrs.at_containing_type);
9103   CHECKSUM_ATTR (attrs.at_count);
9104   CHECKSUM_ATTR (attrs.at_data_location);
9105   CHECKSUM_ATTR (attrs.at_data_member_location);
9106   CHECKSUM_ATTR (attrs.at_decimal_scale);
9107   CHECKSUM_ATTR (attrs.at_decimal_sign);
9108   CHECKSUM_ATTR (attrs.at_default_value);
9109   CHECKSUM_ATTR (attrs.at_digit_count);
9110   CHECKSUM_ATTR (attrs.at_discr);
9111   CHECKSUM_ATTR (attrs.at_discr_list);
9112   CHECKSUM_ATTR (attrs.at_discr_value);
9113   CHECKSUM_ATTR (attrs.at_encoding);
9114   CHECKSUM_ATTR (attrs.at_endianity);
9115   CHECKSUM_ATTR (attrs.at_explicit);
9116   CHECKSUM_ATTR (attrs.at_is_optional);
9117   CHECKSUM_ATTR (attrs.at_location);
9118   CHECKSUM_ATTR (attrs.at_lower_bound);
9119   CHECKSUM_ATTR (attrs.at_mutable);
9120   CHECKSUM_ATTR (attrs.at_ordering);
9121   CHECKSUM_ATTR (attrs.at_picture_string);
9122   CHECKSUM_ATTR (attrs.at_prototyped);
9123   CHECKSUM_ATTR (attrs.at_small);
9124   CHECKSUM_ATTR (attrs.at_segment);
9125   CHECKSUM_ATTR (attrs.at_string_length);
9126   CHECKSUM_ATTR (attrs.at_threads_scaled);
9127   CHECKSUM_ATTR (attrs.at_upper_bound);
9128   CHECKSUM_ATTR (attrs.at_use_location);
9129   CHECKSUM_ATTR (attrs.at_use_UTF8);
9130   CHECKSUM_ATTR (attrs.at_variable_parameter);
9131   CHECKSUM_ATTR (attrs.at_virtuality);
9132   CHECKSUM_ATTR (attrs.at_visibility);
9133   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9134   CHECKSUM_ATTR (attrs.at_type);
9135   CHECKSUM_ATTR (attrs.at_friend);
9136
9137   /* Checksum the child DIEs, except for nested types and member functions.  */
9138   c = die->die_child;
9139   if (c) do {
9140     dw_attr_ref name_attr;
9141
9142     c = c->die_sib;
9143     name_attr = get_AT (c, DW_AT_name);
9144     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9145         && name_attr != NULL)
9146       {
9147         CHECKSUM_ULEB128 ('S');
9148         CHECKSUM_ULEB128 (c->die_tag);
9149         CHECKSUM_STRING (AT_string (name_attr));
9150       }
9151     else
9152       {
9153         /* Mark this DIE so it gets processed when unmarking.  */
9154         if (c->die_mark == 0)
9155           c->die_mark = -1;
9156         die_checksum_ordered (c, ctx, mark);
9157       }
9158   } while (c != die->die_child);
9159
9160   CHECKSUM_ULEB128 (0);
9161 }
9162
9163 #undef CHECKSUM
9164 #undef CHECKSUM_STRING
9165 #undef CHECKSUM_ATTR
9166 #undef CHECKSUM_LEB128
9167 #undef CHECKSUM_ULEB128
9168
9169 /* Generate the type signature for DIE.  This is computed by generating an
9170    MD5 checksum over the DIE's tag, its relevant attributes, and its
9171    children.  Attributes that are references to other DIEs are processed
9172    by recursion, using the MARK field to prevent infinite recursion.
9173    If the DIE is nested inside a namespace or another type, we also
9174    need to include that context in the signature.  The lower 64 bits
9175    of the resulting MD5 checksum comprise the signature.  */
9176
9177 static void
9178 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9179 {
9180   int mark;
9181   const char *name;
9182   unsigned char checksum[16];
9183   struct md5_ctx ctx;
9184   dw_die_ref decl;
9185
9186   name = get_AT_string (die, DW_AT_name);
9187   decl = get_AT_ref (die, DW_AT_specification);
9188
9189   /* First, compute a signature for just the type name (and its surrounding
9190      context, if any.  This is stored in the type unit DIE for link-time
9191      ODR (one-definition rule) checking.  */
9192
9193   if (is_cxx() && name != NULL)
9194     {
9195       md5_init_ctx (&ctx);
9196
9197       /* Checksum the names of surrounding namespaces and structures.  */
9198       if (decl != NULL && decl->die_parent != NULL)
9199         checksum_die_context (decl->die_parent, &ctx);
9200
9201       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9202       md5_process_bytes (name, strlen (name) + 1, &ctx);
9203       md5_finish_ctx (&ctx, checksum);
9204
9205       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9206     }
9207
9208   /* Next, compute the complete type signature.  */
9209
9210   md5_init_ctx (&ctx);
9211   mark = 1;
9212   die->die_mark = mark;
9213
9214   /* Checksum the names of surrounding namespaces and structures.  */
9215   if (decl != NULL && decl->die_parent != NULL)
9216     checksum_die_context (decl->die_parent, &ctx);
9217
9218   /* Checksum the DIE and its children.  */
9219   die_checksum_ordered (die, &ctx, &mark);
9220   unmark_all_dies (die);
9221   md5_finish_ctx (&ctx, checksum);
9222
9223   /* Store the signature in the type node and link the type DIE and the
9224      type node together.  */
9225   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9226           DWARF_TYPE_SIGNATURE_SIZE);
9227   die->die_id.die_type_node = type_node;
9228   type_node->type_die = die;
9229
9230   /* If the DIE is a specification, link its declaration to the type node
9231      as well.  */
9232   if (decl != NULL)
9233     decl->die_id.die_type_node = type_node;
9234 }
9235
9236 /* Do the location expressions look same?  */
9237 static inline int
9238 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9239 {
9240   return loc1->dw_loc_opc == loc2->dw_loc_opc
9241          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9242          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9243 }
9244
9245 /* Do the values look the same?  */
9246 static int
9247 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9248 {
9249   dw_loc_descr_ref loc1, loc2;
9250   rtx r1, r2;
9251
9252   if (v1->val_class != v2->val_class)
9253     return 0;
9254
9255   switch (v1->val_class)
9256     {
9257     case dw_val_class_const:
9258       return v1->v.val_int == v2->v.val_int;
9259     case dw_val_class_unsigned_const:
9260       return v1->v.val_unsigned == v2->v.val_unsigned;
9261     case dw_val_class_const_double:
9262       return v1->v.val_double.high == v2->v.val_double.high
9263              && v1->v.val_double.low == v2->v.val_double.low;
9264     case dw_val_class_vec:
9265       if (v1->v.val_vec.length != v2->v.val_vec.length
9266           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9267         return 0;
9268       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9269                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9270         return 0;
9271       return 1;
9272     case dw_val_class_flag:
9273       return v1->v.val_flag == v2->v.val_flag;
9274     case dw_val_class_str:
9275       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9276
9277     case dw_val_class_addr:
9278       r1 = v1->v.val_addr;
9279       r2 = v2->v.val_addr;
9280       if (GET_CODE (r1) != GET_CODE (r2))
9281         return 0;
9282       return !rtx_equal_p (r1, r2);
9283
9284     case dw_val_class_offset:
9285       return v1->v.val_offset == v2->v.val_offset;
9286
9287     case dw_val_class_loc:
9288       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9289            loc1 && loc2;
9290            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9291         if (!same_loc_p (loc1, loc2, mark))
9292           return 0;
9293       return !loc1 && !loc2;
9294
9295     case dw_val_class_die_ref:
9296       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9297
9298     case dw_val_class_fde_ref:
9299     case dw_val_class_vms_delta:
9300     case dw_val_class_lbl_id:
9301     case dw_val_class_lineptr:
9302     case dw_val_class_macptr:
9303       return 1;
9304
9305     case dw_val_class_file:
9306       return v1->v.val_file == v2->v.val_file;
9307
9308     case dw_val_class_data8:
9309       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9310
9311     default:
9312       return 1;
9313     }
9314 }
9315
9316 /* Do the attributes look the same?  */
9317
9318 static int
9319 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9320 {
9321   if (at1->dw_attr != at2->dw_attr)
9322     return 0;
9323
9324   /* We don't care that this was compiled with a different compiler
9325      snapshot; if the output is the same, that's what matters. */
9326   if (at1->dw_attr == DW_AT_producer)
9327     return 1;
9328
9329   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9330 }
9331
9332 /* Do the dies look the same?  */
9333
9334 static int
9335 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9336 {
9337   dw_die_ref c1, c2;
9338   dw_attr_ref a1;
9339   unsigned ix;
9340
9341   /* To avoid infinite recursion.  */
9342   if (die1->die_mark)
9343     return die1->die_mark == die2->die_mark;
9344   die1->die_mark = die2->die_mark = ++(*mark);
9345
9346   if (die1->die_tag != die2->die_tag)
9347     return 0;
9348
9349   if (VEC_length (dw_attr_node, die1->die_attr)
9350       != VEC_length (dw_attr_node, die2->die_attr))
9351     return 0;
9352
9353   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9354     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9355       return 0;
9356
9357   c1 = die1->die_child;
9358   c2 = die2->die_child;
9359   if (! c1)
9360     {
9361       if (c2)
9362         return 0;
9363     }
9364   else
9365     for (;;)
9366       {
9367         if (!same_die_p (c1, c2, mark))
9368           return 0;
9369         c1 = c1->die_sib;
9370         c2 = c2->die_sib;
9371         if (c1 == die1->die_child)
9372           {
9373             if (c2 == die2->die_child)
9374               break;
9375             else
9376               return 0;
9377           }
9378     }
9379
9380   return 1;
9381 }
9382
9383 /* Do the dies look the same?  Wrapper around same_die_p.  */
9384
9385 static int
9386 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9387 {
9388   int mark = 0;
9389   int ret = same_die_p (die1, die2, &mark);
9390
9391   unmark_all_dies (die1);
9392   unmark_all_dies (die2);
9393
9394   return ret;
9395 }
9396
9397 /* The prefix to attach to symbols on DIEs in the current comdat debug
9398    info section.  */
9399 static char *comdat_symbol_id;
9400
9401 /* The index of the current symbol within the current comdat CU.  */
9402 static unsigned int comdat_symbol_number;
9403
9404 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9405    children, and set comdat_symbol_id accordingly.  */
9406
9407 static void
9408 compute_section_prefix (dw_die_ref unit_die)
9409 {
9410   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9411   const char *base = die_name ? lbasename (die_name) : "anonymous";
9412   char *name = XALLOCAVEC (char, strlen (base) + 64);
9413   char *p;
9414   int i, mark;
9415   unsigned char checksum[16];
9416   struct md5_ctx ctx;
9417
9418   /* Compute the checksum of the DIE, then append part of it as hex digits to
9419      the name filename of the unit.  */
9420
9421   md5_init_ctx (&ctx);
9422   mark = 0;
9423   die_checksum (unit_die, &ctx, &mark);
9424   unmark_all_dies (unit_die);
9425   md5_finish_ctx (&ctx, checksum);
9426
9427   sprintf (name, "%s.", base);
9428   clean_symbol_name (name);
9429
9430   p = name + strlen (name);
9431   for (i = 0; i < 4; i++)
9432     {
9433       sprintf (p, "%.2x", checksum[i]);
9434       p += 2;
9435     }
9436
9437   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9438   comdat_symbol_number = 0;
9439 }
9440
9441 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9442
9443 static int
9444 is_type_die (dw_die_ref die)
9445 {
9446   switch (die->die_tag)
9447     {
9448     case DW_TAG_array_type:
9449     case DW_TAG_class_type:
9450     case DW_TAG_interface_type:
9451     case DW_TAG_enumeration_type:
9452     case DW_TAG_pointer_type:
9453     case DW_TAG_reference_type:
9454     case DW_TAG_rvalue_reference_type:
9455     case DW_TAG_string_type:
9456     case DW_TAG_structure_type:
9457     case DW_TAG_subroutine_type:
9458     case DW_TAG_union_type:
9459     case DW_TAG_ptr_to_member_type:
9460     case DW_TAG_set_type:
9461     case DW_TAG_subrange_type:
9462     case DW_TAG_base_type:
9463     case DW_TAG_const_type:
9464     case DW_TAG_file_type:
9465     case DW_TAG_packed_type:
9466     case DW_TAG_volatile_type:
9467     case DW_TAG_typedef:
9468       return 1;
9469     default:
9470       return 0;
9471     }
9472 }
9473
9474 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9475    Basically, we want to choose the bits that are likely to be shared between
9476    compilations (types) and leave out the bits that are specific to individual
9477    compilations (functions).  */
9478
9479 static int
9480 is_comdat_die (dw_die_ref c)
9481 {
9482   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9483      we do for stabs.  The advantage is a greater likelihood of sharing between
9484      objects that don't include headers in the same order (and therefore would
9485      put the base types in a different comdat).  jason 8/28/00 */
9486
9487   if (c->die_tag == DW_TAG_base_type)
9488     return 0;
9489
9490   if (c->die_tag == DW_TAG_pointer_type
9491       || c->die_tag == DW_TAG_reference_type
9492       || c->die_tag == DW_TAG_rvalue_reference_type
9493       || c->die_tag == DW_TAG_const_type
9494       || c->die_tag == DW_TAG_volatile_type)
9495     {
9496       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9497
9498       return t ? is_comdat_die (t) : 0;
9499     }
9500
9501   return is_type_die (c);
9502 }
9503
9504 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9505    compilation unit.  */
9506
9507 static int
9508 is_symbol_die (dw_die_ref c)
9509 {
9510   return (is_type_die (c)
9511           || is_declaration_die (c)
9512           || c->die_tag == DW_TAG_namespace
9513           || c->die_tag == DW_TAG_module);
9514 }
9515
9516 /* Returns true iff C is a compile-unit DIE.  */
9517
9518 static inline bool
9519 is_cu_die (dw_die_ref c)
9520 {
9521   return c && c->die_tag == DW_TAG_compile_unit;
9522 }
9523
9524 static char *
9525 gen_internal_sym (const char *prefix)
9526 {
9527   char buf[256];
9528
9529   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9530   return xstrdup (buf);
9531 }
9532
9533 /* Assign symbols to all worthy DIEs under DIE.  */
9534
9535 static void
9536 assign_symbol_names (dw_die_ref die)
9537 {
9538   dw_die_ref c;
9539
9540   if (is_symbol_die (die))
9541     {
9542       if (comdat_symbol_id)
9543         {
9544           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9545
9546           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9547                    comdat_symbol_id, comdat_symbol_number++);
9548           die->die_id.die_symbol = xstrdup (p);
9549         }
9550       else
9551         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9552     }
9553
9554   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9555 }
9556
9557 struct cu_hash_table_entry
9558 {
9559   dw_die_ref cu;
9560   unsigned min_comdat_num, max_comdat_num;
9561   struct cu_hash_table_entry *next;
9562 };
9563
9564 /* Routines to manipulate hash table of CUs.  */
9565 static hashval_t
9566 htab_cu_hash (const void *of)
9567 {
9568   const struct cu_hash_table_entry *const entry =
9569     (const struct cu_hash_table_entry *) of;
9570
9571   return htab_hash_string (entry->cu->die_id.die_symbol);
9572 }
9573
9574 static int
9575 htab_cu_eq (const void *of1, const void *of2)
9576 {
9577   const struct cu_hash_table_entry *const entry1 =
9578     (const struct cu_hash_table_entry *) of1;
9579   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9580
9581   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9582 }
9583
9584 static void
9585 htab_cu_del (void *what)
9586 {
9587   struct cu_hash_table_entry *next,
9588     *entry = (struct cu_hash_table_entry *) what;
9589
9590   while (entry)
9591     {
9592       next = entry->next;
9593       free (entry);
9594       entry = next;
9595     }
9596 }
9597
9598 /* Check whether we have already seen this CU and set up SYM_NUM
9599    accordingly.  */
9600 static int
9601 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9602 {
9603   struct cu_hash_table_entry dummy;
9604   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9605
9606   dummy.max_comdat_num = 0;
9607
9608   slot = (struct cu_hash_table_entry **)
9609     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9610         INSERT);
9611   entry = *slot;
9612
9613   for (; entry; last = entry, entry = entry->next)
9614     {
9615       if (same_die_p_wrap (cu, entry->cu))
9616         break;
9617     }
9618
9619   if (entry)
9620     {
9621       *sym_num = entry->min_comdat_num;
9622       return 1;
9623     }
9624
9625   entry = XCNEW (struct cu_hash_table_entry);
9626   entry->cu = cu;
9627   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9628   entry->next = *slot;
9629   *slot = entry;
9630
9631   return 0;
9632 }
9633
9634 /* Record SYM_NUM to record of CU in HTABLE.  */
9635 static void
9636 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9637 {
9638   struct cu_hash_table_entry **slot, *entry;
9639
9640   slot = (struct cu_hash_table_entry **)
9641     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9642         NO_INSERT);
9643   entry = *slot;
9644
9645   entry->max_comdat_num = sym_num;
9646 }
9647
9648 /* Traverse the DIE (which is always comp_unit_die), and set up
9649    additional compilation units for each of the include files we see
9650    bracketed by BINCL/EINCL.  */
9651
9652 static void
9653 break_out_includes (dw_die_ref die)
9654 {
9655   dw_die_ref c;
9656   dw_die_ref unit = NULL;
9657   limbo_die_node *node, **pnode;
9658   htab_t cu_hash_table;
9659
9660   c = die->die_child;
9661   if (c) do {
9662     dw_die_ref prev = c;
9663     c = c->die_sib;
9664     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9665            || (unit && is_comdat_die (c)))
9666       {
9667         dw_die_ref next = c->die_sib;
9668
9669         /* This DIE is for a secondary CU; remove it from the main one.  */
9670         remove_child_with_prev (c, prev);
9671
9672         if (c->die_tag == DW_TAG_GNU_BINCL)
9673           unit = push_new_compile_unit (unit, c);
9674         else if (c->die_tag == DW_TAG_GNU_EINCL)
9675           unit = pop_compile_unit (unit);
9676         else
9677           add_child_die (unit, c);
9678         c = next;
9679         if (c == die->die_child)
9680           break;
9681       }
9682   } while (c != die->die_child);
9683
9684 #if 0
9685   /* We can only use this in debugging, since the frontend doesn't check
9686      to make sure that we leave every include file we enter.  */
9687   gcc_assert (!unit);
9688 #endif
9689
9690   assign_symbol_names (die);
9691   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9692   for (node = limbo_die_list, pnode = &limbo_die_list;
9693        node;
9694        node = node->next)
9695     {
9696       int is_dupl;
9697
9698       compute_section_prefix (node->die);
9699       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9700                         &comdat_symbol_number);
9701       assign_symbol_names (node->die);
9702       if (is_dupl)
9703         *pnode = node->next;
9704       else
9705         {
9706           pnode = &node->next;
9707           record_comdat_symbol_number (node->die, cu_hash_table,
9708                 comdat_symbol_number);
9709         }
9710     }
9711   htab_delete (cu_hash_table);
9712 }
9713
9714 /* Return non-zero if this DIE is a declaration.  */
9715
9716 static int
9717 is_declaration_die (dw_die_ref die)
9718 {
9719   dw_attr_ref a;
9720   unsigned ix;
9721
9722   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9723     if (a->dw_attr == DW_AT_declaration)
9724       return 1;
9725
9726   return 0;
9727 }
9728
9729 /* Return non-zero if this DIE is nested inside a subprogram.  */
9730
9731 static int
9732 is_nested_in_subprogram (dw_die_ref die)
9733 {
9734   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9735
9736   if (decl == NULL)
9737     decl = die;
9738   return local_scope_p (decl);
9739 }
9740
9741 /* Return non-zero if this is a type DIE that should be moved to a
9742    COMDAT .debug_types section.  */
9743
9744 static int
9745 should_move_die_to_comdat (dw_die_ref die)
9746 {
9747   switch (die->die_tag)
9748     {
9749     case DW_TAG_class_type:
9750     case DW_TAG_structure_type:
9751     case DW_TAG_enumeration_type:
9752     case DW_TAG_union_type:
9753       /* Don't move declarations, inlined instances, or types nested in a
9754          subprogram.  */
9755       if (is_declaration_die (die)
9756           || get_AT (die, DW_AT_abstract_origin)
9757           || is_nested_in_subprogram (die))
9758         return 0;
9759       return 1;
9760     case DW_TAG_array_type:
9761     case DW_TAG_interface_type:
9762     case DW_TAG_pointer_type:
9763     case DW_TAG_reference_type:
9764     case DW_TAG_rvalue_reference_type:
9765     case DW_TAG_string_type:
9766     case DW_TAG_subroutine_type:
9767     case DW_TAG_ptr_to_member_type:
9768     case DW_TAG_set_type:
9769     case DW_TAG_subrange_type:
9770     case DW_TAG_base_type:
9771     case DW_TAG_const_type:
9772     case DW_TAG_file_type:
9773     case DW_TAG_packed_type:
9774     case DW_TAG_volatile_type:
9775     case DW_TAG_typedef:
9776     default:
9777       return 0;
9778     }
9779 }
9780
9781 /* Make a clone of DIE.  */
9782
9783 static dw_die_ref
9784 clone_die (dw_die_ref die)
9785 {
9786   dw_die_ref clone;
9787   dw_attr_ref a;
9788   unsigned ix;
9789
9790   clone = ggc_alloc_cleared_die_node ();
9791   clone->die_tag = die->die_tag;
9792
9793   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9794     add_dwarf_attr (clone, a);
9795
9796   return clone;
9797 }
9798
9799 /* Make a clone of the tree rooted at DIE.  */
9800
9801 static dw_die_ref
9802 clone_tree (dw_die_ref die)
9803 {
9804   dw_die_ref c;
9805   dw_die_ref clone = clone_die (die);
9806
9807   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9808
9809   return clone;
9810 }
9811
9812 /* Make a clone of DIE as a declaration.  */
9813
9814 static dw_die_ref
9815 clone_as_declaration (dw_die_ref die)
9816 {
9817   dw_die_ref clone;
9818   dw_die_ref decl;
9819   dw_attr_ref a;
9820   unsigned ix;
9821
9822   /* If the DIE is already a declaration, just clone it.  */
9823   if (is_declaration_die (die))
9824     return clone_die (die);
9825
9826   /* If the DIE is a specification, just clone its declaration DIE.  */
9827   decl = get_AT_ref (die, DW_AT_specification);
9828   if (decl != NULL)
9829     return clone_die (decl);
9830
9831   clone = ggc_alloc_cleared_die_node ();
9832   clone->die_tag = die->die_tag;
9833
9834   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9835     {
9836       /* We don't want to copy over all attributes.
9837          For example we don't want DW_AT_byte_size because otherwise we will no
9838          longer have a declaration and GDB will treat it as a definition.  */
9839
9840       switch (a->dw_attr)
9841         {
9842         case DW_AT_artificial:
9843         case DW_AT_containing_type:
9844         case DW_AT_external:
9845         case DW_AT_name:
9846         case DW_AT_type:
9847         case DW_AT_virtuality:
9848         case DW_AT_linkage_name:
9849         case DW_AT_MIPS_linkage_name:
9850           add_dwarf_attr (clone, a);
9851           break;
9852         case DW_AT_byte_size:
9853         default:
9854           break;
9855         }
9856     }
9857
9858   if (die->die_id.die_type_node)
9859     add_AT_die_ref (clone, DW_AT_signature, die);
9860
9861   add_AT_flag (clone, DW_AT_declaration, 1);
9862   return clone;
9863 }
9864
9865 /* Copy the declaration context to the new compile unit DIE.  This includes
9866    any surrounding namespace or type declarations.  If the DIE has an
9867    AT_specification attribute, it also includes attributes and children
9868    attached to the specification.  */
9869
9870 static void
9871 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9872 {
9873   dw_die_ref decl;
9874   dw_die_ref new_decl;
9875
9876   decl = get_AT_ref (die, DW_AT_specification);
9877   if (decl == NULL)
9878     decl = die;
9879   else
9880     {
9881       unsigned ix;
9882       dw_die_ref c;
9883       dw_attr_ref a;
9884
9885       /* Copy the type node pointer from the new DIE to the original
9886          declaration DIE so we can forward references later.  */
9887       decl->die_id.die_type_node = die->die_id.die_type_node;
9888
9889       remove_AT (die, DW_AT_specification);
9890
9891       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
9892         {
9893           if (a->dw_attr != DW_AT_name
9894               && a->dw_attr != DW_AT_declaration
9895               && a->dw_attr != DW_AT_external)
9896             add_dwarf_attr (die, a);
9897         }
9898
9899       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9900     }
9901
9902   if (decl->die_parent != NULL
9903       && decl->die_parent->die_tag != DW_TAG_compile_unit
9904       && decl->die_parent->die_tag != DW_TAG_type_unit)
9905     {
9906       new_decl = copy_ancestor_tree (unit, decl, NULL);
9907       if (new_decl != NULL)
9908         {
9909           remove_AT (new_decl, DW_AT_signature);
9910           add_AT_specification (die, new_decl);
9911         }
9912     }
9913 }
9914
9915 /* Generate the skeleton ancestor tree for the given NODE, then clone
9916    the DIE and add the clone into the tree.  */
9917
9918 static void
9919 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9920 {
9921   if (node->new_die != NULL)
9922     return;
9923
9924   node->new_die = clone_as_declaration (node->old_die);
9925
9926   if (node->parent != NULL)
9927     {
9928       generate_skeleton_ancestor_tree (node->parent);
9929       add_child_die (node->parent->new_die, node->new_die);
9930     }
9931 }
9932
9933 /* Generate a skeleton tree of DIEs containing any declarations that are
9934    found in the original tree.  We traverse the tree looking for declaration
9935    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9936
9937 static void
9938 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9939 {
9940   skeleton_chain_node node;
9941   dw_die_ref c;
9942   dw_die_ref first;
9943   dw_die_ref prev = NULL;
9944   dw_die_ref next = NULL;
9945
9946   node.parent = parent;
9947
9948   first = c = parent->old_die->die_child;
9949   if (c)
9950     next = c->die_sib;
9951   if (c) do {
9952     if (prev == NULL || prev->die_sib == c)
9953       prev = c;
9954     c = next;
9955     next = (c == first ? NULL : c->die_sib);
9956     node.old_die = c;
9957     node.new_die = NULL;
9958     if (is_declaration_die (c))
9959       {
9960         /* Clone the existing DIE, move the original to the skeleton
9961            tree (which is in the main CU), and put the clone, with
9962            all the original's children, where the original came from.  */
9963         dw_die_ref clone = clone_die (c);
9964         move_all_children (c, clone);
9965
9966         replace_child (c, clone, prev);
9967         generate_skeleton_ancestor_tree (parent);
9968         add_child_die (parent->new_die, c);
9969         node.new_die = c;
9970         c = clone;
9971       }
9972     generate_skeleton_bottom_up (&node);
9973   } while (next != NULL);
9974 }
9975
9976 /* Wrapper function for generate_skeleton_bottom_up.  */
9977
9978 static dw_die_ref
9979 generate_skeleton (dw_die_ref die)
9980 {
9981   skeleton_chain_node node;
9982
9983   node.old_die = die;
9984   node.new_die = NULL;
9985   node.parent = NULL;
9986
9987   /* If this type definition is nested inside another type,
9988      always leave at least a declaration in its place.  */
9989   if (die->die_parent != NULL && is_type_die (die->die_parent))
9990     node.new_die = clone_as_declaration (die);
9991
9992   generate_skeleton_bottom_up (&node);
9993   return node.new_die;
9994 }
9995
9996 /* Remove the DIE from its parent, possibly replacing it with a cloned
9997    declaration.  The original DIE will be moved to a new compile unit
9998    so that existing references to it follow it to the new location.  If
9999    any of the original DIE's descendants is a declaration, we need to
10000    replace the original DIE with a skeleton tree and move the
10001    declarations back into the skeleton tree.  */
10002
10003 static dw_die_ref
10004 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10005 {
10006   dw_die_ref skeleton;
10007
10008   skeleton = generate_skeleton (child);
10009   if (skeleton == NULL)
10010     remove_child_with_prev (child, prev);
10011   else
10012     {
10013       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10014       replace_child (child, skeleton, prev);
10015     }
10016
10017   return skeleton;
10018 }
10019
10020 /* Traverse the DIE and set up additional .debug_types sections for each
10021    type worthy of being placed in a COMDAT section.  */
10022
10023 static void
10024 break_out_comdat_types (dw_die_ref die)
10025 {
10026   dw_die_ref c;
10027   dw_die_ref first;
10028   dw_die_ref prev = NULL;
10029   dw_die_ref next = NULL;
10030   dw_die_ref unit = NULL;
10031
10032   first = c = die->die_child;
10033   if (c)
10034     next = c->die_sib;
10035   if (c) do {
10036     if (prev == NULL || prev->die_sib == c)
10037       prev = c;
10038     c = next;
10039     next = (c == first ? NULL : c->die_sib);
10040     if (should_move_die_to_comdat (c))
10041       {
10042         dw_die_ref replacement;
10043         comdat_type_node_ref type_node;
10044
10045         /* Create a new type unit DIE as the root for the new tree, and
10046            add it to the list of comdat types.  */
10047         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10048         add_AT_unsigned (unit, DW_AT_language,
10049                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10050         type_node = ggc_alloc_cleared_comdat_type_node ();
10051         type_node->root_die = unit;
10052         type_node->next = comdat_type_list;
10053         comdat_type_list = type_node;
10054
10055         /* Generate the type signature.  */
10056         generate_type_signature (c, type_node);
10057
10058         /* Copy the declaration context, attributes, and children of the
10059            declaration into the new compile unit DIE.  */
10060         copy_declaration_context (unit, c);
10061
10062         /* Remove this DIE from the main CU.  */
10063         replacement = remove_child_or_replace_with_skeleton (c, prev);
10064
10065         /* Break out nested types into their own type units.  */
10066         break_out_comdat_types (c);
10067
10068         /* Add the DIE to the new compunit.  */
10069         add_child_die (unit, c);
10070
10071         if (replacement != NULL)
10072           c = replacement;
10073       }
10074     else if (c->die_tag == DW_TAG_namespace
10075              || c->die_tag == DW_TAG_class_type
10076              || c->die_tag == DW_TAG_structure_type
10077              || c->die_tag == DW_TAG_union_type)
10078       {
10079         /* Look for nested types that can be broken out.  */
10080         break_out_comdat_types (c);
10081       }
10082   } while (next != NULL);
10083 }
10084
10085 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10086
10087 struct decl_table_entry
10088 {
10089   dw_die_ref orig;
10090   dw_die_ref copy;
10091 };
10092
10093 /* Routines to manipulate hash table of copied declarations.  */
10094
10095 static hashval_t
10096 htab_decl_hash (const void *of)
10097 {
10098   const struct decl_table_entry *const entry =
10099     (const struct decl_table_entry *) of;
10100
10101   return htab_hash_pointer (entry->orig);
10102 }
10103
10104 static int
10105 htab_decl_eq (const void *of1, const void *of2)
10106 {
10107   const struct decl_table_entry *const entry1 =
10108     (const struct decl_table_entry *) of1;
10109   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10110
10111   return entry1->orig == entry2;
10112 }
10113
10114 static void
10115 htab_decl_del (void *what)
10116 {
10117   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10118
10119   free (entry);
10120 }
10121
10122 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10123    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10124    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10125    to check if the ancestor has already been copied into UNIT.  */
10126
10127 static dw_die_ref
10128 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10129 {
10130   dw_die_ref parent = die->die_parent;
10131   dw_die_ref new_parent = unit;
10132   dw_die_ref copy;
10133   void **slot = NULL;
10134   struct decl_table_entry *entry = NULL;
10135
10136   if (decl_table)
10137     {
10138       /* Check if the entry has already been copied to UNIT.  */
10139       slot = htab_find_slot_with_hash (decl_table, die,
10140                                        htab_hash_pointer (die), INSERT);
10141       if (*slot != HTAB_EMPTY_ENTRY)
10142         {
10143           entry = (struct decl_table_entry *) *slot;
10144           return entry->copy;
10145         }
10146
10147       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10148       entry = XCNEW (struct decl_table_entry);
10149       entry->orig = die;
10150       entry->copy = NULL;
10151       *slot = entry;
10152     }
10153
10154   if (parent != NULL)
10155     {
10156       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10157       if (spec != NULL)
10158         parent = spec;
10159       if (parent->die_tag != DW_TAG_compile_unit
10160           && parent->die_tag != DW_TAG_type_unit)
10161         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10162     }
10163
10164   copy = clone_as_declaration (die);
10165   add_child_die (new_parent, copy);
10166
10167   if (decl_table != NULL)
10168     {
10169       /* Record the pointer to the copy.  */
10170       entry->copy = copy;
10171     }
10172
10173   return copy;
10174 }
10175
10176 /* Walk the DIE and its children, looking for references to incomplete
10177    or trivial types that are unmarked (i.e., that are not in the current
10178    type_unit).  */
10179
10180 static void
10181 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10182 {
10183   dw_die_ref c;
10184   dw_attr_ref a;
10185   unsigned ix;
10186
10187   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10188     {
10189       if (AT_class (a) == dw_val_class_die_ref)
10190         {
10191           dw_die_ref targ = AT_ref (a);
10192           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10193           void **slot;
10194           struct decl_table_entry *entry;
10195
10196           if (targ->die_mark != 0 || type_node != NULL)
10197             continue;
10198
10199           slot = htab_find_slot_with_hash (decl_table, targ,
10200                                            htab_hash_pointer (targ), INSERT);
10201
10202           if (*slot != HTAB_EMPTY_ENTRY)
10203             {
10204               /* TARG has already been copied, so we just need to
10205                  modify the reference to point to the copy.  */
10206               entry = (struct decl_table_entry *) *slot;
10207               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10208             }
10209           else
10210             {
10211               dw_die_ref parent = unit;
10212               dw_die_ref copy = clone_tree (targ);
10213
10214               /* Make sure the cloned tree is marked as part of the
10215                  type unit.  */
10216               mark_dies (copy);
10217
10218               /* Record in DECL_TABLE that TARG has been copied.
10219                  Need to do this now, before the recursive call,
10220                  because DECL_TABLE may be expanded and SLOT
10221                  would no longer be a valid pointer.  */
10222               entry = XCNEW (struct decl_table_entry);
10223               entry->orig = targ;
10224               entry->copy = copy;
10225               *slot = entry;
10226
10227               /* If TARG has surrounding context, copy its ancestor tree
10228                  into the new type unit.  */
10229               if (targ->die_parent != NULL
10230                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10231                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10232                 parent = copy_ancestor_tree (unit, targ->die_parent,
10233                                              decl_table);
10234
10235               add_child_die (parent, copy);
10236               a->dw_attr_val.v.val_die_ref.die = copy;
10237
10238               /* Make sure the newly-copied DIE is walked.  If it was
10239                  installed in a previously-added context, it won't
10240                  get visited otherwise.  */
10241               if (parent != unit)
10242                 {
10243                   /* Find the highest point of the newly-added tree,
10244                      mark each node along the way, and walk from there.  */
10245                   parent->die_mark = 1;
10246                   while (parent->die_parent
10247                          && parent->die_parent->die_mark == 0)
10248                     {
10249                       parent = parent->die_parent;
10250                       parent->die_mark = 1;
10251                     }
10252                   copy_decls_walk (unit, parent, decl_table);
10253                 }
10254             }
10255         }
10256     }
10257
10258   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10259 }
10260
10261 /* Copy declarations for "unworthy" types into the new comdat section.
10262    Incomplete types, modified types, and certain other types aren't broken
10263    out into comdat sections of their own, so they don't have a signature,
10264    and we need to copy the declaration into the same section so that we
10265    don't have an external reference.  */
10266
10267 static void
10268 copy_decls_for_unworthy_types (dw_die_ref unit)
10269 {
10270   htab_t decl_table;
10271
10272   mark_dies (unit);
10273   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10274   copy_decls_walk (unit, unit, decl_table);
10275   htab_delete (decl_table);
10276   unmark_dies (unit);
10277 }
10278
10279 /* Traverse the DIE and add a sibling attribute if it may have the
10280    effect of speeding up access to siblings.  To save some space,
10281    avoid generating sibling attributes for DIE's without children.  */
10282
10283 static void
10284 add_sibling_attributes (dw_die_ref die)
10285 {
10286   dw_die_ref c;
10287
10288   if (! die->die_child)
10289     return;
10290
10291   if (die->die_parent && die != die->die_parent->die_child)
10292     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10293
10294   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10295 }
10296
10297 /* Output all location lists for the DIE and its children.  */
10298
10299 static void
10300 output_location_lists (dw_die_ref die)
10301 {
10302   dw_die_ref c;
10303   dw_attr_ref a;
10304   unsigned ix;
10305
10306   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10307     if (AT_class (a) == dw_val_class_loc_list)
10308       output_loc_list (AT_loc_list (a));
10309
10310   FOR_EACH_CHILD (die, c, output_location_lists (c));
10311 }
10312
10313 /* The format of each DIE (and its attribute value pairs) is encoded in an
10314    abbreviation table.  This routine builds the abbreviation table and assigns
10315    a unique abbreviation id for each abbreviation entry.  The children of each
10316    die are visited recursively.  */
10317
10318 static void
10319 build_abbrev_table (dw_die_ref die)
10320 {
10321   unsigned long abbrev_id;
10322   unsigned int n_alloc;
10323   dw_die_ref c;
10324   dw_attr_ref a;
10325   unsigned ix;
10326
10327   /* Scan the DIE references, and mark as external any that refer to
10328      DIEs from other CUs (i.e. those which are not marked).  */
10329   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10330     if (AT_class (a) == dw_val_class_die_ref
10331         && AT_ref (a)->die_mark == 0)
10332       {
10333         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10334         set_AT_ref_external (a, 1);
10335       }
10336
10337   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10338     {
10339       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10340       dw_attr_ref die_a, abbrev_a;
10341       unsigned ix;
10342       bool ok = true;
10343
10344       if (abbrev->die_tag != die->die_tag)
10345         continue;
10346       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10347         continue;
10348
10349       if (VEC_length (dw_attr_node, abbrev->die_attr)
10350           != VEC_length (dw_attr_node, die->die_attr))
10351         continue;
10352
10353       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10354         {
10355           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10356           if ((abbrev_a->dw_attr != die_a->dw_attr)
10357               || (value_format (abbrev_a) != value_format (die_a)))
10358             {
10359               ok = false;
10360               break;
10361             }
10362         }
10363       if (ok)
10364         break;
10365     }
10366
10367   if (abbrev_id >= abbrev_die_table_in_use)
10368     {
10369       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10370         {
10371           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10372           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10373                                             n_alloc);
10374
10375           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10376                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10377           abbrev_die_table_allocated = n_alloc;
10378         }
10379
10380       ++abbrev_die_table_in_use;
10381       abbrev_die_table[abbrev_id] = die;
10382     }
10383
10384   die->die_abbrev = abbrev_id;
10385   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10386 }
10387 \f
10388 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10389
10390 static int
10391 constant_size (unsigned HOST_WIDE_INT value)
10392 {
10393   int log;
10394
10395   if (value == 0)
10396     log = 0;
10397   else
10398     log = floor_log2 (value);
10399
10400   log = log / 8;
10401   log = 1 << (floor_log2 (log) + 1);
10402
10403   return log;
10404 }
10405
10406 /* Return the size of a DIE as it is represented in the
10407    .debug_info section.  */
10408
10409 static unsigned long
10410 size_of_die (dw_die_ref die)
10411 {
10412   unsigned long size = 0;
10413   dw_attr_ref a;
10414   unsigned ix;
10415
10416   size += size_of_uleb128 (die->die_abbrev);
10417   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10418     {
10419       switch (AT_class (a))
10420         {
10421         case dw_val_class_addr:
10422           size += DWARF2_ADDR_SIZE;
10423           break;
10424         case dw_val_class_offset:
10425           size += DWARF_OFFSET_SIZE;
10426           break;
10427         case dw_val_class_loc:
10428           {
10429             unsigned long lsize = size_of_locs (AT_loc (a));
10430
10431             /* Block length.  */
10432             if (dwarf_version >= 4)
10433               size += size_of_uleb128 (lsize);
10434             else
10435               size += constant_size (lsize);
10436             size += lsize;
10437           }
10438           break;
10439         case dw_val_class_loc_list:
10440           size += DWARF_OFFSET_SIZE;
10441           break;
10442         case dw_val_class_range_list:
10443           size += DWARF_OFFSET_SIZE;
10444           break;
10445         case dw_val_class_const:
10446           size += size_of_sleb128 (AT_int (a));
10447           break;
10448         case dw_val_class_unsigned_const:
10449           size += constant_size (AT_unsigned (a));
10450           break;
10451         case dw_val_class_const_double:
10452           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10453           if (HOST_BITS_PER_WIDE_INT >= 64)
10454             size++; /* block */
10455           break;
10456         case dw_val_class_vec:
10457           size += constant_size (a->dw_attr_val.v.val_vec.length
10458                                  * a->dw_attr_val.v.val_vec.elt_size)
10459                   + a->dw_attr_val.v.val_vec.length
10460                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10461           break;
10462         case dw_val_class_flag:
10463           if (dwarf_version >= 4)
10464             /* Currently all add_AT_flag calls pass in 1 as last argument,
10465                so DW_FORM_flag_present can be used.  If that ever changes,
10466                we'll need to use DW_FORM_flag and have some optimization
10467                in build_abbrev_table that will change those to
10468                DW_FORM_flag_present if it is set to 1 in all DIEs using
10469                the same abbrev entry.  */
10470             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10471           else
10472             size += 1;
10473           break;
10474         case dw_val_class_die_ref:
10475           if (AT_ref_external (a))
10476             {
10477               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10478                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10479                  is sized by target address length, whereas in DWARF3
10480                  it's always sized as an offset.  */
10481               if (dwarf_version >= 4)
10482                 size += DWARF_TYPE_SIGNATURE_SIZE;
10483               else if (dwarf_version == 2)
10484                 size += DWARF2_ADDR_SIZE;
10485               else
10486                 size += DWARF_OFFSET_SIZE;
10487             }
10488           else
10489             size += DWARF_OFFSET_SIZE;
10490           break;
10491         case dw_val_class_fde_ref:
10492           size += DWARF_OFFSET_SIZE;
10493           break;
10494         case dw_val_class_lbl_id:
10495           size += DWARF2_ADDR_SIZE;
10496           break;
10497         case dw_val_class_lineptr:
10498         case dw_val_class_macptr:
10499           size += DWARF_OFFSET_SIZE;
10500           break;
10501         case dw_val_class_str:
10502           if (AT_string_form (a) == DW_FORM_strp)
10503             size += DWARF_OFFSET_SIZE;
10504           else
10505             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10506           break;
10507         case dw_val_class_file:
10508           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10509           break;
10510         case dw_val_class_data8:
10511           size += 8;
10512           break;
10513         case dw_val_class_vms_delta:
10514           size += DWARF_OFFSET_SIZE;
10515           break;
10516         default:
10517           gcc_unreachable ();
10518         }
10519     }
10520
10521   return size;
10522 }
10523
10524 /* Size the debugging information associated with a given DIE.  Visits the
10525    DIE's children recursively.  Updates the global variable next_die_offset, on
10526    each time through.  Uses the current value of next_die_offset to update the
10527    die_offset field in each DIE.  */
10528
10529 static void
10530 calc_die_sizes (dw_die_ref die)
10531 {
10532   dw_die_ref c;
10533
10534   die->die_offset = next_die_offset;
10535   next_die_offset += size_of_die (die);
10536
10537   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10538
10539   if (die->die_child != NULL)
10540     /* Count the null byte used to terminate sibling lists.  */
10541     next_die_offset += 1;
10542 }
10543
10544 /* Set the marks for a die and its children.  We do this so
10545    that we know whether or not a reference needs to use FORM_ref_addr; only
10546    DIEs in the same CU will be marked.  We used to clear out the offset
10547    and use that as the flag, but ran into ordering problems.  */
10548
10549 static void
10550 mark_dies (dw_die_ref die)
10551 {
10552   dw_die_ref c;
10553
10554   gcc_assert (!die->die_mark);
10555
10556   die->die_mark = 1;
10557   FOR_EACH_CHILD (die, c, mark_dies (c));
10558 }
10559
10560 /* Clear the marks for a die and its children.  */
10561
10562 static void
10563 unmark_dies (dw_die_ref die)
10564 {
10565   dw_die_ref c;
10566
10567   if (dwarf_version < 4)
10568     gcc_assert (die->die_mark);
10569
10570   die->die_mark = 0;
10571   FOR_EACH_CHILD (die, c, unmark_dies (c));
10572 }
10573
10574 /* Clear the marks for a die, its children and referred dies.  */
10575
10576 static void
10577 unmark_all_dies (dw_die_ref die)
10578 {
10579   dw_die_ref c;
10580   dw_attr_ref a;
10581   unsigned ix;
10582
10583   if (!die->die_mark)
10584     return;
10585   die->die_mark = 0;
10586
10587   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10588
10589   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10590     if (AT_class (a) == dw_val_class_die_ref)
10591       unmark_all_dies (AT_ref (a));
10592 }
10593
10594 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10595    generated for the compilation unit.  */
10596
10597 static unsigned long
10598 size_of_pubnames (VEC (pubname_entry, gc) * names)
10599 {
10600   unsigned long size;
10601   unsigned i;
10602   pubname_ref p;
10603
10604   size = DWARF_PUBNAMES_HEADER_SIZE;
10605   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10606     if (names != pubtype_table
10607         || p->die->die_offset != 0
10608         || !flag_eliminate_unused_debug_types)
10609       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10610
10611   size += DWARF_OFFSET_SIZE;
10612   return size;
10613 }
10614
10615 /* Return the size of the information in the .debug_aranges section.  */
10616
10617 static unsigned long
10618 size_of_aranges (void)
10619 {
10620   unsigned long size;
10621
10622   size = DWARF_ARANGES_HEADER_SIZE;
10623
10624   /* Count the address/length pair for this compilation unit.  */
10625   if (text_section_used)
10626     size += 2 * DWARF2_ADDR_SIZE;
10627   if (cold_text_section_used)
10628     size += 2 * DWARF2_ADDR_SIZE;
10629   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10630
10631   /* Count the two zero words used to terminated the address range table.  */
10632   size += 2 * DWARF2_ADDR_SIZE;
10633   return size;
10634 }
10635 \f
10636 /* Select the encoding of an attribute value.  */
10637
10638 static enum dwarf_form
10639 value_format (dw_attr_ref a)
10640 {
10641   switch (a->dw_attr_val.val_class)
10642     {
10643     case dw_val_class_addr:
10644       /* Only very few attributes allow DW_FORM_addr.  */
10645       switch (a->dw_attr)
10646         {
10647         case DW_AT_low_pc:
10648         case DW_AT_high_pc:
10649         case DW_AT_entry_pc:
10650         case DW_AT_trampoline:
10651           return DW_FORM_addr;
10652         default:
10653           break;
10654         }
10655       switch (DWARF2_ADDR_SIZE)
10656         {
10657         case 1:
10658           return DW_FORM_data1;
10659         case 2:
10660           return DW_FORM_data2;
10661         case 4:
10662           return DW_FORM_data4;
10663         case 8:
10664           return DW_FORM_data8;
10665         default:
10666           gcc_unreachable ();
10667         }
10668     case dw_val_class_range_list:
10669     case dw_val_class_loc_list:
10670       if (dwarf_version >= 4)
10671         return DW_FORM_sec_offset;
10672       /* FALLTHRU */
10673     case dw_val_class_vms_delta:
10674     case dw_val_class_offset:
10675       switch (DWARF_OFFSET_SIZE)
10676         {
10677         case 4:
10678           return DW_FORM_data4;
10679         case 8:
10680           return DW_FORM_data8;
10681         default:
10682           gcc_unreachable ();
10683         }
10684     case dw_val_class_loc:
10685       if (dwarf_version >= 4)
10686         return DW_FORM_exprloc;
10687       switch (constant_size (size_of_locs (AT_loc (a))))
10688         {
10689         case 1:
10690           return DW_FORM_block1;
10691         case 2:
10692           return DW_FORM_block2;
10693         default:
10694           gcc_unreachable ();
10695         }
10696     case dw_val_class_const:
10697       return DW_FORM_sdata;
10698     case dw_val_class_unsigned_const:
10699       switch (constant_size (AT_unsigned (a)))
10700         {
10701         case 1:
10702           return DW_FORM_data1;
10703         case 2:
10704           return DW_FORM_data2;
10705         case 4:
10706           return DW_FORM_data4;
10707         case 8:
10708           return DW_FORM_data8;
10709         default:
10710           gcc_unreachable ();
10711         }
10712     case dw_val_class_const_double:
10713       switch (HOST_BITS_PER_WIDE_INT)
10714         {
10715         case 8:
10716           return DW_FORM_data2;
10717         case 16:
10718           return DW_FORM_data4;
10719         case 32:
10720           return DW_FORM_data8;
10721         case 64:
10722         default:
10723           return DW_FORM_block1;
10724         }
10725     case dw_val_class_vec:
10726       switch (constant_size (a->dw_attr_val.v.val_vec.length
10727                              * a->dw_attr_val.v.val_vec.elt_size))
10728         {
10729         case 1:
10730           return DW_FORM_block1;
10731         case 2:
10732           return DW_FORM_block2;
10733         case 4:
10734           return DW_FORM_block4;
10735         default:
10736           gcc_unreachable ();
10737         }
10738     case dw_val_class_flag:
10739       if (dwarf_version >= 4)
10740         {
10741           /* Currently all add_AT_flag calls pass in 1 as last argument,
10742              so DW_FORM_flag_present can be used.  If that ever changes,
10743              we'll need to use DW_FORM_flag and have some optimization
10744              in build_abbrev_table that will change those to
10745              DW_FORM_flag_present if it is set to 1 in all DIEs using
10746              the same abbrev entry.  */
10747           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10748           return DW_FORM_flag_present;
10749         }
10750       return DW_FORM_flag;
10751     case dw_val_class_die_ref:
10752       if (AT_ref_external (a))
10753         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10754       else
10755         return DW_FORM_ref;
10756     case dw_val_class_fde_ref:
10757       return DW_FORM_data;
10758     case dw_val_class_lbl_id:
10759       return DW_FORM_addr;
10760     case dw_val_class_lineptr:
10761     case dw_val_class_macptr:
10762       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10763     case dw_val_class_str:
10764       return AT_string_form (a);
10765     case dw_val_class_file:
10766       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10767         {
10768         case 1:
10769           return DW_FORM_data1;
10770         case 2:
10771           return DW_FORM_data2;
10772         case 4:
10773           return DW_FORM_data4;
10774         default:
10775           gcc_unreachable ();
10776         }
10777
10778     case dw_val_class_data8:
10779       return DW_FORM_data8;
10780
10781     default:
10782       gcc_unreachable ();
10783     }
10784 }
10785
10786 /* Output the encoding of an attribute value.  */
10787
10788 static void
10789 output_value_format (dw_attr_ref a)
10790 {
10791   enum dwarf_form form = value_format (a);
10792
10793   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10794 }
10795
10796 /* Output the .debug_abbrev section which defines the DIE abbreviation
10797    table.  */
10798
10799 static void
10800 output_abbrev_section (void)
10801 {
10802   unsigned long abbrev_id;
10803
10804   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10805     {
10806       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10807       unsigned ix;
10808       dw_attr_ref a_attr;
10809
10810       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10811       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10812                                    dwarf_tag_name (abbrev->die_tag));
10813
10814       if (abbrev->die_child != NULL)
10815         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10816       else
10817         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10818
10819       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10820            ix++)
10821         {
10822           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10823                                        dwarf_attr_name (a_attr->dw_attr));
10824           output_value_format (a_attr);
10825         }
10826
10827       dw2_asm_output_data (1, 0, NULL);
10828       dw2_asm_output_data (1, 0, NULL);
10829     }
10830
10831   /* Terminate the table.  */
10832   dw2_asm_output_data (1, 0, NULL);
10833 }
10834
10835 /* Output a symbol we can use to refer to this DIE from another CU.  */
10836
10837 static inline void
10838 output_die_symbol (dw_die_ref die)
10839 {
10840   char *sym = die->die_id.die_symbol;
10841
10842   if (sym == 0)
10843     return;
10844
10845   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10846     /* We make these global, not weak; if the target doesn't support
10847        .linkonce, it doesn't support combining the sections, so debugging
10848        will break.  */
10849     targetm.asm_out.globalize_label (asm_out_file, sym);
10850
10851   ASM_OUTPUT_LABEL (asm_out_file, sym);
10852 }
10853
10854 /* Return a new location list, given the begin and end range, and the
10855    expression.  */
10856
10857 static inline dw_loc_list_ref
10858 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10859               const char *section)
10860 {
10861   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10862
10863   retlist->begin = begin;
10864   retlist->end = end;
10865   retlist->expr = expr;
10866   retlist->section = section;
10867
10868   return retlist;
10869 }
10870
10871 /* Generate a new internal symbol for this location list node, if it
10872    hasn't got one yet.  */
10873
10874 static inline void
10875 gen_llsym (dw_loc_list_ref list)
10876 {
10877   gcc_assert (!list->ll_symbol);
10878   list->ll_symbol = gen_internal_sym ("LLST");
10879 }
10880
10881 /* Output the location list given to us.  */
10882
10883 static void
10884 output_loc_list (dw_loc_list_ref list_head)
10885 {
10886   dw_loc_list_ref curr = list_head;
10887
10888   if (list_head->emitted)
10889     return;
10890   list_head->emitted = true;
10891
10892   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10893
10894   /* Walk the location list, and output each range + expression.  */
10895   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10896     {
10897       unsigned long size;
10898       /* Don't output an entry that starts and ends at the same address.  */
10899       if (strcmp (curr->begin, curr->end) == 0)
10900         continue;
10901       if (!have_multiple_function_sections)
10902         {
10903           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10904                                 "Location list begin address (%s)",
10905                                 list_head->ll_symbol);
10906           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10907                                 "Location list end address (%s)",
10908                                 list_head->ll_symbol);
10909         }
10910       else
10911         {
10912           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10913                                "Location list begin address (%s)",
10914                                list_head->ll_symbol);
10915           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10916                                "Location list end address (%s)",
10917                                list_head->ll_symbol);
10918         }
10919       size = size_of_locs (curr->expr);
10920
10921       /* Output the block length for this list of location operations.  */
10922       gcc_assert (size <= 0xffff);
10923       dw2_asm_output_data (2, size, "%s", "Location expression size");
10924
10925       output_loc_sequence (curr->expr);
10926     }
10927
10928   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10929                        "Location list terminator begin (%s)",
10930                        list_head->ll_symbol);
10931   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10932                        "Location list terminator end (%s)",
10933                        list_head->ll_symbol);
10934 }
10935
10936 /* Output a type signature.  */
10937
10938 static inline void
10939 output_signature (const char *sig, const char *name)
10940 {
10941   int i;
10942
10943   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10944     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10945 }
10946
10947 /* Output the DIE and its attributes.  Called recursively to generate
10948    the definitions of each child DIE.  */
10949
10950 static void
10951 output_die (dw_die_ref die)
10952 {
10953   dw_attr_ref a;
10954   dw_die_ref c;
10955   unsigned long size;
10956   unsigned ix;
10957
10958   /* If someone in another CU might refer to us, set up a symbol for
10959      them to point to.  */
10960   if (dwarf_version < 4 && die->die_id.die_symbol)
10961     output_die_symbol (die);
10962
10963   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10964                                (unsigned long)die->die_offset,
10965                                dwarf_tag_name (die->die_tag));
10966
10967   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10968     {
10969       const char *name = dwarf_attr_name (a->dw_attr);
10970
10971       switch (AT_class (a))
10972         {
10973         case dw_val_class_addr:
10974           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10975           break;
10976
10977         case dw_val_class_offset:
10978           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10979                                "%s", name);
10980           break;
10981
10982         case dw_val_class_range_list:
10983           {
10984             char *p = strchr (ranges_section_label, '\0');
10985
10986             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10987                      a->dw_attr_val.v.val_offset);
10988             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10989                                    debug_ranges_section, "%s", name);
10990             *p = '\0';
10991           }
10992           break;
10993
10994         case dw_val_class_loc:
10995           size = size_of_locs (AT_loc (a));
10996
10997           /* Output the block length for this list of location operations.  */
10998           if (dwarf_version >= 4)
10999             dw2_asm_output_data_uleb128 (size, "%s", name);
11000           else
11001             dw2_asm_output_data (constant_size (size), size, "%s", name);
11002
11003           output_loc_sequence (AT_loc (a));
11004           break;
11005
11006         case dw_val_class_const:
11007           /* ??? It would be slightly more efficient to use a scheme like is
11008              used for unsigned constants below, but gdb 4.x does not sign
11009              extend.  Gdb 5.x does sign extend.  */
11010           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11011           break;
11012
11013         case dw_val_class_unsigned_const:
11014           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11015                                AT_unsigned (a), "%s", name);
11016           break;
11017
11018         case dw_val_class_const_double:
11019           {
11020             unsigned HOST_WIDE_INT first, second;
11021
11022             if (HOST_BITS_PER_WIDE_INT >= 64)
11023               dw2_asm_output_data (1,
11024                                    2 * HOST_BITS_PER_WIDE_INT
11025                                    / HOST_BITS_PER_CHAR,
11026                                    NULL);
11027
11028             if (WORDS_BIG_ENDIAN)
11029               {
11030                 first = a->dw_attr_val.v.val_double.high;
11031                 second = a->dw_attr_val.v.val_double.low;
11032               }
11033             else
11034               {
11035                 first = a->dw_attr_val.v.val_double.low;
11036                 second = a->dw_attr_val.v.val_double.high;
11037               }
11038
11039             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11040                                  first, name);
11041             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11042                                  second, NULL);
11043           }
11044           break;
11045
11046         case dw_val_class_vec:
11047           {
11048             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11049             unsigned int len = a->dw_attr_val.v.val_vec.length;
11050             unsigned int i;
11051             unsigned char *p;
11052
11053             dw2_asm_output_data (constant_size (len * elt_size),
11054                                  len * elt_size, "%s", name);
11055             if (elt_size > sizeof (HOST_WIDE_INT))
11056               {
11057                 elt_size /= 2;
11058                 len *= 2;
11059               }
11060             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11061                  i < len;
11062                  i++, p += elt_size)
11063               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11064                                    "fp or vector constant word %u", i);
11065             break;
11066           }
11067
11068         case dw_val_class_flag:
11069           if (dwarf_version >= 4)
11070             {
11071               /* Currently all add_AT_flag calls pass in 1 as last argument,
11072                  so DW_FORM_flag_present can be used.  If that ever changes,
11073                  we'll need to use DW_FORM_flag and have some optimization
11074                  in build_abbrev_table that will change those to
11075                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11076                  the same abbrev entry.  */
11077               gcc_assert (AT_flag (a) == 1);
11078               if (flag_debug_asm)
11079                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11080                          ASM_COMMENT_START, name);
11081               break;
11082             }
11083           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11084           break;
11085
11086         case dw_val_class_loc_list:
11087           {
11088             char *sym = AT_loc_list (a)->ll_symbol;
11089
11090             gcc_assert (sym);
11091             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11092                                    "%s", name);
11093           }
11094           break;
11095
11096         case dw_val_class_die_ref:
11097           if (AT_ref_external (a))
11098             {
11099               if (dwarf_version >= 4)
11100                 {
11101                   comdat_type_node_ref type_node =
11102                     AT_ref (a)->die_id.die_type_node;
11103
11104                   gcc_assert (type_node);
11105                   output_signature (type_node->signature, name);
11106                 }
11107               else
11108                 {
11109                   char *sym = AT_ref (a)->die_id.die_symbol;
11110                   int size;
11111
11112                   gcc_assert (sym);
11113                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11114                      length, whereas in DWARF3 it's always sized as an
11115                      offset.  */
11116                   if (dwarf_version == 2)
11117                     size = DWARF2_ADDR_SIZE;
11118                   else
11119                     size = DWARF_OFFSET_SIZE;
11120                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11121                                          name);
11122                 }
11123             }
11124           else
11125             {
11126               gcc_assert (AT_ref (a)->die_offset);
11127               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11128                                    "%s", name);
11129             }
11130           break;
11131
11132         case dw_val_class_fde_ref:
11133           {
11134             char l1[20];
11135
11136             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11137                                          a->dw_attr_val.v.val_fde_index * 2);
11138             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11139                                    "%s", name);
11140           }
11141           break;
11142
11143         case dw_val_class_vms_delta:
11144           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11145                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11146                                     "%s", name);
11147           break;
11148
11149         case dw_val_class_lbl_id:
11150           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11151           break;
11152
11153         case dw_val_class_lineptr:
11154           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11155                                  debug_line_section, "%s", name);
11156           break;
11157
11158         case dw_val_class_macptr:
11159           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11160                                  debug_macinfo_section, "%s", name);
11161           break;
11162
11163         case dw_val_class_str:
11164           if (AT_string_form (a) == DW_FORM_strp)
11165             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11166                                    a->dw_attr_val.v.val_str->label,
11167                                    debug_str_section,
11168                                    "%s: \"%s\"", name, AT_string (a));
11169           else
11170             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11171           break;
11172
11173         case dw_val_class_file:
11174           {
11175             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11176
11177             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11178                                  a->dw_attr_val.v.val_file->filename);
11179             break;
11180           }
11181
11182         case dw_val_class_data8:
11183           {
11184             int i;
11185
11186             for (i = 0; i < 8; i++)
11187               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11188                                    i == 0 ? "%s" : NULL, name);
11189             break;
11190           }
11191
11192         default:
11193           gcc_unreachable ();
11194         }
11195     }
11196
11197   FOR_EACH_CHILD (die, c, output_die (c));
11198
11199   /* Add null byte to terminate sibling list.  */
11200   if (die->die_child != NULL)
11201     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11202                          (unsigned long) die->die_offset);
11203 }
11204
11205 /* Output the compilation unit that appears at the beginning of the
11206    .debug_info section, and precedes the DIE descriptions.  */
11207
11208 static void
11209 output_compilation_unit_header (void)
11210 {
11211   int ver = dwarf_version;
11212
11213   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11214     dw2_asm_output_data (4, 0xffffffff,
11215       "Initial length escape value indicating 64-bit DWARF extension");
11216   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11217                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11218                        "Length of Compilation Unit Info");
11219   dw2_asm_output_data (2, ver, "DWARF version number");
11220   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11221                          debug_abbrev_section,
11222                          "Offset Into Abbrev. Section");
11223   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11224 }
11225
11226 /* Output the compilation unit DIE and its children.  */
11227
11228 static void
11229 output_comp_unit (dw_die_ref die, int output_if_empty)
11230 {
11231   const char *secname;
11232   char *oldsym, *tmp;
11233
11234   /* Unless we are outputting main CU, we may throw away empty ones.  */
11235   if (!output_if_empty && die->die_child == NULL)
11236     return;
11237
11238   /* Even if there are no children of this DIE, we must output the information
11239      about the compilation unit.  Otherwise, on an empty translation unit, we
11240      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11241      will then complain when examining the file.  First mark all the DIEs in
11242      this CU so we know which get local refs.  */
11243   mark_dies (die);
11244
11245   build_abbrev_table (die);
11246
11247   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11248   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11249   calc_die_sizes (die);
11250
11251   oldsym = die->die_id.die_symbol;
11252   if (oldsym)
11253     {
11254       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11255
11256       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11257       secname = tmp;
11258       die->die_id.die_symbol = NULL;
11259       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11260     }
11261   else
11262     switch_to_section (debug_info_section);
11263
11264   /* Output debugging information.  */
11265   output_compilation_unit_header ();
11266   output_die (die);
11267
11268   /* Leave the marks on the main CU, so we can check them in
11269      output_pubnames.  */
11270   if (oldsym)
11271     {
11272       unmark_dies (die);
11273       die->die_id.die_symbol = oldsym;
11274     }
11275 }
11276
11277 /* Output a comdat type unit DIE and its children.  */
11278
11279 static void
11280 output_comdat_type_unit (comdat_type_node *node)
11281 {
11282   const char *secname;
11283   char *tmp;
11284   int i;
11285 #if defined (OBJECT_FORMAT_ELF)
11286   tree comdat_key;
11287 #endif
11288
11289   /* First mark all the DIEs in this CU so we know which get local refs.  */
11290   mark_dies (node->root_die);
11291
11292   build_abbrev_table (node->root_die);
11293
11294   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11295   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11296   calc_die_sizes (node->root_die);
11297
11298 #if defined (OBJECT_FORMAT_ELF)
11299   secname = ".debug_types";
11300   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11301   sprintf (tmp, "wt.");
11302   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11303     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11304   comdat_key = get_identifier (tmp);
11305   targetm.asm_out.named_section (secname,
11306                                  SECTION_DEBUG | SECTION_LINKONCE,
11307                                  comdat_key);
11308 #else
11309   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11310   sprintf (tmp, ".gnu.linkonce.wt.");
11311   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11312     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11313   secname = tmp;
11314   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11315 #endif
11316
11317   /* Output debugging information.  */
11318   output_compilation_unit_header ();
11319   output_signature (node->signature, "Type Signature");
11320   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11321                        "Offset to Type DIE");
11322   output_die (node->root_die);
11323
11324   unmark_dies (node->root_die);
11325 }
11326
11327 /* Return the DWARF2/3 pubname associated with a decl.  */
11328
11329 static const char *
11330 dwarf2_name (tree decl, int scope)
11331 {
11332   if (DECL_NAMELESS (decl))
11333     return NULL;
11334   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11335 }
11336
11337 /* Add a new entry to .debug_pubnames if appropriate.  */
11338
11339 static void
11340 add_pubname_string (const char *str, dw_die_ref die)
11341 {
11342   if (targetm.want_debug_pub_sections)
11343     {
11344       pubname_entry e;
11345
11346       e.die = die;
11347       e.name = xstrdup (str);
11348       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11349     }
11350 }
11351
11352 static void
11353 add_pubname (tree decl, dw_die_ref die)
11354 {
11355   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11356     {
11357       const char *name = dwarf2_name (decl, 1);
11358       if (name)
11359         add_pubname_string (name, die);
11360     }
11361 }
11362
11363 /* Add a new entry to .debug_pubtypes if appropriate.  */
11364
11365 static void
11366 add_pubtype (tree decl, dw_die_ref die)
11367 {
11368   pubname_entry e;
11369
11370   if (!targetm.want_debug_pub_sections)
11371     return;
11372
11373   e.name = NULL;
11374   if ((TREE_PUBLIC (decl)
11375        || is_cu_die (die->die_parent))
11376       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11377     {
11378       e.die = die;
11379       if (TYPE_P (decl))
11380         {
11381           if (TYPE_NAME (decl))
11382             {
11383               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11384                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11385               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11386                        && DECL_NAME (TYPE_NAME (decl)))
11387                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11388               else
11389                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11390             }
11391         }
11392       else
11393         {
11394           e.name = dwarf2_name (decl, 1);
11395           if (e.name)
11396             e.name = xstrdup (e.name);
11397         }
11398
11399       /* If we don't have a name for the type, there's no point in adding
11400          it to the table.  */
11401       if (e.name && e.name[0] != '\0')
11402         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11403     }
11404 }
11405
11406 /* Output the public names table used to speed up access to externally
11407    visible names; or the public types table used to find type definitions.  */
11408
11409 static void
11410 output_pubnames (VEC (pubname_entry, gc) * names)
11411 {
11412   unsigned i;
11413   unsigned long pubnames_length = size_of_pubnames (names);
11414   pubname_ref pub;
11415
11416   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11417     dw2_asm_output_data (4, 0xffffffff,
11418       "Initial length escape value indicating 64-bit DWARF extension");
11419   if (names == pubname_table)
11420     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11421                          "Length of Public Names Info");
11422   else
11423     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11424                          "Length of Public Type Names Info");
11425   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11426   dw2_asm_output_data (2, 2, "DWARF Version");
11427   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11428                          debug_info_section,
11429                          "Offset of Compilation Unit Info");
11430   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11431                        "Compilation Unit Length");
11432
11433   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11434     {
11435       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11436       if (names == pubname_table)
11437         gcc_assert (pub->die->die_mark);
11438
11439       if (names != pubtype_table
11440           || pub->die->die_offset != 0
11441           || !flag_eliminate_unused_debug_types)
11442         {
11443           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11444                                "DIE offset");
11445
11446           dw2_asm_output_nstring (pub->name, -1, "external name");
11447         }
11448     }
11449
11450   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11451 }
11452
11453 /* Add a new entry to .debug_aranges if appropriate.  */
11454
11455 static void
11456 add_arange (tree decl, dw_die_ref die)
11457 {
11458   if (! DECL_SECTION_NAME (decl))
11459     return;
11460
11461   if (arange_table_in_use == arange_table_allocated)
11462     {
11463       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11464       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11465                                     arange_table_allocated);
11466       memset (arange_table + arange_table_in_use, 0,
11467               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11468     }
11469
11470   arange_table[arange_table_in_use++] = die;
11471 }
11472
11473 /* Output the information that goes into the .debug_aranges table.
11474    Namely, define the beginning and ending address range of the
11475    text section generated for this compilation unit.  */
11476
11477 static void
11478 output_aranges (void)
11479 {
11480   unsigned i;
11481   unsigned long aranges_length = size_of_aranges ();
11482
11483   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11484     dw2_asm_output_data (4, 0xffffffff,
11485       "Initial length escape value indicating 64-bit DWARF extension");
11486   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11487                        "Length of Address Ranges Info");
11488   /* Version number for aranges is still 2, even in DWARF3.  */
11489   dw2_asm_output_data (2, 2, "DWARF Version");
11490   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11491                          debug_info_section,
11492                          "Offset of Compilation Unit Info");
11493   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11494   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11495
11496   /* We need to align to twice the pointer size here.  */
11497   if (DWARF_ARANGES_PAD_SIZE)
11498     {
11499       /* Pad using a 2 byte words so that padding is correct for any
11500          pointer size.  */
11501       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11502                            2 * DWARF2_ADDR_SIZE);
11503       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11504         dw2_asm_output_data (2, 0, NULL);
11505     }
11506
11507   /* It is necessary not to output these entries if the sections were
11508      not used; if the sections were not used, the length will be 0 and
11509      the address may end up as 0 if the section is discarded by ld
11510      --gc-sections, leaving an invalid (0, 0) entry that can be
11511      confused with the terminator.  */
11512   if (text_section_used)
11513     {
11514       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11515       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11516                             text_section_label, "Length");
11517     }
11518   if (cold_text_section_used)
11519     {
11520       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11521                            "Address");
11522       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11523                             cold_text_section_label, "Length");
11524     }
11525
11526   for (i = 0; i < arange_table_in_use; i++)
11527     {
11528       dw_die_ref die = arange_table[i];
11529
11530       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11531       gcc_assert (die->die_mark);
11532
11533       if (die->die_tag == DW_TAG_subprogram)
11534         {
11535           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11536                                "Address");
11537           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11538                                 get_AT_low_pc (die), "Length");
11539         }
11540       else
11541         {
11542           /* A static variable; extract the symbol from DW_AT_location.
11543              Note that this code isn't currently hit, as we only emit
11544              aranges for functions (jason 9/23/99).  */
11545           dw_attr_ref a = get_AT (die, DW_AT_location);
11546           dw_loc_descr_ref loc;
11547
11548           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11549
11550           loc = AT_loc (a);
11551           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11552
11553           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11554                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11555           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11556                                get_AT_unsigned (die, DW_AT_byte_size),
11557                                "Length");
11558         }
11559     }
11560
11561   /* Output the terminator words.  */
11562   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11563   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11564 }
11565
11566 /* Add a new entry to .debug_ranges.  Return the offset at which it
11567    was placed.  */
11568
11569 static unsigned int
11570 add_ranges_num (int num)
11571 {
11572   unsigned int in_use = ranges_table_in_use;
11573
11574   if (in_use == ranges_table_allocated)
11575     {
11576       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11577       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11578                                     ranges_table_allocated);
11579       memset (ranges_table + ranges_table_in_use, 0,
11580               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11581     }
11582
11583   ranges_table[in_use].num = num;
11584   ranges_table_in_use = in_use + 1;
11585
11586   return in_use * 2 * DWARF2_ADDR_SIZE;
11587 }
11588
11589 /* Add a new entry to .debug_ranges corresponding to a block, or a
11590    range terminator if BLOCK is NULL.  */
11591
11592 static unsigned int
11593 add_ranges (const_tree block)
11594 {
11595   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11596 }
11597
11598 /* Add a new entry to .debug_ranges corresponding to a pair of
11599    labels.  */
11600
11601 static void
11602 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11603                       bool *added)
11604 {
11605   unsigned int in_use = ranges_by_label_in_use;
11606   unsigned int offset;
11607
11608   if (in_use == ranges_by_label_allocated)
11609     {
11610       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11611       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11612                                        ranges_by_label,
11613                                        ranges_by_label_allocated);
11614       memset (ranges_by_label + ranges_by_label_in_use, 0,
11615               RANGES_TABLE_INCREMENT
11616               * sizeof (struct dw_ranges_by_label_struct));
11617     }
11618
11619   ranges_by_label[in_use].begin = begin;
11620   ranges_by_label[in_use].end = end;
11621   ranges_by_label_in_use = in_use + 1;
11622
11623   offset = add_ranges_num (-(int)in_use - 1);
11624   if (!*added)
11625     {
11626       add_AT_range_list (die, DW_AT_ranges, offset);
11627       *added = true;
11628     }
11629 }
11630
11631 static void
11632 output_ranges (void)
11633 {
11634   unsigned i;
11635   static const char *const start_fmt = "Offset %#x";
11636   const char *fmt = start_fmt;
11637
11638   for (i = 0; i < ranges_table_in_use; i++)
11639     {
11640       int block_num = ranges_table[i].num;
11641
11642       if (block_num > 0)
11643         {
11644           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11645           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11646
11647           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11648           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11649
11650           /* If all code is in the text section, then the compilation
11651              unit base address defaults to DW_AT_low_pc, which is the
11652              base of the text section.  */
11653           if (!have_multiple_function_sections)
11654             {
11655               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11656                                     text_section_label,
11657                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11658               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11659                                     text_section_label, NULL);
11660             }
11661
11662           /* Otherwise, the compilation unit base address is zero,
11663              which allows us to use absolute addresses, and not worry
11664              about whether the target supports cross-section
11665              arithmetic.  */
11666           else
11667             {
11668               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11669                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11670               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11671             }
11672
11673           fmt = NULL;
11674         }
11675
11676       /* Negative block_num stands for an index into ranges_by_label.  */
11677       else if (block_num < 0)
11678         {
11679           int lab_idx = - block_num - 1;
11680
11681           if (!have_multiple_function_sections)
11682             {
11683               gcc_unreachable ();
11684 #if 0
11685               /* If we ever use add_ranges_by_labels () for a single
11686                  function section, all we have to do is to take out
11687                  the #if 0 above.  */
11688               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11689                                     ranges_by_label[lab_idx].begin,
11690                                     text_section_label,
11691                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11692               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11693                                     ranges_by_label[lab_idx].end,
11694                                     text_section_label, NULL);
11695 #endif
11696             }
11697           else
11698             {
11699               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11700                                    ranges_by_label[lab_idx].begin,
11701                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11702               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11703                                    ranges_by_label[lab_idx].end,
11704                                    NULL);
11705             }
11706         }
11707       else
11708         {
11709           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11710           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11711           fmt = start_fmt;
11712         }
11713     }
11714 }
11715
11716 /* Data structure containing information about input files.  */
11717 struct file_info
11718 {
11719   const char *path;     /* Complete file name.  */
11720   const char *fname;    /* File name part.  */
11721   int length;           /* Length of entire string.  */
11722   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11723   int dir_idx;          /* Index in directory table.  */
11724 };
11725
11726 /* Data structure containing information about directories with source
11727    files.  */
11728 struct dir_info
11729 {
11730   const char *path;     /* Path including directory name.  */
11731   int length;           /* Path length.  */
11732   int prefix;           /* Index of directory entry which is a prefix.  */
11733   int count;            /* Number of files in this directory.  */
11734   int dir_idx;          /* Index of directory used as base.  */
11735 };
11736
11737 /* Callback function for file_info comparison.  We sort by looking at
11738    the directories in the path.  */
11739
11740 static int
11741 file_info_cmp (const void *p1, const void *p2)
11742 {
11743   const struct file_info *const s1 = (const struct file_info *) p1;
11744   const struct file_info *const s2 = (const struct file_info *) p2;
11745   const unsigned char *cp1;
11746   const unsigned char *cp2;
11747
11748   /* Take care of file names without directories.  We need to make sure that
11749      we return consistent values to qsort since some will get confused if
11750      we return the same value when identical operands are passed in opposite
11751      orders.  So if neither has a directory, return 0 and otherwise return
11752      1 or -1 depending on which one has the directory.  */
11753   if ((s1->path == s1->fname || s2->path == s2->fname))
11754     return (s2->path == s2->fname) - (s1->path == s1->fname);
11755
11756   cp1 = (const unsigned char *) s1->path;
11757   cp2 = (const unsigned char *) s2->path;
11758
11759   while (1)
11760     {
11761       ++cp1;
11762       ++cp2;
11763       /* Reached the end of the first path?  If so, handle like above.  */
11764       if ((cp1 == (const unsigned char *) s1->fname)
11765           || (cp2 == (const unsigned char *) s2->fname))
11766         return ((cp2 == (const unsigned char *) s2->fname)
11767                 - (cp1 == (const unsigned char *) s1->fname));
11768
11769       /* Character of current path component the same?  */
11770       else if (*cp1 != *cp2)
11771         return *cp1 - *cp2;
11772     }
11773 }
11774
11775 struct file_name_acquire_data
11776 {
11777   struct file_info *files;
11778   int used_files;
11779   int max_files;
11780 };
11781
11782 /* Traversal function for the hash table.  */
11783
11784 static int
11785 file_name_acquire (void ** slot, void *data)
11786 {
11787   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11788   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11789   struct file_info *fi;
11790   const char *f;
11791
11792   gcc_assert (fnad->max_files >= d->emitted_number);
11793
11794   if (! d->emitted_number)
11795     return 1;
11796
11797   gcc_assert (fnad->max_files != fnad->used_files);
11798
11799   fi = fnad->files + fnad->used_files++;
11800
11801   /* Skip all leading "./".  */
11802   f = d->filename;
11803   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11804     f += 2;
11805
11806   /* Create a new array entry.  */
11807   fi->path = f;
11808   fi->length = strlen (f);
11809   fi->file_idx = d;
11810
11811   /* Search for the file name part.  */
11812   f = strrchr (f, DIR_SEPARATOR);
11813 #if defined (DIR_SEPARATOR_2)
11814   {
11815     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11816
11817     if (g != NULL)
11818       {
11819         if (f == NULL || f < g)
11820           f = g;
11821       }
11822   }
11823 #endif
11824
11825   fi->fname = f == NULL ? fi->path : f + 1;
11826   return 1;
11827 }
11828
11829 /* Output the directory table and the file name table.  We try to minimize
11830    the total amount of memory needed.  A heuristic is used to avoid large
11831    slowdowns with many input files.  */
11832
11833 static void
11834 output_file_names (void)
11835 {
11836   struct file_name_acquire_data fnad;
11837   int numfiles;
11838   struct file_info *files;
11839   struct dir_info *dirs;
11840   int *saved;
11841   int *savehere;
11842   int *backmap;
11843   int ndirs;
11844   int idx_offset;
11845   int i;
11846
11847   if (!last_emitted_file)
11848     {
11849       dw2_asm_output_data (1, 0, "End directory table");
11850       dw2_asm_output_data (1, 0, "End file name table");
11851       return;
11852     }
11853
11854   numfiles = last_emitted_file->emitted_number;
11855
11856   /* Allocate the various arrays we need.  */
11857   files = XALLOCAVEC (struct file_info, numfiles);
11858   dirs = XALLOCAVEC (struct dir_info, numfiles);
11859
11860   fnad.files = files;
11861   fnad.used_files = 0;
11862   fnad.max_files = numfiles;
11863   htab_traverse (file_table, file_name_acquire, &fnad);
11864   gcc_assert (fnad.used_files == fnad.max_files);
11865
11866   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11867
11868   /* Find all the different directories used.  */
11869   dirs[0].path = files[0].path;
11870   dirs[0].length = files[0].fname - files[0].path;
11871   dirs[0].prefix = -1;
11872   dirs[0].count = 1;
11873   dirs[0].dir_idx = 0;
11874   files[0].dir_idx = 0;
11875   ndirs = 1;
11876
11877   for (i = 1; i < numfiles; i++)
11878     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11879         && memcmp (dirs[ndirs - 1].path, files[i].path,
11880                    dirs[ndirs - 1].length) == 0)
11881       {
11882         /* Same directory as last entry.  */
11883         files[i].dir_idx = ndirs - 1;
11884         ++dirs[ndirs - 1].count;
11885       }
11886     else
11887       {
11888         int j;
11889
11890         /* This is a new directory.  */
11891         dirs[ndirs].path = files[i].path;
11892         dirs[ndirs].length = files[i].fname - files[i].path;
11893         dirs[ndirs].count = 1;
11894         dirs[ndirs].dir_idx = ndirs;
11895         files[i].dir_idx = ndirs;
11896
11897         /* Search for a prefix.  */
11898         dirs[ndirs].prefix = -1;
11899         for (j = 0; j < ndirs; j++)
11900           if (dirs[j].length < dirs[ndirs].length
11901               && dirs[j].length > 1
11902               && (dirs[ndirs].prefix == -1
11903                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11904               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11905             dirs[ndirs].prefix = j;
11906
11907         ++ndirs;
11908       }
11909
11910   /* Now to the actual work.  We have to find a subset of the directories which
11911      allow expressing the file name using references to the directory table
11912      with the least amount of characters.  We do not do an exhaustive search
11913      where we would have to check out every combination of every single
11914      possible prefix.  Instead we use a heuristic which provides nearly optimal
11915      results in most cases and never is much off.  */
11916   saved = XALLOCAVEC (int, ndirs);
11917   savehere = XALLOCAVEC (int, ndirs);
11918
11919   memset (saved, '\0', ndirs * sizeof (saved[0]));
11920   for (i = 0; i < ndirs; i++)
11921     {
11922       int j;
11923       int total;
11924
11925       /* We can always save some space for the current directory.  But this
11926          does not mean it will be enough to justify adding the directory.  */
11927       savehere[i] = dirs[i].length;
11928       total = (savehere[i] - saved[i]) * dirs[i].count;
11929
11930       for (j = i + 1; j < ndirs; j++)
11931         {
11932           savehere[j] = 0;
11933           if (saved[j] < dirs[i].length)
11934             {
11935               /* Determine whether the dirs[i] path is a prefix of the
11936                  dirs[j] path.  */
11937               int k;
11938
11939               k = dirs[j].prefix;
11940               while (k != -1 && k != (int) i)
11941                 k = dirs[k].prefix;
11942
11943               if (k == (int) i)
11944                 {
11945                   /* Yes it is.  We can possibly save some memory by
11946                      writing the filenames in dirs[j] relative to
11947                      dirs[i].  */
11948                   savehere[j] = dirs[i].length;
11949                   total += (savehere[j] - saved[j]) * dirs[j].count;
11950                 }
11951             }
11952         }
11953
11954       /* Check whether we can save enough to justify adding the dirs[i]
11955          directory.  */
11956       if (total > dirs[i].length + 1)
11957         {
11958           /* It's worthwhile adding.  */
11959           for (j = i; j < ndirs; j++)
11960             if (savehere[j] > 0)
11961               {
11962                 /* Remember how much we saved for this directory so far.  */
11963                 saved[j] = savehere[j];
11964
11965                 /* Remember the prefix directory.  */
11966                 dirs[j].dir_idx = i;
11967               }
11968         }
11969     }
11970
11971   /* Emit the directory name table.  */
11972   idx_offset = dirs[0].length > 0 ? 1 : 0;
11973   for (i = 1 - idx_offset; i < ndirs; i++)
11974     dw2_asm_output_nstring (dirs[i].path,
11975                             dirs[i].length
11976                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11977                             "Directory Entry: %#x", i + idx_offset);
11978
11979   dw2_asm_output_data (1, 0, "End directory table");
11980
11981   /* We have to emit them in the order of emitted_number since that's
11982      used in the debug info generation.  To do this efficiently we
11983      generate a back-mapping of the indices first.  */
11984   backmap = XALLOCAVEC (int, numfiles);
11985   for (i = 0; i < numfiles; i++)
11986     backmap[files[i].file_idx->emitted_number - 1] = i;
11987
11988   /* Now write all the file names.  */
11989   for (i = 0; i < numfiles; i++)
11990     {
11991       int file_idx = backmap[i];
11992       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11993
11994 #ifdef VMS_DEBUGGING_INFO
11995 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11996
11997       /* Setting these fields can lead to debugger miscomparisons,
11998          but VMS Debug requires them to be set correctly.  */
11999
12000       int ver;
12001       long long cdt;
12002       long siz;
12003       int maxfilelen = strlen (files[file_idx].path)
12004                                + dirs[dir_idx].length
12005                                + MAX_VMS_VERSION_LEN + 1;
12006       char *filebuf = XALLOCAVEC (char, maxfilelen);
12007
12008       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12009       snprintf (filebuf, maxfilelen, "%s;%d",
12010                 files[file_idx].path + dirs[dir_idx].length, ver);
12011
12012       dw2_asm_output_nstring
12013         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12014
12015       /* Include directory index.  */
12016       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12017
12018       /* Modification time.  */
12019       dw2_asm_output_data_uleb128
12020         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12021           ? cdt : 0,
12022          NULL);
12023
12024       /* File length in bytes.  */
12025       dw2_asm_output_data_uleb128
12026         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12027           ? siz : 0,
12028          NULL);
12029 #else
12030       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12031                               "File Entry: %#x", (unsigned) i + 1);
12032
12033       /* Include directory index.  */
12034       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12035
12036       /* Modification time.  */
12037       dw2_asm_output_data_uleb128 (0, NULL);
12038
12039       /* File length in bytes.  */
12040       dw2_asm_output_data_uleb128 (0, NULL);
12041 #endif /* VMS_DEBUGGING_INFO */
12042     }
12043
12044   dw2_asm_output_data (1, 0, "End file name table");
12045 }
12046
12047
12048 /* Output the source line number correspondence information.  This
12049    information goes into the .debug_line section.  */
12050
12051 static void
12052 output_line_info (void)
12053 {
12054   char l1[20], l2[20], p1[20], p2[20];
12055   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12056   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12057   unsigned opc;
12058   unsigned n_op_args;
12059   unsigned long lt_index;
12060   unsigned long current_line;
12061   long line_offset;
12062   long line_delta;
12063   unsigned long current_file;
12064   unsigned long function;
12065   int ver = dwarf_version;
12066
12067   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12068   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12069   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12070   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12071
12072   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12073     dw2_asm_output_data (4, 0xffffffff,
12074       "Initial length escape value indicating 64-bit DWARF extension");
12075   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12076                         "Length of Source Line Info");
12077   ASM_OUTPUT_LABEL (asm_out_file, l1);
12078
12079   dw2_asm_output_data (2, ver, "DWARF Version");
12080   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12081   ASM_OUTPUT_LABEL (asm_out_file, p1);
12082
12083   /* Define the architecture-dependent minimum instruction length (in
12084    bytes).  In this implementation of DWARF, this field is used for
12085    information purposes only.  Since GCC generates assembly language,
12086    we have no a priori knowledge of how many instruction bytes are
12087    generated for each source line, and therefore can use only the
12088    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12089    commands.  Accordingly, we fix this as `1', which is "correct
12090    enough" for all architectures, and don't let the target override.  */
12091   dw2_asm_output_data (1, 1,
12092                        "Minimum Instruction Length");
12093
12094   if (ver >= 4)
12095     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12096                          "Maximum Operations Per Instruction");
12097   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12098                        "Default is_stmt_start flag");
12099   dw2_asm_output_data (1, DWARF_LINE_BASE,
12100                        "Line Base Value (Special Opcodes)");
12101   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12102                        "Line Range Value (Special Opcodes)");
12103   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12104                        "Special Opcode Base");
12105
12106   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12107     {
12108       switch (opc)
12109         {
12110         case DW_LNS_advance_pc:
12111         case DW_LNS_advance_line:
12112         case DW_LNS_set_file:
12113         case DW_LNS_set_column:
12114         case DW_LNS_fixed_advance_pc:
12115           n_op_args = 1;
12116           break;
12117         default:
12118           n_op_args = 0;
12119           break;
12120         }
12121
12122       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12123                            opc, n_op_args);
12124     }
12125
12126   /* Write out the information about the files we use.  */
12127   output_file_names ();
12128   ASM_OUTPUT_LABEL (asm_out_file, p2);
12129
12130   /* We used to set the address register to the first location in the text
12131      section here, but that didn't accomplish anything since we already
12132      have a line note for the opening brace of the first function.  */
12133
12134   /* Generate the line number to PC correspondence table, encoded as
12135      a series of state machine operations.  */
12136   current_file = 1;
12137   current_line = 1;
12138
12139   if (cfun && in_cold_section_p)
12140     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12141   else
12142     strcpy (prev_line_label, text_section_label);
12143   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12144     {
12145       dw_line_info_ref line_info = &line_info_table[lt_index];
12146
12147 #if 0
12148       /* Disable this optimization for now; GDB wants to see two line notes
12149          at the beginning of a function so it can find the end of the
12150          prologue.  */
12151
12152       /* Don't emit anything for redundant notes.  Just updating the
12153          address doesn't accomplish anything, because we already assume
12154          that anything after the last address is this line.  */
12155       if (line_info->dw_line_num == current_line
12156           && line_info->dw_file_num == current_file)
12157         continue;
12158 #endif
12159
12160       /* Emit debug info for the address of the current line.
12161
12162          Unfortunately, we have little choice here currently, and must always
12163          use the most general form.  GCC does not know the address delta
12164          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12165          attributes which will give an upper bound on the address range.  We
12166          could perhaps use length attributes to determine when it is safe to
12167          use DW_LNS_fixed_advance_pc.  */
12168
12169       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12170       if (0)
12171         {
12172           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12173           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12174                                "DW_LNS_fixed_advance_pc");
12175           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12176         }
12177       else
12178         {
12179           /* This can handle any delta.  This takes
12180              4+DWARF2_ADDR_SIZE bytes.  */
12181           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12182           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12183           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12184           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12185         }
12186
12187       strcpy (prev_line_label, line_label);
12188
12189       /* Emit debug info for the source file of the current line, if
12190          different from the previous line.  */
12191       if (line_info->dw_file_num != current_file)
12192         {
12193           current_file = line_info->dw_file_num;
12194           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12195           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12196         }
12197
12198       /* Emit debug info for the current line number, choosing the encoding
12199          that uses the least amount of space.  */
12200       if (line_info->dw_line_num != current_line)
12201         {
12202           line_offset = line_info->dw_line_num - current_line;
12203           line_delta = line_offset - DWARF_LINE_BASE;
12204           current_line = line_info->dw_line_num;
12205           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12206             /* This can handle deltas from -10 to 234, using the current
12207                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12208                takes 1 byte.  */
12209             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12210                                  "line %lu", current_line);
12211           else
12212             {
12213               /* This can handle any delta.  This takes at least 4 bytes,
12214                  depending on the value being encoded.  */
12215               dw2_asm_output_data (1, DW_LNS_advance_line,
12216                                    "advance to line %lu", current_line);
12217               dw2_asm_output_data_sleb128 (line_offset, NULL);
12218               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12219             }
12220         }
12221       else
12222         /* We still need to start a new row, so output a copy insn.  */
12223         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12224     }
12225
12226   /* Emit debug info for the address of the end of the function.  */
12227   if (0)
12228     {
12229       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12230                            "DW_LNS_fixed_advance_pc");
12231       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12232     }
12233   else
12234     {
12235       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12236       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12237       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12238       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12239     }
12240
12241   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12242   dw2_asm_output_data_uleb128 (1, NULL);
12243   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12244
12245   function = 0;
12246   current_file = 1;
12247   current_line = 1;
12248   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12249     {
12250       dw_separate_line_info_ref line_info
12251         = &separate_line_info_table[lt_index];
12252
12253 #if 0
12254       /* Don't emit anything for redundant notes.  */
12255       if (line_info->dw_line_num == current_line
12256           && line_info->dw_file_num == current_file
12257           && line_info->function == function)
12258         goto cont;
12259 #endif
12260
12261       /* Emit debug info for the address of the current line.  If this is
12262          a new function, or the first line of a function, then we need
12263          to handle it differently.  */
12264       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12265                                    lt_index);
12266       if (function != line_info->function)
12267         {
12268           function = line_info->function;
12269
12270           /* Set the address register to the first line in the function.  */
12271           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12272           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12273           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12274           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12275         }
12276       else
12277         {
12278           /* ??? See the DW_LNS_advance_pc comment above.  */
12279           if (0)
12280             {
12281               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12282                                    "DW_LNS_fixed_advance_pc");
12283               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12284             }
12285           else
12286             {
12287               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12288               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12289               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12290               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12291             }
12292         }
12293
12294       strcpy (prev_line_label, line_label);
12295
12296       /* Emit debug info for the source file of the current line, if
12297          different from the previous line.  */
12298       if (line_info->dw_file_num != current_file)
12299         {
12300           current_file = line_info->dw_file_num;
12301           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12302           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12303         }
12304
12305       /* Emit debug info for the current line number, choosing the encoding
12306          that uses the least amount of space.  */
12307       if (line_info->dw_line_num != current_line)
12308         {
12309           line_offset = line_info->dw_line_num - current_line;
12310           line_delta = line_offset - DWARF_LINE_BASE;
12311           current_line = line_info->dw_line_num;
12312           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12313             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12314                                  "line %lu", current_line);
12315           else
12316             {
12317               dw2_asm_output_data (1, DW_LNS_advance_line,
12318                                    "advance to line %lu", current_line);
12319               dw2_asm_output_data_sleb128 (line_offset, NULL);
12320               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12321             }
12322         }
12323       else
12324         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12325
12326 #if 0
12327     cont:
12328 #endif
12329
12330       lt_index++;
12331
12332       /* If we're done with a function, end its sequence.  */
12333       if (lt_index == separate_line_info_table_in_use
12334           || separate_line_info_table[lt_index].function != function)
12335         {
12336           current_file = 1;
12337           current_line = 1;
12338
12339           /* Emit debug info for the address of the end of the function.  */
12340           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12341           if (0)
12342             {
12343               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12344                                    "DW_LNS_fixed_advance_pc");
12345               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12346             }
12347           else
12348             {
12349               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12350               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12351               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12352               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12353             }
12354
12355           /* Output the marker for the end of this sequence.  */
12356           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12357           dw2_asm_output_data_uleb128 (1, NULL);
12358           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12359         }
12360     }
12361
12362   /* Output the marker for the end of the line number info.  */
12363   ASM_OUTPUT_LABEL (asm_out_file, l2);
12364 }
12365
12366 /* Return the size of the .debug_dcall table for the compilation unit.  */
12367
12368 static unsigned long
12369 size_of_dcall_table (void)
12370 {
12371   unsigned long size;
12372   unsigned int i;
12373   dcall_entry *p;
12374   tree last_poc_decl = NULL;
12375
12376   /* Header:  version + debug info section pointer + pointer size.  */
12377   size = 2 + DWARF_OFFSET_SIZE + 1;
12378
12379   /* Each entry:  code label + DIE offset.  */
12380   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12381     {
12382       gcc_assert (p->targ_die != NULL);
12383       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12384       if (p->poc_decl != last_poc_decl)
12385         {
12386           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12387           gcc_assert (poc_die);
12388           last_poc_decl = p->poc_decl;
12389           if (poc_die)
12390             size += (DWARF_OFFSET_SIZE
12391                      + size_of_uleb128 (poc_die->die_offset));
12392         }
12393       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12394     }
12395
12396   return size;
12397 }
12398
12399 /* Output the direct call table used to disambiguate PC values when
12400    identical function have been merged.  */
12401
12402 static void
12403 output_dcall_table (void)
12404 {
12405   unsigned i;
12406   unsigned long dcall_length = size_of_dcall_table ();
12407   dcall_entry *p;
12408   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12409   tree last_poc_decl = NULL;
12410
12411   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12412     dw2_asm_output_data (4, 0xffffffff,
12413       "Initial length escape value indicating 64-bit DWARF extension");
12414   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12415                        "Length of Direct Call Table");
12416   dw2_asm_output_data (2, 4, "Version number");
12417   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12418                          debug_info_section,
12419                          "Offset of Compilation Unit Info");
12420   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12421
12422   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12423     {
12424       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12425       if (p->poc_decl != last_poc_decl)
12426         {
12427           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12428           last_poc_decl = p->poc_decl;
12429           if (poc_die)
12430             {
12431               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12432               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12433                                            "Caller DIE offset");
12434             }
12435         }
12436       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12437       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12438       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12439                                    "Callee DIE offset");
12440     }
12441 }
12442 \f
12443 /* Return the size of the .debug_vcall table for the compilation unit.  */
12444
12445 static unsigned long
12446 size_of_vcall_table (void)
12447 {
12448   unsigned long size;
12449   unsigned int i;
12450   vcall_entry *p;
12451
12452   /* Header:  version + pointer size.  */
12453   size = 2 + 1;
12454
12455   /* Each entry:  code label + vtable slot index.  */
12456   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12457     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12458
12459   return size;
12460 }
12461
12462 /* Output the virtual call table used to disambiguate PC values when
12463    identical function have been merged.  */
12464
12465 static void
12466 output_vcall_table (void)
12467 {
12468   unsigned i;
12469   unsigned long vcall_length = size_of_vcall_table ();
12470   vcall_entry *p;
12471   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12472
12473   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12474     dw2_asm_output_data (4, 0xffffffff,
12475       "Initial length escape value indicating 64-bit DWARF extension");
12476   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12477                        "Length of Virtual Call Table");
12478   dw2_asm_output_data (2, 4, "Version number");
12479   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12480
12481   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12482     {
12483       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12484       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12485       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12486     }
12487 }
12488 \f
12489 /* Given a pointer to a tree node for some base type, return a pointer to
12490    a DIE that describes the given type.
12491
12492    This routine must only be called for GCC type nodes that correspond to
12493    Dwarf base (fundamental) types.  */
12494
12495 static dw_die_ref
12496 base_type_die (tree type)
12497 {
12498   dw_die_ref base_type_result;
12499   enum dwarf_type encoding;
12500
12501   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12502     return 0;
12503
12504   /* If this is a subtype that should not be emitted as a subrange type,
12505      use the base type.  See subrange_type_for_debug_p.  */
12506   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12507     type = TREE_TYPE (type);
12508
12509   switch (TREE_CODE (type))
12510     {
12511     case INTEGER_TYPE:
12512       if ((dwarf_version >= 4 || !dwarf_strict)
12513           && TYPE_NAME (type)
12514           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12515           && DECL_IS_BUILTIN (TYPE_NAME (type))
12516           && DECL_NAME (TYPE_NAME (type)))
12517         {
12518           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12519           if (strcmp (name, "char16_t") == 0
12520               || strcmp (name, "char32_t") == 0)
12521             {
12522               encoding = DW_ATE_UTF;
12523               break;
12524             }
12525         }
12526       if (TYPE_STRING_FLAG (type))
12527         {
12528           if (TYPE_UNSIGNED (type))
12529             encoding = DW_ATE_unsigned_char;
12530           else
12531             encoding = DW_ATE_signed_char;
12532         }
12533       else if (TYPE_UNSIGNED (type))
12534         encoding = DW_ATE_unsigned;
12535       else
12536         encoding = DW_ATE_signed;
12537       break;
12538
12539     case REAL_TYPE:
12540       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12541         {
12542           if (dwarf_version >= 3 || !dwarf_strict)
12543             encoding = DW_ATE_decimal_float;
12544           else
12545             encoding = DW_ATE_lo_user;
12546         }
12547       else
12548         encoding = DW_ATE_float;
12549       break;
12550
12551     case FIXED_POINT_TYPE:
12552       if (!(dwarf_version >= 3 || !dwarf_strict))
12553         encoding = DW_ATE_lo_user;
12554       else if (TYPE_UNSIGNED (type))
12555         encoding = DW_ATE_unsigned_fixed;
12556       else
12557         encoding = DW_ATE_signed_fixed;
12558       break;
12559
12560       /* Dwarf2 doesn't know anything about complex ints, so use
12561          a user defined type for it.  */
12562     case COMPLEX_TYPE:
12563       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12564         encoding = DW_ATE_complex_float;
12565       else
12566         encoding = DW_ATE_lo_user;
12567       break;
12568
12569     case BOOLEAN_TYPE:
12570       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12571       encoding = DW_ATE_boolean;
12572       break;
12573
12574     default:
12575       /* No other TREE_CODEs are Dwarf fundamental types.  */
12576       gcc_unreachable ();
12577     }
12578
12579   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12580
12581   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12582                    int_size_in_bytes (type));
12583   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12584
12585   return base_type_result;
12586 }
12587
12588 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12589    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12590
12591 static inline int
12592 is_base_type (tree type)
12593 {
12594   switch (TREE_CODE (type))
12595     {
12596     case ERROR_MARK:
12597     case VOID_TYPE:
12598     case INTEGER_TYPE:
12599     case REAL_TYPE:
12600     case FIXED_POINT_TYPE:
12601     case COMPLEX_TYPE:
12602     case BOOLEAN_TYPE:
12603       return 1;
12604
12605     case ARRAY_TYPE:
12606     case RECORD_TYPE:
12607     case UNION_TYPE:
12608     case QUAL_UNION_TYPE:
12609     case ENUMERAL_TYPE:
12610     case FUNCTION_TYPE:
12611     case METHOD_TYPE:
12612     case POINTER_TYPE:
12613     case REFERENCE_TYPE:
12614     case NULLPTR_TYPE:
12615     case OFFSET_TYPE:
12616     case LANG_TYPE:
12617     case VECTOR_TYPE:
12618       return 0;
12619
12620     default:
12621       gcc_unreachable ();
12622     }
12623
12624   return 0;
12625 }
12626
12627 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12628    node, return the size in bits for the type if it is a constant, or else
12629    return the alignment for the type if the type's size is not constant, or
12630    else return BITS_PER_WORD if the type actually turns out to be an
12631    ERROR_MARK node.  */
12632
12633 static inline unsigned HOST_WIDE_INT
12634 simple_type_size_in_bits (const_tree type)
12635 {
12636   if (TREE_CODE (type) == ERROR_MARK)
12637     return BITS_PER_WORD;
12638   else if (TYPE_SIZE (type) == NULL_TREE)
12639     return 0;
12640   else if (host_integerp (TYPE_SIZE (type), 1))
12641     return tree_low_cst (TYPE_SIZE (type), 1);
12642   else
12643     return TYPE_ALIGN (type);
12644 }
12645
12646 /* Similarly, but return a double_int instead of UHWI.  */
12647
12648 static inline double_int
12649 double_int_type_size_in_bits (const_tree type)
12650 {
12651   if (TREE_CODE (type) == ERROR_MARK)
12652     return uhwi_to_double_int (BITS_PER_WORD);
12653   else if (TYPE_SIZE (type) == NULL_TREE)
12654     return double_int_zero;
12655   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12656     return tree_to_double_int (TYPE_SIZE (type));
12657   else
12658     return uhwi_to_double_int (TYPE_ALIGN (type));
12659 }
12660
12661 /*  Given a pointer to a tree node for a subrange type, return a pointer
12662     to a DIE that describes the given type.  */
12663
12664 static dw_die_ref
12665 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12666 {
12667   dw_die_ref subrange_die;
12668   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12669
12670   if (context_die == NULL)
12671     context_die = comp_unit_die ();
12672
12673   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12674
12675   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12676     {
12677       /* The size of the subrange type and its base type do not match,
12678          so we need to generate a size attribute for the subrange type.  */
12679       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12680     }
12681
12682   if (low)
12683     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12684   if (high)
12685     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12686
12687   return subrange_die;
12688 }
12689
12690 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12691    entry that chains various modifiers in front of the given type.  */
12692
12693 static dw_die_ref
12694 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12695                    dw_die_ref context_die)
12696 {
12697   enum tree_code code = TREE_CODE (type);
12698   dw_die_ref mod_type_die;
12699   dw_die_ref sub_die = NULL;
12700   tree item_type = NULL;
12701   tree qualified_type;
12702   tree name, low, high;
12703
12704   if (code == ERROR_MARK)
12705     return NULL;
12706
12707   /* See if we already have the appropriately qualified variant of
12708      this type.  */
12709   qualified_type
12710     = get_qualified_type (type,
12711                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12712                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12713
12714   if (qualified_type == sizetype
12715       && TYPE_NAME (qualified_type)
12716       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12717     {
12718       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12719
12720       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12721                            && TYPE_PRECISION (t)
12722                            == TYPE_PRECISION (qualified_type)
12723                            && TYPE_UNSIGNED (t)
12724                            == TYPE_UNSIGNED (qualified_type));
12725       qualified_type = t;
12726     }
12727
12728   /* If we do, then we can just use its DIE, if it exists.  */
12729   if (qualified_type)
12730     {
12731       mod_type_die = lookup_type_die (qualified_type);
12732       if (mod_type_die)
12733         return mod_type_die;
12734     }
12735
12736   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12737
12738   /* Handle C typedef types.  */
12739   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12740       && !DECL_ARTIFICIAL (name))
12741     {
12742       tree dtype = TREE_TYPE (name);
12743
12744       if (qualified_type == dtype)
12745         {
12746           /* For a named type, use the typedef.  */
12747           gen_type_die (qualified_type, context_die);
12748           return lookup_type_die (qualified_type);
12749         }
12750       else if (is_const_type < TYPE_READONLY (dtype)
12751                || is_volatile_type < TYPE_VOLATILE (dtype)
12752                || (is_const_type <= TYPE_READONLY (dtype)
12753                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12754                    && DECL_ORIGINAL_TYPE (name) != type))
12755         /* cv-unqualified version of named type.  Just use the unnamed
12756            type to which it refers.  */
12757         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12758                                   is_const_type, is_volatile_type,
12759                                   context_die);
12760       /* Else cv-qualified version of named type; fall through.  */
12761     }
12762
12763   if (is_const_type)
12764     {
12765       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12766       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12767     }
12768   else if (is_volatile_type)
12769     {
12770       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12771       sub_die = modified_type_die (type, 0, 0, context_die);
12772     }
12773   else if (code == POINTER_TYPE)
12774     {
12775       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12776       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12777                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12778       item_type = TREE_TYPE (type);
12779       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12780         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12781                          TYPE_ADDR_SPACE (item_type));
12782     }
12783   else if (code == REFERENCE_TYPE)
12784     {
12785       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12786         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12787                                 type);
12788       else
12789         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12790       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12791                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12792       item_type = TREE_TYPE (type);
12793       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12794         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12795                          TYPE_ADDR_SPACE (item_type));
12796     }
12797   else if (code == INTEGER_TYPE
12798            && TREE_TYPE (type) != NULL_TREE
12799            && subrange_type_for_debug_p (type, &low, &high))
12800     {
12801       mod_type_die = subrange_type_die (type, low, high, context_die);
12802       item_type = TREE_TYPE (type);
12803     }
12804   else if (is_base_type (type))
12805     mod_type_die = base_type_die (type);
12806   else
12807     {
12808       gen_type_die (type, context_die);
12809
12810       /* We have to get the type_main_variant here (and pass that to the
12811          `lookup_type_die' routine) because the ..._TYPE node we have
12812          might simply be a *copy* of some original type node (where the
12813          copy was created to help us keep track of typedef names) and
12814          that copy might have a different TYPE_UID from the original
12815          ..._TYPE node.  */
12816       if (TREE_CODE (type) != VECTOR_TYPE)
12817         return lookup_type_die (type_main_variant (type));
12818       else
12819         /* Vectors have the debugging information in the type,
12820            not the main variant.  */
12821         return lookup_type_die (type);
12822     }
12823
12824   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12825      don't output a DW_TAG_typedef, since there isn't one in the
12826      user's program; just attach a DW_AT_name to the type.
12827      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12828      if the base type already has the same name.  */
12829   if (name
12830       && ((TREE_CODE (name) != TYPE_DECL
12831            && (qualified_type == TYPE_MAIN_VARIANT (type)
12832                || (!is_const_type && !is_volatile_type)))
12833           || (TREE_CODE (name) == TYPE_DECL
12834               && TREE_TYPE (name) == qualified_type
12835               && DECL_NAME (name))))
12836     {
12837       if (TREE_CODE (name) == TYPE_DECL)
12838         /* Could just call add_name_and_src_coords_attributes here,
12839            but since this is a builtin type it doesn't have any
12840            useful source coordinates anyway.  */
12841         name = DECL_NAME (name);
12842       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12843     }
12844   /* This probably indicates a bug.  */
12845   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12846     add_name_attribute (mod_type_die, "__unknown__");
12847
12848   if (qualified_type)
12849     equate_type_number_to_die (qualified_type, mod_type_die);
12850
12851   if (item_type)
12852     /* We must do this after the equate_type_number_to_die call, in case
12853        this is a recursive type.  This ensures that the modified_type_die
12854        recursion will terminate even if the type is recursive.  Recursive
12855        types are possible in Ada.  */
12856     sub_die = modified_type_die (item_type,
12857                                  TYPE_READONLY (item_type),
12858                                  TYPE_VOLATILE (item_type),
12859                                  context_die);
12860
12861   if (sub_die != NULL)
12862     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12863
12864   return mod_type_die;
12865 }
12866
12867 /* Generate DIEs for the generic parameters of T.
12868    T must be either a generic type or a generic function.
12869    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12870
12871 static void
12872 gen_generic_params_dies (tree t)
12873 {
12874   tree parms, args;
12875   int parms_num, i;
12876   dw_die_ref die = NULL;
12877
12878   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12879     return;
12880
12881   if (TYPE_P (t))
12882     die = lookup_type_die (t);
12883   else if (DECL_P (t))
12884     die = lookup_decl_die (t);
12885
12886   gcc_assert (die);
12887
12888   parms = lang_hooks.get_innermost_generic_parms (t);
12889   if (!parms)
12890     /* T has no generic parameter. It means T is neither a generic type
12891        or function. End of story.  */
12892     return;
12893
12894   parms_num = TREE_VEC_LENGTH (parms);
12895   args = lang_hooks.get_innermost_generic_args (t);
12896   for (i = 0; i < parms_num; i++)
12897     {
12898       tree parm, arg, arg_pack_elems;
12899
12900       parm = TREE_VEC_ELT (parms, i);
12901       arg = TREE_VEC_ELT (args, i);
12902       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12903       gcc_assert (parm && TREE_VALUE (parm) && arg);
12904
12905       if (parm && TREE_VALUE (parm) && arg)
12906         {
12907           /* If PARM represents a template parameter pack,
12908              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12909              by DW_TAG_template_*_parameter DIEs for the argument
12910              pack elements of ARG. Note that ARG would then be
12911              an argument pack.  */
12912           if (arg_pack_elems)
12913             template_parameter_pack_die (TREE_VALUE (parm),
12914                                          arg_pack_elems,
12915                                          die);
12916           else
12917             generic_parameter_die (TREE_VALUE (parm), arg,
12918                                    true /* Emit DW_AT_name */, die);
12919         }
12920     }
12921 }
12922
12923 /* Create and return a DIE for PARM which should be
12924    the representation of a generic type parameter.
12925    For instance, in the C++ front end, PARM would be a template parameter.
12926    ARG is the argument to PARM.
12927    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12928    name of the PARM.
12929    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12930    as a child node.  */
12931
12932 static dw_die_ref
12933 generic_parameter_die (tree parm, tree arg,
12934                        bool emit_name_p,
12935                        dw_die_ref parent_die)
12936 {
12937   dw_die_ref tmpl_die = NULL;
12938   const char *name = NULL;
12939
12940   if (!parm || !DECL_NAME (parm) || !arg)
12941     return NULL;
12942
12943   /* We support non-type generic parameters and arguments,
12944      type generic parameters and arguments, as well as
12945      generic generic parameters (a.k.a. template template parameters in C++)
12946      and arguments.  */
12947   if (TREE_CODE (parm) == PARM_DECL)
12948     /* PARM is a nontype generic parameter  */
12949     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12950   else if (TREE_CODE (parm) == TYPE_DECL)
12951     /* PARM is a type generic parameter.  */
12952     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12953   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12954     /* PARM is a generic generic parameter.
12955        Its DIE is a GNU extension. It shall have a
12956        DW_AT_name attribute to represent the name of the template template
12957        parameter, and a DW_AT_GNU_template_name attribute to represent the
12958        name of the template template argument.  */
12959     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12960                         parent_die, parm);
12961   else
12962     gcc_unreachable ();
12963
12964   if (tmpl_die)
12965     {
12966       tree tmpl_type;
12967
12968       /* If PARM is a generic parameter pack, it means we are
12969          emitting debug info for a template argument pack element.
12970          In other terms, ARG is a template argument pack element.
12971          In that case, we don't emit any DW_AT_name attribute for
12972          the die.  */
12973       if (emit_name_p)
12974         {
12975           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12976           gcc_assert (name);
12977           add_AT_string (tmpl_die, DW_AT_name, name);
12978         }
12979
12980       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12981         {
12982           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12983              TMPL_DIE should have a child DW_AT_type attribute that is set
12984              to the type of the argument to PARM, which is ARG.
12985              If PARM is a type generic parameter, TMPL_DIE should have a
12986              child DW_AT_type that is set to ARG.  */
12987           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12988           add_type_attribute (tmpl_die, tmpl_type, 0,
12989                               TREE_THIS_VOLATILE (tmpl_type),
12990                               parent_die);
12991         }
12992       else
12993         {
12994           /* So TMPL_DIE is a DIE representing a
12995              a generic generic template parameter, a.k.a template template
12996              parameter in C++ and arg is a template.  */
12997
12998           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12999              to the name of the argument.  */
13000           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13001           if (name)
13002             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13003         }
13004
13005       if (TREE_CODE (parm) == PARM_DECL)
13006         /* So PARM is a non-type generic parameter.
13007            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13008            attribute of TMPL_DIE which value represents the value
13009            of ARG.
13010            We must be careful here:
13011            The value of ARG might reference some function decls.
13012            We might currently be emitting debug info for a generic
13013            type and types are emitted before function decls, we don't
13014            know if the function decls referenced by ARG will actually be
13015            emitted after cgraph computations.
13016            So must defer the generation of the DW_AT_const_value to
13017            after cgraph is ready.  */
13018         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13019     }
13020
13021   return tmpl_die;
13022 }
13023
13024 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13025    PARM_PACK must be a template parameter pack. The returned DIE
13026    will be child DIE of PARENT_DIE.  */
13027
13028 static dw_die_ref
13029 template_parameter_pack_die (tree parm_pack,
13030                              tree parm_pack_args,
13031                              dw_die_ref parent_die)
13032 {
13033   dw_die_ref die;
13034   int j;
13035
13036   gcc_assert (parent_die && parm_pack);
13037
13038   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13039   add_name_and_src_coords_attributes (die, parm_pack);
13040   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13041     generic_parameter_die (parm_pack,
13042                            TREE_VEC_ELT (parm_pack_args, j),
13043                            false /* Don't emit DW_AT_name */,
13044                            die);
13045   return die;
13046 }
13047
13048 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13049    an enumerated type.  */
13050
13051 static inline int
13052 type_is_enum (const_tree type)
13053 {
13054   return TREE_CODE (type) == ENUMERAL_TYPE;
13055 }
13056
13057 /* Return the DBX register number described by a given RTL node.  */
13058
13059 static unsigned int
13060 dbx_reg_number (const_rtx rtl)
13061 {
13062   unsigned regno = REGNO (rtl);
13063
13064   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13065
13066 #ifdef LEAF_REG_REMAP
13067   if (current_function_uses_only_leaf_regs)
13068     {
13069       int leaf_reg = LEAF_REG_REMAP (regno);
13070       if (leaf_reg != -1)
13071         regno = (unsigned) leaf_reg;
13072     }
13073 #endif
13074
13075   return DBX_REGISTER_NUMBER (regno);
13076 }
13077
13078 /* Optionally add a DW_OP_piece term to a location description expression.
13079    DW_OP_piece is only added if the location description expression already
13080    doesn't end with DW_OP_piece.  */
13081
13082 static void
13083 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13084 {
13085   dw_loc_descr_ref loc;
13086
13087   if (*list_head != NULL)
13088     {
13089       /* Find the end of the chain.  */
13090       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13091         ;
13092
13093       if (loc->dw_loc_opc != DW_OP_piece)
13094         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13095     }
13096 }
13097
13098 /* Return a location descriptor that designates a machine register or
13099    zero if there is none.  */
13100
13101 static dw_loc_descr_ref
13102 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13103 {
13104   rtx regs;
13105
13106   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13107     return 0;
13108
13109   /* We only use "frame base" when we're sure we're talking about the
13110      post-prologue local stack frame.  We do this by *not* running
13111      register elimination until this point, and recognizing the special
13112      argument pointer and soft frame pointer rtx's.
13113      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13114   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13115       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13116     {
13117       dw_loc_descr_ref result = NULL;
13118
13119       if (dwarf_version >= 4 || !dwarf_strict)
13120         {
13121           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13122           if (result)
13123             add_loc_descr (&result,
13124                            new_loc_descr (DW_OP_stack_value, 0, 0));
13125         }
13126       return result;
13127     }
13128
13129   regs = targetm.dwarf_register_span (rtl);
13130
13131   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13132     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13133   else
13134     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13135 }
13136
13137 /* Return a location descriptor that designates a machine register for
13138    a given hard register number.  */
13139
13140 static dw_loc_descr_ref
13141 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13142 {
13143   dw_loc_descr_ref reg_loc_descr;
13144
13145   if (regno <= 31)
13146     reg_loc_descr
13147       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13148   else
13149     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13150
13151   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13152     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13153
13154   return reg_loc_descr;
13155 }
13156
13157 /* Given an RTL of a register, return a location descriptor that
13158    designates a value that spans more than one register.  */
13159
13160 static dw_loc_descr_ref
13161 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13162                              enum var_init_status initialized)
13163 {
13164   int nregs, size, i;
13165   unsigned reg;
13166   dw_loc_descr_ref loc_result = NULL;
13167
13168   reg = REGNO (rtl);
13169 #ifdef LEAF_REG_REMAP
13170   if (current_function_uses_only_leaf_regs)
13171     {
13172       int leaf_reg = LEAF_REG_REMAP (reg);
13173       if (leaf_reg != -1)
13174         reg = (unsigned) leaf_reg;
13175     }
13176 #endif
13177   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13178   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13179
13180   /* Simple, contiguous registers.  */
13181   if (regs == NULL_RTX)
13182     {
13183       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13184
13185       loc_result = NULL;
13186       while (nregs--)
13187         {
13188           dw_loc_descr_ref t;
13189
13190           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13191                                       VAR_INIT_STATUS_INITIALIZED);
13192           add_loc_descr (&loc_result, t);
13193           add_loc_descr_op_piece (&loc_result, size);
13194           ++reg;
13195         }
13196       return loc_result;
13197     }
13198
13199   /* Now onto stupid register sets in non contiguous locations.  */
13200
13201   gcc_assert (GET_CODE (regs) == PARALLEL);
13202
13203   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13204   loc_result = NULL;
13205
13206   for (i = 0; i < XVECLEN (regs, 0); ++i)
13207     {
13208       dw_loc_descr_ref t;
13209
13210       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13211                                   VAR_INIT_STATUS_INITIALIZED);
13212       add_loc_descr (&loc_result, t);
13213       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13214       add_loc_descr_op_piece (&loc_result, size);
13215     }
13216
13217   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13218     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13219   return loc_result;
13220 }
13221
13222 /* Return a location descriptor that designates a constant.  */
13223
13224 static dw_loc_descr_ref
13225 int_loc_descriptor (HOST_WIDE_INT i)
13226 {
13227   enum dwarf_location_atom op;
13228
13229   /* Pick the smallest representation of a constant, rather than just
13230      defaulting to the LEB encoding.  */
13231   if (i >= 0)
13232     {
13233       if (i <= 31)
13234         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13235       else if (i <= 0xff)
13236         op = DW_OP_const1u;
13237       else if (i <= 0xffff)
13238         op = DW_OP_const2u;
13239       else if (HOST_BITS_PER_WIDE_INT == 32
13240                || i <= 0xffffffff)
13241         op = DW_OP_const4u;
13242       else
13243         op = DW_OP_constu;
13244     }
13245   else
13246     {
13247       if (i >= -0x80)
13248         op = DW_OP_const1s;
13249       else if (i >= -0x8000)
13250         op = DW_OP_const2s;
13251       else if (HOST_BITS_PER_WIDE_INT == 32
13252                || i >= -0x80000000)
13253         op = DW_OP_const4s;
13254       else
13255         op = DW_OP_consts;
13256     }
13257
13258   return new_loc_descr (op, i, 0);
13259 }
13260
13261 /* Return loc description representing "address" of integer value.
13262    This can appear only as toplevel expression.  */
13263
13264 static dw_loc_descr_ref
13265 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13266 {
13267   int litsize;
13268   dw_loc_descr_ref loc_result = NULL;
13269
13270   if (!(dwarf_version >= 4 || !dwarf_strict))
13271     return NULL;
13272
13273   if (i >= 0)
13274     {
13275       if (i <= 31)
13276         litsize = 1;
13277       else if (i <= 0xff)
13278         litsize = 2;
13279       else if (i <= 0xffff)
13280         litsize = 3;
13281       else if (HOST_BITS_PER_WIDE_INT == 32
13282                || i <= 0xffffffff)
13283         litsize = 5;
13284       else
13285         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13286     }
13287   else
13288     {
13289       if (i >= -0x80)
13290         litsize = 2;
13291       else if (i >= -0x8000)
13292         litsize = 3;
13293       else if (HOST_BITS_PER_WIDE_INT == 32
13294                || i >= -0x80000000)
13295         litsize = 5;
13296       else
13297         litsize = 1 + size_of_sleb128 (i);
13298     }
13299   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13300      is more compact.  For DW_OP_stack_value we need:
13301      litsize + 1 (DW_OP_stack_value)
13302      and for DW_OP_implicit_value:
13303      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13304   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13305     {
13306       loc_result = int_loc_descriptor (i);
13307       add_loc_descr (&loc_result,
13308                      new_loc_descr (DW_OP_stack_value, 0, 0));
13309       return loc_result;
13310     }
13311
13312   loc_result = new_loc_descr (DW_OP_implicit_value,
13313                               size, 0);
13314   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13315   loc_result->dw_loc_oprnd2.v.val_int = i;
13316   return loc_result;
13317 }
13318
13319 /* Return a location descriptor that designates a base+offset location.  */
13320
13321 static dw_loc_descr_ref
13322 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13323                  enum var_init_status initialized)
13324 {
13325   unsigned int regno;
13326   dw_loc_descr_ref result;
13327   dw_fde_ref fde = current_fde ();
13328
13329   /* We only use "frame base" when we're sure we're talking about the
13330      post-prologue local stack frame.  We do this by *not* running
13331      register elimination until this point, and recognizing the special
13332      argument pointer and soft frame pointer rtx's.  */
13333   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13334     {
13335       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13336
13337       if (elim != reg)
13338         {
13339           if (GET_CODE (elim) == PLUS)
13340             {
13341               offset += INTVAL (XEXP (elim, 1));
13342               elim = XEXP (elim, 0);
13343             }
13344           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13345                        && (elim == hard_frame_pointer_rtx
13346                            || elim == stack_pointer_rtx))
13347                       || elim == (frame_pointer_needed
13348                                   ? hard_frame_pointer_rtx
13349                                   : stack_pointer_rtx));
13350
13351           /* If drap register is used to align stack, use frame
13352              pointer + offset to access stack variables.  If stack
13353              is aligned without drap, use stack pointer + offset to
13354              access stack variables.  */
13355           if (crtl->stack_realign_tried
13356               && reg == frame_pointer_rtx)
13357             {
13358               int base_reg
13359                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13360                                       ? HARD_FRAME_POINTER_REGNUM
13361                                       : STACK_POINTER_REGNUM);
13362               return new_reg_loc_descr (base_reg, offset);
13363             }
13364
13365           offset += frame_pointer_fb_offset;
13366           return new_loc_descr (DW_OP_fbreg, offset, 0);
13367         }
13368     }
13369   else if (!optimize
13370            && fde
13371            && (fde->drap_reg == REGNO (reg)
13372                || fde->vdrap_reg == REGNO (reg)))
13373     {
13374       /* Use cfa+offset to represent the location of arguments passed
13375          on the stack when drap is used to align stack.
13376          Only do this when not optimizing, for optimized code var-tracking
13377          is supposed to track where the arguments live and the register
13378          used as vdrap or drap in some spot might be used for something
13379          else in other part of the routine.  */
13380       return new_loc_descr (DW_OP_fbreg, offset, 0);
13381     }
13382
13383   regno = dbx_reg_number (reg);
13384   if (regno <= 31)
13385     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13386                             offset, 0);
13387   else
13388     result = new_loc_descr (DW_OP_bregx, regno, offset);
13389
13390   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13391     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13392
13393   return result;
13394 }
13395
13396 /* Return true if this RTL expression describes a base+offset calculation.  */
13397
13398 static inline int
13399 is_based_loc (const_rtx rtl)
13400 {
13401   return (GET_CODE (rtl) == PLUS
13402           && ((REG_P (XEXP (rtl, 0))
13403                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13404                && CONST_INT_P (XEXP (rtl, 1)))));
13405 }
13406
13407 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13408    failed.  */
13409
13410 static dw_loc_descr_ref
13411 tls_mem_loc_descriptor (rtx mem)
13412 {
13413   tree base;
13414   dw_loc_descr_ref loc_result;
13415
13416   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13417     return NULL;
13418
13419   base = get_base_address (MEM_EXPR (mem));
13420   if (base == NULL
13421       || TREE_CODE (base) != VAR_DECL
13422       || !DECL_THREAD_LOCAL_P (base))
13423     return NULL;
13424
13425   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13426   if (loc_result == NULL)
13427     return NULL;
13428
13429   if (INTVAL (MEM_OFFSET (mem)))
13430     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13431
13432   return loc_result;
13433 }
13434
13435 /* Output debug info about reason why we failed to expand expression as dwarf
13436    expression.  */
13437
13438 static void
13439 expansion_failed (tree expr, rtx rtl, char const *reason)
13440 {
13441   if (dump_file && (dump_flags & TDF_DETAILS))
13442     {
13443       fprintf (dump_file, "Failed to expand as dwarf: ");
13444       if (expr)
13445         print_generic_expr (dump_file, expr, dump_flags);
13446       if (rtl)
13447         {
13448           fprintf (dump_file, "\n");
13449           print_rtl (dump_file, rtl);
13450         }
13451       fprintf (dump_file, "\nReason: %s\n", reason);
13452     }
13453 }
13454
13455 /* Helper function for const_ok_for_output, called either directly
13456    or via for_each_rtx.  */
13457
13458 static int
13459 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13460 {
13461   rtx rtl = *rtlp;
13462
13463   if (GET_CODE (rtl) == UNSPEC)
13464     {
13465       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13466          we can't express it in the debug info.  */
13467 #ifdef ENABLE_CHECKING
13468       inform (current_function_decl
13469               ? DECL_SOURCE_LOCATION (current_function_decl)
13470               : UNKNOWN_LOCATION,
13471               "non-delegitimized UNSPEC %d found in variable location",
13472               XINT (rtl, 1));
13473 #endif
13474       expansion_failed (NULL_TREE, rtl,
13475                         "UNSPEC hasn't been delegitimized.\n");
13476       return 1;
13477     }
13478
13479   if (GET_CODE (rtl) != SYMBOL_REF)
13480     return 0;
13481
13482   if (CONSTANT_POOL_ADDRESS_P (rtl))
13483     {
13484       bool marked;
13485       get_pool_constant_mark (rtl, &marked);
13486       /* If all references to this pool constant were optimized away,
13487          it was not output and thus we can't represent it.  */
13488       if (!marked)
13489         {
13490           expansion_failed (NULL_TREE, rtl,
13491                             "Constant was removed from constant pool.\n");
13492           return 1;
13493         }
13494     }
13495
13496   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13497     return 1;
13498
13499   /* Avoid references to external symbols in debug info, on several targets
13500      the linker might even refuse to link when linking a shared library,
13501      and in many other cases the relocations for .debug_info/.debug_loc are
13502      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13503      to be defined within the same shared library or executable are fine.  */
13504   if (SYMBOL_REF_EXTERNAL_P (rtl))
13505     {
13506       tree decl = SYMBOL_REF_DECL (rtl);
13507
13508       if (decl == NULL || !targetm.binds_local_p (decl))
13509         {
13510           expansion_failed (NULL_TREE, rtl,
13511                             "Symbol not defined in current TU.\n");
13512           return 1;
13513         }
13514     }
13515
13516   return 0;
13517 }
13518
13519 /* Return true if constant RTL can be emitted in DW_OP_addr or
13520    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13521    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13522
13523 static bool
13524 const_ok_for_output (rtx rtl)
13525 {
13526   if (GET_CODE (rtl) == SYMBOL_REF)
13527     return const_ok_for_output_1 (&rtl, NULL) == 0;
13528
13529   if (GET_CODE (rtl) == CONST)
13530     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13531
13532   return true;
13533 }
13534
13535 /* The following routine converts the RTL for a variable or parameter
13536    (resident in memory) into an equivalent Dwarf representation of a
13537    mechanism for getting the address of that same variable onto the top of a
13538    hypothetical "address evaluation" stack.
13539
13540    When creating memory location descriptors, we are effectively transforming
13541    the RTL for a memory-resident object into its Dwarf postfix expression
13542    equivalent.  This routine recursively descends an RTL tree, turning
13543    it into Dwarf postfix code as it goes.
13544
13545    MODE is the mode of the memory reference, needed to handle some
13546    autoincrement addressing modes.
13547
13548    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13549    location list for RTL.
13550
13551    Return 0 if we can't represent the location.  */
13552
13553 static dw_loc_descr_ref
13554 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13555                     enum var_init_status initialized)
13556 {
13557   dw_loc_descr_ref mem_loc_result = NULL;
13558   enum dwarf_location_atom op;
13559   dw_loc_descr_ref op0, op1;
13560
13561   /* Note that for a dynamically sized array, the location we will generate a
13562      description of here will be the lowest numbered location which is
13563      actually within the array.  That's *not* necessarily the same as the
13564      zeroth element of the array.  */
13565
13566   rtl = targetm.delegitimize_address (rtl);
13567
13568   switch (GET_CODE (rtl))
13569     {
13570     case POST_INC:
13571     case POST_DEC:
13572     case POST_MODIFY:
13573       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13574
13575     case SUBREG:
13576       /* The case of a subreg may arise when we have a local (register)
13577          variable or a formal (register) parameter which doesn't quite fill
13578          up an entire register.  For now, just assume that it is
13579          legitimate to make the Dwarf info refer to the whole register which
13580          contains the given subreg.  */
13581       if (!subreg_lowpart_p (rtl))
13582         break;
13583       rtl = SUBREG_REG (rtl);
13584       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13585         break;
13586       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13587         break;
13588       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13589       break;
13590
13591     case REG:
13592       /* Whenever a register number forms a part of the description of the
13593          method for calculating the (dynamic) address of a memory resident
13594          object, DWARF rules require the register number be referred to as
13595          a "base register".  This distinction is not based in any way upon
13596          what category of register the hardware believes the given register
13597          belongs to.  This is strictly DWARF terminology we're dealing with
13598          here. Note that in cases where the location of a memory-resident
13599          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13600          OP_CONST (0)) the actual DWARF location descriptor that we generate
13601          may just be OP_BASEREG (basereg).  This may look deceptively like
13602          the object in question was allocated to a register (rather than in
13603          memory) so DWARF consumers need to be aware of the subtle
13604          distinction between OP_REG and OP_BASEREG.  */
13605       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13606         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13607       else if (stack_realign_drap
13608                && crtl->drap_reg
13609                && crtl->args.internal_arg_pointer == rtl
13610                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13611         {
13612           /* If RTL is internal_arg_pointer, which has been optimized
13613              out, use DRAP instead.  */
13614           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13615                                             VAR_INIT_STATUS_INITIALIZED);
13616         }
13617       break;
13618
13619     case SIGN_EXTEND:
13620     case ZERO_EXTEND:
13621       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13622                                 VAR_INIT_STATUS_INITIALIZED);
13623       if (op0 == 0)
13624         break;
13625       else
13626         {
13627           int shift = DWARF2_ADDR_SIZE
13628                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13629           shift *= BITS_PER_UNIT;
13630           if (GET_CODE (rtl) == SIGN_EXTEND)
13631             op = DW_OP_shra;
13632           else
13633             op = DW_OP_shr;
13634           mem_loc_result = op0;
13635           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13636           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13637           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13638           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13639         }
13640       break;
13641
13642     case MEM:
13643       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13644                                            VAR_INIT_STATUS_INITIALIZED);
13645       if (mem_loc_result == NULL)
13646         mem_loc_result = tls_mem_loc_descriptor (rtl);
13647       if (mem_loc_result != 0)
13648         {
13649           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13650             {
13651               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13652               return 0;
13653             }
13654           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13655             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13656           else
13657             add_loc_descr (&mem_loc_result,
13658                            new_loc_descr (DW_OP_deref_size,
13659                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13660         }
13661       else
13662         {
13663           rtx new_rtl = avoid_constant_pool_reference (rtl);
13664           if (new_rtl != rtl)
13665             return mem_loc_descriptor (new_rtl, mode, initialized);
13666         }
13667       break;
13668
13669     case LO_SUM:
13670          rtl = XEXP (rtl, 1);
13671
13672       /* ... fall through ...  */
13673
13674     case LABEL_REF:
13675       /* Some ports can transform a symbol ref into a label ref, because
13676          the symbol ref is too far away and has to be dumped into a constant
13677          pool.  */
13678     case CONST:
13679     case SYMBOL_REF:
13680       if (GET_CODE (rtl) == SYMBOL_REF
13681           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13682         {
13683           dw_loc_descr_ref temp;
13684
13685           /* If this is not defined, we have no way to emit the data.  */
13686           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13687             break;
13688
13689           /* We used to emit DW_OP_addr here, but that's wrong, since
13690              DW_OP_addr should be relocated by the debug info consumer,
13691              while DW_OP_GNU_push_tls_address operand should not.  */
13692           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13693                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13694           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13695           temp->dw_loc_oprnd1.v.val_addr = rtl;
13696           temp->dtprel = true;
13697
13698           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13699           add_loc_descr (&mem_loc_result, temp);
13700
13701           break;
13702         }
13703
13704       if (!const_ok_for_output (rtl))
13705         break;
13706
13707     symref:
13708       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13709       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13710       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13711       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13712       break;
13713
13714     case CONCAT:
13715     case CONCATN:
13716     case VAR_LOCATION:
13717     case DEBUG_IMPLICIT_PTR:
13718       expansion_failed (NULL_TREE, rtl,
13719                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13720       return 0;
13721
13722     case PRE_MODIFY:
13723       /* Extract the PLUS expression nested inside and fall into
13724          PLUS code below.  */
13725       rtl = XEXP (rtl, 1);
13726       goto plus;
13727
13728     case PRE_INC:
13729     case PRE_DEC:
13730       /* Turn these into a PLUS expression and fall into the PLUS code
13731          below.  */
13732       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13733                           GEN_INT (GET_CODE (rtl) == PRE_INC
13734                                    ? GET_MODE_UNIT_SIZE (mode)
13735                                    : -GET_MODE_UNIT_SIZE (mode)));
13736
13737       /* ... fall through ...  */
13738
13739     case PLUS:
13740     plus:
13741       if (is_based_loc (rtl))
13742         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13743                                           INTVAL (XEXP (rtl, 1)),
13744                                           VAR_INIT_STATUS_INITIALIZED);
13745       else
13746         {
13747           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13748                                                VAR_INIT_STATUS_INITIALIZED);
13749           if (mem_loc_result == 0)
13750             break;
13751
13752           if (CONST_INT_P (XEXP (rtl, 1)))
13753             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13754           else
13755             {
13756               dw_loc_descr_ref mem_loc_result2
13757                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13758                                       VAR_INIT_STATUS_INITIALIZED);
13759               if (mem_loc_result2 == 0)
13760                 break;
13761               add_loc_descr (&mem_loc_result, mem_loc_result2);
13762               add_loc_descr (&mem_loc_result,
13763                              new_loc_descr (DW_OP_plus, 0, 0));
13764             }
13765         }
13766       break;
13767
13768     /* If a pseudo-reg is optimized away, it is possible for it to
13769        be replaced with a MEM containing a multiply or shift.  */
13770     case MINUS:
13771       op = DW_OP_minus;
13772       goto do_binop;
13773
13774     case MULT:
13775       op = DW_OP_mul;
13776       goto do_binop;
13777
13778     case DIV:
13779       op = DW_OP_div;
13780       goto do_binop;
13781
13782     case UMOD:
13783       op = DW_OP_mod;
13784       goto do_binop;
13785
13786     case ASHIFT:
13787       op = DW_OP_shl;
13788       goto do_binop;
13789
13790     case ASHIFTRT:
13791       op = DW_OP_shra;
13792       goto do_binop;
13793
13794     case LSHIFTRT:
13795       op = DW_OP_shr;
13796       goto do_binop;
13797
13798     case AND:
13799       op = DW_OP_and;
13800       goto do_binop;
13801
13802     case IOR:
13803       op = DW_OP_or;
13804       goto do_binop;
13805
13806     case XOR:
13807       op = DW_OP_xor;
13808       goto do_binop;
13809
13810     do_binop:
13811       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13812                                 VAR_INIT_STATUS_INITIALIZED);
13813       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13814                                 VAR_INIT_STATUS_INITIALIZED);
13815
13816       if (op0 == 0 || op1 == 0)
13817         break;
13818
13819       mem_loc_result = op0;
13820       add_loc_descr (&mem_loc_result, op1);
13821       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13822       break;
13823
13824     case MOD:
13825       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13826                                 VAR_INIT_STATUS_INITIALIZED);
13827       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13828                                 VAR_INIT_STATUS_INITIALIZED);
13829
13830       if (op0 == 0 || op1 == 0)
13831         break;
13832
13833       mem_loc_result = op0;
13834       add_loc_descr (&mem_loc_result, op1);
13835       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13836       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13837       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13838       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13839       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13840       break;
13841
13842     case NOT:
13843       op = DW_OP_not;
13844       goto do_unop;
13845
13846     case ABS:
13847       op = DW_OP_abs;
13848       goto do_unop;
13849
13850     case NEG:
13851       op = DW_OP_neg;
13852       goto do_unop;
13853
13854     do_unop:
13855       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13856                                 VAR_INIT_STATUS_INITIALIZED);
13857
13858       if (op0 == 0)
13859         break;
13860
13861       mem_loc_result = op0;
13862       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13863       break;
13864
13865     case CONST_INT:
13866       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13867       break;
13868
13869     case EQ:
13870       op = DW_OP_eq;
13871       goto do_scompare;
13872
13873     case GE:
13874       op = DW_OP_ge;
13875       goto do_scompare;
13876
13877     case GT:
13878       op = DW_OP_gt;
13879       goto do_scompare;
13880
13881     case LE:
13882       op = DW_OP_le;
13883       goto do_scompare;
13884
13885     case LT:
13886       op = DW_OP_lt;
13887       goto do_scompare;
13888
13889     case NE:
13890       op = DW_OP_ne;
13891       goto do_scompare;
13892
13893     do_scompare:
13894       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13895           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13896         break;
13897       else
13898         {
13899           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13900
13901           if (op_mode == VOIDmode)
13902             op_mode = GET_MODE (XEXP (rtl, 1));
13903           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13904             break;
13905
13906           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13907                                     VAR_INIT_STATUS_INITIALIZED);
13908           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13909                                     VAR_INIT_STATUS_INITIALIZED);
13910
13911           if (op0 == 0 || op1 == 0)
13912             break;
13913
13914           if (op_mode != VOIDmode
13915               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13916             {
13917               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13918               shift *= BITS_PER_UNIT;
13919               /* For eq/ne, if the operands are known to be zero-extended,
13920                  there is no need to do the fancy shifting up.  */
13921               if (op == DW_OP_eq || op == DW_OP_ne)
13922                 {
13923                   dw_loc_descr_ref last0, last1;
13924                   for (last0 = op0;
13925                        last0->dw_loc_next != NULL;
13926                        last0 = last0->dw_loc_next)
13927                     ;
13928                   for (last1 = op1;
13929                        last1->dw_loc_next != NULL;
13930                        last1 = last1->dw_loc_next)
13931                     ;
13932                   /* deref_size zero extends, and for constants we can check
13933                      whether they are zero extended or not.  */
13934                   if (((last0->dw_loc_opc == DW_OP_deref_size
13935                         && last0->dw_loc_oprnd1.v.val_int
13936                            <= GET_MODE_SIZE (op_mode))
13937                        || (CONST_INT_P (XEXP (rtl, 0))
13938                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13939                                == (INTVAL (XEXP (rtl, 0))
13940                                    & GET_MODE_MASK (op_mode))))
13941                       && ((last1->dw_loc_opc == DW_OP_deref_size
13942                            && last1->dw_loc_oprnd1.v.val_int
13943                               <= GET_MODE_SIZE (op_mode))
13944                           || (CONST_INT_P (XEXP (rtl, 1))
13945                               && (unsigned HOST_WIDE_INT)
13946                                  INTVAL (XEXP (rtl, 1))
13947                                  == (INTVAL (XEXP (rtl, 1))
13948                                      & GET_MODE_MASK (op_mode)))))
13949                     goto do_compare;
13950                 }
13951               add_loc_descr (&op0, int_loc_descriptor (shift));
13952               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13953               if (CONST_INT_P (XEXP (rtl, 1)))
13954                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13955               else
13956                 {
13957                   add_loc_descr (&op1, int_loc_descriptor (shift));
13958                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13959                 }
13960             }
13961         }
13962
13963     do_compare:
13964       mem_loc_result = op0;
13965       add_loc_descr (&mem_loc_result, op1);
13966       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13967       if (STORE_FLAG_VALUE != 1)
13968         {
13969           add_loc_descr (&mem_loc_result,
13970                          int_loc_descriptor (STORE_FLAG_VALUE));
13971           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13972         }
13973       break;
13974
13975     case GEU:
13976       op = DW_OP_ge;
13977       goto do_ucompare;
13978
13979     case GTU:
13980       op = DW_OP_gt;
13981       goto do_ucompare;
13982
13983     case LEU:
13984       op = DW_OP_le;
13985       goto do_ucompare;
13986
13987     case LTU:
13988       op = DW_OP_lt;
13989       goto do_ucompare;
13990
13991     do_ucompare:
13992       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13993           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13994         break;
13995       else
13996         {
13997           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13998
13999           if (op_mode == VOIDmode)
14000             op_mode = GET_MODE (XEXP (rtl, 1));
14001           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14002             break;
14003
14004           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14005                                     VAR_INIT_STATUS_INITIALIZED);
14006           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14007                                     VAR_INIT_STATUS_INITIALIZED);
14008
14009           if (op0 == 0 || op1 == 0)
14010             break;
14011
14012           if (op_mode != VOIDmode
14013               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14014             {
14015               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14016               dw_loc_descr_ref last0, last1;
14017               for (last0 = op0;
14018                    last0->dw_loc_next != NULL;
14019                    last0 = last0->dw_loc_next)
14020                 ;
14021               for (last1 = op1;
14022                    last1->dw_loc_next != NULL;
14023                    last1 = last1->dw_loc_next)
14024                 ;
14025               if (CONST_INT_P (XEXP (rtl, 0)))
14026                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14027               /* deref_size zero extends, so no need to mask it again.  */
14028               else if (last0->dw_loc_opc != DW_OP_deref_size
14029                        || last0->dw_loc_oprnd1.v.val_int
14030                           > GET_MODE_SIZE (op_mode))
14031                 {
14032                   add_loc_descr (&op0, int_loc_descriptor (mask));
14033                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14034                 }
14035               if (CONST_INT_P (XEXP (rtl, 1)))
14036                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14037               /* deref_size zero extends, so no need to mask it again.  */
14038               else if (last1->dw_loc_opc != DW_OP_deref_size
14039                        || last1->dw_loc_oprnd1.v.val_int
14040                           > GET_MODE_SIZE (op_mode))
14041                 {
14042                   add_loc_descr (&op1, int_loc_descriptor (mask));
14043                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14044                 }
14045             }
14046           else
14047             {
14048               HOST_WIDE_INT bias = 1;
14049               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14050               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14051               if (CONST_INT_P (XEXP (rtl, 1)))
14052                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14053                                           + INTVAL (XEXP (rtl, 1)));
14054               else
14055                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14056                                                     bias, 0));
14057             }
14058         }
14059       goto do_compare;
14060
14061     case SMIN:
14062     case SMAX:
14063     case UMIN:
14064     case UMAX:
14065       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14066           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14067           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14068         break;
14069
14070       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14071                                 VAR_INIT_STATUS_INITIALIZED);
14072       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14073                                 VAR_INIT_STATUS_INITIALIZED);
14074
14075       if (op0 == 0 || op1 == 0)
14076         break;
14077
14078       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14079       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14080       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14081       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14082         {
14083           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14084             {
14085               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14086               add_loc_descr (&op0, int_loc_descriptor (mask));
14087               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14088               add_loc_descr (&op1, int_loc_descriptor (mask));
14089               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
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               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14097             }
14098         }
14099       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14100         {
14101           int shift = DWARF2_ADDR_SIZE
14102                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14103           shift *= BITS_PER_UNIT;
14104           add_loc_descr (&op0, int_loc_descriptor (shift));
14105           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14106           add_loc_descr (&op1, int_loc_descriptor (shift));
14107           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14108         }
14109
14110       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14111         op = DW_OP_lt;
14112       else
14113         op = DW_OP_gt;
14114       mem_loc_result = op0;
14115       add_loc_descr (&mem_loc_result, op1);
14116       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14117       {
14118         dw_loc_descr_ref bra_node, drop_node;
14119
14120         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14121         add_loc_descr (&mem_loc_result, bra_node);
14122         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14123         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14124         add_loc_descr (&mem_loc_result, drop_node);
14125         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14126         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14127       }
14128       break;
14129
14130     case ZERO_EXTRACT:
14131     case SIGN_EXTRACT:
14132       if (CONST_INT_P (XEXP (rtl, 1))
14133           && CONST_INT_P (XEXP (rtl, 2))
14134           && ((unsigned) INTVAL (XEXP (rtl, 1))
14135               + (unsigned) INTVAL (XEXP (rtl, 2))
14136               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14137           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14138           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14139         {
14140           int shift, size;
14141           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14142                                     VAR_INIT_STATUS_INITIALIZED);
14143           if (op0 == 0)
14144             break;
14145           if (GET_CODE (rtl) == SIGN_EXTRACT)
14146             op = DW_OP_shra;
14147           else
14148             op = DW_OP_shr;
14149           mem_loc_result = op0;
14150           size = INTVAL (XEXP (rtl, 1));
14151           shift = INTVAL (XEXP (rtl, 2));
14152           if (BITS_BIG_ENDIAN)
14153             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14154                     - shift - size;
14155           if (shift + size != (int) DWARF2_ADDR_SIZE)
14156             {
14157               add_loc_descr (&mem_loc_result,
14158                              int_loc_descriptor (DWARF2_ADDR_SIZE
14159                                                  - shift - size));
14160               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14161             }
14162           if (size != (int) DWARF2_ADDR_SIZE)
14163             {
14164               add_loc_descr (&mem_loc_result,
14165                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14166               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14167             }
14168         }
14169       break;
14170
14171     case IF_THEN_ELSE:
14172       {
14173         dw_loc_descr_ref op2, bra_node, drop_node;
14174         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14175                                   VAR_INIT_STATUS_INITIALIZED);
14176         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14177                                   VAR_INIT_STATUS_INITIALIZED);
14178         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14179                                   VAR_INIT_STATUS_INITIALIZED);
14180         if (op0 == NULL || op1 == NULL || op2 == NULL)
14181           break;
14182
14183         mem_loc_result = op1;
14184         add_loc_descr (&mem_loc_result, op2);
14185         add_loc_descr (&mem_loc_result, op0);
14186         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14187         add_loc_descr (&mem_loc_result, bra_node);
14188         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14189         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14190         add_loc_descr (&mem_loc_result, drop_node);
14191         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14192         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14193       }
14194       break;
14195
14196     case COMPARE:
14197     case ROTATE:
14198     case ROTATERT:
14199     case TRUNCATE:
14200       /* In theory, we could implement the above.  */
14201       /* DWARF cannot represent the unsigned compare operations
14202          natively.  */
14203     case SS_MULT:
14204     case US_MULT:
14205     case SS_DIV:
14206     case US_DIV:
14207     case SS_PLUS:
14208     case US_PLUS:
14209     case SS_MINUS:
14210     case US_MINUS:
14211     case SS_NEG:
14212     case US_NEG:
14213     case SS_ABS:
14214     case SS_ASHIFT:
14215     case US_ASHIFT:
14216     case SS_TRUNCATE:
14217     case US_TRUNCATE:
14218     case UDIV:
14219     case UNORDERED:
14220     case ORDERED:
14221     case UNEQ:
14222     case UNGE:
14223     case UNGT:
14224     case UNLE:
14225     case UNLT:
14226     case LTGT:
14227     case FLOAT_EXTEND:
14228     case FLOAT_TRUNCATE:
14229     case FLOAT:
14230     case UNSIGNED_FLOAT:
14231     case FIX:
14232     case UNSIGNED_FIX:
14233     case FRACT_CONVERT:
14234     case UNSIGNED_FRACT_CONVERT:
14235     case SAT_FRACT:
14236     case UNSIGNED_SAT_FRACT:
14237     case SQRT:
14238     case BSWAP:
14239     case FFS:
14240     case CLZ:
14241     case CTZ:
14242     case POPCOUNT:
14243     case PARITY:
14244     case ASM_OPERANDS:
14245     case VEC_MERGE:
14246     case VEC_SELECT:
14247     case VEC_CONCAT:
14248     case VEC_DUPLICATE:
14249     case UNSPEC:
14250     case HIGH:
14251       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14252          can't express it in the debug info.  This can happen e.g. with some
14253          TLS UNSPECs.  */
14254       break;
14255
14256     case CONST_STRING:
14257       resolve_one_addr (&rtl, NULL);
14258       goto symref;
14259
14260     default:
14261 #ifdef ENABLE_CHECKING
14262       print_rtl (stderr, rtl);
14263       gcc_unreachable ();
14264 #else
14265       break;
14266 #endif
14267     }
14268
14269   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14270     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14271
14272   return mem_loc_result;
14273 }
14274
14275 /* Return a descriptor that describes the concatenation of two locations.
14276    This is typically a complex variable.  */
14277
14278 static dw_loc_descr_ref
14279 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14280 {
14281   dw_loc_descr_ref cc_loc_result = NULL;
14282   dw_loc_descr_ref x0_ref
14283     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14284   dw_loc_descr_ref x1_ref
14285     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14286
14287   if (x0_ref == 0 || x1_ref == 0)
14288     return 0;
14289
14290   cc_loc_result = x0_ref;
14291   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14292
14293   add_loc_descr (&cc_loc_result, x1_ref);
14294   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14295
14296   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14297     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14298
14299   return cc_loc_result;
14300 }
14301
14302 /* Return a descriptor that describes the concatenation of N
14303    locations.  */
14304
14305 static dw_loc_descr_ref
14306 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14307 {
14308   unsigned int i;
14309   dw_loc_descr_ref cc_loc_result = NULL;
14310   unsigned int n = XVECLEN (concatn, 0);
14311
14312   for (i = 0; i < n; ++i)
14313     {
14314       dw_loc_descr_ref ref;
14315       rtx x = XVECEXP (concatn, 0, i);
14316
14317       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14318       if (ref == NULL)
14319         return NULL;
14320
14321       add_loc_descr (&cc_loc_result, ref);
14322       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14323     }
14324
14325   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14326     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14327
14328   return cc_loc_result;
14329 }
14330
14331 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14332    for DEBUG_IMPLICIT_PTR RTL.  */
14333
14334 static dw_loc_descr_ref
14335 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14336 {
14337   dw_loc_descr_ref ret;
14338   dw_die_ref ref;
14339
14340   if (dwarf_strict)
14341     return NULL;
14342   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14343               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14344               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14345   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14346   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14347   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14348   if (ref)
14349     {
14350       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14351       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14352       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14353     }
14354   else
14355     {
14356       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14357       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14358     }
14359   return ret;
14360 }
14361
14362 /* Output a proper Dwarf location descriptor for a variable or parameter
14363    which is either allocated in a register or in a memory location.  For a
14364    register, we just generate an OP_REG and the register number.  For a
14365    memory location we provide a Dwarf postfix expression describing how to
14366    generate the (dynamic) address of the object onto the address stack.
14367
14368    MODE is mode of the decl if this loc_descriptor is going to be used in
14369    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14370    allowed, VOIDmode otherwise.
14371
14372    If we don't know how to describe it, return 0.  */
14373
14374 static dw_loc_descr_ref
14375 loc_descriptor (rtx rtl, enum machine_mode mode,
14376                 enum var_init_status initialized)
14377 {
14378   dw_loc_descr_ref loc_result = NULL;
14379
14380   switch (GET_CODE (rtl))
14381     {
14382     case SUBREG:
14383       /* The case of a subreg may arise when we have a local (register)
14384          variable or a formal (register) parameter which doesn't quite fill
14385          up an entire register.  For now, just assume that it is
14386          legitimate to make the Dwarf info refer to the whole register which
14387          contains the given subreg.  */
14388       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14389       break;
14390
14391     case REG:
14392       loc_result = reg_loc_descriptor (rtl, initialized);
14393       break;
14394
14395     case MEM:
14396       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14397                                        initialized);
14398       if (loc_result == NULL)
14399         loc_result = tls_mem_loc_descriptor (rtl);
14400       if (loc_result == NULL)
14401         {
14402           rtx new_rtl = avoid_constant_pool_reference (rtl);
14403           if (new_rtl != rtl)
14404             loc_result = loc_descriptor (new_rtl, mode, initialized);
14405         }
14406       break;
14407
14408     case CONCAT:
14409       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14410                                           initialized);
14411       break;
14412
14413     case CONCATN:
14414       loc_result = concatn_loc_descriptor (rtl, initialized);
14415       break;
14416
14417     case VAR_LOCATION:
14418       /* Single part.  */
14419       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14420         {
14421           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14422           if (GET_CODE (loc) == EXPR_LIST)
14423             loc = XEXP (loc, 0);
14424           loc_result = loc_descriptor (loc, mode, initialized);
14425           break;
14426         }
14427
14428       rtl = XEXP (rtl, 1);
14429       /* FALLTHRU */
14430
14431     case PARALLEL:
14432       {
14433         rtvec par_elems = XVEC (rtl, 0);
14434         int num_elem = GET_NUM_ELEM (par_elems);
14435         enum machine_mode mode;
14436         int i;
14437
14438         /* Create the first one, so we have something to add to.  */
14439         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14440                                      VOIDmode, initialized);
14441         if (loc_result == NULL)
14442           return NULL;
14443         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14444         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14445         for (i = 1; i < num_elem; i++)
14446           {
14447             dw_loc_descr_ref temp;
14448
14449             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14450                                    VOIDmode, initialized);
14451             if (temp == NULL)
14452               return NULL;
14453             add_loc_descr (&loc_result, temp);
14454             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14455             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14456           }
14457       }
14458       break;
14459
14460     case CONST_INT:
14461       if (mode != VOIDmode && mode != BLKmode)
14462         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14463                                                     INTVAL (rtl));
14464       break;
14465
14466     case CONST_DOUBLE:
14467       if (mode == VOIDmode)
14468         mode = GET_MODE (rtl);
14469
14470       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14471         {
14472           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14473
14474           /* Note that a CONST_DOUBLE rtx could represent either an integer
14475              or a floating-point constant.  A CONST_DOUBLE is used whenever
14476              the constant requires more than one word in order to be
14477              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14478           loc_result = new_loc_descr (DW_OP_implicit_value,
14479                                       GET_MODE_SIZE (mode), 0);
14480           if (SCALAR_FLOAT_MODE_P (mode))
14481             {
14482               unsigned int length = GET_MODE_SIZE (mode);
14483               unsigned char *array
14484                   = (unsigned char*) ggc_alloc_atomic (length);
14485
14486               insert_float (rtl, array);
14487               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14488               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14489               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14490               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14491             }
14492           else
14493             {
14494               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14495               loc_result->dw_loc_oprnd2.v.val_double
14496                 = rtx_to_double_int (rtl);
14497             }
14498         }
14499       break;
14500
14501     case CONST_VECTOR:
14502       if (mode == VOIDmode)
14503         mode = GET_MODE (rtl);
14504
14505       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14506         {
14507           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14508           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14509           unsigned char *array = (unsigned char *)
14510             ggc_alloc_atomic (length * elt_size);
14511           unsigned int i;
14512           unsigned char *p;
14513
14514           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14515           switch (GET_MODE_CLASS (mode))
14516             {
14517             case MODE_VECTOR_INT:
14518               for (i = 0, p = array; i < length; i++, p += elt_size)
14519                 {
14520                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14521                   double_int val = rtx_to_double_int (elt);
14522
14523                   if (elt_size <= sizeof (HOST_WIDE_INT))
14524                     insert_int (double_int_to_shwi (val), elt_size, p);
14525                   else
14526                     {
14527                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14528                       insert_double (val, p);
14529                     }
14530                 }
14531               break;
14532
14533             case MODE_VECTOR_FLOAT:
14534               for (i = 0, p = array; i < length; i++, p += elt_size)
14535                 {
14536                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14537                   insert_float (elt, p);
14538                 }
14539               break;
14540
14541             default:
14542               gcc_unreachable ();
14543             }
14544
14545           loc_result = new_loc_descr (DW_OP_implicit_value,
14546                                       length * elt_size, 0);
14547           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14548           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14549           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14550           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14551         }
14552       break;
14553
14554     case CONST:
14555       if (mode == VOIDmode
14556           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14557           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14558           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14559         {
14560           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14561           break;
14562         }
14563       /* FALLTHROUGH */
14564     case SYMBOL_REF:
14565       if (!const_ok_for_output (rtl))
14566         break;
14567     case LABEL_REF:
14568       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14569           && (dwarf_version >= 4 || !dwarf_strict))
14570         {
14571           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14572           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14573           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14574           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14575           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14576         }
14577       break;
14578
14579     case DEBUG_IMPLICIT_PTR:
14580       loc_result = implicit_ptr_descriptor (rtl, 0);
14581       break;
14582
14583     case PLUS:
14584       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14585           && CONST_INT_P (XEXP (rtl, 1)))
14586         {
14587           loc_result
14588             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14589           break;
14590         }
14591       /* FALLTHRU */
14592     default:
14593       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14594           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14595           && (dwarf_version >= 4 || !dwarf_strict))
14596         {
14597           /* Value expression.  */
14598           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14599           if (loc_result)
14600             add_loc_descr (&loc_result,
14601                            new_loc_descr (DW_OP_stack_value, 0, 0));
14602         }
14603       break;
14604     }
14605
14606   return loc_result;
14607 }
14608
14609 /* We need to figure out what section we should use as the base for the
14610    address ranges where a given location is valid.
14611    1. If this particular DECL has a section associated with it, use that.
14612    2. If this function has a section associated with it, use that.
14613    3. Otherwise, use the text section.
14614    XXX: If you split a variable across multiple sections, we won't notice.  */
14615
14616 static const char *
14617 secname_for_decl (const_tree decl)
14618 {
14619   const char *secname;
14620
14621   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14622     {
14623       tree sectree = DECL_SECTION_NAME (decl);
14624       secname = TREE_STRING_POINTER (sectree);
14625     }
14626   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14627     {
14628       tree sectree = DECL_SECTION_NAME (current_function_decl);
14629       secname = TREE_STRING_POINTER (sectree);
14630     }
14631   else if (cfun && in_cold_section_p)
14632     secname = crtl->subsections.cold_section_label;
14633   else
14634     secname = text_section_label;
14635
14636   return secname;
14637 }
14638
14639 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14640
14641 static bool
14642 decl_by_reference_p (tree decl)
14643 {
14644   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14645            || TREE_CODE (decl) == VAR_DECL)
14646           && DECL_BY_REFERENCE (decl));
14647 }
14648
14649 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14650    for VARLOC.  */
14651
14652 static dw_loc_descr_ref
14653 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14654                enum var_init_status initialized)
14655 {
14656   int have_address = 0;
14657   dw_loc_descr_ref descr;
14658   enum machine_mode mode;
14659
14660   if (want_address != 2)
14661     {
14662       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14663       /* Single part.  */
14664       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14665         {
14666           varloc = PAT_VAR_LOCATION_LOC (varloc);
14667           if (GET_CODE (varloc) == EXPR_LIST)
14668             varloc = XEXP (varloc, 0);
14669           mode = GET_MODE (varloc);
14670           if (MEM_P (varloc))
14671             {
14672               rtx addr = XEXP (varloc, 0);
14673               descr = mem_loc_descriptor (addr, mode, initialized);
14674               if (descr)
14675                 have_address = 1;
14676               else
14677                 {
14678                   rtx x = avoid_constant_pool_reference (varloc);
14679                   if (x != varloc)
14680                     descr = mem_loc_descriptor (x, mode, initialized);
14681                 }
14682             }
14683           else
14684             descr = mem_loc_descriptor (varloc, mode, initialized);
14685         }
14686       else
14687         return 0;
14688     }
14689   else
14690     {
14691       if (GET_CODE (varloc) == VAR_LOCATION)
14692         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14693       else
14694         mode = DECL_MODE (loc);
14695       descr = loc_descriptor (varloc, mode, initialized);
14696       have_address = 1;
14697     }
14698
14699   if (!descr)
14700     return 0;
14701
14702   if (want_address == 2 && !have_address
14703       && (dwarf_version >= 4 || !dwarf_strict))
14704     {
14705       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14706         {
14707           expansion_failed (loc, NULL_RTX,
14708                             "DWARF address size mismatch");
14709           return 0;
14710         }
14711       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14712       have_address = 1;
14713     }
14714   /* Show if we can't fill the request for an address.  */
14715   if (want_address && !have_address)
14716     {
14717       expansion_failed (loc, NULL_RTX,
14718                         "Want address and only have value");
14719       return 0;
14720     }
14721
14722   /* If we've got an address and don't want one, dereference.  */
14723   if (!want_address && have_address)
14724     {
14725       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14726       enum dwarf_location_atom op;
14727
14728       if (size > DWARF2_ADDR_SIZE || size == -1)
14729         {
14730           expansion_failed (loc, NULL_RTX,
14731                             "DWARF address size mismatch");
14732           return 0;
14733         }
14734       else if (size == DWARF2_ADDR_SIZE)
14735         op = DW_OP_deref;
14736       else
14737         op = DW_OP_deref_size;
14738
14739       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14740     }
14741
14742   return descr;
14743 }
14744
14745 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14746    if it is not possible.  */
14747
14748 static dw_loc_descr_ref
14749 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14750 {
14751   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14752     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14753   else if (dwarf_version >= 3 || !dwarf_strict)
14754     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14755   else
14756     return NULL;
14757 }
14758
14759 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14760    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14761
14762 static dw_loc_descr_ref
14763 dw_sra_loc_expr (tree decl, rtx loc)
14764 {
14765   rtx p;
14766   unsigned int padsize = 0;
14767   dw_loc_descr_ref descr, *descr_tail;
14768   unsigned HOST_WIDE_INT decl_size;
14769   rtx varloc;
14770   enum var_init_status initialized;
14771
14772   if (DECL_SIZE (decl) == NULL
14773       || !host_integerp (DECL_SIZE (decl), 1))
14774     return NULL;
14775
14776   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14777   descr = NULL;
14778   descr_tail = &descr;
14779
14780   for (p = loc; p; p = XEXP (p, 1))
14781     {
14782       unsigned int bitsize = decl_piece_bitsize (p);
14783       rtx loc_note = *decl_piece_varloc_ptr (p);
14784       dw_loc_descr_ref cur_descr;
14785       dw_loc_descr_ref *tail, last = NULL;
14786       unsigned int opsize = 0;
14787
14788       if (loc_note == NULL_RTX
14789           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14790         {
14791           padsize += bitsize;
14792           continue;
14793         }
14794       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14795       varloc = NOTE_VAR_LOCATION (loc_note);
14796       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14797       if (cur_descr == NULL)
14798         {
14799           padsize += bitsize;
14800           continue;
14801         }
14802
14803       /* Check that cur_descr either doesn't use
14804          DW_OP_*piece operations, or their sum is equal
14805          to bitsize.  Otherwise we can't embed it.  */
14806       for (tail = &cur_descr; *tail != NULL;
14807            tail = &(*tail)->dw_loc_next)
14808         if ((*tail)->dw_loc_opc == DW_OP_piece)
14809           {
14810             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14811                       * BITS_PER_UNIT;
14812             last = *tail;
14813           }
14814         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14815           {
14816             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14817             last = *tail;
14818           }
14819
14820       if (last != NULL && opsize != bitsize)
14821         {
14822           padsize += bitsize;
14823           continue;
14824         }
14825
14826       /* If there is a hole, add DW_OP_*piece after empty DWARF
14827          expression, which means that those bits are optimized out.  */
14828       if (padsize)
14829         {
14830           if (padsize > decl_size)
14831             return NULL;
14832           decl_size -= padsize;
14833           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14834           if (*descr_tail == NULL)
14835             return NULL;
14836           descr_tail = &(*descr_tail)->dw_loc_next;
14837           padsize = 0;
14838         }
14839       *descr_tail = cur_descr;
14840       descr_tail = tail;
14841       if (bitsize > decl_size)
14842         return NULL;
14843       decl_size -= bitsize;
14844       if (last == NULL)
14845         {
14846           HOST_WIDE_INT offset = 0;
14847           if (GET_CODE (varloc) == VAR_LOCATION
14848               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14849             {
14850               varloc = PAT_VAR_LOCATION_LOC (varloc);
14851               if (GET_CODE (varloc) == EXPR_LIST)
14852                 varloc = XEXP (varloc, 0);
14853             }
14854           do 
14855             {
14856               if (GET_CODE (varloc) == CONST
14857                   || GET_CODE (varloc) == SIGN_EXTEND
14858                   || GET_CODE (varloc) == ZERO_EXTEND)
14859                 varloc = XEXP (varloc, 0);
14860               else if (GET_CODE (varloc) == SUBREG)
14861                 varloc = SUBREG_REG (varloc);
14862               else
14863                 break;
14864             }
14865           while (1);
14866           /* DW_OP_bit_size offset should be zero for register
14867              or implicit location descriptions and empty location
14868              descriptions, but for memory addresses needs big endian
14869              adjustment.  */
14870           if (MEM_P (varloc))
14871             {
14872               unsigned HOST_WIDE_INT memsize
14873                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14874               if (memsize != bitsize)
14875                 {
14876                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14877                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14878                     return NULL;
14879                   if (memsize < bitsize)
14880                     return NULL;
14881                   if (BITS_BIG_ENDIAN)
14882                     offset = memsize - bitsize;
14883                 }
14884             }
14885
14886           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14887           if (*descr_tail == NULL)
14888             return NULL;
14889           descr_tail = &(*descr_tail)->dw_loc_next;
14890         }
14891     }
14892
14893   /* If there were any non-empty expressions, add padding till the end of
14894      the decl.  */
14895   if (descr != NULL && decl_size != 0)
14896     {
14897       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14898       if (*descr_tail == NULL)
14899         return NULL;
14900     }
14901   return descr;
14902 }
14903
14904 /* Return the dwarf representation of the location list LOC_LIST of
14905    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14906    function.  */
14907
14908 static dw_loc_list_ref
14909 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14910 {
14911   const char *endname, *secname;
14912   rtx varloc;
14913   enum var_init_status initialized;
14914   struct var_loc_node *node;
14915   dw_loc_descr_ref descr;
14916   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14917   dw_loc_list_ref list = NULL;
14918   dw_loc_list_ref *listp = &list;
14919
14920   /* Now that we know what section we are using for a base,
14921      actually construct the list of locations.
14922      The first location information is what is passed to the
14923      function that creates the location list, and the remaining
14924      locations just get added on to that list.
14925      Note that we only know the start address for a location
14926      (IE location changes), so to build the range, we use
14927      the range [current location start, next location start].
14928      This means we have to special case the last node, and generate
14929      a range of [last location start, end of function label].  */
14930
14931   secname = secname_for_decl (decl);
14932
14933   for (node = loc_list->first; node; node = node->next)
14934     if (GET_CODE (node->loc) == EXPR_LIST
14935         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14936       {
14937         if (GET_CODE (node->loc) == EXPR_LIST)
14938           {
14939             /* This requires DW_OP_{,bit_}piece, which is not usable
14940                inside DWARF expressions.  */
14941             if (want_address != 2)
14942               continue;
14943             descr = dw_sra_loc_expr (decl, node->loc);
14944             if (descr == NULL)
14945               continue;
14946           }
14947         else
14948           {
14949             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14950             varloc = NOTE_VAR_LOCATION (node->loc);
14951             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14952           }
14953         if (descr)
14954           {
14955             /* The variable has a location between NODE->LABEL and
14956                NODE->NEXT->LABEL.  */
14957             if (node->next)
14958               endname = node->next->label;
14959             /* If the variable has a location at the last label
14960                it keeps its location until the end of function.  */
14961             else if (!current_function_decl)
14962               endname = text_end_label;
14963             else
14964               {
14965                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14966                                              current_function_funcdef_no);
14967                 endname = ggc_strdup (label_id);
14968               }
14969
14970             *listp = new_loc_list (descr, node->label, endname, secname);
14971             listp = &(*listp)->dw_loc_next;
14972           }
14973       }
14974
14975   /* Try to avoid the overhead of a location list emitting a location
14976      expression instead, but only if we didn't have more than one
14977      location entry in the first place.  If some entries were not
14978      representable, we don't want to pretend a single entry that was
14979      applies to the entire scope in which the variable is
14980      available.  */
14981   if (list && loc_list->first->next)
14982     gen_llsym (list);
14983
14984   return list;
14985 }
14986
14987 /* Return if the loc_list has only single element and thus can be represented
14988    as location description.   */
14989
14990 static bool
14991 single_element_loc_list_p (dw_loc_list_ref list)
14992 {
14993   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14994   return !list->ll_symbol;
14995 }
14996
14997 /* To each location in list LIST add loc descr REF.  */
14998
14999 static void
15000 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15001 {
15002   dw_loc_descr_ref copy;
15003   add_loc_descr (&list->expr, ref);
15004   list = list->dw_loc_next;
15005   while (list)
15006     {
15007       copy = ggc_alloc_dw_loc_descr_node ();
15008       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15009       add_loc_descr (&list->expr, copy);
15010       while (copy->dw_loc_next)
15011         {
15012           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15013           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15014           copy->dw_loc_next = new_copy;
15015           copy = new_copy;
15016         }
15017       list = list->dw_loc_next;
15018     }
15019 }
15020
15021 /* Given two lists RET and LIST
15022    produce location list that is result of adding expression in LIST
15023    to expression in RET on each possition in program.
15024    Might be destructive on both RET and LIST.
15025
15026    TODO: We handle only simple cases of RET or LIST having at most one
15027    element. General case would inolve sorting the lists in program order
15028    and merging them that will need some additional work.
15029    Adding that will improve quality of debug info especially for SRA-ed
15030    structures.  */
15031
15032 static void
15033 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15034 {
15035   if (!list)
15036     return;
15037   if (!*ret)
15038     {
15039       *ret = list;
15040       return;
15041     }
15042   if (!list->dw_loc_next)
15043     {
15044       add_loc_descr_to_each (*ret, list->expr);
15045       return;
15046     }
15047   if (!(*ret)->dw_loc_next)
15048     {
15049       add_loc_descr_to_each (list, (*ret)->expr);
15050       *ret = list;
15051       return;
15052     }
15053   expansion_failed (NULL_TREE, NULL_RTX,
15054                     "Don't know how to merge two non-trivial"
15055                     " location lists.\n");
15056   *ret = NULL;
15057   return;
15058 }
15059
15060 /* LOC is constant expression.  Try a luck, look it up in constant
15061    pool and return its loc_descr of its address.  */
15062
15063 static dw_loc_descr_ref
15064 cst_pool_loc_descr (tree loc)
15065 {
15066   /* Get an RTL for this, if something has been emitted.  */
15067   rtx rtl = lookup_constant_def (loc);
15068   enum machine_mode mode;
15069
15070   if (!rtl || !MEM_P (rtl))
15071     {
15072       gcc_assert (!rtl);
15073       return 0;
15074     }
15075   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15076
15077   /* TODO: We might get more coverage if we was actually delaying expansion
15078      of all expressions till end of compilation when constant pools are fully
15079      populated.  */
15080   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15081     {
15082       expansion_failed (loc, NULL_RTX,
15083                         "CST value in contant pool but not marked.");
15084       return 0;
15085     }
15086   mode = GET_MODE (rtl);
15087   rtl = XEXP (rtl, 0);
15088   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15089 }
15090
15091 /* Return dw_loc_list representing address of addr_expr LOC
15092    by looking for innder INDIRECT_REF expression and turing it
15093    into simple arithmetics.  */
15094
15095 static dw_loc_list_ref
15096 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15097 {
15098   tree obj, offset;
15099   HOST_WIDE_INT bitsize, bitpos, bytepos;
15100   enum machine_mode mode;
15101   int volatilep;
15102   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15103   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15104
15105   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15106                              &bitsize, &bitpos, &offset, &mode,
15107                              &unsignedp, &volatilep, false);
15108   STRIP_NOPS (obj);
15109   if (bitpos % BITS_PER_UNIT)
15110     {
15111       expansion_failed (loc, NULL_RTX, "bitfield access");
15112       return 0;
15113     }
15114   if (!INDIRECT_REF_P (obj))
15115     {
15116       expansion_failed (obj,
15117                         NULL_RTX, "no indirect ref in inner refrence");
15118       return 0;
15119     }
15120   if (!offset && !bitpos)
15121     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15122   else if (toplev
15123            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15124            && (dwarf_version >= 4 || !dwarf_strict))
15125     {
15126       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15127       if (!list_ret)
15128         return 0;
15129       if (offset)
15130         {
15131           /* Variable offset.  */
15132           list_ret1 = loc_list_from_tree (offset, 0);
15133           if (list_ret1 == 0)
15134             return 0;
15135           add_loc_list (&list_ret, list_ret1);
15136           if (!list_ret)
15137             return 0;
15138           add_loc_descr_to_each (list_ret,
15139                                  new_loc_descr (DW_OP_plus, 0, 0));
15140         }
15141       bytepos = bitpos / BITS_PER_UNIT;
15142       if (bytepos > 0)
15143         add_loc_descr_to_each (list_ret,
15144                                new_loc_descr (DW_OP_plus_uconst,
15145                                               bytepos, 0));
15146       else if (bytepos < 0)
15147         loc_list_plus_const (list_ret, bytepos);
15148       add_loc_descr_to_each (list_ret,
15149                              new_loc_descr (DW_OP_stack_value, 0, 0));
15150     }
15151   return list_ret;
15152 }
15153
15154
15155 /* Generate Dwarf location list representing LOC.
15156    If WANT_ADDRESS is false, expression computing LOC will be computed
15157    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15158    if WANT_ADDRESS is 2, expression computing address useable in location
15159      will be returned (i.e. DW_OP_reg can be used
15160      to refer to register values).  */
15161
15162 static dw_loc_list_ref
15163 loc_list_from_tree (tree loc, int want_address)
15164 {
15165   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15166   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15167   int have_address = 0;
15168   enum dwarf_location_atom op;
15169
15170   /* ??? Most of the time we do not take proper care for sign/zero
15171      extending the values properly.  Hopefully this won't be a real
15172      problem...  */
15173
15174   switch (TREE_CODE (loc))
15175     {
15176     case ERROR_MARK:
15177       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15178       return 0;
15179
15180     case PLACEHOLDER_EXPR:
15181       /* This case involves extracting fields from an object to determine the
15182          position of other fields.  We don't try to encode this here.  The
15183          only user of this is Ada, which encodes the needed information using
15184          the names of types.  */
15185       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15186       return 0;
15187
15188     case CALL_EXPR:
15189       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15190       /* There are no opcodes for these operations.  */
15191       return 0;
15192
15193     case PREINCREMENT_EXPR:
15194     case PREDECREMENT_EXPR:
15195     case POSTINCREMENT_EXPR:
15196     case POSTDECREMENT_EXPR:
15197       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15198       /* There are no opcodes for these operations.  */
15199       return 0;
15200
15201     case ADDR_EXPR:
15202       /* If we already want an address, see if there is INDIRECT_REF inside
15203          e.g. for &this->field.  */
15204       if (want_address)
15205         {
15206           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15207                        (loc, want_address == 2);
15208           if (list_ret)
15209             have_address = 1;
15210           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15211                    && (ret = cst_pool_loc_descr (loc)))
15212             have_address = 1;
15213         }
15214         /* Otherwise, process the argument and look for the address.  */
15215       if (!list_ret && !ret)
15216         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15217       else
15218         {
15219           if (want_address)
15220             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15221           return NULL;
15222         }
15223       break;
15224
15225     case VAR_DECL:
15226       if (DECL_THREAD_LOCAL_P (loc))
15227         {
15228           rtx rtl;
15229           enum dwarf_location_atom first_op;
15230           enum dwarf_location_atom second_op;
15231           bool dtprel = false;
15232
15233           if (targetm.have_tls)
15234             {
15235               /* If this is not defined, we have no way to emit the
15236                  data.  */
15237               if (!targetm.asm_out.output_dwarf_dtprel)
15238                 return 0;
15239
15240                /* The way DW_OP_GNU_push_tls_address is specified, we
15241                   can only look up addresses of objects in the current
15242                   module.  We used DW_OP_addr as first op, but that's
15243                   wrong, because DW_OP_addr is relocated by the debug
15244                   info consumer, while DW_OP_GNU_push_tls_address
15245                   operand shouldn't be.  */
15246               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15247                 return 0;
15248               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15249               dtprel = true;
15250               second_op = DW_OP_GNU_push_tls_address;
15251             }
15252           else
15253             {
15254               if (!targetm.emutls.debug_form_tls_address
15255                   || !(dwarf_version >= 3 || !dwarf_strict))
15256                 return 0;
15257               /* We stuffed the control variable into the DECL_VALUE_EXPR
15258                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15259                  no longer appear in gimple code.  We used the control
15260                  variable in specific so that we could pick it up here.  */
15261               loc = DECL_VALUE_EXPR (loc);
15262               first_op = DW_OP_addr;
15263               second_op = DW_OP_form_tls_address;
15264             }
15265
15266           rtl = rtl_for_decl_location (loc);
15267           if (rtl == NULL_RTX)
15268             return 0;
15269
15270           if (!MEM_P (rtl))
15271             return 0;
15272           rtl = XEXP (rtl, 0);
15273           if (! CONSTANT_P (rtl))
15274             return 0;
15275
15276           ret = new_loc_descr (first_op, 0, 0);
15277           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15278           ret->dw_loc_oprnd1.v.val_addr = rtl;
15279           ret->dtprel = dtprel;
15280
15281           ret1 = new_loc_descr (second_op, 0, 0);
15282           add_loc_descr (&ret, ret1);
15283
15284           have_address = 1;
15285           break;
15286         }
15287       /* FALLTHRU */
15288
15289     case PARM_DECL:
15290       if (DECL_HAS_VALUE_EXPR_P (loc))
15291         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15292                                    want_address);
15293       /* FALLTHRU */
15294
15295     case RESULT_DECL:
15296     case FUNCTION_DECL:
15297       {
15298         rtx rtl;
15299         var_loc_list *loc_list = lookup_decl_loc (loc);
15300
15301         if (loc_list && loc_list->first)
15302           {
15303             list_ret = dw_loc_list (loc_list, loc, want_address);
15304             have_address = want_address != 0;
15305             break;
15306           }
15307         rtl = rtl_for_decl_location (loc);
15308         if (rtl == NULL_RTX)
15309           {
15310             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15311             return 0;
15312           }
15313         else if (CONST_INT_P (rtl))
15314           {
15315             HOST_WIDE_INT val = INTVAL (rtl);
15316             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15317               val &= GET_MODE_MASK (DECL_MODE (loc));
15318             ret = int_loc_descriptor (val);
15319           }
15320         else if (GET_CODE (rtl) == CONST_STRING)
15321           {
15322             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15323             return 0;
15324           }
15325         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15326           {
15327             ret = new_loc_descr (DW_OP_addr, 0, 0);
15328             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15329             ret->dw_loc_oprnd1.v.val_addr = rtl;
15330           }
15331         else
15332           {
15333             enum machine_mode mode;
15334
15335             /* Certain constructs can only be represented at top-level.  */
15336             if (want_address == 2)
15337               {
15338                 ret = loc_descriptor (rtl, VOIDmode,
15339                                       VAR_INIT_STATUS_INITIALIZED);
15340                 have_address = 1;
15341               }
15342             else
15343               {
15344                 mode = GET_MODE (rtl);
15345                 if (MEM_P (rtl))
15346                   {
15347                     rtl = XEXP (rtl, 0);
15348                     have_address = 1;
15349                   }
15350                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15351               }
15352             if (!ret)
15353               expansion_failed (loc, rtl,
15354                                 "failed to produce loc descriptor for rtl");
15355           }
15356       }
15357       break;
15358
15359     case MEM_REF:
15360       /* ??? FIXME.  */
15361       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15362         return 0;
15363       /* Fallthru.  */
15364     case INDIRECT_REF:
15365       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15366       have_address = 1;
15367       break;
15368
15369     case COMPOUND_EXPR:
15370       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15371
15372     CASE_CONVERT:
15373     case VIEW_CONVERT_EXPR:
15374     case SAVE_EXPR:
15375     case MODIFY_EXPR:
15376       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15377
15378     case COMPONENT_REF:
15379     case BIT_FIELD_REF:
15380     case ARRAY_REF:
15381     case ARRAY_RANGE_REF:
15382     case REALPART_EXPR:
15383     case IMAGPART_EXPR:
15384       {
15385         tree obj, offset;
15386         HOST_WIDE_INT bitsize, bitpos, bytepos;
15387         enum machine_mode mode;
15388         int volatilep;
15389         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15390
15391         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15392                                    &unsignedp, &volatilep, false);
15393
15394         gcc_assert (obj != loc);
15395
15396         list_ret = loc_list_from_tree (obj,
15397                                        want_address == 2
15398                                        && !bitpos && !offset ? 2 : 1);
15399         /* TODO: We can extract value of the small expression via shifting even
15400            for nonzero bitpos.  */
15401         if (list_ret == 0)
15402           return 0;
15403         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15404           {
15405             expansion_failed (loc, NULL_RTX,
15406                               "bitfield access");
15407             return 0;
15408           }
15409
15410         if (offset != NULL_TREE)
15411           {
15412             /* Variable offset.  */
15413             list_ret1 = loc_list_from_tree (offset, 0);
15414             if (list_ret1 == 0)
15415               return 0;
15416             add_loc_list (&list_ret, list_ret1);
15417             if (!list_ret)
15418               return 0;
15419             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15420           }
15421
15422         bytepos = bitpos / BITS_PER_UNIT;
15423         if (bytepos > 0)
15424           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15425         else if (bytepos < 0)
15426           loc_list_plus_const (list_ret, bytepos);
15427
15428         have_address = 1;
15429         break;
15430       }
15431
15432     case INTEGER_CST:
15433       if ((want_address || !host_integerp (loc, 0))
15434           && (ret = cst_pool_loc_descr (loc)))
15435         have_address = 1;
15436       else if (want_address == 2
15437                && host_integerp (loc, 0)
15438                && (ret = address_of_int_loc_descriptor
15439                            (int_size_in_bytes (TREE_TYPE (loc)),
15440                             tree_low_cst (loc, 0))))
15441         have_address = 1;
15442       else if (host_integerp (loc, 0))
15443         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15444       else
15445         {
15446           expansion_failed (loc, NULL_RTX,
15447                             "Integer operand is not host integer");
15448           return 0;
15449         }
15450       break;
15451
15452     case CONSTRUCTOR:
15453     case REAL_CST:
15454     case STRING_CST:
15455     case COMPLEX_CST:
15456       if ((ret = cst_pool_loc_descr (loc)))
15457         have_address = 1;
15458       else
15459       /* We can construct small constants here using int_loc_descriptor.  */
15460         expansion_failed (loc, NULL_RTX,
15461                           "constructor or constant not in constant pool");
15462       break;
15463
15464     case TRUTH_AND_EXPR:
15465     case TRUTH_ANDIF_EXPR:
15466     case BIT_AND_EXPR:
15467       op = DW_OP_and;
15468       goto do_binop;
15469
15470     case TRUTH_XOR_EXPR:
15471     case BIT_XOR_EXPR:
15472       op = DW_OP_xor;
15473       goto do_binop;
15474
15475     case TRUTH_OR_EXPR:
15476     case TRUTH_ORIF_EXPR:
15477     case BIT_IOR_EXPR:
15478       op = DW_OP_or;
15479       goto do_binop;
15480
15481     case FLOOR_DIV_EXPR:
15482     case CEIL_DIV_EXPR:
15483     case ROUND_DIV_EXPR:
15484     case TRUNC_DIV_EXPR:
15485       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15486         return 0;
15487       op = DW_OP_div;
15488       goto do_binop;
15489
15490     case MINUS_EXPR:
15491       op = DW_OP_minus;
15492       goto do_binop;
15493
15494     case FLOOR_MOD_EXPR:
15495     case CEIL_MOD_EXPR:
15496     case ROUND_MOD_EXPR:
15497     case TRUNC_MOD_EXPR:
15498       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15499         {
15500           op = DW_OP_mod;
15501           goto do_binop;
15502         }
15503       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15504       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15505       if (list_ret == 0 || list_ret1 == 0)
15506         return 0;
15507
15508       add_loc_list (&list_ret, list_ret1);
15509       if (list_ret == 0)
15510         return 0;
15511       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15512       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15513       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15514       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15515       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15516       break;
15517
15518     case MULT_EXPR:
15519       op = DW_OP_mul;
15520       goto do_binop;
15521
15522     case LSHIFT_EXPR:
15523       op = DW_OP_shl;
15524       goto do_binop;
15525
15526     case RSHIFT_EXPR:
15527       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15528       goto do_binop;
15529
15530     case POINTER_PLUS_EXPR:
15531     case PLUS_EXPR:
15532       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15533         {
15534           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15535           if (list_ret == 0)
15536             return 0;
15537
15538           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15539           break;
15540         }
15541
15542       op = DW_OP_plus;
15543       goto do_binop;
15544
15545     case LE_EXPR:
15546       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15547         return 0;
15548
15549       op = DW_OP_le;
15550       goto do_binop;
15551
15552     case GE_EXPR:
15553       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15554         return 0;
15555
15556       op = DW_OP_ge;
15557       goto do_binop;
15558
15559     case LT_EXPR:
15560       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15561         return 0;
15562
15563       op = DW_OP_lt;
15564       goto do_binop;
15565
15566     case GT_EXPR:
15567       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15568         return 0;
15569
15570       op = DW_OP_gt;
15571       goto do_binop;
15572
15573     case EQ_EXPR:
15574       op = DW_OP_eq;
15575       goto do_binop;
15576
15577     case NE_EXPR:
15578       op = DW_OP_ne;
15579       goto do_binop;
15580
15581     do_binop:
15582       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15583       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15584       if (list_ret == 0 || list_ret1 == 0)
15585         return 0;
15586
15587       add_loc_list (&list_ret, list_ret1);
15588       if (list_ret == 0)
15589         return 0;
15590       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15591       break;
15592
15593     case TRUTH_NOT_EXPR:
15594     case BIT_NOT_EXPR:
15595       op = DW_OP_not;
15596       goto do_unop;
15597
15598     case ABS_EXPR:
15599       op = DW_OP_abs;
15600       goto do_unop;
15601
15602     case NEGATE_EXPR:
15603       op = DW_OP_neg;
15604       goto do_unop;
15605
15606     do_unop:
15607       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15608       if (list_ret == 0)
15609         return 0;
15610
15611       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15612       break;
15613
15614     case MIN_EXPR:
15615     case MAX_EXPR:
15616       {
15617         const enum tree_code code =
15618           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15619
15620         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15621                       build2 (code, integer_type_node,
15622                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15623                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15624       }
15625
15626       /* ... fall through ...  */
15627
15628     case COND_EXPR:
15629       {
15630         dw_loc_descr_ref lhs
15631           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15632         dw_loc_list_ref rhs
15633           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15634         dw_loc_descr_ref bra_node, jump_node, tmp;
15635
15636         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15637         if (list_ret == 0 || lhs == 0 || rhs == 0)
15638           return 0;
15639
15640         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15641         add_loc_descr_to_each (list_ret, bra_node);
15642
15643         add_loc_list (&list_ret, rhs);
15644         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15645         add_loc_descr_to_each (list_ret, jump_node);
15646
15647         add_loc_descr_to_each (list_ret, lhs);
15648         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15649         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15650
15651         /* ??? Need a node to point the skip at.  Use a nop.  */
15652         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15653         add_loc_descr_to_each (list_ret, tmp);
15654         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15655         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15656       }
15657       break;
15658
15659     case FIX_TRUNC_EXPR:
15660       return 0;
15661
15662     default:
15663       /* Leave front-end specific codes as simply unknown.  This comes
15664          up, for instance, with the C STMT_EXPR.  */
15665       if ((unsigned int) TREE_CODE (loc)
15666           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15667         {
15668           expansion_failed (loc, NULL_RTX,
15669                             "language specific tree node");
15670           return 0;
15671         }
15672
15673 #ifdef ENABLE_CHECKING
15674       /* Otherwise this is a generic code; we should just lists all of
15675          these explicitly.  We forgot one.  */
15676       gcc_unreachable ();
15677 #else
15678       /* In a release build, we want to degrade gracefully: better to
15679          generate incomplete debugging information than to crash.  */
15680       return NULL;
15681 #endif
15682     }
15683
15684   if (!ret && !list_ret)
15685     return 0;
15686
15687   if (want_address == 2 && !have_address
15688       && (dwarf_version >= 4 || !dwarf_strict))
15689     {
15690       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15691         {
15692           expansion_failed (loc, NULL_RTX,
15693                             "DWARF address size mismatch");
15694           return 0;
15695         }
15696       if (ret)
15697         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15698       else
15699         add_loc_descr_to_each (list_ret,
15700                                new_loc_descr (DW_OP_stack_value, 0, 0));
15701       have_address = 1;
15702     }
15703   /* Show if we can't fill the request for an address.  */
15704   if (want_address && !have_address)
15705     {
15706       expansion_failed (loc, NULL_RTX,
15707                         "Want address and only have value");
15708       return 0;
15709     }
15710
15711   gcc_assert (!ret || !list_ret);
15712
15713   /* If we've got an address and don't want one, dereference.  */
15714   if (!want_address && have_address)
15715     {
15716       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15717
15718       if (size > DWARF2_ADDR_SIZE || size == -1)
15719         {
15720           expansion_failed (loc, NULL_RTX,
15721                             "DWARF address size mismatch");
15722           return 0;
15723         }
15724       else if (size == DWARF2_ADDR_SIZE)
15725         op = DW_OP_deref;
15726       else
15727         op = DW_OP_deref_size;
15728
15729       if (ret)
15730         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15731       else
15732         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15733     }
15734   if (ret)
15735     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15736
15737   return list_ret;
15738 }
15739
15740 /* Same as above but return only single location expression.  */
15741 static dw_loc_descr_ref
15742 loc_descriptor_from_tree (tree loc, int want_address)
15743 {
15744   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15745   if (!ret)
15746     return NULL;
15747   if (ret->dw_loc_next)
15748     {
15749       expansion_failed (loc, NULL_RTX,
15750                         "Location list where only loc descriptor needed");
15751       return NULL;
15752     }
15753   return ret->expr;
15754 }
15755
15756 /* Given a value, round it up to the lowest multiple of `boundary'
15757    which is not less than the value itself.  */
15758
15759 static inline HOST_WIDE_INT
15760 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15761 {
15762   return (((value + boundary - 1) / boundary) * boundary);
15763 }
15764
15765 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15766    pointer to the declared type for the relevant field variable, or return
15767    `integer_type_node' if the given node turns out to be an
15768    ERROR_MARK node.  */
15769
15770 static inline tree
15771 field_type (const_tree decl)
15772 {
15773   tree type;
15774
15775   if (TREE_CODE (decl) == ERROR_MARK)
15776     return integer_type_node;
15777
15778   type = DECL_BIT_FIELD_TYPE (decl);
15779   if (type == NULL_TREE)
15780     type = TREE_TYPE (decl);
15781
15782   return type;
15783 }
15784
15785 /* Given a pointer to a tree node, return the alignment in bits for
15786    it, or else return BITS_PER_WORD if the node actually turns out to
15787    be an ERROR_MARK node.  */
15788
15789 static inline unsigned
15790 simple_type_align_in_bits (const_tree type)
15791 {
15792   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15793 }
15794
15795 static inline unsigned
15796 simple_decl_align_in_bits (const_tree decl)
15797 {
15798   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15799 }
15800
15801 /* Return the result of rounding T up to ALIGN.  */
15802
15803 static inline double_int
15804 round_up_to_align (double_int t, unsigned int align)
15805 {
15806   double_int alignd = uhwi_to_double_int (align);
15807   t = double_int_add (t, alignd);
15808   t = double_int_add (t, double_int_minus_one);
15809   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15810   t = double_int_mul (t, alignd);
15811   return t;
15812 }
15813
15814 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15815    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15816    or return 0 if we are unable to determine what that offset is, either
15817    because the argument turns out to be a pointer to an ERROR_MARK node, or
15818    because the offset is actually variable.  (We can't handle the latter case
15819    just yet).  */
15820
15821 static HOST_WIDE_INT
15822 field_byte_offset (const_tree decl)
15823 {
15824   double_int object_offset_in_bits;
15825   double_int object_offset_in_bytes;
15826   double_int bitpos_int;
15827
15828   if (TREE_CODE (decl) == ERROR_MARK)
15829     return 0;
15830
15831   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15832
15833   /* We cannot yet cope with fields whose positions are variable, so
15834      for now, when we see such things, we simply return 0.  Someday, we may
15835      be able to handle such cases, but it will be damn difficult.  */
15836   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15837     return 0;
15838
15839   bitpos_int = tree_to_double_int (bit_position (decl));
15840
15841 #ifdef PCC_BITFIELD_TYPE_MATTERS
15842   if (PCC_BITFIELD_TYPE_MATTERS)
15843     {
15844       tree type;
15845       tree field_size_tree;
15846       double_int deepest_bitpos;
15847       double_int field_size_in_bits;
15848       unsigned int type_align_in_bits;
15849       unsigned int decl_align_in_bits;
15850       double_int type_size_in_bits;
15851
15852       type = field_type (decl);
15853       type_size_in_bits = double_int_type_size_in_bits (type);
15854       type_align_in_bits = simple_type_align_in_bits (type);
15855
15856       field_size_tree = DECL_SIZE (decl);
15857
15858       /* The size could be unspecified if there was an error, or for
15859          a flexible array member.  */
15860       if (!field_size_tree)
15861         field_size_tree = bitsize_zero_node;
15862
15863       /* If the size of the field is not constant, use the type size.  */
15864       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15865         field_size_in_bits = tree_to_double_int (field_size_tree);
15866       else
15867         field_size_in_bits = type_size_in_bits;
15868
15869       decl_align_in_bits = simple_decl_align_in_bits (decl);
15870
15871       /* The GCC front-end doesn't make any attempt to keep track of the
15872          starting bit offset (relative to the start of the containing
15873          structure type) of the hypothetical "containing object" for a
15874          bit-field.  Thus, when computing the byte offset value for the
15875          start of the "containing object" of a bit-field, we must deduce
15876          this information on our own. This can be rather tricky to do in
15877          some cases.  For example, handling the following structure type
15878          definition when compiling for an i386/i486 target (which only
15879          aligns long long's to 32-bit boundaries) can be very tricky:
15880
15881          struct S { int field1; long long field2:31; };
15882
15883          Fortunately, there is a simple rule-of-thumb which can be used
15884          in such cases.  When compiling for an i386/i486, GCC will
15885          allocate 8 bytes for the structure shown above.  It decides to
15886          do this based upon one simple rule for bit-field allocation.
15887          GCC allocates each "containing object" for each bit-field at
15888          the first (i.e. lowest addressed) legitimate alignment boundary
15889          (based upon the required minimum alignment for the declared
15890          type of the field) which it can possibly use, subject to the
15891          condition that there is still enough available space remaining
15892          in the containing object (when allocated at the selected point)
15893          to fully accommodate all of the bits of the bit-field itself.
15894
15895          This simple rule makes it obvious why GCC allocates 8 bytes for
15896          each object of the structure type shown above.  When looking
15897          for a place to allocate the "containing object" for `field2',
15898          the compiler simply tries to allocate a 64-bit "containing
15899          object" at each successive 32-bit boundary (starting at zero)
15900          until it finds a place to allocate that 64- bit field such that
15901          at least 31 contiguous (and previously unallocated) bits remain
15902          within that selected 64 bit field.  (As it turns out, for the
15903          example above, the compiler finds it is OK to allocate the
15904          "containing object" 64-bit field at bit-offset zero within the
15905          structure type.)
15906
15907          Here we attempt to work backwards from the limited set of facts
15908          we're given, and we try to deduce from those facts, where GCC
15909          must have believed that the containing object started (within
15910          the structure type). The value we deduce is then used (by the
15911          callers of this routine) to generate DW_AT_location and
15912          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15913          the case of DW_AT_location, regular fields as well).  */
15914
15915       /* Figure out the bit-distance from the start of the structure to
15916          the "deepest" bit of the bit-field.  */
15917       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15918
15919       /* This is the tricky part.  Use some fancy footwork to deduce
15920          where the lowest addressed bit of the containing object must
15921          be.  */
15922       object_offset_in_bits
15923         = double_int_sub (deepest_bitpos, type_size_in_bits);
15924
15925       /* Round up to type_align by default.  This works best for
15926          bitfields.  */
15927       object_offset_in_bits
15928         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15929
15930       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15931         {
15932           object_offset_in_bits
15933             = double_int_sub (deepest_bitpos, type_size_in_bits);
15934
15935           /* Round up to decl_align instead.  */
15936           object_offset_in_bits
15937             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15938         }
15939     }
15940   else
15941 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15942     object_offset_in_bits = bitpos_int;
15943
15944   object_offset_in_bytes
15945     = double_int_div (object_offset_in_bits,
15946                       uhwi_to_double_int (BITS_PER_UNIT), true,
15947                       TRUNC_DIV_EXPR);
15948   return double_int_to_shwi (object_offset_in_bytes);
15949 }
15950 \f
15951 /* The following routines define various Dwarf attributes and any data
15952    associated with them.  */
15953
15954 /* Add a location description attribute value to a DIE.
15955
15956    This emits location attributes suitable for whole variables and
15957    whole parameters.  Note that the location attributes for struct fields are
15958    generated by the routine `data_member_location_attribute' below.  */
15959
15960 static inline void
15961 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15962                              dw_loc_list_ref descr)
15963 {
15964   if (descr == 0)
15965     return;
15966   if (single_element_loc_list_p (descr))
15967     add_AT_loc (die, attr_kind, descr->expr);
15968   else
15969     add_AT_loc_list (die, attr_kind, descr);
15970 }
15971
15972 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15973
15974 static void
15975 add_accessibility_attribute (dw_die_ref die, tree decl)
15976 {
15977   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15978      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15979      the default has always been DW_ACCESS_public.  */
15980   if (TREE_PROTECTED (decl))
15981     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15982   else if (TREE_PRIVATE (decl))
15983     {
15984       if (dwarf_version == 2
15985           || die->die_parent == NULL
15986           || die->die_parent->die_tag != DW_TAG_class_type)
15987         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15988     }
15989   else if (dwarf_version > 2
15990            && die->die_parent
15991            && die->die_parent->die_tag == DW_TAG_class_type)
15992     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15993 }
15994
15995 /* Attach the specialized form of location attribute used for data members of
15996    struct and union types.  In the special case of a FIELD_DECL node which
15997    represents a bit-field, the "offset" part of this special location
15998    descriptor must indicate the distance in bytes from the lowest-addressed
15999    byte of the containing struct or union type to the lowest-addressed byte of
16000    the "containing object" for the bit-field.  (See the `field_byte_offset'
16001    function above).
16002
16003    For any given bit-field, the "containing object" is a hypothetical object
16004    (of some integral or enum type) within which the given bit-field lives.  The
16005    type of this hypothetical "containing object" is always the same as the
16006    declared type of the individual bit-field itself (for GCC anyway... the
16007    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16008    bytes) of the hypothetical "containing object" which will be given in the
16009    DW_AT_byte_size attribute for this bit-field.  (See the
16010    `byte_size_attribute' function below.)  It is also used when calculating the
16011    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16012    function below.)  */
16013
16014 static void
16015 add_data_member_location_attribute (dw_die_ref die, tree decl)
16016 {
16017   HOST_WIDE_INT offset;
16018   dw_loc_descr_ref loc_descr = 0;
16019
16020   if (TREE_CODE (decl) == TREE_BINFO)
16021     {
16022       /* We're working on the TAG_inheritance for a base class.  */
16023       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16024         {
16025           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16026              aren't at a fixed offset from all (sub)objects of the same
16027              type.  We need to extract the appropriate offset from our
16028              vtable.  The following dwarf expression means
16029
16030                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16031
16032              This is specific to the V3 ABI, of course.  */
16033
16034           dw_loc_descr_ref tmp;
16035
16036           /* Make a copy of the object address.  */
16037           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16038           add_loc_descr (&loc_descr, tmp);
16039
16040           /* Extract the vtable address.  */
16041           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16042           add_loc_descr (&loc_descr, tmp);
16043
16044           /* Calculate the address of the offset.  */
16045           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16046           gcc_assert (offset < 0);
16047
16048           tmp = int_loc_descriptor (-offset);
16049           add_loc_descr (&loc_descr, tmp);
16050           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16051           add_loc_descr (&loc_descr, tmp);
16052
16053           /* Extract the offset.  */
16054           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16055           add_loc_descr (&loc_descr, tmp);
16056
16057           /* Add it to the object address.  */
16058           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16059           add_loc_descr (&loc_descr, tmp);
16060         }
16061       else
16062         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16063     }
16064   else
16065     offset = field_byte_offset (decl);
16066
16067   if (! loc_descr)
16068     {
16069       if (dwarf_version > 2)
16070         {
16071           /* Don't need to output a location expression, just the constant. */
16072           if (offset < 0)
16073             add_AT_int (die, DW_AT_data_member_location, offset);
16074           else
16075             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16076           return;
16077         }
16078       else
16079         {
16080           enum dwarf_location_atom op;
16081
16082           /* The DWARF2 standard says that we should assume that the structure
16083              address is already on the stack, so we can specify a structure
16084              field address by using DW_OP_plus_uconst.  */
16085
16086 #ifdef MIPS_DEBUGGING_INFO
16087           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16088              operator correctly.  It works only if we leave the offset on the
16089              stack.  */
16090           op = DW_OP_constu;
16091 #else
16092           op = DW_OP_plus_uconst;
16093 #endif
16094
16095           loc_descr = new_loc_descr (op, offset, 0);
16096         }
16097     }
16098
16099   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16100 }
16101
16102 /* Writes integer values to dw_vec_const array.  */
16103
16104 static void
16105 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16106 {
16107   while (size != 0)
16108     {
16109       *dest++ = val & 0xff;
16110       val >>= 8;
16111       --size;
16112     }
16113 }
16114
16115 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16116
16117 static HOST_WIDE_INT
16118 extract_int (const unsigned char *src, unsigned int size)
16119 {
16120   HOST_WIDE_INT val = 0;
16121
16122   src += size;
16123   while (size != 0)
16124     {
16125       val <<= 8;
16126       val |= *--src & 0xff;
16127       --size;
16128     }
16129   return val;
16130 }
16131
16132 /* Writes double_int values to dw_vec_const array.  */
16133
16134 static void
16135 insert_double (double_int val, unsigned char *dest)
16136 {
16137   unsigned char *p0 = dest;
16138   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16139
16140   if (WORDS_BIG_ENDIAN)
16141     {
16142       p0 = p1;
16143       p1 = dest;
16144     }
16145
16146   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16147   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16148 }
16149
16150 /* Writes floating point values to dw_vec_const array.  */
16151
16152 static void
16153 insert_float (const_rtx rtl, unsigned char *array)
16154 {
16155   REAL_VALUE_TYPE rv;
16156   long val[4];
16157   int i;
16158
16159   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16160   real_to_target (val, &rv, GET_MODE (rtl));
16161
16162   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16163   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16164     {
16165       insert_int (val[i], 4, array);
16166       array += 4;
16167     }
16168 }
16169
16170 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16171    does not have a "location" either in memory or in a register.  These
16172    things can arise in GNU C when a constant is passed as an actual parameter
16173    to an inlined function.  They can also arise in C++ where declared
16174    constants do not necessarily get memory "homes".  */
16175
16176 static bool
16177 add_const_value_attribute (dw_die_ref die, rtx rtl)
16178 {
16179   switch (GET_CODE (rtl))
16180     {
16181     case CONST_INT:
16182       {
16183         HOST_WIDE_INT val = INTVAL (rtl);
16184
16185         if (val < 0)
16186           add_AT_int (die, DW_AT_const_value, val);
16187         else
16188           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16189       }
16190       return true;
16191
16192     case CONST_DOUBLE:
16193       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16194          floating-point constant.  A CONST_DOUBLE is used whenever the
16195          constant requires more than one word in order to be adequately
16196          represented.  */
16197       {
16198         enum machine_mode mode = GET_MODE (rtl);
16199
16200         if (SCALAR_FLOAT_MODE_P (mode))
16201           {
16202             unsigned int length = GET_MODE_SIZE (mode);
16203             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16204
16205             insert_float (rtl, array);
16206             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16207           }
16208         else
16209           add_AT_double (die, DW_AT_const_value,
16210                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16211       }
16212       return true;
16213
16214     case CONST_VECTOR:
16215       {
16216         enum machine_mode mode = GET_MODE (rtl);
16217         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16218         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16219         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16220           (length * elt_size);
16221         unsigned int i;
16222         unsigned char *p;
16223
16224         switch (GET_MODE_CLASS (mode))
16225           {
16226           case MODE_VECTOR_INT:
16227             for (i = 0, p = array; i < length; i++, p += elt_size)
16228               {
16229                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16230                 double_int val = rtx_to_double_int (elt);
16231
16232                 if (elt_size <= sizeof (HOST_WIDE_INT))
16233                   insert_int (double_int_to_shwi (val), elt_size, p);
16234                 else
16235                   {
16236                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16237                     insert_double (val, p);
16238                   }
16239               }
16240             break;
16241
16242           case MODE_VECTOR_FLOAT:
16243             for (i = 0, p = array; i < length; i++, p += elt_size)
16244               {
16245                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16246                 insert_float (elt, p);
16247               }
16248             break;
16249
16250           default:
16251             gcc_unreachable ();
16252           }
16253
16254         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16255       }
16256       return true;
16257
16258     case CONST_STRING:
16259       if (dwarf_version >= 4 || !dwarf_strict)
16260         {
16261           dw_loc_descr_ref loc_result;
16262           resolve_one_addr (&rtl, NULL);
16263         rtl_addr:
16264           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16265           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16266           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16267           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16268           add_AT_loc (die, DW_AT_location, loc_result);
16269           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16270           return true;
16271         }
16272       return false;
16273
16274     case CONST:
16275       if (CONSTANT_P (XEXP (rtl, 0)))
16276         return add_const_value_attribute (die, XEXP (rtl, 0));
16277       /* FALLTHROUGH */
16278     case SYMBOL_REF:
16279       if (!const_ok_for_output (rtl))
16280         return false;
16281     case LABEL_REF:
16282       if (dwarf_version >= 4 || !dwarf_strict)
16283         goto rtl_addr;
16284       return false;
16285
16286     case PLUS:
16287       /* In cases where an inlined instance of an inline function is passed
16288          the address of an `auto' variable (which is local to the caller) we
16289          can get a situation where the DECL_RTL of the artificial local
16290          variable (for the inlining) which acts as a stand-in for the
16291          corresponding formal parameter (of the inline function) will look
16292          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16293          exactly a compile-time constant expression, but it isn't the address
16294          of the (artificial) local variable either.  Rather, it represents the
16295          *value* which the artificial local variable always has during its
16296          lifetime.  We currently have no way to represent such quasi-constant
16297          values in Dwarf, so for now we just punt and generate nothing.  */
16298       return false;
16299
16300     case HIGH:
16301     case CONST_FIXED:
16302       return false;
16303
16304     case MEM:
16305       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16306           && MEM_READONLY_P (rtl)
16307           && GET_MODE (rtl) == BLKmode)
16308         {
16309           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16310           return true;
16311         }
16312       return false;
16313
16314     default:
16315       /* No other kinds of rtx should be possible here.  */
16316       gcc_unreachable ();
16317     }
16318   return false;
16319 }
16320
16321 /* Determine whether the evaluation of EXPR references any variables
16322    or functions which aren't otherwise used (and therefore may not be
16323    output).  */
16324 static tree
16325 reference_to_unused (tree * tp, int * walk_subtrees,
16326                      void * data ATTRIBUTE_UNUSED)
16327 {
16328   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16329     *walk_subtrees = 0;
16330
16331   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16332       && ! TREE_ASM_WRITTEN (*tp))
16333     return *tp;
16334   /* ???  The C++ FE emits debug information for using decls, so
16335      putting gcc_unreachable here falls over.  See PR31899.  For now
16336      be conservative.  */
16337   else if (!cgraph_global_info_ready
16338            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16339     return *tp;
16340   else if (TREE_CODE (*tp) == VAR_DECL)
16341     {
16342       struct varpool_node *node = varpool_get_node (*tp);
16343       if (!node || !node->needed)
16344         return *tp;
16345     }
16346   else if (TREE_CODE (*tp) == FUNCTION_DECL
16347            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16348     {
16349       /* The call graph machinery must have finished analyzing,
16350          optimizing and gimplifying the CU by now.
16351          So if *TP has no call graph node associated
16352          to it, it means *TP will not be emitted.  */
16353       if (!cgraph_get_node (*tp))
16354         return *tp;
16355     }
16356   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16357     return *tp;
16358
16359   return NULL_TREE;
16360 }
16361
16362 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16363    for use in a later add_const_value_attribute call.  */
16364
16365 static rtx
16366 rtl_for_decl_init (tree init, tree type)
16367 {
16368   rtx rtl = NULL_RTX;
16369
16370   /* If a variable is initialized with a string constant without embedded
16371      zeros, build CONST_STRING.  */
16372   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16373     {
16374       tree enttype = TREE_TYPE (type);
16375       tree domain = TYPE_DOMAIN (type);
16376       enum machine_mode mode = TYPE_MODE (enttype);
16377
16378       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16379           && domain
16380           && integer_zerop (TYPE_MIN_VALUE (domain))
16381           && compare_tree_int (TYPE_MAX_VALUE (domain),
16382                                TREE_STRING_LENGTH (init) - 1) == 0
16383           && ((size_t) TREE_STRING_LENGTH (init)
16384               == strlen (TREE_STRING_POINTER (init)) + 1))
16385         {
16386           rtl = gen_rtx_CONST_STRING (VOIDmode,
16387                                       ggc_strdup (TREE_STRING_POINTER (init)));
16388           rtl = gen_rtx_MEM (BLKmode, rtl);
16389           MEM_READONLY_P (rtl) = 1;
16390         }
16391     }
16392   /* Other aggregates, and complex values, could be represented using
16393      CONCAT: FIXME!  */
16394   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16395     ;
16396   /* Vectors only work if their mode is supported by the target.
16397      FIXME: generic vectors ought to work too.  */
16398   else if (TREE_CODE (type) == VECTOR_TYPE
16399            && !VECTOR_MODE_P (TYPE_MODE (type)))
16400     ;
16401   /* If the initializer is something that we know will expand into an
16402      immediate RTL constant, expand it now.  We must be careful not to
16403      reference variables which won't be output.  */
16404   else if (initializer_constant_valid_p (init, type)
16405            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16406     {
16407       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16408          possible.  */
16409       if (TREE_CODE (type) == VECTOR_TYPE)
16410         switch (TREE_CODE (init))
16411           {
16412           case VECTOR_CST:
16413             break;
16414           case CONSTRUCTOR:
16415             if (TREE_CONSTANT (init))
16416               {
16417                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16418                 bool constant_p = true;
16419                 tree value;
16420                 unsigned HOST_WIDE_INT ix;
16421
16422                 /* Even when ctor is constant, it might contain non-*_CST
16423                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16424                    belong into VECTOR_CST nodes.  */
16425                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16426                   if (!CONSTANT_CLASS_P (value))
16427                     {
16428                       constant_p = false;
16429                       break;
16430                     }
16431
16432                 if (constant_p)
16433                   {
16434                     init = build_vector_from_ctor (type, elts);
16435                     break;
16436                   }
16437               }
16438             /* FALLTHRU */
16439
16440           default:
16441             return NULL;
16442           }
16443
16444       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16445
16446       /* If expand_expr returns a MEM, it wasn't immediate.  */
16447       gcc_assert (!rtl || !MEM_P (rtl));
16448     }
16449
16450   return rtl;
16451 }
16452
16453 /* Generate RTL for the variable DECL to represent its location.  */
16454
16455 static rtx
16456 rtl_for_decl_location (tree decl)
16457 {
16458   rtx rtl;
16459
16460   /* Here we have to decide where we are going to say the parameter "lives"
16461      (as far as the debugger is concerned).  We only have a couple of
16462      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16463
16464      DECL_RTL normally indicates where the parameter lives during most of the
16465      activation of the function.  If optimization is enabled however, this
16466      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16467      that the parameter doesn't really live anywhere (as far as the code
16468      generation parts of GCC are concerned) during most of the function's
16469      activation.  That will happen (for example) if the parameter is never
16470      referenced within the function.
16471
16472      We could just generate a location descriptor here for all non-NULL
16473      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16474      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16475      where DECL_RTL is NULL or is a pseudo-reg.
16476
16477      Note however that we can only get away with using DECL_INCOMING_RTL as
16478      a backup substitute for DECL_RTL in certain limited cases.  In cases
16479      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16480      we can be sure that the parameter was passed using the same type as it is
16481      declared to have within the function, and that its DECL_INCOMING_RTL
16482      points us to a place where a value of that type is passed.
16483
16484      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16485      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16486      because in these cases DECL_INCOMING_RTL points us to a value of some
16487      type which is *different* from the type of the parameter itself.  Thus,
16488      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16489      such cases, the debugger would end up (for example) trying to fetch a
16490      `float' from a place which actually contains the first part of a
16491      `double'.  That would lead to really incorrect and confusing
16492      output at debug-time.
16493
16494      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16495      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16496      are a couple of exceptions however.  On little-endian machines we can
16497      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16498      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16499      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16500      when (on a little-endian machine) a non-prototyped function has a
16501      parameter declared to be of type `short' or `char'.  In such cases,
16502      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16503      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16504      passed `int' value.  If the debugger then uses that address to fetch
16505      a `short' or a `char' (on a little-endian machine) the result will be
16506      the correct data, so we allow for such exceptional cases below.
16507
16508      Note that our goal here is to describe the place where the given formal
16509      parameter lives during most of the function's activation (i.e. between the
16510      end of the prologue and the start of the epilogue).  We'll do that as best
16511      as we can. Note however that if the given formal parameter is modified
16512      sometime during the execution of the function, then a stack backtrace (at
16513      debug-time) will show the function as having been called with the *new*
16514      value rather than the value which was originally passed in.  This happens
16515      rarely enough that it is not a major problem, but it *is* a problem, and
16516      I'd like to fix it.
16517
16518      A future version of dwarf2out.c may generate two additional attributes for
16519      any given DW_TAG_formal_parameter DIE which will describe the "passed
16520      type" and the "passed location" for the given formal parameter in addition
16521      to the attributes we now generate to indicate the "declared type" and the
16522      "active location" for each parameter.  This additional set of attributes
16523      could be used by debuggers for stack backtraces. Separately, note that
16524      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16525      This happens (for example) for inlined-instances of inline function formal
16526      parameters which are never referenced.  This really shouldn't be
16527      happening.  All PARM_DECL nodes should get valid non-NULL
16528      DECL_INCOMING_RTL values.  FIXME.  */
16529
16530   /* Use DECL_RTL as the "location" unless we find something better.  */
16531   rtl = DECL_RTL_IF_SET (decl);
16532
16533   /* When generating abstract instances, ignore everything except
16534      constants, symbols living in memory, and symbols living in
16535      fixed registers.  */
16536   if (! reload_completed)
16537     {
16538       if (rtl
16539           && (CONSTANT_P (rtl)
16540               || (MEM_P (rtl)
16541                   && CONSTANT_P (XEXP (rtl, 0)))
16542               || (REG_P (rtl)
16543                   && TREE_CODE (decl) == VAR_DECL
16544                   && TREE_STATIC (decl))))
16545         {
16546           rtl = targetm.delegitimize_address (rtl);
16547           return rtl;
16548         }
16549       rtl = NULL_RTX;
16550     }
16551   else if (TREE_CODE (decl) == PARM_DECL)
16552     {
16553       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16554         {
16555           tree declared_type = TREE_TYPE (decl);
16556           tree passed_type = DECL_ARG_TYPE (decl);
16557           enum machine_mode dmode = TYPE_MODE (declared_type);
16558           enum machine_mode pmode = TYPE_MODE (passed_type);
16559
16560           /* This decl represents a formal parameter which was optimized out.
16561              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16562              all cases where (rtl == NULL_RTX) just below.  */
16563           if (dmode == pmode)
16564             rtl = DECL_INCOMING_RTL (decl);
16565           else if (SCALAR_INT_MODE_P (dmode)
16566                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16567                    && DECL_INCOMING_RTL (decl))
16568             {
16569               rtx inc = DECL_INCOMING_RTL (decl);
16570               if (REG_P (inc))
16571                 rtl = inc;
16572               else if (MEM_P (inc))
16573                 {
16574                   if (BYTES_BIG_ENDIAN)
16575                     rtl = adjust_address_nv (inc, dmode,
16576                                              GET_MODE_SIZE (pmode)
16577                                              - GET_MODE_SIZE (dmode));
16578                   else
16579                     rtl = inc;
16580                 }
16581             }
16582         }
16583
16584       /* If the parm was passed in registers, but lives on the stack, then
16585          make a big endian correction if the mode of the type of the
16586          parameter is not the same as the mode of the rtl.  */
16587       /* ??? This is the same series of checks that are made in dbxout.c before
16588          we reach the big endian correction code there.  It isn't clear if all
16589          of these checks are necessary here, but keeping them all is the safe
16590          thing to do.  */
16591       else if (MEM_P (rtl)
16592                && XEXP (rtl, 0) != const0_rtx
16593                && ! CONSTANT_P (XEXP (rtl, 0))
16594                /* Not passed in memory.  */
16595                && !MEM_P (DECL_INCOMING_RTL (decl))
16596                /* Not passed by invisible reference.  */
16597                && (!REG_P (XEXP (rtl, 0))
16598                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16599                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16600 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16601                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16602 #endif
16603                      )
16604                /* Big endian correction check.  */
16605                && BYTES_BIG_ENDIAN
16606                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16607                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16608                    < UNITS_PER_WORD))
16609         {
16610           int offset = (UNITS_PER_WORD
16611                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16612
16613           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16614                              plus_constant (XEXP (rtl, 0), offset));
16615         }
16616     }
16617   else if (TREE_CODE (decl) == VAR_DECL
16618            && rtl
16619            && MEM_P (rtl)
16620            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16621            && BYTES_BIG_ENDIAN)
16622     {
16623       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16624       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16625
16626       /* If a variable is declared "register" yet is smaller than
16627          a register, then if we store the variable to memory, it
16628          looks like we're storing a register-sized value, when in
16629          fact we are not.  We need to adjust the offset of the
16630          storage location to reflect the actual value's bytes,
16631          else gdb will not be able to display it.  */
16632       if (rsize > dsize)
16633         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16634                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16635     }
16636
16637   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16638      and will have been substituted directly into all expressions that use it.
16639      C does not have such a concept, but C++ and other languages do.  */
16640   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16641     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16642
16643   if (rtl)
16644     rtl = targetm.delegitimize_address (rtl);
16645
16646   /* If we don't look past the constant pool, we risk emitting a
16647      reference to a constant pool entry that isn't referenced from
16648      code, and thus is not emitted.  */
16649   if (rtl)
16650     rtl = avoid_constant_pool_reference (rtl);
16651
16652   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16653      in the current CU, resolve_addr will remove the expression referencing
16654      it.  */
16655   if (rtl == NULL_RTX
16656       && TREE_CODE (decl) == VAR_DECL
16657       && !DECL_EXTERNAL (decl)
16658       && TREE_STATIC (decl)
16659       && DECL_NAME (decl)
16660       && !DECL_HARD_REGISTER (decl)
16661       && DECL_MODE (decl) != VOIDmode)
16662     {
16663       rtl = make_decl_rtl_for_debug (decl);
16664       if (!MEM_P (rtl)
16665           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16666           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16667         rtl = NULL_RTX;
16668     }
16669
16670   return rtl;
16671 }
16672
16673 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16674    returned.  If so, the decl for the COMMON block is returned, and the
16675    value is the offset into the common block for the symbol.  */
16676
16677 static tree
16678 fortran_common (tree decl, HOST_WIDE_INT *value)
16679 {
16680   tree val_expr, cvar;
16681   enum machine_mode mode;
16682   HOST_WIDE_INT bitsize, bitpos;
16683   tree offset;
16684   int volatilep = 0, unsignedp = 0;
16685
16686   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16687      it does not have a value (the offset into the common area), or if it
16688      is thread local (as opposed to global) then it isn't common, and shouldn't
16689      be handled as such.  */
16690   if (TREE_CODE (decl) != VAR_DECL
16691       || !TREE_STATIC (decl)
16692       || !DECL_HAS_VALUE_EXPR_P (decl)
16693       || !is_fortran ())
16694     return NULL_TREE;
16695
16696   val_expr = DECL_VALUE_EXPR (decl);
16697   if (TREE_CODE (val_expr) != COMPONENT_REF)
16698     return NULL_TREE;
16699
16700   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16701                               &mode, &unsignedp, &volatilep, true);
16702
16703   if (cvar == NULL_TREE
16704       || TREE_CODE (cvar) != VAR_DECL
16705       || DECL_ARTIFICIAL (cvar)
16706       || !TREE_PUBLIC (cvar))
16707     return NULL_TREE;
16708
16709   *value = 0;
16710   if (offset != NULL)
16711     {
16712       if (!host_integerp (offset, 0))
16713         return NULL_TREE;
16714       *value = tree_low_cst (offset, 0);
16715     }
16716   if (bitpos != 0)
16717     *value += bitpos / BITS_PER_UNIT;
16718
16719   return cvar;
16720 }
16721
16722 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16723    data attribute for a variable or a parameter.  We generate the
16724    DW_AT_const_value attribute only in those cases where the given variable
16725    or parameter does not have a true "location" either in memory or in a
16726    register.  This can happen (for example) when a constant is passed as an
16727    actual argument in a call to an inline function.  (It's possible that
16728    these things can crop up in other ways also.)  Note that one type of
16729    constant value which can be passed into an inlined function is a constant
16730    pointer.  This can happen for example if an actual argument in an inlined
16731    function call evaluates to a compile-time constant address.  */
16732
16733 static bool
16734 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16735                                        enum dwarf_attribute attr)
16736 {
16737   rtx rtl;
16738   dw_loc_list_ref list;
16739   var_loc_list *loc_list;
16740
16741   if (TREE_CODE (decl) == ERROR_MARK)
16742     return false;
16743
16744   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16745               || TREE_CODE (decl) == RESULT_DECL);
16746
16747   /* Try to get some constant RTL for this decl, and use that as the value of
16748      the location.  */
16749
16750   rtl = rtl_for_decl_location (decl);
16751   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16752       && add_const_value_attribute (die, rtl))
16753     return true;
16754
16755   /* See if we have single element location list that is equivalent to
16756      a constant value.  That way we are better to use add_const_value_attribute
16757      rather than expanding constant value equivalent.  */
16758   loc_list = lookup_decl_loc (decl);
16759   if (loc_list
16760       && loc_list->first
16761       && loc_list->first->next == NULL
16762       && NOTE_P (loc_list->first->loc)
16763       && NOTE_VAR_LOCATION (loc_list->first->loc)
16764       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16765     {
16766       struct var_loc_node *node;
16767
16768       node = loc_list->first;
16769       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16770       if (GET_CODE (rtl) == EXPR_LIST)
16771         rtl = XEXP (rtl, 0);
16772       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16773           && add_const_value_attribute (die, rtl))
16774          return true;
16775     }
16776   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16777   if (list)
16778     {
16779       add_AT_location_description (die, attr, list);
16780       return true;
16781     }
16782   /* None of that worked, so it must not really have a location;
16783      try adding a constant value attribute from the DECL_INITIAL.  */
16784   return tree_add_const_value_attribute_for_decl (die, decl);
16785 }
16786
16787 /* Add VARIABLE and DIE into deferred locations list.  */
16788
16789 static void
16790 defer_location (tree variable, dw_die_ref die)
16791 {
16792   deferred_locations entry;
16793   entry.variable = variable;
16794   entry.die = die;
16795   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16796 }
16797
16798 /* Helper function for tree_add_const_value_attribute.  Natively encode
16799    initializer INIT into an array.  Return true if successful.  */
16800
16801 static bool
16802 native_encode_initializer (tree init, unsigned char *array, int size)
16803 {
16804   tree type;
16805
16806   if (init == NULL_TREE)
16807     return false;
16808
16809   STRIP_NOPS (init);
16810   switch (TREE_CODE (init))
16811     {
16812     case STRING_CST:
16813       type = TREE_TYPE (init);
16814       if (TREE_CODE (type) == ARRAY_TYPE)
16815         {
16816           tree enttype = TREE_TYPE (type);
16817           enum machine_mode mode = TYPE_MODE (enttype);
16818
16819           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16820             return false;
16821           if (int_size_in_bytes (type) != size)
16822             return false;
16823           if (size > TREE_STRING_LENGTH (init))
16824             {
16825               memcpy (array, TREE_STRING_POINTER (init),
16826                       TREE_STRING_LENGTH (init));
16827               memset (array + TREE_STRING_LENGTH (init),
16828                       '\0', size - TREE_STRING_LENGTH (init));
16829             }
16830           else
16831             memcpy (array, TREE_STRING_POINTER (init), size);
16832           return true;
16833         }
16834       return false;
16835     case CONSTRUCTOR:
16836       type = TREE_TYPE (init);
16837       if (int_size_in_bytes (type) != size)
16838         return false;
16839       if (TREE_CODE (type) == ARRAY_TYPE)
16840         {
16841           HOST_WIDE_INT min_index;
16842           unsigned HOST_WIDE_INT cnt;
16843           int curpos = 0, fieldsize;
16844           constructor_elt *ce;
16845
16846           if (TYPE_DOMAIN (type) == NULL_TREE
16847               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16848             return false;
16849
16850           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16851           if (fieldsize <= 0)
16852             return false;
16853
16854           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16855           memset (array, '\0', size);
16856           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16857             {
16858               tree val = ce->value;
16859               tree index = ce->index;
16860               int pos = curpos;
16861               if (index && TREE_CODE (index) == RANGE_EXPR)
16862                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16863                       * fieldsize;
16864               else if (index)
16865                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16866
16867               if (val)
16868                 {
16869                   STRIP_NOPS (val);
16870                   if (!native_encode_initializer (val, array + pos, fieldsize))
16871                     return false;
16872                 }
16873               curpos = pos + fieldsize;
16874               if (index && TREE_CODE (index) == RANGE_EXPR)
16875                 {
16876                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16877                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16878                   while (count > 0)
16879                     {
16880                       if (val)
16881                         memcpy (array + curpos, array + pos, fieldsize);
16882                       curpos += fieldsize;
16883                     }
16884                 }
16885               gcc_assert (curpos <= size);
16886             }
16887           return true;
16888         }
16889       else if (TREE_CODE (type) == RECORD_TYPE
16890                || TREE_CODE (type) == UNION_TYPE)
16891         {
16892           tree field = NULL_TREE;
16893           unsigned HOST_WIDE_INT cnt;
16894           constructor_elt *ce;
16895
16896           if (int_size_in_bytes (type) != size)
16897             return false;
16898
16899           if (TREE_CODE (type) == RECORD_TYPE)
16900             field = TYPE_FIELDS (type);
16901
16902           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16903             {
16904               tree val = ce->value;
16905               int pos, fieldsize;
16906
16907               if (ce->index != 0)
16908                 field = ce->index;
16909
16910               if (val)
16911                 STRIP_NOPS (val);
16912
16913               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16914                 return false;
16915
16916               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16917                   && TYPE_DOMAIN (TREE_TYPE (field))
16918                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16919                 return false;
16920               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16921                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16922                 return false;
16923               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16924               pos = int_byte_position (field);
16925               gcc_assert (pos + fieldsize <= size);
16926               if (val
16927                   && !native_encode_initializer (val, array + pos, fieldsize))
16928                 return false;
16929             }
16930           return true;
16931         }
16932       return false;
16933     case VIEW_CONVERT_EXPR:
16934     case NON_LVALUE_EXPR:
16935       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16936     default:
16937       return native_encode_expr (init, array, size) == size;
16938     }
16939 }
16940
16941 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16942    attribute is the const value T.  */
16943
16944 static bool
16945 tree_add_const_value_attribute (dw_die_ref die, tree t)
16946 {
16947   tree init;
16948   tree type = TREE_TYPE (t);
16949   rtx rtl;
16950
16951   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16952     return false;
16953
16954   init = t;
16955   gcc_assert (!DECL_P (init));
16956
16957   rtl = rtl_for_decl_init (init, type);
16958   if (rtl)
16959     return add_const_value_attribute (die, rtl);
16960   /* If the host and target are sane, try harder.  */
16961   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16962            && initializer_constant_valid_p (init, type))
16963     {
16964       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16965       if (size > 0 && (int) size == size)
16966         {
16967           unsigned char *array = (unsigned char *)
16968             ggc_alloc_cleared_atomic (size);
16969
16970           if (native_encode_initializer (init, array, size))
16971             {
16972               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16973               return true;
16974             }
16975         }
16976     }
16977   return false;
16978 }
16979
16980 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16981    attribute is the const value of T, where T is an integral constant
16982    variable with static storage duration
16983    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16984
16985 static bool
16986 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16987 {
16988
16989   if (!decl
16990       || (TREE_CODE (decl) != VAR_DECL
16991           && TREE_CODE (decl) != CONST_DECL))
16992     return false;
16993
16994     if (TREE_READONLY (decl)
16995         && ! TREE_THIS_VOLATILE (decl)
16996         && DECL_INITIAL (decl))
16997       /* OK */;
16998     else
16999       return false;
17000
17001   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17002   if (get_AT (var_die, DW_AT_const_value))
17003     return false;
17004
17005   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17006 }
17007
17008 /* Convert the CFI instructions for the current function into a
17009    location list.  This is used for DW_AT_frame_base when we targeting
17010    a dwarf2 consumer that does not support the dwarf3
17011    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17012    expressions.  */
17013
17014 static dw_loc_list_ref
17015 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17016 {
17017   dw_fde_ref fde;
17018   dw_loc_list_ref list, *list_tail;
17019   dw_cfi_ref cfi;
17020   dw_cfa_location last_cfa, next_cfa;
17021   const char *start_label, *last_label, *section;
17022   dw_cfa_location remember;
17023
17024   fde = current_fde ();
17025   gcc_assert (fde != NULL);
17026
17027   section = secname_for_decl (current_function_decl);
17028   list_tail = &list;
17029   list = NULL;
17030
17031   memset (&next_cfa, 0, sizeof (next_cfa));
17032   next_cfa.reg = INVALID_REGNUM;
17033   remember = next_cfa;
17034
17035   start_label = fde->dw_fde_begin;
17036
17037   /* ??? Bald assumption that the CIE opcode list does not contain
17038      advance opcodes.  */
17039   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17040     lookup_cfa_1 (cfi, &next_cfa, &remember);
17041
17042   last_cfa = next_cfa;
17043   last_label = start_label;
17044
17045   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17046     switch (cfi->dw_cfi_opc)
17047       {
17048       case DW_CFA_set_loc:
17049       case DW_CFA_advance_loc1:
17050       case DW_CFA_advance_loc2:
17051       case DW_CFA_advance_loc4:
17052         if (!cfa_equal_p (&last_cfa, &next_cfa))
17053           {
17054             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17055                                        start_label, last_label, section);
17056
17057             list_tail = &(*list_tail)->dw_loc_next;
17058             last_cfa = next_cfa;
17059             start_label = last_label;
17060           }
17061         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17062         break;
17063
17064       case DW_CFA_advance_loc:
17065         /* The encoding is complex enough that we should never emit this.  */
17066         gcc_unreachable ();
17067
17068       default:
17069         lookup_cfa_1 (cfi, &next_cfa, &remember);
17070         break;
17071       }
17072
17073   if (!cfa_equal_p (&last_cfa, &next_cfa))
17074     {
17075       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17076                                  start_label, last_label, section);
17077       list_tail = &(*list_tail)->dw_loc_next;
17078       start_label = last_label;
17079     }
17080
17081   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17082                              start_label, fde->dw_fde_end, section);
17083
17084   if (list && list->dw_loc_next)
17085     gen_llsym (list);
17086
17087   return list;
17088 }
17089
17090 /* Compute a displacement from the "steady-state frame pointer" to the
17091    frame base (often the same as the CFA), and store it in
17092    frame_pointer_fb_offset.  OFFSET is added to the displacement
17093    before the latter is negated.  */
17094
17095 static void
17096 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17097 {
17098   rtx reg, elim;
17099
17100 #ifdef FRAME_POINTER_CFA_OFFSET
17101   reg = frame_pointer_rtx;
17102   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17103 #else
17104   reg = arg_pointer_rtx;
17105   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17106 #endif
17107
17108   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17109   if (GET_CODE (elim) == PLUS)
17110     {
17111       offset += INTVAL (XEXP (elim, 1));
17112       elim = XEXP (elim, 0);
17113     }
17114
17115   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17116                && (elim == hard_frame_pointer_rtx
17117                    || elim == stack_pointer_rtx))
17118               || elim == (frame_pointer_needed
17119                           ? hard_frame_pointer_rtx
17120                           : stack_pointer_rtx));
17121
17122   frame_pointer_fb_offset = -offset;
17123 }
17124
17125 /* Generate a DW_AT_name attribute given some string value to be included as
17126    the value of the attribute.  */
17127
17128 static void
17129 add_name_attribute (dw_die_ref die, const char *name_string)
17130 {
17131   if (name_string != NULL && *name_string != 0)
17132     {
17133       if (demangle_name_func)
17134         name_string = (*demangle_name_func) (name_string);
17135
17136       add_AT_string (die, DW_AT_name, name_string);
17137     }
17138 }
17139
17140 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17141
17142 static void
17143 add_comp_dir_attribute (dw_die_ref die)
17144 {
17145   const char *wd = get_src_pwd ();
17146   char *wd1;
17147
17148   if (wd == NULL)
17149     return;
17150
17151   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17152     {
17153       int wdlen;
17154
17155       wdlen = strlen (wd);
17156       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17157       strcpy (wd1, wd);
17158       wd1 [wdlen] = DIR_SEPARATOR;
17159       wd1 [wdlen + 1] = 0;
17160       wd = wd1;
17161     }
17162
17163     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17164 }
17165
17166 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17167    default.  */
17168
17169 static int
17170 lower_bound_default (void)
17171 {
17172   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17173     {
17174     case DW_LANG_C:
17175     case DW_LANG_C89:
17176     case DW_LANG_C99:
17177     case DW_LANG_C_plus_plus:
17178     case DW_LANG_ObjC:
17179     case DW_LANG_ObjC_plus_plus:
17180     case DW_LANG_Java:
17181       return 0;
17182     case DW_LANG_Fortran77:
17183     case DW_LANG_Fortran90:
17184     case DW_LANG_Fortran95:
17185       return 1;
17186     case DW_LANG_UPC:
17187     case DW_LANG_D:
17188     case DW_LANG_Python:
17189       return dwarf_version >= 4 ? 0 : -1;
17190     case DW_LANG_Ada95:
17191     case DW_LANG_Ada83:
17192     case DW_LANG_Cobol74:
17193     case DW_LANG_Cobol85:
17194     case DW_LANG_Pascal83:
17195     case DW_LANG_Modula2:
17196     case DW_LANG_PLI:
17197       return dwarf_version >= 4 ? 1 : -1;
17198     default:
17199       return -1;
17200     }
17201 }
17202
17203 /* Given a tree node describing an array bound (either lower or upper) output
17204    a representation for that bound.  */
17205
17206 static void
17207 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17208 {
17209   switch (TREE_CODE (bound))
17210     {
17211     case ERROR_MARK:
17212       return;
17213
17214     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17215     case INTEGER_CST:
17216       {
17217         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17218         int dflt;
17219
17220         /* Use the default if possible.  */
17221         if (bound_attr == DW_AT_lower_bound
17222             && host_integerp (bound, 0)
17223             && (dflt = lower_bound_default ()) != -1
17224             && tree_low_cst (bound, 0) == dflt)
17225           ;
17226
17227         /* Otherwise represent the bound as an unsigned value with the
17228            precision of its type.  The precision and signedness of the
17229            type will be necessary to re-interpret it unambiguously.  */
17230         else if (prec < HOST_BITS_PER_WIDE_INT)
17231           {
17232             unsigned HOST_WIDE_INT mask
17233               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17234             add_AT_unsigned (subrange_die, bound_attr,
17235                              TREE_INT_CST_LOW (bound) & mask);
17236           }
17237         else if (prec == HOST_BITS_PER_WIDE_INT
17238                  || TREE_INT_CST_HIGH (bound) == 0)
17239           add_AT_unsigned (subrange_die, bound_attr,
17240                            TREE_INT_CST_LOW (bound));
17241         else
17242           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17243                          TREE_INT_CST_LOW (bound));
17244       }
17245       break;
17246
17247     CASE_CONVERT:
17248     case VIEW_CONVERT_EXPR:
17249       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17250       break;
17251
17252     case SAVE_EXPR:
17253       break;
17254
17255     case VAR_DECL:
17256     case PARM_DECL:
17257     case RESULT_DECL:
17258       {
17259         dw_die_ref decl_die = lookup_decl_die (bound);
17260
17261         /* ??? Can this happen, or should the variable have been bound
17262            first?  Probably it can, since I imagine that we try to create
17263            the types of parameters in the order in which they exist in
17264            the list, and won't have created a forward reference to a
17265            later parameter.  */
17266         if (decl_die != NULL)
17267           {
17268             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17269             break;
17270           }
17271       }
17272       /* FALLTHRU */
17273
17274     default:
17275       {
17276         /* Otherwise try to create a stack operation procedure to
17277            evaluate the value of the array bound.  */
17278
17279         dw_die_ref ctx, decl_die;
17280         dw_loc_list_ref list;
17281
17282         list = loc_list_from_tree (bound, 2);
17283         if (list == NULL || single_element_loc_list_p (list))
17284           {
17285             /* If DW_AT_*bound is not a reference nor constant, it is
17286                a DWARF expression rather than location description.
17287                For that loc_list_from_tree (bound, 0) is needed.
17288                If that fails to give a single element list,
17289                fall back to outputting this as a reference anyway.  */
17290             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17291             if (list2 && single_element_loc_list_p (list2))
17292               {
17293                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17294                 break;
17295               }
17296           }
17297         if (list == NULL)
17298           break;
17299
17300         if (current_function_decl == 0)
17301           ctx = comp_unit_die ();
17302         else
17303           ctx = lookup_decl_die (current_function_decl);
17304
17305         decl_die = new_die (DW_TAG_variable, ctx, bound);
17306         add_AT_flag (decl_die, DW_AT_artificial, 1);
17307         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17308         add_AT_location_description (decl_die, DW_AT_location, list);
17309         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17310         break;
17311       }
17312     }
17313 }
17314
17315 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17316    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17317    Note that the block of subscript information for an array type also
17318    includes information about the element type of the given array type.  */
17319
17320 static void
17321 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17322 {
17323   unsigned dimension_number;
17324   tree lower, upper;
17325   dw_die_ref subrange_die;
17326
17327   for (dimension_number = 0;
17328        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17329        type = TREE_TYPE (type), dimension_number++)
17330     {
17331       tree domain = TYPE_DOMAIN (type);
17332
17333       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17334         break;
17335
17336       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17337          and (in GNU C only) variable bounds.  Handle all three forms
17338          here.  */
17339       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17340       if (domain)
17341         {
17342           /* We have an array type with specified bounds.  */
17343           lower = TYPE_MIN_VALUE (domain);
17344           upper = TYPE_MAX_VALUE (domain);
17345
17346           /* Define the index type.  */
17347           if (TREE_TYPE (domain))
17348             {
17349               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17350                  TREE_TYPE field.  We can't emit debug info for this
17351                  because it is an unnamed integral type.  */
17352               if (TREE_CODE (domain) == INTEGER_TYPE
17353                   && TYPE_NAME (domain) == NULL_TREE
17354                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17355                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17356                 ;
17357               else
17358                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17359                                     type_die);
17360             }
17361
17362           /* ??? If upper is NULL, the array has unspecified length,
17363              but it does have a lower bound.  This happens with Fortran
17364                dimension arr(N:*)
17365              Since the debugger is definitely going to need to know N
17366              to produce useful results, go ahead and output the lower
17367              bound solo, and hope the debugger can cope.  */
17368
17369           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17370           if (upper)
17371             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17372         }
17373
17374       /* Otherwise we have an array type with an unspecified length.  The
17375          DWARF-2 spec does not say how to handle this; let's just leave out the
17376          bounds.  */
17377     }
17378 }
17379
17380 static void
17381 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17382 {
17383   unsigned size;
17384
17385   switch (TREE_CODE (tree_node))
17386     {
17387     case ERROR_MARK:
17388       size = 0;
17389       break;
17390     case ENUMERAL_TYPE:
17391     case RECORD_TYPE:
17392     case UNION_TYPE:
17393     case QUAL_UNION_TYPE:
17394       size = int_size_in_bytes (tree_node);
17395       break;
17396     case FIELD_DECL:
17397       /* For a data member of a struct or union, the DW_AT_byte_size is
17398          generally given as the number of bytes normally allocated for an
17399          object of the *declared* type of the member itself.  This is true
17400          even for bit-fields.  */
17401       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17402       break;
17403     default:
17404       gcc_unreachable ();
17405     }
17406
17407   /* Note that `size' might be -1 when we get to this point.  If it is, that
17408      indicates that the byte size of the entity in question is variable.  We
17409      have no good way of expressing this fact in Dwarf at the present time,
17410      so just let the -1 pass on through.  */
17411   add_AT_unsigned (die, DW_AT_byte_size, size);
17412 }
17413
17414 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17415    which specifies the distance in bits from the highest order bit of the
17416    "containing object" for the bit-field to the highest order bit of the
17417    bit-field itself.
17418
17419    For any given bit-field, the "containing object" is a hypothetical object
17420    (of some integral or enum type) within which the given bit-field lives.  The
17421    type of this hypothetical "containing object" is always the same as the
17422    declared type of the individual bit-field itself.  The determination of the
17423    exact location of the "containing object" for a bit-field is rather
17424    complicated.  It's handled by the `field_byte_offset' function (above).
17425
17426    Note that it is the size (in bytes) of the hypothetical "containing object"
17427    which will be given in the DW_AT_byte_size attribute for this bit-field.
17428    (See `byte_size_attribute' above).  */
17429
17430 static inline void
17431 add_bit_offset_attribute (dw_die_ref die, tree decl)
17432 {
17433   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17434   tree type = DECL_BIT_FIELD_TYPE (decl);
17435   HOST_WIDE_INT bitpos_int;
17436   HOST_WIDE_INT highest_order_object_bit_offset;
17437   HOST_WIDE_INT highest_order_field_bit_offset;
17438   HOST_WIDE_INT unsigned bit_offset;
17439
17440   /* Must be a field and a bit field.  */
17441   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17442
17443   /* We can't yet handle bit-fields whose offsets are variable, so if we
17444      encounter such things, just return without generating any attribute
17445      whatsoever.  Likewise for variable or too large size.  */
17446   if (! host_integerp (bit_position (decl), 0)
17447       || ! host_integerp (DECL_SIZE (decl), 1))
17448     return;
17449
17450   bitpos_int = int_bit_position (decl);
17451
17452   /* Note that the bit offset is always the distance (in bits) from the
17453      highest-order bit of the "containing object" to the highest-order bit of
17454      the bit-field itself.  Since the "high-order end" of any object or field
17455      is different on big-endian and little-endian machines, the computation
17456      below must take account of these differences.  */
17457   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17458   highest_order_field_bit_offset = bitpos_int;
17459
17460   if (! BYTES_BIG_ENDIAN)
17461     {
17462       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17463       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17464     }
17465
17466   bit_offset
17467     = (! BYTES_BIG_ENDIAN
17468        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17469        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17470
17471   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17472 }
17473
17474 /* For a FIELD_DECL node which represents a bit field, output an attribute
17475    which specifies the length in bits of the given field.  */
17476
17477 static inline void
17478 add_bit_size_attribute (dw_die_ref die, tree decl)
17479 {
17480   /* Must be a field and a bit field.  */
17481   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17482               && DECL_BIT_FIELD_TYPE (decl));
17483
17484   if (host_integerp (DECL_SIZE (decl), 1))
17485     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17486 }
17487
17488 /* If the compiled language is ANSI C, then add a 'prototyped'
17489    attribute, if arg types are given for the parameters of a function.  */
17490
17491 static inline void
17492 add_prototyped_attribute (dw_die_ref die, tree func_type)
17493 {
17494   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17495       && TYPE_ARG_TYPES (func_type) != NULL)
17496     add_AT_flag (die, DW_AT_prototyped, 1);
17497 }
17498
17499 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17500    by looking in either the type declaration or object declaration
17501    equate table.  */
17502
17503 static inline dw_die_ref
17504 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17505 {
17506   dw_die_ref origin_die = NULL;
17507
17508   if (TREE_CODE (origin) != FUNCTION_DECL)
17509     {
17510       /* We may have gotten separated from the block for the inlined
17511          function, if we're in an exception handler or some such; make
17512          sure that the abstract function has been written out.
17513
17514          Doing this for nested functions is wrong, however; functions are
17515          distinct units, and our context might not even be inline.  */
17516       tree fn = origin;
17517
17518       if (TYPE_P (fn))
17519         fn = TYPE_STUB_DECL (fn);
17520
17521       fn = decl_function_context (fn);
17522       if (fn)
17523         dwarf2out_abstract_function (fn);
17524     }
17525
17526   if (DECL_P (origin))
17527     origin_die = lookup_decl_die (origin);
17528   else if (TYPE_P (origin))
17529     origin_die = lookup_type_die (origin);
17530
17531   /* XXX: Functions that are never lowered don't always have correct block
17532      trees (in the case of java, they simply have no block tree, in some other
17533      languages).  For these functions, there is nothing we can really do to
17534      output correct debug info for inlined functions in all cases.  Rather
17535      than die, we'll just produce deficient debug info now, in that we will
17536      have variables without a proper abstract origin.  In the future, when all
17537      functions are lowered, we should re-add a gcc_assert (origin_die)
17538      here.  */
17539
17540   if (origin_die)
17541     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17542   return origin_die;
17543 }
17544
17545 /* We do not currently support the pure_virtual attribute.  */
17546
17547 static inline void
17548 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17549 {
17550   if (DECL_VINDEX (func_decl))
17551     {
17552       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17553
17554       if (host_integerp (DECL_VINDEX (func_decl), 0))
17555         add_AT_loc (die, DW_AT_vtable_elem_location,
17556                     new_loc_descr (DW_OP_constu,
17557                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17558                                    0));
17559
17560       /* GNU extension: Record what type this method came from originally.  */
17561       if (debug_info_level > DINFO_LEVEL_TERSE
17562           && DECL_CONTEXT (func_decl))
17563         add_AT_die_ref (die, DW_AT_containing_type,
17564                         lookup_type_die (DECL_CONTEXT (func_decl)));
17565     }
17566 }
17567 \f
17568 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17569    given decl.  This used to be a vendor extension until after DWARF 4
17570    standardized it.  */
17571
17572 static void
17573 add_linkage_attr (dw_die_ref die, tree decl)
17574 {
17575   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17576
17577   /* Mimic what assemble_name_raw does with a leading '*'.  */
17578   if (name[0] == '*')
17579     name = &name[1];
17580
17581   if (dwarf_version >= 4)
17582     add_AT_string (die, DW_AT_linkage_name, name);
17583   else
17584     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17585 }
17586
17587 /* Add source coordinate attributes for the given decl.  */
17588
17589 static void
17590 add_src_coords_attributes (dw_die_ref die, tree decl)
17591 {
17592   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17593
17594   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17595   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17596 }
17597
17598 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17599
17600 static void
17601 add_linkage_name (dw_die_ref die, tree decl)
17602 {
17603   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17604        && TREE_PUBLIC (decl)
17605        && !DECL_ABSTRACT (decl)
17606        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17607        && die->die_tag != DW_TAG_member)
17608     {
17609       /* Defer until we have an assembler name set.  */
17610       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17611         {
17612           limbo_die_node *asm_name;
17613
17614           asm_name = ggc_alloc_cleared_limbo_die_node ();
17615           asm_name->die = die;
17616           asm_name->created_for = decl;
17617           asm_name->next = deferred_asm_name;
17618           deferred_asm_name = asm_name;
17619         }
17620       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17621         add_linkage_attr (die, decl);
17622     }
17623 }
17624
17625 /* Add a DW_AT_name attribute and source coordinate attribute for the
17626    given decl, but only if it actually has a name.  */
17627
17628 static void
17629 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17630 {
17631   tree decl_name;
17632
17633   decl_name = DECL_NAME (decl);
17634   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17635     {
17636       const char *name = dwarf2_name (decl, 0);
17637       if (name)
17638         add_name_attribute (die, name);
17639       if (! DECL_ARTIFICIAL (decl))
17640         add_src_coords_attributes (die, decl);
17641
17642       add_linkage_name (die, decl);
17643     }
17644
17645 #ifdef VMS_DEBUGGING_INFO
17646   /* Get the function's name, as described by its RTL.  This may be different
17647      from the DECL_NAME name used in the source file.  */
17648   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17649     {
17650       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17651                    XEXP (DECL_RTL (decl), 0));
17652       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17653     }
17654 #endif /* VMS_DEBUGGING_INFO */
17655 }
17656
17657 #ifdef VMS_DEBUGGING_INFO
17658 /* Output the debug main pointer die for VMS */
17659
17660 void
17661 dwarf2out_vms_debug_main_pointer (void)
17662 {
17663   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17664   dw_die_ref die;
17665
17666   /* Allocate the VMS debug main subprogram die.  */
17667   die = ggc_alloc_cleared_die_node ();
17668   die->die_tag = DW_TAG_subprogram;
17669   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17670   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17671                                current_function_funcdef_no);
17672   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17673
17674   /* Make it the first child of comp_unit_die ().  */
17675   die->die_parent = comp_unit_die ();
17676   if (comp_unit_die ()->die_child)
17677     {
17678       die->die_sib = comp_unit_die ()->die_child->die_sib;
17679       comp_unit_die ()->die_child->die_sib = die;
17680     }
17681   else
17682     {
17683       die->die_sib = die;
17684       comp_unit_die ()->die_child = die;
17685     }
17686 }
17687 #endif /* VMS_DEBUGGING_INFO */
17688
17689 /* Push a new declaration scope.  */
17690
17691 static void
17692 push_decl_scope (tree scope)
17693 {
17694   VEC_safe_push (tree, gc, decl_scope_table, scope);
17695 }
17696
17697 /* Pop a declaration scope.  */
17698
17699 static inline void
17700 pop_decl_scope (void)
17701 {
17702   VEC_pop (tree, decl_scope_table);
17703 }
17704
17705 /* Return the DIE for the scope that immediately contains this type.
17706    Non-named types get global scope.  Named types nested in other
17707    types get their containing scope if it's open, or global scope
17708    otherwise.  All other types (i.e. function-local named types) get
17709    the current active scope.  */
17710
17711 static dw_die_ref
17712 scope_die_for (tree t, dw_die_ref context_die)
17713 {
17714   dw_die_ref scope_die = NULL;
17715   tree containing_scope;
17716   int i;
17717
17718   /* Non-types always go in the current scope.  */
17719   gcc_assert (TYPE_P (t));
17720
17721   containing_scope = TYPE_CONTEXT (t);
17722
17723   /* Use the containing namespace if it was passed in (for a declaration).  */
17724   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17725     {
17726       if (context_die == lookup_decl_die (containing_scope))
17727         /* OK */;
17728       else
17729         containing_scope = NULL_TREE;
17730     }
17731
17732   /* Ignore function type "scopes" from the C frontend.  They mean that
17733      a tagged type is local to a parmlist of a function declarator, but
17734      that isn't useful to DWARF.  */
17735   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17736     containing_scope = NULL_TREE;
17737
17738   if (containing_scope == NULL_TREE)
17739     scope_die = comp_unit_die ();
17740   else if (TYPE_P (containing_scope))
17741     {
17742       /* For types, we can just look up the appropriate DIE.  But
17743          first we check to see if we're in the middle of emitting it
17744          so we know where the new DIE should go.  */
17745       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17746         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17747           break;
17748
17749       if (i < 0)
17750         {
17751           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17752                       || TREE_ASM_WRITTEN (containing_scope));
17753           /*We are not in the middle of emitting the type
17754             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17755           scope_die = lookup_type_die (containing_scope);
17756
17757           /* If none of the current dies are suitable, we get file scope.  */
17758           if (scope_die == NULL)
17759             scope_die = comp_unit_die ();
17760         }
17761       else
17762         scope_die = lookup_type_die (containing_scope);
17763     }
17764   else
17765     scope_die = context_die;
17766
17767   return scope_die;
17768 }
17769
17770 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17771
17772 static inline int
17773 local_scope_p (dw_die_ref context_die)
17774 {
17775   for (; context_die; context_die = context_die->die_parent)
17776     if (context_die->die_tag == DW_TAG_inlined_subroutine
17777         || context_die->die_tag == DW_TAG_subprogram)
17778       return 1;
17779
17780   return 0;
17781 }
17782
17783 /* Returns nonzero if CONTEXT_DIE is a class.  */
17784
17785 static inline int
17786 class_scope_p (dw_die_ref context_die)
17787 {
17788   return (context_die
17789           && (context_die->die_tag == DW_TAG_structure_type
17790               || context_die->die_tag == DW_TAG_class_type
17791               || context_die->die_tag == DW_TAG_interface_type
17792               || context_die->die_tag == DW_TAG_union_type));
17793 }
17794
17795 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17796    whether or not to treat a DIE in this context as a declaration.  */
17797
17798 static inline int
17799 class_or_namespace_scope_p (dw_die_ref context_die)
17800 {
17801   return (class_scope_p (context_die)
17802           || (context_die && context_die->die_tag == DW_TAG_namespace));
17803 }
17804
17805 /* Many forms of DIEs require a "type description" attribute.  This
17806    routine locates the proper "type descriptor" die for the type given
17807    by 'type', and adds a DW_AT_type attribute below the given die.  */
17808
17809 static void
17810 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17811                     int decl_volatile, dw_die_ref context_die)
17812 {
17813   enum tree_code code  = TREE_CODE (type);
17814   dw_die_ref type_die  = NULL;
17815
17816   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17817      or fixed-point type, use the inner type.  This is because we have no
17818      support for unnamed types in base_type_die.  This can happen if this is
17819      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17820   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17821       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17822     type = TREE_TYPE (type), code = TREE_CODE (type);
17823
17824   if (code == ERROR_MARK
17825       /* Handle a special case.  For functions whose return type is void, we
17826          generate *no* type attribute.  (Note that no object may have type
17827          `void', so this only applies to function return types).  */
17828       || code == VOID_TYPE)
17829     return;
17830
17831   type_die = modified_type_die (type,
17832                                 decl_const || TYPE_READONLY (type),
17833                                 decl_volatile || TYPE_VOLATILE (type),
17834                                 context_die);
17835
17836   if (type_die != NULL)
17837     add_AT_die_ref (object_die, DW_AT_type, type_die);
17838 }
17839
17840 /* Given an object die, add the calling convention attribute for the
17841    function call type.  */
17842 static void
17843 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17844 {
17845   enum dwarf_calling_convention value = DW_CC_normal;
17846
17847   value = ((enum dwarf_calling_convention)
17848            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17849
17850   if (is_fortran ()
17851       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17852     {
17853       /* DWARF 2 doesn't provide a way to identify a program's source-level
17854         entry point.  DW_AT_calling_convention attributes are only meant
17855         to describe functions' calling conventions.  However, lacking a
17856         better way to signal the Fortran main program, we used this for 
17857         a long time, following existing custom.  Now, DWARF 4 has 
17858         DW_AT_main_subprogram, which we add below, but some tools still
17859         rely on the old way, which we thus keep.  */
17860       value = DW_CC_program;
17861
17862       if (dwarf_version >= 4 || !dwarf_strict)
17863         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17864     }
17865
17866   /* Only add the attribute if the backend requests it, and
17867      is not DW_CC_normal.  */
17868   if (value && (value != DW_CC_normal))
17869     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17870 }
17871
17872 /* Given a tree pointer to a struct, class, union, or enum type node, return
17873    a pointer to the (string) tag name for the given type, or zero if the type
17874    was declared without a tag.  */
17875
17876 static const char *
17877 type_tag (const_tree type)
17878 {
17879   const char *name = 0;
17880
17881   if (TYPE_NAME (type) != 0)
17882     {
17883       tree t = 0;
17884
17885       /* Find the IDENTIFIER_NODE for the type name.  */
17886       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17887           && !TYPE_NAMELESS (type))
17888         t = TYPE_NAME (type);
17889
17890       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17891          a TYPE_DECL node, regardless of whether or not a `typedef' was
17892          involved.  */
17893       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17894                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17895         {
17896           /* We want to be extra verbose.  Don't call dwarf_name if
17897              DECL_NAME isn't set.  The default hook for decl_printable_name
17898              doesn't like that, and in this context it's correct to return
17899              0, instead of "<anonymous>" or the like.  */
17900           if (DECL_NAME (TYPE_NAME (type))
17901               && !DECL_NAMELESS (TYPE_NAME (type)))
17902             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17903         }
17904
17905       /* Now get the name as a string, or invent one.  */
17906       if (!name && t != 0)
17907         name = IDENTIFIER_POINTER (t);
17908     }
17909
17910   return (name == 0 || *name == '\0') ? 0 : name;
17911 }
17912
17913 /* Return the type associated with a data member, make a special check
17914    for bit field types.  */
17915
17916 static inline tree
17917 member_declared_type (const_tree member)
17918 {
17919   return (DECL_BIT_FIELD_TYPE (member)
17920           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17921 }
17922
17923 /* Get the decl's label, as described by its RTL. This may be different
17924    from the DECL_NAME name used in the source file.  */
17925
17926 #if 0
17927 static const char *
17928 decl_start_label (tree decl)
17929 {
17930   rtx x;
17931   const char *fnname;
17932
17933   x = DECL_RTL (decl);
17934   gcc_assert (MEM_P (x));
17935
17936   x = XEXP (x, 0);
17937   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17938
17939   fnname = XSTR (x, 0);
17940   return fnname;
17941 }
17942 #endif
17943 \f
17944 /* These routines generate the internal representation of the DIE's for
17945    the compilation unit.  Debugging information is collected by walking
17946    the declaration trees passed in from dwarf2out_decl().  */
17947
17948 static void
17949 gen_array_type_die (tree type, dw_die_ref context_die)
17950 {
17951   dw_die_ref scope_die = scope_die_for (type, context_die);
17952   dw_die_ref array_die;
17953
17954   /* GNU compilers represent multidimensional array types as sequences of one
17955      dimensional array types whose element types are themselves array types.
17956      We sometimes squish that down to a single array_type DIE with multiple
17957      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17958      say that we are allowed to do this kind of compression in C, because
17959      there is no difference between an array of arrays and a multidimensional
17960      array.  We don't do this for Ada to remain as close as possible to the
17961      actual representation, which is especially important against the language
17962      flexibilty wrt arrays of variable size.  */
17963
17964   bool collapse_nested_arrays = !is_ada ();
17965   tree element_type;
17966
17967   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17968      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17969   if (TYPE_STRING_FLAG (type)
17970       && TREE_CODE (type) == ARRAY_TYPE
17971       && is_fortran ()
17972       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17973     {
17974       HOST_WIDE_INT size;
17975
17976       array_die = new_die (DW_TAG_string_type, scope_die, type);
17977       add_name_attribute (array_die, type_tag (type));
17978       equate_type_number_to_die (type, array_die);
17979       size = int_size_in_bytes (type);
17980       if (size >= 0)
17981         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17982       else if (TYPE_DOMAIN (type) != NULL_TREE
17983                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17984                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17985         {
17986           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17987           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17988
17989           size = int_size_in_bytes (TREE_TYPE (szdecl));
17990           if (loc && size > 0)
17991             {
17992               add_AT_location_description (array_die, DW_AT_string_length, loc);
17993               if (size != DWARF2_ADDR_SIZE)
17994                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17995             }
17996         }
17997       return;
17998     }
17999
18000   /* ??? The SGI dwarf reader fails for array of array of enum types
18001      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18002      array type comes before the outer array type.  We thus call gen_type_die
18003      before we new_die and must prevent nested array types collapsing for this
18004      target.  */
18005
18006 #ifdef MIPS_DEBUGGING_INFO
18007   gen_type_die (TREE_TYPE (type), context_die);
18008   collapse_nested_arrays = false;
18009 #endif
18010
18011   array_die = new_die (DW_TAG_array_type, scope_die, type);
18012   add_name_attribute (array_die, type_tag (type));
18013   equate_type_number_to_die (type, array_die);
18014
18015   if (TREE_CODE (type) == VECTOR_TYPE)
18016     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18017
18018   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18019   if (is_fortran ()
18020       && TREE_CODE (type) == ARRAY_TYPE
18021       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18022       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18023     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18024
18025 #if 0
18026   /* We default the array ordering.  SDB will probably do
18027      the right things even if DW_AT_ordering is not present.  It's not even
18028      an issue until we start to get into multidimensional arrays anyway.  If
18029      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18030      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18031      and when we find out that we need to put these in, we will only do so
18032      for multidimensional arrays.  */
18033   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18034 #endif
18035
18036 #ifdef MIPS_DEBUGGING_INFO
18037   /* The SGI compilers handle arrays of unknown bound by setting
18038      AT_declaration and not emitting any subrange DIEs.  */
18039   if (TREE_CODE (type) == ARRAY_TYPE
18040       && ! TYPE_DOMAIN (type))
18041     add_AT_flag (array_die, DW_AT_declaration, 1);
18042   else
18043 #endif
18044   if (TREE_CODE (type) == VECTOR_TYPE)
18045     {
18046       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18047       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18048       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18049       add_bound_info (subrange_die, DW_AT_upper_bound,
18050                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18051     }
18052   else
18053     add_subscript_info (array_die, type, collapse_nested_arrays);
18054
18055   /* Add representation of the type of the elements of this array type and
18056      emit the corresponding DIE if we haven't done it already.  */
18057   element_type = TREE_TYPE (type);
18058   if (collapse_nested_arrays)
18059     while (TREE_CODE (element_type) == ARRAY_TYPE)
18060       {
18061         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18062           break;
18063         element_type = TREE_TYPE (element_type);
18064       }
18065
18066 #ifndef MIPS_DEBUGGING_INFO
18067   gen_type_die (element_type, context_die);
18068 #endif
18069
18070   add_type_attribute (array_die, element_type, 0, 0, context_die);
18071
18072   if (get_AT (array_die, DW_AT_name))
18073     add_pubtype (type, array_die);
18074 }
18075
18076 static dw_loc_descr_ref
18077 descr_info_loc (tree val, tree base_decl)
18078 {
18079   HOST_WIDE_INT size;
18080   dw_loc_descr_ref loc, loc2;
18081   enum dwarf_location_atom op;
18082
18083   if (val == base_decl)
18084     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18085
18086   switch (TREE_CODE (val))
18087     {
18088     CASE_CONVERT:
18089       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18090     case VAR_DECL:
18091       return loc_descriptor_from_tree (val, 0);
18092     case INTEGER_CST:
18093       if (host_integerp (val, 0))
18094         return int_loc_descriptor (tree_low_cst (val, 0));
18095       break;
18096     case INDIRECT_REF:
18097       size = int_size_in_bytes (TREE_TYPE (val));
18098       if (size < 0)
18099         break;
18100       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18101       if (!loc)
18102         break;
18103       if (size == DWARF2_ADDR_SIZE)
18104         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18105       else
18106         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18107       return loc;
18108     case POINTER_PLUS_EXPR:
18109     case PLUS_EXPR:
18110       if (host_integerp (TREE_OPERAND (val, 1), 1)
18111           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18112              < 16384)
18113         {
18114           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18115           if (!loc)
18116             break;
18117           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18118         }
18119       else
18120         {
18121           op = DW_OP_plus;
18122         do_binop:
18123           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18124           if (!loc)
18125             break;
18126           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18127           if (!loc2)
18128             break;
18129           add_loc_descr (&loc, loc2);
18130           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18131         }
18132       return loc;
18133     case MINUS_EXPR:
18134       op = DW_OP_minus;
18135       goto do_binop;
18136     case MULT_EXPR:
18137       op = DW_OP_mul;
18138       goto do_binop;
18139     case EQ_EXPR:
18140       op = DW_OP_eq;
18141       goto do_binop;
18142     case NE_EXPR:
18143       op = DW_OP_ne;
18144       goto do_binop;
18145     default:
18146       break;
18147     }
18148   return NULL;
18149 }
18150
18151 static void
18152 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18153                       tree val, tree base_decl)
18154 {
18155   dw_loc_descr_ref loc;
18156
18157   if (host_integerp (val, 0))
18158     {
18159       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18160       return;
18161     }
18162
18163   loc = descr_info_loc (val, base_decl);
18164   if (!loc)
18165     return;
18166
18167   add_AT_loc (die, attr, loc);
18168 }
18169
18170 /* This routine generates DIE for array with hidden descriptor, details
18171    are filled into *info by a langhook.  */
18172
18173 static void
18174 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18175                           dw_die_ref context_die)
18176 {
18177   dw_die_ref scope_die = scope_die_for (type, context_die);
18178   dw_die_ref array_die;
18179   int dim;
18180
18181   array_die = new_die (DW_TAG_array_type, scope_die, type);
18182   add_name_attribute (array_die, type_tag (type));
18183   equate_type_number_to_die (type, array_die);
18184
18185   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18186   if (is_fortran ()
18187       && info->ndimensions >= 2)
18188     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18189
18190   if (info->data_location)
18191     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18192                           info->base_decl);
18193   if (info->associated)
18194     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18195                           info->base_decl);
18196   if (info->allocated)
18197     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18198                           info->base_decl);
18199
18200   for (dim = 0; dim < info->ndimensions; dim++)
18201     {
18202       dw_die_ref subrange_die
18203         = new_die (DW_TAG_subrange_type, array_die, NULL);
18204
18205       if (info->dimen[dim].lower_bound)
18206         {
18207           /* If it is the default value, omit it.  */
18208           int dflt;
18209
18210           if (host_integerp (info->dimen[dim].lower_bound, 0)
18211               && (dflt = lower_bound_default ()) != -1
18212               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18213             ;
18214           else
18215             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18216                                   info->dimen[dim].lower_bound,
18217                                   info->base_decl);
18218         }
18219       if (info->dimen[dim].upper_bound)
18220         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18221                               info->dimen[dim].upper_bound,
18222                               info->base_decl);
18223       if (info->dimen[dim].stride)
18224         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18225                               info->dimen[dim].stride,
18226                               info->base_decl);
18227     }
18228
18229   gen_type_die (info->element_type, context_die);
18230   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18231
18232   if (get_AT (array_die, DW_AT_name))
18233     add_pubtype (type, array_die);
18234 }
18235
18236 #if 0
18237 static void
18238 gen_entry_point_die (tree decl, dw_die_ref context_die)
18239 {
18240   tree origin = decl_ultimate_origin (decl);
18241   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18242
18243   if (origin != NULL)
18244     add_abstract_origin_attribute (decl_die, origin);
18245   else
18246     {
18247       add_name_and_src_coords_attributes (decl_die, decl);
18248       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18249                           0, 0, context_die);
18250     }
18251
18252   if (DECL_ABSTRACT (decl))
18253     equate_decl_number_to_die (decl, decl_die);
18254   else
18255     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18256 }
18257 #endif
18258
18259 /* Walk through the list of incomplete types again, trying once more to
18260    emit full debugging info for them.  */
18261
18262 static void
18263 retry_incomplete_types (void)
18264 {
18265   int i;
18266
18267   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18268     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18269                                   DINFO_USAGE_DIR_USE))
18270       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18271 }
18272
18273 /* Determine what tag to use for a record type.  */
18274
18275 static enum dwarf_tag
18276 record_type_tag (tree type)
18277 {
18278   if (! lang_hooks.types.classify_record)
18279     return DW_TAG_structure_type;
18280
18281   switch (lang_hooks.types.classify_record (type))
18282     {
18283     case RECORD_IS_STRUCT:
18284       return DW_TAG_structure_type;
18285
18286     case RECORD_IS_CLASS:
18287       return DW_TAG_class_type;
18288
18289     case RECORD_IS_INTERFACE:
18290       if (dwarf_version >= 3 || !dwarf_strict)
18291         return DW_TAG_interface_type;
18292       return DW_TAG_structure_type;
18293
18294     default:
18295       gcc_unreachable ();
18296     }
18297 }
18298
18299 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18300    include all of the information about the enumeration values also. Each
18301    enumerated type name/value is listed as a child of the enumerated type
18302    DIE.  */
18303
18304 static dw_die_ref
18305 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18306 {
18307   dw_die_ref type_die = lookup_type_die (type);
18308
18309   if (type_die == NULL)
18310     {
18311       type_die = new_die (DW_TAG_enumeration_type,
18312                           scope_die_for (type, context_die), type);
18313       equate_type_number_to_die (type, type_die);
18314       add_name_attribute (type_die, type_tag (type));
18315       if (dwarf_version >= 4 || !dwarf_strict)
18316         {
18317           if (ENUM_IS_SCOPED (type))
18318             add_AT_flag (type_die, DW_AT_enum_class, 1);
18319           if (ENUM_IS_OPAQUE (type))
18320             add_AT_flag (type_die, DW_AT_declaration, 1);
18321         }
18322     }
18323   else if (! TYPE_SIZE (type))
18324     return type_die;
18325   else
18326     remove_AT (type_die, DW_AT_declaration);
18327
18328   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18329      given enum type is incomplete, do not generate the DW_AT_byte_size
18330      attribute or the DW_AT_element_list attribute.  */
18331   if (TYPE_SIZE (type))
18332     {
18333       tree link;
18334
18335       TREE_ASM_WRITTEN (type) = 1;
18336       add_byte_size_attribute (type_die, type);
18337       if (TYPE_STUB_DECL (type) != NULL_TREE)
18338         {
18339           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18340           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18341         }
18342
18343       /* If the first reference to this type was as the return type of an
18344          inline function, then it may not have a parent.  Fix this now.  */
18345       if (type_die->die_parent == NULL)
18346         add_child_die (scope_die_for (type, context_die), type_die);
18347
18348       for (link = TYPE_VALUES (type);
18349            link != NULL; link = TREE_CHAIN (link))
18350         {
18351           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18352           tree value = TREE_VALUE (link);
18353
18354           add_name_attribute (enum_die,
18355                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18356
18357           if (TREE_CODE (value) == CONST_DECL)
18358             value = DECL_INITIAL (value);
18359
18360           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18361             /* DWARF2 does not provide a way of indicating whether or
18362                not enumeration constants are signed or unsigned.  GDB
18363                always assumes the values are signed, so we output all
18364                values as if they were signed.  That means that
18365                enumeration constants with very large unsigned values
18366                will appear to have negative values in the debugger.  */
18367             add_AT_int (enum_die, DW_AT_const_value,
18368                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18369         }
18370     }
18371   else
18372     add_AT_flag (type_die, DW_AT_declaration, 1);
18373
18374   if (get_AT (type_die, DW_AT_name))
18375     add_pubtype (type, type_die);
18376
18377   return type_die;
18378 }
18379
18380 /* Generate a DIE to represent either a real live formal parameter decl or to
18381    represent just the type of some formal parameter position in some function
18382    type.
18383
18384    Note that this routine is a bit unusual because its argument may be a
18385    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18386    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18387    node.  If it's the former then this function is being called to output a
18388    DIE to represent a formal parameter object (or some inlining thereof).  If
18389    it's the latter, then this function is only being called to output a
18390    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18391    argument type of some subprogram type.
18392    If EMIT_NAME_P is true, name and source coordinate attributes
18393    are emitted.  */
18394
18395 static dw_die_ref
18396 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18397                           dw_die_ref context_die)
18398 {
18399   tree node_or_origin = node ? node : origin;
18400   tree ultimate_origin;
18401   dw_die_ref parm_die
18402     = new_die (DW_TAG_formal_parameter, context_die, node);
18403
18404   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18405     {
18406     case tcc_declaration:
18407       ultimate_origin = decl_ultimate_origin (node_or_origin);
18408       if (node || ultimate_origin)
18409         origin = ultimate_origin;
18410       if (origin != NULL)
18411         add_abstract_origin_attribute (parm_die, origin);
18412       else if (emit_name_p)
18413         add_name_and_src_coords_attributes (parm_die, node);
18414       if (origin == NULL
18415           || (! DECL_ABSTRACT (node_or_origin)
18416               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18417                                            decl_function_context
18418                                                             (node_or_origin))))
18419         {
18420           tree type = TREE_TYPE (node_or_origin);
18421           if (decl_by_reference_p (node_or_origin))
18422             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18423                                 context_die);
18424           else
18425             add_type_attribute (parm_die, type,
18426                                 TREE_READONLY (node_or_origin),
18427                                 TREE_THIS_VOLATILE (node_or_origin),
18428                                 context_die);
18429         }
18430       if (origin == NULL && DECL_ARTIFICIAL (node))
18431         add_AT_flag (parm_die, DW_AT_artificial, 1);
18432
18433       if (node && node != origin)
18434         equate_decl_number_to_die (node, parm_die);
18435       if (! DECL_ABSTRACT (node_or_origin))
18436         add_location_or_const_value_attribute (parm_die, node_or_origin,
18437                                                DW_AT_location);
18438
18439       break;
18440
18441     case tcc_type:
18442       /* We were called with some kind of a ..._TYPE node.  */
18443       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18444       break;
18445
18446     default:
18447       gcc_unreachable ();
18448     }
18449
18450   return parm_die;
18451 }
18452
18453 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18454    children DW_TAG_formal_parameter DIEs representing the arguments of the
18455    parameter pack.
18456
18457    PARM_PACK must be a function parameter pack.
18458    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18459    must point to the subsequent arguments of the function PACK_ARG belongs to.
18460    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18461    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18462    following the last one for which a DIE was generated.  */
18463
18464 static dw_die_ref
18465 gen_formal_parameter_pack_die  (tree parm_pack,
18466                                 tree pack_arg,
18467                                 dw_die_ref subr_die,
18468                                 tree *next_arg)
18469 {
18470   tree arg;
18471   dw_die_ref parm_pack_die;
18472
18473   gcc_assert (parm_pack
18474               && lang_hooks.function_parameter_pack_p (parm_pack)
18475               && subr_die);
18476
18477   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18478   add_src_coords_attributes (parm_pack_die, parm_pack);
18479
18480   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18481     {
18482       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18483                                                                  parm_pack))
18484         break;
18485       gen_formal_parameter_die (arg, NULL,
18486                                 false /* Don't emit name attribute.  */,
18487                                 parm_pack_die);
18488     }
18489   if (next_arg)
18490     *next_arg = arg;
18491   return parm_pack_die;
18492 }
18493
18494 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18495    at the end of an (ANSI prototyped) formal parameters list.  */
18496
18497 static void
18498 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18499 {
18500   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18501 }
18502
18503 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18504    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18505    parameters as specified in some function type specification (except for
18506    those which appear as part of a function *definition*).  */
18507
18508 static void
18509 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18510 {
18511   tree link;
18512   tree formal_type = NULL;
18513   tree first_parm_type;
18514   tree arg;
18515
18516   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18517     {
18518       arg = DECL_ARGUMENTS (function_or_method_type);
18519       function_or_method_type = TREE_TYPE (function_or_method_type);
18520     }
18521   else
18522     arg = NULL_TREE;
18523
18524   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18525
18526   /* Make our first pass over the list of formal parameter types and output a
18527      DW_TAG_formal_parameter DIE for each one.  */
18528   for (link = first_parm_type; link; )
18529     {
18530       dw_die_ref parm_die;
18531
18532       formal_type = TREE_VALUE (link);
18533       if (formal_type == void_type_node)
18534         break;
18535
18536       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18537       parm_die = gen_formal_parameter_die (formal_type, NULL,
18538                                            true /* Emit name attribute.  */,
18539                                            context_die);
18540       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18541           && link == first_parm_type)
18542         {
18543           add_AT_flag (parm_die, DW_AT_artificial, 1);
18544           if (dwarf_version >= 3 || !dwarf_strict)
18545             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18546         }
18547       else if (arg && DECL_ARTIFICIAL (arg))
18548         add_AT_flag (parm_die, DW_AT_artificial, 1);
18549
18550       link = TREE_CHAIN (link);
18551       if (arg)
18552         arg = DECL_CHAIN (arg);
18553     }
18554
18555   /* If this function type has an ellipsis, add a
18556      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18557   if (formal_type != void_type_node)
18558     gen_unspecified_parameters_die (function_or_method_type, context_die);
18559
18560   /* Make our second (and final) pass over the list of formal parameter types
18561      and output DIEs to represent those types (as necessary).  */
18562   for (link = TYPE_ARG_TYPES (function_or_method_type);
18563        link && TREE_VALUE (link);
18564        link = TREE_CHAIN (link))
18565     gen_type_die (TREE_VALUE (link), context_die);
18566 }
18567
18568 /* We want to generate the DIE for TYPE so that we can generate the
18569    die for MEMBER, which has been defined; we will need to refer back
18570    to the member declaration nested within TYPE.  If we're trying to
18571    generate minimal debug info for TYPE, processing TYPE won't do the
18572    trick; we need to attach the member declaration by hand.  */
18573
18574 static void
18575 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18576 {
18577   gen_type_die (type, context_die);
18578
18579   /* If we're trying to avoid duplicate debug info, we may not have
18580      emitted the member decl for this function.  Emit it now.  */
18581   if (TYPE_STUB_DECL (type)
18582       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18583       && ! lookup_decl_die (member))
18584     {
18585       dw_die_ref type_die;
18586       gcc_assert (!decl_ultimate_origin (member));
18587
18588       push_decl_scope (type);
18589       type_die = lookup_type_die (type);
18590       if (TREE_CODE (member) == FUNCTION_DECL)
18591         gen_subprogram_die (member, type_die);
18592       else if (TREE_CODE (member) == FIELD_DECL)
18593         {
18594           /* Ignore the nameless fields that are used to skip bits but handle
18595              C++ anonymous unions and structs.  */
18596           if (DECL_NAME (member) != NULL_TREE
18597               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18598               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18599             {
18600               gen_type_die (member_declared_type (member), type_die);
18601               gen_field_die (member, type_die);
18602             }
18603         }
18604       else
18605         gen_variable_die (member, NULL_TREE, type_die);
18606
18607       pop_decl_scope ();
18608     }
18609 }
18610
18611 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18612    may later generate inlined and/or out-of-line instances of.  */
18613
18614 static void
18615 dwarf2out_abstract_function (tree decl)
18616 {
18617   dw_die_ref old_die;
18618   tree save_fn;
18619   tree context;
18620   int was_abstract;
18621   htab_t old_decl_loc_table;
18622
18623   /* Make sure we have the actual abstract inline, not a clone.  */
18624   decl = DECL_ORIGIN (decl);
18625
18626   old_die = lookup_decl_die (decl);
18627   if (old_die && get_AT (old_die, DW_AT_inline))
18628     /* We've already generated the abstract instance.  */
18629     return;
18630
18631   /* We can be called while recursively when seeing block defining inlined subroutine
18632      DIE.  Be sure to not clobber the outer location table nor use it or we would
18633      get locations in abstract instantces.  */
18634   old_decl_loc_table = decl_loc_table;
18635   decl_loc_table = NULL;
18636
18637   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18638      we don't get confused by DECL_ABSTRACT.  */
18639   if (debug_info_level > DINFO_LEVEL_TERSE)
18640     {
18641       context = decl_class_context (decl);
18642       if (context)
18643         gen_type_die_for_member
18644           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18645     }
18646
18647   /* Pretend we've just finished compiling this function.  */
18648   save_fn = current_function_decl;
18649   current_function_decl = decl;
18650   push_cfun (DECL_STRUCT_FUNCTION (decl));
18651
18652   was_abstract = DECL_ABSTRACT (decl);
18653   set_decl_abstract_flags (decl, 1);
18654   dwarf2out_decl (decl);
18655   if (! was_abstract)
18656     set_decl_abstract_flags (decl, 0);
18657
18658   current_function_decl = save_fn;
18659   decl_loc_table = old_decl_loc_table;
18660   pop_cfun ();
18661 }
18662
18663 /* Helper function of premark_used_types() which gets called through
18664    htab_traverse.
18665
18666    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18667    marked as unused by prune_unused_types.  */
18668
18669 static int
18670 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18671 {
18672   tree type;
18673   dw_die_ref die;
18674
18675   type = (tree) *slot;
18676   die = lookup_type_die (type);
18677   if (die != NULL)
18678     die->die_perennial_p = 1;
18679   return 1;
18680 }
18681
18682 /* Helper function of premark_types_used_by_global_vars which gets called
18683    through htab_traverse.
18684
18685    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18686    marked as unused by prune_unused_types. The DIE of the type is marked
18687    only if the global variable using the type will actually be emitted.  */
18688
18689 static int
18690 premark_types_used_by_global_vars_helper (void **slot,
18691                                           void *data ATTRIBUTE_UNUSED)
18692 {
18693   struct types_used_by_vars_entry *entry;
18694   dw_die_ref die;
18695
18696   entry = (struct types_used_by_vars_entry *) *slot;
18697   gcc_assert (entry->type != NULL
18698               && entry->var_decl != NULL);
18699   die = lookup_type_die (entry->type);
18700   if (die)
18701     {
18702       /* Ask cgraph if the global variable really is to be emitted.
18703          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18704       struct varpool_node *node = varpool_get_node (entry->var_decl);
18705       if (node && node->needed)
18706         {
18707           die->die_perennial_p = 1;
18708           /* Keep the parent DIEs as well.  */
18709           while ((die = die->die_parent) && die->die_perennial_p == 0)
18710             die->die_perennial_p = 1;
18711         }
18712     }
18713   return 1;
18714 }
18715
18716 /* Mark all members of used_types_hash as perennial.  */
18717
18718 static void
18719 premark_used_types (void)
18720 {
18721   if (cfun && cfun->used_types_hash)
18722     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18723 }
18724
18725 /* Mark all members of types_used_by_vars_entry as perennial.  */
18726
18727 static void
18728 premark_types_used_by_global_vars (void)
18729 {
18730   if (types_used_by_vars_hash)
18731     htab_traverse (types_used_by_vars_hash,
18732                    premark_types_used_by_global_vars_helper, NULL);
18733 }
18734
18735 /* Generate a DIE to represent a declared function (either file-scope or
18736    block-local).  */
18737
18738 static void
18739 gen_subprogram_die (tree decl, dw_die_ref context_die)
18740 {
18741   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18742   tree origin = decl_ultimate_origin (decl);
18743   dw_die_ref subr_die;
18744   tree fn_arg_types;
18745   tree outer_scope;
18746   dw_die_ref old_die = lookup_decl_die (decl);
18747   int declaration = (current_function_decl != decl
18748                      || class_or_namespace_scope_p (context_die));
18749
18750   premark_used_types ();
18751
18752   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18753      started to generate the abstract instance of an inline, decided to output
18754      its containing class, and proceeded to emit the declaration of the inline
18755      from the member list for the class.  If so, DECLARATION takes priority;
18756      we'll get back to the abstract instance when done with the class.  */
18757
18758   /* The class-scope declaration DIE must be the primary DIE.  */
18759   if (origin && declaration && class_or_namespace_scope_p (context_die))
18760     {
18761       origin = NULL;
18762       gcc_assert (!old_die);
18763     }
18764
18765   /* Now that the C++ front end lazily declares artificial member fns, we
18766      might need to retrofit the declaration into its class.  */
18767   if (!declaration && !origin && !old_die
18768       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18769       && !class_or_namespace_scope_p (context_die)
18770       && debug_info_level > DINFO_LEVEL_TERSE)
18771     old_die = force_decl_die (decl);
18772
18773   if (origin != NULL)
18774     {
18775       gcc_assert (!declaration || local_scope_p (context_die));
18776
18777       /* Fixup die_parent for the abstract instance of a nested
18778          inline function.  */
18779       if (old_die && old_die->die_parent == NULL)
18780         add_child_die (context_die, old_die);
18781
18782       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18783       add_abstract_origin_attribute (subr_die, origin);
18784     }
18785   else if (old_die)
18786     {
18787       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18788       struct dwarf_file_data * file_index = lookup_filename (s.file);
18789
18790       if (!get_AT_flag (old_die, DW_AT_declaration)
18791           /* We can have a normal definition following an inline one in the
18792              case of redefinition of GNU C extern inlines.
18793              It seems reasonable to use AT_specification in this case.  */
18794           && !get_AT (old_die, DW_AT_inline))
18795         {
18796           /* Detect and ignore this case, where we are trying to output
18797              something we have already output.  */
18798           return;
18799         }
18800
18801       /* If the definition comes from the same place as the declaration,
18802          maybe use the old DIE.  We always want the DIE for this function
18803          that has the *_pc attributes to be under comp_unit_die so the
18804          debugger can find it.  We also need to do this for abstract
18805          instances of inlines, since the spec requires the out-of-line copy
18806          to have the same parent.  For local class methods, this doesn't
18807          apply; we just use the old DIE.  */
18808       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18809           && (DECL_ARTIFICIAL (decl)
18810               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18811                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18812                       == (unsigned) s.line))))
18813         {
18814           subr_die = old_die;
18815
18816           /* Clear out the declaration attribute and the formal parameters.
18817              Do not remove all children, because it is possible that this
18818              declaration die was forced using force_decl_die(). In such
18819              cases die that forced declaration die (e.g. TAG_imported_module)
18820              is one of the children that we do not want to remove.  */
18821           remove_AT (subr_die, DW_AT_declaration);
18822           remove_AT (subr_die, DW_AT_object_pointer);
18823           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18824         }
18825       else
18826         {
18827           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18828           add_AT_specification (subr_die, old_die);
18829           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18830             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18831           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18832             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18833         }
18834     }
18835   else
18836     {
18837       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18838
18839       if (TREE_PUBLIC (decl))
18840         add_AT_flag (subr_die, DW_AT_external, 1);
18841
18842       add_name_and_src_coords_attributes (subr_die, decl);
18843       if (debug_info_level > DINFO_LEVEL_TERSE)
18844         {
18845           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18846           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18847                               0, 0, context_die);
18848         }
18849
18850       add_pure_or_virtual_attribute (subr_die, decl);
18851       if (DECL_ARTIFICIAL (decl))
18852         add_AT_flag (subr_die, DW_AT_artificial, 1);
18853
18854       add_accessibility_attribute (subr_die, decl);
18855     }
18856
18857   if (declaration)
18858     {
18859       if (!old_die || !get_AT (old_die, DW_AT_inline))
18860         {
18861           add_AT_flag (subr_die, DW_AT_declaration, 1);
18862
18863           /* If this is an explicit function declaration then generate
18864              a DW_AT_explicit attribute.  */
18865           if (lang_hooks.decls.function_decl_explicit_p (decl)
18866               && (dwarf_version >= 3 || !dwarf_strict))
18867             add_AT_flag (subr_die, DW_AT_explicit, 1);
18868
18869           /* The first time we see a member function, it is in the context of
18870              the class to which it belongs.  We make sure of this by emitting
18871              the class first.  The next time is the definition, which is
18872              handled above.  The two may come from the same source text.
18873
18874              Note that force_decl_die() forces function declaration die. It is
18875              later reused to represent definition.  */
18876           equate_decl_number_to_die (decl, subr_die);
18877         }
18878     }
18879   else if (DECL_ABSTRACT (decl))
18880     {
18881       if (DECL_DECLARED_INLINE_P (decl))
18882         {
18883           if (cgraph_function_possibly_inlined_p (decl))
18884             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18885           else
18886             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18887         }
18888       else
18889         {
18890           if (cgraph_function_possibly_inlined_p (decl))
18891             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18892           else
18893             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18894         }
18895
18896       if (DECL_DECLARED_INLINE_P (decl)
18897           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18898         add_AT_flag (subr_die, DW_AT_artificial, 1);
18899
18900       equate_decl_number_to_die (decl, subr_die);
18901     }
18902   else if (!DECL_EXTERNAL (decl))
18903     {
18904       HOST_WIDE_INT cfa_fb_offset;
18905
18906       if (!old_die || !get_AT (old_die, DW_AT_inline))
18907         equate_decl_number_to_die (decl, subr_die);
18908
18909       if (!flag_reorder_blocks_and_partition)
18910         {
18911           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18912                                        current_function_funcdef_no);
18913           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18914           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18915                                        current_function_funcdef_no);
18916           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18917
18918 #if VMS_DEBUGGING_INFO
18919       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18920          Section 2.3 Prologue and Epilogue Attributes:
18921          When a breakpoint is set on entry to a function, it is generally
18922          desirable for execution to be suspended, not on the very first
18923          instruction of the function, but rather at a point after the
18924          function's frame has been set up, after any language defined local
18925          declaration processing has been completed, and before execution of
18926          the first statement of the function begins. Debuggers generally
18927          cannot properly determine where this point is.  Similarly for a
18928          breakpoint set on exit from a function. The prologue and epilogue
18929          attributes allow a compiler to communicate the location(s) to use.  */
18930
18931       {
18932         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18933
18934         if (fde->dw_fde_vms_end_prologue)
18935           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18936             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18937
18938         if (fde->dw_fde_vms_begin_epilogue)
18939           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18940             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18941       }
18942 #endif
18943
18944           add_pubname (decl, subr_die);
18945           add_arange (decl, subr_die);
18946         }
18947       else
18948         {  /* Do nothing for now; maybe need to duplicate die, one for
18949               hot section and one for cold section, then use the hot/cold
18950               section begin/end labels to generate the aranges...  */
18951           /*
18952             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18953             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18954             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18955             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18956
18957             add_pubname (decl, subr_die);
18958             add_arange (decl, subr_die);
18959             add_arange (decl, subr_die);
18960            */
18961         }
18962
18963 #ifdef MIPS_DEBUGGING_INFO
18964       /* Add a reference to the FDE for this routine.  */
18965       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18966 #endif
18967
18968       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18969
18970       /* We define the "frame base" as the function's CFA.  This is more
18971          convenient for several reasons: (1) It's stable across the prologue
18972          and epilogue, which makes it better than just a frame pointer,
18973          (2) With dwarf3, there exists a one-byte encoding that allows us
18974          to reference the .debug_frame data by proxy, but failing that,
18975          (3) We can at least reuse the code inspection and interpretation
18976          code that determines the CFA position at various points in the
18977          function.  */
18978       if (dwarf_version >= 3)
18979         {
18980           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18981           add_AT_loc (subr_die, DW_AT_frame_base, op);
18982         }
18983       else
18984         {
18985           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18986           if (list->dw_loc_next)
18987             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18988           else
18989             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18990         }
18991
18992       /* Compute a displacement from the "steady-state frame pointer" to
18993          the CFA.  The former is what all stack slots and argument slots
18994          will reference in the rtl; the later is what we've told the
18995          debugger about.  We'll need to adjust all frame_base references
18996          by this displacement.  */
18997       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18998
18999       if (cfun->static_chain_decl)
19000         add_AT_location_description (subr_die, DW_AT_static_link,
19001                  loc_list_from_tree (cfun->static_chain_decl, 2));
19002     }
19003
19004   /* Generate child dies for template paramaters.  */
19005   if (debug_info_level > DINFO_LEVEL_TERSE)
19006     gen_generic_params_dies (decl);
19007
19008   /* Now output descriptions of the arguments for this function. This gets
19009      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19010      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19011      `...' at the end of the formal parameter list.  In order to find out if
19012      there was a trailing ellipsis or not, we must instead look at the type
19013      associated with the FUNCTION_DECL.  This will be a node of type
19014      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19015      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19016      an ellipsis at the end.  */
19017
19018   /* In the case where we are describing a mere function declaration, all we
19019      need to do here (and all we *can* do here) is to describe the *types* of
19020      its formal parameters.  */
19021   if (debug_info_level <= DINFO_LEVEL_TERSE)
19022     ;
19023   else if (declaration)
19024     gen_formal_types_die (decl, subr_die);
19025   else
19026     {
19027       /* Generate DIEs to represent all known formal parameters.  */
19028       tree parm = DECL_ARGUMENTS (decl);
19029       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19030       tree generic_decl_parm = generic_decl
19031                                 ? DECL_ARGUMENTS (generic_decl)
19032                                 : NULL;
19033
19034       /* Now we want to walk the list of parameters of the function and
19035          emit their relevant DIEs.
19036
19037          We consider the case of DECL being an instance of a generic function
19038          as well as it being a normal function.
19039
19040          If DECL is an instance of a generic function we walk the
19041          parameters of the generic function declaration _and_ the parameters of
19042          DECL itself. This is useful because we want to emit specific DIEs for
19043          function parameter packs and those are declared as part of the
19044          generic function declaration. In that particular case,
19045          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19046          That DIE has children DIEs representing the set of arguments
19047          of the pack. Note that the set of pack arguments can be empty.
19048          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19049          children DIE.
19050
19051          Otherwise, we just consider the parameters of DECL.  */
19052       while (generic_decl_parm || parm)
19053         {
19054           if (generic_decl_parm
19055               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19056             gen_formal_parameter_pack_die (generic_decl_parm,
19057                                            parm, subr_die,
19058                                            &parm);
19059           else if (parm)
19060             {
19061               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19062
19063               if (parm == DECL_ARGUMENTS (decl)
19064                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19065                   && parm_die
19066                   && (dwarf_version >= 3 || !dwarf_strict))
19067                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19068
19069               parm = DECL_CHAIN (parm);
19070             }
19071
19072           if (generic_decl_parm)
19073             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19074         }
19075
19076       /* Decide whether we need an unspecified_parameters DIE at the end.
19077          There are 2 more cases to do this for: 1) the ansi ... declaration -
19078          this is detectable when the end of the arg list is not a
19079          void_type_node 2) an unprototyped function declaration (not a
19080          definition).  This just means that we have no info about the
19081          parameters at all.  */
19082       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
19083       if (fn_arg_types != NULL)
19084         {
19085           /* This is the prototyped case, check for....  */
19086           if (stdarg_p (TREE_TYPE (decl)))
19087             gen_unspecified_parameters_die (decl, subr_die);
19088         }
19089       else if (DECL_INITIAL (decl) == NULL_TREE)
19090         gen_unspecified_parameters_die (decl, subr_die);
19091     }
19092
19093   /* Output Dwarf info for all of the stuff within the body of the function
19094      (if it has one - it may be just a declaration).  */
19095   outer_scope = DECL_INITIAL (decl);
19096
19097   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19098      a function.  This BLOCK actually represents the outermost binding contour
19099      for the function, i.e. the contour in which the function's formal
19100      parameters and labels get declared. Curiously, it appears that the front
19101      end doesn't actually put the PARM_DECL nodes for the current function onto
19102      the BLOCK_VARS list for this outer scope, but are strung off of the
19103      DECL_ARGUMENTS list for the function instead.
19104
19105      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19106      the LABEL_DECL nodes for the function however, and we output DWARF info
19107      for those in decls_for_scope.  Just within the `outer_scope' there will be
19108      a BLOCK node representing the function's outermost pair of curly braces,
19109      and any blocks used for the base and member initializers of a C++
19110      constructor function.  */
19111   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19112     {
19113       /* Emit a DW_TAG_variable DIE for a named return value.  */
19114       if (DECL_NAME (DECL_RESULT (decl)))
19115         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19116
19117       current_function_has_inlines = 0;
19118       decls_for_scope (outer_scope, subr_die, 0);
19119     }
19120   /* Add the calling convention attribute if requested.  */
19121   add_calling_convention_attribute (subr_die, decl);
19122
19123 }
19124
19125 /* Returns a hash value for X (which really is a die_struct).  */
19126
19127 static hashval_t
19128 common_block_die_table_hash (const void *x)
19129 {
19130   const_dw_die_ref d = (const_dw_die_ref) x;
19131   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19132 }
19133
19134 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19135    as decl_id and die_parent of die_struct Y.  */
19136
19137 static int
19138 common_block_die_table_eq (const void *x, const void *y)
19139 {
19140   const_dw_die_ref d = (const_dw_die_ref) x;
19141   const_dw_die_ref e = (const_dw_die_ref) y;
19142   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19143 }
19144
19145 /* Generate a DIE to represent a declared data object.
19146    Either DECL or ORIGIN must be non-null.  */
19147
19148 static void
19149 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19150 {
19151   HOST_WIDE_INT off;
19152   tree com_decl;
19153   tree decl_or_origin = decl ? decl : origin;
19154   tree ultimate_origin;
19155   dw_die_ref var_die;
19156   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19157   dw_die_ref origin_die;
19158   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19159                       || class_or_namespace_scope_p (context_die));
19160   bool specialization_p = false;
19161
19162   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19163   if (decl || ultimate_origin)
19164     origin = ultimate_origin;
19165   com_decl = fortran_common (decl_or_origin, &off);
19166
19167   /* Symbol in common gets emitted as a child of the common block, in the form
19168      of a data member.  */
19169   if (com_decl)
19170     {
19171       dw_die_ref com_die;
19172       dw_loc_list_ref loc;
19173       die_node com_die_arg;
19174
19175       var_die = lookup_decl_die (decl_or_origin);
19176       if (var_die)
19177         {
19178           if (get_AT (var_die, DW_AT_location) == NULL)
19179             {
19180               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19181               if (loc)
19182                 {
19183                   if (off)
19184                     {
19185                       /* Optimize the common case.  */
19186                       if (single_element_loc_list_p (loc)
19187                           && loc->expr->dw_loc_opc == DW_OP_addr
19188                           && loc->expr->dw_loc_next == NULL
19189                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19190                              == SYMBOL_REF)
19191                         loc->expr->dw_loc_oprnd1.v.val_addr
19192                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19193                         else
19194                           loc_list_plus_const (loc, off);
19195                     }
19196                   add_AT_location_description (var_die, DW_AT_location, loc);
19197                   remove_AT (var_die, DW_AT_declaration);
19198                 }
19199             }
19200           return;
19201         }
19202
19203       if (common_block_die_table == NULL)
19204         common_block_die_table
19205           = htab_create_ggc (10, common_block_die_table_hash,
19206                              common_block_die_table_eq, NULL);
19207
19208       com_die_arg.decl_id = DECL_UID (com_decl);
19209       com_die_arg.die_parent = context_die;
19210       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19211       loc = loc_list_from_tree (com_decl, 2);
19212       if (com_die == NULL)
19213         {
19214           const char *cnam
19215             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19216           void **slot;
19217
19218           com_die = new_die (DW_TAG_common_block, context_die, decl);
19219           add_name_and_src_coords_attributes (com_die, com_decl);
19220           if (loc)
19221             {
19222               add_AT_location_description (com_die, DW_AT_location, loc);
19223               /* Avoid sharing the same loc descriptor between
19224                  DW_TAG_common_block and DW_TAG_variable.  */
19225               loc = loc_list_from_tree (com_decl, 2);
19226             }
19227           else if (DECL_EXTERNAL (decl))
19228             add_AT_flag (com_die, DW_AT_declaration, 1);
19229           add_pubname_string (cnam, com_die); /* ??? needed? */
19230           com_die->decl_id = DECL_UID (com_decl);
19231           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19232           *slot = (void *) com_die;
19233         }
19234       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19235         {
19236           add_AT_location_description (com_die, DW_AT_location, loc);
19237           loc = loc_list_from_tree (com_decl, 2);
19238           remove_AT (com_die, DW_AT_declaration);
19239         }
19240       var_die = new_die (DW_TAG_variable, com_die, decl);
19241       add_name_and_src_coords_attributes (var_die, decl);
19242       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19243                           TREE_THIS_VOLATILE (decl), context_die);
19244       add_AT_flag (var_die, DW_AT_external, 1);
19245       if (loc)
19246         {
19247           if (off)
19248             {
19249               /* Optimize the common case.  */
19250               if (single_element_loc_list_p (loc)
19251                   && loc->expr->dw_loc_opc == DW_OP_addr
19252                   && loc->expr->dw_loc_next == NULL
19253                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19254                 loc->expr->dw_loc_oprnd1.v.val_addr
19255                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19256               else
19257                 loc_list_plus_const (loc, off);
19258             }
19259           add_AT_location_description (var_die, DW_AT_location, loc);
19260         }
19261       else if (DECL_EXTERNAL (decl))
19262         add_AT_flag (var_die, DW_AT_declaration, 1);
19263       equate_decl_number_to_die (decl, var_die);
19264       return;
19265     }
19266
19267   /* If the compiler emitted a definition for the DECL declaration
19268      and if we already emitted a DIE for it, don't emit a second
19269      DIE for it again. Allow re-declarations of DECLs that are
19270      inside functions, though.  */
19271   if (old_die && declaration && !local_scope_p (context_die))
19272     return;
19273
19274   /* For static data members, the declaration in the class is supposed
19275      to have DW_TAG_member tag; the specification should still be
19276      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19277   if (declaration && class_scope_p (context_die))
19278     var_die = new_die (DW_TAG_member, context_die, decl);
19279   else
19280     var_die = new_die (DW_TAG_variable, context_die, decl);
19281
19282   origin_die = NULL;
19283   if (origin != NULL)
19284     origin_die = add_abstract_origin_attribute (var_die, origin);
19285
19286   /* Loop unrolling can create multiple blocks that refer to the same
19287      static variable, so we must test for the DW_AT_declaration flag.
19288
19289      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19290      copy decls and set the DECL_ABSTRACT flag on them instead of
19291      sharing them.
19292
19293      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19294
19295      ??? The declare_in_namespace support causes us to get two DIEs for one
19296      variable, both of which are declarations.  We want to avoid considering
19297      one to be a specification, so we must test that this DIE is not a
19298      declaration.  */
19299   else if (old_die && TREE_STATIC (decl) && ! declaration
19300            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19301     {
19302       /* This is a definition of a C++ class level static.  */
19303       add_AT_specification (var_die, old_die);
19304       specialization_p = true;
19305       if (DECL_NAME (decl))
19306         {
19307           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19308           struct dwarf_file_data * file_index = lookup_filename (s.file);
19309
19310           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19311             add_AT_file (var_die, DW_AT_decl_file, file_index);
19312
19313           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19314             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19315
19316           if (old_die->die_tag == DW_TAG_member)
19317             add_linkage_name (var_die, decl);
19318         }
19319     }
19320   else
19321     add_name_and_src_coords_attributes (var_die, decl);
19322
19323   if ((origin == NULL && !specialization_p)
19324       || (origin != NULL
19325           && !DECL_ABSTRACT (decl_or_origin)
19326           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19327                                        decl_function_context
19328                                                         (decl_or_origin))))
19329     {
19330       tree type = TREE_TYPE (decl_or_origin);
19331
19332       if (decl_by_reference_p (decl_or_origin))
19333         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19334       else
19335         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19336                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19337     }
19338
19339   if (origin == NULL && !specialization_p)
19340     {
19341       if (TREE_PUBLIC (decl))
19342         add_AT_flag (var_die, DW_AT_external, 1);
19343
19344       if (DECL_ARTIFICIAL (decl))
19345         add_AT_flag (var_die, DW_AT_artificial, 1);
19346
19347       add_accessibility_attribute (var_die, decl);
19348     }
19349
19350   if (declaration)
19351     add_AT_flag (var_die, DW_AT_declaration, 1);
19352
19353   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19354     equate_decl_number_to_die (decl, var_die);
19355
19356   if (! declaration
19357       && (! DECL_ABSTRACT (decl_or_origin)
19358           /* Local static vars are shared between all clones/inlines,
19359              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19360              already set.  */
19361           || (TREE_CODE (decl_or_origin) == VAR_DECL
19362               && TREE_STATIC (decl_or_origin)
19363               && DECL_RTL_SET_P (decl_or_origin)))
19364       /* When abstract origin already has DW_AT_location attribute, no need
19365          to add it again.  */
19366       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19367     {
19368       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19369           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19370         defer_location (decl_or_origin, var_die);
19371       else
19372         add_location_or_const_value_attribute (var_die,
19373                                                decl_or_origin,
19374                                                DW_AT_location);
19375       add_pubname (decl_or_origin, var_die);
19376     }
19377   else
19378     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19379 }
19380
19381 /* Generate a DIE to represent a named constant.  */
19382
19383 static void
19384 gen_const_die (tree decl, dw_die_ref context_die)
19385 {
19386   dw_die_ref const_die;
19387   tree type = TREE_TYPE (decl);
19388
19389   const_die = new_die (DW_TAG_constant, context_die, decl);
19390   add_name_and_src_coords_attributes (const_die, decl);
19391   add_type_attribute (const_die, type, 1, 0, context_die);
19392   if (TREE_PUBLIC (decl))
19393     add_AT_flag (const_die, DW_AT_external, 1);
19394   if (DECL_ARTIFICIAL (decl))
19395     add_AT_flag (const_die, DW_AT_artificial, 1);
19396   tree_add_const_value_attribute_for_decl (const_die, decl);
19397 }
19398
19399 /* Generate a DIE to represent a label identifier.  */
19400
19401 static void
19402 gen_label_die (tree decl, dw_die_ref context_die)
19403 {
19404   tree origin = decl_ultimate_origin (decl);
19405   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19406   rtx insn;
19407   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19408
19409   if (origin != NULL)
19410     add_abstract_origin_attribute (lbl_die, origin);
19411   else
19412     add_name_and_src_coords_attributes (lbl_die, decl);
19413
19414   if (DECL_ABSTRACT (decl))
19415     equate_decl_number_to_die (decl, lbl_die);
19416   else
19417     {
19418       insn = DECL_RTL_IF_SET (decl);
19419
19420       /* Deleted labels are programmer specified labels which have been
19421          eliminated because of various optimizations.  We still emit them
19422          here so that it is possible to put breakpoints on them.  */
19423       if (insn
19424           && (LABEL_P (insn)
19425               || ((NOTE_P (insn)
19426                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19427         {
19428           /* When optimization is enabled (via -O) some parts of the compiler
19429              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19430              represent source-level labels which were explicitly declared by
19431              the user.  This really shouldn't be happening though, so catch
19432              it if it ever does happen.  */
19433           gcc_assert (!INSN_DELETED_P (insn));
19434
19435           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19436           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19437         }
19438     }
19439 }
19440
19441 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19442    attributes to the DIE for a block STMT, to describe where the inlined
19443    function was called from.  This is similar to add_src_coords_attributes.  */
19444
19445 static inline void
19446 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19447 {
19448   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19449
19450   if (dwarf_version >= 3 || !dwarf_strict)
19451     {
19452       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19453       add_AT_unsigned (die, DW_AT_call_line, s.line);
19454     }
19455 }
19456
19457
19458 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19459    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19460
19461 static inline void
19462 add_high_low_attributes (tree stmt, dw_die_ref die)
19463 {
19464   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19465
19466   if (BLOCK_FRAGMENT_CHAIN (stmt)
19467       && (dwarf_version >= 3 || !dwarf_strict))
19468     {
19469       tree chain;
19470
19471       if (inlined_function_outer_scope_p (stmt))
19472         {
19473           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19474                                        BLOCK_NUMBER (stmt));
19475           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19476         }
19477
19478       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19479
19480       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19481       do
19482         {
19483           add_ranges (chain);
19484           chain = BLOCK_FRAGMENT_CHAIN (chain);
19485         }
19486       while (chain);
19487       add_ranges (NULL);
19488     }
19489   else
19490     {
19491       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19492                                    BLOCK_NUMBER (stmt));
19493       add_AT_lbl_id (die, DW_AT_low_pc, label);
19494       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19495                                    BLOCK_NUMBER (stmt));
19496       add_AT_lbl_id (die, DW_AT_high_pc, label);
19497     }
19498 }
19499
19500 /* Generate a DIE for a lexical block.  */
19501
19502 static void
19503 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19504 {
19505   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19506
19507   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19508     add_high_low_attributes (stmt, stmt_die);
19509
19510   decls_for_scope (stmt, stmt_die, depth);
19511 }
19512
19513 /* Generate a DIE for an inlined subprogram.  */
19514
19515 static void
19516 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19517 {
19518   tree decl;
19519
19520   /* The instance of function that is effectively being inlined shall not
19521      be abstract.  */
19522   gcc_assert (! BLOCK_ABSTRACT (stmt));
19523
19524   decl = block_ultimate_origin (stmt);
19525
19526   /* Emit info for the abstract instance first, if we haven't yet.  We
19527      must emit this even if the block is abstract, otherwise when we
19528      emit the block below (or elsewhere), we may end up trying to emit
19529      a die whose origin die hasn't been emitted, and crashing.  */
19530   dwarf2out_abstract_function (decl);
19531
19532   if (! BLOCK_ABSTRACT (stmt))
19533     {
19534       dw_die_ref subr_die
19535         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19536
19537       add_abstract_origin_attribute (subr_die, decl);
19538       if (TREE_ASM_WRITTEN (stmt))
19539         add_high_low_attributes (stmt, subr_die);
19540       add_call_src_coords_attributes (stmt, subr_die);
19541
19542       decls_for_scope (stmt, subr_die, depth);
19543       current_function_has_inlines = 1;
19544     }
19545 }
19546
19547 /* Generate a DIE for a field in a record, or structure.  */
19548
19549 static void
19550 gen_field_die (tree decl, dw_die_ref context_die)
19551 {
19552   dw_die_ref decl_die;
19553
19554   if (TREE_TYPE (decl) == error_mark_node)
19555     return;
19556
19557   decl_die = new_die (DW_TAG_member, context_die, decl);
19558   add_name_and_src_coords_attributes (decl_die, decl);
19559   add_type_attribute (decl_die, member_declared_type (decl),
19560                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19561                       context_die);
19562
19563   if (DECL_BIT_FIELD_TYPE (decl))
19564     {
19565       add_byte_size_attribute (decl_die, decl);
19566       add_bit_size_attribute (decl_die, decl);
19567       add_bit_offset_attribute (decl_die, decl);
19568     }
19569
19570   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19571     add_data_member_location_attribute (decl_die, decl);
19572
19573   if (DECL_ARTIFICIAL (decl))
19574     add_AT_flag (decl_die, DW_AT_artificial, 1);
19575
19576   add_accessibility_attribute (decl_die, decl);
19577
19578   /* Equate decl number to die, so that we can look up this decl later on.  */
19579   equate_decl_number_to_die (decl, decl_die);
19580 }
19581
19582 #if 0
19583 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19584    Use modified_type_die instead.
19585    We keep this code here just in case these types of DIEs may be needed to
19586    represent certain things in other languages (e.g. Pascal) someday.  */
19587
19588 static void
19589 gen_pointer_type_die (tree type, dw_die_ref context_die)
19590 {
19591   dw_die_ref ptr_die
19592     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19593
19594   equate_type_number_to_die (type, ptr_die);
19595   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19596   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19597 }
19598
19599 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19600    Use modified_type_die instead.
19601    We keep this code here just in case these types of DIEs may be needed to
19602    represent certain things in other languages (e.g. Pascal) someday.  */
19603
19604 static void
19605 gen_reference_type_die (tree type, dw_die_ref context_die)
19606 {
19607   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19608
19609   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19610     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19611   else
19612     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19613
19614   equate_type_number_to_die (type, ref_die);
19615   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19616   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19617 }
19618 #endif
19619
19620 /* Generate a DIE for a pointer to a member type.  */
19621
19622 static void
19623 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19624 {
19625   dw_die_ref ptr_die
19626     = new_die (DW_TAG_ptr_to_member_type,
19627                scope_die_for (type, context_die), type);
19628
19629   equate_type_number_to_die (type, ptr_die);
19630   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19631                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19632   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19633 }
19634
19635 /* Generate the DIE for the compilation unit.  */
19636
19637 static dw_die_ref
19638 gen_compile_unit_die (const char *filename)
19639 {
19640   dw_die_ref die;
19641   char producer[250];
19642   const char *language_string = lang_hooks.name;
19643   int language;
19644
19645   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19646
19647   if (filename)
19648     {
19649       add_name_attribute (die, filename);
19650       /* Don't add cwd for <built-in>.  */
19651       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19652         add_comp_dir_attribute (die);
19653     }
19654
19655   sprintf (producer, "%s %s", language_string, version_string);
19656
19657 #ifdef MIPS_DEBUGGING_INFO
19658   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19659      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19660      not appear in the producer string, the debugger reaches the conclusion
19661      that the object file is stripped and has no debugging information.
19662      To get the MIPS/SGI debugger to believe that there is debugging
19663      information in the object file, we add a -g to the producer string.  */
19664   if (debug_info_level > DINFO_LEVEL_TERSE)
19665     strcat (producer, " -g");
19666 #endif
19667
19668   add_AT_string (die, DW_AT_producer, producer);
19669
19670   /* If our producer is LTO try to figure out a common language to use
19671      from the global list of translation units.  */
19672   if (strcmp (language_string, "GNU GIMPLE") == 0)
19673     {
19674       unsigned i;
19675       tree t;
19676       const char *common_lang = NULL;
19677
19678       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
19679         {
19680           if (!TRANSLATION_UNIT_LANGUAGE (t))
19681             continue;
19682           if (!common_lang)
19683             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19684           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19685             ;
19686           else if (strncmp (common_lang, "GNU C", 5) == 0
19687                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19688             /* Mixing C and C++ is ok, use C++ in that case.  */
19689             common_lang = "GNU C++";
19690           else
19691             {
19692               /* Fall back to C.  */
19693               common_lang = NULL;
19694               break;
19695             }
19696         }
19697
19698       if (common_lang)
19699         language_string = common_lang;
19700     }
19701
19702   language = DW_LANG_C89;
19703   if (strcmp (language_string, "GNU C++") == 0)
19704     language = DW_LANG_C_plus_plus;
19705   else if (strcmp (language_string, "GNU F77") == 0)
19706     language = DW_LANG_Fortran77;
19707   else if (strcmp (language_string, "GNU Pascal") == 0)
19708     language = DW_LANG_Pascal83;
19709   else if (dwarf_version >= 3 || !dwarf_strict)
19710     {
19711       if (strcmp (language_string, "GNU Ada") == 0)
19712         language = DW_LANG_Ada95;
19713       else if (strcmp (language_string, "GNU Fortran") == 0)
19714         language = DW_LANG_Fortran95;
19715       else if (strcmp (language_string, "GNU Java") == 0)
19716         language = DW_LANG_Java;
19717       else if (strcmp (language_string, "GNU Objective-C") == 0)
19718         language = DW_LANG_ObjC;
19719       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19720         language = DW_LANG_ObjC_plus_plus;
19721     }
19722
19723   add_AT_unsigned (die, DW_AT_language, language);
19724
19725   switch (language)
19726     {
19727     case DW_LANG_Fortran77:
19728     case DW_LANG_Fortran90:
19729     case DW_LANG_Fortran95:
19730       /* Fortran has case insensitive identifiers and the front-end
19731          lowercases everything.  */
19732       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19733       break;
19734     default:
19735       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19736       break;
19737     }
19738   return die;
19739 }
19740
19741 /* Generate the DIE for a base class.  */
19742
19743 static void
19744 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19745 {
19746   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19747
19748   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19749   add_data_member_location_attribute (die, binfo);
19750
19751   if (BINFO_VIRTUAL_P (binfo))
19752     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19753
19754   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19755      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19756      the default has always been DW_ACCESS_private.  */
19757   if (access == access_public_node)
19758     {
19759       if (dwarf_version == 2
19760           || context_die->die_tag == DW_TAG_class_type)
19761       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19762     }
19763   else if (access == access_protected_node)
19764     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19765   else if (dwarf_version > 2
19766            && context_die->die_tag != DW_TAG_class_type)
19767     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19768 }
19769
19770 /* Generate a DIE for a class member.  */
19771
19772 static void
19773 gen_member_die (tree type, dw_die_ref context_die)
19774 {
19775   tree member;
19776   tree binfo = TYPE_BINFO (type);
19777   dw_die_ref child;
19778
19779   /* If this is not an incomplete type, output descriptions of each of its
19780      members. Note that as we output the DIEs necessary to represent the
19781      members of this record or union type, we will also be trying to output
19782      DIEs to represent the *types* of those members. However the `type'
19783      function (above) will specifically avoid generating type DIEs for member
19784      types *within* the list of member DIEs for this (containing) type except
19785      for those types (of members) which are explicitly marked as also being
19786      members of this (containing) type themselves.  The g++ front- end can
19787      force any given type to be treated as a member of some other (containing)
19788      type by setting the TYPE_CONTEXT of the given (member) type to point to
19789      the TREE node representing the appropriate (containing) type.  */
19790
19791   /* First output info about the base classes.  */
19792   if (binfo)
19793     {
19794       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19795       int i;
19796       tree base;
19797
19798       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19799         gen_inheritance_die (base,
19800                              (accesses ? VEC_index (tree, accesses, i)
19801                               : access_public_node), context_die);
19802     }
19803
19804   /* Now output info about the data members and type members.  */
19805   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19806     {
19807       /* If we thought we were generating minimal debug info for TYPE
19808          and then changed our minds, some of the member declarations
19809          may have already been defined.  Don't define them again, but
19810          do put them in the right order.  */
19811
19812       child = lookup_decl_die (member);
19813       if (child)
19814         splice_child_die (context_die, child);
19815       else
19816         gen_decl_die (member, NULL, context_die);
19817     }
19818
19819   /* Now output info about the function members (if any).  */
19820   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19821     {
19822       /* Don't include clones in the member list.  */
19823       if (DECL_ABSTRACT_ORIGIN (member))
19824         continue;
19825
19826       child = lookup_decl_die (member);
19827       if (child)
19828         splice_child_die (context_die, child);
19829       else
19830         gen_decl_die (member, NULL, context_die);
19831     }
19832 }
19833
19834 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19835    is set, we pretend that the type was never defined, so we only get the
19836    member DIEs needed by later specification DIEs.  */
19837
19838 static void
19839 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19840                                 enum debug_info_usage usage)
19841 {
19842   dw_die_ref type_die = lookup_type_die (type);
19843   dw_die_ref scope_die = 0;
19844   int nested = 0;
19845   int complete = (TYPE_SIZE (type)
19846                   && (! TYPE_STUB_DECL (type)
19847                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19848   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19849   complete = complete && should_emit_struct_debug (type, usage);
19850
19851   if (type_die && ! complete)
19852     return;
19853
19854   if (TYPE_CONTEXT (type) != NULL_TREE
19855       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19856           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19857     nested = 1;
19858
19859   scope_die = scope_die_for (type, context_die);
19860
19861   if (! type_die || (nested && is_cu_die (scope_die)))
19862     /* First occurrence of type or toplevel definition of nested class.  */
19863     {
19864       dw_die_ref old_die = type_die;
19865
19866       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19867                           ? record_type_tag (type) : DW_TAG_union_type,
19868                           scope_die, type);
19869       equate_type_number_to_die (type, type_die);
19870       if (old_die)
19871         add_AT_specification (type_die, old_die);
19872       else
19873         add_name_attribute (type_die, type_tag (type));
19874     }
19875   else
19876     remove_AT (type_die, DW_AT_declaration);
19877
19878   /* Generate child dies for template paramaters.  */
19879   if (debug_info_level > DINFO_LEVEL_TERSE
19880       && COMPLETE_TYPE_P (type))
19881     gen_generic_params_dies (type);
19882
19883   /* If this type has been completed, then give it a byte_size attribute and
19884      then give a list of members.  */
19885   if (complete && !ns_decl)
19886     {
19887       /* Prevent infinite recursion in cases where the type of some member of
19888          this type is expressed in terms of this type itself.  */
19889       TREE_ASM_WRITTEN (type) = 1;
19890       add_byte_size_attribute (type_die, type);
19891       if (TYPE_STUB_DECL (type) != NULL_TREE)
19892         {
19893           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19894           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19895         }
19896
19897       /* If the first reference to this type was as the return type of an
19898          inline function, then it may not have a parent.  Fix this now.  */
19899       if (type_die->die_parent == NULL)
19900         add_child_die (scope_die, type_die);
19901
19902       push_decl_scope (type);
19903       gen_member_die (type, type_die);
19904       pop_decl_scope ();
19905
19906       /* GNU extension: Record what type our vtable lives in.  */
19907       if (TYPE_VFIELD (type))
19908         {
19909           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19910
19911           gen_type_die (vtype, context_die);
19912           add_AT_die_ref (type_die, DW_AT_containing_type,
19913                           lookup_type_die (vtype));
19914         }
19915     }
19916   else
19917     {
19918       add_AT_flag (type_die, DW_AT_declaration, 1);
19919
19920       /* We don't need to do this for function-local types.  */
19921       if (TYPE_STUB_DECL (type)
19922           && ! decl_function_context (TYPE_STUB_DECL (type)))
19923         VEC_safe_push (tree, gc, incomplete_types, type);
19924     }
19925
19926   if (get_AT (type_die, DW_AT_name))
19927     add_pubtype (type, type_die);
19928 }
19929
19930 /* Generate a DIE for a subroutine _type_.  */
19931
19932 static void
19933 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19934 {
19935   tree return_type = TREE_TYPE (type);
19936   dw_die_ref subr_die
19937     = new_die (DW_TAG_subroutine_type,
19938                scope_die_for (type, context_die), type);
19939
19940   equate_type_number_to_die (type, subr_die);
19941   add_prototyped_attribute (subr_die, type);
19942   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19943   gen_formal_types_die (type, subr_die);
19944
19945   if (get_AT (subr_die, DW_AT_name))
19946     add_pubtype (type, subr_die);
19947 }
19948
19949 /* Generate a DIE for a type definition.  */
19950
19951 static void
19952 gen_typedef_die (tree decl, dw_die_ref context_die)
19953 {
19954   dw_die_ref type_die;
19955   tree origin;
19956
19957   if (TREE_ASM_WRITTEN (decl))
19958     return;
19959
19960   TREE_ASM_WRITTEN (decl) = 1;
19961   type_die = new_die (DW_TAG_typedef, context_die, decl);
19962   origin = decl_ultimate_origin (decl);
19963   if (origin != NULL)
19964     add_abstract_origin_attribute (type_die, origin);
19965   else
19966     {
19967       tree type;
19968
19969       add_name_and_src_coords_attributes (type_die, decl);
19970       if (DECL_ORIGINAL_TYPE (decl))
19971         {
19972           type = DECL_ORIGINAL_TYPE (decl);
19973
19974           gcc_assert (type != TREE_TYPE (decl));
19975           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19976         }
19977       else
19978         {
19979           type = TREE_TYPE (decl);
19980
19981           if (is_naming_typedef_decl (TYPE_NAME (type)))
19982             {
19983               /* Here, we are in the case of decl being a typedef naming
19984                  an anonymous type, e.g:
19985                      typedef struct {...} foo;
19986                  In that case TREE_TYPE (decl) is not a typedef variant
19987                  type and TYPE_NAME of the anonymous type is set to the
19988                  TYPE_DECL of the typedef. This construct is emitted by
19989                  the C++ FE.
19990
19991                  TYPE is the anonymous struct named by the typedef
19992                  DECL. As we need the DW_AT_type attribute of the
19993                  DW_TAG_typedef to point to the DIE of TYPE, let's
19994                  generate that DIE right away. add_type_attribute
19995                  called below will then pick (via lookup_type_die) that
19996                  anonymous struct DIE.  */
19997               if (!TREE_ASM_WRITTEN (type))
19998                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19999             }
20000         }
20001
20002       add_type_attribute (type_die, type, TREE_READONLY (decl),
20003                           TREE_THIS_VOLATILE (decl), context_die);
20004
20005       if (is_naming_typedef_decl (decl))
20006         /* We want that all subsequent calls to lookup_type_die with
20007            TYPE in argument yield the DW_TAG_typedef we have just
20008            created.  */
20009         equate_type_number_to_die (type, type_die);
20010
20011       add_accessibility_attribute (type_die, decl);
20012     }
20013
20014   if (DECL_ABSTRACT (decl))
20015     equate_decl_number_to_die (decl, type_die);
20016
20017   if (get_AT (type_die, DW_AT_name))
20018     add_pubtype (decl, type_die);
20019 }
20020
20021 /* Generate a DIE for a struct, class, enum or union type.  */
20022
20023 static void
20024 gen_tagged_type_die (tree type,
20025                      dw_die_ref context_die,
20026                      enum debug_info_usage usage)
20027 {
20028   int need_pop;
20029
20030   if (type == NULL_TREE
20031       || !is_tagged_type (type))
20032     return;
20033
20034   /* If this is a nested type whose containing class hasn't been written
20035      out yet, writing it out will cover this one, too.  This does not apply
20036      to instantiations of member class templates; they need to be added to
20037      the containing class as they are generated.  FIXME: This hurts the
20038      idea of combining type decls from multiple TUs, since we can't predict
20039      what set of template instantiations we'll get.  */
20040   if (TYPE_CONTEXT (type)
20041       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20042       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20043     {
20044       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20045
20046       if (TREE_ASM_WRITTEN (type))
20047         return;
20048
20049       /* If that failed, attach ourselves to the stub.  */
20050       push_decl_scope (TYPE_CONTEXT (type));
20051       context_die = lookup_type_die (TYPE_CONTEXT (type));
20052       need_pop = 1;
20053     }
20054   else if (TYPE_CONTEXT (type) != NULL_TREE
20055            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20056     {
20057       /* If this type is local to a function that hasn't been written
20058          out yet, use a NULL context for now; it will be fixed up in
20059          decls_for_scope.  */
20060       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20061       need_pop = 0;
20062     }
20063   else
20064     {
20065       context_die = declare_in_namespace (type, context_die);
20066       need_pop = 0;
20067     }
20068
20069   if (TREE_CODE (type) == ENUMERAL_TYPE)
20070     {
20071       /* This might have been written out by the call to
20072          declare_in_namespace.  */
20073       if (!TREE_ASM_WRITTEN (type))
20074         gen_enumeration_type_die (type, context_die);
20075     }
20076   else
20077     gen_struct_or_union_type_die (type, context_die, usage);
20078
20079   if (need_pop)
20080     pop_decl_scope ();
20081
20082   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20083      it up if it is ever completed.  gen_*_type_die will set it for us
20084      when appropriate.  */
20085 }
20086
20087 /* Generate a type description DIE.  */
20088
20089 static void
20090 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20091                                 enum debug_info_usage usage)
20092 {
20093   struct array_descr_info info;
20094
20095   if (type == NULL_TREE || type == error_mark_node)
20096     return;
20097
20098   /* If TYPE is a typedef type variant, let's generate debug info
20099      for the parent typedef which TYPE is a type of.  */
20100   if (typedef_variant_p (type))
20101     {
20102       if (TREE_ASM_WRITTEN (type))
20103         return;
20104
20105       /* Prevent broken recursion; we can't hand off to the same type.  */
20106       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20107
20108       /* Use the DIE of the containing namespace as the parent DIE of
20109          the type description DIE we want to generate.  */
20110       if (DECL_CONTEXT (TYPE_NAME (type))
20111           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20112         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20113
20114       TREE_ASM_WRITTEN (type) = 1;
20115
20116       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20117       return;
20118     }
20119
20120   /* If type is an anonymous tagged type named by a typedef, let's
20121      generate debug info for the typedef.  */
20122   if (is_naming_typedef_decl (TYPE_NAME (type)))
20123     {
20124       /* Use the DIE of the containing namespace as the parent DIE of
20125          the type description DIE we want to generate.  */
20126       if (DECL_CONTEXT (TYPE_NAME (type))
20127           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20128         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20129       
20130       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20131       return;
20132     }
20133
20134   /* If this is an array type with hidden descriptor, handle it first.  */
20135   if (!TREE_ASM_WRITTEN (type)
20136       && lang_hooks.types.get_array_descr_info
20137       && lang_hooks.types.get_array_descr_info (type, &info)
20138       && (dwarf_version >= 3 || !dwarf_strict))
20139     {
20140       gen_descr_array_type_die (type, &info, context_die);
20141       TREE_ASM_WRITTEN (type) = 1;
20142       return;
20143     }
20144
20145   /* We are going to output a DIE to represent the unqualified version
20146      of this type (i.e. without any const or volatile qualifiers) so
20147      get the main variant (i.e. the unqualified version) of this type
20148      now.  (Vectors are special because the debugging info is in the
20149      cloned type itself).  */
20150   if (TREE_CODE (type) != VECTOR_TYPE)
20151     type = type_main_variant (type);
20152
20153   if (TREE_ASM_WRITTEN (type))
20154     return;
20155
20156   switch (TREE_CODE (type))
20157     {
20158     case ERROR_MARK:
20159       break;
20160
20161     case POINTER_TYPE:
20162     case REFERENCE_TYPE:
20163       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20164          ensures that the gen_type_die recursion will terminate even if the
20165          type is recursive.  Recursive types are possible in Ada.  */
20166       /* ??? We could perhaps do this for all types before the switch
20167          statement.  */
20168       TREE_ASM_WRITTEN (type) = 1;
20169
20170       /* For these types, all that is required is that we output a DIE (or a
20171          set of DIEs) to represent the "basis" type.  */
20172       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20173                                 DINFO_USAGE_IND_USE);
20174       break;
20175
20176     case OFFSET_TYPE:
20177       /* This code is used for C++ pointer-to-data-member types.
20178          Output a description of the relevant class type.  */
20179       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20180                                         DINFO_USAGE_IND_USE);
20181
20182       /* Output a description of the type of the object pointed to.  */
20183       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20184                                         DINFO_USAGE_IND_USE);
20185
20186       /* Now output a DIE to represent this pointer-to-data-member type
20187          itself.  */
20188       gen_ptr_to_mbr_type_die (type, context_die);
20189       break;
20190
20191     case FUNCTION_TYPE:
20192       /* Force out return type (in case it wasn't forced out already).  */
20193       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20194                                         DINFO_USAGE_DIR_USE);
20195       gen_subroutine_type_die (type, context_die);
20196       break;
20197
20198     case METHOD_TYPE:
20199       /* Force out return type (in case it wasn't forced out already).  */
20200       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20201                                         DINFO_USAGE_DIR_USE);
20202       gen_subroutine_type_die (type, context_die);
20203       break;
20204
20205     case ARRAY_TYPE:
20206       gen_array_type_die (type, context_die);
20207       break;
20208
20209     case VECTOR_TYPE:
20210       gen_array_type_die (type, context_die);
20211       break;
20212
20213     case ENUMERAL_TYPE:
20214     case RECORD_TYPE:
20215     case UNION_TYPE:
20216     case QUAL_UNION_TYPE:
20217       gen_tagged_type_die (type, context_die, usage);
20218       return;
20219
20220     case VOID_TYPE:
20221     case INTEGER_TYPE:
20222     case REAL_TYPE:
20223     case FIXED_POINT_TYPE:
20224     case COMPLEX_TYPE:
20225     case BOOLEAN_TYPE:
20226       /* No DIEs needed for fundamental types.  */
20227       break;
20228
20229     case NULLPTR_TYPE:
20230     case LANG_TYPE:
20231       /* Just use DW_TAG_unspecified_type.  */
20232       {
20233         dw_die_ref type_die = lookup_type_die (type);
20234         if (type_die == NULL)
20235           {
20236             tree name = TYPE_NAME (type);
20237             if (TREE_CODE (name) == TYPE_DECL)
20238               name = DECL_NAME (name);
20239             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20240             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20241             equate_type_number_to_die (type, type_die);
20242           }
20243       }
20244       break;
20245
20246     default:
20247       gcc_unreachable ();
20248     }
20249
20250   TREE_ASM_WRITTEN (type) = 1;
20251 }
20252
20253 static void
20254 gen_type_die (tree type, dw_die_ref context_die)
20255 {
20256   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20257 }
20258
20259 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20260    things which are local to the given block.  */
20261
20262 static void
20263 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20264 {
20265   int must_output_die = 0;
20266   bool inlined_func;
20267
20268   /* Ignore blocks that are NULL.  */
20269   if (stmt == NULL_TREE)
20270     return;
20271
20272   inlined_func = inlined_function_outer_scope_p (stmt);
20273
20274   /* If the block is one fragment of a non-contiguous block, do not
20275      process the variables, since they will have been done by the
20276      origin block.  Do process subblocks.  */
20277   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20278     {
20279       tree sub;
20280
20281       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20282         gen_block_die (sub, context_die, depth + 1);
20283
20284       return;
20285     }
20286
20287   /* Determine if we need to output any Dwarf DIEs at all to represent this
20288      block.  */
20289   if (inlined_func)
20290     /* The outer scopes for inlinings *must* always be represented.  We
20291        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20292     must_output_die = 1;
20293   else
20294     {
20295       /* Determine if this block directly contains any "significant"
20296          local declarations which we will need to output DIEs for.  */
20297       if (debug_info_level > DINFO_LEVEL_TERSE)
20298         /* We are not in terse mode so *any* local declaration counts
20299            as being a "significant" one.  */
20300         must_output_die = ((BLOCK_VARS (stmt) != NULL
20301                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20302                            && (TREE_USED (stmt)
20303                                || TREE_ASM_WRITTEN (stmt)
20304                                || BLOCK_ABSTRACT (stmt)));
20305       else if ((TREE_USED (stmt)
20306                 || TREE_ASM_WRITTEN (stmt)
20307                 || BLOCK_ABSTRACT (stmt))
20308                && !dwarf2out_ignore_block (stmt))
20309         must_output_die = 1;
20310     }
20311
20312   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20313      DIE for any block which contains no significant local declarations at
20314      all.  Rather, in such cases we just call `decls_for_scope' so that any
20315      needed Dwarf info for any sub-blocks will get properly generated. Note
20316      that in terse mode, our definition of what constitutes a "significant"
20317      local declaration gets restricted to include only inlined function
20318      instances and local (nested) function definitions.  */
20319   if (must_output_die)
20320     {
20321       if (inlined_func)
20322         {
20323           /* If STMT block is abstract, that means we have been called
20324              indirectly from dwarf2out_abstract_function.
20325              That function rightfully marks the descendent blocks (of
20326              the abstract function it is dealing with) as being abstract,
20327              precisely to prevent us from emitting any
20328              DW_TAG_inlined_subroutine DIE as a descendent
20329              of an abstract function instance. So in that case, we should
20330              not call gen_inlined_subroutine_die.
20331
20332              Later though, when cgraph asks dwarf2out to emit info
20333              for the concrete instance of the function decl into which
20334              the concrete instance of STMT got inlined, the later will lead
20335              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20336           if (! BLOCK_ABSTRACT (stmt))
20337             gen_inlined_subroutine_die (stmt, context_die, depth);
20338         }
20339       else
20340         gen_lexical_block_die (stmt, context_die, depth);
20341     }
20342   else
20343     decls_for_scope (stmt, context_die, depth);
20344 }
20345
20346 /* Process variable DECL (or variable with origin ORIGIN) within
20347    block STMT and add it to CONTEXT_DIE.  */
20348 static void
20349 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20350 {
20351   dw_die_ref die;
20352   tree decl_or_origin = decl ? decl : origin;
20353
20354   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20355     die = lookup_decl_die (decl_or_origin);
20356   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20357            && TYPE_DECL_IS_STUB (decl_or_origin))
20358     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20359   else
20360     die = NULL;
20361
20362   if (die != NULL && die->die_parent == NULL)
20363     add_child_die (context_die, die);
20364   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20365     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20366                                          stmt, context_die);
20367   else
20368     gen_decl_die (decl, origin, context_die);
20369 }
20370
20371 /* Generate all of the decls declared within a given scope and (recursively)
20372    all of its sub-blocks.  */
20373
20374 static void
20375 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20376 {
20377   tree decl;
20378   unsigned int i;
20379   tree subblocks;
20380
20381   /* Ignore NULL blocks.  */
20382   if (stmt == NULL_TREE)
20383     return;
20384
20385   /* Output the DIEs to represent all of the data objects and typedefs
20386      declared directly within this block but not within any nested
20387      sub-blocks.  Also, nested function and tag DIEs have been
20388      generated with a parent of NULL; fix that up now.  */
20389   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20390     process_scope_var (stmt, decl, NULL_TREE, context_die);
20391   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20392     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20393                        context_die);
20394
20395   /* If we're at -g1, we're not interested in subblocks.  */
20396   if (debug_info_level <= DINFO_LEVEL_TERSE)
20397     return;
20398
20399   /* Output the DIEs to represent all sub-blocks (and the items declared
20400      therein) of this block.  */
20401   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20402        subblocks != NULL;
20403        subblocks = BLOCK_CHAIN (subblocks))
20404     gen_block_die (subblocks, context_die, depth + 1);
20405 }
20406
20407 /* Is this a typedef we can avoid emitting?  */
20408
20409 static inline int
20410 is_redundant_typedef (const_tree decl)
20411 {
20412   if (TYPE_DECL_IS_STUB (decl))
20413     return 1;
20414
20415   if (DECL_ARTIFICIAL (decl)
20416       && DECL_CONTEXT (decl)
20417       && is_tagged_type (DECL_CONTEXT (decl))
20418       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20419       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20420     /* Also ignore the artificial member typedef for the class name.  */
20421     return 1;
20422
20423   return 0;
20424 }
20425
20426 /* Return TRUE if TYPE is a typedef that names a type for linkage
20427    purposes. This kind of typedefs is produced by the C++ FE for
20428    constructs like:
20429
20430    typedef struct {...} foo;
20431
20432    In that case, there is no typedef variant type produced for foo.
20433    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20434    struct type.  */
20435
20436 static bool
20437 is_naming_typedef_decl (const_tree decl)
20438 {
20439   if (decl == NULL_TREE
20440       || TREE_CODE (decl) != TYPE_DECL
20441       || !is_tagged_type (TREE_TYPE (decl))
20442       || DECL_IS_BUILTIN (decl)
20443       || is_redundant_typedef (decl)
20444       /* It looks like Ada produces TYPE_DECLs that are very similar
20445          to C++ naming typedefs but that have different
20446          semantics. Let's be specific to c++ for now.  */
20447       || !is_cxx ())
20448     return FALSE;
20449
20450   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20451           && TYPE_NAME (TREE_TYPE (decl)) == decl
20452           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20453               != TYPE_NAME (TREE_TYPE (decl))));
20454 }
20455
20456 /* Returns the DIE for a context.  */
20457
20458 static inline dw_die_ref
20459 get_context_die (tree context)
20460 {
20461   if (context)
20462     {
20463       /* Find die that represents this context.  */
20464       if (TYPE_P (context))
20465         return force_type_die (TYPE_MAIN_VARIANT (context));
20466       else
20467         return force_decl_die (context);
20468     }
20469   return comp_unit_die ();
20470 }
20471
20472 /* Returns the DIE for decl.  A DIE will always be returned.  */
20473
20474 static dw_die_ref
20475 force_decl_die (tree decl)
20476 {
20477   dw_die_ref decl_die;
20478   unsigned saved_external_flag;
20479   tree save_fn = NULL_TREE;
20480   decl_die = lookup_decl_die (decl);
20481   if (!decl_die)
20482     {
20483       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20484
20485       decl_die = lookup_decl_die (decl);
20486       if (decl_die)
20487         return decl_die;
20488
20489       switch (TREE_CODE (decl))
20490         {
20491         case FUNCTION_DECL:
20492           /* Clear current_function_decl, so that gen_subprogram_die thinks
20493              that this is a declaration. At this point, we just want to force
20494              declaration die.  */
20495           save_fn = current_function_decl;
20496           current_function_decl = NULL_TREE;
20497           gen_subprogram_die (decl, context_die);
20498           current_function_decl = save_fn;
20499           break;
20500
20501         case VAR_DECL:
20502           /* Set external flag to force declaration die. Restore it after
20503            gen_decl_die() call.  */
20504           saved_external_flag = DECL_EXTERNAL (decl);
20505           DECL_EXTERNAL (decl) = 1;
20506           gen_decl_die (decl, NULL, context_die);
20507           DECL_EXTERNAL (decl) = saved_external_flag;
20508           break;
20509
20510         case NAMESPACE_DECL:
20511           if (dwarf_version >= 3 || !dwarf_strict)
20512             dwarf2out_decl (decl);
20513           else
20514             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20515             decl_die = comp_unit_die ();
20516           break;
20517
20518         case TRANSLATION_UNIT_DECL:
20519           decl_die = comp_unit_die ();
20520           break;
20521
20522         default:
20523           gcc_unreachable ();
20524         }
20525
20526       /* We should be able to find the DIE now.  */
20527       if (!decl_die)
20528         decl_die = lookup_decl_die (decl);
20529       gcc_assert (decl_die);
20530     }
20531
20532   return decl_die;
20533 }
20534
20535 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20536    always returned.  */
20537
20538 static dw_die_ref
20539 force_type_die (tree type)
20540 {
20541   dw_die_ref type_die;
20542
20543   type_die = lookup_type_die (type);
20544   if (!type_die)
20545     {
20546       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20547
20548       type_die = modified_type_die (type, TYPE_READONLY (type),
20549                                     TYPE_VOLATILE (type), context_die);
20550       gcc_assert (type_die);
20551     }
20552   return type_die;
20553 }
20554
20555 /* Force out any required namespaces to be able to output DECL,
20556    and return the new context_die for it, if it's changed.  */
20557
20558 static dw_die_ref
20559 setup_namespace_context (tree thing, dw_die_ref context_die)
20560 {
20561   tree context = (DECL_P (thing)
20562                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20563   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20564     /* Force out the namespace.  */
20565     context_die = force_decl_die (context);
20566
20567   return context_die;
20568 }
20569
20570 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20571    type) within its namespace, if appropriate.
20572
20573    For compatibility with older debuggers, namespace DIEs only contain
20574    declarations; all definitions are emitted at CU scope.  */
20575
20576 static dw_die_ref
20577 declare_in_namespace (tree thing, dw_die_ref context_die)
20578 {
20579   dw_die_ref ns_context;
20580
20581   if (debug_info_level <= DINFO_LEVEL_TERSE)
20582     return context_die;
20583
20584   /* If this decl is from an inlined function, then don't try to emit it in its
20585      namespace, as we will get confused.  It would have already been emitted
20586      when the abstract instance of the inline function was emitted anyways.  */
20587   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20588     return context_die;
20589
20590   ns_context = setup_namespace_context (thing, context_die);
20591
20592   if (ns_context != context_die)
20593     {
20594       if (is_fortran ())
20595         return ns_context;
20596       if (DECL_P (thing))
20597         gen_decl_die (thing, NULL, ns_context);
20598       else
20599         gen_type_die (thing, ns_context);
20600     }
20601   return context_die;
20602 }
20603
20604 /* Generate a DIE for a namespace or namespace alias.  */
20605
20606 static void
20607 gen_namespace_die (tree decl, dw_die_ref context_die)
20608 {
20609   dw_die_ref namespace_die;
20610
20611   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20612      they are an alias of.  */
20613   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20614     {
20615       /* Output a real namespace or module.  */
20616       context_die = setup_namespace_context (decl, comp_unit_die ());
20617       namespace_die = new_die (is_fortran ()
20618                                ? DW_TAG_module : DW_TAG_namespace,
20619                                context_die, decl);
20620       /* For Fortran modules defined in different CU don't add src coords.  */
20621       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20622         {
20623           const char *name = dwarf2_name (decl, 0);
20624           if (name)
20625             add_name_attribute (namespace_die, name);
20626         }
20627       else
20628         add_name_and_src_coords_attributes (namespace_die, decl);
20629       if (DECL_EXTERNAL (decl))
20630         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20631       equate_decl_number_to_die (decl, namespace_die);
20632     }
20633   else
20634     {
20635       /* Output a namespace alias.  */
20636
20637       /* Force out the namespace we are an alias of, if necessary.  */
20638       dw_die_ref origin_die
20639         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20640
20641       if (DECL_FILE_SCOPE_P (decl)
20642           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20643         context_die = setup_namespace_context (decl, comp_unit_die ());
20644       /* Now create the namespace alias DIE.  */
20645       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20646       add_name_and_src_coords_attributes (namespace_die, decl);
20647       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20648       equate_decl_number_to_die (decl, namespace_die);
20649     }
20650 }
20651
20652 /* Generate Dwarf debug information for a decl described by DECL.
20653    The return value is currently only meaningful for PARM_DECLs,
20654    for all other decls it returns NULL.  */
20655
20656 static dw_die_ref
20657 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20658 {
20659   tree decl_or_origin = decl ? decl : origin;
20660   tree class_origin = NULL, ultimate_origin;
20661
20662   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20663     return NULL;
20664
20665   switch (TREE_CODE (decl_or_origin))
20666     {
20667     case ERROR_MARK:
20668       break;
20669
20670     case CONST_DECL:
20671       if (!is_fortran () && !is_ada ())
20672         {
20673           /* The individual enumerators of an enum type get output when we output
20674              the Dwarf representation of the relevant enum type itself.  */
20675           break;
20676         }
20677
20678       /* Emit its type.  */
20679       gen_type_die (TREE_TYPE (decl), context_die);
20680
20681       /* And its containing namespace.  */
20682       context_die = declare_in_namespace (decl, context_die);
20683
20684       gen_const_die (decl, context_die);
20685       break;
20686
20687     case FUNCTION_DECL:
20688       /* Don't output any DIEs to represent mere function declarations,
20689          unless they are class members or explicit block externs.  */
20690       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20691           && DECL_FILE_SCOPE_P (decl_or_origin)
20692           && (current_function_decl == NULL_TREE
20693               || DECL_ARTIFICIAL (decl_or_origin)))
20694         break;
20695
20696 #if 0
20697       /* FIXME */
20698       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20699          on local redeclarations of global functions.  That seems broken.  */
20700       if (current_function_decl != decl)
20701         /* This is only a declaration.  */;
20702 #endif
20703
20704       /* If we're emitting a clone, emit info for the abstract instance.  */
20705       if (origin || DECL_ORIGIN (decl) != decl)
20706         dwarf2out_abstract_function (origin
20707                                      ? DECL_ORIGIN (origin)
20708                                      : DECL_ABSTRACT_ORIGIN (decl));
20709
20710       /* If we're emitting an out-of-line copy of an inline function,
20711          emit info for the abstract instance and set up to refer to it.  */
20712       else if (cgraph_function_possibly_inlined_p (decl)
20713                && ! DECL_ABSTRACT (decl)
20714                && ! class_or_namespace_scope_p (context_die)
20715                /* dwarf2out_abstract_function won't emit a die if this is just
20716                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20717                   that case, because that works only if we have a die.  */
20718                && DECL_INITIAL (decl) != NULL_TREE)
20719         {
20720           dwarf2out_abstract_function (decl);
20721           set_decl_origin_self (decl);
20722         }
20723
20724       /* Otherwise we're emitting the primary DIE for this decl.  */
20725       else if (debug_info_level > DINFO_LEVEL_TERSE)
20726         {
20727           /* Before we describe the FUNCTION_DECL itself, make sure that we
20728              have its containing type.  */
20729           if (!origin)
20730             origin = decl_class_context (decl);
20731           if (origin != NULL_TREE)
20732             gen_type_die (origin, context_die);
20733
20734           /* And its return type.  */
20735           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20736
20737           /* And its virtual context.  */
20738           if (DECL_VINDEX (decl) != NULL_TREE)
20739             gen_type_die (DECL_CONTEXT (decl), context_die);
20740
20741           /* Make sure we have a member DIE for decl.  */
20742           if (origin != NULL_TREE)
20743             gen_type_die_for_member (origin, decl, context_die);
20744
20745           /* And its containing namespace.  */
20746           context_die = declare_in_namespace (decl, context_die);
20747         }
20748
20749       /* Now output a DIE to represent the function itself.  */
20750       if (decl)
20751         gen_subprogram_die (decl, context_die);
20752       break;
20753
20754     case TYPE_DECL:
20755       /* If we are in terse mode, don't generate any DIEs to represent any
20756          actual typedefs.  */
20757       if (debug_info_level <= DINFO_LEVEL_TERSE)
20758         break;
20759
20760       /* In the special case of a TYPE_DECL node representing the declaration
20761          of some type tag, if the given TYPE_DECL is marked as having been
20762          instantiated from some other (original) TYPE_DECL node (e.g. one which
20763          was generated within the original definition of an inline function) we
20764          used to generate a special (abbreviated) DW_TAG_structure_type,
20765          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20766          should be actually referencing those DIEs, as variable DIEs with that
20767          type would be emitted already in the abstract origin, so it was always
20768          removed during unused type prunning.  Don't add anything in this
20769          case.  */
20770       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20771         break;
20772
20773       if (is_redundant_typedef (decl))
20774         gen_type_die (TREE_TYPE (decl), context_die);
20775       else
20776         /* Output a DIE to represent the typedef itself.  */
20777         gen_typedef_die (decl, context_die);
20778       break;
20779
20780     case LABEL_DECL:
20781       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20782         gen_label_die (decl, context_die);
20783       break;
20784
20785     case VAR_DECL:
20786     case RESULT_DECL:
20787       /* If we are in terse mode, don't generate any DIEs to represent any
20788          variable declarations or definitions.  */
20789       if (debug_info_level <= DINFO_LEVEL_TERSE)
20790         break;
20791
20792       /* Output any DIEs that are needed to specify the type of this data
20793          object.  */
20794       if (decl_by_reference_p (decl_or_origin))
20795         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20796       else
20797         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20798
20799       /* And its containing type.  */
20800       class_origin = decl_class_context (decl_or_origin);
20801       if (class_origin != NULL_TREE)
20802         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20803
20804       /* And its containing namespace.  */
20805       context_die = declare_in_namespace (decl_or_origin, context_die);
20806
20807       /* Now output the DIE to represent the data object itself.  This gets
20808          complicated because of the possibility that the VAR_DECL really
20809          represents an inlined instance of a formal parameter for an inline
20810          function.  */
20811       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20812       if (ultimate_origin != NULL_TREE
20813           && TREE_CODE (ultimate_origin) == PARM_DECL)
20814         gen_formal_parameter_die (decl, origin,
20815                                   true /* Emit name attribute.  */,
20816                                   context_die);
20817       else
20818         gen_variable_die (decl, origin, context_die);
20819       break;
20820
20821     case FIELD_DECL:
20822       /* Ignore the nameless fields that are used to skip bits but handle C++
20823          anonymous unions and structs.  */
20824       if (DECL_NAME (decl) != NULL_TREE
20825           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20826           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20827         {
20828           gen_type_die (member_declared_type (decl), context_die);
20829           gen_field_die (decl, context_die);
20830         }
20831       break;
20832
20833     case PARM_DECL:
20834       if (DECL_BY_REFERENCE (decl_or_origin))
20835         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20836       else
20837         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20838       return gen_formal_parameter_die (decl, origin,
20839                                        true /* Emit name attribute.  */,
20840                                        context_die);
20841
20842     case NAMESPACE_DECL:
20843     case IMPORTED_DECL:
20844       if (dwarf_version >= 3 || !dwarf_strict)
20845         gen_namespace_die (decl, context_die);
20846       break;
20847
20848     default:
20849       /* Probably some frontend-internal decl.  Assume we don't care.  */
20850       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20851       break;
20852     }
20853
20854   return NULL;
20855 }
20856 \f
20857 /* Output debug information for global decl DECL.  Called from toplev.c after
20858    compilation proper has finished.  */
20859
20860 static void
20861 dwarf2out_global_decl (tree decl)
20862 {
20863   /* Output DWARF2 information for file-scope tentative data object
20864      declarations, file-scope (extern) function declarations (which
20865      had no corresponding body) and file-scope tagged type declarations
20866      and definitions which have not yet been forced out.  */
20867   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20868     dwarf2out_decl (decl);
20869 }
20870
20871 /* Output debug information for type decl DECL.  Called from toplev.c
20872    and from language front ends (to record built-in types).  */
20873 static void
20874 dwarf2out_type_decl (tree decl, int local)
20875 {
20876   if (!local)
20877     dwarf2out_decl (decl);
20878 }
20879
20880 /* Output debug information for imported module or decl DECL.
20881    NAME is non-NULL name in the lexical block if the decl has been renamed.
20882    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20883    that DECL belongs to.
20884    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20885 static void
20886 dwarf2out_imported_module_or_decl_1 (tree decl,
20887                                      tree name,
20888                                      tree lexical_block,
20889                                      dw_die_ref lexical_block_die)
20890 {
20891   expanded_location xloc;
20892   dw_die_ref imported_die = NULL;
20893   dw_die_ref at_import_die;
20894
20895   if (TREE_CODE (decl) == IMPORTED_DECL)
20896     {
20897       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20898       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20899       gcc_assert (decl);
20900     }
20901   else
20902     xloc = expand_location (input_location);
20903
20904   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20905     {
20906       at_import_die = force_type_die (TREE_TYPE (decl));
20907       /* For namespace N { typedef void T; } using N::T; base_type_die
20908          returns NULL, but DW_TAG_imported_declaration requires
20909          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20910       if (!at_import_die)
20911         {
20912           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20913           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20914           at_import_die = lookup_type_die (TREE_TYPE (decl));
20915           gcc_assert (at_import_die);
20916         }
20917     }
20918   else
20919     {
20920       at_import_die = lookup_decl_die (decl);
20921       if (!at_import_die)
20922         {
20923           /* If we're trying to avoid duplicate debug info, we may not have
20924              emitted the member decl for this field.  Emit it now.  */
20925           if (TREE_CODE (decl) == FIELD_DECL)
20926             {
20927               tree type = DECL_CONTEXT (decl);
20928
20929               if (TYPE_CONTEXT (type)
20930                   && TYPE_P (TYPE_CONTEXT (type))
20931                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20932                                                 DINFO_USAGE_DIR_USE))
20933                 return;
20934               gen_type_die_for_member (type, decl,
20935                                        get_context_die (TYPE_CONTEXT (type)));
20936             }
20937           at_import_die = force_decl_die (decl);
20938         }
20939     }
20940
20941   if (TREE_CODE (decl) == NAMESPACE_DECL)
20942     {
20943       if (dwarf_version >= 3 || !dwarf_strict)
20944         imported_die = new_die (DW_TAG_imported_module,
20945                                 lexical_block_die,
20946                                 lexical_block);
20947       else
20948         return;
20949     }
20950   else
20951     imported_die = new_die (DW_TAG_imported_declaration,
20952                             lexical_block_die,
20953                             lexical_block);
20954
20955   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20956   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20957   if (name)
20958     add_AT_string (imported_die, DW_AT_name,
20959                    IDENTIFIER_POINTER (name));
20960   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20961 }
20962
20963 /* Output debug information for imported module or decl DECL.
20964    NAME is non-NULL name in context if the decl has been renamed.
20965    CHILD is true if decl is one of the renamed decls as part of
20966    importing whole module.  */
20967
20968 static void
20969 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20970                                    bool child)
20971 {
20972   /* dw_die_ref at_import_die;  */
20973   dw_die_ref scope_die;
20974
20975   if (debug_info_level <= DINFO_LEVEL_TERSE)
20976     return;
20977
20978   gcc_assert (decl);
20979
20980   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20981      We need decl DIE for reference and scope die. First, get DIE for the decl
20982      itself.  */
20983
20984   /* Get the scope die for decl context. Use comp_unit_die for global module
20985      or decl. If die is not found for non globals, force new die.  */
20986   if (context
20987       && TYPE_P (context)
20988       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20989     return;
20990
20991   if (!(dwarf_version >= 3 || !dwarf_strict))
20992     return;
20993
20994   scope_die = get_context_die (context);
20995
20996   if (child)
20997     {
20998       gcc_assert (scope_die->die_child);
20999       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21000       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21001       scope_die = scope_die->die_child;
21002     }
21003
21004   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21005   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21006
21007 }
21008
21009 /* Write the debugging output for DECL.  */
21010
21011 void
21012 dwarf2out_decl (tree decl)
21013 {
21014   dw_die_ref context_die = comp_unit_die ();
21015
21016   switch (TREE_CODE (decl))
21017     {
21018     case ERROR_MARK:
21019       return;
21020
21021     case FUNCTION_DECL:
21022       /* What we would really like to do here is to filter out all mere
21023          file-scope declarations of file-scope functions which are never
21024          referenced later within this translation unit (and keep all of ones
21025          that *are* referenced later on) but we aren't clairvoyant, so we have
21026          no idea which functions will be referenced in the future (i.e. later
21027          on within the current translation unit). So here we just ignore all
21028          file-scope function declarations which are not also definitions.  If
21029          and when the debugger needs to know something about these functions,
21030          it will have to hunt around and find the DWARF information associated
21031          with the definition of the function.
21032
21033          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21034          nodes represent definitions and which ones represent mere
21035          declarations.  We have to check DECL_INITIAL instead. That's because
21036          the C front-end supports some weird semantics for "extern inline"
21037          function definitions.  These can get inlined within the current
21038          translation unit (and thus, we need to generate Dwarf info for their
21039          abstract instances so that the Dwarf info for the concrete inlined
21040          instances can have something to refer to) but the compiler never
21041          generates any out-of-lines instances of such things (despite the fact
21042          that they *are* definitions).
21043
21044          The important point is that the C front-end marks these "extern
21045          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21046          them anyway. Note that the C++ front-end also plays some similar games
21047          for inline function definitions appearing within include files which
21048          also contain `#pragma interface' pragmas.  */
21049       if (DECL_INITIAL (decl) == NULL_TREE)
21050         return;
21051
21052       /* If we're a nested function, initially use a parent of NULL; if we're
21053          a plain function, this will be fixed up in decls_for_scope.  If
21054          we're a method, it will be ignored, since we already have a DIE.  */
21055       if (decl_function_context (decl)
21056           /* But if we're in terse mode, we don't care about scope.  */
21057           && debug_info_level > DINFO_LEVEL_TERSE)
21058         context_die = NULL;
21059       break;
21060
21061     case VAR_DECL:
21062       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21063          declaration and if the declaration was never even referenced from
21064          within this entire compilation unit.  We suppress these DIEs in
21065          order to save space in the .debug section (by eliminating entries
21066          which are probably useless).  Note that we must not suppress
21067          block-local extern declarations (whether used or not) because that
21068          would screw-up the debugger's name lookup mechanism and cause it to
21069          miss things which really ought to be in scope at a given point.  */
21070       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21071         return;
21072
21073       /* For local statics lookup proper context die.  */
21074       if (TREE_STATIC (decl) && decl_function_context (decl))
21075         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21076
21077       /* If we are in terse mode, don't generate any DIEs to represent any
21078          variable declarations or definitions.  */
21079       if (debug_info_level <= DINFO_LEVEL_TERSE)
21080         return;
21081       break;
21082
21083     case CONST_DECL:
21084       if (debug_info_level <= DINFO_LEVEL_TERSE)
21085         return;
21086       if (!is_fortran () && !is_ada ())
21087         return;
21088       if (TREE_STATIC (decl) && decl_function_context (decl))
21089         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21090       break;
21091
21092     case NAMESPACE_DECL:
21093     case IMPORTED_DECL:
21094       if (debug_info_level <= DINFO_LEVEL_TERSE)
21095         return;
21096       if (lookup_decl_die (decl) != NULL)
21097         return;
21098       break;
21099
21100     case TYPE_DECL:
21101       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21102       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21103         return;
21104
21105       /* Don't bother trying to generate any DIEs to represent any of the
21106          normal built-in types for the language we are compiling.  */
21107       if (DECL_IS_BUILTIN (decl))
21108         return;
21109
21110       /* If we are in terse mode, don't generate any DIEs for types.  */
21111       if (debug_info_level <= DINFO_LEVEL_TERSE)
21112         return;
21113
21114       /* If we're a function-scope tag, initially use a parent of NULL;
21115          this will be fixed up in decls_for_scope.  */
21116       if (decl_function_context (decl))
21117         context_die = NULL;
21118
21119       break;
21120
21121     default:
21122       return;
21123     }
21124
21125   gen_decl_die (decl, NULL, context_die);
21126 }
21127
21128 /* Write the debugging output for DECL.  */
21129
21130 static void
21131 dwarf2out_function_decl (tree decl)
21132 {
21133   dwarf2out_decl (decl);
21134
21135   htab_empty (decl_loc_table);
21136 }
21137
21138 /* Output a marker (i.e. a label) for the beginning of the generated code for
21139    a lexical block.  */
21140
21141 static void
21142 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21143                        unsigned int blocknum)
21144 {
21145   switch_to_section (current_function_section ());
21146   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21147 }
21148
21149 /* Output a marker (i.e. a label) for the end of the generated code for a
21150    lexical block.  */
21151
21152 static void
21153 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21154 {
21155   switch_to_section (current_function_section ());
21156   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21157 }
21158
21159 /* Returns nonzero if it is appropriate not to emit any debugging
21160    information for BLOCK, because it doesn't contain any instructions.
21161
21162    Don't allow this for blocks with nested functions or local classes
21163    as we would end up with orphans, and in the presence of scheduling
21164    we may end up calling them anyway.  */
21165
21166 static bool
21167 dwarf2out_ignore_block (const_tree block)
21168 {
21169   tree decl;
21170   unsigned int i;
21171
21172   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21173     if (TREE_CODE (decl) == FUNCTION_DECL
21174         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21175       return 0;
21176   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21177     {
21178       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21179       if (TREE_CODE (decl) == FUNCTION_DECL
21180           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21181       return 0;
21182     }
21183
21184   return 1;
21185 }
21186
21187 /* Hash table routines for file_hash.  */
21188
21189 static int
21190 file_table_eq (const void *p1_p, const void *p2_p)
21191 {
21192   const struct dwarf_file_data *const p1 =
21193     (const struct dwarf_file_data *) p1_p;
21194   const char *const p2 = (const char *) p2_p;
21195   return strcmp (p1->filename, p2) == 0;
21196 }
21197
21198 static hashval_t
21199 file_table_hash (const void *p_p)
21200 {
21201   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21202   return htab_hash_string (p->filename);
21203 }
21204
21205 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21206    dwarf2out.c) and return its "index".  The index of each (known) filename is
21207    just a unique number which is associated with only that one filename.  We
21208    need such numbers for the sake of generating labels (in the .debug_sfnames
21209    section) and references to those files numbers (in the .debug_srcinfo
21210    and.debug_macinfo sections).  If the filename given as an argument is not
21211    found in our current list, add it to the list and assign it the next
21212    available unique index number.  In order to speed up searches, we remember
21213    the index of the filename was looked up last.  This handles the majority of
21214    all searches.  */
21215
21216 static struct dwarf_file_data *
21217 lookup_filename (const char *file_name)
21218 {
21219   void ** slot;
21220   struct dwarf_file_data * created;
21221
21222   /* Check to see if the file name that was searched on the previous
21223      call matches this file name.  If so, return the index.  */
21224   if (file_table_last_lookup
21225       && (file_name == file_table_last_lookup->filename
21226           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21227     return file_table_last_lookup;
21228
21229   /* Didn't match the previous lookup, search the table.  */
21230   slot = htab_find_slot_with_hash (file_table, file_name,
21231                                    htab_hash_string (file_name), INSERT);
21232   if (*slot)
21233     return (struct dwarf_file_data *) *slot;
21234
21235   created = ggc_alloc_dwarf_file_data ();
21236   created->filename = file_name;
21237   created->emitted_number = 0;
21238   *slot = created;
21239   return created;
21240 }
21241
21242 /* If the assembler will construct the file table, then translate the compiler
21243    internal file table number into the assembler file table number, and emit
21244    a .file directive if we haven't already emitted one yet.  The file table
21245    numbers are different because we prune debug info for unused variables and
21246    types, which may include filenames.  */
21247
21248 static int
21249 maybe_emit_file (struct dwarf_file_data * fd)
21250 {
21251   if (! fd->emitted_number)
21252     {
21253       if (last_emitted_file)
21254         fd->emitted_number = last_emitted_file->emitted_number + 1;
21255       else
21256         fd->emitted_number = 1;
21257       last_emitted_file = fd;
21258
21259       if (DWARF2_ASM_LINE_DEBUG_INFO)
21260         {
21261           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21262           output_quoted_string (asm_out_file,
21263                                 remap_debug_filename (fd->filename));
21264           fputc ('\n', asm_out_file);
21265         }
21266     }
21267
21268   return fd->emitted_number;
21269 }
21270
21271 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21272    That generation should happen after function debug info has been
21273    generated. The value of the attribute is the constant value of ARG.  */
21274
21275 static void
21276 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21277 {
21278   die_arg_entry entry;
21279
21280   if (!die || !arg)
21281     return;
21282
21283   if (!tmpl_value_parm_die_table)
21284     tmpl_value_parm_die_table
21285       = VEC_alloc (die_arg_entry, gc, 32);
21286
21287   entry.die = die;
21288   entry.arg = arg;
21289   VEC_safe_push (die_arg_entry, gc,
21290                  tmpl_value_parm_die_table,
21291                  &entry);
21292 }
21293
21294 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21295    by append_entry_to_tmpl_value_parm_die_table. This function must
21296    be called after function DIEs have been generated.  */
21297
21298 static void
21299 gen_remaining_tmpl_value_param_die_attribute (void)
21300 {
21301   if (tmpl_value_parm_die_table)
21302     {
21303       unsigned i;
21304       die_arg_entry *e;
21305
21306       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21307         tree_add_const_value_attribute (e->die, e->arg);
21308     }
21309 }
21310
21311
21312 /* Replace DW_AT_name for the decl with name.  */
21313
21314 static void
21315 dwarf2out_set_name (tree decl, tree name)
21316 {
21317   dw_die_ref die;
21318   dw_attr_ref attr;
21319   const char *dname;
21320
21321   die = TYPE_SYMTAB_DIE (decl);
21322   if (!die)
21323     return;
21324
21325   dname = dwarf2_name (name, 0);
21326   if (!dname)
21327     return;
21328
21329   attr = get_AT (die, DW_AT_name);
21330   if (attr)
21331     {
21332       struct indirect_string_node *node;
21333
21334       node = find_AT_string (dname);
21335       /* replace the string.  */
21336       attr->dw_attr_val.v.val_str = node;
21337     }
21338
21339   else
21340     add_name_attribute (die, dname);
21341 }
21342
21343 /* Called by the final INSN scan whenever we see a direct function call.
21344    Make an entry into the direct call table, recording the point of call
21345    and a reference to the target function's debug entry.  */
21346
21347 static void
21348 dwarf2out_direct_call (tree targ)
21349 {
21350   dcall_entry e;
21351   tree origin = decl_ultimate_origin (targ);
21352
21353   /* If this is a clone, use the abstract origin as the target.  */
21354   if (origin)
21355     targ = origin;
21356
21357   e.poc_label_num = poc_label_num++;
21358   e.poc_decl = current_function_decl;
21359   e.targ_die = force_decl_die (targ);
21360   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21361
21362   /* Drop a label at the return point to mark the point of call.  */
21363   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21364 }
21365
21366 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21367
21368 static hashval_t
21369 vcall_insn_table_hash (const void *x)
21370 {
21371   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21372 }
21373
21374 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21375    insnd_uid of *Y.  */
21376
21377 static int
21378 vcall_insn_table_eq (const void *x, const void *y)
21379 {
21380   return (((const struct vcall_insn *) x)->insn_uid
21381           == ((const struct vcall_insn *) y)->insn_uid);
21382 }
21383
21384 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21385
21386 static void
21387 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21388 {
21389   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21390   struct vcall_insn **slot;
21391
21392   gcc_assert (item);
21393   item->insn_uid = insn_uid;
21394   item->vtable_slot = vtable_slot;
21395   slot = (struct vcall_insn **)
21396       htab_find_slot_with_hash (vcall_insn_table, &item,
21397                                 (hashval_t) insn_uid, INSERT);
21398   *slot = item;
21399 }
21400
21401 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21402
21403 static unsigned int
21404 lookup_vcall_insn (unsigned int insn_uid)
21405 {
21406   struct vcall_insn item;
21407   struct vcall_insn *p;
21408
21409   item.insn_uid = insn_uid;
21410   item.vtable_slot = 0;
21411   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21412                                                  (void *) &item,
21413                                                  (hashval_t) insn_uid);
21414   if (p == NULL)
21415     return (unsigned int) -1;
21416   return p->vtable_slot;
21417 }
21418
21419
21420 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21421    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21422    is the vtable slot index that we will need to put in the virtual call
21423    table later.  */
21424
21425 static void
21426 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21427 {
21428   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21429     {
21430       tree token = OBJ_TYPE_REF_TOKEN (addr);
21431       if (TREE_CODE (token) == INTEGER_CST)
21432         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21433     }
21434 }
21435
21436 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21437    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21438    with NEW_INSN.  */
21439
21440 static void
21441 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21442 {
21443   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21444
21445   if (vtable_slot != (unsigned int) -1)
21446     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21447 }
21448
21449 /* Called by the final INSN scan whenever we see a virtual function call.
21450    Make an entry into the virtual call table, recording the point of call
21451    and the slot index of the vtable entry used to call the virtual member
21452    function.  The slot index was associated with the INSN_UID during the
21453    lowering to RTL.  */
21454
21455 static void
21456 dwarf2out_virtual_call (int insn_uid)
21457 {
21458   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21459   vcall_entry e;
21460
21461   if (vtable_slot == (unsigned int) -1)
21462     return;
21463
21464   e.poc_label_num = poc_label_num++;
21465   e.vtable_slot = vtable_slot;
21466   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21467
21468   /* Drop a label at the return point to mark the point of call.  */
21469   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21470 }
21471
21472 /* Called by the final INSN scan whenever we see a var location.  We
21473    use it to drop labels in the right places, and throw the location in
21474    our lookup table.  */
21475
21476 static void
21477 dwarf2out_var_location (rtx loc_note)
21478 {
21479   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21480   struct var_loc_node *newloc;
21481   rtx next_real;
21482   static const char *last_label;
21483   static const char *last_postcall_label;
21484   static bool last_in_cold_section_p;
21485   tree decl;
21486
21487   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21488     return;
21489
21490   next_real = next_real_insn (loc_note);
21491   /* If there are no instructions which would be affected by this note,
21492      don't do anything.  */
21493   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21494     return;
21495
21496   /* If there were any real insns between note we processed last time
21497      and this note (or if it is the first note), clear
21498      last_{,postcall_}label so that they are not reused this time.  */
21499   if (last_var_location_insn == NULL_RTX
21500       || last_var_location_insn != next_real
21501       || last_in_cold_section_p != in_cold_section_p)
21502     {
21503       last_label = NULL;
21504       last_postcall_label = NULL;
21505     }
21506
21507   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21508   newloc = add_var_loc_to_decl (decl, loc_note,
21509                                 NOTE_DURING_CALL_P (loc_note)
21510                                 ? last_postcall_label : last_label);
21511   if (newloc == NULL)
21512     return;
21513
21514   /* If there were no real insns between note we processed last time
21515      and this note, use the label we emitted last time.  Otherwise
21516      create a new label and emit it.  */
21517   if (last_label == NULL)
21518     {
21519       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21520       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21521       loclabel_num++;
21522       last_label = ggc_strdup (loclabel);
21523     }
21524
21525   if (!NOTE_DURING_CALL_P (loc_note))
21526     newloc->label = last_label;
21527   else
21528     {
21529       if (!last_postcall_label)
21530         {
21531           sprintf (loclabel, "%s-1", last_label);
21532           last_postcall_label = ggc_strdup (loclabel);
21533         }
21534       newloc->label = last_postcall_label;
21535     }
21536
21537   last_var_location_insn = next_real;
21538   last_in_cold_section_p = in_cold_section_p;
21539 }
21540
21541 /* We need to reset the locations at the beginning of each
21542    function. We can't do this in the end_function hook, because the
21543    declarations that use the locations won't have been output when
21544    that hook is called.  Also compute have_multiple_function_sections here.  */
21545
21546 static void
21547 dwarf2out_begin_function (tree fun)
21548 {
21549   if (function_section (fun) != text_section)
21550     have_multiple_function_sections = true;
21551
21552   dwarf2out_note_section_used ();
21553 }
21554
21555 /* Output a label to mark the beginning of a source code line entry
21556    and record information relating to this source line, in
21557    'line_info_table' for later output of the .debug_line section.  */
21558
21559 static void
21560 dwarf2out_source_line (unsigned int line, const char *filename,
21561                        int discriminator, bool is_stmt)
21562 {
21563   static bool last_is_stmt = true;
21564
21565   if (debug_info_level >= DINFO_LEVEL_NORMAL
21566       && line != 0)
21567     {
21568       int file_num = maybe_emit_file (lookup_filename (filename));
21569
21570       switch_to_section (current_function_section ());
21571
21572       /* If requested, emit something human-readable.  */
21573       if (flag_debug_asm)
21574         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21575                  filename, line);
21576
21577       if (DWARF2_ASM_LINE_DEBUG_INFO)
21578         {
21579           /* Emit the .loc directive understood by GNU as.  */
21580           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21581           if (is_stmt != last_is_stmt)
21582             {
21583               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21584               last_is_stmt = is_stmt;
21585             }
21586           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21587             fprintf (asm_out_file, " discriminator %d", discriminator);
21588           fputc ('\n', asm_out_file);
21589
21590           /* Indicate that line number info exists.  */
21591           line_info_table_in_use++;
21592         }
21593       else if (function_section (current_function_decl) != text_section)
21594         {
21595           dw_separate_line_info_ref line_info;
21596           targetm.asm_out.internal_label (asm_out_file,
21597                                           SEPARATE_LINE_CODE_LABEL,
21598                                           separate_line_info_table_in_use);
21599
21600           /* Expand the line info table if necessary.  */
21601           if (separate_line_info_table_in_use
21602               == separate_line_info_table_allocated)
21603             {
21604               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21605               separate_line_info_table
21606                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21607                                  separate_line_info_table,
21608                                  separate_line_info_table_allocated);
21609               memset (separate_line_info_table
21610                        + separate_line_info_table_in_use,
21611                       0,
21612                       (LINE_INFO_TABLE_INCREMENT
21613                        * sizeof (dw_separate_line_info_entry)));
21614             }
21615
21616           /* Add the new entry at the end of the line_info_table.  */
21617           line_info
21618             = &separate_line_info_table[separate_line_info_table_in_use++];
21619           line_info->dw_file_num = file_num;
21620           line_info->dw_line_num = line;
21621           line_info->function = current_function_funcdef_no;
21622         }
21623       else
21624         {
21625           dw_line_info_ref line_info;
21626
21627           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21628                                      line_info_table_in_use);
21629
21630           /* Expand the line info table if necessary.  */
21631           if (line_info_table_in_use == line_info_table_allocated)
21632             {
21633               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21634               line_info_table
21635                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21636                                  line_info_table_allocated);
21637               memset (line_info_table + line_info_table_in_use, 0,
21638                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21639             }
21640
21641           /* Add the new entry at the end of the line_info_table.  */
21642           line_info = &line_info_table[line_info_table_in_use++];
21643           line_info->dw_file_num = file_num;
21644           line_info->dw_line_num = line;
21645         }
21646     }
21647 }
21648
21649 /* Record the beginning of a new source file.  */
21650
21651 static void
21652 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21653 {
21654   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21655     {
21656       /* Record the beginning of the file for break_out_includes.  */
21657       dw_die_ref bincl_die;
21658
21659       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21660       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21661     }
21662
21663   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21664     {
21665       int file_num = maybe_emit_file (lookup_filename (filename));
21666
21667       switch_to_section (debug_macinfo_section);
21668       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21669       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21670                                    lineno);
21671
21672       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21673     }
21674 }
21675
21676 /* Record the end of a source file.  */
21677
21678 static void
21679 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21680 {
21681   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21682     /* Record the end of the file for break_out_includes.  */
21683     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21684
21685   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21686     {
21687       switch_to_section (debug_macinfo_section);
21688       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21689     }
21690 }
21691
21692 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21693    the tail part of the directive line, i.e. the part which is past the
21694    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21695
21696 static void
21697 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21698                   const char *buffer ATTRIBUTE_UNUSED)
21699 {
21700   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21701     {
21702       switch_to_section (debug_macinfo_section);
21703       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21704       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21705       dw2_asm_output_nstring (buffer, -1, "The macro");
21706     }
21707 }
21708
21709 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21710    the tail part of the directive line, i.e. the part which is past the
21711    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21712
21713 static void
21714 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21715                  const char *buffer ATTRIBUTE_UNUSED)
21716 {
21717   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21718     {
21719       switch_to_section (debug_macinfo_section);
21720       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21721       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21722       dw2_asm_output_nstring (buffer, -1, "The macro");
21723     }
21724 }
21725
21726 /* Set up for Dwarf output at the start of compilation.  */
21727
21728 static void
21729 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21730 {
21731   /* Allocate the file_table.  */
21732   file_table = htab_create_ggc (50, file_table_hash,
21733                                 file_table_eq, NULL);
21734
21735   /* Allocate the decl_die_table.  */
21736   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21737                                     decl_die_table_eq, NULL);
21738
21739   /* Allocate the decl_loc_table.  */
21740   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21741                                     decl_loc_table_eq, NULL);
21742
21743   /* Allocate the initial hunk of the decl_scope_table.  */
21744   decl_scope_table = VEC_alloc (tree, gc, 256);
21745
21746   /* Allocate the initial hunk of the abbrev_die_table.  */
21747   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21748     (ABBREV_DIE_TABLE_INCREMENT);
21749   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21750   /* Zero-th entry is allocated, but unused.  */
21751   abbrev_die_table_in_use = 1;
21752
21753   /* Allocate the initial hunk of the line_info_table.  */
21754   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21755     (LINE_INFO_TABLE_INCREMENT);
21756   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21757
21758   /* Zero-th entry is allocated, but unused.  */
21759   line_info_table_in_use = 1;
21760
21761   /* Allocate the pubtypes and pubnames vectors.  */
21762   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21763   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21764
21765   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21766   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21767                                       vcall_insn_table_eq, NULL);
21768
21769   incomplete_types = VEC_alloc (tree, gc, 64);
21770
21771   used_rtx_array = VEC_alloc (rtx, gc, 32);
21772
21773   debug_info_section = get_section (DEBUG_INFO_SECTION,
21774                                     SECTION_DEBUG, NULL);
21775   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21776                                       SECTION_DEBUG, NULL);
21777   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21778                                        SECTION_DEBUG, NULL);
21779   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21780                                        SECTION_DEBUG, NULL);
21781   debug_line_section = get_section (DEBUG_LINE_SECTION,
21782                                     SECTION_DEBUG, NULL);
21783   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21784                                    SECTION_DEBUG, NULL);
21785   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21786                                         SECTION_DEBUG, NULL);
21787   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21788                                         SECTION_DEBUG, NULL);
21789   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21790                                      SECTION_DEBUG, NULL);
21791   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21792                                      SECTION_DEBUG, NULL);
21793   debug_str_section = get_section (DEBUG_STR_SECTION,
21794                                    DEBUG_STR_SECTION_FLAGS, NULL);
21795   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21796                                       SECTION_DEBUG, NULL);
21797   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21798                                      SECTION_DEBUG, NULL);
21799
21800   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21801   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21802                                DEBUG_ABBREV_SECTION_LABEL, 0);
21803   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21804   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21805                                COLD_TEXT_SECTION_LABEL, 0);
21806   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21807
21808   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21809                                DEBUG_INFO_SECTION_LABEL, 0);
21810   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21811                                DEBUG_LINE_SECTION_LABEL, 0);
21812   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21813                                DEBUG_RANGES_SECTION_LABEL, 0);
21814   switch_to_section (debug_abbrev_section);
21815   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21816   switch_to_section (debug_info_section);
21817   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21818   switch_to_section (debug_line_section);
21819   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21820
21821   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21822     {
21823       switch_to_section (debug_macinfo_section);
21824       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21825                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21826       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21827     }
21828
21829   switch_to_section (text_section);
21830   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21831   if (flag_reorder_blocks_and_partition)
21832     {
21833       cold_text_section = unlikely_text_section ();
21834       switch_to_section (cold_text_section);
21835       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21836     }
21837
21838 }
21839
21840 /* Called before cgraph_optimize starts outputtting functions, variables
21841    and toplevel asms into assembly.  */
21842
21843 static void
21844 dwarf2out_assembly_start (void)
21845 {
21846   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21847       && dwarf2out_do_cfi_asm ()
21848       && (!(flag_unwind_tables || flag_exceptions)
21849           || targetm.except_unwind_info () != UI_DWARF2))
21850     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21851 }
21852
21853 /* A helper function for dwarf2out_finish called through
21854    htab_traverse.  Emit one queued .debug_str string.  */
21855
21856 static int
21857 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21858 {
21859   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21860
21861   if (node->label && node->refcount)
21862     {
21863       switch_to_section (debug_str_section);
21864       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21865       assemble_string (node->str, strlen (node->str) + 1);
21866     }
21867
21868   return 1;
21869 }
21870
21871 #if ENABLE_ASSERT_CHECKING
21872 /* Verify that all marks are clear.  */
21873
21874 static void
21875 verify_marks_clear (dw_die_ref die)
21876 {
21877   dw_die_ref c;
21878
21879   gcc_assert (! die->die_mark);
21880   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21881 }
21882 #endif /* ENABLE_ASSERT_CHECKING */
21883
21884 /* Clear the marks for a die and its children.
21885    Be cool if the mark isn't set.  */
21886
21887 static void
21888 prune_unmark_dies (dw_die_ref die)
21889 {
21890   dw_die_ref c;
21891
21892   if (die->die_mark)
21893     die->die_mark = 0;
21894   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21895 }
21896
21897 /* Given DIE that we're marking as used, find any other dies
21898    it references as attributes and mark them as used.  */
21899
21900 static void
21901 prune_unused_types_walk_attribs (dw_die_ref die)
21902 {
21903   dw_attr_ref a;
21904   unsigned ix;
21905
21906   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21907     {
21908       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21909         {
21910           /* A reference to another DIE.
21911              Make sure that it will get emitted.
21912              If it was broken out into a comdat group, don't follow it.  */
21913           if (dwarf_version < 4
21914               || a->dw_attr == DW_AT_specification
21915               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21916             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21917         }
21918       /* Set the string's refcount to 0 so that prune_unused_types_mark
21919          accounts properly for it.  */
21920       if (AT_class (a) == dw_val_class_str)
21921         a->dw_attr_val.v.val_str->refcount = 0;
21922     }
21923 }
21924
21925
21926 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21927    to DIE's children.  */
21928
21929 static void
21930 prune_unused_types_mark (dw_die_ref die, int dokids)
21931 {
21932   dw_die_ref c;
21933
21934   if (die->die_mark == 0)
21935     {
21936       /* We haven't done this node yet.  Mark it as used.  */
21937       die->die_mark = 1;
21938
21939       /* We also have to mark its parents as used.
21940          (But we don't want to mark our parents' kids due to this.)  */
21941       if (die->die_parent)
21942         prune_unused_types_mark (die->die_parent, 0);
21943
21944       /* Mark any referenced nodes.  */
21945       prune_unused_types_walk_attribs (die);
21946
21947       /* If this node is a specification,
21948          also mark the definition, if it exists.  */
21949       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21950         prune_unused_types_mark (die->die_definition, 1);
21951     }
21952
21953   if (dokids && die->die_mark != 2)
21954     {
21955       /* We need to walk the children, but haven't done so yet.
21956          Remember that we've walked the kids.  */
21957       die->die_mark = 2;
21958
21959       /* If this is an array type, we need to make sure our
21960          kids get marked, even if they're types.  If we're
21961          breaking out types into comdat sections, do this
21962          for all type definitions.  */
21963       if (die->die_tag == DW_TAG_array_type
21964           || (dwarf_version >= 4
21965               && is_type_die (die) && ! is_declaration_die (die)))
21966         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21967       else
21968         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21969     }
21970 }
21971
21972 /* For local classes, look if any static member functions were emitted
21973    and if so, mark them.  */
21974
21975 static void
21976 prune_unused_types_walk_local_classes (dw_die_ref die)
21977 {
21978   dw_die_ref c;
21979
21980   if (die->die_mark == 2)
21981     return;
21982
21983   switch (die->die_tag)
21984     {
21985     case DW_TAG_structure_type:
21986     case DW_TAG_union_type:
21987     case DW_TAG_class_type:
21988       break;
21989
21990     case DW_TAG_subprogram:
21991       if (!get_AT_flag (die, DW_AT_declaration)
21992           || die->die_definition != NULL)
21993         prune_unused_types_mark (die, 1);
21994       return;
21995
21996     default:
21997       return;
21998     }
21999
22000   /* Mark children.  */
22001   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22002 }
22003
22004 /* Walk the tree DIE and mark types that we actually use.  */
22005
22006 static void
22007 prune_unused_types_walk (dw_die_ref die)
22008 {
22009   dw_die_ref c;
22010
22011   /* Don't do anything if this node is already marked and
22012      children have been marked as well.  */
22013   if (die->die_mark == 2)
22014     return;
22015
22016   switch (die->die_tag)
22017     {
22018     case DW_TAG_structure_type:
22019     case DW_TAG_union_type:
22020     case DW_TAG_class_type:
22021       if (die->die_perennial_p)
22022         break;
22023
22024       for (c = die->die_parent; c; c = c->die_parent)
22025         if (c->die_tag == DW_TAG_subprogram)
22026           break;
22027
22028       /* Finding used static member functions inside of classes
22029          is needed just for local classes, because for other classes
22030          static member function DIEs with DW_AT_specification
22031          are emitted outside of the DW_TAG_*_type.  If we ever change
22032          it, we'd need to call this even for non-local classes.  */
22033       if (c)
22034         prune_unused_types_walk_local_classes (die);
22035
22036       /* It's a type node --- don't mark it.  */
22037       return;
22038
22039     case DW_TAG_const_type:
22040     case DW_TAG_packed_type:
22041     case DW_TAG_pointer_type:
22042     case DW_TAG_reference_type:
22043     case DW_TAG_rvalue_reference_type:
22044     case DW_TAG_volatile_type:
22045     case DW_TAG_typedef:
22046     case DW_TAG_array_type:
22047     case DW_TAG_interface_type:
22048     case DW_TAG_friend:
22049     case DW_TAG_variant_part:
22050     case DW_TAG_enumeration_type:
22051     case DW_TAG_subroutine_type:
22052     case DW_TAG_string_type:
22053     case DW_TAG_set_type:
22054     case DW_TAG_subrange_type:
22055     case DW_TAG_ptr_to_member_type:
22056     case DW_TAG_file_type:
22057       if (die->die_perennial_p)
22058         break;
22059
22060       /* It's a type node --- don't mark it.  */
22061       return;
22062
22063     default:
22064       /* Mark everything else.  */
22065       break;
22066   }
22067
22068   if (die->die_mark == 0)
22069     {
22070       die->die_mark = 1;
22071
22072       /* Now, mark any dies referenced from here.  */
22073       prune_unused_types_walk_attribs (die);
22074     }
22075
22076   die->die_mark = 2;
22077
22078   /* Mark children.  */
22079   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22080 }
22081
22082 /* Increment the string counts on strings referred to from DIE's
22083    attributes.  */
22084
22085 static void
22086 prune_unused_types_update_strings (dw_die_ref die)
22087 {
22088   dw_attr_ref a;
22089   unsigned ix;
22090
22091   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22092     if (AT_class (a) == dw_val_class_str)
22093       {
22094         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22095         s->refcount++;
22096         /* Avoid unnecessarily putting strings that are used less than
22097            twice in the hash table.  */
22098         if (s->refcount
22099             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22100           {
22101             void ** slot;
22102             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22103                                              htab_hash_string (s->str),
22104                                              INSERT);
22105             gcc_assert (*slot == NULL);
22106             *slot = s;
22107           }
22108       }
22109 }
22110
22111 /* Remove from the tree DIE any dies that aren't marked.  */
22112
22113 static void
22114 prune_unused_types_prune (dw_die_ref die)
22115 {
22116   dw_die_ref c;
22117
22118   gcc_assert (die->die_mark);
22119   prune_unused_types_update_strings (die);
22120
22121   if (! die->die_child)
22122     return;
22123
22124   c = die->die_child;
22125   do {
22126     dw_die_ref prev = c;
22127     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22128       if (c == die->die_child)
22129         {
22130           /* No marked children between 'prev' and the end of the list.  */
22131           if (prev == c)
22132             /* No marked children at all.  */
22133             die->die_child = NULL;
22134           else
22135             {
22136               prev->die_sib = c->die_sib;
22137               die->die_child = prev;
22138             }
22139           return;
22140         }
22141
22142     if (c != prev->die_sib)
22143       prev->die_sib = c;
22144     prune_unused_types_prune (c);
22145   } while (c != die->die_child);
22146 }
22147
22148 /* A helper function for dwarf2out_finish called through
22149    htab_traverse.  Clear .debug_str strings that we haven't already
22150    decided to emit.  */
22151
22152 static int
22153 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22154 {
22155   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22156
22157   if (!node->label || !node->refcount)
22158     htab_clear_slot (debug_str_hash, h);
22159
22160   return 1;
22161 }
22162
22163 /* Remove dies representing declarations that we never use.  */
22164
22165 static void
22166 prune_unused_types (void)
22167 {
22168   unsigned int i;
22169   limbo_die_node *node;
22170   comdat_type_node *ctnode;
22171   pubname_ref pub;
22172   dcall_entry *dcall;
22173
22174 #if ENABLE_ASSERT_CHECKING
22175   /* All the marks should already be clear.  */
22176   verify_marks_clear (comp_unit_die ());
22177   for (node = limbo_die_list; node; node = node->next)
22178     verify_marks_clear (node->die);
22179   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22180     verify_marks_clear (ctnode->root_die);
22181 #endif /* ENABLE_ASSERT_CHECKING */
22182
22183   /* Mark types that are used in global variables.  */
22184   premark_types_used_by_global_vars ();
22185
22186   /* Set the mark on nodes that are actually used.  */
22187   prune_unused_types_walk (comp_unit_die ());
22188   for (node = limbo_die_list; node; node = node->next)
22189     prune_unused_types_walk (node->die);
22190   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22191     {
22192       prune_unused_types_walk (ctnode->root_die);
22193       prune_unused_types_mark (ctnode->type_die, 1);
22194     }
22195
22196   /* Also set the mark on nodes referenced from the
22197      pubname_table or arange_table.  */
22198   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22199     prune_unused_types_mark (pub->die, 1);
22200   for (i = 0; i < arange_table_in_use; i++)
22201     prune_unused_types_mark (arange_table[i], 1);
22202
22203   /* Mark nodes referenced from the direct call table.  */
22204   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22205     prune_unused_types_mark (dcall->targ_die, 1);
22206
22207   /* Get rid of nodes that aren't marked; and update the string counts.  */
22208   if (debug_str_hash && debug_str_hash_forced)
22209     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22210   else if (debug_str_hash)
22211     htab_empty (debug_str_hash);
22212   prune_unused_types_prune (comp_unit_die ());
22213   for (node = limbo_die_list; node; node = node->next)
22214     prune_unused_types_prune (node->die);
22215   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22216     prune_unused_types_prune (ctnode->root_die);
22217
22218   /* Leave the marks clear.  */
22219   prune_unmark_dies (comp_unit_die ());
22220   for (node = limbo_die_list; node; node = node->next)
22221     prune_unmark_dies (node->die);
22222   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22223     prune_unmark_dies (ctnode->root_die);
22224 }
22225
22226 /* Set the parameter to true if there are any relative pathnames in
22227    the file table.  */
22228 static int
22229 file_table_relative_p (void ** slot, void *param)
22230 {
22231   bool *p = (bool *) param;
22232   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22233   if (!IS_ABSOLUTE_PATH (d->filename))
22234     {
22235       *p = true;
22236       return 0;
22237     }
22238   return 1;
22239 }
22240
22241 /* Routines to manipulate hash table of comdat type units.  */
22242
22243 static hashval_t
22244 htab_ct_hash (const void *of)
22245 {
22246   hashval_t h;
22247   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22248
22249   memcpy (&h, type_node->signature, sizeof (h));
22250   return h;
22251 }
22252
22253 static int
22254 htab_ct_eq (const void *of1, const void *of2)
22255 {
22256   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22257   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22258
22259   return (! memcmp (type_node_1->signature, type_node_2->signature,
22260                     DWARF_TYPE_SIGNATURE_SIZE));
22261 }
22262
22263 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22264    to the location it would have been added, should we know its
22265    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22266    probably improve compactness of debug info, removing equivalent
22267    abbrevs, and hide any differences caused by deferring the
22268    computation of the assembler name, triggered by e.g. PCH.  */
22269
22270 static inline void
22271 move_linkage_attr (dw_die_ref die)
22272 {
22273   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22274   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22275
22276   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22277               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22278
22279   while (--ix > 0)
22280     {
22281       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22282
22283       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22284         break;
22285     }
22286
22287   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22288     {
22289       VEC_pop (dw_attr_node, die->die_attr);
22290       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22291     }
22292 }
22293
22294 /* Helper function for resolve_addr, attempt to resolve
22295    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22296    SYMBOL_REFs refer to variables emitted in the current CU.  */
22297
22298 static int
22299 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22300 {
22301   rtx rtl = *addr;
22302
22303   if (GET_CODE (rtl) == CONST_STRING)
22304     {
22305       size_t len = strlen (XSTR (rtl, 0)) + 1;
22306       tree t = build_string (len, XSTR (rtl, 0));
22307       tree tlen = build_int_cst (NULL_TREE, len - 1);
22308       TREE_TYPE (t)
22309         = build_array_type (char_type_node, build_index_type (tlen));
22310       rtl = lookup_constant_def (t);
22311       if (!rtl || !MEM_P (rtl))
22312         return 1;
22313       rtl = XEXP (rtl, 0);
22314       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22315       *addr = rtl;
22316       return 0;
22317     }
22318
22319   if (GET_CODE (rtl) == SYMBOL_REF
22320       && SYMBOL_REF_DECL (rtl)
22321       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22322     return 1;
22323
22324   if (GET_CODE (rtl) == CONST
22325       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22326     return 1;
22327
22328   return 0;
22329 }
22330
22331 /* Helper function for resolve_addr, handle one location
22332    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22333    the location list couldn't be resolved.  */
22334
22335 static bool
22336 resolve_addr_in_expr (dw_loc_descr_ref loc)
22337 {
22338   for (; loc; loc = loc->dw_loc_next)
22339     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22340          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22341         || (loc->dw_loc_opc == DW_OP_implicit_value
22342             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22343             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22344       return false;
22345     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22346              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22347       {
22348         dw_die_ref ref
22349           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22350         if (ref == NULL)
22351           return false;
22352         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22353         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22354         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22355       }
22356   return true;
22357 }
22358
22359 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22360    an address in .rodata section if the string literal is emitted there,
22361    or remove the containing location list or replace DW_AT_const_value
22362    with DW_AT_location and empty location expression, if it isn't found
22363    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22364    to something that has been emitted in the current CU.  */
22365
22366 static void
22367 resolve_addr (dw_die_ref die)
22368 {
22369   dw_die_ref c;
22370   dw_attr_ref a;
22371   dw_loc_list_ref *curr;
22372   unsigned ix;
22373
22374   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22375     switch (AT_class (a))
22376       {
22377       case dw_val_class_loc_list:
22378         curr = AT_loc_list_ptr (a);
22379         while (*curr)
22380           {
22381             if (!resolve_addr_in_expr ((*curr)->expr))
22382               {
22383                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22384                 if (next && (*curr)->ll_symbol)
22385                   {
22386                     gcc_assert (!next->ll_symbol);
22387                     next->ll_symbol = (*curr)->ll_symbol;
22388                   }
22389                 *curr = next;
22390               }
22391             else
22392               curr = &(*curr)->dw_loc_next;
22393           }
22394         if (!AT_loc_list (a))
22395           {
22396             remove_AT (die, a->dw_attr);
22397             ix--;
22398           }
22399         break;
22400       case dw_val_class_loc:
22401         if (!resolve_addr_in_expr (AT_loc (a)))
22402           {
22403             remove_AT (die, a->dw_attr);
22404             ix--;
22405           }
22406         break;
22407       case dw_val_class_addr:
22408         if (a->dw_attr == DW_AT_const_value
22409             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22410           {
22411             remove_AT (die, a->dw_attr);
22412             ix--;
22413           }
22414         break;
22415       default:
22416         break;
22417       }
22418
22419   FOR_EACH_CHILD (die, c, resolve_addr (c));
22420 }
22421 \f
22422 /* Helper routines for optimize_location_lists.
22423    This pass tries to share identical local lists in .debug_loc
22424    section.  */
22425
22426 /* Iteratively hash operands of LOC opcode.  */
22427
22428 static inline hashval_t
22429 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22430 {
22431   dw_val_ref val1 = &loc->dw_loc_oprnd1;
22432   dw_val_ref val2 = &loc->dw_loc_oprnd2;
22433
22434   switch (loc->dw_loc_opc)
22435     {
22436     case DW_OP_const4u:
22437     case DW_OP_const8u:
22438       if (loc->dtprel)
22439         goto hash_addr;
22440       /* FALLTHRU */
22441     case DW_OP_const1u:
22442     case DW_OP_const1s:
22443     case DW_OP_const2u:
22444     case DW_OP_const2s:
22445     case DW_OP_const4s:
22446     case DW_OP_const8s:
22447     case DW_OP_constu:
22448     case DW_OP_consts:
22449     case DW_OP_pick:
22450     case DW_OP_plus_uconst:
22451     case DW_OP_breg0:
22452     case DW_OP_breg1:
22453     case DW_OP_breg2:
22454     case DW_OP_breg3:
22455     case DW_OP_breg4:
22456     case DW_OP_breg5:
22457     case DW_OP_breg6:
22458     case DW_OP_breg7:
22459     case DW_OP_breg8:
22460     case DW_OP_breg9:
22461     case DW_OP_breg10:
22462     case DW_OP_breg11:
22463     case DW_OP_breg12:
22464     case DW_OP_breg13:
22465     case DW_OP_breg14:
22466     case DW_OP_breg15:
22467     case DW_OP_breg16:
22468     case DW_OP_breg17:
22469     case DW_OP_breg18:
22470     case DW_OP_breg19:
22471     case DW_OP_breg20:
22472     case DW_OP_breg21:
22473     case DW_OP_breg22:
22474     case DW_OP_breg23:
22475     case DW_OP_breg24:
22476     case DW_OP_breg25:
22477     case DW_OP_breg26:
22478     case DW_OP_breg27:
22479     case DW_OP_breg28:
22480     case DW_OP_breg29:
22481     case DW_OP_breg30:
22482     case DW_OP_breg31:
22483     case DW_OP_regx:
22484     case DW_OP_fbreg:
22485     case DW_OP_piece:
22486     case DW_OP_deref_size:
22487     case DW_OP_xderef_size:
22488       hash = iterative_hash_object (val1->v.val_int, hash);
22489       break;
22490     case DW_OP_skip:
22491     case DW_OP_bra:
22492       {
22493         int offset;
22494
22495         gcc_assert (val1->val_class == dw_val_class_loc);
22496         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22497         hash = iterative_hash_object (offset, hash);
22498       }
22499       break;
22500     case DW_OP_implicit_value:
22501       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22502       switch (val2->val_class)
22503         {
22504         case dw_val_class_const:
22505           hash = iterative_hash_object (val2->v.val_int, hash);
22506           break;
22507         case dw_val_class_vec:
22508           {
22509             unsigned int elt_size = val2->v.val_vec.elt_size;
22510             unsigned int len = val2->v.val_vec.length;
22511
22512             hash = iterative_hash_object (elt_size, hash);
22513             hash = iterative_hash_object (len, hash);
22514             hash = iterative_hash (val2->v.val_vec.array,
22515                                    len * elt_size, hash);
22516           }
22517           break;
22518         case dw_val_class_const_double:
22519           hash = iterative_hash_object (val2->v.val_double.low, hash);
22520           hash = iterative_hash_object (val2->v.val_double.high, hash);
22521           break;
22522         case dw_val_class_addr:
22523           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22524           break;
22525         default:
22526           gcc_unreachable ();
22527         }
22528       break;
22529     case DW_OP_bregx:
22530     case DW_OP_bit_piece:
22531       hash = iterative_hash_object (val1->v.val_int, hash);
22532       hash = iterative_hash_object (val2->v.val_int, hash);
22533       break;
22534     case DW_OP_addr:
22535     hash_addr:
22536       if (loc->dtprel)
22537         {
22538           unsigned char dtprel = 0xd1;
22539           hash = iterative_hash_object (dtprel, hash);
22540         }
22541       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22542       break;
22543     case DW_OP_GNU_implicit_pointer:
22544       hash = iterative_hash_object (val2->v.val_int, hash);
22545       break;
22546
22547     default:
22548       /* Other codes have no operands.  */
22549       break;
22550     }
22551   return hash;
22552 }
22553
22554 /* Iteratively hash the whole DWARF location expression LOC.  */
22555
22556 static inline hashval_t
22557 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22558 {
22559   dw_loc_descr_ref l;
22560   bool sizes_computed = false;
22561   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22562   size_of_locs (loc);
22563
22564   for (l = loc; l != NULL; l = l->dw_loc_next)
22565     {
22566       enum dwarf_location_atom opc = l->dw_loc_opc;
22567       hash = iterative_hash_object (opc, hash);
22568       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22569         {
22570           size_of_locs (loc);
22571           sizes_computed = true;
22572         }
22573       hash = hash_loc_operands (l, hash);
22574     }
22575   return hash;
22576 }
22577
22578 /* Compute hash of the whole location list LIST_HEAD.  */
22579
22580 static inline void
22581 hash_loc_list (dw_loc_list_ref list_head)
22582 {
22583   dw_loc_list_ref curr = list_head;
22584   hashval_t hash = 0;
22585
22586   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22587     {
22588       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22589       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22590       if (curr->section)
22591         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22592                                hash);
22593       hash = hash_locs (curr->expr, hash);
22594     }
22595   list_head->hash = hash;
22596 }
22597
22598 /* Return true if X and Y opcodes have the same operands.  */
22599
22600 static inline bool
22601 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22602 {
22603   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22604   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22605   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22606   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22607
22608   switch (x->dw_loc_opc)
22609     {
22610     case DW_OP_const4u:
22611     case DW_OP_const8u:
22612       if (x->dtprel)
22613         goto hash_addr;
22614       /* FALLTHRU */
22615     case DW_OP_const1u:
22616     case DW_OP_const1s:
22617     case DW_OP_const2u:
22618     case DW_OP_const2s:
22619     case DW_OP_const4s:
22620     case DW_OP_const8s:
22621     case DW_OP_constu:
22622     case DW_OP_consts:
22623     case DW_OP_pick:
22624     case DW_OP_plus_uconst:
22625     case DW_OP_breg0:
22626     case DW_OP_breg1:
22627     case DW_OP_breg2:
22628     case DW_OP_breg3:
22629     case DW_OP_breg4:
22630     case DW_OP_breg5:
22631     case DW_OP_breg6:
22632     case DW_OP_breg7:
22633     case DW_OP_breg8:
22634     case DW_OP_breg9:
22635     case DW_OP_breg10:
22636     case DW_OP_breg11:
22637     case DW_OP_breg12:
22638     case DW_OP_breg13:
22639     case DW_OP_breg14:
22640     case DW_OP_breg15:
22641     case DW_OP_breg16:
22642     case DW_OP_breg17:
22643     case DW_OP_breg18:
22644     case DW_OP_breg19:
22645     case DW_OP_breg20:
22646     case DW_OP_breg21:
22647     case DW_OP_breg22:
22648     case DW_OP_breg23:
22649     case DW_OP_breg24:
22650     case DW_OP_breg25:
22651     case DW_OP_breg26:
22652     case DW_OP_breg27:
22653     case DW_OP_breg28:
22654     case DW_OP_breg29:
22655     case DW_OP_breg30:
22656     case DW_OP_breg31:
22657     case DW_OP_regx:
22658     case DW_OP_fbreg:
22659     case DW_OP_piece:
22660     case DW_OP_deref_size:
22661     case DW_OP_xderef_size:
22662       return valx1->v.val_int == valy1->v.val_int;
22663     case DW_OP_skip:
22664     case DW_OP_bra:
22665       gcc_assert (valx1->val_class == dw_val_class_loc
22666                   && valy1->val_class == dw_val_class_loc
22667                   && x->dw_loc_addr == y->dw_loc_addr);
22668       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22669     case DW_OP_implicit_value:
22670       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22671           || valx2->val_class != valy2->val_class)
22672         return false;
22673       switch (valx2->val_class)
22674         {
22675         case dw_val_class_const:
22676           return valx2->v.val_int == valy2->v.val_int;
22677         case dw_val_class_vec:
22678           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22679                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22680                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22681                             valx2->v.val_vec.elt_size
22682                             * valx2->v.val_vec.length) == 0;
22683         case dw_val_class_const_double:
22684           return valx2->v.val_double.low == valy2->v.val_double.low
22685                  && valx2->v.val_double.high == valy2->v.val_double.high;
22686         case dw_val_class_addr:
22687           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22688         default:
22689           gcc_unreachable ();
22690         }
22691     case DW_OP_bregx:
22692     case DW_OP_bit_piece:
22693       return valx1->v.val_int == valy1->v.val_int
22694              && valx2->v.val_int == valy2->v.val_int;
22695     case DW_OP_addr:
22696     hash_addr:
22697       return rtx_equal_p (valx1->v.val_addr, valx2->v.val_addr);
22698     case DW_OP_GNU_implicit_pointer:
22699       return valx1->val_class == dw_val_class_die_ref
22700              && valx1->val_class == valy1->val_class
22701              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22702              && valx2->v.val_int == valy2->v.val_int;
22703     default:
22704       /* Other codes have no operands.  */
22705       return true;
22706     }
22707 }
22708
22709 /* Return true if DWARF location expressions X and Y are the same.  */
22710
22711 static inline bool
22712 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22713 {
22714   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22715     if (x->dw_loc_opc != y->dw_loc_opc
22716         || x->dtprel != y->dtprel
22717         || !compare_loc_operands (x, y))
22718       break;
22719   return x == NULL && y == NULL;
22720 }
22721
22722 /* Return precomputed hash of location list X.  */
22723
22724 static hashval_t
22725 loc_list_hash (const void *x)
22726 {
22727   return ((const struct dw_loc_list_struct *) x)->hash;
22728 }
22729
22730 /* Return 1 if location lists X and Y are the same.  */
22731
22732 static int
22733 loc_list_eq (const void *x, const void *y)
22734 {
22735   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22736   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22737   if (a == b)
22738     return 1;
22739   if (a->hash != b->hash)
22740     return 0;
22741   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22742     if (strcmp (a->begin, b->begin) != 0
22743         || strcmp (a->end, b->end) != 0
22744         || (a->section == NULL) != (b->section == NULL)
22745         || (a->section && strcmp (a->section, b->section) != 0)
22746         || !compare_locs (a->expr, b->expr))
22747       break;
22748   return a == NULL && b == NULL;
22749 }
22750
22751 /* Recursively optimize location lists referenced from DIE
22752    children and share them whenever possible.  */
22753
22754 static void
22755 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22756 {
22757   dw_die_ref c;
22758   dw_attr_ref a;
22759   unsigned ix;
22760   void **slot;
22761
22762   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22763     if (AT_class (a) == dw_val_class_loc_list)
22764       {
22765         dw_loc_list_ref list = AT_loc_list (a);
22766         /* TODO: perform some optimizations here, before hashing
22767            it and storing into the hash table.  */
22768         hash_loc_list (list);
22769         slot = htab_find_slot_with_hash (htab, list, list->hash,
22770                                          INSERT);
22771         if (*slot == NULL)
22772           *slot = (void *) list;
22773         else
22774           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22775       }
22776
22777   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22778 }
22779
22780 /* Optimize location lists referenced from DIE
22781    children and share them whenever possible.  */
22782
22783 static void
22784 optimize_location_lists (dw_die_ref die)
22785 {
22786   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22787   optimize_location_lists_1 (die, htab);
22788   htab_delete (htab);
22789 }
22790 \f
22791 /* Output stuff that dwarf requires at the end of every file,
22792    and generate the DWARF-2 debugging info.  */
22793
22794 static void
22795 dwarf2out_finish (const char *filename)
22796 {
22797   limbo_die_node *node, *next_node;
22798   comdat_type_node *ctnode;
22799   htab_t comdat_type_table;
22800   dw_die_ref die = 0;
22801   unsigned int i;
22802
22803   gen_remaining_tmpl_value_param_die_attribute ();
22804
22805   /* Add the name for the main input file now.  We delayed this from
22806      dwarf2out_init to avoid complications with PCH.  */
22807   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22808   if (!IS_ABSOLUTE_PATH (filename))
22809     add_comp_dir_attribute (comp_unit_die ());
22810   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22811     {
22812       bool p = false;
22813       htab_traverse (file_table, file_table_relative_p, &p);
22814       if (p)
22815         add_comp_dir_attribute (comp_unit_die ());
22816     }
22817
22818   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22819     {
22820       add_location_or_const_value_attribute (
22821         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22822         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22823         DW_AT_location);
22824     }
22825
22826   /* Traverse the limbo die list, and add parent/child links.  The only
22827      dies without parents that should be here are concrete instances of
22828      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22829      For concrete instances, we can get the parent die from the abstract
22830      instance.  */
22831   for (node = limbo_die_list; node; node = next_node)
22832     {
22833       next_node = node->next;
22834       die = node->die;
22835
22836       if (die->die_parent == NULL)
22837         {
22838           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22839
22840           if (origin)
22841             add_child_die (origin->die_parent, die);
22842           else if (is_cu_die (die))
22843             ;
22844           else if (seen_error ())
22845             /* It's OK to be confused by errors in the input.  */
22846             add_child_die (comp_unit_die (), die);
22847           else
22848             {
22849               /* In certain situations, the lexical block containing a
22850                  nested function can be optimized away, which results
22851                  in the nested function die being orphaned.  Likewise
22852                  with the return type of that nested function.  Force
22853                  this to be a child of the containing function.
22854
22855                  It may happen that even the containing function got fully
22856                  inlined and optimized out.  In that case we are lost and
22857                  assign the empty child.  This should not be big issue as
22858                  the function is likely unreachable too.  */
22859               tree context = NULL_TREE;
22860
22861               gcc_assert (node->created_for);
22862
22863               if (DECL_P (node->created_for))
22864                 context = DECL_CONTEXT (node->created_for);
22865               else if (TYPE_P (node->created_for))
22866                 context = TYPE_CONTEXT (node->created_for);
22867
22868               gcc_assert (context
22869                           && (TREE_CODE (context) == FUNCTION_DECL
22870                               || TREE_CODE (context) == NAMESPACE_DECL));
22871
22872               origin = lookup_decl_die (context);
22873               if (origin)
22874                 add_child_die (origin, die);
22875               else
22876                 add_child_die (comp_unit_die (), die);
22877             }
22878         }
22879     }
22880
22881   limbo_die_list = NULL;
22882
22883   resolve_addr (comp_unit_die ());
22884
22885   for (node = deferred_asm_name; node; node = node->next)
22886     {
22887       tree decl = node->created_for;
22888       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22889         {
22890           add_linkage_attr (node->die, decl);
22891           move_linkage_attr (node->die);
22892         }
22893     }
22894
22895   deferred_asm_name = NULL;
22896
22897   /* Walk through the list of incomplete types again, trying once more to
22898      emit full debugging info for them.  */
22899   retry_incomplete_types ();
22900
22901   if (flag_eliminate_unused_debug_types)
22902     prune_unused_types ();
22903
22904   /* Generate separate CUs for each of the include files we've seen.
22905      They will go into limbo_die_list.  */
22906   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22907     break_out_includes (comp_unit_die ());
22908
22909   /* Generate separate COMDAT sections for type DIEs. */
22910   if (dwarf_version >= 4)
22911     {
22912       break_out_comdat_types (comp_unit_die ());
22913
22914       /* Each new type_unit DIE was added to the limbo die list when created.
22915          Since these have all been added to comdat_type_list, clear the
22916          limbo die list.  */
22917       limbo_die_list = NULL;
22918
22919       /* For each new comdat type unit, copy declarations for incomplete
22920          types to make the new unit self-contained (i.e., no direct
22921          references to the main compile unit).  */
22922       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22923         copy_decls_for_unworthy_types (ctnode->root_die);
22924       copy_decls_for_unworthy_types (comp_unit_die ());
22925
22926       /* In the process of copying declarations from one unit to another,
22927          we may have left some declarations behind that are no longer
22928          referenced.  Prune them.  */
22929       prune_unused_types ();
22930     }
22931
22932   /* Traverse the DIE's and add add sibling attributes to those DIE's
22933      that have children.  */
22934   add_sibling_attributes (comp_unit_die ());
22935   for (node = limbo_die_list; node; node = node->next)
22936     add_sibling_attributes (node->die);
22937   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22938     add_sibling_attributes (ctnode->root_die);
22939
22940   /* Output a terminator label for the .text section.  */
22941   switch_to_section (text_section);
22942   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22943   if (flag_reorder_blocks_and_partition)
22944     {
22945       switch_to_section (unlikely_text_section ());
22946       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22947     }
22948
22949   /* We can only use the low/high_pc attributes if all of the code was
22950      in .text.  */
22951   if (!have_multiple_function_sections
22952       || !(dwarf_version >= 3 || !dwarf_strict))
22953     {
22954       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22955       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22956     }
22957
22958   else
22959     {
22960       unsigned fde_idx = 0;
22961       bool range_list_added = false;
22962
22963       /* We need to give .debug_loc and .debug_ranges an appropriate
22964          "base address".  Use zero so that these addresses become
22965          absolute.  Historically, we've emitted the unexpected
22966          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22967          Emit both to give time for other tools to adapt.  */
22968       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22969       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22970
22971       if (text_section_used)
22972         add_ranges_by_labels (comp_unit_die (), text_section_label,
22973                               text_end_label, &range_list_added);
22974       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22975         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22976                               cold_end_label, &range_list_added);
22977
22978       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22979         {
22980           dw_fde_ref fde = &fde_table[fde_idx];
22981
22982           if (fde->dw_fde_switched_sections)
22983             {
22984               if (!fde->in_std_section)
22985                 add_ranges_by_labels (comp_unit_die (),
22986                                       fde->dw_fde_hot_section_label,
22987                                       fde->dw_fde_hot_section_end_label,
22988                                       &range_list_added);
22989               if (!fde->cold_in_std_section)
22990                 add_ranges_by_labels (comp_unit_die (),
22991                                       fde->dw_fde_unlikely_section_label,
22992                                       fde->dw_fde_unlikely_section_end_label,
22993                                       &range_list_added);
22994             }
22995           else if (!fde->in_std_section)
22996             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22997                                   fde->dw_fde_end, &range_list_added);
22998         }
22999
23000       if (range_list_added)
23001         add_ranges (NULL);
23002     }
23003
23004   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23005     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23006                     debug_line_section_label);
23007
23008   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23009     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23010
23011   if (have_location_lists)
23012     optimize_location_lists (die);
23013
23014   /* Output all of the compilation units.  We put the main one last so that
23015      the offsets are available to output_pubnames.  */
23016   for (node = limbo_die_list; node; node = node->next)
23017     output_comp_unit (node->die, 0);
23018
23019   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23020   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23021     {
23022       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23023
23024       /* Don't output duplicate types.  */
23025       if (*slot != HTAB_EMPTY_ENTRY)
23026         continue;
23027
23028       /* Add a pointer to the line table for the main compilation unit
23029          so that the debugger can make sense of DW_AT_decl_file
23030          attributes.  */
23031       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23032         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23033                         debug_line_section_label);
23034
23035       output_comdat_type_unit (ctnode);
23036       *slot = ctnode;
23037     }
23038   htab_delete (comdat_type_table);
23039
23040   /* Output the main compilation unit if non-empty or if .debug_macinfo
23041      has been emitted.  */
23042   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23043
23044   /* Output the abbreviation table.  */
23045   switch_to_section (debug_abbrev_section);
23046   output_abbrev_section ();
23047
23048   /* Output location list section if necessary.  */
23049   if (have_location_lists)
23050     {
23051       /* Output the location lists info.  */
23052       switch_to_section (debug_loc_section);
23053       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23054                                    DEBUG_LOC_SECTION_LABEL, 0);
23055       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23056       output_location_lists (die);
23057     }
23058
23059   /* Output public names table if necessary.  */
23060   if (!VEC_empty (pubname_entry, pubname_table))
23061     {
23062       switch_to_section (debug_pubnames_section);
23063       output_pubnames (pubname_table);
23064     }
23065
23066   /* Output public types table if necessary.  */
23067   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23068      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23069      simply won't look for the section.  */
23070   if (!VEC_empty (pubname_entry, pubtype_table))
23071     {
23072       switch_to_section (debug_pubtypes_section);
23073       output_pubnames (pubtype_table);
23074     }
23075
23076   /* Output direct and virtual call tables if necessary.  */
23077   if (!VEC_empty (dcall_entry, dcall_table))
23078     {
23079       switch_to_section (debug_dcall_section);
23080       output_dcall_table ();
23081     }
23082   if (!VEC_empty (vcall_entry, vcall_table))
23083     {
23084       switch_to_section (debug_vcall_section);
23085       output_vcall_table ();
23086     }
23087
23088   /* Output the address range information.  We only put functions in the arange
23089      table, so don't write it out if we don't have any.  */
23090   if (fde_table_in_use)
23091     {
23092       switch_to_section (debug_aranges_section);
23093       output_aranges ();
23094     }
23095
23096   /* Output ranges section if necessary.  */
23097   if (ranges_table_in_use)
23098     {
23099       switch_to_section (debug_ranges_section);
23100       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23101       output_ranges ();
23102     }
23103
23104   /* Output the source line correspondence table.  We must do this
23105      even if there is no line information.  Otherwise, on an empty
23106      translation unit, we will generate a present, but empty,
23107      .debug_info section.  IRIX 6.5 `nm' will then complain when
23108      examining the file.  This is done late so that any filenames
23109      used by the debug_info section are marked as 'used'.  */
23110   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23111     {
23112       switch_to_section (debug_line_section);
23113       output_line_info ();
23114     }
23115
23116   /* Have to end the macro section.  */
23117   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23118     {
23119       switch_to_section (debug_macinfo_section);
23120       dw2_asm_output_data (1, 0, "End compilation unit");
23121     }
23122
23123   /* If we emitted any DW_FORM_strp form attribute, output the string
23124      table too.  */
23125   if (debug_str_hash)
23126     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23127 }
23128
23129 #include "gt-dwarf2out.h"