OSDN Git Service

PR debug/48466
[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, 2011
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 #include "cfglayout.h"
96
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104     which are output in the debug info should end with a separator.  */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107    from generating indirect strings in DWARF2 debug information, for instance
108    if your target is stuck with an old version of GDB that is unable to
109    process them properly or uses VMS Debug.  */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically.  They've been
117    totally replaced in this file; make sure it stays that way.  */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
126 #endif
127
128 /* Map register numbers held in the call frame info that gcc has
129    collected using DWARF_FRAME_REGNUM to those that should be output in
130    .debug_frame and .eh_frame.  */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
134
135 /* Save the result of dwarf2out_do_frame across PCH.  */
136 static GTY(()) bool saved_do_cfi_asm = 0;
137
138 /* Decide whether we want to emit frame unwind information for the current
139    translation unit.  */
140
141 int
142 dwarf2out_do_frame (void)
143 {
144   /* We want to emit correct CFA location expressions or lists, so we
145      have to return true if we're going to output debug info, even if
146      we're not going to output frame or unwind info.  */
147   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148     return true;
149
150   if (saved_do_cfi_asm)
151     return true;
152
153   if (targetm.debug_unwind_info () == UI_DWARF2)
154     return true;
155
156   if ((flag_unwind_tables || flag_exceptions)
157       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158     return true;
159
160   return false;
161 }
162
163 /* Decide whether to emit frame unwind via assembler directives.  */
164
165 int
166 dwarf2out_do_cfi_asm (void)
167 {
168   int enc;
169
170 #ifdef MIPS_DEBUGGING_INFO
171   return false;
172 #endif
173   if (saved_do_cfi_asm)
174     return true;
175   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176     return false;
177   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178     return false;
179
180   /* Make sure the personality encoding is one the assembler can support.
181      In particular, aligned addresses can't be handled.  */
182   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184     return false;
185   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187     return false;
188
189   /* If we can't get the assembler to emit only .debug_frame, and we don't need
190      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
191   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192       && !flag_unwind_tables && !flag_exceptions
193       && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194     return false;
195
196   saved_do_cfi_asm = true;
197   return true;
198 }
199
200 /* The size of the target's pointer type.  */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
204
205 /* Array of RTXes referenced by the debugging information, which therefore
206    must be kept around forever.  */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208
209 /* A pointer to the base of a list of incomplete types which might be
210    completed at some later time.  incomplete_types_list needs to be a
211    VEC(tree,gc) because we want to tell the garbage collector about
212    it.  */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
214
215 /* A pointer to the base of a table of references to declaration
216    scopes.  This table is a display which tracks the nesting
217    of declaration scopes at the current scope and containing
218    scopes.  This table is used to find the proper place to
219    define type declaration DIE's.  */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
221
222 /* Pointers to various DWARF2 sections.  */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
234
235 /* Personality decl of current unit.  Used only when assembler does not support
236    personality CFI.  */
237 static GTY(()) rtx current_unit_personality;
238
239 /* How to start an assembler comment.  */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
243
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
247
248 /* Call frames are described using a sequence of Call Frame
249    Information instructions.  The register number, offset
250    and address fields are provided as possible operands;
251    their use is selected by the opcode field.  */
252
253 enum dw_cfi_oprnd_type {
254   dw_cfi_oprnd_unused,
255   dw_cfi_oprnd_reg_num,
256   dw_cfi_oprnd_offset,
257   dw_cfi_oprnd_addr,
258   dw_cfi_oprnd_loc
259 };
260
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
266 }
267 dw_cfi_oprnd;
268
269 typedef struct GTY(()) dw_cfi_struct {
270   dw_cfi_ref dw_cfi_next;
271   enum dwarf_call_frame_info dw_cfi_opc;
272   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
273     dw_cfi_oprnd1;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd2;
276 }
277 dw_cfi_node;
278
279 /* This is how we define the location of the CFA. We use to handle it
280    as REG + OFFSET all the time,  but now it can be more complex.
281    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
282    Instead of passing around REG and OFFSET, we pass a copy
283    of this structure.  */
284 typedef struct cfa_loc {
285   HOST_WIDE_INT offset;
286   HOST_WIDE_INT base_offset;
287   unsigned int reg;
288   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
289   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
290 } dw_cfa_location;
291
292 /* All call frame descriptions (FDE's) in the GCC generated DWARF
293    refer to a single Common Information Entry (CIE), defined at
294    the beginning of the .debug_frame section.  This use of a single
295    CIE obviates the need to keep track of multiple CIE's
296    in the DWARF generation routines below.  */
297
298 typedef struct GTY(()) dw_fde_struct {
299   tree decl;
300   const char *dw_fde_begin;
301   const char *dw_fde_current_label;
302   const char *dw_fde_end;
303   const char *dw_fde_vms_end_prologue;
304   const char *dw_fde_vms_begin_epilogue;
305   const char *dw_fde_second_begin;
306   const char *dw_fde_second_end;
307   dw_cfi_ref dw_fde_cfi;
308   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
309   HOST_WIDE_INT stack_realignment;
310   unsigned funcdef_number;
311   /* Dynamic realign argument pointer register.  */
312   unsigned int drap_reg;
313   /* Virtual dynamic realign argument pointer register.  */
314   unsigned int vdrap_reg;
315   /* These 3 flags are copied from rtl_data in function.h.  */
316   unsigned all_throwers_are_sibcalls : 1;
317   unsigned uses_eh_lsda : 1;
318   unsigned nothrow : 1;
319   /* Whether we did stack realign in this call frame.  */
320   unsigned stack_realign : 1;
321   /* Whether dynamic realign argument pointer register has been saved.  */
322   unsigned drap_reg_saved: 1;
323   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
324   unsigned in_std_section : 1;
325   /* True iff dw_fde_second_begin label is in text_section or
326      cold_text_section.  */
327   unsigned second_in_std_section : 1;
328 }
329 dw_fde_node;
330
331 /* Maximum size (in bytes) of an artificially generated label.  */
332 #define MAX_ARTIFICIAL_LABEL_BYTES      30
333
334 /* The size of addresses as they appear in the Dwarf 2 data.
335    Some architectures use word addresses to refer to code locations,
336    but Dwarf 2 info always uses byte addresses.  On such machines,
337    Dwarf 2 addresses need to be larger than the architecture's
338    pointers.  */
339 #ifndef DWARF2_ADDR_SIZE
340 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
341 #endif
342
343 /* The size in bytes of a DWARF field indicating an offset or length
344    relative to a debug info section, specified to be 4 bytes in the
345    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
346    as PTR_SIZE.  */
347
348 #ifndef DWARF_OFFSET_SIZE
349 #define DWARF_OFFSET_SIZE 4
350 #endif
351
352 /* The size in bytes of a DWARF 4 type signature.  */
353
354 #ifndef DWARF_TYPE_SIGNATURE_SIZE
355 #define DWARF_TYPE_SIGNATURE_SIZE 8
356 #endif
357
358 /* According to the (draft) DWARF 3 specification, the initial length
359    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
360    bytes are 0xffffffff, followed by the length stored in the next 8
361    bytes.
362
363    However, the SGI/MIPS ABI uses an initial length which is equal to
364    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
365
366 #ifndef DWARF_INITIAL_LENGTH_SIZE
367 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
368 #endif
369
370 /* Round SIZE up to the nearest BOUNDARY.  */
371 #define DWARF_ROUND(SIZE,BOUNDARY) \
372   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
373
374 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
375 #ifndef DWARF_CIE_DATA_ALIGNMENT
376 #ifdef STACK_GROWS_DOWNWARD
377 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
378 #else
379 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
380 #endif
381 #endif
382
383 /* CIE identifier.  */
384 #if HOST_BITS_PER_WIDE_INT >= 64
385 #define DWARF_CIE_ID \
386   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
387 #else
388 #define DWARF_CIE_ID DW_CIE_ID
389 #endif
390
391 /* A pointer to the base of a table that contains frame description
392    information for each routine.  */
393 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
394
395 /* Number of elements currently allocated for fde_table.  */
396 static GTY(()) unsigned fde_table_allocated;
397
398 /* Number of elements in fde_table currently in use.  */
399 static GTY(()) unsigned fde_table_in_use;
400
401 /* Size (in elements) of increments by which we may expand the
402    fde_table.  */
403 #define FDE_TABLE_INCREMENT 256
404
405 /* Get the current fde_table entry we should use.  */
406
407 static inline dw_fde_ref
408 current_fde (void)
409 {
410   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
411 }
412
413 /* A list of call frame insns for the CIE.  */
414 static GTY(()) dw_cfi_ref cie_cfi_head;
415
416 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
417    attribute that accelerates the lookup of the FDE associated
418    with the subprogram.  This variable holds the table index of the FDE
419    associated with the current function (body) definition.  */
420 static unsigned current_funcdef_fde;
421
422 struct GTY(()) indirect_string_node {
423   const char *str;
424   unsigned int refcount;
425   enum dwarf_form form;
426   char *label;
427 };
428
429 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
430
431 /* True if the compilation unit has location entries that reference
432    debug strings.  */
433 static GTY(()) bool debug_str_hash_forced = false;
434
435 static GTY(()) int dw2_string_counter;
436 static GTY(()) unsigned long dwarf2out_cfi_label_num;
437
438 /* True if the compilation unit places functions in more than one section.  */
439 static GTY(()) bool have_multiple_function_sections = false;
440
441 /* Whether the default text and cold text sections have been used at all.  */
442
443 static GTY(()) bool text_section_used = false;
444 static GTY(()) bool cold_text_section_used = false;
445
446 /* The default cold text section.  */
447 static GTY(()) section *cold_text_section;
448
449 /* Forward declarations for functions defined in this file.  */
450
451 static char *stripattributes (const char *);
452 static const char *dwarf_cfi_name (unsigned);
453 static dw_cfi_ref new_cfi (void);
454 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
455 static void add_fde_cfi (const char *, dw_cfi_ref);
456 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
457 static void lookup_cfa (dw_cfa_location *);
458 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
459 static void initial_return_save (rtx);
460 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
461                                           HOST_WIDE_INT);
462 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
463 static void output_cfi_directive (dw_cfi_ref);
464 static void output_call_frame_info (int);
465 static void dwarf2out_note_section_used (void);
466 static bool clobbers_queued_reg_save (const_rtx);
467 static void dwarf2out_frame_debug_expr (rtx, const char *);
468
469 /* Support for complex CFA locations.  */
470 static void output_cfa_loc (dw_cfi_ref, int);
471 static void output_cfa_loc_raw (dw_cfi_ref);
472 static void get_cfa_from_loc_descr (dw_cfa_location *,
473                                     struct dw_loc_descr_struct *);
474 static struct dw_loc_descr_struct *build_cfa_loc
475   (dw_cfa_location *, HOST_WIDE_INT);
476 static struct dw_loc_descr_struct *build_cfa_aligned_loc
477   (HOST_WIDE_INT, HOST_WIDE_INT);
478 static void def_cfa_1 (const char *, dw_cfa_location *);
479 static struct dw_loc_descr_struct *mem_loc_descriptor
480   (rtx, enum machine_mode mode, enum var_init_status);
481
482 /* How to start an assembler comment.  */
483 #ifndef ASM_COMMENT_START
484 #define ASM_COMMENT_START ";#"
485 #endif
486
487 /* Data and reference forms for relocatable data.  */
488 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
489 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
490
491 #ifndef DEBUG_FRAME_SECTION
492 #define DEBUG_FRAME_SECTION     ".debug_frame"
493 #endif
494
495 #ifndef FUNC_BEGIN_LABEL
496 #define FUNC_BEGIN_LABEL        "LFB"
497 #endif
498
499 #ifndef FUNC_END_LABEL
500 #define FUNC_END_LABEL          "LFE"
501 #endif
502
503 #ifndef PROLOGUE_END_LABEL
504 #define PROLOGUE_END_LABEL      "LPE"
505 #endif
506
507 #ifndef EPILOGUE_BEGIN_LABEL
508 #define EPILOGUE_BEGIN_LABEL    "LEB"
509 #endif
510
511 #ifndef FRAME_BEGIN_LABEL
512 #define FRAME_BEGIN_LABEL       "Lframe"
513 #endif
514 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
515 #define CIE_END_LABEL           "LECIE"
516 #define FDE_LABEL               "LSFDE"
517 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
518 #define FDE_END_LABEL           "LEFDE"
519 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
520 #define LINE_NUMBER_END_LABEL   "LELT"
521 #define LN_PROLOG_AS_LABEL      "LASLTP"
522 #define LN_PROLOG_END_LABEL     "LELTP"
523 #define DIE_LABEL_PREFIX        "DW"
524
525 /* The DWARF 2 CFA column which tracks the return address.  Normally this
526    is the column for PC, or the first column after all of the hard
527    registers.  */
528 #ifndef DWARF_FRAME_RETURN_COLUMN
529 #ifdef PC_REGNUM
530 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
531 #else
532 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
533 #endif
534 #endif
535
536 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
537    default, we just provide columns for all registers.  */
538 #ifndef DWARF_FRAME_REGNUM
539 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
540 #endif
541 \f
542 /* Match the base name of a file to the base name of a compilation unit. */
543
544 static int
545 matches_main_base (const char *path)
546 {
547   /* Cache the last query. */
548   static const char *last_path = NULL;
549   static int last_match = 0;
550   if (path != last_path)
551     {
552       const char *base;
553       int length = base_of_path (path, &base);
554       last_path = path;
555       last_match = (length == main_input_baselength
556                     && memcmp (base, main_input_basename, length) == 0);
557     }
558   return last_match;
559 }
560
561 #ifdef DEBUG_DEBUG_STRUCT
562
563 static int
564 dump_struct_debug (tree type, enum debug_info_usage usage,
565                    enum debug_struct_file criterion, int generic,
566                    int matches, int result)
567 {
568   /* Find the type name. */
569   tree type_decl = TYPE_STUB_DECL (type);
570   tree t = type_decl;
571   const char *name = 0;
572   if (TREE_CODE (t) == TYPE_DECL)
573     t = DECL_NAME (t);
574   if (t)
575     name = IDENTIFIER_POINTER (t);
576
577   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
578            criterion,
579            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
580            matches ? "bas" : "hdr",
581            generic ? "gen" : "ord",
582            usage == DINFO_USAGE_DFN ? ";" :
583              usage == DINFO_USAGE_DIR_USE ? "." : "*",
584            result,
585            (void*) type_decl, name);
586   return result;
587 }
588 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
589   dump_struct_debug (type, usage, criterion, generic, matches, result)
590
591 #else
592
593 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
594   (result)
595
596 #endif
597
598 static bool
599 should_emit_struct_debug (tree type, enum debug_info_usage usage)
600 {
601   enum debug_struct_file criterion;
602   tree type_decl;
603   bool generic = lang_hooks.types.generic_p (type);
604
605   if (generic)
606     criterion = debug_struct_generic[usage];
607   else
608     criterion = debug_struct_ordinary[usage];
609
610   if (criterion == DINFO_STRUCT_FILE_NONE)
611     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
612   if (criterion == DINFO_STRUCT_FILE_ANY)
613     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
614
615   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
616
617   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
618     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
619
620   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
621     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
622   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
623 }
624 \f
625 /* Hook used by __throw.  */
626
627 rtx
628 expand_builtin_dwarf_sp_column (void)
629 {
630   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
631   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
632 }
633
634 /* Return a pointer to a copy of the section string name S with all
635    attributes stripped off, and an asterisk prepended (for assemble_name).  */
636
637 static inline char *
638 stripattributes (const char *s)
639 {
640   char *stripped = XNEWVEC (char, strlen (s) + 2);
641   char *p = stripped;
642
643   *p++ = '*';
644
645   while (*s && *s != ',')
646     *p++ = *s++;
647
648   *p = '\0';
649   return stripped;
650 }
651
652 /* MEM is a memory reference for the register size table, each element of
653    which has mode MODE.  Initialize column C as a return address column.  */
654
655 static void
656 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
657 {
658   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
659   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
660   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
661 }
662
663 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
664
665 static inline HOST_WIDE_INT
666 div_data_align (HOST_WIDE_INT off)
667 {
668   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
669   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
670   return r;
671 }
672
673 /* Return true if we need a signed version of a given opcode
674    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
675
676 static inline bool
677 need_data_align_sf_opcode (HOST_WIDE_INT off)
678 {
679   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
680 }
681
682 /* Generate code to initialize the register size table.  */
683
684 void
685 expand_builtin_init_dwarf_reg_sizes (tree address)
686 {
687   unsigned int i;
688   enum machine_mode mode = TYPE_MODE (char_type_node);
689   rtx addr = expand_normal (address);
690   rtx mem = gen_rtx_MEM (BLKmode, addr);
691   bool wrote_return_column = false;
692
693   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
694     {
695       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
696
697       if (rnum < DWARF_FRAME_REGISTERS)
698         {
699           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
700           enum machine_mode save_mode = reg_raw_mode[i];
701           HOST_WIDE_INT size;
702
703           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
704             save_mode = choose_hard_reg_mode (i, 1, true);
705           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
706             {
707               if (save_mode == VOIDmode)
708                 continue;
709               wrote_return_column = true;
710             }
711           size = GET_MODE_SIZE (save_mode);
712           if (offset < 0)
713             continue;
714
715           emit_move_insn (adjust_address (mem, mode, offset),
716                           gen_int_mode (size, mode));
717         }
718     }
719
720   if (!wrote_return_column)
721     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
722
723 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
724   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
725 #endif
726
727   targetm.init_dwarf_reg_sizes_extra (address);
728 }
729
730 /* Convert a DWARF call frame info. operation to its string name */
731
732 static const char *
733 dwarf_cfi_name (unsigned int cfi_opc)
734 {
735   switch (cfi_opc)
736     {
737     case DW_CFA_advance_loc:
738       return "DW_CFA_advance_loc";
739     case DW_CFA_offset:
740       return "DW_CFA_offset";
741     case DW_CFA_restore:
742       return "DW_CFA_restore";
743     case DW_CFA_nop:
744       return "DW_CFA_nop";
745     case DW_CFA_set_loc:
746       return "DW_CFA_set_loc";
747     case DW_CFA_advance_loc1:
748       return "DW_CFA_advance_loc1";
749     case DW_CFA_advance_loc2:
750       return "DW_CFA_advance_loc2";
751     case DW_CFA_advance_loc4:
752       return "DW_CFA_advance_loc4";
753     case DW_CFA_offset_extended:
754       return "DW_CFA_offset_extended";
755     case DW_CFA_restore_extended:
756       return "DW_CFA_restore_extended";
757     case DW_CFA_undefined:
758       return "DW_CFA_undefined";
759     case DW_CFA_same_value:
760       return "DW_CFA_same_value";
761     case DW_CFA_register:
762       return "DW_CFA_register";
763     case DW_CFA_remember_state:
764       return "DW_CFA_remember_state";
765     case DW_CFA_restore_state:
766       return "DW_CFA_restore_state";
767     case DW_CFA_def_cfa:
768       return "DW_CFA_def_cfa";
769     case DW_CFA_def_cfa_register:
770       return "DW_CFA_def_cfa_register";
771     case DW_CFA_def_cfa_offset:
772       return "DW_CFA_def_cfa_offset";
773
774     /* DWARF 3 */
775     case DW_CFA_def_cfa_expression:
776       return "DW_CFA_def_cfa_expression";
777     case DW_CFA_expression:
778       return "DW_CFA_expression";
779     case DW_CFA_offset_extended_sf:
780       return "DW_CFA_offset_extended_sf";
781     case DW_CFA_def_cfa_sf:
782       return "DW_CFA_def_cfa_sf";
783     case DW_CFA_def_cfa_offset_sf:
784       return "DW_CFA_def_cfa_offset_sf";
785
786     /* SGI/MIPS specific */
787     case DW_CFA_MIPS_advance_loc8:
788       return "DW_CFA_MIPS_advance_loc8";
789
790     /* GNU extensions */
791     case DW_CFA_GNU_window_save:
792       return "DW_CFA_GNU_window_save";
793     case DW_CFA_GNU_args_size:
794       return "DW_CFA_GNU_args_size";
795     case DW_CFA_GNU_negative_offset_extended:
796       return "DW_CFA_GNU_negative_offset_extended";
797
798     default:
799       return "DW_CFA_<unknown>";
800     }
801 }
802
803 /* Return a pointer to a newly allocated Call Frame Instruction.  */
804
805 static inline dw_cfi_ref
806 new_cfi (void)
807 {
808   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
809
810   cfi->dw_cfi_next = NULL;
811   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
812   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
813
814   return cfi;
815 }
816
817 /* Add a Call Frame Instruction to list of instructions.  */
818
819 static inline void
820 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
821 {
822   dw_cfi_ref *p;
823   dw_fde_ref fde = current_fde ();
824
825   /* When DRAP is used, CFA is defined with an expression.  Redefine
826      CFA may lead to a different CFA value.   */
827   /* ??? Of course, this heuristic fails when we're annotating epilogues,
828      because of course we'll always want to redefine the CFA back to the
829      stack pointer on the way out.  Where should we move this check?  */
830   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
831     switch (cfi->dw_cfi_opc)
832       {
833         case DW_CFA_def_cfa_register:
834         case DW_CFA_def_cfa_offset:
835         case DW_CFA_def_cfa_offset_sf:
836         case DW_CFA_def_cfa:
837         case DW_CFA_def_cfa_sf:
838           gcc_unreachable ();
839
840         default:
841           break;
842       }
843
844   /* Find the end of the chain.  */
845   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
846     ;
847
848   *p = cfi;
849 }
850
851 /* Generate a new label for the CFI info to refer to.  FORCE is true
852    if a label needs to be output even when using .cfi_* directives.  */
853
854 char *
855 dwarf2out_cfi_label (bool force)
856 {
857   static char label[20];
858
859   if (!force && dwarf2out_do_cfi_asm ())
860     {
861       /* In this case, we will be emitting the asm directive instead of
862          the label, so just return a placeholder to keep the rest of the
863          interfaces happy.  */
864       strcpy (label, "<do not output>");
865     }
866   else
867     {
868       int num = dwarf2out_cfi_label_num++;
869       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
870       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
871     }
872
873   return label;
874 }
875
876 /* True if remember_state should be emitted before following CFI directive.  */
877 static bool emit_cfa_remember;
878
879 /* True if any CFI directives were emitted at the current insn.  */
880 static bool any_cfis_emitted;
881
882 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
883    or to the CIE if LABEL is NULL.  */
884
885 static void
886 add_fde_cfi (const char *label, dw_cfi_ref cfi)
887 {
888   dw_cfi_ref *list_head;
889
890   if (emit_cfa_remember)
891     {
892       dw_cfi_ref cfi_remember;
893
894       /* Emit the state save.  */
895       emit_cfa_remember = false;
896       cfi_remember = new_cfi ();
897       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
898       add_fde_cfi (label, cfi_remember);
899     }
900
901   list_head = &cie_cfi_head;
902
903   if (dwarf2out_do_cfi_asm ())
904     {
905       if (label)
906         {
907           dw_fde_ref fde = current_fde ();
908
909           gcc_assert (fde != NULL);
910
911           /* We still have to add the cfi to the list so that lookup_cfa
912              works later on.  When -g2 and above we even need to force
913              emitting of CFI labels and add to list a DW_CFA_set_loc for
914              convert_cfa_to_fb_loc_list purposes.  If we're generating
915              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
916              convert_cfa_to_fb_loc_list.  */
917           if (dwarf_version == 2
918               && debug_info_level > DINFO_LEVEL_TERSE
919               && (write_symbols == DWARF2_DEBUG
920                   || write_symbols == VMS_AND_DWARF2_DEBUG))
921             {
922               switch (cfi->dw_cfi_opc)
923                 {
924                 case DW_CFA_def_cfa_offset:
925                 case DW_CFA_def_cfa_offset_sf:
926                 case DW_CFA_def_cfa_register:
927                 case DW_CFA_def_cfa:
928                 case DW_CFA_def_cfa_sf:
929                 case DW_CFA_def_cfa_expression:
930                 case DW_CFA_restore_state:
931                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
932                     label = dwarf2out_cfi_label (true);
933
934                   if (fde->dw_fde_current_label == NULL
935                       || strcmp (label, fde->dw_fde_current_label) != 0)
936                     {
937                       dw_cfi_ref xcfi;
938
939                       label = xstrdup (label);
940
941                       /* Set the location counter to the new label.  */
942                       xcfi = new_cfi ();
943                       /* It doesn't metter whether DW_CFA_set_loc
944                          or DW_CFA_advance_loc4 is added here, those aren't
945                          emitted into assembly, only looked up by
946                          convert_cfa_to_fb_loc_list.  */
947                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
948                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
949                       add_cfi (&fde->dw_fde_cfi, xcfi);
950                       fde->dw_fde_current_label = label;
951                     }
952                   break;
953                 default:
954                   break;
955                 }
956             }
957
958           output_cfi_directive (cfi);
959
960           list_head = &fde->dw_fde_cfi;
961           any_cfis_emitted = true;
962         }
963       /* ??? If this is a CFI for the CIE, we don't emit.  This
964          assumes that the standard CIE contents that the assembler
965          uses matches the standard CIE contents that the compiler
966          uses.  This is probably a bad assumption.  I'm not quite
967          sure how to address this for now.  */
968     }
969   else if (label)
970     {
971       dw_fde_ref fde = current_fde ();
972
973       gcc_assert (fde != NULL);
974
975       if (*label == 0)
976         label = dwarf2out_cfi_label (false);
977
978       if (fde->dw_fde_current_label == NULL
979           || strcmp (label, fde->dw_fde_current_label) != 0)
980         {
981           dw_cfi_ref xcfi;
982
983           label = xstrdup (label);
984
985           /* Set the location counter to the new label.  */
986           xcfi = new_cfi ();
987           /* If we have a current label, advance from there, otherwise
988              set the location directly using set_loc.  */
989           xcfi->dw_cfi_opc = fde->dw_fde_current_label
990                              ? DW_CFA_advance_loc4
991                              : DW_CFA_set_loc;
992           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
993           add_cfi (&fde->dw_fde_cfi, xcfi);
994
995           fde->dw_fde_current_label = label;
996         }
997
998       list_head = &fde->dw_fde_cfi;
999       any_cfis_emitted = true;
1000     }
1001
1002   add_cfi (list_head, cfi);
1003 }
1004
1005 /* Subroutine of lookup_cfa.  */
1006
1007 static void
1008 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1009 {
1010   switch (cfi->dw_cfi_opc)
1011     {
1012     case DW_CFA_def_cfa_offset:
1013     case DW_CFA_def_cfa_offset_sf:
1014       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1015       break;
1016     case DW_CFA_def_cfa_register:
1017       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1018       break;
1019     case DW_CFA_def_cfa:
1020     case DW_CFA_def_cfa_sf:
1021       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1022       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1023       break;
1024     case DW_CFA_def_cfa_expression:
1025       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1026       break;
1027
1028     case DW_CFA_remember_state:
1029       gcc_assert (!remember->in_use);
1030       *remember = *loc;
1031       remember->in_use = 1;
1032       break;
1033     case DW_CFA_restore_state:
1034       gcc_assert (remember->in_use);
1035       *loc = *remember;
1036       remember->in_use = 0;
1037       break;
1038
1039     default:
1040       break;
1041     }
1042 }
1043
1044 /* Find the previous value for the CFA.  */
1045
1046 static void
1047 lookup_cfa (dw_cfa_location *loc)
1048 {
1049   dw_cfi_ref cfi;
1050   dw_fde_ref fde;
1051   dw_cfa_location remember;
1052
1053   memset (loc, 0, sizeof (*loc));
1054   loc->reg = INVALID_REGNUM;
1055   remember = *loc;
1056
1057   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1058     lookup_cfa_1 (cfi, loc, &remember);
1059
1060   fde = current_fde ();
1061   if (fde)
1062     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1063       lookup_cfa_1 (cfi, loc, &remember);
1064 }
1065
1066 /* The current rule for calculating the DWARF2 canonical frame address.  */
1067 static dw_cfa_location cfa;
1068
1069 /* The register used for saving registers to the stack, and its offset
1070    from the CFA.  */
1071 static dw_cfa_location cfa_store;
1072
1073 /* The current save location around an epilogue.  */
1074 static dw_cfa_location cfa_remember;
1075
1076 /* The running total of the size of arguments pushed onto the stack.  */
1077 static HOST_WIDE_INT args_size;
1078
1079 /* The last args_size we actually output.  */
1080 static HOST_WIDE_INT old_args_size;
1081
1082 /* Entry point to update the canonical frame address (CFA).
1083    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1084    calculated from REG+OFFSET.  */
1085
1086 void
1087 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1088 {
1089   dw_cfa_location loc;
1090   loc.indirect = 0;
1091   loc.base_offset = 0;
1092   loc.reg = reg;
1093   loc.offset = offset;
1094   def_cfa_1 (label, &loc);
1095 }
1096
1097 /* Determine if two dw_cfa_location structures define the same data.  */
1098
1099 static bool
1100 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1101 {
1102   return (loc1->reg == loc2->reg
1103           && loc1->offset == loc2->offset
1104           && loc1->indirect == loc2->indirect
1105           && (loc1->indirect == 0
1106               || loc1->base_offset == loc2->base_offset));
1107 }
1108
1109 /* This routine does the actual work.  The CFA is now calculated from
1110    the dw_cfa_location structure.  */
1111
1112 static void
1113 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1114 {
1115   dw_cfi_ref cfi;
1116   dw_cfa_location old_cfa, loc;
1117
1118   cfa = *loc_p;
1119   loc = *loc_p;
1120
1121   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1122     cfa_store.offset = loc.offset;
1123
1124   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1125   lookup_cfa (&old_cfa);
1126
1127   /* If nothing changed, no need to issue any call frame instructions.  */
1128   if (cfa_equal_p (&loc, &old_cfa))
1129     return;
1130
1131   cfi = new_cfi ();
1132
1133   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1134     {
1135       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1136          the CFA register did not change but the offset did.  The data
1137          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1138          in the assembler via the .cfi_def_cfa_offset directive.  */
1139       if (loc.offset < 0)
1140         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1141       else
1142         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1143       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1144     }
1145
1146 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1147   else if (loc.offset == old_cfa.offset
1148            && old_cfa.reg != INVALID_REGNUM
1149            && !loc.indirect
1150            && !old_cfa.indirect)
1151     {
1152       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1153          indicating the CFA register has changed to <register> but the
1154          offset has not changed.  */
1155       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1156       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1157     }
1158 #endif
1159
1160   else if (loc.indirect == 0)
1161     {
1162       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1163          indicating the CFA register has changed to <register> with
1164          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1165          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1166          directive.  */
1167       if (loc.offset < 0)
1168         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1169       else
1170         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1171       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1172       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1173     }
1174   else
1175     {
1176       /* Construct a DW_CFA_def_cfa_expression instruction to
1177          calculate the CFA using a full location expression since no
1178          register-offset pair is available.  */
1179       struct dw_loc_descr_struct *loc_list;
1180
1181       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1182       loc_list = build_cfa_loc (&loc, 0);
1183       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1184     }
1185
1186   add_fde_cfi (label, cfi);
1187 }
1188
1189 /* Add the CFI for saving a register.  REG is the CFA column number.
1190    LABEL is passed to add_fde_cfi.
1191    If SREG is -1, the register is saved at OFFSET from the CFA;
1192    otherwise it is saved in SREG.  */
1193
1194 static void
1195 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1196 {
1197   dw_cfi_ref cfi = new_cfi ();
1198   dw_fde_ref fde = current_fde ();
1199
1200   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1201
1202   /* When stack is aligned, store REG using DW_CFA_expression with
1203      FP.  */
1204   if (fde
1205       && fde->stack_realign
1206       && sreg == INVALID_REGNUM)
1207     {
1208       cfi->dw_cfi_opc = DW_CFA_expression;
1209       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1210       cfi->dw_cfi_oprnd2.dw_cfi_loc
1211         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1212     }
1213   else if (sreg == INVALID_REGNUM)
1214     {
1215       if (need_data_align_sf_opcode (offset))
1216         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1217       else if (reg & ~0x3f)
1218         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1219       else
1220         cfi->dw_cfi_opc = DW_CFA_offset;
1221       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1222     }
1223   else if (sreg == reg)
1224     cfi->dw_cfi_opc = DW_CFA_same_value;
1225   else
1226     {
1227       cfi->dw_cfi_opc = DW_CFA_register;
1228       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1229     }
1230
1231   add_fde_cfi (label, cfi);
1232 }
1233
1234 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1235    This CFI tells the unwinder that it needs to restore the window registers
1236    from the previous frame's window save area.
1237
1238    ??? Perhaps we should note in the CIE where windows are saved (instead of
1239    assuming 0(cfa)) and what registers are in the window.  */
1240
1241 void
1242 dwarf2out_window_save (const char *label)
1243 {
1244   dw_cfi_ref cfi = new_cfi ();
1245
1246   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1247   add_fde_cfi (label, cfi);
1248 }
1249
1250 /* Entry point for saving a register to the stack.  REG is the GCC register
1251    number.  LABEL and OFFSET are passed to reg_save.  */
1252
1253 void
1254 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1255 {
1256   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1257 }
1258
1259 /* Entry point for saving the return address in the stack.
1260    LABEL and OFFSET are passed to reg_save.  */
1261
1262 void
1263 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1264 {
1265   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1266 }
1267
1268 /* Entry point for saving the return address in a register.
1269    LABEL and SREG are passed to reg_save.  */
1270
1271 void
1272 dwarf2out_return_reg (const char *label, unsigned int sreg)
1273 {
1274   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1275 }
1276
1277 /* Record the initial position of the return address.  RTL is
1278    INCOMING_RETURN_ADDR_RTX.  */
1279
1280 static void
1281 initial_return_save (rtx rtl)
1282 {
1283   unsigned int reg = INVALID_REGNUM;
1284   HOST_WIDE_INT offset = 0;
1285
1286   switch (GET_CODE (rtl))
1287     {
1288     case REG:
1289       /* RA is in a register.  */
1290       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1291       break;
1292
1293     case MEM:
1294       /* RA is on the stack.  */
1295       rtl = XEXP (rtl, 0);
1296       switch (GET_CODE (rtl))
1297         {
1298         case REG:
1299           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1300           offset = 0;
1301           break;
1302
1303         case PLUS:
1304           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1305           offset = INTVAL (XEXP (rtl, 1));
1306           break;
1307
1308         case MINUS:
1309           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1310           offset = -INTVAL (XEXP (rtl, 1));
1311           break;
1312
1313         default:
1314           gcc_unreachable ();
1315         }
1316
1317       break;
1318
1319     case PLUS:
1320       /* The return address is at some offset from any value we can
1321          actually load.  For instance, on the SPARC it is in %i7+8. Just
1322          ignore the offset for now; it doesn't matter for unwinding frames.  */
1323       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1324       initial_return_save (XEXP (rtl, 0));
1325       return;
1326
1327     default:
1328       gcc_unreachable ();
1329     }
1330
1331   if (reg != DWARF_FRAME_RETURN_COLUMN)
1332     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1333 }
1334
1335 /* Given a SET, calculate the amount of stack adjustment it
1336    contains.  */
1337
1338 static HOST_WIDE_INT
1339 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1340                      HOST_WIDE_INT cur_offset)
1341 {
1342   const_rtx src = SET_SRC (pattern);
1343   const_rtx dest = SET_DEST (pattern);
1344   HOST_WIDE_INT offset = 0;
1345   enum rtx_code code;
1346
1347   if (dest == stack_pointer_rtx)
1348     {
1349       code = GET_CODE (src);
1350
1351       /* Assume (set (reg sp) (reg whatever)) sets args_size
1352          level to 0.  */
1353       if (code == REG && src != stack_pointer_rtx)
1354         {
1355           offset = -cur_args_size;
1356 #ifndef STACK_GROWS_DOWNWARD
1357           offset = -offset;
1358 #endif
1359           return offset - cur_offset;
1360         }
1361
1362       if (! (code == PLUS || code == MINUS)
1363           || XEXP (src, 0) != stack_pointer_rtx
1364           || !CONST_INT_P (XEXP (src, 1)))
1365         return 0;
1366
1367       /* (set (reg sp) (plus (reg sp) (const_int))) */
1368       offset = INTVAL (XEXP (src, 1));
1369       if (code == PLUS)
1370         offset = -offset;
1371       return offset;
1372     }
1373
1374   if (MEM_P (src) && !MEM_P (dest))
1375     dest = src;
1376   if (MEM_P (dest))
1377     {
1378       /* (set (mem (pre_dec (reg sp))) (foo)) */
1379       src = XEXP (dest, 0);
1380       code = GET_CODE (src);
1381
1382       switch (code)
1383         {
1384         case PRE_MODIFY:
1385         case POST_MODIFY:
1386           if (XEXP (src, 0) == stack_pointer_rtx)
1387             {
1388               rtx val = XEXP (XEXP (src, 1), 1);
1389               /* We handle only adjustments by constant amount.  */
1390               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1391                           && CONST_INT_P (val));
1392               offset = -INTVAL (val);
1393               break;
1394             }
1395           return 0;
1396
1397         case PRE_DEC:
1398         case POST_DEC:
1399           if (XEXP (src, 0) == stack_pointer_rtx)
1400             {
1401               offset = GET_MODE_SIZE (GET_MODE (dest));
1402               break;
1403             }
1404           return 0;
1405
1406         case PRE_INC:
1407         case POST_INC:
1408           if (XEXP (src, 0) == stack_pointer_rtx)
1409             {
1410               offset = -GET_MODE_SIZE (GET_MODE (dest));
1411               break;
1412             }
1413           return 0;
1414
1415         default:
1416           return 0;
1417         }
1418     }
1419   else
1420     return 0;
1421
1422   return offset;
1423 }
1424
1425 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1426    indexed by INSN_UID.  */
1427
1428 static HOST_WIDE_INT *barrier_args_size;
1429
1430 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1431
1432 static HOST_WIDE_INT
1433 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1434                              VEC (rtx, heap) **next)
1435 {
1436   HOST_WIDE_INT offset = 0;
1437   int i;
1438
1439   if (! RTX_FRAME_RELATED_P (insn))
1440     {
1441       if (prologue_epilogue_contains (insn))
1442         /* Nothing */;
1443       else if (GET_CODE (PATTERN (insn)) == SET)
1444         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1445       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1446                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1447         {
1448           /* There may be stack adjustments inside compound insns.  Search
1449              for them.  */
1450           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1451             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1452               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1453                                              cur_args_size, offset);
1454         }
1455     }
1456   else
1457     {
1458       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1459
1460       if (expr)
1461         {
1462           expr = XEXP (expr, 0);
1463           if (GET_CODE (expr) == PARALLEL
1464               || GET_CODE (expr) == SEQUENCE)
1465             for (i = 1; i < XVECLEN (expr, 0); i++)
1466               {
1467                 rtx elem = XVECEXP (expr, 0, i);
1468
1469                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1470                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1471               }
1472         }
1473     }
1474
1475 #ifndef STACK_GROWS_DOWNWARD
1476   offset = -offset;
1477 #endif
1478
1479   cur_args_size += offset;
1480   if (cur_args_size < 0)
1481     cur_args_size = 0;
1482
1483   if (JUMP_P (insn))
1484     {
1485       rtx dest = JUMP_LABEL (insn);
1486
1487       if (dest)
1488         {
1489           if (barrier_args_size [INSN_UID (dest)] < 0)
1490             {
1491               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1492               VEC_safe_push (rtx, heap, *next, dest);
1493             }
1494         }
1495     }
1496
1497   return cur_args_size;
1498 }
1499
1500 /* Walk the whole function and compute args_size on BARRIERs.  */
1501
1502 static void
1503 compute_barrier_args_size (void)
1504 {
1505   int max_uid = get_max_uid (), i;
1506   rtx insn;
1507   VEC (rtx, heap) *worklist, *next, *tmp;
1508
1509   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1510   for (i = 0; i < max_uid; i++)
1511     barrier_args_size[i] = -1;
1512
1513   worklist = VEC_alloc (rtx, heap, 20);
1514   next = VEC_alloc (rtx, heap, 20);
1515   insn = get_insns ();
1516   barrier_args_size[INSN_UID (insn)] = 0;
1517   VEC_quick_push (rtx, worklist, insn);
1518   for (;;)
1519     {
1520       while (!VEC_empty (rtx, worklist))
1521         {
1522           rtx prev, body, first_insn;
1523           HOST_WIDE_INT cur_args_size;
1524
1525           first_insn = insn = VEC_pop (rtx, worklist);
1526           cur_args_size = barrier_args_size[INSN_UID (insn)];
1527           prev = prev_nonnote_insn (insn);
1528           if (prev && BARRIER_P (prev))
1529             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1530
1531           for (; insn; insn = NEXT_INSN (insn))
1532             {
1533               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1534                 continue;
1535               if (BARRIER_P (insn))
1536                 break;
1537
1538               if (LABEL_P (insn))
1539                 {
1540                   if (insn == first_insn)
1541                     continue;
1542                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1543                     {
1544                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1545                       continue;
1546                     }
1547                   else
1548                     {
1549                       /* The insns starting with this label have been
1550                          already scanned or are in the worklist.  */
1551                       break;
1552                     }
1553                 }
1554
1555               body = PATTERN (insn);
1556               if (GET_CODE (body) == SEQUENCE)
1557                 {
1558                   HOST_WIDE_INT dest_args_size = cur_args_size;
1559                   for (i = 1; i < XVECLEN (body, 0); i++)
1560                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1561                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1562                       dest_args_size
1563                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1564                                                        dest_args_size, &next);
1565                     else
1566                       cur_args_size
1567                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1568                                                        cur_args_size, &next);
1569
1570                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1571                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1572                                                  dest_args_size, &next);
1573                   else
1574                     cur_args_size
1575                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1576                                                      cur_args_size, &next);
1577                 }
1578               else
1579                 cur_args_size
1580                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1581             }
1582         }
1583
1584       if (VEC_empty (rtx, next))
1585         break;
1586
1587       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1588       tmp = next;
1589       next = worklist;
1590       worklist = tmp;
1591       VEC_truncate (rtx, next, 0);
1592     }
1593
1594   VEC_free (rtx, heap, worklist);
1595   VEC_free (rtx, heap, next);
1596 }
1597
1598 /* Add a CFI to update the running total of the size of arguments
1599    pushed onto the stack.  */
1600
1601 static void
1602 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1603 {
1604   dw_cfi_ref cfi;
1605
1606   if (size == old_args_size)
1607     return;
1608
1609   old_args_size = size;
1610
1611   cfi = new_cfi ();
1612   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1613   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1614   add_fde_cfi (label, cfi);
1615 }
1616
1617 /* Record a stack adjustment of OFFSET bytes.  */
1618
1619 static void
1620 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1621 {
1622   if (cfa.reg == STACK_POINTER_REGNUM)
1623     cfa.offset += offset;
1624
1625   if (cfa_store.reg == STACK_POINTER_REGNUM)
1626     cfa_store.offset += offset;
1627
1628   if (ACCUMULATE_OUTGOING_ARGS)
1629     return;
1630
1631 #ifndef STACK_GROWS_DOWNWARD
1632   offset = -offset;
1633 #endif
1634
1635   args_size += offset;
1636   if (args_size < 0)
1637     args_size = 0;
1638
1639   def_cfa_1 (label, &cfa);
1640   if (flag_asynchronous_unwind_tables)
1641     dwarf2out_args_size (label, args_size);
1642 }
1643
1644 /* Check INSN to see if it looks like a push or a stack adjustment, and
1645    make a note of it if it does.  EH uses this information to find out
1646    how much extra space it needs to pop off the stack.  */
1647
1648 static void
1649 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1650 {
1651   HOST_WIDE_INT offset;
1652   const char *label;
1653   int i;
1654
1655   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1656      with this function.  Proper support would require all frame-related
1657      insns to be marked, and to be able to handle saving state around
1658      epilogues textually in the middle of the function.  */
1659   if (prologue_epilogue_contains (insn))
1660     return;
1661
1662   /* If INSN is an instruction from target of an annulled branch, the
1663      effects are for the target only and so current argument size
1664      shouldn't change at all.  */
1665   if (final_sequence
1666       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1667       && INSN_FROM_TARGET_P (insn))
1668     return;
1669
1670   /* If only calls can throw, and we have a frame pointer,
1671      save up adjustments until we see the CALL_INSN.  */
1672   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1673     {
1674       if (CALL_P (insn) && !after_p)
1675         {
1676           /* Extract the size of the args from the CALL rtx itself.  */
1677           insn = PATTERN (insn);
1678           if (GET_CODE (insn) == PARALLEL)
1679             insn = XVECEXP (insn, 0, 0);
1680           if (GET_CODE (insn) == SET)
1681             insn = SET_SRC (insn);
1682           gcc_assert (GET_CODE (insn) == CALL);
1683           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1684         }
1685       return;
1686     }
1687
1688   if (CALL_P (insn) && !after_p)
1689     {
1690       if (!flag_asynchronous_unwind_tables)
1691         dwarf2out_args_size ("", args_size);
1692       return;
1693     }
1694   else if (BARRIER_P (insn))
1695     {
1696       /* Don't call compute_barrier_args_size () if the only
1697          BARRIER is at the end of function.  */
1698       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1699         compute_barrier_args_size ();
1700       if (barrier_args_size == NULL)
1701         offset = 0;
1702       else
1703         {
1704           offset = barrier_args_size[INSN_UID (insn)];
1705           if (offset < 0)
1706             offset = 0;
1707         }
1708
1709       offset -= args_size;
1710 #ifndef STACK_GROWS_DOWNWARD
1711       offset = -offset;
1712 #endif
1713     }
1714   else if (GET_CODE (PATTERN (insn)) == SET)
1715     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1716   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1717            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1718     {
1719       /* There may be stack adjustments inside compound insns.  Search
1720          for them.  */
1721       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1722         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1723           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1724                                          args_size, offset);
1725     }
1726   else
1727     return;
1728
1729   if (offset == 0)
1730     return;
1731
1732   label = dwarf2out_cfi_label (false);
1733   dwarf2out_stack_adjust (offset, label);
1734 }
1735
1736 /* We delay emitting a register save until either (a) we reach the end
1737    of the prologue or (b) the register is clobbered.  This clusters
1738    register saves so that there are fewer pc advances.  */
1739
1740 struct GTY(()) queued_reg_save {
1741   struct queued_reg_save *next;
1742   rtx reg;
1743   HOST_WIDE_INT cfa_offset;
1744   rtx saved_reg;
1745 };
1746
1747 static GTY(()) struct queued_reg_save *queued_reg_saves;
1748
1749 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1750 struct GTY(()) reg_saved_in_data {
1751   rtx orig_reg;
1752   rtx saved_in_reg;
1753 };
1754
1755 /* A list of registers saved in other registers.
1756    The list intentionally has a small maximum capacity of 4; if your
1757    port needs more than that, you might consider implementing a
1758    more efficient data structure.  */
1759 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1760 static GTY(()) size_t num_regs_saved_in_regs;
1761
1762 static const char *last_reg_save_label;
1763
1764 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1765    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1766
1767 static void
1768 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1769 {
1770   struct queued_reg_save *q;
1771
1772   /* Duplicates waste space, but it's also necessary to remove them
1773      for correctness, since the queue gets output in reverse
1774      order.  */
1775   for (q = queued_reg_saves; q != NULL; q = q->next)
1776     if (REGNO (q->reg) == REGNO (reg))
1777       break;
1778
1779   if (q == NULL)
1780     {
1781       q = ggc_alloc_queued_reg_save ();
1782       q->next = queued_reg_saves;
1783       queued_reg_saves = q;
1784     }
1785
1786   q->reg = reg;
1787   q->cfa_offset = offset;
1788   q->saved_reg = sreg;
1789
1790   last_reg_save_label = label;
1791 }
1792
1793 /* Output all the entries in QUEUED_REG_SAVES.  */
1794
1795 void
1796 dwarf2out_flush_queued_reg_saves (void)
1797 {
1798   struct queued_reg_save *q;
1799
1800   for (q = queued_reg_saves; q; q = q->next)
1801     {
1802       size_t i;
1803       unsigned int reg, sreg;
1804
1805       for (i = 0; i < num_regs_saved_in_regs; i++)
1806         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1807           break;
1808       if (q->saved_reg && i == num_regs_saved_in_regs)
1809         {
1810           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1811           num_regs_saved_in_regs++;
1812         }
1813       if (i != num_regs_saved_in_regs)
1814         {
1815           regs_saved_in_regs[i].orig_reg = q->reg;
1816           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1817         }
1818
1819       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1820       if (q->saved_reg)
1821         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1822       else
1823         sreg = INVALID_REGNUM;
1824       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1825     }
1826
1827   queued_reg_saves = NULL;
1828   last_reg_save_label = NULL;
1829 }
1830
1831 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1832    location for?  Or, does it clobber a register which we've previously
1833    said that some other register is saved in, and for which we now
1834    have a new location for?  */
1835
1836 static bool
1837 clobbers_queued_reg_save (const_rtx insn)
1838 {
1839   struct queued_reg_save *q;
1840
1841   for (q = queued_reg_saves; q; q = q->next)
1842     {
1843       size_t i;
1844       if (modified_in_p (q->reg, insn))
1845         return true;
1846       for (i = 0; i < num_regs_saved_in_regs; i++)
1847         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1848             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1849           return true;
1850     }
1851
1852   return false;
1853 }
1854
1855 /* Entry point for saving the first register into the second.  */
1856
1857 void
1858 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1859 {
1860   size_t i;
1861   unsigned int regno, sregno;
1862
1863   for (i = 0; i < num_regs_saved_in_regs; i++)
1864     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1865       break;
1866   if (i == num_regs_saved_in_regs)
1867     {
1868       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1869       num_regs_saved_in_regs++;
1870     }
1871   regs_saved_in_regs[i].orig_reg = reg;
1872   regs_saved_in_regs[i].saved_in_reg = sreg;
1873
1874   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1875   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1876   reg_save (label, regno, sregno, 0);
1877 }
1878
1879 /* What register, if any, is currently saved in REG?  */
1880
1881 static rtx
1882 reg_saved_in (rtx reg)
1883 {
1884   unsigned int regn = REGNO (reg);
1885   size_t i;
1886   struct queued_reg_save *q;
1887
1888   for (q = queued_reg_saves; q; q = q->next)
1889     if (q->saved_reg && regn == REGNO (q->saved_reg))
1890       return q->reg;
1891
1892   for (i = 0; i < num_regs_saved_in_regs; i++)
1893     if (regs_saved_in_regs[i].saved_in_reg
1894         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1895       return regs_saved_in_regs[i].orig_reg;
1896
1897   return NULL_RTX;
1898 }
1899
1900
1901 /* A temporary register holding an integral value used in adjusting SP
1902    or setting up the store_reg.  The "offset" field holds the integer
1903    value, not an offset.  */
1904 static dw_cfa_location cfa_temp;
1905
1906 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1907
1908 static void
1909 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1910 {
1911   memset (&cfa, 0, sizeof (cfa));
1912
1913   switch (GET_CODE (pat))
1914     {
1915     case PLUS:
1916       cfa.reg = REGNO (XEXP (pat, 0));
1917       cfa.offset = INTVAL (XEXP (pat, 1));
1918       break;
1919
1920     case REG:
1921       cfa.reg = REGNO (pat);
1922       break;
1923
1924     case MEM:
1925       cfa.indirect = 1;
1926       pat = XEXP (pat, 0);
1927       if (GET_CODE (pat) == PLUS)
1928         {
1929           cfa.base_offset = INTVAL (XEXP (pat, 1));
1930           pat = XEXP (pat, 0);
1931         }
1932       cfa.reg = REGNO (pat);
1933       break;
1934
1935     default:
1936       /* Recurse and define an expression.  */
1937       gcc_unreachable ();
1938     }
1939
1940   def_cfa_1 (label, &cfa);
1941 }
1942
1943 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1944
1945 static void
1946 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1947 {
1948   rtx src, dest;
1949
1950   gcc_assert (GET_CODE (pat) == SET);
1951   dest = XEXP (pat, 0);
1952   src = XEXP (pat, 1);
1953
1954   switch (GET_CODE (src))
1955     {
1956     case PLUS:
1957       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1958       cfa.offset -= INTVAL (XEXP (src, 1));
1959       break;
1960
1961     case REG:
1962         break;
1963
1964     default:
1965         gcc_unreachable ();
1966     }
1967
1968   cfa.reg = REGNO (dest);
1969   gcc_assert (cfa.indirect == 0);
1970
1971   def_cfa_1 (label, &cfa);
1972 }
1973
1974 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1975
1976 static void
1977 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1978 {
1979   HOST_WIDE_INT offset;
1980   rtx src, addr, span;
1981
1982   src = XEXP (set, 1);
1983   addr = XEXP (set, 0);
1984   gcc_assert (MEM_P (addr));
1985   addr = XEXP (addr, 0);
1986
1987   /* As documented, only consider extremely simple addresses.  */
1988   switch (GET_CODE (addr))
1989     {
1990     case REG:
1991       gcc_assert (REGNO (addr) == cfa.reg);
1992       offset = -cfa.offset;
1993       break;
1994     case PLUS:
1995       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1996       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1997       break;
1998     default:
1999       gcc_unreachable ();
2000     }
2001
2002   span = targetm.dwarf_register_span (src);
2003
2004   /* ??? We'd like to use queue_reg_save, but we need to come up with
2005      a different flushing heuristic for epilogues.  */
2006   if (!span)
2007     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2008   else
2009     {
2010       /* We have a PARALLEL describing where the contents of SRC live.
2011          Queue register saves for each piece of the PARALLEL.  */
2012       int par_index;
2013       int limit;
2014       HOST_WIDE_INT span_offset = offset;
2015
2016       gcc_assert (GET_CODE (span) == PARALLEL);
2017
2018       limit = XVECLEN (span, 0);
2019       for (par_index = 0; par_index < limit; par_index++)
2020         {
2021           rtx elem = XVECEXP (span, 0, par_index);
2022
2023           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2024                     INVALID_REGNUM, span_offset);
2025           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2026         }
2027     }
2028 }
2029
2030 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2031
2032 static void
2033 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2034 {
2035   rtx src, dest;
2036   unsigned sregno, dregno;
2037
2038   src = XEXP (set, 1);
2039   dest = XEXP (set, 0);
2040
2041   if (src == pc_rtx)
2042     sregno = DWARF_FRAME_RETURN_COLUMN;
2043   else
2044     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2045
2046   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2047
2048   /* ??? We'd like to use queue_reg_save, but we need to come up with
2049      a different flushing heuristic for epilogues.  */
2050   reg_save (label, sregno, dregno, 0);
2051 }
2052
2053 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2054
2055 static void
2056 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2057 {
2058   rtx src, dest, span;
2059   dw_cfi_ref cfi = new_cfi ();
2060
2061   dest = SET_DEST (set);
2062   src = SET_SRC (set);
2063
2064   gcc_assert (REG_P (src));
2065   gcc_assert (MEM_P (dest));
2066
2067   span = targetm.dwarf_register_span (src);
2068   gcc_assert (!span);
2069
2070   cfi->dw_cfi_opc = DW_CFA_expression;
2071   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2072   cfi->dw_cfi_oprnd2.dw_cfi_loc
2073     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2074                           VAR_INIT_STATUS_INITIALIZED);
2075
2076   /* ??? We'd like to use queue_reg_save, were the interface different,
2077      and, as above, we could manage flushing for epilogues.  */
2078   add_fde_cfi (label, cfi);
2079 }
2080
2081 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2082
2083 static void
2084 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2085 {
2086   dw_cfi_ref cfi = new_cfi ();
2087   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2088
2089   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2090   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2091
2092   add_fde_cfi (label, cfi);
2093 }
2094
2095 /* Record call frame debugging information for an expression EXPR,
2096    which either sets SP or FP (adjusting how we calculate the frame
2097    address) or saves a register to the stack or another register.
2098    LABEL indicates the address of EXPR.
2099
2100    This function encodes a state machine mapping rtxes to actions on
2101    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2102    users need not read the source code.
2103
2104   The High-Level Picture
2105
2106   Changes in the register we use to calculate the CFA: Currently we
2107   assume that if you copy the CFA register into another register, we
2108   should take the other one as the new CFA register; this seems to
2109   work pretty well.  If it's wrong for some target, it's simple
2110   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2111
2112   Changes in the register we use for saving registers to the stack:
2113   This is usually SP, but not always.  Again, we deduce that if you
2114   copy SP into another register (and SP is not the CFA register),
2115   then the new register is the one we will be using for register
2116   saves.  This also seems to work.
2117
2118   Register saves: There's not much guesswork about this one; if
2119   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2120   register save, and the register used to calculate the destination
2121   had better be the one we think we're using for this purpose.
2122   It's also assumed that a copy from a call-saved register to another
2123   register is saving that register if RTX_FRAME_RELATED_P is set on
2124   that instruction.  If the copy is from a call-saved register to
2125   the *same* register, that means that the register is now the same
2126   value as in the caller.
2127
2128   Except: If the register being saved is the CFA register, and the
2129   offset is nonzero, we are saving the CFA, so we assume we have to
2130   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2131   the intent is to save the value of SP from the previous frame.
2132
2133   In addition, if a register has previously been saved to a different
2134   register,
2135
2136   Invariants / Summaries of Rules
2137
2138   cfa          current rule for calculating the CFA.  It usually
2139                consists of a register and an offset.
2140   cfa_store    register used by prologue code to save things to the stack
2141                cfa_store.offset is the offset from the value of
2142                cfa_store.reg to the actual CFA
2143   cfa_temp     register holding an integral value.  cfa_temp.offset
2144                stores the value, which will be used to adjust the
2145                stack pointer.  cfa_temp is also used like cfa_store,
2146                to track stores to the stack via fp or a temp reg.
2147
2148   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2149                with cfa.reg as the first operand changes the cfa.reg and its
2150                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2151                cfa_temp.offset.
2152
2153   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2154                expression yielding a constant.  This sets cfa_temp.reg
2155                and cfa_temp.offset.
2156
2157   Rule 5:      Create a new register cfa_store used to save items to the
2158                stack.
2159
2160   Rules 10-14: Save a register to the stack.  Define offset as the
2161                difference of the original location and cfa_store's
2162                location (or cfa_temp's location if cfa_temp is used).
2163
2164   Rules 16-20: If AND operation happens on sp in prologue, we assume
2165                stack is realigned.  We will use a group of DW_OP_XXX
2166                expressions to represent the location of the stored
2167                register instead of CFA+offset.
2168
2169   The Rules
2170
2171   "{a,b}" indicates a choice of a xor b.
2172   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2173
2174   Rule 1:
2175   (set <reg1> <reg2>:cfa.reg)
2176   effects: cfa.reg = <reg1>
2177            cfa.offset unchanged
2178            cfa_temp.reg = <reg1>
2179            cfa_temp.offset = cfa.offset
2180
2181   Rule 2:
2182   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2183                               {<const_int>,<reg>:cfa_temp.reg}))
2184   effects: cfa.reg = sp if fp used
2185            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2186            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2187              if cfa_store.reg==sp
2188
2189   Rule 3:
2190   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2191   effects: cfa.reg = fp
2192            cfa_offset += +/- <const_int>
2193
2194   Rule 4:
2195   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2196   constraints: <reg1> != fp
2197                <reg1> != sp
2198   effects: cfa.reg = <reg1>
2199            cfa_temp.reg = <reg1>
2200            cfa_temp.offset = cfa.offset
2201
2202   Rule 5:
2203   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2204   constraints: <reg1> != fp
2205                <reg1> != sp
2206   effects: cfa_store.reg = <reg1>
2207            cfa_store.offset = cfa.offset - cfa_temp.offset
2208
2209   Rule 6:
2210   (set <reg> <const_int>)
2211   effects: cfa_temp.reg = <reg>
2212            cfa_temp.offset = <const_int>
2213
2214   Rule 7:
2215   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2216   effects: cfa_temp.reg = <reg1>
2217            cfa_temp.offset |= <const_int>
2218
2219   Rule 8:
2220   (set <reg> (high <exp>))
2221   effects: none
2222
2223   Rule 9:
2224   (set <reg> (lo_sum <exp> <const_int>))
2225   effects: cfa_temp.reg = <reg>
2226            cfa_temp.offset = <const_int>
2227
2228   Rule 10:
2229   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2230   effects: cfa_store.offset -= <const_int>
2231            cfa.offset = cfa_store.offset if cfa.reg == sp
2232            cfa.reg = sp
2233            cfa.base_offset = -cfa_store.offset
2234
2235   Rule 11:
2236   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2237   effects: cfa_store.offset += -/+ mode_size(mem)
2238            cfa.offset = cfa_store.offset if cfa.reg == sp
2239            cfa.reg = sp
2240            cfa.base_offset = -cfa_store.offset
2241
2242   Rule 12:
2243   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2244
2245        <reg2>)
2246   effects: cfa.reg = <reg1>
2247            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2248
2249   Rule 13:
2250   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2251   effects: cfa.reg = <reg1>
2252            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2253
2254   Rule 14:
2255   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2256   effects: cfa.reg = <reg1>
2257            cfa.base_offset = -cfa_temp.offset
2258            cfa_temp.offset -= mode_size(mem)
2259
2260   Rule 15:
2261   (set <reg> {unspec, unspec_volatile})
2262   effects: target-dependent
2263
2264   Rule 16:
2265   (set sp (and: sp <const_int>))
2266   constraints: cfa_store.reg == sp
2267   effects: current_fde.stack_realign = 1
2268            cfa_store.offset = 0
2269            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2270
2271   Rule 17:
2272   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2273   effects: cfa_store.offset += -/+ mode_size(mem)
2274
2275   Rule 18:
2276   (set (mem ({pre_inc, pre_dec} sp)) fp)
2277   constraints: fde->stack_realign == 1
2278   effects: cfa_store.offset = 0
2279            cfa.reg != HARD_FRAME_POINTER_REGNUM
2280
2281   Rule 19:
2282   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2283   constraints: fde->stack_realign == 1
2284                && cfa.offset == 0
2285                && cfa.indirect == 0
2286                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2287   effects: Use DW_CFA_def_cfa_expression to define cfa
2288            cfa.reg == fde->drap_reg  */
2289
2290 static void
2291 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2292 {
2293   rtx src, dest, span;
2294   HOST_WIDE_INT offset;
2295   dw_fde_ref fde;
2296
2297   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2298      the PARALLEL independently. The first element is always processed if
2299      it is a SET. This is for backward compatibility.   Other elements
2300      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2301      flag is set in them.  */
2302   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2303     {
2304       int par_index;
2305       int limit = XVECLEN (expr, 0);
2306       rtx elem;
2307
2308       /* PARALLELs have strict read-modify-write semantics, so we
2309          ought to evaluate every rvalue before changing any lvalue.
2310          It's cumbersome to do that in general, but there's an
2311          easy approximation that is enough for all current users:
2312          handle register saves before register assignments.  */
2313       if (GET_CODE (expr) == PARALLEL)
2314         for (par_index = 0; par_index < limit; par_index++)
2315           {
2316             elem = XVECEXP (expr, 0, par_index);
2317             if (GET_CODE (elem) == SET
2318                 && MEM_P (SET_DEST (elem))
2319                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2320               dwarf2out_frame_debug_expr (elem, label);
2321           }
2322
2323       for (par_index = 0; par_index < limit; par_index++)
2324         {
2325           elem = XVECEXP (expr, 0, par_index);
2326           if (GET_CODE (elem) == SET
2327               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2328               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2329             dwarf2out_frame_debug_expr (elem, label);
2330           else if (GET_CODE (elem) == SET
2331                    && par_index != 0
2332                    && !RTX_FRAME_RELATED_P (elem))
2333             {
2334               /* Stack adjustment combining might combine some post-prologue
2335                  stack adjustment into a prologue stack adjustment.  */
2336               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2337
2338               if (offset != 0)
2339                 dwarf2out_stack_adjust (offset, label);
2340             }
2341         }
2342       return;
2343     }
2344
2345   gcc_assert (GET_CODE (expr) == SET);
2346
2347   src = SET_SRC (expr);
2348   dest = SET_DEST (expr);
2349
2350   if (REG_P (src))
2351     {
2352       rtx rsi = reg_saved_in (src);
2353       if (rsi)
2354         src = rsi;
2355     }
2356
2357   fde = current_fde ();
2358
2359   switch (GET_CODE (dest))
2360     {
2361     case REG:
2362       switch (GET_CODE (src))
2363         {
2364           /* Setting FP from SP.  */
2365         case REG:
2366           if (cfa.reg == (unsigned) REGNO (src))
2367             {
2368               /* Rule 1 */
2369               /* Update the CFA rule wrt SP or FP.  Make sure src is
2370                  relative to the current CFA register.
2371
2372                  We used to require that dest be either SP or FP, but the
2373                  ARM copies SP to a temporary register, and from there to
2374                  FP.  So we just rely on the backends to only set
2375                  RTX_FRAME_RELATED_P on appropriate insns.  */
2376               cfa.reg = REGNO (dest);
2377               cfa_temp.reg = cfa.reg;
2378               cfa_temp.offset = cfa.offset;
2379             }
2380           else
2381             {
2382               /* Saving a register in a register.  */
2383               gcc_assert (!fixed_regs [REGNO (dest)]
2384                           /* For the SPARC and its register window.  */
2385                           || (DWARF_FRAME_REGNUM (REGNO (src))
2386                               == DWARF_FRAME_RETURN_COLUMN));
2387
2388               /* After stack is aligned, we can only save SP in FP
2389                  if drap register is used.  In this case, we have
2390                  to restore stack pointer with the CFA value and we
2391                  don't generate this DWARF information.  */
2392               if (fde
2393                   && fde->stack_realign
2394                   && REGNO (src) == STACK_POINTER_REGNUM)
2395                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2396                             && fde->drap_reg != INVALID_REGNUM
2397                             && cfa.reg != REGNO (src));
2398               else
2399                 queue_reg_save (label, src, dest, 0);
2400             }
2401           break;
2402
2403         case PLUS:
2404         case MINUS:
2405         case LO_SUM:
2406           if (dest == stack_pointer_rtx)
2407             {
2408               /* Rule 2 */
2409               /* Adjusting SP.  */
2410               switch (GET_CODE (XEXP (src, 1)))
2411                 {
2412                 case CONST_INT:
2413                   offset = INTVAL (XEXP (src, 1));
2414                   break;
2415                 case REG:
2416                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2417                               == cfa_temp.reg);
2418                   offset = cfa_temp.offset;
2419                   break;
2420                 default:
2421                   gcc_unreachable ();
2422                 }
2423
2424               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2425                 {
2426                   /* Restoring SP from FP in the epilogue.  */
2427                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2428                   cfa.reg = STACK_POINTER_REGNUM;
2429                 }
2430               else if (GET_CODE (src) == LO_SUM)
2431                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2432                 ;
2433               else
2434                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2435
2436               if (GET_CODE (src) != MINUS)
2437                 offset = -offset;
2438               if (cfa.reg == STACK_POINTER_REGNUM)
2439                 cfa.offset += offset;
2440               if (cfa_store.reg == STACK_POINTER_REGNUM)
2441                 cfa_store.offset += offset;
2442             }
2443           else if (dest == hard_frame_pointer_rtx)
2444             {
2445               /* Rule 3 */
2446               /* Either setting the FP from an offset of the SP,
2447                  or adjusting the FP */
2448               gcc_assert (frame_pointer_needed);
2449
2450               gcc_assert (REG_P (XEXP (src, 0))
2451                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2452                           && CONST_INT_P (XEXP (src, 1)));
2453               offset = INTVAL (XEXP (src, 1));
2454               if (GET_CODE (src) != MINUS)
2455                 offset = -offset;
2456               cfa.offset += offset;
2457               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2458             }
2459           else
2460             {
2461               gcc_assert (GET_CODE (src) != MINUS);
2462
2463               /* Rule 4 */
2464               if (REG_P (XEXP (src, 0))
2465                   && REGNO (XEXP (src, 0)) == cfa.reg
2466                   && CONST_INT_P (XEXP (src, 1)))
2467                 {
2468                   /* Setting a temporary CFA register that will be copied
2469                      into the FP later on.  */
2470                   offset = - INTVAL (XEXP (src, 1));
2471                   cfa.offset += offset;
2472                   cfa.reg = REGNO (dest);
2473                   /* Or used to save regs to the stack.  */
2474                   cfa_temp.reg = cfa.reg;
2475                   cfa_temp.offset = cfa.offset;
2476                 }
2477
2478               /* Rule 5 */
2479               else if (REG_P (XEXP (src, 0))
2480                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2481                        && XEXP (src, 1) == stack_pointer_rtx)
2482                 {
2483                   /* Setting a scratch register that we will use instead
2484                      of SP for saving registers to the stack.  */
2485                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2486                   cfa_store.reg = REGNO (dest);
2487                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2488                 }
2489
2490               /* Rule 9 */
2491               else if (GET_CODE (src) == LO_SUM
2492                        && CONST_INT_P (XEXP (src, 1)))
2493                 {
2494                   cfa_temp.reg = REGNO (dest);
2495                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2496                 }
2497               else
2498                 gcc_unreachable ();
2499             }
2500           break;
2501
2502           /* Rule 6 */
2503         case CONST_INT:
2504           cfa_temp.reg = REGNO (dest);
2505           cfa_temp.offset = INTVAL (src);
2506           break;
2507
2508           /* Rule 7 */
2509         case IOR:
2510           gcc_assert (REG_P (XEXP (src, 0))
2511                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2512                       && CONST_INT_P (XEXP (src, 1)));
2513
2514           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2515             cfa_temp.reg = REGNO (dest);
2516           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2517           break;
2518
2519           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2520              which will fill in all of the bits.  */
2521           /* Rule 8 */
2522         case HIGH:
2523           break;
2524
2525           /* Rule 15 */
2526         case UNSPEC:
2527         case UNSPEC_VOLATILE:
2528           gcc_assert (targetm.dwarf_handle_frame_unspec);
2529           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2530           return;
2531
2532           /* Rule 16 */
2533         case AND:
2534           /* If this AND operation happens on stack pointer in prologue,
2535              we assume the stack is realigned and we extract the
2536              alignment.  */
2537           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2538             {
2539               /* We interpret reg_save differently with stack_realign set.
2540                  Thus we must flush whatever we have queued first.  */
2541               dwarf2out_flush_queued_reg_saves ();
2542
2543               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2544               fde->stack_realign = 1;
2545               fde->stack_realignment = INTVAL (XEXP (src, 1));
2546               cfa_store.offset = 0;
2547
2548               if (cfa.reg != STACK_POINTER_REGNUM
2549                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2550                 fde->drap_reg = cfa.reg;
2551             }
2552           return;
2553
2554         default:
2555           gcc_unreachable ();
2556         }
2557
2558       def_cfa_1 (label, &cfa);
2559       break;
2560
2561     case MEM:
2562
2563       /* Saving a register to the stack.  Make sure dest is relative to the
2564          CFA register.  */
2565       switch (GET_CODE (XEXP (dest, 0)))
2566         {
2567           /* Rule 10 */
2568           /* With a push.  */
2569         case PRE_MODIFY:
2570           /* We can't handle variable size modifications.  */
2571           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2572                       == CONST_INT);
2573           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2574
2575           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2576                       && cfa_store.reg == STACK_POINTER_REGNUM);
2577
2578           cfa_store.offset += offset;
2579           if (cfa.reg == STACK_POINTER_REGNUM)
2580             cfa.offset = cfa_store.offset;
2581
2582           offset = -cfa_store.offset;
2583           break;
2584
2585           /* Rule 11 */
2586         case PRE_INC:
2587         case PRE_DEC:
2588         case POST_DEC:
2589           offset = GET_MODE_SIZE (GET_MODE (dest));
2590           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2591             offset = -offset;
2592
2593           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2594                        == STACK_POINTER_REGNUM)
2595                       && cfa_store.reg == STACK_POINTER_REGNUM);
2596
2597           cfa_store.offset += offset;
2598
2599           /* Rule 18: If stack is aligned, we will use FP as a
2600              reference to represent the address of the stored
2601              regiser.  */
2602           if (fde
2603               && fde->stack_realign
2604               && src == hard_frame_pointer_rtx)
2605             {
2606               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2607               cfa_store.offset = 0;
2608             }
2609
2610           if (cfa.reg == STACK_POINTER_REGNUM)
2611             cfa.offset = cfa_store.offset;
2612
2613           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2614             offset += -cfa_store.offset;
2615           else
2616             offset = -cfa_store.offset;
2617           break;
2618
2619           /* Rule 12 */
2620           /* With an offset.  */
2621         case PLUS:
2622         case MINUS:
2623         case LO_SUM:
2624           {
2625             int regno;
2626
2627             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2628                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2629             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2630             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2631               offset = -offset;
2632
2633             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2634
2635             if (cfa.reg == (unsigned) regno)
2636               offset -= cfa.offset;
2637             else if (cfa_store.reg == (unsigned) regno)
2638               offset -= cfa_store.offset;
2639             else
2640               {
2641                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2642                 offset -= cfa_temp.offset;
2643               }
2644           }
2645           break;
2646
2647           /* Rule 13 */
2648           /* Without an offset.  */
2649         case REG:
2650           {
2651             int regno = REGNO (XEXP (dest, 0));
2652
2653             if (cfa.reg == (unsigned) regno)
2654               offset = -cfa.offset;
2655             else if (cfa_store.reg == (unsigned) regno)
2656               offset = -cfa_store.offset;
2657             else
2658               {
2659                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2660                 offset = -cfa_temp.offset;
2661               }
2662           }
2663           break;
2664
2665           /* Rule 14 */
2666         case POST_INC:
2667           gcc_assert (cfa_temp.reg
2668                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2669           offset = -cfa_temp.offset;
2670           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2671           break;
2672
2673         default:
2674           gcc_unreachable ();
2675         }
2676
2677         /* Rule 17 */
2678         /* If the source operand of this MEM operation is not a
2679            register, basically the source is return address.  Here
2680            we only care how much stack grew and we don't save it.  */
2681       if (!REG_P (src))
2682         break;
2683
2684       if (REGNO (src) != STACK_POINTER_REGNUM
2685           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2686           && (unsigned) REGNO (src) == cfa.reg)
2687         {
2688           /* We're storing the current CFA reg into the stack.  */
2689
2690           if (cfa.offset == 0)
2691             {
2692               /* Rule 19 */
2693               /* If stack is aligned, putting CFA reg into stack means
2694                  we can no longer use reg + offset to represent CFA.
2695                  Here we use DW_CFA_def_cfa_expression instead.  The
2696                  result of this expression equals to the original CFA
2697                  value.  */
2698               if (fde
2699                   && fde->stack_realign
2700                   && cfa.indirect == 0
2701                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2702                 {
2703                   dw_cfa_location cfa_exp;
2704
2705                   gcc_assert (fde->drap_reg == cfa.reg);
2706
2707                   cfa_exp.indirect = 1;
2708                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2709                   cfa_exp.base_offset = offset;
2710                   cfa_exp.offset = 0;
2711
2712                   fde->drap_reg_saved = 1;
2713
2714                   def_cfa_1 (label, &cfa_exp);
2715                   break;
2716                 }
2717
2718               /* If the source register is exactly the CFA, assume
2719                  we're saving SP like any other register; this happens
2720                  on the ARM.  */
2721               def_cfa_1 (label, &cfa);
2722               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2723               break;
2724             }
2725           else
2726             {
2727               /* Otherwise, we'll need to look in the stack to
2728                  calculate the CFA.  */
2729               rtx x = XEXP (dest, 0);
2730
2731               if (!REG_P (x))
2732                 x = XEXP (x, 0);
2733               gcc_assert (REG_P (x));
2734
2735               cfa.reg = REGNO (x);
2736               cfa.base_offset = offset;
2737               cfa.indirect = 1;
2738               def_cfa_1 (label, &cfa);
2739               break;
2740             }
2741         }
2742
2743       def_cfa_1 (label, &cfa);
2744       {
2745         span = targetm.dwarf_register_span (src);
2746
2747         if (!span)
2748           queue_reg_save (label, src, NULL_RTX, offset);
2749         else
2750           {
2751             /* We have a PARALLEL describing where the contents of SRC
2752                live.  Queue register saves for each piece of the
2753                PARALLEL.  */
2754             int par_index;
2755             int limit;
2756             HOST_WIDE_INT span_offset = offset;
2757
2758             gcc_assert (GET_CODE (span) == PARALLEL);
2759
2760             limit = XVECLEN (span, 0);
2761             for (par_index = 0; par_index < limit; par_index++)
2762               {
2763                 rtx elem = XVECEXP (span, 0, par_index);
2764
2765                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2766                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2767               }
2768           }
2769       }
2770       break;
2771
2772     default:
2773       gcc_unreachable ();
2774     }
2775 }
2776
2777 /* Record call frame debugging information for INSN, which either
2778    sets SP or FP (adjusting how we calculate the frame address) or saves a
2779    register to the stack.  If INSN is NULL_RTX, initialize our state.
2780
2781    If AFTER_P is false, we're being called before the insn is emitted,
2782    otherwise after.  Call instructions get invoked twice.  */
2783
2784 void
2785 dwarf2out_frame_debug (rtx insn, bool after_p)
2786 {
2787   const char *label;
2788   rtx note, n;
2789   bool handled_one = false;
2790
2791   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2792     dwarf2out_flush_queued_reg_saves ();
2793
2794   if (!RTX_FRAME_RELATED_P (insn))
2795     {
2796       /* ??? This should be done unconditionally since stack adjustments
2797          matter if the stack pointer is not the CFA register anymore but
2798          is still used to save registers.  */
2799       if (!ACCUMULATE_OUTGOING_ARGS)
2800         dwarf2out_notice_stack_adjust (insn, after_p);
2801       return;
2802     }
2803
2804   label = dwarf2out_cfi_label (false);
2805   any_cfis_emitted = false;
2806
2807   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2808     switch (REG_NOTE_KIND (note))
2809       {
2810       case REG_FRAME_RELATED_EXPR:
2811         insn = XEXP (note, 0);
2812         goto found;
2813
2814       case REG_CFA_DEF_CFA:
2815         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2816         handled_one = true;
2817         break;
2818
2819       case REG_CFA_ADJUST_CFA:
2820         n = XEXP (note, 0);
2821         if (n == NULL)
2822           {
2823             n = PATTERN (insn);
2824             if (GET_CODE (n) == PARALLEL)
2825               n = XVECEXP (n, 0, 0);
2826           }
2827         dwarf2out_frame_debug_adjust_cfa (n, label);
2828         handled_one = true;
2829         break;
2830
2831       case REG_CFA_OFFSET:
2832         n = XEXP (note, 0);
2833         if (n == NULL)
2834           n = single_set (insn);
2835         dwarf2out_frame_debug_cfa_offset (n, label);
2836         handled_one = true;
2837         break;
2838
2839       case REG_CFA_REGISTER:
2840         n = XEXP (note, 0);
2841         if (n == NULL)
2842           {
2843             n = PATTERN (insn);
2844             if (GET_CODE (n) == PARALLEL)
2845               n = XVECEXP (n, 0, 0);
2846           }
2847         dwarf2out_frame_debug_cfa_register (n, label);
2848         handled_one = true;
2849         break;
2850
2851       case REG_CFA_EXPRESSION:
2852         n = XEXP (note, 0);
2853         if (n == NULL)
2854           n = single_set (insn);
2855         dwarf2out_frame_debug_cfa_expression (n, label);
2856         handled_one = true;
2857         break;
2858
2859       case REG_CFA_RESTORE:
2860         n = XEXP (note, 0);
2861         if (n == NULL)
2862           {
2863             n = PATTERN (insn);
2864             if (GET_CODE (n) == PARALLEL)
2865               n = XVECEXP (n, 0, 0);
2866             n = XEXP (n, 0);
2867           }
2868         dwarf2out_frame_debug_cfa_restore (n, label);
2869         handled_one = true;
2870         break;
2871
2872       case REG_CFA_SET_VDRAP:
2873         n = XEXP (note, 0);
2874         if (REG_P (n))
2875           {
2876             dw_fde_ref fde = current_fde ();
2877             if (fde)
2878               {
2879                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2880                 if (REG_P (n))
2881                   fde->vdrap_reg = REGNO (n);
2882               }
2883           }
2884         handled_one = true;
2885         break;
2886
2887       default:
2888         break;
2889       }
2890   if (handled_one)
2891     {
2892       if (any_cfis_emitted)
2893         dwarf2out_flush_queued_reg_saves ();
2894       return;
2895     }
2896
2897   insn = PATTERN (insn);
2898  found:
2899   dwarf2out_frame_debug_expr (insn, label);
2900
2901   /* Check again.  A parallel can save and update the same register.
2902      We could probably check just once, here, but this is safer than
2903      removing the check above.  */
2904   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2905     dwarf2out_flush_queued_reg_saves ();
2906 }
2907
2908 /* Called once at the start of final to initialize some data for the
2909    current function.  */
2910 void
2911 dwarf2out_frame_debug_init (void)
2912 {
2913   size_t i;
2914
2915   /* Flush any queued register saves.  */
2916   dwarf2out_flush_queued_reg_saves ();
2917
2918   /* Set up state for generating call frame debug info.  */
2919   lookup_cfa (&cfa);
2920   gcc_assert (cfa.reg
2921               == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2922
2923   cfa.reg = STACK_POINTER_REGNUM;
2924   cfa_store = cfa;
2925   cfa_temp.reg = -1;
2926   cfa_temp.offset = 0;
2927
2928   for (i = 0; i < num_regs_saved_in_regs; i++)
2929     {
2930       regs_saved_in_regs[i].orig_reg = NULL_RTX;
2931       regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2932     }
2933   num_regs_saved_in_regs = 0;
2934
2935   if (barrier_args_size)
2936     {
2937       XDELETEVEC (barrier_args_size);
2938       barrier_args_size = NULL;
2939     }
2940 }
2941
2942 /* Determine if we need to save and restore CFI information around this
2943    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2944    we do need to save/restore, then emit the save now, and insert a
2945    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2946
2947 void
2948 dwarf2out_cfi_begin_epilogue (rtx insn)
2949 {
2950   bool saw_frp = false;
2951   rtx i;
2952
2953   /* Scan forward to the return insn, noticing if there are possible
2954      frame related insns.  */
2955   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2956     {
2957       if (!INSN_P (i))
2958         continue;
2959
2960       /* Look for both regular and sibcalls to end the block.  */
2961       if (returnjump_p (i))
2962         break;
2963       if (CALL_P (i) && SIBLING_CALL_P (i))
2964         break;
2965
2966       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2967         {
2968           int idx;
2969           rtx seq = PATTERN (i);
2970
2971           if (returnjump_p (XVECEXP (seq, 0, 0)))
2972             break;
2973           if (CALL_P (XVECEXP (seq, 0, 0))
2974               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2975             break;
2976
2977           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2978             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2979               saw_frp = true;
2980         }
2981
2982       if (RTX_FRAME_RELATED_P (i))
2983         saw_frp = true;
2984     }
2985
2986   /* If the port doesn't emit epilogue unwind info, we don't need a
2987      save/restore pair.  */
2988   if (!saw_frp)
2989     return;
2990
2991   /* Otherwise, search forward to see if the return insn was the last
2992      basic block of the function.  If so, we don't need save/restore.  */
2993   gcc_assert (i != NULL);
2994   i = next_real_insn (i);
2995   if (i == NULL)
2996     return;
2997
2998   /* Insert the restore before that next real insn in the stream, and before
2999      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3000      properly nested.  This should be after any label or alignment.  This
3001      will be pushed into the CFI stream by the function below.  */
3002   while (1)
3003     {
3004       rtx p = PREV_INSN (i);
3005       if (!NOTE_P (p))
3006         break;
3007       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3008         break;
3009       i = p;
3010     }
3011   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3012
3013   emit_cfa_remember = true;
3014
3015   /* And emulate the state save.  */
3016   gcc_assert (!cfa_remember.in_use);
3017   cfa_remember = cfa;
3018   cfa_remember.in_use = 1;
3019 }
3020
3021 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3022    required.  */
3023
3024 void
3025 dwarf2out_frame_debug_restore_state (void)
3026 {
3027   dw_cfi_ref cfi = new_cfi ();
3028   const char *label = dwarf2out_cfi_label (false);
3029
3030   cfi->dw_cfi_opc = DW_CFA_restore_state;
3031   add_fde_cfi (label, cfi);
3032
3033   gcc_assert (cfa_remember.in_use);
3034   cfa = cfa_remember;
3035   cfa_remember.in_use = 0;
3036 }
3037
3038 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3039 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3040  (enum dwarf_call_frame_info cfi);
3041
3042 static enum dw_cfi_oprnd_type
3043 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3044 {
3045   switch (cfi)
3046     {
3047     case DW_CFA_nop:
3048     case DW_CFA_GNU_window_save:
3049     case DW_CFA_remember_state:
3050     case DW_CFA_restore_state:
3051       return dw_cfi_oprnd_unused;
3052
3053     case DW_CFA_set_loc:
3054     case DW_CFA_advance_loc1:
3055     case DW_CFA_advance_loc2:
3056     case DW_CFA_advance_loc4:
3057     case DW_CFA_MIPS_advance_loc8:
3058       return dw_cfi_oprnd_addr;
3059
3060     case DW_CFA_offset:
3061     case DW_CFA_offset_extended:
3062     case DW_CFA_def_cfa:
3063     case DW_CFA_offset_extended_sf:
3064     case DW_CFA_def_cfa_sf:
3065     case DW_CFA_restore:
3066     case DW_CFA_restore_extended:
3067     case DW_CFA_undefined:
3068     case DW_CFA_same_value:
3069     case DW_CFA_def_cfa_register:
3070     case DW_CFA_register:
3071     case DW_CFA_expression:
3072       return dw_cfi_oprnd_reg_num;
3073
3074     case DW_CFA_def_cfa_offset:
3075     case DW_CFA_GNU_args_size:
3076     case DW_CFA_def_cfa_offset_sf:
3077       return dw_cfi_oprnd_offset;
3078
3079     case DW_CFA_def_cfa_expression:
3080       return dw_cfi_oprnd_loc;
3081
3082     default:
3083       gcc_unreachable ();
3084     }
3085 }
3086
3087 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3088 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3089  (enum dwarf_call_frame_info cfi);
3090
3091 static enum dw_cfi_oprnd_type
3092 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3093 {
3094   switch (cfi)
3095     {
3096     case DW_CFA_def_cfa:
3097     case DW_CFA_def_cfa_sf:
3098     case DW_CFA_offset:
3099     case DW_CFA_offset_extended_sf:
3100     case DW_CFA_offset_extended:
3101       return dw_cfi_oprnd_offset;
3102
3103     case DW_CFA_register:
3104       return dw_cfi_oprnd_reg_num;
3105
3106     case DW_CFA_expression:
3107       return dw_cfi_oprnd_loc;
3108
3109     default:
3110       return dw_cfi_oprnd_unused;
3111     }
3112 }
3113
3114 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3115    switch to the data section instead, and write out a synthetic start label
3116    for collect2 the first time around.  */
3117
3118 static void
3119 switch_to_eh_frame_section (bool back)
3120 {
3121   tree label;
3122
3123 #ifdef EH_FRAME_SECTION_NAME
3124   if (eh_frame_section == 0)
3125     {
3126       int flags;
3127
3128       if (EH_TABLES_CAN_BE_READ_ONLY)
3129         {
3130           int fde_encoding;
3131           int per_encoding;
3132           int lsda_encoding;
3133
3134           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3135                                                        /*global=*/0);
3136           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3137                                                        /*global=*/1);
3138           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3139                                                         /*global=*/0);
3140           flags = ((! flag_pic
3141                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3142                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3143                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3144                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3145                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3146                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3147                    ? 0 : SECTION_WRITE);
3148         }
3149       else
3150         flags = SECTION_WRITE;
3151       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3152     }
3153 #endif /* EH_FRAME_SECTION_NAME */
3154
3155   if (eh_frame_section)
3156     switch_to_section (eh_frame_section);
3157   else
3158     {
3159       /* We have no special eh_frame section.  Put the information in
3160          the data section and emit special labels to guide collect2.  */
3161       switch_to_section (data_section);
3162
3163       if (!back)
3164         {
3165           label = get_file_function_name ("F");
3166           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3167           targetm.asm_out.globalize_label (asm_out_file,
3168                                            IDENTIFIER_POINTER (label));
3169           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3170         }
3171     }
3172 }
3173
3174 /* Switch [BACK] to the eh or debug frame table section, depending on
3175    FOR_EH.  */
3176
3177 static void
3178 switch_to_frame_table_section (int for_eh, bool back)
3179 {
3180   if (for_eh)
3181     switch_to_eh_frame_section (back);
3182   else
3183     {
3184       if (!debug_frame_section)
3185         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3186                                            SECTION_DEBUG, NULL);
3187       switch_to_section (debug_frame_section);
3188     }
3189 }
3190
3191 /* Output a Call Frame Information opcode and its operand(s).  */
3192
3193 static void
3194 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3195 {
3196   unsigned long r;
3197   HOST_WIDE_INT off;
3198
3199   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3200     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3201                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3202                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3203                          ((unsigned HOST_WIDE_INT)
3204                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3205   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3206     {
3207       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3208       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3209                            "DW_CFA_offset, column %#lx", r);
3210       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3211       dw2_asm_output_data_uleb128 (off, NULL);
3212     }
3213   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3214     {
3215       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3216       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3217                            "DW_CFA_restore, column %#lx", r);
3218     }
3219   else
3220     {
3221       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3222                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3223
3224       switch (cfi->dw_cfi_opc)
3225         {
3226         case DW_CFA_set_loc:
3227           if (for_eh)
3228             dw2_asm_output_encoded_addr_rtx (
3229                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3230                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3231                 false, NULL);
3232           else
3233             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3234                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3235           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3236           break;
3237
3238         case DW_CFA_advance_loc1:
3239           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3240                                 fde->dw_fde_current_label, NULL);
3241           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3242           break;
3243
3244         case DW_CFA_advance_loc2:
3245           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3246                                 fde->dw_fde_current_label, NULL);
3247           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3248           break;
3249
3250         case DW_CFA_advance_loc4:
3251           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3252                                 fde->dw_fde_current_label, NULL);
3253           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3254           break;
3255
3256         case DW_CFA_MIPS_advance_loc8:
3257           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3258                                 fde->dw_fde_current_label, NULL);
3259           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3260           break;
3261
3262         case DW_CFA_offset_extended:
3263           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3264           dw2_asm_output_data_uleb128 (r, NULL);
3265           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3266           dw2_asm_output_data_uleb128 (off, NULL);
3267           break;
3268
3269         case DW_CFA_def_cfa:
3270           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3271           dw2_asm_output_data_uleb128 (r, NULL);
3272           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3273           break;
3274
3275         case DW_CFA_offset_extended_sf:
3276           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3277           dw2_asm_output_data_uleb128 (r, NULL);
3278           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3279           dw2_asm_output_data_sleb128 (off, NULL);
3280           break;
3281
3282         case DW_CFA_def_cfa_sf:
3283           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3284           dw2_asm_output_data_uleb128 (r, NULL);
3285           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3286           dw2_asm_output_data_sleb128 (off, NULL);
3287           break;
3288
3289         case DW_CFA_restore_extended:
3290         case DW_CFA_undefined:
3291         case DW_CFA_same_value:
3292         case DW_CFA_def_cfa_register:
3293           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3294           dw2_asm_output_data_uleb128 (r, NULL);
3295           break;
3296
3297         case DW_CFA_register:
3298           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3299           dw2_asm_output_data_uleb128 (r, NULL);
3300           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3301           dw2_asm_output_data_uleb128 (r, NULL);
3302           break;
3303
3304         case DW_CFA_def_cfa_offset:
3305         case DW_CFA_GNU_args_size:
3306           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3307           break;
3308
3309         case DW_CFA_def_cfa_offset_sf:
3310           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3311           dw2_asm_output_data_sleb128 (off, NULL);
3312           break;
3313
3314         case DW_CFA_GNU_window_save:
3315           break;
3316
3317         case DW_CFA_def_cfa_expression:
3318         case DW_CFA_expression:
3319           output_cfa_loc (cfi, for_eh);
3320           break;
3321
3322         case DW_CFA_GNU_negative_offset_extended:
3323           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3324           gcc_unreachable ();
3325
3326         default:
3327           break;
3328         }
3329     }
3330 }
3331
3332 /* Similar, but do it via assembler directives instead.  */
3333
3334 static void
3335 output_cfi_directive (dw_cfi_ref cfi)
3336 {
3337   unsigned long r, r2;
3338
3339   switch (cfi->dw_cfi_opc)
3340     {
3341     case DW_CFA_advance_loc:
3342     case DW_CFA_advance_loc1:
3343     case DW_CFA_advance_loc2:
3344     case DW_CFA_advance_loc4:
3345     case DW_CFA_MIPS_advance_loc8:
3346     case DW_CFA_set_loc:
3347       /* Should only be created by add_fde_cfi in a code path not
3348          followed when emitting via directives.  The assembler is
3349          going to take care of this for us.  */
3350       gcc_unreachable ();
3351
3352     case DW_CFA_offset:
3353     case DW_CFA_offset_extended:
3354     case DW_CFA_offset_extended_sf:
3355       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3356       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3357                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3358       break;
3359
3360     case DW_CFA_restore:
3361     case DW_CFA_restore_extended:
3362       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3363       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3364       break;
3365
3366     case DW_CFA_undefined:
3367       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3368       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3369       break;
3370
3371     case DW_CFA_same_value:
3372       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3373       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3374       break;
3375
3376     case DW_CFA_def_cfa:
3377     case DW_CFA_def_cfa_sf:
3378       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3379       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3380                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3381       break;
3382
3383     case DW_CFA_def_cfa_register:
3384       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3385       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3386       break;
3387
3388     case DW_CFA_register:
3389       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3390       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3391       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3392       break;
3393
3394     case DW_CFA_def_cfa_offset:
3395     case DW_CFA_def_cfa_offset_sf:
3396       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3397                HOST_WIDE_INT_PRINT_DEC"\n",
3398                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3399       break;
3400
3401     case DW_CFA_remember_state:
3402       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3403       break;
3404     case DW_CFA_restore_state:
3405       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3406       break;
3407
3408     case DW_CFA_GNU_args_size:
3409       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3410       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3411       if (flag_debug_asm)
3412         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3413                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3414       fputc ('\n', asm_out_file);
3415       break;
3416
3417     case DW_CFA_GNU_window_save:
3418       fprintf (asm_out_file, "\t.cfi_window_save\n");
3419       break;
3420
3421     case DW_CFA_def_cfa_expression:
3422     case DW_CFA_expression:
3423       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3424       output_cfa_loc_raw (cfi);
3425       fputc ('\n', asm_out_file);
3426       break;
3427
3428     default:
3429       gcc_unreachable ();
3430     }
3431 }
3432
3433 DEF_VEC_P (dw_cfi_ref);
3434 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3435
3436 /* Output CFIs to bring current FDE to the same state as after executing
3437    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3438    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3439    other arguments to pass to output_cfi.  */
3440
3441 static void
3442 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3443 {
3444   struct dw_cfi_struct cfi_buf;
3445   dw_cfi_ref cfi2;
3446   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3447   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3448   unsigned int len, idx;
3449
3450   for (;; cfi = cfi->dw_cfi_next)
3451     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3452       {
3453       case DW_CFA_advance_loc:
3454       case DW_CFA_advance_loc1:
3455       case DW_CFA_advance_loc2:
3456       case DW_CFA_advance_loc4:
3457       case DW_CFA_MIPS_advance_loc8:
3458       case DW_CFA_set_loc:
3459         /* All advances should be ignored.  */
3460         break;
3461       case DW_CFA_remember_state:
3462         {
3463           dw_cfi_ref args_size = cfi_args_size;
3464
3465           /* Skip everything between .cfi_remember_state and
3466              .cfi_restore_state.  */
3467           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3468             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3469               break;
3470             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3471               args_size = cfi2;
3472             else
3473               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3474
3475           if (cfi2 == NULL)
3476             goto flush_all;
3477           else
3478             {
3479               cfi = cfi2;
3480               cfi_args_size = args_size;
3481             }
3482           break;
3483         }
3484       case DW_CFA_GNU_args_size:
3485         cfi_args_size = cfi;
3486         break;
3487       case DW_CFA_GNU_window_save:
3488         goto flush_all;
3489       case DW_CFA_offset:
3490       case DW_CFA_offset_extended:
3491       case DW_CFA_offset_extended_sf:
3492       case DW_CFA_restore:
3493       case DW_CFA_restore_extended:
3494       case DW_CFA_undefined:
3495       case DW_CFA_same_value:
3496       case DW_CFA_register:
3497       case DW_CFA_val_offset:
3498       case DW_CFA_val_offset_sf:
3499       case DW_CFA_expression:
3500       case DW_CFA_val_expression:
3501       case DW_CFA_GNU_negative_offset_extended:
3502         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3503           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3504                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3505         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3506         break;
3507       case DW_CFA_def_cfa:
3508       case DW_CFA_def_cfa_sf:
3509       case DW_CFA_def_cfa_expression:
3510         cfi_cfa = cfi;
3511         cfi_cfa_offset = cfi;
3512         break;
3513       case DW_CFA_def_cfa_register:
3514         cfi_cfa = cfi;
3515         break;
3516       case DW_CFA_def_cfa_offset:
3517       case DW_CFA_def_cfa_offset_sf:
3518         cfi_cfa_offset = cfi;
3519         break;
3520       case DW_CFA_nop:
3521         gcc_assert (cfi == NULL);
3522       flush_all:
3523         len = VEC_length (dw_cfi_ref, regs);
3524         for (idx = 0; idx < len; idx++)
3525           {
3526             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3527             if (cfi2 != NULL
3528                 && cfi2->dw_cfi_opc != DW_CFA_restore
3529                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3530               {
3531                 if (do_cfi_asm)
3532                   output_cfi_directive (cfi2);
3533                 else
3534                   output_cfi (cfi2, fde, for_eh);
3535               }
3536           }
3537         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3538           {
3539             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3540             cfi_buf = *cfi_cfa;
3541             switch (cfi_cfa_offset->dw_cfi_opc)
3542               {
3543               case DW_CFA_def_cfa_offset:
3544                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3545                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3546                 break;
3547               case DW_CFA_def_cfa_offset_sf:
3548                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3549                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3550                 break;
3551               case DW_CFA_def_cfa:
3552               case DW_CFA_def_cfa_sf:
3553                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3554                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3555                 break;
3556               default:
3557                 gcc_unreachable ();
3558               }
3559             cfi_cfa = &cfi_buf;
3560           }
3561         else if (cfi_cfa_offset)
3562           cfi_cfa = cfi_cfa_offset;
3563         if (cfi_cfa)
3564           {
3565             if (do_cfi_asm)
3566               output_cfi_directive (cfi_cfa);
3567             else
3568               output_cfi (cfi_cfa, fde, for_eh);
3569           }
3570         cfi_cfa = NULL;
3571         cfi_cfa_offset = NULL;
3572         if (cfi_args_size
3573             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3574           {
3575             if (do_cfi_asm)
3576               output_cfi_directive (cfi_args_size);
3577             else
3578               output_cfi (cfi_args_size, fde, for_eh);
3579           }
3580         cfi_args_size = NULL;
3581         if (cfi == NULL)
3582           {
3583             VEC_free (dw_cfi_ref, heap, regs);
3584             return;
3585           }
3586         else if (do_cfi_asm)
3587           output_cfi_directive (cfi);
3588         else
3589           output_cfi (cfi, fde, for_eh);
3590         break;
3591       default:
3592         gcc_unreachable ();
3593     }
3594 }
3595
3596 /* Output one FDE.  */
3597
3598 static void
3599 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3600             char *section_start_label, int fde_encoding, char *augmentation,
3601             bool any_lsda_needed, int lsda_encoding)
3602 {
3603   const char *begin, *end;
3604   static unsigned int j;
3605   char l1[20], l2[20];
3606   dw_cfi_ref cfi;
3607
3608   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3609                                      /* empty */ 0);
3610   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3611                                   for_eh + j);
3612   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3613   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3614   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3615     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3616                          " indicating 64-bit DWARF extension");
3617   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3618                         "FDE Length");
3619   ASM_OUTPUT_LABEL (asm_out_file, l1);
3620
3621   if (for_eh)
3622     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3623   else
3624     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3625                            debug_frame_section, "FDE CIE offset");
3626
3627   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3628   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3629
3630   if (for_eh)
3631     {
3632       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3633       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3634       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3635                                        "FDE initial location");
3636       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3637                             end, begin, "FDE address range");
3638     }
3639   else
3640     {
3641       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3642       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3643     }
3644
3645   if (augmentation[0])
3646     {
3647       if (any_lsda_needed)
3648         {
3649           int size = size_of_encoded_value (lsda_encoding);
3650
3651           if (lsda_encoding == DW_EH_PE_aligned)
3652             {
3653               int offset = (  4         /* Length */
3654                             + 4         /* CIE offset */
3655                             + 2 * size_of_encoded_value (fde_encoding)
3656                             + 1         /* Augmentation size */ );
3657               int pad = -offset & (PTR_SIZE - 1);
3658
3659               size += pad;
3660               gcc_assert (size_of_uleb128 (size) == 1);
3661             }
3662
3663           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3664
3665           if (fde->uses_eh_lsda)
3666             {
3667               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3668                                            fde->funcdef_number);
3669               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3670                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3671                                                false,
3672                                                "Language Specific Data Area");
3673             }
3674           else
3675             {
3676               if (lsda_encoding == DW_EH_PE_aligned)
3677                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3678               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3679                                    "Language Specific Data Area (none)");
3680             }
3681         }
3682       else
3683         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3684     }
3685
3686   /* Loop through the Call Frame Instructions associated with
3687      this FDE.  */
3688   fde->dw_fde_current_label = begin;
3689   if (fde->dw_fde_second_begin == NULL)
3690     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3691       output_cfi (cfi, fde, for_eh);
3692   else if (!second)
3693     {
3694       if (fde->dw_fde_switch_cfi)
3695         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3696           {
3697             output_cfi (cfi, fde, for_eh);
3698             if (cfi == fde->dw_fde_switch_cfi)
3699               break;
3700           }
3701     }
3702   else
3703     {
3704       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3705
3706       if (fde->dw_fde_switch_cfi)
3707         {
3708           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3709           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3710           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3711           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3712         }
3713       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3714         output_cfi (cfi, fde, for_eh);
3715     }
3716
3717   /* If we are to emit a ref/link from function bodies to their frame tables,
3718      do it now.  This is typically performed to make sure that tables
3719      associated with functions are dragged with them and not discarded in
3720      garbage collecting links. We need to do this on a per function basis to
3721      cope with -ffunction-sections.  */
3722
3723 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3724   /* Switch to the function section, emit the ref to the tables, and
3725      switch *back* into the table section.  */
3726   switch_to_section (function_section (fde->decl));
3727   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3728   switch_to_frame_table_section (for_eh, true);
3729 #endif
3730
3731   /* Pad the FDE out to an address sized boundary.  */
3732   ASM_OUTPUT_ALIGN (asm_out_file,
3733                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3734   ASM_OUTPUT_LABEL (asm_out_file, l2);
3735
3736   j += 2;
3737 }
3738
3739 /* Return true if frame description entry FDE is needed for EH.  */
3740
3741 static bool
3742 fde_needed_for_eh_p (dw_fde_ref fde)
3743 {
3744   if (flag_asynchronous_unwind_tables)
3745     return true;
3746
3747   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3748     return true;
3749
3750   if (fde->uses_eh_lsda)
3751     return true;
3752
3753   /* If exceptions are enabled, we have collected nothrow info.  */
3754   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3755     return false;
3756
3757   return true;
3758 }
3759
3760 /* Output the call frame information used to record information
3761    that relates to calculating the frame pointer, and records the
3762    location of saved registers.  */
3763
3764 static void
3765 output_call_frame_info (int for_eh)
3766 {
3767   unsigned int i;
3768   dw_fde_ref fde;
3769   dw_cfi_ref cfi;
3770   char l1[20], l2[20], section_start_label[20];
3771   bool any_lsda_needed = false;
3772   char augmentation[6];
3773   int augmentation_size;
3774   int fde_encoding = DW_EH_PE_absptr;
3775   int per_encoding = DW_EH_PE_absptr;
3776   int lsda_encoding = DW_EH_PE_absptr;
3777   int return_reg;
3778   rtx personality = NULL;
3779   int dw_cie_version;
3780
3781   /* Don't emit a CIE if there won't be any FDEs.  */
3782   if (fde_table_in_use == 0)
3783     return;
3784
3785   /* Nothing to do if the assembler's doing it all.  */
3786   if (dwarf2out_do_cfi_asm ())
3787     return;
3788
3789   /* If we don't have any functions we'll want to unwind out of, don't emit
3790      any EH unwind information.  If we make FDEs linkonce, we may have to
3791      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3792      want to avoid having an FDE kept around when the function it refers to
3793      is discarded.  Example where this matters: a primary function template
3794      in C++ requires EH information, an explicit specialization doesn't.  */
3795   if (for_eh)
3796     {
3797       bool any_eh_needed = false;
3798
3799       for (i = 0; i < fde_table_in_use; i++)
3800         if (fde_table[i].uses_eh_lsda)
3801           any_eh_needed = any_lsda_needed = true;
3802         else if (fde_needed_for_eh_p (&fde_table[i]))
3803           any_eh_needed = true;
3804         else if (TARGET_USES_WEAK_UNWIND_INFO)
3805           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3806                                              1, 1);
3807
3808       if (!any_eh_needed)
3809         return;
3810     }
3811
3812   /* We're going to be generating comments, so turn on app.  */
3813   if (flag_debug_asm)
3814     app_enable ();
3815
3816   /* Switch to the proper frame section, first time.  */
3817   switch_to_frame_table_section (for_eh, false);
3818
3819   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3820   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3821
3822   /* Output the CIE.  */
3823   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3824   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3825   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3826     dw2_asm_output_data (4, 0xffffffff,
3827       "Initial length escape value indicating 64-bit DWARF extension");
3828   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3829                         "Length of Common Information Entry");
3830   ASM_OUTPUT_LABEL (asm_out_file, l1);
3831
3832   /* Now that the CIE pointer is PC-relative for EH,
3833      use 0 to identify the CIE.  */
3834   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3835                        (for_eh ? 0 : DWARF_CIE_ID),
3836                        "CIE Identifier Tag");
3837
3838   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3839      use CIE version 1, unless that would produce incorrect results
3840      due to overflowing the return register column.  */
3841   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3842   dw_cie_version = 1;
3843   if (return_reg >= 256 || dwarf_version > 2)
3844     dw_cie_version = 3;
3845   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3846
3847   augmentation[0] = 0;
3848   augmentation_size = 0;
3849
3850   personality = current_unit_personality;
3851   if (for_eh)
3852     {
3853       char *p;
3854
3855       /* Augmentation:
3856          z      Indicates that a uleb128 is present to size the
3857                 augmentation section.
3858          L      Indicates the encoding (and thus presence) of
3859                 an LSDA pointer in the FDE augmentation.
3860          R      Indicates a non-default pointer encoding for
3861                 FDE code pointers.
3862          P      Indicates the presence of an encoding + language
3863                 personality routine in the CIE augmentation.  */
3864
3865       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3866       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3867       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3868
3869       p = augmentation + 1;
3870       if (personality)
3871         {
3872           *p++ = 'P';
3873           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3874           assemble_external_libcall (personality);
3875         }
3876       if (any_lsda_needed)
3877         {
3878           *p++ = 'L';
3879           augmentation_size += 1;
3880         }
3881       if (fde_encoding != DW_EH_PE_absptr)
3882         {
3883           *p++ = 'R';
3884           augmentation_size += 1;
3885         }
3886       if (p > augmentation + 1)
3887         {
3888           augmentation[0] = 'z';
3889           *p = '\0';
3890         }
3891
3892       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3893       if (personality && per_encoding == DW_EH_PE_aligned)
3894         {
3895           int offset = (  4             /* Length */
3896                         + 4             /* CIE Id */
3897                         + 1             /* CIE version */
3898                         + strlen (augmentation) + 1     /* Augmentation */
3899                         + size_of_uleb128 (1)           /* Code alignment */
3900                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3901                         + 1             /* RA column */
3902                         + 1             /* Augmentation size */
3903                         + 1             /* Personality encoding */ );
3904           int pad = -offset & (PTR_SIZE - 1);
3905
3906           augmentation_size += pad;
3907
3908           /* Augmentations should be small, so there's scarce need to
3909              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3910           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3911         }
3912     }
3913
3914   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3915   if (dw_cie_version >= 4)
3916     {
3917       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3918       dw2_asm_output_data (1, 0, "CIE Segment Size");
3919     }
3920   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3921   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3922                                "CIE Data Alignment Factor");
3923
3924   if (dw_cie_version == 1)
3925     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3926   else
3927     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3928
3929   if (augmentation[0])
3930     {
3931       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3932       if (personality)
3933         {
3934           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3935                                eh_data_format_name (per_encoding));
3936           dw2_asm_output_encoded_addr_rtx (per_encoding,
3937                                            personality,
3938                                            true, NULL);
3939         }
3940
3941       if (any_lsda_needed)
3942         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3943                              eh_data_format_name (lsda_encoding));
3944
3945       if (fde_encoding != DW_EH_PE_absptr)
3946         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3947                              eh_data_format_name (fde_encoding));
3948     }
3949
3950   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3951     output_cfi (cfi, NULL, for_eh);
3952
3953   /* Pad the CIE out to an address sized boundary.  */
3954   ASM_OUTPUT_ALIGN (asm_out_file,
3955                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3956   ASM_OUTPUT_LABEL (asm_out_file, l2);
3957
3958   /* Loop through all of the FDE's.  */
3959   for (i = 0; i < fde_table_in_use; i++)
3960     {
3961       unsigned int k;
3962       fde = &fde_table[i];
3963
3964       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3965       if (for_eh && !fde_needed_for_eh_p (fde))
3966         continue;
3967
3968       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
3969         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3970                     augmentation, any_lsda_needed, lsda_encoding);
3971     }
3972
3973   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3974     dw2_asm_output_data (4, 0, "End of Table");
3975 #ifdef MIPS_DEBUGGING_INFO
3976   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3977      get a value of 0.  Putting .align 0 after the label fixes it.  */
3978   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3979 #endif
3980
3981   /* Turn off app to make assembly quicker.  */
3982   if (flag_debug_asm)
3983     app_disable ();
3984 }
3985
3986 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3987
3988 static void
3989 dwarf2out_do_cfi_startproc (bool second)
3990 {
3991   int enc;
3992   rtx ref;
3993   rtx personality = get_personality_function (current_function_decl);
3994
3995   fprintf (asm_out_file, "\t.cfi_startproc\n");
3996
3997   if (personality)
3998     {
3999       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4000       ref = personality;
4001
4002       /* ??? The GAS support isn't entirely consistent.  We have to
4003          handle indirect support ourselves, but PC-relative is done
4004          in the assembler.  Further, the assembler can't handle any
4005          of the weirder relocation types.  */
4006       if (enc & DW_EH_PE_indirect)
4007         ref = dw2_force_const_mem (ref, true);
4008
4009       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4010       output_addr_const (asm_out_file, ref);
4011       fputc ('\n', asm_out_file);
4012     }
4013
4014   if (crtl->uses_eh_lsda)
4015     {
4016       char lab[20];
4017
4018       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4019       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4020                                    current_function_funcdef_no);
4021       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4022       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4023
4024       if (enc & DW_EH_PE_indirect)
4025         ref = dw2_force_const_mem (ref, true);
4026
4027       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4028       output_addr_const (asm_out_file, ref);
4029       fputc ('\n', asm_out_file);
4030     }
4031 }
4032
4033 /* Output a marker (i.e. a label) for the beginning of a function, before
4034    the prologue.  */
4035
4036 void
4037 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4038                           const char *file ATTRIBUTE_UNUSED)
4039 {
4040   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4041   char * dup_label;
4042   dw_fde_ref fde;
4043   section *fnsec;
4044   bool do_frame;
4045
4046   current_function_func_begin_label = NULL;
4047
4048   do_frame = dwarf2out_do_frame ();
4049
4050   /* ??? current_function_func_begin_label is also used by except.c for
4051      call-site information.  We must emit this label if it might be used.  */
4052   if (!do_frame
4053       && (!flag_exceptions
4054           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4055     return;
4056
4057   fnsec = function_section (current_function_decl);
4058   switch_to_section (fnsec);
4059   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4060                                current_function_funcdef_no);
4061   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4062                           current_function_funcdef_no);
4063   dup_label = xstrdup (label);
4064   current_function_func_begin_label = dup_label;
4065
4066   /* We can elide the fde allocation if we're not emitting debug info.  */
4067   if (!do_frame)
4068     return;
4069
4070   /* Expand the fde table if necessary.  */
4071   if (fde_table_in_use == fde_table_allocated)
4072     {
4073       fde_table_allocated += FDE_TABLE_INCREMENT;
4074       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4075       memset (fde_table + fde_table_in_use, 0,
4076               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4077     }
4078
4079   /* Record the FDE associated with this function.  */
4080   current_funcdef_fde = fde_table_in_use;
4081
4082   /* Add the new FDE at the end of the fde_table.  */
4083   fde = &fde_table[fde_table_in_use++];
4084   fde->decl = current_function_decl;
4085   fde->dw_fde_begin = dup_label;
4086   fde->dw_fde_end = NULL;
4087   fde->dw_fde_current_label = dup_label;
4088   fde->dw_fde_second_begin = NULL;
4089   fde->dw_fde_second_end = NULL;
4090   fde->dw_fde_vms_end_prologue = NULL;
4091   fde->dw_fde_vms_begin_epilogue = NULL;
4092   fde->dw_fde_cfi = NULL;
4093   fde->dw_fde_switch_cfi = NULL;
4094   fde->funcdef_number = current_function_funcdef_no;
4095   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4096   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4097   fde->nothrow = crtl->nothrow;
4098   fde->drap_reg = INVALID_REGNUM;
4099   fde->vdrap_reg = INVALID_REGNUM;
4100   fde->in_std_section = (fnsec == text_section
4101                          || (cold_text_section && fnsec == cold_text_section));
4102   fde->second_in_std_section = 0;
4103
4104   args_size = old_args_size = 0;
4105
4106   /* We only want to output line number information for the genuine dwarf2
4107      prologue case, not the eh frame case.  */
4108 #ifdef DWARF2_DEBUGGING_INFO
4109   if (file)
4110     dwarf2out_source_line (line, file, 0, true);
4111 #endif
4112
4113   if (dwarf2out_do_cfi_asm ())
4114     dwarf2out_do_cfi_startproc (false);
4115   else
4116     {
4117       rtx personality = get_personality_function (current_function_decl);
4118       if (!current_unit_personality)
4119         current_unit_personality = personality;
4120
4121       /* We cannot keep a current personality per function as without CFI
4122          asm, at the point where we emit the CFI data, there is no current
4123          function anymore.  */
4124       if (personality && current_unit_personality != personality)
4125         sorry ("multiple EH personalities are supported only with assemblers "
4126                "supporting .cfi_personality directive");
4127     }
4128 }
4129
4130 /* Output a marker (i.e. a label) for the end of the generated code
4131    for a function prologue.  This gets called *after* the prologue code has
4132    been generated.  */
4133
4134 void
4135 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4136                         const char *file ATTRIBUTE_UNUSED)
4137 {
4138   dw_fde_ref fde;
4139   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4140
4141   /* Output a label to mark the endpoint of the code generated for this
4142      function.  */
4143   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4144                                current_function_funcdef_no);
4145   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4146                           current_function_funcdef_no);
4147   fde = &fde_table[fde_table_in_use - 1];
4148   fde->dw_fde_vms_end_prologue = xstrdup (label);
4149 }
4150
4151 /* Output a marker (i.e. a label) for the beginning of the generated code
4152    for a function epilogue.  This gets called *before* the prologue code has
4153    been generated.  */
4154
4155 void
4156 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4157                           const char *file ATTRIBUTE_UNUSED)
4158 {
4159   dw_fde_ref fde;
4160   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4161
4162   fde = &fde_table[fde_table_in_use - 1];
4163   if (fde->dw_fde_vms_begin_epilogue)
4164     return;
4165
4166   /* Output a label to mark the endpoint of the code generated for this
4167      function.  */
4168   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4169                                current_function_funcdef_no);
4170   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4171                           current_function_funcdef_no);
4172   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4173 }
4174
4175 /* Output a marker (i.e. a label) for the absolute end of the generated code
4176    for a function definition.  This gets called *after* the epilogue code has
4177    been generated.  */
4178
4179 void
4180 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4181                         const char *file ATTRIBUTE_UNUSED)
4182 {
4183   dw_fde_ref fde;
4184   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4185
4186   last_var_location_insn = NULL_RTX;
4187
4188   if (dwarf2out_do_cfi_asm ())
4189     fprintf (asm_out_file, "\t.cfi_endproc\n");
4190
4191   /* Output a label to mark the endpoint of the code generated for this
4192      function.  */
4193   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4194                                current_function_funcdef_no);
4195   ASM_OUTPUT_LABEL (asm_out_file, label);
4196   fde = current_fde ();
4197   gcc_assert (fde != NULL);
4198   if (fde->dw_fde_second_begin == NULL)
4199     fde->dw_fde_end = xstrdup (label);
4200 }
4201
4202 void
4203 dwarf2out_frame_init (void)
4204 {
4205   /* Allocate the initial hunk of the fde_table.  */
4206   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4207   fde_table_allocated = FDE_TABLE_INCREMENT;
4208   fde_table_in_use = 0;
4209
4210   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4211      sake of lookup_cfa.  */
4212
4213   /* On entry, the Canonical Frame Address is at SP.  */
4214   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4215
4216   if (targetm.debug_unwind_info () == UI_DWARF2
4217       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4218     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4219 }
4220
4221 void
4222 dwarf2out_frame_finish (void)
4223 {
4224   /* Output call frame information.  */
4225   if (targetm.debug_unwind_info () == UI_DWARF2)
4226     output_call_frame_info (0);
4227
4228   /* Output another copy for the unwinder.  */
4229   if ((flag_unwind_tables || flag_exceptions)
4230       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4231     output_call_frame_info (1);
4232 }
4233
4234 /* Note that the current function section is being used for code.  */
4235
4236 static void
4237 dwarf2out_note_section_used (void)
4238 {
4239   section *sec = current_function_section ();
4240   if (sec == text_section)
4241     text_section_used = true;
4242   else if (sec == cold_text_section)
4243     cold_text_section_used = true;
4244 }
4245
4246 static void var_location_switch_text_section (void);
4247 static void set_cur_line_info_table (section *);
4248
4249 void
4250 dwarf2out_switch_text_section (void)
4251 {
4252   section *sect;
4253   dw_fde_ref fde = current_fde ();
4254   dw_cfi_ref cfi;
4255
4256   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4257
4258   if (!in_cold_section_p)
4259     {
4260       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4261       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4262       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4263     }
4264   else
4265     {
4266       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4267       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4268       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4269     }
4270   have_multiple_function_sections = true;
4271
4272   /* Reset the current label on switching text sections, so that we
4273      don't attempt to advance_loc4 between labels in different sections.  */
4274   fde->dw_fde_current_label = NULL;
4275
4276   /* There is no need to mark used sections when not debugging.  */
4277   if (cold_text_section != NULL)
4278     dwarf2out_note_section_used ();
4279
4280   if (dwarf2out_do_cfi_asm ())
4281     fprintf (asm_out_file, "\t.cfi_endproc\n");
4282
4283   /* Now do the real section switch.  */
4284   sect = current_function_section ();
4285   switch_to_section (sect);
4286
4287   fde->second_in_std_section
4288     = (sect == text_section
4289        || (cold_text_section && sect == cold_text_section));
4290
4291   if (dwarf2out_do_cfi_asm ())
4292     {
4293       dwarf2out_do_cfi_startproc (true);
4294       /* As this is a different FDE, insert all current CFI instructions
4295          again.  */
4296       output_cfis (fde->dw_fde_cfi, true, fde, true);
4297     }
4298   cfi = fde->dw_fde_cfi;
4299   if (cfi)
4300     while (cfi->dw_cfi_next != NULL)
4301       cfi = cfi->dw_cfi_next;
4302   fde->dw_fde_switch_cfi = cfi;
4303   var_location_switch_text_section ();
4304
4305   set_cur_line_info_table (sect);
4306 }
4307 \f
4308 /* And now, the subset of the debugging information support code necessary
4309    for emitting location expressions.  */
4310
4311 /* Data about a single source file.  */
4312 struct GTY(()) dwarf_file_data {
4313   const char * filename;
4314   int emitted_number;
4315 };
4316
4317 typedef struct dw_val_struct *dw_val_ref;
4318 typedef struct die_struct *dw_die_ref;
4319 typedef const struct die_struct *const_dw_die_ref;
4320 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4321 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4322
4323 typedef struct GTY(()) deferred_locations_struct
4324 {
4325   tree variable;
4326   dw_die_ref die;
4327 } deferred_locations;
4328
4329 DEF_VEC_O(deferred_locations);
4330 DEF_VEC_ALLOC_O(deferred_locations,gc);
4331
4332 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4333
4334 DEF_VEC_P(dw_die_ref);
4335 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4336
4337 /* Each DIE may have a series of attribute/value pairs.  Values
4338    can take on several forms.  The forms that are used in this
4339    implementation are listed below.  */
4340
4341 enum dw_val_class
4342 {
4343   dw_val_class_addr,
4344   dw_val_class_offset,
4345   dw_val_class_loc,
4346   dw_val_class_loc_list,
4347   dw_val_class_range_list,
4348   dw_val_class_const,
4349   dw_val_class_unsigned_const,
4350   dw_val_class_const_double,
4351   dw_val_class_vec,
4352   dw_val_class_flag,
4353   dw_val_class_die_ref,
4354   dw_val_class_fde_ref,
4355   dw_val_class_lbl_id,
4356   dw_val_class_lineptr,
4357   dw_val_class_str,
4358   dw_val_class_macptr,
4359   dw_val_class_file,
4360   dw_val_class_data8,
4361   dw_val_class_decl_ref,
4362   dw_val_class_vms_delta
4363 };
4364
4365 /* Describe a floating point constant value, or a vector constant value.  */
4366
4367 typedef struct GTY(()) dw_vec_struct {
4368   unsigned char * GTY((length ("%h.length"))) array;
4369   unsigned length;
4370   unsigned elt_size;
4371 }
4372 dw_vec_const;
4373
4374 /* The dw_val_node describes an attribute's value, as it is
4375    represented internally.  */
4376
4377 typedef struct GTY(()) dw_val_struct {
4378   enum dw_val_class val_class;
4379   union dw_val_struct_union
4380     {
4381       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4382       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4383       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4384       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4385       HOST_WIDE_INT GTY ((default)) val_int;
4386       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4387       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4388       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4389       struct dw_val_die_union
4390         {
4391           dw_die_ref die;
4392           int external;
4393         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4394       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4395       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4396       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4397       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4398       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4399       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4400       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4401       struct dw_val_vms_delta_union
4402         {
4403           char * lbl1;
4404           char * lbl2;
4405         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4406     }
4407   GTY ((desc ("%1.val_class"))) v;
4408 }
4409 dw_val_node;
4410
4411 /* Locations in memory are described using a sequence of stack machine
4412    operations.  */
4413
4414 typedef struct GTY(()) dw_loc_descr_struct {
4415   dw_loc_descr_ref dw_loc_next;
4416   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4417   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4418      from DW_OP_addr with a dtp-relative symbol relocation.  */
4419   unsigned int dtprel : 1;
4420   int dw_loc_addr;
4421   dw_val_node dw_loc_oprnd1;
4422   dw_val_node dw_loc_oprnd2;
4423 }
4424 dw_loc_descr_node;
4425
4426 /* Location lists are ranges + location descriptions for that range,
4427    so you can track variables that are in different places over
4428    their entire life.  */
4429 typedef struct GTY(()) dw_loc_list_struct {
4430   dw_loc_list_ref dw_loc_next;
4431   const char *begin; /* Label for begin address of range */
4432   const char *end;  /* Label for end address of range */
4433   char *ll_symbol; /* Label for beginning of location list.
4434                       Only on head of list */
4435   const char *section; /* Section this loclist is relative to */
4436   dw_loc_descr_ref expr;
4437   hashval_t hash;
4438   /* True if all addresses in this and subsequent lists are known to be
4439      resolved.  */
4440   bool resolved_addr;
4441   /* True if this list has been replaced by dw_loc_next.  */
4442   bool replaced;
4443   bool emitted;
4444 } dw_loc_list_node;
4445
4446 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4447
4448 /* Convert a DWARF stack opcode into its string name.  */
4449
4450 static const char *
4451 dwarf_stack_op_name (unsigned int op)
4452 {
4453   switch (op)
4454     {
4455     case DW_OP_addr:
4456       return "DW_OP_addr";
4457     case DW_OP_deref:
4458       return "DW_OP_deref";
4459     case DW_OP_const1u:
4460       return "DW_OP_const1u";
4461     case DW_OP_const1s:
4462       return "DW_OP_const1s";
4463     case DW_OP_const2u:
4464       return "DW_OP_const2u";
4465     case DW_OP_const2s:
4466       return "DW_OP_const2s";
4467     case DW_OP_const4u:
4468       return "DW_OP_const4u";
4469     case DW_OP_const4s:
4470       return "DW_OP_const4s";
4471     case DW_OP_const8u:
4472       return "DW_OP_const8u";
4473     case DW_OP_const8s:
4474       return "DW_OP_const8s";
4475     case DW_OP_constu:
4476       return "DW_OP_constu";
4477     case DW_OP_consts:
4478       return "DW_OP_consts";
4479     case DW_OP_dup:
4480       return "DW_OP_dup";
4481     case DW_OP_drop:
4482       return "DW_OP_drop";
4483     case DW_OP_over:
4484       return "DW_OP_over";
4485     case DW_OP_pick:
4486       return "DW_OP_pick";
4487     case DW_OP_swap:
4488       return "DW_OP_swap";
4489     case DW_OP_rot:
4490       return "DW_OP_rot";
4491     case DW_OP_xderef:
4492       return "DW_OP_xderef";
4493     case DW_OP_abs:
4494       return "DW_OP_abs";
4495     case DW_OP_and:
4496       return "DW_OP_and";
4497     case DW_OP_div:
4498       return "DW_OP_div";
4499     case DW_OP_minus:
4500       return "DW_OP_minus";
4501     case DW_OP_mod:
4502       return "DW_OP_mod";
4503     case DW_OP_mul:
4504       return "DW_OP_mul";
4505     case DW_OP_neg:
4506       return "DW_OP_neg";
4507     case DW_OP_not:
4508       return "DW_OP_not";
4509     case DW_OP_or:
4510       return "DW_OP_or";
4511     case DW_OP_plus:
4512       return "DW_OP_plus";
4513     case DW_OP_plus_uconst:
4514       return "DW_OP_plus_uconst";
4515     case DW_OP_shl:
4516       return "DW_OP_shl";
4517     case DW_OP_shr:
4518       return "DW_OP_shr";
4519     case DW_OP_shra:
4520       return "DW_OP_shra";
4521     case DW_OP_xor:
4522       return "DW_OP_xor";
4523     case DW_OP_bra:
4524       return "DW_OP_bra";
4525     case DW_OP_eq:
4526       return "DW_OP_eq";
4527     case DW_OP_ge:
4528       return "DW_OP_ge";
4529     case DW_OP_gt:
4530       return "DW_OP_gt";
4531     case DW_OP_le:
4532       return "DW_OP_le";
4533     case DW_OP_lt:
4534       return "DW_OP_lt";
4535     case DW_OP_ne:
4536       return "DW_OP_ne";
4537     case DW_OP_skip:
4538       return "DW_OP_skip";
4539     case DW_OP_lit0:
4540       return "DW_OP_lit0";
4541     case DW_OP_lit1:
4542       return "DW_OP_lit1";
4543     case DW_OP_lit2:
4544       return "DW_OP_lit2";
4545     case DW_OP_lit3:
4546       return "DW_OP_lit3";
4547     case DW_OP_lit4:
4548       return "DW_OP_lit4";
4549     case DW_OP_lit5:
4550       return "DW_OP_lit5";
4551     case DW_OP_lit6:
4552       return "DW_OP_lit6";
4553     case DW_OP_lit7:
4554       return "DW_OP_lit7";
4555     case DW_OP_lit8:
4556       return "DW_OP_lit8";
4557     case DW_OP_lit9:
4558       return "DW_OP_lit9";
4559     case DW_OP_lit10:
4560       return "DW_OP_lit10";
4561     case DW_OP_lit11:
4562       return "DW_OP_lit11";
4563     case DW_OP_lit12:
4564       return "DW_OP_lit12";
4565     case DW_OP_lit13:
4566       return "DW_OP_lit13";
4567     case DW_OP_lit14:
4568       return "DW_OP_lit14";
4569     case DW_OP_lit15:
4570       return "DW_OP_lit15";
4571     case DW_OP_lit16:
4572       return "DW_OP_lit16";
4573     case DW_OP_lit17:
4574       return "DW_OP_lit17";
4575     case DW_OP_lit18:
4576       return "DW_OP_lit18";
4577     case DW_OP_lit19:
4578       return "DW_OP_lit19";
4579     case DW_OP_lit20:
4580       return "DW_OP_lit20";
4581     case DW_OP_lit21:
4582       return "DW_OP_lit21";
4583     case DW_OP_lit22:
4584       return "DW_OP_lit22";
4585     case DW_OP_lit23:
4586       return "DW_OP_lit23";
4587     case DW_OP_lit24:
4588       return "DW_OP_lit24";
4589     case DW_OP_lit25:
4590       return "DW_OP_lit25";
4591     case DW_OP_lit26:
4592       return "DW_OP_lit26";
4593     case DW_OP_lit27:
4594       return "DW_OP_lit27";
4595     case DW_OP_lit28:
4596       return "DW_OP_lit28";
4597     case DW_OP_lit29:
4598       return "DW_OP_lit29";
4599     case DW_OP_lit30:
4600       return "DW_OP_lit30";
4601     case DW_OP_lit31:
4602       return "DW_OP_lit31";
4603     case DW_OP_reg0:
4604       return "DW_OP_reg0";
4605     case DW_OP_reg1:
4606       return "DW_OP_reg1";
4607     case DW_OP_reg2:
4608       return "DW_OP_reg2";
4609     case DW_OP_reg3:
4610       return "DW_OP_reg3";
4611     case DW_OP_reg4:
4612       return "DW_OP_reg4";
4613     case DW_OP_reg5:
4614       return "DW_OP_reg5";
4615     case DW_OP_reg6:
4616       return "DW_OP_reg6";
4617     case DW_OP_reg7:
4618       return "DW_OP_reg7";
4619     case DW_OP_reg8:
4620       return "DW_OP_reg8";
4621     case DW_OP_reg9:
4622       return "DW_OP_reg9";
4623     case DW_OP_reg10:
4624       return "DW_OP_reg10";
4625     case DW_OP_reg11:
4626       return "DW_OP_reg11";
4627     case DW_OP_reg12:
4628       return "DW_OP_reg12";
4629     case DW_OP_reg13:
4630       return "DW_OP_reg13";
4631     case DW_OP_reg14:
4632       return "DW_OP_reg14";
4633     case DW_OP_reg15:
4634       return "DW_OP_reg15";
4635     case DW_OP_reg16:
4636       return "DW_OP_reg16";
4637     case DW_OP_reg17:
4638       return "DW_OP_reg17";
4639     case DW_OP_reg18:
4640       return "DW_OP_reg18";
4641     case DW_OP_reg19:
4642       return "DW_OP_reg19";
4643     case DW_OP_reg20:
4644       return "DW_OP_reg20";
4645     case DW_OP_reg21:
4646       return "DW_OP_reg21";
4647     case DW_OP_reg22:
4648       return "DW_OP_reg22";
4649     case DW_OP_reg23:
4650       return "DW_OP_reg23";
4651     case DW_OP_reg24:
4652       return "DW_OP_reg24";
4653     case DW_OP_reg25:
4654       return "DW_OP_reg25";
4655     case DW_OP_reg26:
4656       return "DW_OP_reg26";
4657     case DW_OP_reg27:
4658       return "DW_OP_reg27";
4659     case DW_OP_reg28:
4660       return "DW_OP_reg28";
4661     case DW_OP_reg29:
4662       return "DW_OP_reg29";
4663     case DW_OP_reg30:
4664       return "DW_OP_reg30";
4665     case DW_OP_reg31:
4666       return "DW_OP_reg31";
4667     case DW_OP_breg0:
4668       return "DW_OP_breg0";
4669     case DW_OP_breg1:
4670       return "DW_OP_breg1";
4671     case DW_OP_breg2:
4672       return "DW_OP_breg2";
4673     case DW_OP_breg3:
4674       return "DW_OP_breg3";
4675     case DW_OP_breg4:
4676       return "DW_OP_breg4";
4677     case DW_OP_breg5:
4678       return "DW_OP_breg5";
4679     case DW_OP_breg6:
4680       return "DW_OP_breg6";
4681     case DW_OP_breg7:
4682       return "DW_OP_breg7";
4683     case DW_OP_breg8:
4684       return "DW_OP_breg8";
4685     case DW_OP_breg9:
4686       return "DW_OP_breg9";
4687     case DW_OP_breg10:
4688       return "DW_OP_breg10";
4689     case DW_OP_breg11:
4690       return "DW_OP_breg11";
4691     case DW_OP_breg12:
4692       return "DW_OP_breg12";
4693     case DW_OP_breg13:
4694       return "DW_OP_breg13";
4695     case DW_OP_breg14:
4696       return "DW_OP_breg14";
4697     case DW_OP_breg15:
4698       return "DW_OP_breg15";
4699     case DW_OP_breg16:
4700       return "DW_OP_breg16";
4701     case DW_OP_breg17:
4702       return "DW_OP_breg17";
4703     case DW_OP_breg18:
4704       return "DW_OP_breg18";
4705     case DW_OP_breg19:
4706       return "DW_OP_breg19";
4707     case DW_OP_breg20:
4708       return "DW_OP_breg20";
4709     case DW_OP_breg21:
4710       return "DW_OP_breg21";
4711     case DW_OP_breg22:
4712       return "DW_OP_breg22";
4713     case DW_OP_breg23:
4714       return "DW_OP_breg23";
4715     case DW_OP_breg24:
4716       return "DW_OP_breg24";
4717     case DW_OP_breg25:
4718       return "DW_OP_breg25";
4719     case DW_OP_breg26:
4720       return "DW_OP_breg26";
4721     case DW_OP_breg27:
4722       return "DW_OP_breg27";
4723     case DW_OP_breg28:
4724       return "DW_OP_breg28";
4725     case DW_OP_breg29:
4726       return "DW_OP_breg29";
4727     case DW_OP_breg30:
4728       return "DW_OP_breg30";
4729     case DW_OP_breg31:
4730       return "DW_OP_breg31";
4731     case DW_OP_regx:
4732       return "DW_OP_regx";
4733     case DW_OP_fbreg:
4734       return "DW_OP_fbreg";
4735     case DW_OP_bregx:
4736       return "DW_OP_bregx";
4737     case DW_OP_piece:
4738       return "DW_OP_piece";
4739     case DW_OP_deref_size:
4740       return "DW_OP_deref_size";
4741     case DW_OP_xderef_size:
4742       return "DW_OP_xderef_size";
4743     case DW_OP_nop:
4744       return "DW_OP_nop";
4745
4746     case DW_OP_push_object_address:
4747       return "DW_OP_push_object_address";
4748     case DW_OP_call2:
4749       return "DW_OP_call2";
4750     case DW_OP_call4:
4751       return "DW_OP_call4";
4752     case DW_OP_call_ref:
4753       return "DW_OP_call_ref";
4754     case DW_OP_implicit_value:
4755       return "DW_OP_implicit_value";
4756     case DW_OP_stack_value:
4757       return "DW_OP_stack_value";
4758     case DW_OP_form_tls_address:
4759       return "DW_OP_form_tls_address";
4760     case DW_OP_call_frame_cfa:
4761       return "DW_OP_call_frame_cfa";
4762     case DW_OP_bit_piece:
4763       return "DW_OP_bit_piece";
4764
4765     case DW_OP_GNU_push_tls_address:
4766       return "DW_OP_GNU_push_tls_address";
4767     case DW_OP_GNU_uninit:
4768       return "DW_OP_GNU_uninit";
4769     case DW_OP_GNU_encoded_addr:
4770       return "DW_OP_GNU_encoded_addr";
4771     case DW_OP_GNU_implicit_pointer:
4772       return "DW_OP_GNU_implicit_pointer";
4773     case DW_OP_GNU_entry_value:
4774       return "DW_OP_GNU_entry_value";
4775
4776     default:
4777       return "OP_<unknown>";
4778     }
4779 }
4780
4781 /* Return a pointer to a newly allocated location description.  Location
4782    descriptions are simple expression terms that can be strung
4783    together to form more complicated location (address) descriptions.  */
4784
4785 static inline dw_loc_descr_ref
4786 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4787                unsigned HOST_WIDE_INT oprnd2)
4788 {
4789   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4790
4791   descr->dw_loc_opc = op;
4792   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4793   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4794   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4795   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4796
4797   return descr;
4798 }
4799
4800 /* Return a pointer to a newly allocated location description for
4801    REG and OFFSET.  */
4802
4803 static inline dw_loc_descr_ref
4804 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4805 {
4806   if (reg <= 31)
4807     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4808                           offset, 0);
4809   else
4810     return new_loc_descr (DW_OP_bregx, reg, offset);
4811 }
4812
4813 /* Add a location description term to a location description expression.  */
4814
4815 static inline void
4816 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4817 {
4818   dw_loc_descr_ref *d;
4819
4820   /* Find the end of the chain.  */
4821   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4822     ;
4823
4824   *d = descr;
4825 }
4826
4827 /* Add a constant OFFSET to a location expression.  */
4828
4829 static void
4830 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4831 {
4832   dw_loc_descr_ref loc;
4833   HOST_WIDE_INT *p;
4834
4835   gcc_assert (*list_head != NULL);
4836
4837   if (!offset)
4838     return;
4839
4840   /* Find the end of the chain.  */
4841   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4842     ;
4843
4844   p = NULL;
4845   if (loc->dw_loc_opc == DW_OP_fbreg
4846       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4847     p = &loc->dw_loc_oprnd1.v.val_int;
4848   else if (loc->dw_loc_opc == DW_OP_bregx)
4849     p = &loc->dw_loc_oprnd2.v.val_int;
4850
4851   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4852      offset.  Don't optimize if an signed integer overflow would happen.  */
4853   if (p != NULL
4854       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4855           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4856     *p += offset;
4857
4858   else if (offset > 0)
4859     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4860
4861   else
4862     {
4863       loc->dw_loc_next = int_loc_descriptor (-offset);
4864       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4865     }
4866 }
4867
4868 /* Add a constant OFFSET to a location list.  */
4869
4870 static void
4871 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4872 {
4873   dw_loc_list_ref d;
4874   for (d = list_head; d != NULL; d = d->dw_loc_next)
4875     loc_descr_plus_const (&d->expr, offset);
4876 }
4877
4878 #define DWARF_REF_SIZE  \
4879   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4880
4881 static unsigned long size_of_locs (dw_loc_descr_ref);
4882
4883 /* Return the size of a location descriptor.  */
4884
4885 static unsigned long
4886 size_of_loc_descr (dw_loc_descr_ref loc)
4887 {
4888   unsigned long size = 1;
4889
4890   switch (loc->dw_loc_opc)
4891     {
4892     case DW_OP_addr:
4893       size += DWARF2_ADDR_SIZE;
4894       break;
4895     case DW_OP_const1u:
4896     case DW_OP_const1s:
4897       size += 1;
4898       break;
4899     case DW_OP_const2u:
4900     case DW_OP_const2s:
4901       size += 2;
4902       break;
4903     case DW_OP_const4u:
4904     case DW_OP_const4s:
4905       size += 4;
4906       break;
4907     case DW_OP_const8u:
4908     case DW_OP_const8s:
4909       size += 8;
4910       break;
4911     case DW_OP_constu:
4912       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4913       break;
4914     case DW_OP_consts:
4915       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4916       break;
4917     case DW_OP_pick:
4918       size += 1;
4919       break;
4920     case DW_OP_plus_uconst:
4921       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4922       break;
4923     case DW_OP_skip:
4924     case DW_OP_bra:
4925       size += 2;
4926       break;
4927     case DW_OP_breg0:
4928     case DW_OP_breg1:
4929     case DW_OP_breg2:
4930     case DW_OP_breg3:
4931     case DW_OP_breg4:
4932     case DW_OP_breg5:
4933     case DW_OP_breg6:
4934     case DW_OP_breg7:
4935     case DW_OP_breg8:
4936     case DW_OP_breg9:
4937     case DW_OP_breg10:
4938     case DW_OP_breg11:
4939     case DW_OP_breg12:
4940     case DW_OP_breg13:
4941     case DW_OP_breg14:
4942     case DW_OP_breg15:
4943     case DW_OP_breg16:
4944     case DW_OP_breg17:
4945     case DW_OP_breg18:
4946     case DW_OP_breg19:
4947     case DW_OP_breg20:
4948     case DW_OP_breg21:
4949     case DW_OP_breg22:
4950     case DW_OP_breg23:
4951     case DW_OP_breg24:
4952     case DW_OP_breg25:
4953     case DW_OP_breg26:
4954     case DW_OP_breg27:
4955     case DW_OP_breg28:
4956     case DW_OP_breg29:
4957     case DW_OP_breg30:
4958     case DW_OP_breg31:
4959       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4960       break;
4961     case DW_OP_regx:
4962       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4963       break;
4964     case DW_OP_fbreg:
4965       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4966       break;
4967     case DW_OP_bregx:
4968       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4969       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4970       break;
4971     case DW_OP_piece:
4972       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4973       break;
4974     case DW_OP_bit_piece:
4975       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4976       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4977       break;
4978     case DW_OP_deref_size:
4979     case DW_OP_xderef_size:
4980       size += 1;
4981       break;
4982     case DW_OP_call2:
4983       size += 2;
4984       break;
4985     case DW_OP_call4:
4986       size += 4;
4987       break;
4988     case DW_OP_call_ref:
4989       size += DWARF_REF_SIZE;
4990       break;
4991     case DW_OP_implicit_value:
4992       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4993               + loc->dw_loc_oprnd1.v.val_unsigned;
4994       break;
4995     case DW_OP_GNU_implicit_pointer:
4996       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4997       break;
4998     case DW_OP_GNU_entry_value:
4999       {
5000         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5001         size += size_of_uleb128 (op_size) + op_size;
5002         break;
5003       }
5004     default:
5005       break;
5006     }
5007
5008   return size;
5009 }
5010
5011 /* Return the size of a series of location descriptors.  */
5012
5013 static unsigned long
5014 size_of_locs (dw_loc_descr_ref loc)
5015 {
5016   dw_loc_descr_ref l;
5017   unsigned long size;
5018
5019   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5020      field, to avoid writing to a PCH file.  */
5021   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5022     {
5023       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5024         break;
5025       size += size_of_loc_descr (l);
5026     }
5027   if (! l)
5028     return size;
5029
5030   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5031     {
5032       l->dw_loc_addr = size;
5033       size += size_of_loc_descr (l);
5034     }
5035
5036   return size;
5037 }
5038
5039 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5040 static void get_ref_die_offset_label (char *, dw_die_ref);
5041 static void output_loc_sequence (dw_loc_descr_ref, int);
5042
5043 /* Output location description stack opcode's operands (if any).
5044    The for_eh_or_skip parameter controls whether register numbers are
5045    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5046    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5047    info).  This should be suppressed for the cases that have not been converted
5048    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5049
5050 static void
5051 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5052 {
5053   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5054   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5055
5056   switch (loc->dw_loc_opc)
5057     {
5058 #ifdef DWARF2_DEBUGGING_INFO
5059     case DW_OP_const2u:
5060     case DW_OP_const2s:
5061       dw2_asm_output_data (2, val1->v.val_int, NULL);
5062       break;
5063     case DW_OP_const4u:
5064       if (loc->dtprel)
5065         {
5066           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5067           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5068                                                val1->v.val_addr);
5069           fputc ('\n', asm_out_file);
5070           break;
5071         }
5072       /* FALLTHRU */
5073     case DW_OP_const4s:
5074       dw2_asm_output_data (4, val1->v.val_int, NULL);
5075       break;
5076     case DW_OP_const8u:
5077       if (loc->dtprel)
5078         {
5079           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5080           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5081                                                val1->v.val_addr);
5082           fputc ('\n', asm_out_file);
5083           break;
5084         }
5085       /* FALLTHRU */
5086     case DW_OP_const8s:
5087       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5088       dw2_asm_output_data (8, val1->v.val_int, NULL);
5089       break;
5090     case DW_OP_skip:
5091     case DW_OP_bra:
5092       {
5093         int offset;
5094
5095         gcc_assert (val1->val_class == dw_val_class_loc);
5096         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5097
5098         dw2_asm_output_data (2, offset, NULL);
5099       }
5100       break;
5101     case DW_OP_implicit_value:
5102       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5103       switch (val2->val_class)
5104         {
5105         case dw_val_class_const:
5106           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5107           break;
5108         case dw_val_class_vec:
5109           {
5110             unsigned int elt_size = val2->v.val_vec.elt_size;
5111             unsigned int len = val2->v.val_vec.length;
5112             unsigned int i;
5113             unsigned char *p;
5114
5115             if (elt_size > sizeof (HOST_WIDE_INT))
5116               {
5117                 elt_size /= 2;
5118                 len *= 2;
5119               }
5120             for (i = 0, p = val2->v.val_vec.array;
5121                  i < len;
5122                  i++, p += elt_size)
5123               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5124                                    "fp or vector constant word %u", i);
5125           }
5126           break;
5127         case dw_val_class_const_double:
5128           {
5129             unsigned HOST_WIDE_INT first, second;
5130
5131             if (WORDS_BIG_ENDIAN)
5132               {
5133                 first = val2->v.val_double.high;
5134                 second = val2->v.val_double.low;
5135               }
5136             else
5137               {
5138                 first = val2->v.val_double.low;
5139                 second = val2->v.val_double.high;
5140               }
5141             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5142                                  first, NULL);
5143             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5144                                  second, NULL);
5145           }
5146           break;
5147         case dw_val_class_addr:
5148           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5149           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5150           break;
5151         default:
5152           gcc_unreachable ();
5153         }
5154       break;
5155 #else
5156     case DW_OP_const2u:
5157     case DW_OP_const2s:
5158     case DW_OP_const4u:
5159     case DW_OP_const4s:
5160     case DW_OP_const8u:
5161     case DW_OP_const8s:
5162     case DW_OP_skip:
5163     case DW_OP_bra:
5164     case DW_OP_implicit_value:
5165       /* We currently don't make any attempt to make sure these are
5166          aligned properly like we do for the main unwind info, so
5167          don't support emitting things larger than a byte if we're
5168          only doing unwinding.  */
5169       gcc_unreachable ();
5170 #endif
5171     case DW_OP_const1u:
5172     case DW_OP_const1s:
5173       dw2_asm_output_data (1, val1->v.val_int, NULL);
5174       break;
5175     case DW_OP_constu:
5176       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5177       break;
5178     case DW_OP_consts:
5179       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5180       break;
5181     case DW_OP_pick:
5182       dw2_asm_output_data (1, val1->v.val_int, NULL);
5183       break;
5184     case DW_OP_plus_uconst:
5185       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5186       break;
5187     case DW_OP_breg0:
5188     case DW_OP_breg1:
5189     case DW_OP_breg2:
5190     case DW_OP_breg3:
5191     case DW_OP_breg4:
5192     case DW_OP_breg5:
5193     case DW_OP_breg6:
5194     case DW_OP_breg7:
5195     case DW_OP_breg8:
5196     case DW_OP_breg9:
5197     case DW_OP_breg10:
5198     case DW_OP_breg11:
5199     case DW_OP_breg12:
5200     case DW_OP_breg13:
5201     case DW_OP_breg14:
5202     case DW_OP_breg15:
5203     case DW_OP_breg16:
5204     case DW_OP_breg17:
5205     case DW_OP_breg18:
5206     case DW_OP_breg19:
5207     case DW_OP_breg20:
5208     case DW_OP_breg21:
5209     case DW_OP_breg22:
5210     case DW_OP_breg23:
5211     case DW_OP_breg24:
5212     case DW_OP_breg25:
5213     case DW_OP_breg26:
5214     case DW_OP_breg27:
5215     case DW_OP_breg28:
5216     case DW_OP_breg29:
5217     case DW_OP_breg30:
5218     case DW_OP_breg31:
5219       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5220       break;
5221     case DW_OP_regx:
5222       {
5223         unsigned r = val1->v.val_unsigned;
5224         if (for_eh_or_skip >= 0)
5225           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5226         gcc_assert (size_of_uleb128 (r) 
5227                     == size_of_uleb128 (val1->v.val_unsigned));
5228         dw2_asm_output_data_uleb128 (r, NULL);  
5229       }
5230       break;
5231     case DW_OP_fbreg:
5232       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5233       break;
5234     case DW_OP_bregx:
5235       {
5236         unsigned r = val1->v.val_unsigned;
5237         if (for_eh_or_skip >= 0)
5238           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5239         gcc_assert (size_of_uleb128 (r) 
5240                     == size_of_uleb128 (val1->v.val_unsigned));
5241         dw2_asm_output_data_uleb128 (r, NULL);  
5242         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5243       }
5244       break;
5245     case DW_OP_piece:
5246       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5247       break;
5248     case DW_OP_bit_piece:
5249       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5250       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5251       break;
5252     case DW_OP_deref_size:
5253     case DW_OP_xderef_size:
5254       dw2_asm_output_data (1, val1->v.val_int, NULL);
5255       break;
5256
5257     case DW_OP_addr:
5258       if (loc->dtprel)
5259         {
5260           if (targetm.asm_out.output_dwarf_dtprel)
5261             {
5262               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5263                                                    DWARF2_ADDR_SIZE,
5264                                                    val1->v.val_addr);
5265               fputc ('\n', asm_out_file);
5266             }
5267           else
5268             gcc_unreachable ();
5269         }
5270       else
5271         {
5272 #ifdef DWARF2_DEBUGGING_INFO
5273           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5274 #else
5275           gcc_unreachable ();
5276 #endif
5277         }
5278       break;
5279
5280     case DW_OP_GNU_implicit_pointer:
5281       {
5282         char label[MAX_ARTIFICIAL_LABEL_BYTES
5283                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5284         gcc_assert (val1->val_class == dw_val_class_die_ref);
5285         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5286         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5287         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5288       }
5289       break;
5290
5291     case DW_OP_GNU_entry_value:
5292       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5293       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5294       break;
5295
5296     default:
5297       /* Other codes have no operands.  */
5298       break;
5299     }
5300 }
5301
5302 /* Output a sequence of location operations.  
5303    The for_eh_or_skip parameter controls whether register numbers are
5304    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5305    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5306    info).  This should be suppressed for the cases that have not been converted
5307    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5308
5309 static void
5310 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5311 {
5312   for (; loc != NULL; loc = loc->dw_loc_next)
5313     {
5314       enum dwarf_location_atom opc = loc->dw_loc_opc;
5315       /* Output the opcode.  */
5316       if (for_eh_or_skip >= 0 
5317           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5318         {
5319           unsigned r = (opc - DW_OP_breg0);
5320           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5321           gcc_assert (r <= 31);
5322           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5323         }
5324       else if (for_eh_or_skip >= 0 
5325                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5326         {
5327           unsigned r = (opc - DW_OP_reg0);
5328           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5329           gcc_assert (r <= 31);
5330           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5331         }
5332
5333       dw2_asm_output_data (1, opc,
5334                              "%s", dwarf_stack_op_name (opc));
5335
5336       /* Output the operand(s) (if any).  */
5337       output_loc_operands (loc, for_eh_or_skip);
5338     }
5339 }
5340
5341 /* Output location description stack opcode's operands (if any).
5342    The output is single bytes on a line, suitable for .cfi_escape.  */
5343
5344 static void
5345 output_loc_operands_raw (dw_loc_descr_ref loc)
5346 {
5347   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5348   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5349
5350   switch (loc->dw_loc_opc)
5351     {
5352     case DW_OP_addr:
5353     case DW_OP_implicit_value:
5354       /* We cannot output addresses in .cfi_escape, only bytes.  */
5355       gcc_unreachable ();
5356
5357     case DW_OP_const1u:
5358     case DW_OP_const1s:
5359     case DW_OP_pick:
5360     case DW_OP_deref_size:
5361     case DW_OP_xderef_size:
5362       fputc (',', asm_out_file);
5363       dw2_asm_output_data_raw (1, val1->v.val_int);
5364       break;
5365
5366     case DW_OP_const2u:
5367     case DW_OP_const2s:
5368       fputc (',', asm_out_file);
5369       dw2_asm_output_data_raw (2, val1->v.val_int);
5370       break;
5371
5372     case DW_OP_const4u:
5373     case DW_OP_const4s:
5374       fputc (',', asm_out_file);
5375       dw2_asm_output_data_raw (4, val1->v.val_int);
5376       break;
5377
5378     case DW_OP_const8u:
5379     case DW_OP_const8s:
5380       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5381       fputc (',', asm_out_file);
5382       dw2_asm_output_data_raw (8, val1->v.val_int);
5383       break;
5384
5385     case DW_OP_skip:
5386     case DW_OP_bra:
5387       {
5388         int offset;
5389
5390         gcc_assert (val1->val_class == dw_val_class_loc);
5391         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5392
5393         fputc (',', asm_out_file);
5394         dw2_asm_output_data_raw (2, offset);
5395       }
5396       break;
5397
5398     case DW_OP_regx:
5399       {
5400         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5401         gcc_assert (size_of_uleb128 (r) 
5402                     == size_of_uleb128 (val1->v.val_unsigned));
5403         fputc (',', asm_out_file);
5404         dw2_asm_output_data_uleb128_raw (r);
5405       }
5406       break;
5407       
5408     case DW_OP_constu:
5409     case DW_OP_plus_uconst:
5410     case DW_OP_piece:
5411       fputc (',', asm_out_file);
5412       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5413       break;
5414
5415     case DW_OP_bit_piece:
5416       fputc (',', asm_out_file);
5417       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5418       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5419       break;
5420
5421     case DW_OP_consts:
5422     case DW_OP_breg0:
5423     case DW_OP_breg1:
5424     case DW_OP_breg2:
5425     case DW_OP_breg3:
5426     case DW_OP_breg4:
5427     case DW_OP_breg5:
5428     case DW_OP_breg6:
5429     case DW_OP_breg7:
5430     case DW_OP_breg8:
5431     case DW_OP_breg9:
5432     case DW_OP_breg10:
5433     case DW_OP_breg11:
5434     case DW_OP_breg12:
5435     case DW_OP_breg13:
5436     case DW_OP_breg14:
5437     case DW_OP_breg15:
5438     case DW_OP_breg16:
5439     case DW_OP_breg17:
5440     case DW_OP_breg18:
5441     case DW_OP_breg19:
5442     case DW_OP_breg20:
5443     case DW_OP_breg21:
5444     case DW_OP_breg22:
5445     case DW_OP_breg23:
5446     case DW_OP_breg24:
5447     case DW_OP_breg25:
5448     case DW_OP_breg26:
5449     case DW_OP_breg27:
5450     case DW_OP_breg28:
5451     case DW_OP_breg29:
5452     case DW_OP_breg30:
5453     case DW_OP_breg31:
5454     case DW_OP_fbreg:
5455       fputc (',', asm_out_file);
5456       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5457       break;
5458
5459     case DW_OP_bregx:
5460       {
5461         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5462         gcc_assert (size_of_uleb128 (r) 
5463                     == size_of_uleb128 (val1->v.val_unsigned));
5464         fputc (',', asm_out_file);
5465         dw2_asm_output_data_uleb128_raw (r);
5466         fputc (',', asm_out_file);
5467         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5468       }
5469       break;
5470
5471     case DW_OP_GNU_implicit_pointer:
5472     case DW_OP_GNU_entry_value:
5473       gcc_unreachable ();
5474       break;
5475
5476     default:
5477       /* Other codes have no operands.  */
5478       break;
5479     }
5480 }
5481
5482 static void
5483 output_loc_sequence_raw (dw_loc_descr_ref loc)
5484 {
5485   while (1)
5486     {
5487       enum dwarf_location_atom opc = loc->dw_loc_opc;
5488       /* Output the opcode.  */
5489       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5490         {
5491           unsigned r = (opc - DW_OP_breg0);
5492           r = DWARF2_FRAME_REG_OUT (r, 1);
5493           gcc_assert (r <= 31);
5494           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5495         }
5496       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5497         {
5498           unsigned r = (opc - DW_OP_reg0);
5499           r = DWARF2_FRAME_REG_OUT (r, 1);
5500           gcc_assert (r <= 31);
5501           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5502         }
5503       /* Output the opcode.  */
5504       fprintf (asm_out_file, "%#x", opc);
5505       output_loc_operands_raw (loc);
5506
5507       if (!loc->dw_loc_next)
5508         break;
5509       loc = loc->dw_loc_next;
5510
5511       fputc (',', asm_out_file);
5512     }
5513 }
5514
5515 /* This routine will generate the correct assembly data for a location
5516    description based on a cfi entry with a complex address.  */
5517
5518 static void
5519 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5520 {
5521   dw_loc_descr_ref loc;
5522   unsigned long size;
5523
5524   if (cfi->dw_cfi_opc == DW_CFA_expression)
5525     {
5526       unsigned r = 
5527         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5528       dw2_asm_output_data (1, r, NULL);
5529       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5530     }
5531   else
5532     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5533
5534   /* Output the size of the block.  */
5535   size = size_of_locs (loc);
5536   dw2_asm_output_data_uleb128 (size, NULL);
5537
5538   /* Now output the operations themselves.  */
5539   output_loc_sequence (loc, for_eh);
5540 }
5541
5542 /* Similar, but used for .cfi_escape.  */
5543
5544 static void
5545 output_cfa_loc_raw (dw_cfi_ref cfi)
5546 {
5547   dw_loc_descr_ref loc;
5548   unsigned long size;
5549
5550   if (cfi->dw_cfi_opc == DW_CFA_expression)
5551     {
5552       unsigned r = 
5553         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5554       fprintf (asm_out_file, "%#x,", r);
5555       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5556     }
5557   else
5558     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5559
5560   /* Output the size of the block.  */
5561   size = size_of_locs (loc);
5562   dw2_asm_output_data_uleb128_raw (size);
5563   fputc (',', asm_out_file);
5564
5565   /* Now output the operations themselves.  */
5566   output_loc_sequence_raw (loc);
5567 }
5568
5569 /* This function builds a dwarf location descriptor sequence from a
5570    dw_cfa_location, adding the given OFFSET to the result of the
5571    expression.  */
5572
5573 static struct dw_loc_descr_struct *
5574 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5575 {
5576   struct dw_loc_descr_struct *head, *tmp;
5577
5578   offset += cfa->offset;
5579
5580   if (cfa->indirect)
5581     {
5582       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5583       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5584       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5585       add_loc_descr (&head, tmp);
5586       if (offset != 0)
5587         {
5588           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5589           add_loc_descr (&head, tmp);
5590         }
5591     }
5592   else
5593     head = new_reg_loc_descr (cfa->reg, offset);
5594
5595   return head;
5596 }
5597
5598 /* This function builds a dwarf location descriptor sequence for
5599    the address at OFFSET from the CFA when stack is aligned to
5600    ALIGNMENT byte.  */
5601
5602 static struct dw_loc_descr_struct *
5603 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5604 {
5605   struct dw_loc_descr_struct *head;
5606   unsigned int dwarf_fp
5607     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5608
5609  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5610   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5611     {
5612       head = new_reg_loc_descr (dwarf_fp, 0);
5613       add_loc_descr (&head, int_loc_descriptor (alignment));
5614       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5615       loc_descr_plus_const (&head, offset);
5616     }
5617   else
5618     head = new_reg_loc_descr (dwarf_fp, offset);
5619   return head;
5620 }
5621
5622 /* This function fills in aa dw_cfa_location structure from a dwarf location
5623    descriptor sequence.  */
5624
5625 static void
5626 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5627 {
5628   struct dw_loc_descr_struct *ptr;
5629   cfa->offset = 0;
5630   cfa->base_offset = 0;
5631   cfa->indirect = 0;
5632   cfa->reg = -1;
5633
5634   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5635     {
5636       enum dwarf_location_atom op = ptr->dw_loc_opc;
5637
5638       switch (op)
5639         {
5640         case DW_OP_reg0:
5641         case DW_OP_reg1:
5642         case DW_OP_reg2:
5643         case DW_OP_reg3:
5644         case DW_OP_reg4:
5645         case DW_OP_reg5:
5646         case DW_OP_reg6:
5647         case DW_OP_reg7:
5648         case DW_OP_reg8:
5649         case DW_OP_reg9:
5650         case DW_OP_reg10:
5651         case DW_OP_reg11:
5652         case DW_OP_reg12:
5653         case DW_OP_reg13:
5654         case DW_OP_reg14:
5655         case DW_OP_reg15:
5656         case DW_OP_reg16:
5657         case DW_OP_reg17:
5658         case DW_OP_reg18:
5659         case DW_OP_reg19:
5660         case DW_OP_reg20:
5661         case DW_OP_reg21:
5662         case DW_OP_reg22:
5663         case DW_OP_reg23:
5664         case DW_OP_reg24:
5665         case DW_OP_reg25:
5666         case DW_OP_reg26:
5667         case DW_OP_reg27:
5668         case DW_OP_reg28:
5669         case DW_OP_reg29:
5670         case DW_OP_reg30:
5671         case DW_OP_reg31:
5672           cfa->reg = op - DW_OP_reg0;
5673           break;
5674         case DW_OP_regx:
5675           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5676           break;
5677         case DW_OP_breg0:
5678         case DW_OP_breg1:
5679         case DW_OP_breg2:
5680         case DW_OP_breg3:
5681         case DW_OP_breg4:
5682         case DW_OP_breg5:
5683         case DW_OP_breg6:
5684         case DW_OP_breg7:
5685         case DW_OP_breg8:
5686         case DW_OP_breg9:
5687         case DW_OP_breg10:
5688         case DW_OP_breg11:
5689         case DW_OP_breg12:
5690         case DW_OP_breg13:
5691         case DW_OP_breg14:
5692         case DW_OP_breg15:
5693         case DW_OP_breg16:
5694         case DW_OP_breg17:
5695         case DW_OP_breg18:
5696         case DW_OP_breg19:
5697         case DW_OP_breg20:
5698         case DW_OP_breg21:
5699         case DW_OP_breg22:
5700         case DW_OP_breg23:
5701         case DW_OP_breg24:
5702         case DW_OP_breg25:
5703         case DW_OP_breg26:
5704         case DW_OP_breg27:
5705         case DW_OP_breg28:
5706         case DW_OP_breg29:
5707         case DW_OP_breg30:
5708         case DW_OP_breg31:
5709           cfa->reg = op - DW_OP_breg0;
5710           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5711           break;
5712         case DW_OP_bregx:
5713           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5714           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5715           break;
5716         case DW_OP_deref:
5717           cfa->indirect = 1;
5718           break;
5719         case DW_OP_plus_uconst:
5720           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5721           break;
5722         default:
5723           internal_error ("DW_LOC_OP %s not implemented",
5724                           dwarf_stack_op_name (ptr->dw_loc_opc));
5725         }
5726     }
5727 }
5728 \f
5729 /* And now, the support for symbolic debugging information.  */
5730
5731 /* .debug_str support.  */
5732 static int output_indirect_string (void **, void *);
5733
5734 static void dwarf2out_init (const char *);
5735 static void dwarf2out_finish (const char *);
5736 static void dwarf2out_assembly_start (void);
5737 static void dwarf2out_define (unsigned int, const char *);
5738 static void dwarf2out_undef (unsigned int, const char *);
5739 static void dwarf2out_start_source_file (unsigned, const char *);
5740 static void dwarf2out_end_source_file (unsigned);
5741 static void dwarf2out_function_decl (tree);
5742 static void dwarf2out_begin_block (unsigned, unsigned);
5743 static void dwarf2out_end_block (unsigned, unsigned);
5744 static bool dwarf2out_ignore_block (const_tree);
5745 static void dwarf2out_global_decl (tree);
5746 static void dwarf2out_type_decl (tree, int);
5747 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5748 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5749                                                  dw_die_ref);
5750 static void dwarf2out_abstract_function (tree);
5751 static void dwarf2out_var_location (rtx);
5752 static void dwarf2out_begin_function (tree);
5753 static void dwarf2out_set_name (tree, tree);
5754
5755 /* The debug hooks structure.  */
5756
5757 const struct gcc_debug_hooks dwarf2_debug_hooks =
5758 {
5759   dwarf2out_init,
5760   dwarf2out_finish,
5761   dwarf2out_assembly_start,
5762   dwarf2out_define,
5763   dwarf2out_undef,
5764   dwarf2out_start_source_file,
5765   dwarf2out_end_source_file,
5766   dwarf2out_begin_block,
5767   dwarf2out_end_block,
5768   dwarf2out_ignore_block,
5769   dwarf2out_source_line,
5770   dwarf2out_begin_prologue,
5771 #if VMS_DEBUGGING_INFO
5772   dwarf2out_vms_end_prologue,
5773   dwarf2out_vms_begin_epilogue,
5774 #else
5775   debug_nothing_int_charstar,
5776   debug_nothing_int_charstar,
5777 #endif
5778   dwarf2out_end_epilogue,
5779   dwarf2out_begin_function,
5780   debug_nothing_int,            /* end_function */
5781   dwarf2out_function_decl,      /* function_decl */
5782   dwarf2out_global_decl,
5783   dwarf2out_type_decl,          /* type_decl */
5784   dwarf2out_imported_module_or_decl,
5785   debug_nothing_tree,           /* deferred_inline_function */
5786   /* The DWARF 2 backend tries to reduce debugging bloat by not
5787      emitting the abstract description of inline functions until
5788      something tries to reference them.  */
5789   dwarf2out_abstract_function,  /* outlining_inline_function */
5790   debug_nothing_rtx,            /* label */
5791   debug_nothing_int,            /* handle_pch */
5792   dwarf2out_var_location,
5793   dwarf2out_switch_text_section,
5794   dwarf2out_set_name,
5795   1,                            /* start_end_main_source_file */
5796   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5797 };
5798 \f
5799 /* NOTE: In the comments in this file, many references are made to
5800    "Debugging Information Entries".  This term is abbreviated as `DIE'
5801    throughout the remainder of this file.  */
5802
5803 /* An internal representation of the DWARF output is built, and then
5804    walked to generate the DWARF debugging info.  The walk of the internal
5805    representation is done after the entire program has been compiled.
5806    The types below are used to describe the internal representation.  */
5807
5808 /* Whether to put type DIEs into their own section .debug_types instead
5809    of making them part of the .debug_info section.  Only supported for
5810    Dwarf V4 or higher and the user didn't disable them through
5811    -fno-debug-types-section.  It is more efficient to put them in a
5812    separate comdat sections since the linker will then be able to
5813    remove duplicates.  But not all tools support .debug_types sections
5814    yet.  */
5815
5816 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
5817
5818 /* Various DIE's use offsets relative to the beginning of the
5819    .debug_info section to refer to each other.  */
5820
5821 typedef long int dw_offset;
5822
5823 /* Define typedefs here to avoid circular dependencies.  */
5824
5825 typedef struct dw_attr_struct *dw_attr_ref;
5826 typedef struct dw_line_info_struct *dw_line_info_ref;
5827 typedef struct pubname_struct *pubname_ref;
5828 typedef struct dw_ranges_struct *dw_ranges_ref;
5829 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5830 typedef struct comdat_type_struct *comdat_type_node_ref;
5831
5832 /* The entries in the line_info table more-or-less mirror the opcodes
5833    that are used in the real dwarf line table.  Arrays of these entries
5834    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
5835    supported.  */
5836
5837 enum dw_line_info_opcode {
5838   /* Emit DW_LNE_set_address; the operand is the label index.  */
5839   LI_set_address,
5840
5841   /* Emit a row to the matrix with the given line.  This may be done
5842      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
5843      special opcodes.  */
5844   LI_set_line,
5845
5846   /* Emit a DW_LNS_set_file.  */
5847   LI_set_file,
5848
5849   /* Emit a DW_LNS_set_column.  */
5850   LI_set_column,
5851
5852   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
5853   LI_negate_stmt,
5854
5855   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
5856   LI_set_prologue_end,
5857   LI_set_epilogue_begin,
5858
5859   /* Emit a DW_LNE_set_discriminator.  */
5860   LI_set_discriminator
5861 };
5862
5863 typedef struct GTY(()) dw_line_info_struct {
5864   enum dw_line_info_opcode opcode;
5865   unsigned int val;
5866 } dw_line_info_entry;
5867
5868 DEF_VEC_O(dw_line_info_entry);
5869 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
5870
5871 typedef struct GTY(()) dw_line_info_table_struct {
5872   /* The label that marks the end of this section.  */
5873   const char *end_label;
5874
5875   /* The values for the last row of the matrix, as collected in the table.
5876      These are used to minimize the changes to the next row.  */
5877   unsigned int file_num;
5878   unsigned int line_num;
5879   unsigned int column_num;
5880   int discrim_num;
5881   bool is_stmt;
5882   bool in_use;
5883
5884   VEC(dw_line_info_entry, gc) *entries;
5885 } dw_line_info_table;
5886
5887 typedef dw_line_info_table *dw_line_info_table_p;
5888
5889 DEF_VEC_P(dw_line_info_table_p);
5890 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
5891
5892 /* Each DIE attribute has a field specifying the attribute kind,
5893    a link to the next attribute in the chain, and an attribute value.
5894    Attributes are typically linked below the DIE they modify.  */
5895
5896 typedef struct GTY(()) dw_attr_struct {
5897   enum dwarf_attribute dw_attr;
5898   dw_val_node dw_attr_val;
5899 }
5900 dw_attr_node;
5901
5902 DEF_VEC_O(dw_attr_node);
5903 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5904
5905 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5906    The children of each node form a circular list linked by
5907    die_sib.  die_child points to the node *before* the "first" child node.  */
5908
5909 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5910   union die_symbol_or_type_node
5911     {
5912       char * GTY ((tag ("0"))) die_symbol;
5913       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5914     }
5915   GTY ((desc ("use_debug_types"))) die_id;
5916   VEC(dw_attr_node,gc) * die_attr;
5917   dw_die_ref die_parent;
5918   dw_die_ref die_child;
5919   dw_die_ref die_sib;
5920   dw_die_ref die_definition; /* ref from a specification to its definition */
5921   dw_offset die_offset;
5922   unsigned long die_abbrev;
5923   int die_mark;
5924   /* Die is used and must not be pruned as unused.  */
5925   int die_perennial_p;
5926   unsigned int decl_id;
5927   enum dwarf_tag die_tag;
5928 }
5929 die_node;
5930
5931 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5932 #define FOR_EACH_CHILD(die, c, expr) do {       \
5933   c = die->die_child;                           \
5934   if (c) do {                                   \
5935     c = c->die_sib;                             \
5936     expr;                                       \
5937   } while (c != die->die_child);                \
5938 } while (0)
5939
5940 /* The pubname structure */
5941
5942 typedef struct GTY(()) pubname_struct {
5943   dw_die_ref die;
5944   const char *name;
5945 }
5946 pubname_entry;
5947
5948 DEF_VEC_O(pubname_entry);
5949 DEF_VEC_ALLOC_O(pubname_entry, gc);
5950
5951 struct GTY(()) dw_ranges_struct {
5952   /* If this is positive, it's a block number, otherwise it's a
5953      bitwise-negated index into dw_ranges_by_label.  */
5954   int num;
5955 };
5956
5957 /* A structure to hold a macinfo entry.  */
5958
5959 typedef struct GTY(()) macinfo_struct {
5960   unsigned HOST_WIDE_INT code;
5961   unsigned HOST_WIDE_INT lineno;
5962   const char *info;
5963 }
5964 macinfo_entry;
5965
5966 DEF_VEC_O(macinfo_entry);
5967 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5968
5969 struct GTY(()) dw_ranges_by_label_struct {
5970   const char *begin;
5971   const char *end;
5972 };
5973
5974 /* The comdat type node structure.  */
5975 typedef struct GTY(()) comdat_type_struct
5976 {
5977   dw_die_ref root_die;
5978   dw_die_ref type_die;
5979   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5980   struct comdat_type_struct *next;
5981 }
5982 comdat_type_node;
5983
5984 /* The limbo die list structure.  */
5985 typedef struct GTY(()) limbo_die_struct {
5986   dw_die_ref die;
5987   tree created_for;
5988   struct limbo_die_struct *next;
5989 }
5990 limbo_die_node;
5991
5992 typedef struct skeleton_chain_struct
5993 {
5994   dw_die_ref old_die;
5995   dw_die_ref new_die;
5996   struct skeleton_chain_struct *parent;
5997 }
5998 skeleton_chain_node;
5999
6000 /* How to start an assembler comment.  */
6001 #ifndef ASM_COMMENT_START
6002 #define ASM_COMMENT_START ";#"
6003 #endif
6004
6005 /* Define a macro which returns nonzero for a TYPE_DECL which was
6006    implicitly generated for a tagged type.
6007
6008    Note that unlike the gcc front end (which generates a NULL named
6009    TYPE_DECL node for each complete tagged type, each array type, and
6010    each function type node created) the g++ front end generates a
6011    _named_ TYPE_DECL node for each tagged type node created.
6012    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
6013    generate a DW_TAG_typedef DIE for them.  */
6014
6015 #define TYPE_DECL_IS_STUB(decl)                         \
6016   (DECL_NAME (decl) == NULL_TREE                        \
6017    || (DECL_ARTIFICIAL (decl)                           \
6018        && is_tagged_type (TREE_TYPE (decl))             \
6019        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
6020            /* This is necessary for stub decls that     \
6021               appear in nested inline functions.  */    \
6022            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6023                && (decl_ultimate_origin (decl)          \
6024                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6025
6026 /* Information concerning the compilation unit's programming
6027    language, and compiler version.  */
6028
6029 /* Fixed size portion of the DWARF compilation unit header.  */
6030 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6031   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6032
6033 /* Fixed size portion of the DWARF comdat type unit header.  */
6034 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6035   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6036    + DWARF_OFFSET_SIZE)
6037
6038 /* Fixed size portion of public names info.  */
6039 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6040
6041 /* Fixed size portion of the address range info.  */
6042 #define DWARF_ARANGES_HEADER_SIZE                                       \
6043   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6044                 DWARF2_ADDR_SIZE * 2)                                   \
6045    - DWARF_INITIAL_LENGTH_SIZE)
6046
6047 /* Size of padding portion in the address range info.  It must be
6048    aligned to twice the pointer size.  */
6049 #define DWARF_ARANGES_PAD_SIZE \
6050   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6051                 DWARF2_ADDR_SIZE * 2)                              \
6052    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6053
6054 /* Use assembler line directives if available.  */
6055 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6056 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6057 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6058 #else
6059 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6060 #endif
6061 #endif
6062
6063 /* Minimum line offset in a special line info. opcode.
6064    This value was chosen to give a reasonable range of values.  */
6065 #define DWARF_LINE_BASE  -10
6066
6067 /* First special line opcode - leave room for the standard opcodes.  */
6068 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
6069
6070 /* Range of line offsets in a special line info. opcode.  */
6071 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6072
6073 /* Flag that indicates the initial value of the is_stmt_start flag.
6074    In the present implementation, we do not mark any lines as
6075    the beginning of a source statement, because that information
6076    is not made available by the GCC front-end.  */
6077 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6078
6079 /* Maximum number of operations per instruction bundle.  */
6080 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6081 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6082 #endif
6083
6084 /* This location is used by calc_die_sizes() to keep track
6085    the offset of each DIE within the .debug_info section.  */
6086 static unsigned long next_die_offset;
6087
6088 /* Record the root of the DIE's built for the current compilation unit.  */
6089 static GTY(()) dw_die_ref single_comp_unit_die;
6090
6091 /* A list of type DIEs that have been separated into comdat sections.  */
6092 static GTY(()) comdat_type_node *comdat_type_list;
6093
6094 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6095 static GTY(()) limbo_die_node *limbo_die_list;
6096
6097 /* A list of DIEs for which we may have to generate
6098    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6099 static GTY(()) limbo_die_node *deferred_asm_name;
6100
6101 /* Filenames referenced by this compilation unit.  */
6102 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6103
6104 /* A hash table of references to DIE's that describe declarations.
6105    The key is a DECL_UID() which is a unique number identifying each decl.  */
6106 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6107
6108 /* A hash table of references to DIE's that describe COMMON blocks.
6109    The key is DECL_UID() ^ die_parent.  */
6110 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6111
6112 typedef struct GTY(()) die_arg_entry_struct {
6113     dw_die_ref die;
6114     tree arg;
6115 } die_arg_entry;
6116
6117 DEF_VEC_O(die_arg_entry);
6118 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6119
6120 /* Node of the variable location list.  */
6121 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6122   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6123      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6124      in mode of the EXPR_LIST node and first EXPR_LIST operand
6125      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6126      location or NULL for padding.  For larger bitsizes,
6127      mode is 0 and first operand is a CONCAT with bitsize
6128      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6129      NULL as second operand.  */
6130   rtx GTY (()) loc;
6131   const char * GTY (()) label;
6132   struct var_loc_node * GTY (()) next;
6133 };
6134
6135 /* Variable location list.  */
6136 struct GTY (()) var_loc_list_def {
6137   struct var_loc_node * GTY (()) first;
6138
6139   /* Pointer to the last but one or last element of the
6140      chained list.  If the list is empty, both first and
6141      last are NULL, if the list contains just one node
6142      or the last node certainly is not redundant, it points
6143      to the last node, otherwise points to the last but one.
6144      Do not mark it for GC because it is marked through the chain.  */
6145   struct var_loc_node * GTY ((skip ("%h"))) last;
6146
6147   /* Pointer to the last element before section switch,
6148      if NULL, either sections weren't switched or first
6149      is after section switch.  */
6150   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6151
6152   /* DECL_UID of the variable decl.  */
6153   unsigned int decl_id;
6154 };
6155 typedef struct var_loc_list_def var_loc_list;
6156
6157 /* Call argument location list.  */
6158 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6159   rtx GTY (()) call_arg_loc_note;
6160   const char * GTY (()) label;
6161   tree GTY (()) block;
6162   bool tail_call_p;
6163   rtx GTY (()) symbol_ref;
6164   struct call_arg_loc_node * GTY (()) next;
6165 };
6166
6167
6168 /* Table of decl location linked lists.  */
6169 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6170
6171 /* Head and tail of call_arg_loc chain.  */
6172 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6173 static struct call_arg_loc_node *call_arg_loc_last;
6174
6175 /* Number of call sites in the current function.  */
6176 static int call_site_count = -1;
6177 /* Number of tail call sites in the current function.  */
6178 static int tail_call_site_count = -1;
6179
6180 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6181    DIEs.  */
6182 static VEC (dw_die_ref, heap) *block_map;
6183
6184 /* A cached location list.  */
6185 struct GTY (()) cached_dw_loc_list_def {
6186   /* The DECL_UID of the decl that this entry describes.  */
6187   unsigned int decl_id;
6188
6189   /* The cached location list.  */
6190   dw_loc_list_ref loc_list;
6191 };
6192 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6193
6194 /* Table of cached location lists.  */
6195 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6196
6197 /* A pointer to the base of a list of references to DIE's that
6198    are uniquely identified by their tag, presence/absence of
6199    children DIE's, and list of attribute/value pairs.  */
6200 static GTY((length ("abbrev_die_table_allocated")))
6201   dw_die_ref *abbrev_die_table;
6202
6203 /* Number of elements currently allocated for abbrev_die_table.  */
6204 static GTY(()) unsigned abbrev_die_table_allocated;
6205
6206 /* Number of elements in type_die_table currently in use.  */
6207 static GTY(()) unsigned abbrev_die_table_in_use;
6208
6209 /* Size (in elements) of increments by which we may expand the
6210    abbrev_die_table.  */
6211 #define ABBREV_DIE_TABLE_INCREMENT 256
6212
6213 /* A global counter for generating labels for line number data.  */
6214 static unsigned int line_info_label_num;
6215
6216 /* The current table to which we should emit line number information
6217    for the current function.  This will be set up at the beginning of
6218    assembly for the function.  */
6219 static dw_line_info_table *cur_line_info_table;
6220
6221 /* The two default tables of line number info.  */
6222 static GTY(()) dw_line_info_table *text_section_line_info;
6223 static GTY(()) dw_line_info_table *cold_text_section_line_info;
6224
6225 /* The set of all non-default tables of line number info.  */
6226 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
6227
6228 /* A flag to tell pubnames/types export if there is an info section to
6229    refer to.  */
6230 static bool info_section_emitted;
6231
6232 /* A pointer to the base of a table that contains a list of publicly
6233    accessible names.  */
6234 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6235
6236 /* A pointer to the base of a table that contains a list of publicly
6237    accessible types.  */
6238 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6239
6240 /* A pointer to the base of a table that contains a list of macro
6241    defines/undefines (and file start/end markers).  */
6242 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6243
6244 /* Array of dies for which we should generate .debug_ranges info.  */
6245 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6246
6247 /* Number of elements currently allocated for ranges_table.  */
6248 static GTY(()) unsigned ranges_table_allocated;
6249
6250 /* Number of elements in ranges_table currently in use.  */
6251 static GTY(()) unsigned ranges_table_in_use;
6252
6253 /* Array of pairs of labels referenced in ranges_table.  */
6254 static GTY ((length ("ranges_by_label_allocated")))
6255      dw_ranges_by_label_ref ranges_by_label;
6256
6257 /* Number of elements currently allocated for ranges_by_label.  */
6258 static GTY(()) unsigned ranges_by_label_allocated;
6259
6260 /* Number of elements in ranges_by_label currently in use.  */
6261 static GTY(()) unsigned ranges_by_label_in_use;
6262
6263 /* Size (in elements) of increments by which we may expand the
6264    ranges_table.  */
6265 #define RANGES_TABLE_INCREMENT 64
6266
6267 /* Whether we have location lists that need outputting */
6268 static GTY(()) bool have_location_lists;
6269
6270 /* Unique label counter.  */
6271 static GTY(()) unsigned int loclabel_num;
6272
6273 /* Unique label counter for point-of-call tables.  */
6274 static GTY(()) unsigned int poc_label_num;
6275
6276 /* Record whether the function being analyzed contains inlined functions.  */
6277 static int current_function_has_inlines;
6278
6279 /* The last file entry emitted by maybe_emit_file().  */
6280 static GTY(()) struct dwarf_file_data * last_emitted_file;
6281
6282 /* Number of internal labels generated by gen_internal_sym().  */
6283 static GTY(()) int label_num;
6284
6285 /* Cached result of previous call to lookup_filename.  */
6286 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6287
6288 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6289
6290 /* Instances of generic types for which we need to generate debug
6291    info that describe their generic parameters and arguments. That
6292    generation needs to happen once all types are properly laid out so
6293    we do it at the end of compilation.  */
6294 static GTY(()) VEC(tree,gc) *generic_type_instances;
6295
6296 /* Offset from the "steady-state frame pointer" to the frame base,
6297    within the current function.  */
6298 static HOST_WIDE_INT frame_pointer_fb_offset;
6299
6300 /* Forward declarations for functions defined in this file.  */
6301
6302 static int is_pseudo_reg (const_rtx);
6303 static tree type_main_variant (tree);
6304 static int is_tagged_type (const_tree);
6305 static const char *dwarf_tag_name (unsigned);
6306 static const char *dwarf_attr_name (unsigned);
6307 static const char *dwarf_form_name (unsigned);
6308 static tree decl_ultimate_origin (const_tree);
6309 static tree decl_class_context (tree);
6310 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6311 static inline enum dw_val_class AT_class (dw_attr_ref);
6312 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6313 static inline unsigned AT_flag (dw_attr_ref);
6314 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6315 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6316 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6317 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6318 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6319                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6320 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6321                                unsigned int, unsigned char *);
6322 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6323 static hashval_t debug_str_do_hash (const void *);
6324 static int debug_str_eq (const void *, const void *);
6325 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6326 static inline const char *AT_string (dw_attr_ref);
6327 static enum dwarf_form AT_string_form (dw_attr_ref);
6328 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6329 static void add_AT_specification (dw_die_ref, dw_die_ref);
6330 static inline dw_die_ref AT_ref (dw_attr_ref);
6331 static inline int AT_ref_external (dw_attr_ref);
6332 static inline void set_AT_ref_external (dw_attr_ref, int);
6333 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6334 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6335 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6336 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6337                              dw_loc_list_ref);
6338 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6339 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6340 static inline rtx AT_addr (dw_attr_ref);
6341 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6342 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6343 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6344 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6345                            unsigned HOST_WIDE_INT);
6346 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6347                                unsigned long);
6348 static inline const char *AT_lbl (dw_attr_ref);
6349 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6350 static const char *get_AT_low_pc (dw_die_ref);
6351 static const char *get_AT_hi_pc (dw_die_ref);
6352 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6353 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6354 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6355 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6356 static bool is_cxx (void);
6357 static bool is_fortran (void);
6358 static bool is_ada (void);
6359 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6360 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6361 static void add_child_die (dw_die_ref, dw_die_ref);
6362 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6363 static dw_die_ref lookup_type_die (tree);
6364 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6365 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6366 static void equate_type_number_to_die (tree, dw_die_ref);
6367 static hashval_t decl_die_table_hash (const void *);
6368 static int decl_die_table_eq (const void *, const void *);
6369 static dw_die_ref lookup_decl_die (tree);
6370 static hashval_t common_block_die_table_hash (const void *);
6371 static int common_block_die_table_eq (const void *, const void *);
6372 static hashval_t decl_loc_table_hash (const void *);
6373 static int decl_loc_table_eq (const void *, const void *);
6374 static var_loc_list *lookup_decl_loc (const_tree);
6375 static void equate_decl_number_to_die (tree, dw_die_ref);
6376 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6377 static void print_spaces (FILE *);
6378 static void print_die (dw_die_ref, FILE *);
6379 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6380 static dw_die_ref pop_compile_unit (dw_die_ref);
6381 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6382 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6383 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6384 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6385 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6386 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6387 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6388                                    struct md5_ctx *, int *);
6389 struct checksum_attributes;
6390 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6391 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6392 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6393 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6394 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6395 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6396 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6397 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6398 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6399 static void compute_section_prefix (dw_die_ref);
6400 static int is_type_die (dw_die_ref);
6401 static int is_comdat_die (dw_die_ref);
6402 static int is_symbol_die (dw_die_ref);
6403 static void assign_symbol_names (dw_die_ref);
6404 static void break_out_includes (dw_die_ref);
6405 static int is_declaration_die (dw_die_ref);
6406 static int should_move_die_to_comdat (dw_die_ref);
6407 static dw_die_ref clone_as_declaration (dw_die_ref);
6408 static dw_die_ref clone_die (dw_die_ref);
6409 static dw_die_ref clone_tree (dw_die_ref);
6410 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6411 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6412 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6413 static dw_die_ref generate_skeleton (dw_die_ref);
6414 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6415                                                          dw_die_ref);
6416 static void break_out_comdat_types (dw_die_ref);
6417 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6418 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6419 static void copy_decls_for_unworthy_types (dw_die_ref);
6420
6421 static hashval_t htab_cu_hash (const void *);
6422 static int htab_cu_eq (const void *, const void *);
6423 static void htab_cu_del (void *);
6424 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6425 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6426 static void add_sibling_attributes (dw_die_ref);
6427 static void build_abbrev_table (dw_die_ref);
6428 static void output_location_lists (dw_die_ref);
6429 static int constant_size (unsigned HOST_WIDE_INT);
6430 static unsigned long size_of_die (dw_die_ref);
6431 static void calc_die_sizes (dw_die_ref);
6432 static void mark_dies (dw_die_ref);
6433 static void unmark_dies (dw_die_ref);
6434 static void unmark_all_dies (dw_die_ref);
6435 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6436 static unsigned long size_of_aranges (void);
6437 static enum dwarf_form value_format (dw_attr_ref);
6438 static void output_value_format (dw_attr_ref);
6439 static void output_abbrev_section (void);
6440 static void output_die_symbol (dw_die_ref);
6441 static void output_die (dw_die_ref);
6442 static void output_compilation_unit_header (void);
6443 static void output_comp_unit (dw_die_ref, int);
6444 static void output_comdat_type_unit (comdat_type_node *);
6445 static const char *dwarf2_name (tree, int);
6446 static void add_pubname (tree, dw_die_ref);
6447 static void add_pubname_string (const char *, dw_die_ref);
6448 static void add_pubtype (tree, dw_die_ref);
6449 static void output_pubnames (VEC (pubname_entry,gc) *);
6450 static void output_aranges (unsigned long);
6451 static unsigned int add_ranges_num (int);
6452 static unsigned int add_ranges (const_tree);
6453 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6454                                   bool *);
6455 static void output_ranges (void);
6456 static dw_line_info_table *new_line_info_table (void);
6457 static void output_line_info (void);
6458 static void output_file_names (void);
6459 static dw_die_ref base_type_die (tree);
6460 static int is_base_type (tree);
6461 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6462 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6463 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6464 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6465 static int type_is_enum (const_tree);
6466 static unsigned int dbx_reg_number (const_rtx);
6467 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6468 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6469 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6470                                                 enum var_init_status);
6471 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6472                                                      enum var_init_status);
6473 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6474                                          enum var_init_status);
6475 static int is_based_loc (const_rtx);
6476 static int resolve_one_addr (rtx *, void *);
6477 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6478                                                enum var_init_status);
6479 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6480                                         enum var_init_status);
6481 static dw_loc_list_ref loc_list_from_tree (tree, int);
6482 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6483 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6484 static tree field_type (const_tree);
6485 static unsigned int simple_type_align_in_bits (const_tree);
6486 static unsigned int simple_decl_align_in_bits (const_tree);
6487 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6488 static HOST_WIDE_INT field_byte_offset (const_tree);
6489 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6490                                          dw_loc_list_ref);
6491 static void add_data_member_location_attribute (dw_die_ref, tree);
6492 static bool add_const_value_attribute (dw_die_ref, rtx);
6493 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6494 static void insert_double (double_int, unsigned char *);
6495 static void insert_float (const_rtx, unsigned char *);
6496 static rtx rtl_for_decl_location (tree);
6497 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6498                                                    enum dwarf_attribute);
6499 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6500 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6501 static void add_name_attribute (dw_die_ref, const char *);
6502 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
6503 static void add_comp_dir_attribute (dw_die_ref);
6504 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6505 static void add_subscript_info (dw_die_ref, tree, bool);
6506 static void add_byte_size_attribute (dw_die_ref, tree);
6507 static void add_bit_offset_attribute (dw_die_ref, tree);
6508 static void add_bit_size_attribute (dw_die_ref, tree);
6509 static void add_prototyped_attribute (dw_die_ref, tree);
6510 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6511 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6512 static void add_src_coords_attributes (dw_die_ref, tree);
6513 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6514 static void push_decl_scope (tree);
6515 static void pop_decl_scope (void);
6516 static dw_die_ref scope_die_for (tree, dw_die_ref);
6517 static inline int local_scope_p (dw_die_ref);
6518 static inline int class_scope_p (dw_die_ref);
6519 static inline int class_or_namespace_scope_p (dw_die_ref);
6520 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6521 static void add_calling_convention_attribute (dw_die_ref, tree);
6522 static const char *type_tag (const_tree);
6523 static tree member_declared_type (const_tree);
6524 #if 0
6525 static const char *decl_start_label (tree);
6526 #endif
6527 static void gen_array_type_die (tree, dw_die_ref);
6528 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6529 #if 0
6530 static void gen_entry_point_die (tree, dw_die_ref);
6531 #endif
6532 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6533 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6534 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6535 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6536 static void gen_formal_types_die (tree, dw_die_ref);
6537 static void gen_subprogram_die (tree, dw_die_ref);
6538 static void gen_variable_die (tree, tree, dw_die_ref);
6539 static void gen_const_die (tree, dw_die_ref);
6540 static void gen_label_die (tree, dw_die_ref);
6541 static void gen_lexical_block_die (tree, dw_die_ref, int);
6542 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6543 static void gen_field_die (tree, dw_die_ref);
6544 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6545 static dw_die_ref gen_compile_unit_die (const char *);
6546 static void gen_inheritance_die (tree, tree, dw_die_ref);
6547 static void gen_member_die (tree, dw_die_ref);
6548 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6549                                                 enum debug_info_usage);
6550 static void gen_subroutine_type_die (tree, dw_die_ref);
6551 static void gen_typedef_die (tree, dw_die_ref);
6552 static void gen_type_die (tree, dw_die_ref);
6553 static void gen_block_die (tree, dw_die_ref, int);
6554 static void decls_for_scope (tree, dw_die_ref, int);
6555 static int is_redundant_typedef (const_tree);
6556 static bool is_naming_typedef_decl (const_tree);
6557 static inline dw_die_ref get_context_die (tree);
6558 static void gen_namespace_die (tree, dw_die_ref);
6559 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6560 static dw_die_ref force_decl_die (tree);
6561 static dw_die_ref force_type_die (tree);
6562 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6563 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6564 static struct dwarf_file_data * lookup_filename (const char *);
6565 static void retry_incomplete_types (void);
6566 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6567 static void gen_generic_params_dies (tree);
6568 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6569 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6570 static void splice_child_die (dw_die_ref, dw_die_ref);
6571 static int file_info_cmp (const void *, const void *);
6572 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6573                                      const char *, const char *);
6574 static void output_loc_list (dw_loc_list_ref);
6575 static char *gen_internal_sym (const char *);
6576
6577 static void prune_unmark_dies (dw_die_ref);
6578 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6579 static void prune_unused_types_mark (dw_die_ref, int);
6580 static void prune_unused_types_walk (dw_die_ref);
6581 static void prune_unused_types_walk_attribs (dw_die_ref);
6582 static void prune_unused_types_prune (dw_die_ref);
6583 static void prune_unused_types (void);
6584 static int maybe_emit_file (struct dwarf_file_data *fd);
6585 static inline const char *AT_vms_delta1 (dw_attr_ref);
6586 static inline const char *AT_vms_delta2 (dw_attr_ref);
6587 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6588                                      const char *, const char *);
6589 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6590 static void gen_remaining_tmpl_value_param_die_attribute (void);
6591 static bool generic_type_p (tree);
6592 static void schedule_generic_params_dies_gen (tree t);
6593 static void gen_scheduled_generic_parms_dies (void);
6594
6595 /* Section names used to hold DWARF debugging information.  */
6596 #ifndef DEBUG_INFO_SECTION
6597 #define DEBUG_INFO_SECTION      ".debug_info"
6598 #endif
6599 #ifndef DEBUG_ABBREV_SECTION
6600 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6601 #endif
6602 #ifndef DEBUG_ARANGES_SECTION
6603 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6604 #endif
6605 #ifndef DEBUG_MACINFO_SECTION
6606 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6607 #endif
6608 #ifndef DEBUG_LINE_SECTION
6609 #define DEBUG_LINE_SECTION      ".debug_line"
6610 #endif
6611 #ifndef DEBUG_LOC_SECTION
6612 #define DEBUG_LOC_SECTION       ".debug_loc"
6613 #endif
6614 #ifndef DEBUG_PUBNAMES_SECTION
6615 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6616 #endif
6617 #ifndef DEBUG_PUBTYPES_SECTION
6618 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6619 #endif
6620 #ifndef DEBUG_STR_SECTION
6621 #define DEBUG_STR_SECTION       ".debug_str"
6622 #endif
6623 #ifndef DEBUG_RANGES_SECTION
6624 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6625 #endif
6626
6627 /* Standard ELF section names for compiled code and data.  */
6628 #ifndef TEXT_SECTION_NAME
6629 #define TEXT_SECTION_NAME       ".text"
6630 #endif
6631
6632 /* Section flags for .debug_str section.  */
6633 #define DEBUG_STR_SECTION_FLAGS \
6634   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6635    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6636    : SECTION_DEBUG)
6637
6638 /* Labels we insert at beginning sections we can reference instead of
6639    the section names themselves.  */
6640
6641 #ifndef TEXT_SECTION_LABEL
6642 #define TEXT_SECTION_LABEL              "Ltext"
6643 #endif
6644 #ifndef COLD_TEXT_SECTION_LABEL
6645 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6646 #endif
6647 #ifndef DEBUG_LINE_SECTION_LABEL
6648 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6649 #endif
6650 #ifndef DEBUG_INFO_SECTION_LABEL
6651 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6652 #endif
6653 #ifndef DEBUG_ABBREV_SECTION_LABEL
6654 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6655 #endif
6656 #ifndef DEBUG_LOC_SECTION_LABEL
6657 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6658 #endif
6659 #ifndef DEBUG_RANGES_SECTION_LABEL
6660 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6661 #endif
6662 #ifndef DEBUG_MACINFO_SECTION_LABEL
6663 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6664 #endif
6665
6666
6667 /* Definitions of defaults for formats and names of various special
6668    (artificial) labels which may be generated within this file (when the -g
6669    options is used and DWARF2_DEBUGGING_INFO is in effect.
6670    If necessary, these may be overridden from within the tm.h file, but
6671    typically, overriding these defaults is unnecessary.  */
6672
6673 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6674 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6675 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6676 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6677 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6678 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6679 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6680 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6681 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6682 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6683
6684 #ifndef TEXT_END_LABEL
6685 #define TEXT_END_LABEL          "Letext"
6686 #endif
6687 #ifndef COLD_END_LABEL
6688 #define COLD_END_LABEL          "Letext_cold"
6689 #endif
6690 #ifndef BLOCK_BEGIN_LABEL
6691 #define BLOCK_BEGIN_LABEL       "LBB"
6692 #endif
6693 #ifndef BLOCK_END_LABEL
6694 #define BLOCK_END_LABEL         "LBE"
6695 #endif
6696 #ifndef LINE_CODE_LABEL
6697 #define LINE_CODE_LABEL         "LM"
6698 #endif
6699
6700 \f
6701 /* Return the root of the DIE's built for the current compilation unit.  */
6702 static dw_die_ref
6703 comp_unit_die (void)
6704 {
6705   if (!single_comp_unit_die)
6706     single_comp_unit_die = gen_compile_unit_die (NULL);
6707   return single_comp_unit_die;
6708 }
6709
6710 /* We allow a language front-end to designate a function that is to be
6711    called to "demangle" any name before it is put into a DIE.  */
6712
6713 static const char *(*demangle_name_func) (const char *);
6714
6715 void
6716 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6717 {
6718   demangle_name_func = func;
6719 }
6720
6721 /* Test if rtl node points to a pseudo register.  */
6722
6723 static inline int
6724 is_pseudo_reg (const_rtx rtl)
6725 {
6726   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6727           || (GET_CODE (rtl) == SUBREG
6728               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6729 }
6730
6731 /* Return a reference to a type, with its const and volatile qualifiers
6732    removed.  */
6733
6734 static inline tree
6735 type_main_variant (tree type)
6736 {
6737   type = TYPE_MAIN_VARIANT (type);
6738
6739   /* ??? There really should be only one main variant among any group of
6740      variants of a given type (and all of the MAIN_VARIANT values for all
6741      members of the group should point to that one type) but sometimes the C
6742      front-end messes this up for array types, so we work around that bug
6743      here.  */
6744   if (TREE_CODE (type) == ARRAY_TYPE)
6745     while (type != TYPE_MAIN_VARIANT (type))
6746       type = TYPE_MAIN_VARIANT (type);
6747
6748   return type;
6749 }
6750
6751 /* Return nonzero if the given type node represents a tagged type.  */
6752
6753 static inline int
6754 is_tagged_type (const_tree type)
6755 {
6756   enum tree_code code = TREE_CODE (type);
6757
6758   return (code == RECORD_TYPE || code == UNION_TYPE
6759           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6760 }
6761
6762 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6763
6764 static void
6765 get_ref_die_offset_label (char *label, dw_die_ref ref)
6766 {
6767   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6768 }
6769
6770 /* Convert a DIE tag into its string name.  */
6771
6772 static const char *
6773 dwarf_tag_name (unsigned int tag)
6774 {
6775   switch (tag)
6776     {
6777     case DW_TAG_padding:
6778       return "DW_TAG_padding";
6779     case DW_TAG_array_type:
6780       return "DW_TAG_array_type";
6781     case DW_TAG_class_type:
6782       return "DW_TAG_class_type";
6783     case DW_TAG_entry_point:
6784       return "DW_TAG_entry_point";
6785     case DW_TAG_enumeration_type:
6786       return "DW_TAG_enumeration_type";
6787     case DW_TAG_formal_parameter:
6788       return "DW_TAG_formal_parameter";
6789     case DW_TAG_imported_declaration:
6790       return "DW_TAG_imported_declaration";
6791     case DW_TAG_label:
6792       return "DW_TAG_label";
6793     case DW_TAG_lexical_block:
6794       return "DW_TAG_lexical_block";
6795     case DW_TAG_member:
6796       return "DW_TAG_member";
6797     case DW_TAG_pointer_type:
6798       return "DW_TAG_pointer_type";
6799     case DW_TAG_reference_type:
6800       return "DW_TAG_reference_type";
6801     case DW_TAG_compile_unit:
6802       return "DW_TAG_compile_unit";
6803     case DW_TAG_string_type:
6804       return "DW_TAG_string_type";
6805     case DW_TAG_structure_type:
6806       return "DW_TAG_structure_type";
6807     case DW_TAG_subroutine_type:
6808       return "DW_TAG_subroutine_type";
6809     case DW_TAG_typedef:
6810       return "DW_TAG_typedef";
6811     case DW_TAG_union_type:
6812       return "DW_TAG_union_type";
6813     case DW_TAG_unspecified_parameters:
6814       return "DW_TAG_unspecified_parameters";
6815     case DW_TAG_variant:
6816       return "DW_TAG_variant";
6817     case DW_TAG_common_block:
6818       return "DW_TAG_common_block";
6819     case DW_TAG_common_inclusion:
6820       return "DW_TAG_common_inclusion";
6821     case DW_TAG_inheritance:
6822       return "DW_TAG_inheritance";
6823     case DW_TAG_inlined_subroutine:
6824       return "DW_TAG_inlined_subroutine";
6825     case DW_TAG_module:
6826       return "DW_TAG_module";
6827     case DW_TAG_ptr_to_member_type:
6828       return "DW_TAG_ptr_to_member_type";
6829     case DW_TAG_set_type:
6830       return "DW_TAG_set_type";
6831     case DW_TAG_subrange_type:
6832       return "DW_TAG_subrange_type";
6833     case DW_TAG_with_stmt:
6834       return "DW_TAG_with_stmt";
6835     case DW_TAG_access_declaration:
6836       return "DW_TAG_access_declaration";
6837     case DW_TAG_base_type:
6838       return "DW_TAG_base_type";
6839     case DW_TAG_catch_block:
6840       return "DW_TAG_catch_block";
6841     case DW_TAG_const_type:
6842       return "DW_TAG_const_type";
6843     case DW_TAG_constant:
6844       return "DW_TAG_constant";
6845     case DW_TAG_enumerator:
6846       return "DW_TAG_enumerator";
6847     case DW_TAG_file_type:
6848       return "DW_TAG_file_type";
6849     case DW_TAG_friend:
6850       return "DW_TAG_friend";
6851     case DW_TAG_namelist:
6852       return "DW_TAG_namelist";
6853     case DW_TAG_namelist_item:
6854       return "DW_TAG_namelist_item";
6855     case DW_TAG_packed_type:
6856       return "DW_TAG_packed_type";
6857     case DW_TAG_subprogram:
6858       return "DW_TAG_subprogram";
6859     case DW_TAG_template_type_param:
6860       return "DW_TAG_template_type_param";
6861     case DW_TAG_template_value_param:
6862       return "DW_TAG_template_value_param";
6863     case DW_TAG_thrown_type:
6864       return "DW_TAG_thrown_type";
6865     case DW_TAG_try_block:
6866       return "DW_TAG_try_block";
6867     case DW_TAG_variant_part:
6868       return "DW_TAG_variant_part";
6869     case DW_TAG_variable:
6870       return "DW_TAG_variable";
6871     case DW_TAG_volatile_type:
6872       return "DW_TAG_volatile_type";
6873     case DW_TAG_dwarf_procedure:
6874       return "DW_TAG_dwarf_procedure";
6875     case DW_TAG_restrict_type:
6876       return "DW_TAG_restrict_type";
6877     case DW_TAG_interface_type:
6878       return "DW_TAG_interface_type";
6879     case DW_TAG_namespace:
6880       return "DW_TAG_namespace";
6881     case DW_TAG_imported_module:
6882       return "DW_TAG_imported_module";
6883     case DW_TAG_unspecified_type:
6884       return "DW_TAG_unspecified_type";
6885     case DW_TAG_partial_unit:
6886       return "DW_TAG_partial_unit";
6887     case DW_TAG_imported_unit:
6888       return "DW_TAG_imported_unit";
6889     case DW_TAG_condition:
6890       return "DW_TAG_condition";
6891     case DW_TAG_shared_type:
6892       return "DW_TAG_shared_type";
6893     case DW_TAG_type_unit:
6894       return "DW_TAG_type_unit";
6895     case DW_TAG_rvalue_reference_type:
6896       return "DW_TAG_rvalue_reference_type";
6897     case DW_TAG_template_alias:
6898       return "DW_TAG_template_alias";
6899     case DW_TAG_GNU_template_parameter_pack:
6900       return "DW_TAG_GNU_template_parameter_pack";
6901     case DW_TAG_GNU_formal_parameter_pack:
6902       return "DW_TAG_GNU_formal_parameter_pack";
6903     case DW_TAG_MIPS_loop:
6904       return "DW_TAG_MIPS_loop";
6905     case DW_TAG_format_label:
6906       return "DW_TAG_format_label";
6907     case DW_TAG_function_template:
6908       return "DW_TAG_function_template";
6909     case DW_TAG_class_template:
6910       return "DW_TAG_class_template";
6911     case DW_TAG_GNU_BINCL:
6912       return "DW_TAG_GNU_BINCL";
6913     case DW_TAG_GNU_EINCL:
6914       return "DW_TAG_GNU_EINCL";
6915     case DW_TAG_GNU_template_template_param:
6916       return "DW_TAG_GNU_template_template_param";
6917     case DW_TAG_GNU_call_site:
6918       return "DW_TAG_GNU_call_site";
6919     case DW_TAG_GNU_call_site_parameter:
6920       return "DW_TAG_GNU_call_site_parameter";
6921     default:
6922       return "DW_TAG_<unknown>";
6923     }
6924 }
6925
6926 /* Convert a DWARF attribute code into its string name.  */
6927
6928 static const char *
6929 dwarf_attr_name (unsigned int attr)
6930 {
6931   switch (attr)
6932     {
6933     case DW_AT_sibling:
6934       return "DW_AT_sibling";
6935     case DW_AT_location:
6936       return "DW_AT_location";
6937     case DW_AT_name:
6938       return "DW_AT_name";
6939     case DW_AT_ordering:
6940       return "DW_AT_ordering";
6941     case DW_AT_subscr_data:
6942       return "DW_AT_subscr_data";
6943     case DW_AT_byte_size:
6944       return "DW_AT_byte_size";
6945     case DW_AT_bit_offset:
6946       return "DW_AT_bit_offset";
6947     case DW_AT_bit_size:
6948       return "DW_AT_bit_size";
6949     case DW_AT_element_list:
6950       return "DW_AT_element_list";
6951     case DW_AT_stmt_list:
6952       return "DW_AT_stmt_list";
6953     case DW_AT_low_pc:
6954       return "DW_AT_low_pc";
6955     case DW_AT_high_pc:
6956       return "DW_AT_high_pc";
6957     case DW_AT_language:
6958       return "DW_AT_language";
6959     case DW_AT_member:
6960       return "DW_AT_member";
6961     case DW_AT_discr:
6962       return "DW_AT_discr";
6963     case DW_AT_discr_value:
6964       return "DW_AT_discr_value";
6965     case DW_AT_visibility:
6966       return "DW_AT_visibility";
6967     case DW_AT_import:
6968       return "DW_AT_import";
6969     case DW_AT_string_length:
6970       return "DW_AT_string_length";
6971     case DW_AT_common_reference:
6972       return "DW_AT_common_reference";
6973     case DW_AT_comp_dir:
6974       return "DW_AT_comp_dir";
6975     case DW_AT_const_value:
6976       return "DW_AT_const_value";
6977     case DW_AT_containing_type:
6978       return "DW_AT_containing_type";
6979     case DW_AT_default_value:
6980       return "DW_AT_default_value";
6981     case DW_AT_inline:
6982       return "DW_AT_inline";
6983     case DW_AT_is_optional:
6984       return "DW_AT_is_optional";
6985     case DW_AT_lower_bound:
6986       return "DW_AT_lower_bound";
6987     case DW_AT_producer:
6988       return "DW_AT_producer";
6989     case DW_AT_prototyped:
6990       return "DW_AT_prototyped";
6991     case DW_AT_return_addr:
6992       return "DW_AT_return_addr";
6993     case DW_AT_start_scope:
6994       return "DW_AT_start_scope";
6995     case DW_AT_bit_stride:
6996       return "DW_AT_bit_stride";
6997     case DW_AT_upper_bound:
6998       return "DW_AT_upper_bound";
6999     case DW_AT_abstract_origin:
7000       return "DW_AT_abstract_origin";
7001     case DW_AT_accessibility:
7002       return "DW_AT_accessibility";
7003     case DW_AT_address_class:
7004       return "DW_AT_address_class";
7005     case DW_AT_artificial:
7006       return "DW_AT_artificial";
7007     case DW_AT_base_types:
7008       return "DW_AT_base_types";
7009     case DW_AT_calling_convention:
7010       return "DW_AT_calling_convention";
7011     case DW_AT_count:
7012       return "DW_AT_count";
7013     case DW_AT_data_member_location:
7014       return "DW_AT_data_member_location";
7015     case DW_AT_decl_column:
7016       return "DW_AT_decl_column";
7017     case DW_AT_decl_file:
7018       return "DW_AT_decl_file";
7019     case DW_AT_decl_line:
7020       return "DW_AT_decl_line";
7021     case DW_AT_declaration:
7022       return "DW_AT_declaration";
7023     case DW_AT_discr_list:
7024       return "DW_AT_discr_list";
7025     case DW_AT_encoding:
7026       return "DW_AT_encoding";
7027     case DW_AT_external:
7028       return "DW_AT_external";
7029     case DW_AT_explicit:
7030       return "DW_AT_explicit";
7031     case DW_AT_frame_base:
7032       return "DW_AT_frame_base";
7033     case DW_AT_friend:
7034       return "DW_AT_friend";
7035     case DW_AT_identifier_case:
7036       return "DW_AT_identifier_case";
7037     case DW_AT_macro_info:
7038       return "DW_AT_macro_info";
7039     case DW_AT_namelist_items:
7040       return "DW_AT_namelist_items";
7041     case DW_AT_priority:
7042       return "DW_AT_priority";
7043     case DW_AT_segment:
7044       return "DW_AT_segment";
7045     case DW_AT_specification:
7046       return "DW_AT_specification";
7047     case DW_AT_static_link:
7048       return "DW_AT_static_link";
7049     case DW_AT_type:
7050       return "DW_AT_type";
7051     case DW_AT_use_location:
7052       return "DW_AT_use_location";
7053     case DW_AT_variable_parameter:
7054       return "DW_AT_variable_parameter";
7055     case DW_AT_virtuality:
7056       return "DW_AT_virtuality";
7057     case DW_AT_vtable_elem_location:
7058       return "DW_AT_vtable_elem_location";
7059
7060     case DW_AT_allocated:
7061       return "DW_AT_allocated";
7062     case DW_AT_associated:
7063       return "DW_AT_associated";
7064     case DW_AT_data_location:
7065       return "DW_AT_data_location";
7066     case DW_AT_byte_stride:
7067       return "DW_AT_byte_stride";
7068     case DW_AT_entry_pc:
7069       return "DW_AT_entry_pc";
7070     case DW_AT_use_UTF8:
7071       return "DW_AT_use_UTF8";
7072     case DW_AT_extension:
7073       return "DW_AT_extension";
7074     case DW_AT_ranges:
7075       return "DW_AT_ranges";
7076     case DW_AT_trampoline:
7077       return "DW_AT_trampoline";
7078     case DW_AT_call_column:
7079       return "DW_AT_call_column";
7080     case DW_AT_call_file:
7081       return "DW_AT_call_file";
7082     case DW_AT_call_line:
7083       return "DW_AT_call_line";
7084     case DW_AT_object_pointer:
7085       return "DW_AT_object_pointer";
7086
7087     case DW_AT_signature:
7088       return "DW_AT_signature";
7089     case DW_AT_main_subprogram:
7090       return "DW_AT_main_subprogram";
7091     case DW_AT_data_bit_offset:
7092       return "DW_AT_data_bit_offset";
7093     case DW_AT_const_expr:
7094       return "DW_AT_const_expr";
7095     case DW_AT_enum_class:
7096       return "DW_AT_enum_class";
7097     case DW_AT_linkage_name:
7098       return "DW_AT_linkage_name";
7099
7100     case DW_AT_MIPS_fde:
7101       return "DW_AT_MIPS_fde";
7102     case DW_AT_MIPS_loop_begin:
7103       return "DW_AT_MIPS_loop_begin";
7104     case DW_AT_MIPS_tail_loop_begin:
7105       return "DW_AT_MIPS_tail_loop_begin";
7106     case DW_AT_MIPS_epilog_begin:
7107       return "DW_AT_MIPS_epilog_begin";
7108 #if VMS_DEBUGGING_INFO
7109     case DW_AT_HP_prologue:
7110       return "DW_AT_HP_prologue";
7111 #else
7112     case DW_AT_MIPS_loop_unroll_factor:
7113       return "DW_AT_MIPS_loop_unroll_factor";
7114 #endif
7115     case DW_AT_MIPS_software_pipeline_depth:
7116       return "DW_AT_MIPS_software_pipeline_depth";
7117     case DW_AT_MIPS_linkage_name:
7118       return "DW_AT_MIPS_linkage_name";
7119 #if VMS_DEBUGGING_INFO
7120     case DW_AT_HP_epilogue:
7121       return "DW_AT_HP_epilogue";
7122 #else
7123     case DW_AT_MIPS_stride:
7124       return "DW_AT_MIPS_stride";
7125 #endif
7126     case DW_AT_MIPS_abstract_name:
7127       return "DW_AT_MIPS_abstract_name";
7128     case DW_AT_MIPS_clone_origin:
7129       return "DW_AT_MIPS_clone_origin";
7130     case DW_AT_MIPS_has_inlines:
7131       return "DW_AT_MIPS_has_inlines";
7132
7133     case DW_AT_sf_names:
7134       return "DW_AT_sf_names";
7135     case DW_AT_src_info:
7136       return "DW_AT_src_info";
7137     case DW_AT_mac_info:
7138       return "DW_AT_mac_info";
7139     case DW_AT_src_coords:
7140       return "DW_AT_src_coords";
7141     case DW_AT_body_begin:
7142       return "DW_AT_body_begin";
7143     case DW_AT_body_end:
7144       return "DW_AT_body_end";
7145
7146     case DW_AT_GNU_vector:
7147       return "DW_AT_GNU_vector";
7148     case DW_AT_GNU_guarded_by:
7149       return "DW_AT_GNU_guarded_by";
7150     case DW_AT_GNU_pt_guarded_by:
7151       return "DW_AT_GNU_pt_guarded_by";
7152     case DW_AT_GNU_guarded:
7153       return "DW_AT_GNU_guarded";
7154     case DW_AT_GNU_pt_guarded:
7155       return "DW_AT_GNU_pt_guarded";
7156     case DW_AT_GNU_locks_excluded:
7157       return "DW_AT_GNU_locks_excluded";
7158     case DW_AT_GNU_exclusive_locks_required:
7159       return "DW_AT_GNU_exclusive_locks_required";
7160     case DW_AT_GNU_shared_locks_required:
7161       return "DW_AT_GNU_shared_locks_required";
7162     case DW_AT_GNU_odr_signature:
7163       return "DW_AT_GNU_odr_signature";
7164     case DW_AT_GNU_template_name:
7165       return "DW_AT_GNU_template_name";
7166     case DW_AT_GNU_call_site_value:
7167       return "DW_AT_GNU_call_site_value";
7168     case DW_AT_GNU_call_site_data_value:
7169       return "DW_AT_GNU_call_site_data_value";
7170     case DW_AT_GNU_call_site_target:
7171       return "DW_AT_GNU_call_site_target";
7172     case DW_AT_GNU_call_site_target_clobbered:
7173       return "DW_AT_GNU_call_site_target_clobbered";
7174     case DW_AT_GNU_tail_call:
7175       return "DW_AT_GNU_tail_call";
7176     case DW_AT_GNU_all_tail_call_sites:
7177       return "DW_AT_GNU_all_tail_call_sites";
7178     case DW_AT_GNU_all_call_sites:
7179       return "DW_AT_GNU_all_call_sites";
7180     case DW_AT_GNU_all_source_call_sites:
7181       return "DW_AT_GNU_all_source_call_sites";
7182
7183     case DW_AT_GNAT_descriptive_type:
7184       return "DW_AT_GNAT_descriptive_type";
7185
7186     case DW_AT_VMS_rtnbeg_pd_address:
7187       return "DW_AT_VMS_rtnbeg_pd_address";
7188
7189     default:
7190       return "DW_AT_<unknown>";
7191     }
7192 }
7193
7194 /* Convert a DWARF value form code into its string name.  */
7195
7196 static const char *
7197 dwarf_form_name (unsigned int form)
7198 {
7199   switch (form)
7200     {
7201     case DW_FORM_addr:
7202       return "DW_FORM_addr";
7203     case DW_FORM_block2:
7204       return "DW_FORM_block2";
7205     case DW_FORM_block4:
7206       return "DW_FORM_block4";
7207     case DW_FORM_data2:
7208       return "DW_FORM_data2";
7209     case DW_FORM_data4:
7210       return "DW_FORM_data4";
7211     case DW_FORM_data8:
7212       return "DW_FORM_data8";
7213     case DW_FORM_string:
7214       return "DW_FORM_string";
7215     case DW_FORM_block:
7216       return "DW_FORM_block";
7217     case DW_FORM_block1:
7218       return "DW_FORM_block1";
7219     case DW_FORM_data1:
7220       return "DW_FORM_data1";
7221     case DW_FORM_flag:
7222       return "DW_FORM_flag";
7223     case DW_FORM_sdata:
7224       return "DW_FORM_sdata";
7225     case DW_FORM_strp:
7226       return "DW_FORM_strp";
7227     case DW_FORM_udata:
7228       return "DW_FORM_udata";
7229     case DW_FORM_ref_addr:
7230       return "DW_FORM_ref_addr";
7231     case DW_FORM_ref1:
7232       return "DW_FORM_ref1";
7233     case DW_FORM_ref2:
7234       return "DW_FORM_ref2";
7235     case DW_FORM_ref4:
7236       return "DW_FORM_ref4";
7237     case DW_FORM_ref8:
7238       return "DW_FORM_ref8";
7239     case DW_FORM_ref_udata:
7240       return "DW_FORM_ref_udata";
7241     case DW_FORM_indirect:
7242       return "DW_FORM_indirect";
7243     case DW_FORM_sec_offset:
7244       return "DW_FORM_sec_offset";
7245     case DW_FORM_exprloc:
7246       return "DW_FORM_exprloc";
7247     case DW_FORM_flag_present:
7248       return "DW_FORM_flag_present";
7249     case DW_FORM_ref_sig8:
7250       return "DW_FORM_ref_sig8";
7251     default:
7252       return "DW_FORM_<unknown>";
7253     }
7254 }
7255 \f
7256 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7257    instance of an inlined instance of a decl which is local to an inline
7258    function, so we have to trace all of the way back through the origin chain
7259    to find out what sort of node actually served as the original seed for the
7260    given block.  */
7261
7262 static tree
7263 decl_ultimate_origin (const_tree decl)
7264 {
7265   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7266     return NULL_TREE;
7267
7268   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7269      nodes in the function to point to themselves; ignore that if
7270      we're trying to output the abstract instance of this function.  */
7271   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7272     return NULL_TREE;
7273
7274   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7275      most distant ancestor, this should never happen.  */
7276   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7277
7278   return DECL_ABSTRACT_ORIGIN (decl);
7279 }
7280
7281 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7282    of a virtual function may refer to a base class, so we check the 'this'
7283    parameter.  */
7284
7285 static tree
7286 decl_class_context (tree decl)
7287 {
7288   tree context = NULL_TREE;
7289
7290   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7291     context = DECL_CONTEXT (decl);
7292   else
7293     context = TYPE_MAIN_VARIANT
7294       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7295
7296   if (context && !TYPE_P (context))
7297     context = NULL_TREE;
7298
7299   return context;
7300 }
7301 \f
7302 /* Add an attribute/value pair to a DIE.  */
7303
7304 static inline void
7305 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7306 {
7307   /* Maybe this should be an assert?  */
7308   if (die == NULL)
7309     return;
7310
7311   if (die->die_attr == NULL)
7312     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7313   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7314 }
7315
7316 static inline enum dw_val_class
7317 AT_class (dw_attr_ref a)
7318 {
7319   return a->dw_attr_val.val_class;
7320 }
7321
7322 /* Add a flag value attribute to a DIE.  */
7323
7324 static inline void
7325 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7326 {
7327   dw_attr_node attr;
7328
7329   attr.dw_attr = attr_kind;
7330   attr.dw_attr_val.val_class = dw_val_class_flag;
7331   attr.dw_attr_val.v.val_flag = flag;
7332   add_dwarf_attr (die, &attr);
7333 }
7334
7335 static inline unsigned
7336 AT_flag (dw_attr_ref a)
7337 {
7338   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7339   return a->dw_attr_val.v.val_flag;
7340 }
7341
7342 /* Add a signed integer attribute value to a DIE.  */
7343
7344 static inline void
7345 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7346 {
7347   dw_attr_node attr;
7348
7349   attr.dw_attr = attr_kind;
7350   attr.dw_attr_val.val_class = dw_val_class_const;
7351   attr.dw_attr_val.v.val_int = int_val;
7352   add_dwarf_attr (die, &attr);
7353 }
7354
7355 static inline HOST_WIDE_INT
7356 AT_int (dw_attr_ref a)
7357 {
7358   gcc_assert (a && AT_class (a) == dw_val_class_const);
7359   return a->dw_attr_val.v.val_int;
7360 }
7361
7362 /* Add an unsigned integer attribute value to a DIE.  */
7363
7364 static inline void
7365 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7366                  unsigned HOST_WIDE_INT unsigned_val)
7367 {
7368   dw_attr_node attr;
7369
7370   attr.dw_attr = attr_kind;
7371   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7372   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7373   add_dwarf_attr (die, &attr);
7374 }
7375
7376 static inline unsigned HOST_WIDE_INT
7377 AT_unsigned (dw_attr_ref a)
7378 {
7379   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7380   return a->dw_attr_val.v.val_unsigned;
7381 }
7382
7383 /* Add an unsigned double integer attribute value to a DIE.  */
7384
7385 static inline void
7386 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7387                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7388 {
7389   dw_attr_node attr;
7390
7391   attr.dw_attr = attr_kind;
7392   attr.dw_attr_val.val_class = dw_val_class_const_double;
7393   attr.dw_attr_val.v.val_double.high = high;
7394   attr.dw_attr_val.v.val_double.low = low;
7395   add_dwarf_attr (die, &attr);
7396 }
7397
7398 /* Add a floating point attribute value to a DIE and return it.  */
7399
7400 static inline void
7401 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7402             unsigned int length, unsigned int elt_size, unsigned char *array)
7403 {
7404   dw_attr_node attr;
7405
7406   attr.dw_attr = attr_kind;
7407   attr.dw_attr_val.val_class = dw_val_class_vec;
7408   attr.dw_attr_val.v.val_vec.length = length;
7409   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7410   attr.dw_attr_val.v.val_vec.array = array;
7411   add_dwarf_attr (die, &attr);
7412 }
7413
7414 /* Add an 8-byte data attribute value to a DIE.  */
7415
7416 static inline void
7417 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7418               unsigned char data8[8])
7419 {
7420   dw_attr_node attr;
7421
7422   attr.dw_attr = attr_kind;
7423   attr.dw_attr_val.val_class = dw_val_class_data8;
7424   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7425   add_dwarf_attr (die, &attr);
7426 }
7427
7428 /* Hash and equality functions for debug_str_hash.  */
7429
7430 static hashval_t
7431 debug_str_do_hash (const void *x)
7432 {
7433   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7434 }
7435
7436 static int
7437 debug_str_eq (const void *x1, const void *x2)
7438 {
7439   return strcmp ((((const struct indirect_string_node *)x1)->str),
7440                  (const char *)x2) == 0;
7441 }
7442
7443 /* Add STR to the indirect string hash table.  */
7444
7445 static struct indirect_string_node *
7446 find_AT_string (const char *str)
7447 {
7448   struct indirect_string_node *node;
7449   void **slot;
7450
7451   if (! debug_str_hash)
7452     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7453                                       debug_str_eq, NULL);
7454
7455   slot = htab_find_slot_with_hash (debug_str_hash, str,
7456                                    htab_hash_string (str), INSERT);
7457   if (*slot == NULL)
7458     {
7459       node = ggc_alloc_cleared_indirect_string_node ();
7460       node->str = ggc_strdup (str);
7461       *slot = node;
7462     }
7463   else
7464     node = (struct indirect_string_node *) *slot;
7465
7466   node->refcount++;
7467   return node;
7468 }
7469
7470 /* Add a string attribute value to a DIE.  */
7471
7472 static inline void
7473 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7474 {
7475   dw_attr_node attr;
7476   struct indirect_string_node *node;
7477
7478   node = find_AT_string (str);
7479
7480   attr.dw_attr = attr_kind;
7481   attr.dw_attr_val.val_class = dw_val_class_str;
7482   attr.dw_attr_val.v.val_str = node;
7483   add_dwarf_attr (die, &attr);
7484 }
7485
7486 /* Create a label for an indirect string node, ensuring it is going to
7487    be output, unless its reference count goes down to zero.  */
7488
7489 static inline void
7490 gen_label_for_indirect_string (struct indirect_string_node *node)
7491 {
7492   char label[32];
7493
7494   if (node->label)
7495     return;
7496
7497   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7498   ++dw2_string_counter;
7499   node->label = xstrdup (label);
7500 }
7501
7502 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7503    debug string STR.  */
7504
7505 static inline rtx
7506 get_debug_string_label (const char *str)
7507 {
7508   struct indirect_string_node *node = find_AT_string (str);
7509
7510   debug_str_hash_forced = true;
7511
7512   gen_label_for_indirect_string (node);
7513
7514   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7515 }
7516
7517 static inline const char *
7518 AT_string (dw_attr_ref a)
7519 {
7520   gcc_assert (a && AT_class (a) == dw_val_class_str);
7521   return a->dw_attr_val.v.val_str->str;
7522 }
7523
7524 /* Find out whether a string should be output inline in DIE
7525    or out-of-line in .debug_str section.  */
7526
7527 static enum dwarf_form
7528 AT_string_form (dw_attr_ref a)
7529 {
7530   struct indirect_string_node *node;
7531   unsigned int len;
7532
7533   gcc_assert (a && AT_class (a) == dw_val_class_str);
7534
7535   node = a->dw_attr_val.v.val_str;
7536   if (node->form)
7537     return node->form;
7538
7539   len = strlen (node->str) + 1;
7540
7541   /* If the string is shorter or equal to the size of the reference, it is
7542      always better to put it inline.  */
7543   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7544     return node->form = DW_FORM_string;
7545
7546   /* If we cannot expect the linker to merge strings in .debug_str
7547      section, only put it into .debug_str if it is worth even in this
7548      single module.  */
7549   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7550       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7551       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7552     return node->form = DW_FORM_string;
7553
7554   gen_label_for_indirect_string (node);
7555
7556   return node->form = DW_FORM_strp;
7557 }
7558
7559 /* Add a DIE reference attribute value to a DIE.  */
7560
7561 static inline void
7562 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7563 {
7564   dw_attr_node attr;
7565
7566 #ifdef ENABLE_CHECKING
7567   gcc_assert (targ_die != NULL);
7568 #else
7569   /* With LTO we can end up trying to reference something we didn't create
7570      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7571   if (targ_die == NULL)
7572     return;
7573 #endif
7574
7575   attr.dw_attr = attr_kind;
7576   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7577   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7578   attr.dw_attr_val.v.val_die_ref.external = 0;
7579   add_dwarf_attr (die, &attr);
7580 }
7581
7582 /* Add an AT_specification attribute to a DIE, and also make the back
7583    pointer from the specification to the definition.  */
7584
7585 static inline void
7586 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7587 {
7588   add_AT_die_ref (die, DW_AT_specification, targ_die);
7589   gcc_assert (!targ_die->die_definition);
7590   targ_die->die_definition = die;
7591 }
7592
7593 static inline dw_die_ref
7594 AT_ref (dw_attr_ref a)
7595 {
7596   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7597   return a->dw_attr_val.v.val_die_ref.die;
7598 }
7599
7600 static inline int
7601 AT_ref_external (dw_attr_ref a)
7602 {
7603   if (a && AT_class (a) == dw_val_class_die_ref)
7604     return a->dw_attr_val.v.val_die_ref.external;
7605
7606   return 0;
7607 }
7608
7609 static inline void
7610 set_AT_ref_external (dw_attr_ref a, int i)
7611 {
7612   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7613   a->dw_attr_val.v.val_die_ref.external = i;
7614 }
7615
7616 /* Add an FDE reference attribute value to a DIE.  */
7617
7618 static inline void
7619 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7620 {
7621   dw_attr_node attr;
7622
7623   attr.dw_attr = attr_kind;
7624   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7625   attr.dw_attr_val.v.val_fde_index = targ_fde;
7626   add_dwarf_attr (die, &attr);
7627 }
7628
7629 /* Add a location description attribute value to a DIE.  */
7630
7631 static inline void
7632 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7633 {
7634   dw_attr_node attr;
7635
7636   attr.dw_attr = attr_kind;
7637   attr.dw_attr_val.val_class = dw_val_class_loc;
7638   attr.dw_attr_val.v.val_loc = loc;
7639   add_dwarf_attr (die, &attr);
7640 }
7641
7642 static inline dw_loc_descr_ref
7643 AT_loc (dw_attr_ref a)
7644 {
7645   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7646   return a->dw_attr_val.v.val_loc;
7647 }
7648
7649 static inline void
7650 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7651 {
7652   dw_attr_node attr;
7653
7654   attr.dw_attr = attr_kind;
7655   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7656   attr.dw_attr_val.v.val_loc_list = loc_list;
7657   add_dwarf_attr (die, &attr);
7658   have_location_lists = true;
7659 }
7660
7661 static inline dw_loc_list_ref
7662 AT_loc_list (dw_attr_ref a)
7663 {
7664   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7665   return a->dw_attr_val.v.val_loc_list;
7666 }
7667
7668 static inline dw_loc_list_ref *
7669 AT_loc_list_ptr (dw_attr_ref a)
7670 {
7671   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7672   return &a->dw_attr_val.v.val_loc_list;
7673 }
7674
7675 /* Add an address constant attribute value to a DIE.  */
7676
7677 static inline void
7678 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7679 {
7680   dw_attr_node attr;
7681
7682   attr.dw_attr = attr_kind;
7683   attr.dw_attr_val.val_class = dw_val_class_addr;
7684   attr.dw_attr_val.v.val_addr = addr;
7685   add_dwarf_attr (die, &attr);
7686 }
7687
7688 /* Get the RTX from to an address DIE attribute.  */
7689
7690 static inline rtx
7691 AT_addr (dw_attr_ref a)
7692 {
7693   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7694   return a->dw_attr_val.v.val_addr;
7695 }
7696
7697 /* Add a file attribute value to a DIE.  */
7698
7699 static inline void
7700 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7701              struct dwarf_file_data *fd)
7702 {
7703   dw_attr_node attr;
7704
7705   attr.dw_attr = attr_kind;
7706   attr.dw_attr_val.val_class = dw_val_class_file;
7707   attr.dw_attr_val.v.val_file = fd;
7708   add_dwarf_attr (die, &attr);
7709 }
7710
7711 /* Get the dwarf_file_data from a file DIE attribute.  */
7712
7713 static inline struct dwarf_file_data *
7714 AT_file (dw_attr_ref a)
7715 {
7716   gcc_assert (a && AT_class (a) == dw_val_class_file);
7717   return a->dw_attr_val.v.val_file;
7718 }
7719
7720 /* Add a vms delta attribute value to a DIE.  */
7721
7722 static inline void
7723 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7724                   const char *lbl1, const char *lbl2)
7725 {
7726   dw_attr_node attr;
7727
7728   attr.dw_attr = attr_kind;
7729   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7730   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7731   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7732   add_dwarf_attr (die, &attr);
7733 }
7734
7735 /* Add a label identifier attribute value to a DIE.  */
7736
7737 static inline void
7738 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7739 {
7740   dw_attr_node attr;
7741
7742   attr.dw_attr = attr_kind;
7743   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7744   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7745   add_dwarf_attr (die, &attr);
7746 }
7747
7748 /* Add a section offset attribute value to a DIE, an offset into the
7749    debug_line section.  */
7750
7751 static inline void
7752 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7753                 const char *label)
7754 {
7755   dw_attr_node attr;
7756
7757   attr.dw_attr = attr_kind;
7758   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7759   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7760   add_dwarf_attr (die, &attr);
7761 }
7762
7763 /* Add a section offset attribute value to a DIE, an offset into the
7764    debug_macinfo section.  */
7765
7766 static inline void
7767 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7768                const char *label)
7769 {
7770   dw_attr_node attr;
7771
7772   attr.dw_attr = attr_kind;
7773   attr.dw_attr_val.val_class = dw_val_class_macptr;
7774   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7775   add_dwarf_attr (die, &attr);
7776 }
7777
7778 /* Add an offset attribute value to a DIE.  */
7779
7780 static inline void
7781 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7782                unsigned HOST_WIDE_INT offset)
7783 {
7784   dw_attr_node attr;
7785
7786   attr.dw_attr = attr_kind;
7787   attr.dw_attr_val.val_class = dw_val_class_offset;
7788   attr.dw_attr_val.v.val_offset = offset;
7789   add_dwarf_attr (die, &attr);
7790 }
7791
7792 /* Add an range_list attribute value to a DIE.  */
7793
7794 static void
7795 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7796                    long unsigned int offset)
7797 {
7798   dw_attr_node attr;
7799
7800   attr.dw_attr = attr_kind;
7801   attr.dw_attr_val.val_class = dw_val_class_range_list;
7802   attr.dw_attr_val.v.val_offset = offset;
7803   add_dwarf_attr (die, &attr);
7804 }
7805
7806 /* Return the start label of a delta attribute.  */
7807
7808 static inline const char *
7809 AT_vms_delta1 (dw_attr_ref a)
7810 {
7811   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7812   return a->dw_attr_val.v.val_vms_delta.lbl1;
7813 }
7814
7815 /* Return the end label of a delta attribute.  */
7816
7817 static inline const char *
7818 AT_vms_delta2 (dw_attr_ref a)
7819 {
7820   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7821   return a->dw_attr_val.v.val_vms_delta.lbl2;
7822 }
7823
7824 static inline const char *
7825 AT_lbl (dw_attr_ref a)
7826 {
7827   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7828                     || AT_class (a) == dw_val_class_lineptr
7829                     || AT_class (a) == dw_val_class_macptr));
7830   return a->dw_attr_val.v.val_lbl_id;
7831 }
7832
7833 /* Get the attribute of type attr_kind.  */
7834
7835 static dw_attr_ref
7836 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7837 {
7838   dw_attr_ref a;
7839   unsigned ix;
7840   dw_die_ref spec = NULL;
7841
7842   if (! die)
7843     return NULL;
7844
7845   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7846     if (a->dw_attr == attr_kind)
7847       return a;
7848     else if (a->dw_attr == DW_AT_specification
7849              || a->dw_attr == DW_AT_abstract_origin)
7850       spec = AT_ref (a);
7851
7852   if (spec)
7853     return get_AT (spec, attr_kind);
7854
7855   return NULL;
7856 }
7857
7858 /* Return the "low pc" attribute value, typically associated with a subprogram
7859    DIE.  Return null if the "low pc" attribute is either not present, or if it
7860    cannot be represented as an assembler label identifier.  */
7861
7862 static inline const char *
7863 get_AT_low_pc (dw_die_ref die)
7864 {
7865   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7866
7867   return a ? AT_lbl (a) : NULL;
7868 }
7869
7870 /* Return the "high pc" attribute value, typically associated with a subprogram
7871    DIE.  Return null if the "high pc" attribute is either not present, or if it
7872    cannot be represented as an assembler label identifier.  */
7873
7874 static inline const char *
7875 get_AT_hi_pc (dw_die_ref die)
7876 {
7877   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7878
7879   return a ? AT_lbl (a) : NULL;
7880 }
7881
7882 /* Return the value of the string attribute designated by ATTR_KIND, or
7883    NULL if it is not present.  */
7884
7885 static inline const char *
7886 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7887 {
7888   dw_attr_ref a = get_AT (die, attr_kind);
7889
7890   return a ? AT_string (a) : NULL;
7891 }
7892
7893 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7894    if it is not present.  */
7895
7896 static inline int
7897 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7898 {
7899   dw_attr_ref a = get_AT (die, attr_kind);
7900
7901   return a ? AT_flag (a) : 0;
7902 }
7903
7904 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7905    if it is not present.  */
7906
7907 static inline unsigned
7908 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7909 {
7910   dw_attr_ref a = get_AT (die, attr_kind);
7911
7912   return a ? AT_unsigned (a) : 0;
7913 }
7914
7915 static inline dw_die_ref
7916 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7917 {
7918   dw_attr_ref a = get_AT (die, attr_kind);
7919
7920   return a ? AT_ref (a) : NULL;
7921 }
7922
7923 static inline struct dwarf_file_data *
7924 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7925 {
7926   dw_attr_ref a = get_AT (die, attr_kind);
7927
7928   return a ? AT_file (a) : NULL;
7929 }
7930
7931 /* Return TRUE if the language is C++.  */
7932
7933 static inline bool
7934 is_cxx (void)
7935 {
7936   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7937
7938   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7939 }
7940
7941 /* Return TRUE if the language is Fortran.  */
7942
7943 static inline bool
7944 is_fortran (void)
7945 {
7946   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7947
7948   return (lang == DW_LANG_Fortran77
7949           || lang == DW_LANG_Fortran90
7950           || lang == DW_LANG_Fortran95);
7951 }
7952
7953 /* Return TRUE if the language is Ada.  */
7954
7955 static inline bool
7956 is_ada (void)
7957 {
7958   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7959
7960   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7961 }
7962
7963 /* Remove the specified attribute if present.  */
7964
7965 static void
7966 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7967 {
7968   dw_attr_ref a;
7969   unsigned ix;
7970
7971   if (! die)
7972     return;
7973
7974   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7975     if (a->dw_attr == attr_kind)
7976       {
7977         if (AT_class (a) == dw_val_class_str)
7978           if (a->dw_attr_val.v.val_str->refcount)
7979             a->dw_attr_val.v.val_str->refcount--;
7980
7981         /* VEC_ordered_remove should help reduce the number of abbrevs
7982            that are needed.  */
7983         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7984         return;
7985       }
7986 }
7987
7988 /* Remove CHILD from its parent.  PREV must have the property that
7989    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7990
7991 static void
7992 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7993 {
7994   gcc_assert (child->die_parent == prev->die_parent);
7995   gcc_assert (prev->die_sib == child);
7996   if (prev == child)
7997     {
7998       gcc_assert (child->die_parent->die_child == child);
7999       prev = NULL;
8000     }
8001   else
8002     prev->die_sib = child->die_sib;
8003   if (child->die_parent->die_child == child)
8004     child->die_parent->die_child = prev;
8005 }
8006
8007 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8008    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8009
8010 static void
8011 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8012 {
8013   dw_die_ref parent = old_child->die_parent;
8014
8015   gcc_assert (parent == prev->die_parent);
8016   gcc_assert (prev->die_sib == old_child);
8017
8018   new_child->die_parent = parent;
8019   if (prev == old_child)
8020     {
8021       gcc_assert (parent->die_child == old_child);
8022       new_child->die_sib = new_child;
8023     }
8024   else
8025     {
8026       prev->die_sib = new_child;
8027       new_child->die_sib = old_child->die_sib;
8028     }
8029   if (old_child->die_parent->die_child == old_child)
8030     old_child->die_parent->die_child = new_child;
8031 }
8032
8033 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8034
8035 static void
8036 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8037 {
8038   dw_die_ref c;
8039   new_parent->die_child = old_parent->die_child;
8040   old_parent->die_child = NULL;
8041   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8042 }
8043
8044 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8045    matches TAG.  */
8046
8047 static void
8048 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8049 {
8050   dw_die_ref c;
8051
8052   c = die->die_child;
8053   if (c) do {
8054     dw_die_ref prev = c;
8055     c = c->die_sib;
8056     while (c->die_tag == tag)
8057       {
8058         remove_child_with_prev (c, prev);
8059         /* Might have removed every child.  */
8060         if (c == c->die_sib)
8061           return;
8062         c = c->die_sib;
8063       }
8064   } while (c != die->die_child);
8065 }
8066
8067 /* Add a CHILD_DIE as the last child of DIE.  */
8068
8069 static void
8070 add_child_die (dw_die_ref die, dw_die_ref child_die)
8071 {
8072   /* FIXME this should probably be an assert.  */
8073   if (! die || ! child_die)
8074     return;
8075   gcc_assert (die != child_die);
8076
8077   child_die->die_parent = die;
8078   if (die->die_child)
8079     {
8080       child_die->die_sib = die->die_child->die_sib;
8081       die->die_child->die_sib = child_die;
8082     }
8083   else
8084     child_die->die_sib = child_die;
8085   die->die_child = child_die;
8086 }
8087
8088 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8089    is the specification, to the end of PARENT's list of children.
8090    This is done by removing and re-adding it.  */
8091
8092 static void
8093 splice_child_die (dw_die_ref parent, dw_die_ref child)
8094 {
8095   dw_die_ref p;
8096
8097   /* We want the declaration DIE from inside the class, not the
8098      specification DIE at toplevel.  */
8099   if (child->die_parent != parent)
8100     {
8101       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8102
8103       if (tmp)
8104         child = tmp;
8105     }
8106
8107   gcc_assert (child->die_parent == parent
8108               || (child->die_parent
8109                   == get_AT_ref (parent, DW_AT_specification)));
8110
8111   for (p = child->die_parent->die_child; ; p = p->die_sib)
8112     if (p->die_sib == child)
8113       {
8114         remove_child_with_prev (child, p);
8115         break;
8116       }
8117
8118   add_child_die (parent, child);
8119 }
8120
8121 /* Return a pointer to a newly created DIE node.  */
8122
8123 static inline dw_die_ref
8124 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8125 {
8126   dw_die_ref die = ggc_alloc_cleared_die_node ();
8127
8128   die->die_tag = tag_value;
8129
8130   if (parent_die != NULL)
8131     add_child_die (parent_die, die);
8132   else
8133     {
8134       limbo_die_node *limbo_node;
8135
8136       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8137       limbo_node->die = die;
8138       limbo_node->created_for = t;
8139       limbo_node->next = limbo_die_list;
8140       limbo_die_list = limbo_node;
8141     }
8142
8143   return die;
8144 }
8145
8146 /* Return the DIE associated with the given type specifier.  */
8147
8148 static inline dw_die_ref
8149 lookup_type_die (tree type)
8150 {
8151   return TYPE_SYMTAB_DIE (type);
8152 }
8153
8154 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8155    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8156    anonymous type instead the one of the naming typedef.  */
8157
8158 static inline dw_die_ref
8159 strip_naming_typedef (tree type, dw_die_ref type_die)
8160 {
8161   if (type
8162       && TREE_CODE (type) == RECORD_TYPE
8163       && type_die
8164       && type_die->die_tag == DW_TAG_typedef
8165       && is_naming_typedef_decl (TYPE_NAME (type)))
8166     type_die = get_AT_ref (type_die, DW_AT_type);
8167   return type_die;
8168 }
8169
8170 /* Like lookup_type_die, but if type is an anonymous type named by a
8171    typedef[1], return the DIE of the anonymous type instead the one of
8172    the naming typedef.  This is because in gen_typedef_die, we did
8173    equate the anonymous struct named by the typedef with the DIE of
8174    the naming typedef. So by default, lookup_type_die on an anonymous
8175    struct yields the DIE of the naming typedef.
8176
8177    [1]: Read the comment of is_naming_typedef_decl to learn about what
8178    a naming typedef is.  */
8179
8180 static inline dw_die_ref
8181 lookup_type_die_strip_naming_typedef (tree type)
8182 {
8183   dw_die_ref die = lookup_type_die (type);
8184   return strip_naming_typedef (type, die);
8185 }
8186
8187 /* Equate a DIE to a given type specifier.  */
8188
8189 static inline void
8190 equate_type_number_to_die (tree type, dw_die_ref type_die)
8191 {
8192   TYPE_SYMTAB_DIE (type) = type_die;
8193 }
8194
8195 /* Returns a hash value for X (which really is a die_struct).  */
8196
8197 static hashval_t
8198 decl_die_table_hash (const void *x)
8199 {
8200   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8201 }
8202
8203 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8204
8205 static int
8206 decl_die_table_eq (const void *x, const void *y)
8207 {
8208   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8209 }
8210
8211 /* Return the DIE associated with a given declaration.  */
8212
8213 static inline dw_die_ref
8214 lookup_decl_die (tree decl)
8215 {
8216   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8217 }
8218
8219 /* Returns a hash value for X (which really is a var_loc_list).  */
8220
8221 static hashval_t
8222 decl_loc_table_hash (const void *x)
8223 {
8224   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8225 }
8226
8227 /* Return nonzero if decl_id of var_loc_list X is the same as
8228    UID of decl *Y.  */
8229
8230 static int
8231 decl_loc_table_eq (const void *x, const void *y)
8232 {
8233   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8234 }
8235
8236 /* Return the var_loc list associated with a given declaration.  */
8237
8238 static inline var_loc_list *
8239 lookup_decl_loc (const_tree decl)
8240 {
8241   if (!decl_loc_table)
8242     return NULL;
8243   return (var_loc_list *)
8244     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8245 }
8246
8247 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8248
8249 static hashval_t
8250 cached_dw_loc_list_table_hash (const void *x)
8251 {
8252   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8253 }
8254
8255 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8256    UID of decl *Y.  */
8257
8258 static int
8259 cached_dw_loc_list_table_eq (const void *x, const void *y)
8260 {
8261   return (((const cached_dw_loc_list *) x)->decl_id
8262           == DECL_UID ((const_tree) y));
8263 }
8264
8265 /* Equate a DIE to a particular declaration.  */
8266
8267 static void
8268 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8269 {
8270   unsigned int decl_id = DECL_UID (decl);
8271   void **slot;
8272
8273   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8274   *slot = decl_die;
8275   decl_die->decl_id = decl_id;
8276 }
8277
8278 /* Return how many bits covers PIECE EXPR_LIST.  */
8279
8280 static int
8281 decl_piece_bitsize (rtx piece)
8282 {
8283   int ret = (int) GET_MODE (piece);
8284   if (ret)
8285     return ret;
8286   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8287               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8288   return INTVAL (XEXP (XEXP (piece, 0), 0));
8289 }
8290
8291 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8292
8293 static rtx *
8294 decl_piece_varloc_ptr (rtx piece)
8295 {
8296   if ((int) GET_MODE (piece))
8297     return &XEXP (piece, 0);
8298   else
8299     return &XEXP (XEXP (piece, 0), 1);
8300 }
8301
8302 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8303    Next is the chain of following piece nodes.  */
8304
8305 static rtx
8306 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8307 {
8308   if (bitsize <= (int) MAX_MACHINE_MODE)
8309     return alloc_EXPR_LIST (bitsize, loc_note, next);
8310   else
8311     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8312                                                GEN_INT (bitsize),
8313                                                loc_note), next);
8314 }
8315
8316 /* Return rtx that should be stored into loc field for
8317    LOC_NOTE and BITPOS/BITSIZE.  */
8318
8319 static rtx
8320 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8321                       HOST_WIDE_INT bitsize)
8322 {
8323   if (bitsize != -1)
8324     {
8325       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8326       if (bitpos != 0)
8327         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8328     }
8329   return loc_note;
8330 }
8331
8332 /* This function either modifies location piece list *DEST in
8333    place (if SRC and INNER is NULL), or copies location piece list
8334    *SRC to *DEST while modifying it.  Location BITPOS is modified
8335    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8336    not copied and if needed some padding around it is added.
8337    When modifying in place, DEST should point to EXPR_LIST where
8338    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8339    to the start of the whole list and INNER points to the EXPR_LIST
8340    where earlier pieces cover PIECE_BITPOS bits.  */
8341
8342 static void
8343 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8344                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8345                    HOST_WIDE_INT bitsize, rtx loc_note)
8346 {
8347   int diff;
8348   bool copy = inner != NULL;
8349
8350   if (copy)
8351     {
8352       /* First copy all nodes preceeding the current bitpos.  */
8353       while (src != inner)
8354         {
8355           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8356                                    decl_piece_bitsize (*src), NULL_RTX);
8357           dest = &XEXP (*dest, 1);
8358           src = &XEXP (*src, 1);
8359         }
8360     }
8361   /* Add padding if needed.  */
8362   if (bitpos != piece_bitpos)
8363     {
8364       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8365                                copy ? NULL_RTX : *dest);
8366       dest = &XEXP (*dest, 1);
8367     }
8368   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8369     {
8370       gcc_assert (!copy);
8371       /* A piece with correct bitpos and bitsize already exist,
8372          just update the location for it and return.  */
8373       *decl_piece_varloc_ptr (*dest) = loc_note;
8374       return;
8375     }
8376   /* Add the piece that changed.  */
8377   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8378   dest = &XEXP (*dest, 1);
8379   /* Skip over pieces that overlap it.  */
8380   diff = bitpos - piece_bitpos + bitsize;
8381   if (!copy)
8382     src = dest;
8383   while (diff > 0 && *src)
8384     {
8385       rtx piece = *src;
8386       diff -= decl_piece_bitsize (piece);
8387       if (copy)
8388         src = &XEXP (piece, 1);
8389       else
8390         {
8391           *src = XEXP (piece, 1);
8392           free_EXPR_LIST_node (piece);
8393         }
8394     }
8395   /* Add padding if needed.  */
8396   if (diff < 0 && *src)
8397     {
8398       if (!copy)
8399         dest = src;
8400       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8401       dest = &XEXP (*dest, 1);
8402     }
8403   if (!copy)
8404     return;
8405   /* Finally copy all nodes following it.  */
8406   while (*src)
8407     {
8408       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8409                                decl_piece_bitsize (*src), NULL_RTX);
8410       dest = &XEXP (*dest, 1);
8411       src = &XEXP (*src, 1);
8412     }
8413 }
8414
8415 /* Add a variable location node to the linked list for DECL.  */
8416
8417 static struct var_loc_node *
8418 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8419 {
8420   unsigned int decl_id;
8421   var_loc_list *temp;
8422   void **slot;
8423   struct var_loc_node *loc = NULL;
8424   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8425
8426   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8427     {
8428       tree realdecl = DECL_DEBUG_EXPR (decl);
8429       if (realdecl && handled_component_p (realdecl))
8430         {
8431           HOST_WIDE_INT maxsize;
8432           tree innerdecl;
8433           innerdecl
8434             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8435           if (!DECL_P (innerdecl)
8436               || DECL_IGNORED_P (innerdecl)
8437               || TREE_STATIC (innerdecl)
8438               || bitsize <= 0
8439               || bitpos + bitsize > 256
8440               || bitsize != maxsize)
8441             return NULL;
8442           decl = innerdecl;
8443         }
8444     }
8445
8446   decl_id = DECL_UID (decl);
8447   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8448   if (*slot == NULL)
8449     {
8450       temp = ggc_alloc_cleared_var_loc_list ();
8451       temp->decl_id = decl_id;
8452       *slot = temp;
8453     }
8454   else
8455     temp = (var_loc_list *) *slot;
8456
8457   if (temp->last)
8458     {
8459       struct var_loc_node *last = temp->last, *unused = NULL;
8460       rtx *piece_loc = NULL, last_loc_note;
8461       int piece_bitpos = 0;
8462       if (last->next)
8463         {
8464           last = last->next;
8465           gcc_assert (last->next == NULL);
8466         }
8467       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8468         {
8469           piece_loc = &last->loc;
8470           do
8471             {
8472               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8473               if (piece_bitpos + cur_bitsize > bitpos)
8474                 break;
8475               piece_bitpos += cur_bitsize;
8476               piece_loc = &XEXP (*piece_loc, 1);
8477             }
8478           while (*piece_loc);
8479         }
8480       /* TEMP->LAST here is either pointer to the last but one or
8481          last element in the chained list, LAST is pointer to the
8482          last element.  */
8483       if (label && strcmp (last->label, label) == 0)
8484         {
8485           /* For SRA optimized variables if there weren't any real
8486              insns since last note, just modify the last node.  */
8487           if (piece_loc != NULL)
8488             {
8489               adjust_piece_list (piece_loc, NULL, NULL,
8490                                  bitpos, piece_bitpos, bitsize, loc_note);
8491               return NULL;
8492             }
8493           /* If the last note doesn't cover any instructions, remove it.  */
8494           if (temp->last != last)
8495             {
8496               temp->last->next = NULL;
8497               unused = last;
8498               last = temp->last;
8499               gcc_assert (strcmp (last->label, label) != 0);
8500             }
8501           else
8502             {
8503               gcc_assert (temp->first == temp->last);
8504               memset (temp->last, '\0', sizeof (*temp->last));
8505               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8506               return temp->last;
8507             }
8508         }
8509       if (bitsize == -1 && NOTE_P (last->loc))
8510         last_loc_note = last->loc;
8511       else if (piece_loc != NULL
8512                && *piece_loc != NULL_RTX
8513                && piece_bitpos == bitpos
8514                && decl_piece_bitsize (*piece_loc) == bitsize)
8515         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8516       else
8517         last_loc_note = NULL_RTX;
8518       /* If the current location is the same as the end of the list,
8519          and either both or neither of the locations is uninitialized,
8520          we have nothing to do.  */
8521       if (last_loc_note == NULL_RTX
8522           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8523                             NOTE_VAR_LOCATION_LOC (loc_note)))
8524           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8525                != NOTE_VAR_LOCATION_STATUS (loc_note))
8526               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8527                    == VAR_INIT_STATUS_UNINITIALIZED)
8528                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8529                       == VAR_INIT_STATUS_UNINITIALIZED))))
8530         {
8531           /* Add LOC to the end of list and update LAST.  If the last
8532              element of the list has been removed above, reuse its
8533              memory for the new node, otherwise allocate a new one.  */
8534           if (unused)
8535             {
8536               loc = unused;
8537               memset (loc, '\0', sizeof (*loc));
8538             }
8539           else
8540             loc = ggc_alloc_cleared_var_loc_node ();
8541           if (bitsize == -1 || piece_loc == NULL)
8542             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8543           else
8544             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8545                                bitpos, piece_bitpos, bitsize, loc_note);
8546           last->next = loc;
8547           /* Ensure TEMP->LAST will point either to the new last but one
8548              element of the chain, or to the last element in it.  */
8549           if (last != temp->last)
8550             temp->last = last;
8551         }
8552       else if (unused)
8553         ggc_free (unused);
8554     }
8555   else
8556     {
8557       loc = ggc_alloc_cleared_var_loc_node ();
8558       temp->first = loc;
8559       temp->last = loc;
8560       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8561     }
8562   return loc;
8563 }
8564 \f
8565 /* Keep track of the number of spaces used to indent the
8566    output of the debugging routines that print the structure of
8567    the DIE internal representation.  */
8568 static int print_indent;
8569
8570 /* Indent the line the number of spaces given by print_indent.  */
8571
8572 static inline void
8573 print_spaces (FILE *outfile)
8574 {
8575   fprintf (outfile, "%*s", print_indent, "");
8576 }
8577
8578 /* Print a type signature in hex.  */
8579
8580 static inline void
8581 print_signature (FILE *outfile, char *sig)
8582 {
8583   int i;
8584
8585   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8586     fprintf (outfile, "%02x", sig[i] & 0xff);
8587 }
8588
8589 /* Print the information associated with a given DIE, and its children.
8590    This routine is a debugging aid only.  */
8591
8592 static void
8593 print_die (dw_die_ref die, FILE *outfile)
8594 {
8595   dw_attr_ref a;
8596   dw_die_ref c;
8597   unsigned ix;
8598
8599   print_spaces (outfile);
8600   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8601            die->die_offset, dwarf_tag_name (die->die_tag),
8602            (void*) die);
8603   print_spaces (outfile);
8604   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8605   fprintf (outfile, " offset: %ld", die->die_offset);
8606   fprintf (outfile, " mark: %d\n", die->die_mark);
8607
8608   if (use_debug_types && die->die_id.die_type_node)
8609     {
8610       print_spaces (outfile);
8611       fprintf (outfile, "  signature: ");
8612       print_signature (outfile, die->die_id.die_type_node->signature);
8613       fprintf (outfile, "\n");
8614     }
8615
8616   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8617     {
8618       print_spaces (outfile);
8619       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8620
8621       switch (AT_class (a))
8622         {
8623         case dw_val_class_addr:
8624           fprintf (outfile, "address");
8625           break;
8626         case dw_val_class_offset:
8627           fprintf (outfile, "offset");
8628           break;
8629         case dw_val_class_loc:
8630           fprintf (outfile, "location descriptor");
8631           break;
8632         case dw_val_class_loc_list:
8633           fprintf (outfile, "location list -> label:%s",
8634                    AT_loc_list (a)->ll_symbol);
8635           break;
8636         case dw_val_class_range_list:
8637           fprintf (outfile, "range list");
8638           break;
8639         case dw_val_class_const:
8640           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8641           break;
8642         case dw_val_class_unsigned_const:
8643           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8644           break;
8645         case dw_val_class_const_double:
8646           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8647                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8648                    a->dw_attr_val.v.val_double.high,
8649                    a->dw_attr_val.v.val_double.low);
8650           break;
8651         case dw_val_class_vec:
8652           fprintf (outfile, "floating-point or vector constant");
8653           break;
8654         case dw_val_class_flag:
8655           fprintf (outfile, "%u", AT_flag (a));
8656           break;
8657         case dw_val_class_die_ref:
8658           if (AT_ref (a) != NULL)
8659             {
8660               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8661                 {
8662                   fprintf (outfile, "die -> signature: ");
8663                   print_signature (outfile,
8664                                    AT_ref (a)->die_id.die_type_node->signature);
8665                 }
8666               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8667                 fprintf (outfile, "die -> label: %s",
8668                          AT_ref (a)->die_id.die_symbol);
8669               else
8670                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8671               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8672             }
8673           else
8674             fprintf (outfile, "die -> <null>");
8675           break;
8676         case dw_val_class_vms_delta:
8677           fprintf (outfile, "delta: @slotcount(%s-%s)",
8678                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8679           break;
8680         case dw_val_class_lbl_id:
8681         case dw_val_class_lineptr:
8682         case dw_val_class_macptr:
8683           fprintf (outfile, "label: %s", AT_lbl (a));
8684           break;
8685         case dw_val_class_str:
8686           if (AT_string (a) != NULL)
8687             fprintf (outfile, "\"%s\"", AT_string (a));
8688           else
8689             fprintf (outfile, "<null>");
8690           break;
8691         case dw_val_class_file:
8692           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8693                    AT_file (a)->emitted_number);
8694           break;
8695         case dw_val_class_data8:
8696           {
8697             int i;
8698
8699             for (i = 0; i < 8; i++)
8700               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8701             break;
8702           }
8703         default:
8704           break;
8705         }
8706
8707       fprintf (outfile, "\n");
8708     }
8709
8710   if (die->die_child != NULL)
8711     {
8712       print_indent += 4;
8713       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8714       print_indent -= 4;
8715     }
8716   if (print_indent == 0)
8717     fprintf (outfile, "\n");
8718 }
8719
8720 /* Print the information collected for a given DIE.  */
8721
8722 DEBUG_FUNCTION void
8723 debug_dwarf_die (dw_die_ref die)
8724 {
8725   print_die (die, stderr);
8726 }
8727
8728 /* Print all DWARF information collected for the compilation unit.
8729    This routine is a debugging aid only.  */
8730
8731 DEBUG_FUNCTION void
8732 debug_dwarf (void)
8733 {
8734   print_indent = 0;
8735   print_die (comp_unit_die (), stderr);
8736 }
8737 \f
8738 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8739    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8740    DIE that marks the start of the DIEs for this include file.  */
8741
8742 static dw_die_ref
8743 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8744 {
8745   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8746   dw_die_ref new_unit = gen_compile_unit_die (filename);
8747
8748   new_unit->die_sib = old_unit;
8749   return new_unit;
8750 }
8751
8752 /* Close an include-file CU and reopen the enclosing one.  */
8753
8754 static dw_die_ref
8755 pop_compile_unit (dw_die_ref old_unit)
8756 {
8757   dw_die_ref new_unit = old_unit->die_sib;
8758
8759   old_unit->die_sib = NULL;
8760   return new_unit;
8761 }
8762
8763 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8764 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8765
8766 /* Calculate the checksum of a location expression.  */
8767
8768 static inline void
8769 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8770 {
8771   int tem;
8772
8773   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8774   CHECKSUM (tem);
8775   CHECKSUM (loc->dw_loc_oprnd1);
8776   CHECKSUM (loc->dw_loc_oprnd2);
8777 }
8778
8779 /* Calculate the checksum of an attribute.  */
8780
8781 static void
8782 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8783 {
8784   dw_loc_descr_ref loc;
8785   rtx r;
8786
8787   CHECKSUM (at->dw_attr);
8788
8789   /* We don't care that this was compiled with a different compiler
8790      snapshot; if the output is the same, that's what matters.  */
8791   if (at->dw_attr == DW_AT_producer)
8792     return;
8793
8794   switch (AT_class (at))
8795     {
8796     case dw_val_class_const:
8797       CHECKSUM (at->dw_attr_val.v.val_int);
8798       break;
8799     case dw_val_class_unsigned_const:
8800       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8801       break;
8802     case dw_val_class_const_double:
8803       CHECKSUM (at->dw_attr_val.v.val_double);
8804       break;
8805     case dw_val_class_vec:
8806       CHECKSUM (at->dw_attr_val.v.val_vec);
8807       break;
8808     case dw_val_class_flag:
8809       CHECKSUM (at->dw_attr_val.v.val_flag);
8810       break;
8811     case dw_val_class_str:
8812       CHECKSUM_STRING (AT_string (at));
8813       break;
8814
8815     case dw_val_class_addr:
8816       r = AT_addr (at);
8817       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8818       CHECKSUM_STRING (XSTR (r, 0));
8819       break;
8820
8821     case dw_val_class_offset:
8822       CHECKSUM (at->dw_attr_val.v.val_offset);
8823       break;
8824
8825     case dw_val_class_loc:
8826       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8827         loc_checksum (loc, ctx);
8828       break;
8829
8830     case dw_val_class_die_ref:
8831       die_checksum (AT_ref (at), ctx, mark);
8832       break;
8833
8834     case dw_val_class_fde_ref:
8835     case dw_val_class_vms_delta:
8836     case dw_val_class_lbl_id:
8837     case dw_val_class_lineptr:
8838     case dw_val_class_macptr:
8839       break;
8840
8841     case dw_val_class_file:
8842       CHECKSUM_STRING (AT_file (at)->filename);
8843       break;
8844
8845     case dw_val_class_data8:
8846       CHECKSUM (at->dw_attr_val.v.val_data8);
8847       break;
8848
8849     default:
8850       break;
8851     }
8852 }
8853
8854 /* Calculate the checksum of a DIE.  */
8855
8856 static void
8857 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8858 {
8859   dw_die_ref c;
8860   dw_attr_ref a;
8861   unsigned ix;
8862
8863   /* To avoid infinite recursion.  */
8864   if (die->die_mark)
8865     {
8866       CHECKSUM (die->die_mark);
8867       return;
8868     }
8869   die->die_mark = ++(*mark);
8870
8871   CHECKSUM (die->die_tag);
8872
8873   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8874     attr_checksum (a, ctx, mark);
8875
8876   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8877 }
8878
8879 #undef CHECKSUM
8880 #undef CHECKSUM_STRING
8881
8882 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8883 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8884 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8885 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8886 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8887 #define CHECKSUM_ATTR(FOO) \
8888   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8889
8890 /* Calculate the checksum of a number in signed LEB128 format.  */
8891
8892 static void
8893 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8894 {
8895   unsigned char byte;
8896   bool more;
8897
8898   while (1)
8899     {
8900       byte = (value & 0x7f);
8901       value >>= 7;
8902       more = !((value == 0 && (byte & 0x40) == 0)
8903                 || (value == -1 && (byte & 0x40) != 0));
8904       if (more)
8905         byte |= 0x80;
8906       CHECKSUM (byte);
8907       if (!more)
8908         break;
8909     }
8910 }
8911
8912 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8913
8914 static void
8915 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8916 {
8917   while (1)
8918     {
8919       unsigned char byte = (value & 0x7f);
8920       value >>= 7;
8921       if (value != 0)
8922         /* More bytes to follow.  */
8923         byte |= 0x80;
8924       CHECKSUM (byte);
8925       if (value == 0)
8926         break;
8927     }
8928 }
8929
8930 /* Checksum the context of the DIE.  This adds the names of any
8931    surrounding namespaces or structures to the checksum.  */
8932
8933 static void
8934 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8935 {
8936   const char *name;
8937   dw_die_ref spec;
8938   int tag = die->die_tag;
8939
8940   if (tag != DW_TAG_namespace
8941       && tag != DW_TAG_structure_type
8942       && tag != DW_TAG_class_type)
8943     return;
8944
8945   name = get_AT_string (die, DW_AT_name);
8946
8947   spec = get_AT_ref (die, DW_AT_specification);
8948   if (spec != NULL)
8949     die = spec;
8950
8951   if (die->die_parent != NULL)
8952     checksum_die_context (die->die_parent, ctx);
8953
8954   CHECKSUM_ULEB128 ('C');
8955   CHECKSUM_ULEB128 (tag);
8956   if (name != NULL)
8957     CHECKSUM_STRING (name);
8958 }
8959
8960 /* Calculate the checksum of a location expression.  */
8961
8962 static inline void
8963 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8964 {
8965   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8966      were emitted as a DW_FORM_sdata instead of a location expression.  */
8967   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8968     {
8969       CHECKSUM_ULEB128 (DW_FORM_sdata);
8970       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8971       return;
8972     }
8973
8974   /* Otherwise, just checksum the raw location expression.  */
8975   while (loc != NULL)
8976     {
8977       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8978       CHECKSUM (loc->dw_loc_oprnd1);
8979       CHECKSUM (loc->dw_loc_oprnd2);
8980       loc = loc->dw_loc_next;
8981     }
8982 }
8983
8984 /* Calculate the checksum of an attribute.  */
8985
8986 static void
8987 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8988                        struct md5_ctx *ctx, int *mark)
8989 {
8990   dw_loc_descr_ref loc;
8991   rtx r;
8992
8993   if (AT_class (at) == dw_val_class_die_ref)
8994     {
8995       dw_die_ref target_die = AT_ref (at);
8996
8997       /* For pointer and reference types, we checksum only the (qualified)
8998          name of the target type (if there is a name).  For friend entries,
8999          we checksum only the (qualified) name of the target type or function.
9000          This allows the checksum to remain the same whether the target type
9001          is complete or not.  */
9002       if ((at->dw_attr == DW_AT_type
9003            && (tag == DW_TAG_pointer_type
9004                || tag == DW_TAG_reference_type
9005                || tag == DW_TAG_rvalue_reference_type
9006                || tag == DW_TAG_ptr_to_member_type))
9007           || (at->dw_attr == DW_AT_friend
9008               && tag == DW_TAG_friend))
9009         {
9010           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9011
9012           if (name_attr != NULL)
9013             {
9014               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9015
9016               if (decl == NULL)
9017                 decl = target_die;
9018               CHECKSUM_ULEB128 ('N');
9019               CHECKSUM_ULEB128 (at->dw_attr);
9020               if (decl->die_parent != NULL)
9021                 checksum_die_context (decl->die_parent, ctx);
9022               CHECKSUM_ULEB128 ('E');
9023               CHECKSUM_STRING (AT_string (name_attr));
9024               return;
9025             }
9026         }
9027
9028       /* For all other references to another DIE, we check to see if the
9029          target DIE has already been visited.  If it has, we emit a
9030          backward reference; if not, we descend recursively.  */
9031       if (target_die->die_mark > 0)
9032         {
9033           CHECKSUM_ULEB128 ('R');
9034           CHECKSUM_ULEB128 (at->dw_attr);
9035           CHECKSUM_ULEB128 (target_die->die_mark);
9036         }
9037       else
9038         {
9039           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9040
9041           if (decl == NULL)
9042             decl = target_die;
9043           target_die->die_mark = ++(*mark);
9044           CHECKSUM_ULEB128 ('T');
9045           CHECKSUM_ULEB128 (at->dw_attr);
9046           if (decl->die_parent != NULL)
9047             checksum_die_context (decl->die_parent, ctx);
9048           die_checksum_ordered (target_die, ctx, mark);
9049         }
9050       return;
9051     }
9052
9053   CHECKSUM_ULEB128 ('A');
9054   CHECKSUM_ULEB128 (at->dw_attr);
9055
9056   switch (AT_class (at))
9057     {
9058     case dw_val_class_const:
9059       CHECKSUM_ULEB128 (DW_FORM_sdata);
9060       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9061       break;
9062
9063     case dw_val_class_unsigned_const:
9064       CHECKSUM_ULEB128 (DW_FORM_sdata);
9065       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9066       break;
9067
9068     case dw_val_class_const_double:
9069       CHECKSUM_ULEB128 (DW_FORM_block);
9070       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9071       CHECKSUM (at->dw_attr_val.v.val_double);
9072       break;
9073
9074     case dw_val_class_vec:
9075       CHECKSUM_ULEB128 (DW_FORM_block);
9076       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9077       CHECKSUM (at->dw_attr_val.v.val_vec);
9078       break;
9079
9080     case dw_val_class_flag:
9081       CHECKSUM_ULEB128 (DW_FORM_flag);
9082       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9083       break;
9084
9085     case dw_val_class_str:
9086       CHECKSUM_ULEB128 (DW_FORM_string);
9087       CHECKSUM_STRING (AT_string (at));
9088       break;
9089
9090     case dw_val_class_addr:
9091       r = AT_addr (at);
9092       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9093       CHECKSUM_ULEB128 (DW_FORM_string);
9094       CHECKSUM_STRING (XSTR (r, 0));
9095       break;
9096
9097     case dw_val_class_offset:
9098       CHECKSUM_ULEB128 (DW_FORM_sdata);
9099       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9100       break;
9101
9102     case dw_val_class_loc:
9103       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9104         loc_checksum_ordered (loc, ctx);
9105       break;
9106
9107     case dw_val_class_fde_ref:
9108     case dw_val_class_lbl_id:
9109     case dw_val_class_lineptr:
9110     case dw_val_class_macptr:
9111       break;
9112
9113     case dw_val_class_file:
9114       CHECKSUM_ULEB128 (DW_FORM_string);
9115       CHECKSUM_STRING (AT_file (at)->filename);
9116       break;
9117
9118     case dw_val_class_data8:
9119       CHECKSUM (at->dw_attr_val.v.val_data8);
9120       break;
9121
9122     default:
9123       break;
9124     }
9125 }
9126
9127 struct checksum_attributes
9128 {
9129   dw_attr_ref at_name;
9130   dw_attr_ref at_type;
9131   dw_attr_ref at_friend;
9132   dw_attr_ref at_accessibility;
9133   dw_attr_ref at_address_class;
9134   dw_attr_ref at_allocated;
9135   dw_attr_ref at_artificial;
9136   dw_attr_ref at_associated;
9137   dw_attr_ref at_binary_scale;
9138   dw_attr_ref at_bit_offset;
9139   dw_attr_ref at_bit_size;
9140   dw_attr_ref at_bit_stride;
9141   dw_attr_ref at_byte_size;
9142   dw_attr_ref at_byte_stride;
9143   dw_attr_ref at_const_value;
9144   dw_attr_ref at_containing_type;
9145   dw_attr_ref at_count;
9146   dw_attr_ref at_data_location;
9147   dw_attr_ref at_data_member_location;
9148   dw_attr_ref at_decimal_scale;
9149   dw_attr_ref at_decimal_sign;
9150   dw_attr_ref at_default_value;
9151   dw_attr_ref at_digit_count;
9152   dw_attr_ref at_discr;
9153   dw_attr_ref at_discr_list;
9154   dw_attr_ref at_discr_value;
9155   dw_attr_ref at_encoding;
9156   dw_attr_ref at_endianity;
9157   dw_attr_ref at_explicit;
9158   dw_attr_ref at_is_optional;
9159   dw_attr_ref at_location;
9160   dw_attr_ref at_lower_bound;
9161   dw_attr_ref at_mutable;
9162   dw_attr_ref at_ordering;
9163   dw_attr_ref at_picture_string;
9164   dw_attr_ref at_prototyped;
9165   dw_attr_ref at_small;
9166   dw_attr_ref at_segment;
9167   dw_attr_ref at_string_length;
9168   dw_attr_ref at_threads_scaled;
9169   dw_attr_ref at_upper_bound;
9170   dw_attr_ref at_use_location;
9171   dw_attr_ref at_use_UTF8;
9172   dw_attr_ref at_variable_parameter;
9173   dw_attr_ref at_virtuality;
9174   dw_attr_ref at_visibility;
9175   dw_attr_ref at_vtable_elem_location;
9176 };
9177
9178 /* Collect the attributes that we will want to use for the checksum.  */
9179
9180 static void
9181 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9182 {
9183   dw_attr_ref a;
9184   unsigned ix;
9185
9186   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9187     {
9188       switch (a->dw_attr)
9189         {
9190         case DW_AT_name:
9191           attrs->at_name = a;
9192           break;
9193         case DW_AT_type:
9194           attrs->at_type = a;
9195           break;
9196         case DW_AT_friend:
9197           attrs->at_friend = a;
9198           break;
9199         case DW_AT_accessibility:
9200           attrs->at_accessibility = a;
9201           break;
9202         case DW_AT_address_class:
9203           attrs->at_address_class = a;
9204           break;
9205         case DW_AT_allocated:
9206           attrs->at_allocated = a;
9207           break;
9208         case DW_AT_artificial:
9209           attrs->at_artificial = a;
9210           break;
9211         case DW_AT_associated:
9212           attrs->at_associated = a;
9213           break;
9214         case DW_AT_binary_scale:
9215           attrs->at_binary_scale = a;
9216           break;
9217         case DW_AT_bit_offset:
9218           attrs->at_bit_offset = a;
9219           break;
9220         case DW_AT_bit_size:
9221           attrs->at_bit_size = a;
9222           break;
9223         case DW_AT_bit_stride:
9224           attrs->at_bit_stride = a;
9225           break;
9226         case DW_AT_byte_size:
9227           attrs->at_byte_size = a;
9228           break;
9229         case DW_AT_byte_stride:
9230           attrs->at_byte_stride = a;
9231           break;
9232         case DW_AT_const_value:
9233           attrs->at_const_value = a;
9234           break;
9235         case DW_AT_containing_type:
9236           attrs->at_containing_type = a;
9237           break;
9238         case DW_AT_count:
9239           attrs->at_count = a;
9240           break;
9241         case DW_AT_data_location:
9242           attrs->at_data_location = a;
9243           break;
9244         case DW_AT_data_member_location:
9245           attrs->at_data_member_location = a;
9246           break;
9247         case DW_AT_decimal_scale:
9248           attrs->at_decimal_scale = a;
9249           break;
9250         case DW_AT_decimal_sign:
9251           attrs->at_decimal_sign = a;
9252           break;
9253         case DW_AT_default_value:
9254           attrs->at_default_value = a;
9255           break;
9256         case DW_AT_digit_count:
9257           attrs->at_digit_count = a;
9258           break;
9259         case DW_AT_discr:
9260           attrs->at_discr = a;
9261           break;
9262         case DW_AT_discr_list:
9263           attrs->at_discr_list = a;
9264           break;
9265         case DW_AT_discr_value:
9266           attrs->at_discr_value = a;
9267           break;
9268         case DW_AT_encoding:
9269           attrs->at_encoding = a;
9270           break;
9271         case DW_AT_endianity:
9272           attrs->at_endianity = a;
9273           break;
9274         case DW_AT_explicit:
9275           attrs->at_explicit = a;
9276           break;
9277         case DW_AT_is_optional:
9278           attrs->at_is_optional = a;
9279           break;
9280         case DW_AT_location:
9281           attrs->at_location = a;
9282           break;
9283         case DW_AT_lower_bound:
9284           attrs->at_lower_bound = a;
9285           break;
9286         case DW_AT_mutable:
9287           attrs->at_mutable = a;
9288           break;
9289         case DW_AT_ordering:
9290           attrs->at_ordering = a;
9291           break;
9292         case DW_AT_picture_string:
9293           attrs->at_picture_string = a;
9294           break;
9295         case DW_AT_prototyped:
9296           attrs->at_prototyped = a;
9297           break;
9298         case DW_AT_small:
9299           attrs->at_small = a;
9300           break;
9301         case DW_AT_segment:
9302           attrs->at_segment = a;
9303           break;
9304         case DW_AT_string_length:
9305           attrs->at_string_length = a;
9306           break;
9307         case DW_AT_threads_scaled:
9308           attrs->at_threads_scaled = a;
9309           break;
9310         case DW_AT_upper_bound:
9311           attrs->at_upper_bound = a;
9312           break;
9313         case DW_AT_use_location:
9314           attrs->at_use_location = a;
9315           break;
9316         case DW_AT_use_UTF8:
9317           attrs->at_use_UTF8 = a;
9318           break;
9319         case DW_AT_variable_parameter:
9320           attrs->at_variable_parameter = a;
9321           break;
9322         case DW_AT_virtuality:
9323           attrs->at_virtuality = a;
9324           break;
9325         case DW_AT_visibility:
9326           attrs->at_visibility = a;
9327           break;
9328         case DW_AT_vtable_elem_location:
9329           attrs->at_vtable_elem_location = a;
9330           break;
9331         default:
9332           break;
9333         }
9334     }
9335 }
9336
9337 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9338
9339 static void
9340 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9341 {
9342   dw_die_ref c;
9343   dw_die_ref decl;
9344   struct checksum_attributes attrs;
9345
9346   CHECKSUM_ULEB128 ('D');
9347   CHECKSUM_ULEB128 (die->die_tag);
9348
9349   memset (&attrs, 0, sizeof (attrs));
9350
9351   decl = get_AT_ref (die, DW_AT_specification);
9352   if (decl != NULL)
9353     collect_checksum_attributes (&attrs, decl);
9354   collect_checksum_attributes (&attrs, die);
9355
9356   CHECKSUM_ATTR (attrs.at_name);
9357   CHECKSUM_ATTR (attrs.at_accessibility);
9358   CHECKSUM_ATTR (attrs.at_address_class);
9359   CHECKSUM_ATTR (attrs.at_allocated);
9360   CHECKSUM_ATTR (attrs.at_artificial);
9361   CHECKSUM_ATTR (attrs.at_associated);
9362   CHECKSUM_ATTR (attrs.at_binary_scale);
9363   CHECKSUM_ATTR (attrs.at_bit_offset);
9364   CHECKSUM_ATTR (attrs.at_bit_size);
9365   CHECKSUM_ATTR (attrs.at_bit_stride);
9366   CHECKSUM_ATTR (attrs.at_byte_size);
9367   CHECKSUM_ATTR (attrs.at_byte_stride);
9368   CHECKSUM_ATTR (attrs.at_const_value);
9369   CHECKSUM_ATTR (attrs.at_containing_type);
9370   CHECKSUM_ATTR (attrs.at_count);
9371   CHECKSUM_ATTR (attrs.at_data_location);
9372   CHECKSUM_ATTR (attrs.at_data_member_location);
9373   CHECKSUM_ATTR (attrs.at_decimal_scale);
9374   CHECKSUM_ATTR (attrs.at_decimal_sign);
9375   CHECKSUM_ATTR (attrs.at_default_value);
9376   CHECKSUM_ATTR (attrs.at_digit_count);
9377   CHECKSUM_ATTR (attrs.at_discr);
9378   CHECKSUM_ATTR (attrs.at_discr_list);
9379   CHECKSUM_ATTR (attrs.at_discr_value);
9380   CHECKSUM_ATTR (attrs.at_encoding);
9381   CHECKSUM_ATTR (attrs.at_endianity);
9382   CHECKSUM_ATTR (attrs.at_explicit);
9383   CHECKSUM_ATTR (attrs.at_is_optional);
9384   CHECKSUM_ATTR (attrs.at_location);
9385   CHECKSUM_ATTR (attrs.at_lower_bound);
9386   CHECKSUM_ATTR (attrs.at_mutable);
9387   CHECKSUM_ATTR (attrs.at_ordering);
9388   CHECKSUM_ATTR (attrs.at_picture_string);
9389   CHECKSUM_ATTR (attrs.at_prototyped);
9390   CHECKSUM_ATTR (attrs.at_small);
9391   CHECKSUM_ATTR (attrs.at_segment);
9392   CHECKSUM_ATTR (attrs.at_string_length);
9393   CHECKSUM_ATTR (attrs.at_threads_scaled);
9394   CHECKSUM_ATTR (attrs.at_upper_bound);
9395   CHECKSUM_ATTR (attrs.at_use_location);
9396   CHECKSUM_ATTR (attrs.at_use_UTF8);
9397   CHECKSUM_ATTR (attrs.at_variable_parameter);
9398   CHECKSUM_ATTR (attrs.at_virtuality);
9399   CHECKSUM_ATTR (attrs.at_visibility);
9400   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9401   CHECKSUM_ATTR (attrs.at_type);
9402   CHECKSUM_ATTR (attrs.at_friend);
9403
9404   /* Checksum the child DIEs, except for nested types and member functions.  */
9405   c = die->die_child;
9406   if (c) do {
9407     dw_attr_ref name_attr;
9408
9409     c = c->die_sib;
9410     name_attr = get_AT (c, DW_AT_name);
9411     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9412         && name_attr != NULL)
9413       {
9414         CHECKSUM_ULEB128 ('S');
9415         CHECKSUM_ULEB128 (c->die_tag);
9416         CHECKSUM_STRING (AT_string (name_attr));
9417       }
9418     else
9419       {
9420         /* Mark this DIE so it gets processed when unmarking.  */
9421         if (c->die_mark == 0)
9422           c->die_mark = -1;
9423         die_checksum_ordered (c, ctx, mark);
9424       }
9425   } while (c != die->die_child);
9426
9427   CHECKSUM_ULEB128 (0);
9428 }
9429
9430 #undef CHECKSUM
9431 #undef CHECKSUM_STRING
9432 #undef CHECKSUM_ATTR
9433 #undef CHECKSUM_LEB128
9434 #undef CHECKSUM_ULEB128
9435
9436 /* Generate the type signature for DIE.  This is computed by generating an
9437    MD5 checksum over the DIE's tag, its relevant attributes, and its
9438    children.  Attributes that are references to other DIEs are processed
9439    by recursion, using the MARK field to prevent infinite recursion.
9440    If the DIE is nested inside a namespace or another type, we also
9441    need to include that context in the signature.  The lower 64 bits
9442    of the resulting MD5 checksum comprise the signature.  */
9443
9444 static void
9445 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9446 {
9447   int mark;
9448   const char *name;
9449   unsigned char checksum[16];
9450   struct md5_ctx ctx;
9451   dw_die_ref decl;
9452
9453   name = get_AT_string (die, DW_AT_name);
9454   decl = get_AT_ref (die, DW_AT_specification);
9455
9456   /* First, compute a signature for just the type name (and its surrounding
9457      context, if any.  This is stored in the type unit DIE for link-time
9458      ODR (one-definition rule) checking.  */
9459
9460   if (is_cxx() && name != NULL)
9461     {
9462       md5_init_ctx (&ctx);
9463
9464       /* Checksum the names of surrounding namespaces and structures.  */
9465       if (decl != NULL && decl->die_parent != NULL)
9466         checksum_die_context (decl->die_parent, &ctx);
9467
9468       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9469       md5_process_bytes (name, strlen (name) + 1, &ctx);
9470       md5_finish_ctx (&ctx, checksum);
9471
9472       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9473     }
9474
9475   /* Next, compute the complete type signature.  */
9476
9477   md5_init_ctx (&ctx);
9478   mark = 1;
9479   die->die_mark = mark;
9480
9481   /* Checksum the names of surrounding namespaces and structures.  */
9482   if (decl != NULL && decl->die_parent != NULL)
9483     checksum_die_context (decl->die_parent, &ctx);
9484
9485   /* Checksum the DIE and its children.  */
9486   die_checksum_ordered (die, &ctx, &mark);
9487   unmark_all_dies (die);
9488   md5_finish_ctx (&ctx, checksum);
9489
9490   /* Store the signature in the type node and link the type DIE and the
9491      type node together.  */
9492   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9493           DWARF_TYPE_SIGNATURE_SIZE);
9494   die->die_id.die_type_node = type_node;
9495   type_node->type_die = die;
9496
9497   /* If the DIE is a specification, link its declaration to the type node
9498      as well.  */
9499   if (decl != NULL)
9500     decl->die_id.die_type_node = type_node;
9501 }
9502
9503 /* Do the location expressions look same?  */
9504 static inline int
9505 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9506 {
9507   return loc1->dw_loc_opc == loc2->dw_loc_opc
9508          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9509          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9510 }
9511
9512 /* Do the values look the same?  */
9513 static int
9514 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9515 {
9516   dw_loc_descr_ref loc1, loc2;
9517   rtx r1, r2;
9518
9519   if (v1->val_class != v2->val_class)
9520     return 0;
9521
9522   switch (v1->val_class)
9523     {
9524     case dw_val_class_const:
9525       return v1->v.val_int == v2->v.val_int;
9526     case dw_val_class_unsigned_const:
9527       return v1->v.val_unsigned == v2->v.val_unsigned;
9528     case dw_val_class_const_double:
9529       return v1->v.val_double.high == v2->v.val_double.high
9530              && v1->v.val_double.low == v2->v.val_double.low;
9531     case dw_val_class_vec:
9532       if (v1->v.val_vec.length != v2->v.val_vec.length
9533           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9534         return 0;
9535       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9536                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9537         return 0;
9538       return 1;
9539     case dw_val_class_flag:
9540       return v1->v.val_flag == v2->v.val_flag;
9541     case dw_val_class_str:
9542       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9543
9544     case dw_val_class_addr:
9545       r1 = v1->v.val_addr;
9546       r2 = v2->v.val_addr;
9547       if (GET_CODE (r1) != GET_CODE (r2))
9548         return 0;
9549       return !rtx_equal_p (r1, r2);
9550
9551     case dw_val_class_offset:
9552       return v1->v.val_offset == v2->v.val_offset;
9553
9554     case dw_val_class_loc:
9555       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9556            loc1 && loc2;
9557            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9558         if (!same_loc_p (loc1, loc2, mark))
9559           return 0;
9560       return !loc1 && !loc2;
9561
9562     case dw_val_class_die_ref:
9563       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9564
9565     case dw_val_class_fde_ref:
9566     case dw_val_class_vms_delta:
9567     case dw_val_class_lbl_id:
9568     case dw_val_class_lineptr:
9569     case dw_val_class_macptr:
9570       return 1;
9571
9572     case dw_val_class_file:
9573       return v1->v.val_file == v2->v.val_file;
9574
9575     case dw_val_class_data8:
9576       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9577
9578     default:
9579       return 1;
9580     }
9581 }
9582
9583 /* Do the attributes look the same?  */
9584
9585 static int
9586 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9587 {
9588   if (at1->dw_attr != at2->dw_attr)
9589     return 0;
9590
9591   /* We don't care that this was compiled with a different compiler
9592      snapshot; if the output is the same, that's what matters. */
9593   if (at1->dw_attr == DW_AT_producer)
9594     return 1;
9595
9596   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9597 }
9598
9599 /* Do the dies look the same?  */
9600
9601 static int
9602 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9603 {
9604   dw_die_ref c1, c2;
9605   dw_attr_ref a1;
9606   unsigned ix;
9607
9608   /* To avoid infinite recursion.  */
9609   if (die1->die_mark)
9610     return die1->die_mark == die2->die_mark;
9611   die1->die_mark = die2->die_mark = ++(*mark);
9612
9613   if (die1->die_tag != die2->die_tag)
9614     return 0;
9615
9616   if (VEC_length (dw_attr_node, die1->die_attr)
9617       != VEC_length (dw_attr_node, die2->die_attr))
9618     return 0;
9619
9620   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9621     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9622       return 0;
9623
9624   c1 = die1->die_child;
9625   c2 = die2->die_child;
9626   if (! c1)
9627     {
9628       if (c2)
9629         return 0;
9630     }
9631   else
9632     for (;;)
9633       {
9634         if (!same_die_p (c1, c2, mark))
9635           return 0;
9636         c1 = c1->die_sib;
9637         c2 = c2->die_sib;
9638         if (c1 == die1->die_child)
9639           {
9640             if (c2 == die2->die_child)
9641               break;
9642             else
9643               return 0;
9644           }
9645     }
9646
9647   return 1;
9648 }
9649
9650 /* Do the dies look the same?  Wrapper around same_die_p.  */
9651
9652 static int
9653 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9654 {
9655   int mark = 0;
9656   int ret = same_die_p (die1, die2, &mark);
9657
9658   unmark_all_dies (die1);
9659   unmark_all_dies (die2);
9660
9661   return ret;
9662 }
9663
9664 /* The prefix to attach to symbols on DIEs in the current comdat debug
9665    info section.  */
9666 static char *comdat_symbol_id;
9667
9668 /* The index of the current symbol within the current comdat CU.  */
9669 static unsigned int comdat_symbol_number;
9670
9671 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9672    children, and set comdat_symbol_id accordingly.  */
9673
9674 static void
9675 compute_section_prefix (dw_die_ref unit_die)
9676 {
9677   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9678   const char *base = die_name ? lbasename (die_name) : "anonymous";
9679   char *name = XALLOCAVEC (char, strlen (base) + 64);
9680   char *p;
9681   int i, mark;
9682   unsigned char checksum[16];
9683   struct md5_ctx ctx;
9684
9685   /* Compute the checksum of the DIE, then append part of it as hex digits to
9686      the name filename of the unit.  */
9687
9688   md5_init_ctx (&ctx);
9689   mark = 0;
9690   die_checksum (unit_die, &ctx, &mark);
9691   unmark_all_dies (unit_die);
9692   md5_finish_ctx (&ctx, checksum);
9693
9694   sprintf (name, "%s.", base);
9695   clean_symbol_name (name);
9696
9697   p = name + strlen (name);
9698   for (i = 0; i < 4; i++)
9699     {
9700       sprintf (p, "%.2x", checksum[i]);
9701       p += 2;
9702     }
9703
9704   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9705   comdat_symbol_number = 0;
9706 }
9707
9708 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9709
9710 static int
9711 is_type_die (dw_die_ref die)
9712 {
9713   switch (die->die_tag)
9714     {
9715     case DW_TAG_array_type:
9716     case DW_TAG_class_type:
9717     case DW_TAG_interface_type:
9718     case DW_TAG_enumeration_type:
9719     case DW_TAG_pointer_type:
9720     case DW_TAG_reference_type:
9721     case DW_TAG_rvalue_reference_type:
9722     case DW_TAG_string_type:
9723     case DW_TAG_structure_type:
9724     case DW_TAG_subroutine_type:
9725     case DW_TAG_union_type:
9726     case DW_TAG_ptr_to_member_type:
9727     case DW_TAG_set_type:
9728     case DW_TAG_subrange_type:
9729     case DW_TAG_base_type:
9730     case DW_TAG_const_type:
9731     case DW_TAG_file_type:
9732     case DW_TAG_packed_type:
9733     case DW_TAG_volatile_type:
9734     case DW_TAG_typedef:
9735       return 1;
9736     default:
9737       return 0;
9738     }
9739 }
9740
9741 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9742    Basically, we want to choose the bits that are likely to be shared between
9743    compilations (types) and leave out the bits that are specific to individual
9744    compilations (functions).  */
9745
9746 static int
9747 is_comdat_die (dw_die_ref c)
9748 {
9749   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9750      we do for stabs.  The advantage is a greater likelihood of sharing between
9751      objects that don't include headers in the same order (and therefore would
9752      put the base types in a different comdat).  jason 8/28/00 */
9753
9754   if (c->die_tag == DW_TAG_base_type)
9755     return 0;
9756
9757   if (c->die_tag == DW_TAG_pointer_type
9758       || c->die_tag == DW_TAG_reference_type
9759       || c->die_tag == DW_TAG_rvalue_reference_type
9760       || c->die_tag == DW_TAG_const_type
9761       || c->die_tag == DW_TAG_volatile_type)
9762     {
9763       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9764
9765       return t ? is_comdat_die (t) : 0;
9766     }
9767
9768   return is_type_die (c);
9769 }
9770
9771 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9772    compilation unit.  */
9773
9774 static int
9775 is_symbol_die (dw_die_ref c)
9776 {
9777   return (is_type_die (c)
9778           || is_declaration_die (c)
9779           || c->die_tag == DW_TAG_namespace
9780           || c->die_tag == DW_TAG_module);
9781 }
9782
9783 /* Returns true iff C is a compile-unit DIE.  */
9784
9785 static inline bool
9786 is_cu_die (dw_die_ref c)
9787 {
9788   return c && c->die_tag == DW_TAG_compile_unit;
9789 }
9790
9791 static char *
9792 gen_internal_sym (const char *prefix)
9793 {
9794   char buf[256];
9795
9796   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9797   return xstrdup (buf);
9798 }
9799
9800 /* Assign symbols to all worthy DIEs under DIE.  */
9801
9802 static void
9803 assign_symbol_names (dw_die_ref die)
9804 {
9805   dw_die_ref c;
9806
9807   if (is_symbol_die (die))
9808     {
9809       if (comdat_symbol_id)
9810         {
9811           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9812
9813           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9814                    comdat_symbol_id, comdat_symbol_number++);
9815           die->die_id.die_symbol = xstrdup (p);
9816         }
9817       else
9818         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9819     }
9820
9821   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9822 }
9823
9824 struct cu_hash_table_entry
9825 {
9826   dw_die_ref cu;
9827   unsigned min_comdat_num, max_comdat_num;
9828   struct cu_hash_table_entry *next;
9829 };
9830
9831 /* Routines to manipulate hash table of CUs.  */
9832 static hashval_t
9833 htab_cu_hash (const void *of)
9834 {
9835   const struct cu_hash_table_entry *const entry =
9836     (const struct cu_hash_table_entry *) of;
9837
9838   return htab_hash_string (entry->cu->die_id.die_symbol);
9839 }
9840
9841 static int
9842 htab_cu_eq (const void *of1, const void *of2)
9843 {
9844   const struct cu_hash_table_entry *const entry1 =
9845     (const struct cu_hash_table_entry *) of1;
9846   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9847
9848   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9849 }
9850
9851 static void
9852 htab_cu_del (void *what)
9853 {
9854   struct cu_hash_table_entry *next,
9855     *entry = (struct cu_hash_table_entry *) what;
9856
9857   while (entry)
9858     {
9859       next = entry->next;
9860       free (entry);
9861       entry = next;
9862     }
9863 }
9864
9865 /* Check whether we have already seen this CU and set up SYM_NUM
9866    accordingly.  */
9867 static int
9868 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9869 {
9870   struct cu_hash_table_entry dummy;
9871   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9872
9873   dummy.max_comdat_num = 0;
9874
9875   slot = (struct cu_hash_table_entry **)
9876     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9877         INSERT);
9878   entry = *slot;
9879
9880   for (; entry; last = entry, entry = entry->next)
9881     {
9882       if (same_die_p_wrap (cu, entry->cu))
9883         break;
9884     }
9885
9886   if (entry)
9887     {
9888       *sym_num = entry->min_comdat_num;
9889       return 1;
9890     }
9891
9892   entry = XCNEW (struct cu_hash_table_entry);
9893   entry->cu = cu;
9894   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9895   entry->next = *slot;
9896   *slot = entry;
9897
9898   return 0;
9899 }
9900
9901 /* Record SYM_NUM to record of CU in HTABLE.  */
9902 static void
9903 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9904 {
9905   struct cu_hash_table_entry **slot, *entry;
9906
9907   slot = (struct cu_hash_table_entry **)
9908     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9909         NO_INSERT);
9910   entry = *slot;
9911
9912   entry->max_comdat_num = sym_num;
9913 }
9914
9915 /* Traverse the DIE (which is always comp_unit_die), and set up
9916    additional compilation units for each of the include files we see
9917    bracketed by BINCL/EINCL.  */
9918
9919 static void
9920 break_out_includes (dw_die_ref die)
9921 {
9922   dw_die_ref c;
9923   dw_die_ref unit = NULL;
9924   limbo_die_node *node, **pnode;
9925   htab_t cu_hash_table;
9926
9927   c = die->die_child;
9928   if (c) do {
9929     dw_die_ref prev = c;
9930     c = c->die_sib;
9931     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9932            || (unit && is_comdat_die (c)))
9933       {
9934         dw_die_ref next = c->die_sib;
9935
9936         /* This DIE is for a secondary CU; remove it from the main one.  */
9937         remove_child_with_prev (c, prev);
9938
9939         if (c->die_tag == DW_TAG_GNU_BINCL)
9940           unit = push_new_compile_unit (unit, c);
9941         else if (c->die_tag == DW_TAG_GNU_EINCL)
9942           unit = pop_compile_unit (unit);
9943         else
9944           add_child_die (unit, c);
9945         c = next;
9946         if (c == die->die_child)
9947           break;
9948       }
9949   } while (c != die->die_child);
9950
9951 #if 0
9952   /* We can only use this in debugging, since the frontend doesn't check
9953      to make sure that we leave every include file we enter.  */
9954   gcc_assert (!unit);
9955 #endif
9956
9957   assign_symbol_names (die);
9958   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9959   for (node = limbo_die_list, pnode = &limbo_die_list;
9960        node;
9961        node = node->next)
9962     {
9963       int is_dupl;
9964
9965       compute_section_prefix (node->die);
9966       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9967                         &comdat_symbol_number);
9968       assign_symbol_names (node->die);
9969       if (is_dupl)
9970         *pnode = node->next;
9971       else
9972         {
9973           pnode = &node->next;
9974           record_comdat_symbol_number (node->die, cu_hash_table,
9975                 comdat_symbol_number);
9976         }
9977     }
9978   htab_delete (cu_hash_table);
9979 }
9980
9981 /* Return non-zero if this DIE is a declaration.  */
9982
9983 static int
9984 is_declaration_die (dw_die_ref die)
9985 {
9986   dw_attr_ref a;
9987   unsigned ix;
9988
9989   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9990     if (a->dw_attr == DW_AT_declaration)
9991       return 1;
9992
9993   return 0;
9994 }
9995
9996 /* Return non-zero if this DIE is nested inside a subprogram.  */
9997
9998 static int
9999 is_nested_in_subprogram (dw_die_ref die)
10000 {
10001   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10002
10003   if (decl == NULL)
10004     decl = die;
10005   return local_scope_p (decl);
10006 }
10007
10008 /* Return non-zero if this is a type DIE that should be moved to a
10009    COMDAT .debug_types section.  */
10010
10011 static int
10012 should_move_die_to_comdat (dw_die_ref die)
10013 {
10014   switch (die->die_tag)
10015     {
10016     case DW_TAG_class_type:
10017     case DW_TAG_structure_type:
10018     case DW_TAG_enumeration_type:
10019     case DW_TAG_union_type:
10020       /* Don't move declarations, inlined instances, or types nested in a
10021          subprogram.  */
10022       if (is_declaration_die (die)
10023           || get_AT (die, DW_AT_abstract_origin)
10024           || is_nested_in_subprogram (die))
10025         return 0;
10026       return 1;
10027     case DW_TAG_array_type:
10028     case DW_TAG_interface_type:
10029     case DW_TAG_pointer_type:
10030     case DW_TAG_reference_type:
10031     case DW_TAG_rvalue_reference_type:
10032     case DW_TAG_string_type:
10033     case DW_TAG_subroutine_type:
10034     case DW_TAG_ptr_to_member_type:
10035     case DW_TAG_set_type:
10036     case DW_TAG_subrange_type:
10037     case DW_TAG_base_type:
10038     case DW_TAG_const_type:
10039     case DW_TAG_file_type:
10040     case DW_TAG_packed_type:
10041     case DW_TAG_volatile_type:
10042     case DW_TAG_typedef:
10043     default:
10044       return 0;
10045     }
10046 }
10047
10048 /* Make a clone of DIE.  */
10049
10050 static dw_die_ref
10051 clone_die (dw_die_ref die)
10052 {
10053   dw_die_ref clone;
10054   dw_attr_ref a;
10055   unsigned ix;
10056
10057   clone = ggc_alloc_cleared_die_node ();
10058   clone->die_tag = die->die_tag;
10059
10060   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10061     add_dwarf_attr (clone, a);
10062
10063   return clone;
10064 }
10065
10066 /* Make a clone of the tree rooted at DIE.  */
10067
10068 static dw_die_ref
10069 clone_tree (dw_die_ref die)
10070 {
10071   dw_die_ref c;
10072   dw_die_ref clone = clone_die (die);
10073
10074   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10075
10076   return clone;
10077 }
10078
10079 /* Make a clone of DIE as a declaration.  */
10080
10081 static dw_die_ref
10082 clone_as_declaration (dw_die_ref die)
10083 {
10084   dw_die_ref clone;
10085   dw_die_ref decl;
10086   dw_attr_ref a;
10087   unsigned ix;
10088
10089   /* If the DIE is already a declaration, just clone it.  */
10090   if (is_declaration_die (die))
10091     return clone_die (die);
10092
10093   /* If the DIE is a specification, just clone its declaration DIE.  */
10094   decl = get_AT_ref (die, DW_AT_specification);
10095   if (decl != NULL)
10096     return clone_die (decl);
10097
10098   clone = ggc_alloc_cleared_die_node ();
10099   clone->die_tag = die->die_tag;
10100
10101   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10102     {
10103       /* We don't want to copy over all attributes.
10104          For example we don't want DW_AT_byte_size because otherwise we will no
10105          longer have a declaration and GDB will treat it as a definition.  */
10106
10107       switch (a->dw_attr)
10108         {
10109         case DW_AT_artificial:
10110         case DW_AT_containing_type:
10111         case DW_AT_external:
10112         case DW_AT_name:
10113         case DW_AT_type:
10114         case DW_AT_virtuality:
10115         case DW_AT_linkage_name:
10116         case DW_AT_MIPS_linkage_name:
10117           add_dwarf_attr (clone, a);
10118           break;
10119         case DW_AT_byte_size:
10120         default:
10121           break;
10122         }
10123     }
10124
10125   if (die->die_id.die_type_node)
10126     add_AT_die_ref (clone, DW_AT_signature, die);
10127
10128   add_AT_flag (clone, DW_AT_declaration, 1);
10129   return clone;
10130 }
10131
10132 /* Copy the declaration context to the new compile unit DIE.  This includes
10133    any surrounding namespace or type declarations.  If the DIE has an
10134    AT_specification attribute, it also includes attributes and children
10135    attached to the specification.  */
10136
10137 static void
10138 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10139 {
10140   dw_die_ref decl;
10141   dw_die_ref new_decl;
10142
10143   decl = get_AT_ref (die, DW_AT_specification);
10144   if (decl == NULL)
10145     decl = die;
10146   else
10147     {
10148       unsigned ix;
10149       dw_die_ref c;
10150       dw_attr_ref a;
10151
10152       /* Copy the type node pointer from the new DIE to the original
10153          declaration DIE so we can forward references later.  */
10154       decl->die_id.die_type_node = die->die_id.die_type_node;
10155
10156       remove_AT (die, DW_AT_specification);
10157
10158       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10159         {
10160           if (a->dw_attr != DW_AT_name
10161               && a->dw_attr != DW_AT_declaration
10162               && a->dw_attr != DW_AT_external)
10163             add_dwarf_attr (die, a);
10164         }
10165
10166       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10167     }
10168
10169   if (decl->die_parent != NULL
10170       && decl->die_parent->die_tag != DW_TAG_compile_unit
10171       && decl->die_parent->die_tag != DW_TAG_type_unit)
10172     {
10173       new_decl = copy_ancestor_tree (unit, decl, NULL);
10174       if (new_decl != NULL)
10175         {
10176           remove_AT (new_decl, DW_AT_signature);
10177           add_AT_specification (die, new_decl);
10178         }
10179     }
10180 }
10181
10182 /* Generate the skeleton ancestor tree for the given NODE, then clone
10183    the DIE and add the clone into the tree.  */
10184
10185 static void
10186 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10187 {
10188   if (node->new_die != NULL)
10189     return;
10190
10191   node->new_die = clone_as_declaration (node->old_die);
10192
10193   if (node->parent != NULL)
10194     {
10195       generate_skeleton_ancestor_tree (node->parent);
10196       add_child_die (node->parent->new_die, node->new_die);
10197     }
10198 }
10199
10200 /* Generate a skeleton tree of DIEs containing any declarations that are
10201    found in the original tree.  We traverse the tree looking for declaration
10202    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10203
10204 static void
10205 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10206 {
10207   skeleton_chain_node node;
10208   dw_die_ref c;
10209   dw_die_ref first;
10210   dw_die_ref prev = NULL;
10211   dw_die_ref next = NULL;
10212
10213   node.parent = parent;
10214
10215   first = c = parent->old_die->die_child;
10216   if (c)
10217     next = c->die_sib;
10218   if (c) do {
10219     if (prev == NULL || prev->die_sib == c)
10220       prev = c;
10221     c = next;
10222     next = (c == first ? NULL : c->die_sib);
10223     node.old_die = c;
10224     node.new_die = NULL;
10225     if (is_declaration_die (c))
10226       {
10227         /* Clone the existing DIE, move the original to the skeleton
10228            tree (which is in the main CU), and put the clone, with
10229            all the original's children, where the original came from.  */
10230         dw_die_ref clone = clone_die (c);
10231         move_all_children (c, clone);
10232
10233         replace_child (c, clone, prev);
10234         generate_skeleton_ancestor_tree (parent);
10235         add_child_die (parent->new_die, c);
10236         node.new_die = c;
10237         c = clone;
10238       }
10239     generate_skeleton_bottom_up (&node);
10240   } while (next != NULL);
10241 }
10242
10243 /* Wrapper function for generate_skeleton_bottom_up.  */
10244
10245 static dw_die_ref
10246 generate_skeleton (dw_die_ref die)
10247 {
10248   skeleton_chain_node node;
10249
10250   node.old_die = die;
10251   node.new_die = NULL;
10252   node.parent = NULL;
10253
10254   /* If this type definition is nested inside another type,
10255      always leave at least a declaration in its place.  */
10256   if (die->die_parent != NULL && is_type_die (die->die_parent))
10257     node.new_die = clone_as_declaration (die);
10258
10259   generate_skeleton_bottom_up (&node);
10260   return node.new_die;
10261 }
10262
10263 /* Remove the DIE from its parent, possibly replacing it with a cloned
10264    declaration.  The original DIE will be moved to a new compile unit
10265    so that existing references to it follow it to the new location.  If
10266    any of the original DIE's descendants is a declaration, we need to
10267    replace the original DIE with a skeleton tree and move the
10268    declarations back into the skeleton tree.  */
10269
10270 static dw_die_ref
10271 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10272 {
10273   dw_die_ref skeleton;
10274
10275   skeleton = generate_skeleton (child);
10276   if (skeleton == NULL)
10277     remove_child_with_prev (child, prev);
10278   else
10279     {
10280       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10281       replace_child (child, skeleton, prev);
10282     }
10283
10284   return skeleton;
10285 }
10286
10287 /* Traverse the DIE and set up additional .debug_types sections for each
10288    type worthy of being placed in a COMDAT section.  */
10289
10290 static void
10291 break_out_comdat_types (dw_die_ref die)
10292 {
10293   dw_die_ref c;
10294   dw_die_ref first;
10295   dw_die_ref prev = NULL;
10296   dw_die_ref next = NULL;
10297   dw_die_ref unit = NULL;
10298
10299   first = c = die->die_child;
10300   if (c)
10301     next = c->die_sib;
10302   if (c) do {
10303     if (prev == NULL || prev->die_sib == c)
10304       prev = c;
10305     c = next;
10306     next = (c == first ? NULL : c->die_sib);
10307     if (should_move_die_to_comdat (c))
10308       {
10309         dw_die_ref replacement;
10310         comdat_type_node_ref type_node;
10311
10312         /* Create a new type unit DIE as the root for the new tree, and
10313            add it to the list of comdat types.  */
10314         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10315         add_AT_unsigned (unit, DW_AT_language,
10316                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10317         type_node = ggc_alloc_cleared_comdat_type_node ();
10318         type_node->root_die = unit;
10319         type_node->next = comdat_type_list;
10320         comdat_type_list = type_node;
10321
10322         /* Generate the type signature.  */
10323         generate_type_signature (c, type_node);
10324
10325         /* Copy the declaration context, attributes, and children of the
10326            declaration into the new compile unit DIE.  */
10327         copy_declaration_context (unit, c);
10328
10329         /* Remove this DIE from the main CU.  */
10330         replacement = remove_child_or_replace_with_skeleton (c, prev);
10331
10332         /* Break out nested types into their own type units.  */
10333         break_out_comdat_types (c);
10334
10335         /* Add the DIE to the new compunit.  */
10336         add_child_die (unit, c);
10337
10338         if (replacement != NULL)
10339           c = replacement;
10340       }
10341     else if (c->die_tag == DW_TAG_namespace
10342              || c->die_tag == DW_TAG_class_type
10343              || c->die_tag == DW_TAG_structure_type
10344              || c->die_tag == DW_TAG_union_type)
10345       {
10346         /* Look for nested types that can be broken out.  */
10347         break_out_comdat_types (c);
10348       }
10349   } while (next != NULL);
10350 }
10351
10352 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10353
10354 struct decl_table_entry
10355 {
10356   dw_die_ref orig;
10357   dw_die_ref copy;
10358 };
10359
10360 /* Routines to manipulate hash table of copied declarations.  */
10361
10362 static hashval_t
10363 htab_decl_hash (const void *of)
10364 {
10365   const struct decl_table_entry *const entry =
10366     (const struct decl_table_entry *) of;
10367
10368   return htab_hash_pointer (entry->orig);
10369 }
10370
10371 static int
10372 htab_decl_eq (const void *of1, const void *of2)
10373 {
10374   const struct decl_table_entry *const entry1 =
10375     (const struct decl_table_entry *) of1;
10376   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10377
10378   return entry1->orig == entry2;
10379 }
10380
10381 static void
10382 htab_decl_del (void *what)
10383 {
10384   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10385
10386   free (entry);
10387 }
10388
10389 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10390    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10391    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10392    to check if the ancestor has already been copied into UNIT.  */
10393
10394 static dw_die_ref
10395 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10396 {
10397   dw_die_ref parent = die->die_parent;
10398   dw_die_ref new_parent = unit;
10399   dw_die_ref copy;
10400   void **slot = NULL;
10401   struct decl_table_entry *entry = NULL;
10402
10403   if (decl_table)
10404     {
10405       /* Check if the entry has already been copied to UNIT.  */
10406       slot = htab_find_slot_with_hash (decl_table, die,
10407                                        htab_hash_pointer (die), INSERT);
10408       if (*slot != HTAB_EMPTY_ENTRY)
10409         {
10410           entry = (struct decl_table_entry *) *slot;
10411           return entry->copy;
10412         }
10413
10414       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10415       entry = XCNEW (struct decl_table_entry);
10416       entry->orig = die;
10417       entry->copy = NULL;
10418       *slot = entry;
10419     }
10420
10421   if (parent != NULL)
10422     {
10423       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10424       if (spec != NULL)
10425         parent = spec;
10426       if (parent->die_tag != DW_TAG_compile_unit
10427           && parent->die_tag != DW_TAG_type_unit)
10428         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10429     }
10430
10431   copy = clone_as_declaration (die);
10432   add_child_die (new_parent, copy);
10433
10434   if (decl_table != NULL)
10435     {
10436       /* Record the pointer to the copy.  */
10437       entry->copy = copy;
10438     }
10439
10440   return copy;
10441 }
10442
10443 /* Walk the DIE and its children, looking for references to incomplete
10444    or trivial types that are unmarked (i.e., that are not in the current
10445    type_unit).  */
10446
10447 static void
10448 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10449 {
10450   dw_die_ref c;
10451   dw_attr_ref a;
10452   unsigned ix;
10453
10454   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10455     {
10456       if (AT_class (a) == dw_val_class_die_ref)
10457         {
10458           dw_die_ref targ = AT_ref (a);
10459           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10460           void **slot;
10461           struct decl_table_entry *entry;
10462
10463           if (targ->die_mark != 0 || type_node != NULL)
10464             continue;
10465
10466           slot = htab_find_slot_with_hash (decl_table, targ,
10467                                            htab_hash_pointer (targ), INSERT);
10468
10469           if (*slot != HTAB_EMPTY_ENTRY)
10470             {
10471               /* TARG has already been copied, so we just need to
10472                  modify the reference to point to the copy.  */
10473               entry = (struct decl_table_entry *) *slot;
10474               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10475             }
10476           else
10477             {
10478               dw_die_ref parent = unit;
10479               dw_die_ref copy = clone_tree (targ);
10480
10481               /* Make sure the cloned tree is marked as part of the
10482                  type unit.  */
10483               mark_dies (copy);
10484
10485               /* Record in DECL_TABLE that TARG has been copied.
10486                  Need to do this now, before the recursive call,
10487                  because DECL_TABLE may be expanded and SLOT
10488                  would no longer be a valid pointer.  */
10489               entry = XCNEW (struct decl_table_entry);
10490               entry->orig = targ;
10491               entry->copy = copy;
10492               *slot = entry;
10493
10494               /* If TARG has surrounding context, copy its ancestor tree
10495                  into the new type unit.  */
10496               if (targ->die_parent != NULL
10497                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10498                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10499                 parent = copy_ancestor_tree (unit, targ->die_parent,
10500                                              decl_table);
10501
10502               add_child_die (parent, copy);
10503               a->dw_attr_val.v.val_die_ref.die = copy;
10504
10505               /* Make sure the newly-copied DIE is walked.  If it was
10506                  installed in a previously-added context, it won't
10507                  get visited otherwise.  */
10508               if (parent != unit)
10509                 {
10510                   /* Find the highest point of the newly-added tree,
10511                      mark each node along the way, and walk from there.  */
10512                   parent->die_mark = 1;
10513                   while (parent->die_parent
10514                          && parent->die_parent->die_mark == 0)
10515                     {
10516                       parent = parent->die_parent;
10517                       parent->die_mark = 1;
10518                     }
10519                   copy_decls_walk (unit, parent, decl_table);
10520                 }
10521             }
10522         }
10523     }
10524
10525   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10526 }
10527
10528 /* Copy declarations for "unworthy" types into the new comdat section.
10529    Incomplete types, modified types, and certain other types aren't broken
10530    out into comdat sections of their own, so they don't have a signature,
10531    and we need to copy the declaration into the same section so that we
10532    don't have an external reference.  */
10533
10534 static void
10535 copy_decls_for_unworthy_types (dw_die_ref unit)
10536 {
10537   htab_t decl_table;
10538
10539   mark_dies (unit);
10540   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10541   copy_decls_walk (unit, unit, decl_table);
10542   htab_delete (decl_table);
10543   unmark_dies (unit);
10544 }
10545
10546 /* Traverse the DIE and add a sibling attribute if it may have the
10547    effect of speeding up access to siblings.  To save some space,
10548    avoid generating sibling attributes for DIE's without children.  */
10549
10550 static void
10551 add_sibling_attributes (dw_die_ref die)
10552 {
10553   dw_die_ref c;
10554
10555   if (! die->die_child)
10556     return;
10557
10558   if (die->die_parent && die != die->die_parent->die_child)
10559     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10560
10561   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10562 }
10563
10564 /* Output all location lists for the DIE and its children.  */
10565
10566 static void
10567 output_location_lists (dw_die_ref die)
10568 {
10569   dw_die_ref c;
10570   dw_attr_ref a;
10571   unsigned ix;
10572
10573   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10574     if (AT_class (a) == dw_val_class_loc_list)
10575       output_loc_list (AT_loc_list (a));
10576
10577   FOR_EACH_CHILD (die, c, output_location_lists (c));
10578 }
10579
10580 /* The format of each DIE (and its attribute value pairs) is encoded in an
10581    abbreviation table.  This routine builds the abbreviation table and assigns
10582    a unique abbreviation id for each abbreviation entry.  The children of each
10583    die are visited recursively.  */
10584
10585 static void
10586 build_abbrev_table (dw_die_ref die)
10587 {
10588   unsigned long abbrev_id;
10589   unsigned int n_alloc;
10590   dw_die_ref c;
10591   dw_attr_ref a;
10592   unsigned ix;
10593
10594   /* Scan the DIE references, and mark as external any that refer to
10595      DIEs from other CUs (i.e. those which are not marked).  */
10596   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10597     if (AT_class (a) == dw_val_class_die_ref
10598         && AT_ref (a)->die_mark == 0)
10599       {
10600         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10601         set_AT_ref_external (a, 1);
10602       }
10603
10604   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10605     {
10606       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10607       dw_attr_ref die_a, abbrev_a;
10608       unsigned ix;
10609       bool ok = true;
10610
10611       if (abbrev->die_tag != die->die_tag)
10612         continue;
10613       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10614         continue;
10615
10616       if (VEC_length (dw_attr_node, abbrev->die_attr)
10617           != VEC_length (dw_attr_node, die->die_attr))
10618         continue;
10619
10620       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10621         {
10622           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10623           if ((abbrev_a->dw_attr != die_a->dw_attr)
10624               || (value_format (abbrev_a) != value_format (die_a)))
10625             {
10626               ok = false;
10627               break;
10628             }
10629         }
10630       if (ok)
10631         break;
10632     }
10633
10634   if (abbrev_id >= abbrev_die_table_in_use)
10635     {
10636       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10637         {
10638           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10639           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10640                                             n_alloc);
10641
10642           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10643                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10644           abbrev_die_table_allocated = n_alloc;
10645         }
10646
10647       ++abbrev_die_table_in_use;
10648       abbrev_die_table[abbrev_id] = die;
10649     }
10650
10651   die->die_abbrev = abbrev_id;
10652   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10653 }
10654 \f
10655 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10656
10657 static int
10658 constant_size (unsigned HOST_WIDE_INT value)
10659 {
10660   int log;
10661
10662   if (value == 0)
10663     log = 0;
10664   else
10665     log = floor_log2 (value);
10666
10667   log = log / 8;
10668   log = 1 << (floor_log2 (log) + 1);
10669
10670   return log;
10671 }
10672
10673 /* Return the size of a DIE as it is represented in the
10674    .debug_info section.  */
10675
10676 static unsigned long
10677 size_of_die (dw_die_ref die)
10678 {
10679   unsigned long size = 0;
10680   dw_attr_ref a;
10681   unsigned ix;
10682
10683   size += size_of_uleb128 (die->die_abbrev);
10684   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10685     {
10686       switch (AT_class (a))
10687         {
10688         case dw_val_class_addr:
10689           size += DWARF2_ADDR_SIZE;
10690           break;
10691         case dw_val_class_offset:
10692           size += DWARF_OFFSET_SIZE;
10693           break;
10694         case dw_val_class_loc:
10695           {
10696             unsigned long lsize = size_of_locs (AT_loc (a));
10697
10698             /* Block length.  */
10699             if (dwarf_version >= 4)
10700               size += size_of_uleb128 (lsize);
10701             else
10702               size += constant_size (lsize);
10703             size += lsize;
10704           }
10705           break;
10706         case dw_val_class_loc_list:
10707           size += DWARF_OFFSET_SIZE;
10708           break;
10709         case dw_val_class_range_list:
10710           size += DWARF_OFFSET_SIZE;
10711           break;
10712         case dw_val_class_const:
10713           size += size_of_sleb128 (AT_int (a));
10714           break;
10715         case dw_val_class_unsigned_const:
10716           size += constant_size (AT_unsigned (a));
10717           break;
10718         case dw_val_class_const_double:
10719           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10720           if (HOST_BITS_PER_WIDE_INT >= 64)
10721             size++; /* block */
10722           break;
10723         case dw_val_class_vec:
10724           size += constant_size (a->dw_attr_val.v.val_vec.length
10725                                  * a->dw_attr_val.v.val_vec.elt_size)
10726                   + a->dw_attr_val.v.val_vec.length
10727                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10728           break;
10729         case dw_val_class_flag:
10730           if (dwarf_version >= 4)
10731             /* Currently all add_AT_flag calls pass in 1 as last argument,
10732                so DW_FORM_flag_present can be used.  If that ever changes,
10733                we'll need to use DW_FORM_flag and have some optimization
10734                in build_abbrev_table that will change those to
10735                DW_FORM_flag_present if it is set to 1 in all DIEs using
10736                the same abbrev entry.  */
10737             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10738           else
10739             size += 1;
10740           break;
10741         case dw_val_class_die_ref:
10742           if (AT_ref_external (a))
10743             {
10744               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10745                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10746                  is sized by target address length, whereas in DWARF3
10747                  it's always sized as an offset.  */
10748               if (use_debug_types)
10749                 size += DWARF_TYPE_SIGNATURE_SIZE;
10750               else if (dwarf_version == 2)
10751                 size += DWARF2_ADDR_SIZE;
10752               else
10753                 size += DWARF_OFFSET_SIZE;
10754             }
10755           else
10756             size += DWARF_OFFSET_SIZE;
10757           break;
10758         case dw_val_class_fde_ref:
10759           size += DWARF_OFFSET_SIZE;
10760           break;
10761         case dw_val_class_lbl_id:
10762           size += DWARF2_ADDR_SIZE;
10763           break;
10764         case dw_val_class_lineptr:
10765         case dw_val_class_macptr:
10766           size += DWARF_OFFSET_SIZE;
10767           break;
10768         case dw_val_class_str:
10769           if (AT_string_form (a) == DW_FORM_strp)
10770             size += DWARF_OFFSET_SIZE;
10771           else
10772             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10773           break;
10774         case dw_val_class_file:
10775           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10776           break;
10777         case dw_val_class_data8:
10778           size += 8;
10779           break;
10780         case dw_val_class_vms_delta:
10781           size += DWARF_OFFSET_SIZE;
10782           break;
10783         default:
10784           gcc_unreachable ();
10785         }
10786     }
10787
10788   return size;
10789 }
10790
10791 /* Size the debugging information associated with a given DIE.  Visits the
10792    DIE's children recursively.  Updates the global variable next_die_offset, on
10793    each time through.  Uses the current value of next_die_offset to update the
10794    die_offset field in each DIE.  */
10795
10796 static void
10797 calc_die_sizes (dw_die_ref die)
10798 {
10799   dw_die_ref c;
10800
10801   die->die_offset = next_die_offset;
10802   next_die_offset += size_of_die (die);
10803
10804   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10805
10806   if (die->die_child != NULL)
10807     /* Count the null byte used to terminate sibling lists.  */
10808     next_die_offset += 1;
10809 }
10810
10811 /* Set the marks for a die and its children.  We do this so
10812    that we know whether or not a reference needs to use FORM_ref_addr; only
10813    DIEs in the same CU will be marked.  We used to clear out the offset
10814    and use that as the flag, but ran into ordering problems.  */
10815
10816 static void
10817 mark_dies (dw_die_ref die)
10818 {
10819   dw_die_ref c;
10820
10821   gcc_assert (!die->die_mark);
10822
10823   die->die_mark = 1;
10824   FOR_EACH_CHILD (die, c, mark_dies (c));
10825 }
10826
10827 /* Clear the marks for a die and its children.  */
10828
10829 static void
10830 unmark_dies (dw_die_ref die)
10831 {
10832   dw_die_ref c;
10833
10834   if (! use_debug_types)
10835     gcc_assert (die->die_mark);
10836
10837   die->die_mark = 0;
10838   FOR_EACH_CHILD (die, c, unmark_dies (c));
10839 }
10840
10841 /* Clear the marks for a die, its children and referred dies.  */
10842
10843 static void
10844 unmark_all_dies (dw_die_ref die)
10845 {
10846   dw_die_ref c;
10847   dw_attr_ref a;
10848   unsigned ix;
10849
10850   if (!die->die_mark)
10851     return;
10852   die->die_mark = 0;
10853
10854   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10855
10856   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10857     if (AT_class (a) == dw_val_class_die_ref)
10858       unmark_all_dies (AT_ref (a));
10859 }
10860
10861 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10862    generated for the compilation unit.  */
10863
10864 static unsigned long
10865 size_of_pubnames (VEC (pubname_entry, gc) * names)
10866 {
10867   unsigned long size;
10868   unsigned i;
10869   pubname_ref p;
10870
10871   size = DWARF_PUBNAMES_HEADER_SIZE;
10872   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10873     if (names != pubtype_table
10874         || p->die->die_offset != 0
10875         || !flag_eliminate_unused_debug_types)
10876       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10877
10878   size += DWARF_OFFSET_SIZE;
10879   return size;
10880 }
10881
10882 /* Return the size of the information in the .debug_aranges section.  */
10883
10884 static unsigned long
10885 size_of_aranges (void)
10886 {
10887   unsigned long size;
10888
10889   size = DWARF_ARANGES_HEADER_SIZE;
10890
10891   /* Count the address/length pair for this compilation unit.  */
10892   if (text_section_used)
10893     size += 2 * DWARF2_ADDR_SIZE;
10894   if (cold_text_section_used)
10895     size += 2 * DWARF2_ADDR_SIZE;
10896   if (have_multiple_function_sections)
10897     {
10898       unsigned fde_idx = 0;
10899
10900       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
10901         {
10902           dw_fde_ref fde = &fde_table[fde_idx];
10903
10904           if (!fde->in_std_section)
10905             size += 2 * DWARF2_ADDR_SIZE;
10906           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10907             size += 2 * DWARF2_ADDR_SIZE;
10908         }
10909     }
10910
10911   /* Count the two zero words used to terminated the address range table.  */
10912   size += 2 * DWARF2_ADDR_SIZE;
10913   return size;
10914 }
10915 \f
10916 /* Select the encoding of an attribute value.  */
10917
10918 static enum dwarf_form
10919 value_format (dw_attr_ref a)
10920 {
10921   switch (a->dw_attr_val.val_class)
10922     {
10923     case dw_val_class_addr:
10924       /* Only very few attributes allow DW_FORM_addr.  */
10925       switch (a->dw_attr)
10926         {
10927         case DW_AT_low_pc:
10928         case DW_AT_high_pc:
10929         case DW_AT_entry_pc:
10930         case DW_AT_trampoline:
10931           return DW_FORM_addr;
10932         default:
10933           break;
10934         }
10935       switch (DWARF2_ADDR_SIZE)
10936         {
10937         case 1:
10938           return DW_FORM_data1;
10939         case 2:
10940           return DW_FORM_data2;
10941         case 4:
10942           return DW_FORM_data4;
10943         case 8:
10944           return DW_FORM_data8;
10945         default:
10946           gcc_unreachable ();
10947         }
10948     case dw_val_class_range_list:
10949     case dw_val_class_loc_list:
10950       if (dwarf_version >= 4)
10951         return DW_FORM_sec_offset;
10952       /* FALLTHRU */
10953     case dw_val_class_vms_delta:
10954     case dw_val_class_offset:
10955       switch (DWARF_OFFSET_SIZE)
10956         {
10957         case 4:
10958           return DW_FORM_data4;
10959         case 8:
10960           return DW_FORM_data8;
10961         default:
10962           gcc_unreachable ();
10963         }
10964     case dw_val_class_loc:
10965       if (dwarf_version >= 4)
10966         return DW_FORM_exprloc;
10967       switch (constant_size (size_of_locs (AT_loc (a))))
10968         {
10969         case 1:
10970           return DW_FORM_block1;
10971         case 2:
10972           return DW_FORM_block2;
10973         default:
10974           gcc_unreachable ();
10975         }
10976     case dw_val_class_const:
10977       return DW_FORM_sdata;
10978     case dw_val_class_unsigned_const:
10979       switch (constant_size (AT_unsigned (a)))
10980         {
10981         case 1:
10982           return DW_FORM_data1;
10983         case 2:
10984           return DW_FORM_data2;
10985         case 4:
10986           return DW_FORM_data4;
10987         case 8:
10988           return DW_FORM_data8;
10989         default:
10990           gcc_unreachable ();
10991         }
10992     case dw_val_class_const_double:
10993       switch (HOST_BITS_PER_WIDE_INT)
10994         {
10995         case 8:
10996           return DW_FORM_data2;
10997         case 16:
10998           return DW_FORM_data4;
10999         case 32:
11000           return DW_FORM_data8;
11001         case 64:
11002         default:
11003           return DW_FORM_block1;
11004         }
11005     case dw_val_class_vec:
11006       switch (constant_size (a->dw_attr_val.v.val_vec.length
11007                              * a->dw_attr_val.v.val_vec.elt_size))
11008         {
11009         case 1:
11010           return DW_FORM_block1;
11011         case 2:
11012           return DW_FORM_block2;
11013         case 4:
11014           return DW_FORM_block4;
11015         default:
11016           gcc_unreachable ();
11017         }
11018     case dw_val_class_flag:
11019       if (dwarf_version >= 4)
11020         {
11021           /* Currently all add_AT_flag calls pass in 1 as last argument,
11022              so DW_FORM_flag_present can be used.  If that ever changes,
11023              we'll need to use DW_FORM_flag and have some optimization
11024              in build_abbrev_table that will change those to
11025              DW_FORM_flag_present if it is set to 1 in all DIEs using
11026              the same abbrev entry.  */
11027           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11028           return DW_FORM_flag_present;
11029         }
11030       return DW_FORM_flag;
11031     case dw_val_class_die_ref:
11032       if (AT_ref_external (a))
11033         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11034       else
11035         return DW_FORM_ref;
11036     case dw_val_class_fde_ref:
11037       return DW_FORM_data;
11038     case dw_val_class_lbl_id:
11039       return DW_FORM_addr;
11040     case dw_val_class_lineptr:
11041     case dw_val_class_macptr:
11042       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11043     case dw_val_class_str:
11044       return AT_string_form (a);
11045     case dw_val_class_file:
11046       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11047         {
11048         case 1:
11049           return DW_FORM_data1;
11050         case 2:
11051           return DW_FORM_data2;
11052         case 4:
11053           return DW_FORM_data4;
11054         default:
11055           gcc_unreachable ();
11056         }
11057
11058     case dw_val_class_data8:
11059       return DW_FORM_data8;
11060
11061     default:
11062       gcc_unreachable ();
11063     }
11064 }
11065
11066 /* Output the encoding of an attribute value.  */
11067
11068 static void
11069 output_value_format (dw_attr_ref a)
11070 {
11071   enum dwarf_form form = value_format (a);
11072
11073   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11074 }
11075
11076 /* Output the .debug_abbrev section which defines the DIE abbreviation
11077    table.  */
11078
11079 static void
11080 output_abbrev_section (void)
11081 {
11082   unsigned long abbrev_id;
11083
11084   if (abbrev_die_table_in_use == 1)
11085     return;
11086
11087   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11088     {
11089       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11090       unsigned ix;
11091       dw_attr_ref a_attr;
11092
11093       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11094       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11095                                    dwarf_tag_name (abbrev->die_tag));
11096
11097       if (abbrev->die_child != NULL)
11098         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11099       else
11100         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11101
11102       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11103            ix++)
11104         {
11105           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11106                                        dwarf_attr_name (a_attr->dw_attr));
11107           output_value_format (a_attr);
11108         }
11109
11110       dw2_asm_output_data (1, 0, NULL);
11111       dw2_asm_output_data (1, 0, NULL);
11112     }
11113
11114   /* Terminate the table.  */
11115   dw2_asm_output_data (1, 0, NULL);
11116 }
11117
11118 /* Output a symbol we can use to refer to this DIE from another CU.  */
11119
11120 static inline void
11121 output_die_symbol (dw_die_ref die)
11122 {
11123   char *sym = die->die_id.die_symbol;
11124
11125   if (sym == 0)
11126     return;
11127
11128   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11129     /* We make these global, not weak; if the target doesn't support
11130        .linkonce, it doesn't support combining the sections, so debugging
11131        will break.  */
11132     targetm.asm_out.globalize_label (asm_out_file, sym);
11133
11134   ASM_OUTPUT_LABEL (asm_out_file, sym);
11135 }
11136
11137 /* Return a new location list, given the begin and end range, and the
11138    expression.  */
11139
11140 static inline dw_loc_list_ref
11141 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11142               const char *section)
11143 {
11144   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11145
11146   retlist->begin = begin;
11147   retlist->end = end;
11148   retlist->expr = expr;
11149   retlist->section = section;
11150
11151   return retlist;
11152 }
11153
11154 /* Generate a new internal symbol for this location list node, if it
11155    hasn't got one yet.  */
11156
11157 static inline void
11158 gen_llsym (dw_loc_list_ref list)
11159 {
11160   gcc_assert (!list->ll_symbol);
11161   list->ll_symbol = gen_internal_sym ("LLST");
11162 }
11163
11164 /* Output the location list given to us.  */
11165
11166 static void
11167 output_loc_list (dw_loc_list_ref list_head)
11168 {
11169   dw_loc_list_ref curr = list_head;
11170
11171   if (list_head->emitted)
11172     return;
11173   list_head->emitted = true;
11174
11175   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11176
11177   /* Walk the location list, and output each range + expression.  */
11178   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11179     {
11180       unsigned long size;
11181       /* Don't output an entry that starts and ends at the same address.  */
11182       if (strcmp (curr->begin, curr->end) == 0)
11183         continue;
11184       if (!have_multiple_function_sections)
11185         {
11186           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11187                                 "Location list begin address (%s)",
11188                                 list_head->ll_symbol);
11189           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11190                                 "Location list end address (%s)",
11191                                 list_head->ll_symbol);
11192         }
11193       else
11194         {
11195           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11196                                "Location list begin address (%s)",
11197                                list_head->ll_symbol);
11198           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11199                                "Location list end address (%s)",
11200                                list_head->ll_symbol);
11201         }
11202       size = size_of_locs (curr->expr);
11203
11204       /* Output the block length for this list of location operations.  */
11205       gcc_assert (size <= 0xffff);
11206       dw2_asm_output_data (2, size, "%s", "Location expression size");
11207
11208       output_loc_sequence (curr->expr, -1);
11209     }
11210
11211   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11212                        "Location list terminator begin (%s)",
11213                        list_head->ll_symbol);
11214   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11215                        "Location list terminator end (%s)",
11216                        list_head->ll_symbol);
11217 }
11218
11219 /* Output a type signature.  */
11220
11221 static inline void
11222 output_signature (const char *sig, const char *name)
11223 {
11224   int i;
11225
11226   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11227     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11228 }
11229
11230 /* Output the DIE and its attributes.  Called recursively to generate
11231    the definitions of each child DIE.  */
11232
11233 static void
11234 output_die (dw_die_ref die)
11235 {
11236   dw_attr_ref a;
11237   dw_die_ref c;
11238   unsigned long size;
11239   unsigned ix;
11240
11241   /* If someone in another CU might refer to us, set up a symbol for
11242      them to point to.  */
11243   if (! use_debug_types && die->die_id.die_symbol)
11244     output_die_symbol (die);
11245
11246   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11247                                (unsigned long)die->die_offset,
11248                                dwarf_tag_name (die->die_tag));
11249
11250   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11251     {
11252       const char *name = dwarf_attr_name (a->dw_attr);
11253
11254       switch (AT_class (a))
11255         {
11256         case dw_val_class_addr:
11257           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11258           break;
11259
11260         case dw_val_class_offset:
11261           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11262                                "%s", name);
11263           break;
11264
11265         case dw_val_class_range_list:
11266           {
11267             char *p = strchr (ranges_section_label, '\0');
11268
11269             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11270                      a->dw_attr_val.v.val_offset);
11271             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11272                                    debug_ranges_section, "%s", name);
11273             *p = '\0';
11274           }
11275           break;
11276
11277         case dw_val_class_loc:
11278           size = size_of_locs (AT_loc (a));
11279
11280           /* Output the block length for this list of location operations.  */
11281           if (dwarf_version >= 4)
11282             dw2_asm_output_data_uleb128 (size, "%s", name);
11283           else
11284             dw2_asm_output_data (constant_size (size), size, "%s", name);
11285
11286           output_loc_sequence (AT_loc (a), -1);
11287           break;
11288
11289         case dw_val_class_const:
11290           /* ??? It would be slightly more efficient to use a scheme like is
11291              used for unsigned constants below, but gdb 4.x does not sign
11292              extend.  Gdb 5.x does sign extend.  */
11293           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11294           break;
11295
11296         case dw_val_class_unsigned_const:
11297           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11298                                AT_unsigned (a), "%s", name);
11299           break;
11300
11301         case dw_val_class_const_double:
11302           {
11303             unsigned HOST_WIDE_INT first, second;
11304
11305             if (HOST_BITS_PER_WIDE_INT >= 64)
11306               dw2_asm_output_data (1,
11307                                    2 * HOST_BITS_PER_WIDE_INT
11308                                    / HOST_BITS_PER_CHAR,
11309                                    NULL);
11310
11311             if (WORDS_BIG_ENDIAN)
11312               {
11313                 first = a->dw_attr_val.v.val_double.high;
11314                 second = a->dw_attr_val.v.val_double.low;
11315               }
11316             else
11317               {
11318                 first = a->dw_attr_val.v.val_double.low;
11319                 second = a->dw_attr_val.v.val_double.high;
11320               }
11321
11322             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11323                                  first, name);
11324             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11325                                  second, NULL);
11326           }
11327           break;
11328
11329         case dw_val_class_vec:
11330           {
11331             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11332             unsigned int len = a->dw_attr_val.v.val_vec.length;
11333             unsigned int i;
11334             unsigned char *p;
11335
11336             dw2_asm_output_data (constant_size (len * elt_size),
11337                                  len * elt_size, "%s", name);
11338             if (elt_size > sizeof (HOST_WIDE_INT))
11339               {
11340                 elt_size /= 2;
11341                 len *= 2;
11342               }
11343             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11344                  i < len;
11345                  i++, p += elt_size)
11346               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11347                                    "fp or vector constant word %u", i);
11348             break;
11349           }
11350
11351         case dw_val_class_flag:
11352           if (dwarf_version >= 4)
11353             {
11354               /* Currently all add_AT_flag calls pass in 1 as last argument,
11355                  so DW_FORM_flag_present can be used.  If that ever changes,
11356                  we'll need to use DW_FORM_flag and have some optimization
11357                  in build_abbrev_table that will change those to
11358                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11359                  the same abbrev entry.  */
11360               gcc_assert (AT_flag (a) == 1);
11361               if (flag_debug_asm)
11362                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11363                          ASM_COMMENT_START, name);
11364               break;
11365             }
11366           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11367           break;
11368
11369         case dw_val_class_loc_list:
11370           {
11371             char *sym = AT_loc_list (a)->ll_symbol;
11372
11373             gcc_assert (sym);
11374             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11375                                    "%s", name);
11376           }
11377           break;
11378
11379         case dw_val_class_die_ref:
11380           if (AT_ref_external (a))
11381             {
11382               if (use_debug_types)
11383                 {
11384                   comdat_type_node_ref type_node =
11385                     AT_ref (a)->die_id.die_type_node;
11386
11387                   gcc_assert (type_node);
11388                   output_signature (type_node->signature, name);
11389                 }
11390               else
11391                 {
11392                   char *sym = AT_ref (a)->die_id.die_symbol;
11393                   int size;
11394
11395                   gcc_assert (sym);
11396                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11397                      length, whereas in DWARF3 it's always sized as an
11398                      offset.  */
11399                   if (dwarf_version == 2)
11400                     size = DWARF2_ADDR_SIZE;
11401                   else
11402                     size = DWARF_OFFSET_SIZE;
11403                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11404                                          name);
11405                 }
11406             }
11407           else
11408             {
11409               gcc_assert (AT_ref (a)->die_offset);
11410               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11411                                    "%s", name);
11412             }
11413           break;
11414
11415         case dw_val_class_fde_ref:
11416           {
11417             char l1[20];
11418
11419             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11420                                          a->dw_attr_val.v.val_fde_index * 2);
11421             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11422                                    "%s", name);
11423           }
11424           break;
11425
11426         case dw_val_class_vms_delta:
11427           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11428                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11429                                     "%s", name);
11430           break;
11431
11432         case dw_val_class_lbl_id:
11433           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11434           break;
11435
11436         case dw_val_class_lineptr:
11437           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11438                                  debug_line_section, "%s", name);
11439           break;
11440
11441         case dw_val_class_macptr:
11442           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11443                                  debug_macinfo_section, "%s", name);
11444           break;
11445
11446         case dw_val_class_str:
11447           if (AT_string_form (a) == DW_FORM_strp)
11448             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11449                                    a->dw_attr_val.v.val_str->label,
11450                                    debug_str_section,
11451                                    "%s: \"%s\"", name, AT_string (a));
11452           else
11453             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11454           break;
11455
11456         case dw_val_class_file:
11457           {
11458             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11459
11460             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11461                                  a->dw_attr_val.v.val_file->filename);
11462             break;
11463           }
11464
11465         case dw_val_class_data8:
11466           {
11467             int i;
11468
11469             for (i = 0; i < 8; i++)
11470               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11471                                    i == 0 ? "%s" : NULL, name);
11472             break;
11473           }
11474
11475         default:
11476           gcc_unreachable ();
11477         }
11478     }
11479
11480   FOR_EACH_CHILD (die, c, output_die (c));
11481
11482   /* Add null byte to terminate sibling list.  */
11483   if (die->die_child != NULL)
11484     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11485                          (unsigned long) die->die_offset);
11486 }
11487
11488 /* Output the compilation unit that appears at the beginning of the
11489    .debug_info section, and precedes the DIE descriptions.  */
11490
11491 static void
11492 output_compilation_unit_header (void)
11493 {
11494   int ver = dwarf_version;
11495
11496   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11497     dw2_asm_output_data (4, 0xffffffff,
11498       "Initial length escape value indicating 64-bit DWARF extension");
11499   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11500                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11501                        "Length of Compilation Unit Info");
11502   dw2_asm_output_data (2, ver, "DWARF version number");
11503   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11504                          debug_abbrev_section,
11505                          "Offset Into Abbrev. Section");
11506   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11507 }
11508
11509 /* Output the compilation unit DIE and its children.  */
11510
11511 static void
11512 output_comp_unit (dw_die_ref die, int output_if_empty)
11513 {
11514   const char *secname;
11515   char *oldsym, *tmp;
11516
11517   /* Unless we are outputting main CU, we may throw away empty ones.  */
11518   if (!output_if_empty && die->die_child == NULL)
11519     return;
11520
11521   /* Even if there are no children of this DIE, we must output the information
11522      about the compilation unit.  Otherwise, on an empty translation unit, we
11523      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11524      will then complain when examining the file.  First mark all the DIEs in
11525      this CU so we know which get local refs.  */
11526   mark_dies (die);
11527
11528   build_abbrev_table (die);
11529
11530   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11531   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11532   calc_die_sizes (die);
11533
11534   oldsym = die->die_id.die_symbol;
11535   if (oldsym)
11536     {
11537       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11538
11539       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11540       secname = tmp;
11541       die->die_id.die_symbol = NULL;
11542       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11543     }
11544   else
11545     {
11546       switch_to_section (debug_info_section);
11547       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11548       info_section_emitted = true;
11549     }
11550
11551   /* Output debugging information.  */
11552   output_compilation_unit_header ();
11553   output_die (die);
11554
11555   /* Leave the marks on the main CU, so we can check them in
11556      output_pubnames.  */
11557   if (oldsym)
11558     {
11559       unmark_dies (die);
11560       die->die_id.die_symbol = oldsym;
11561     }
11562 }
11563
11564 /* Output a comdat type unit DIE and its children.  */
11565
11566 static void
11567 output_comdat_type_unit (comdat_type_node *node)
11568 {
11569   const char *secname;
11570   char *tmp;
11571   int i;
11572 #if defined (OBJECT_FORMAT_ELF)
11573   tree comdat_key;
11574 #endif
11575
11576   /* First mark all the DIEs in this CU so we know which get local refs.  */
11577   mark_dies (node->root_die);
11578
11579   build_abbrev_table (node->root_die);
11580
11581   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11582   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11583   calc_die_sizes (node->root_die);
11584
11585 #if defined (OBJECT_FORMAT_ELF)
11586   secname = ".debug_types";
11587   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11588   sprintf (tmp, "wt.");
11589   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11590     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11591   comdat_key = get_identifier (tmp);
11592   targetm.asm_out.named_section (secname,
11593                                  SECTION_DEBUG | SECTION_LINKONCE,
11594                                  comdat_key);
11595 #else
11596   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11597   sprintf (tmp, ".gnu.linkonce.wt.");
11598   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11599     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11600   secname = tmp;
11601   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11602 #endif
11603
11604   /* Output debugging information.  */
11605   output_compilation_unit_header ();
11606   output_signature (node->signature, "Type Signature");
11607   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11608                        "Offset to Type DIE");
11609   output_die (node->root_die);
11610
11611   unmark_dies (node->root_die);
11612 }
11613
11614 /* Return the DWARF2/3 pubname associated with a decl.  */
11615
11616 static const char *
11617 dwarf2_name (tree decl, int scope)
11618 {
11619   if (DECL_NAMELESS (decl))
11620     return NULL;
11621   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11622 }
11623
11624 /* Add a new entry to .debug_pubnames if appropriate.  */
11625
11626 static void
11627 add_pubname_string (const char *str, dw_die_ref die)
11628 {
11629   if (targetm.want_debug_pub_sections)
11630     {
11631       pubname_entry e;
11632
11633       e.die = die;
11634       e.name = xstrdup (str);
11635       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11636     }
11637 }
11638
11639 static void
11640 add_pubname (tree decl, dw_die_ref die)
11641 {
11642   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11643     {
11644       const char *name = dwarf2_name (decl, 1);
11645       if (name)
11646         add_pubname_string (name, die);
11647     }
11648 }
11649
11650 /* Add a new entry to .debug_pubtypes if appropriate.  */
11651
11652 static void
11653 add_pubtype (tree decl, dw_die_ref die)
11654 {
11655   pubname_entry e;
11656
11657   if (!targetm.want_debug_pub_sections)
11658     return;
11659
11660   e.name = NULL;
11661   if ((TREE_PUBLIC (decl)
11662        || is_cu_die (die->die_parent))
11663       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11664     {
11665       e.die = die;
11666       if (TYPE_P (decl))
11667         {
11668           if (TYPE_NAME (decl))
11669             {
11670               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11671                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11672               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11673                        && DECL_NAME (TYPE_NAME (decl)))
11674                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11675               else
11676                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11677             }
11678         }
11679       else
11680         {
11681           e.name = dwarf2_name (decl, 1);
11682           if (e.name)
11683             e.name = xstrdup (e.name);
11684         }
11685
11686       /* If we don't have a name for the type, there's no point in adding
11687          it to the table.  */
11688       if (e.name && e.name[0] != '\0')
11689         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11690     }
11691 }
11692
11693 /* Output the public names table used to speed up access to externally
11694    visible names; or the public types table used to find type definitions.  */
11695
11696 static void
11697 output_pubnames (VEC (pubname_entry, gc) * names)
11698 {
11699   unsigned i;
11700   unsigned long pubnames_length = size_of_pubnames (names);
11701   pubname_ref pub;
11702
11703   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11704     dw2_asm_output_data (4, 0xffffffff,
11705       "Initial length escape value indicating 64-bit DWARF extension");
11706   if (names == pubname_table)
11707     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11708                          "Length of Public Names Info");
11709   else
11710     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11711                          "Length of Public Type Names Info");
11712   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11713   dw2_asm_output_data (2, 2, "DWARF Version");
11714   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11715                          debug_info_section,
11716                          "Offset of Compilation Unit Info");
11717   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11718                        "Compilation Unit Length");
11719
11720   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11721     {
11722       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11723       if (names == pubname_table)
11724         gcc_assert (pub->die->die_mark);
11725
11726       if (names != pubtype_table
11727           || pub->die->die_offset != 0
11728           || !flag_eliminate_unused_debug_types)
11729         {
11730           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11731                                "DIE offset");
11732
11733           dw2_asm_output_nstring (pub->name, -1, "external name");
11734         }
11735     }
11736
11737   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11738 }
11739
11740 /* Output the information that goes into the .debug_aranges table.
11741    Namely, define the beginning and ending address range of the
11742    text section generated for this compilation unit.  */
11743
11744 static void
11745 output_aranges (unsigned long aranges_length)
11746 {
11747   unsigned i;
11748
11749   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11750     dw2_asm_output_data (4, 0xffffffff,
11751       "Initial length escape value indicating 64-bit DWARF extension");
11752   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11753                        "Length of Address Ranges Info");
11754   /* Version number for aranges is still 2, even in DWARF3.  */
11755   dw2_asm_output_data (2, 2, "DWARF Version");
11756   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11757                          debug_info_section,
11758                          "Offset of Compilation Unit Info");
11759   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11760   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11761
11762   /* We need to align to twice the pointer size here.  */
11763   if (DWARF_ARANGES_PAD_SIZE)
11764     {
11765       /* Pad using a 2 byte words so that padding is correct for any
11766          pointer size.  */
11767       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11768                            2 * DWARF2_ADDR_SIZE);
11769       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11770         dw2_asm_output_data (2, 0, NULL);
11771     }
11772
11773   /* It is necessary not to output these entries if the sections were
11774      not used; if the sections were not used, the length will be 0 and
11775      the address may end up as 0 if the section is discarded by ld
11776      --gc-sections, leaving an invalid (0, 0) entry that can be
11777      confused with the terminator.  */
11778   if (text_section_used)
11779     {
11780       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11781       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11782                             text_section_label, "Length");
11783     }
11784   if (cold_text_section_used)
11785     {
11786       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11787                            "Address");
11788       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11789                             cold_text_section_label, "Length");
11790     }
11791
11792   if (have_multiple_function_sections)
11793     {
11794       unsigned fde_idx = 0;
11795
11796       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11797         {
11798           dw_fde_ref fde = &fde_table[fde_idx];
11799
11800           if (!fde->in_std_section)
11801             {
11802               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11803                                    "Address");
11804               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11805                                     fde->dw_fde_begin, "Length");
11806             }
11807           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11808             {
11809               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11810                                    "Address");
11811               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11812                                     fde->dw_fde_second_begin, "Length");
11813             }
11814         }
11815     }
11816
11817   /* Output the terminator words.  */
11818   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11819   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11820 }
11821
11822 /* Add a new entry to .debug_ranges.  Return the offset at which it
11823    was placed.  */
11824
11825 static unsigned int
11826 add_ranges_num (int num)
11827 {
11828   unsigned int in_use = ranges_table_in_use;
11829
11830   if (in_use == ranges_table_allocated)
11831     {
11832       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11833       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11834                                     ranges_table_allocated);
11835       memset (ranges_table + ranges_table_in_use, 0,
11836               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11837     }
11838
11839   ranges_table[in_use].num = num;
11840   ranges_table_in_use = in_use + 1;
11841
11842   return in_use * 2 * DWARF2_ADDR_SIZE;
11843 }
11844
11845 /* Add a new entry to .debug_ranges corresponding to a block, or a
11846    range terminator if BLOCK is NULL.  */
11847
11848 static unsigned int
11849 add_ranges (const_tree block)
11850 {
11851   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11852 }
11853
11854 /* Add a new entry to .debug_ranges corresponding to a pair of
11855    labels.  */
11856
11857 static void
11858 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11859                       bool *added)
11860 {
11861   unsigned int in_use = ranges_by_label_in_use;
11862   unsigned int offset;
11863
11864   if (in_use == ranges_by_label_allocated)
11865     {
11866       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11867       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11868                                        ranges_by_label,
11869                                        ranges_by_label_allocated);
11870       memset (ranges_by_label + ranges_by_label_in_use, 0,
11871               RANGES_TABLE_INCREMENT
11872               * sizeof (struct dw_ranges_by_label_struct));
11873     }
11874
11875   ranges_by_label[in_use].begin = begin;
11876   ranges_by_label[in_use].end = end;
11877   ranges_by_label_in_use = in_use + 1;
11878
11879   offset = add_ranges_num (-(int)in_use - 1);
11880   if (!*added)
11881     {
11882       add_AT_range_list (die, DW_AT_ranges, offset);
11883       *added = true;
11884     }
11885 }
11886
11887 static void
11888 output_ranges (void)
11889 {
11890   unsigned i;
11891   static const char *const start_fmt = "Offset %#x";
11892   const char *fmt = start_fmt;
11893
11894   for (i = 0; i < ranges_table_in_use; i++)
11895     {
11896       int block_num = ranges_table[i].num;
11897
11898       if (block_num > 0)
11899         {
11900           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11901           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11902
11903           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11904           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11905
11906           /* If all code is in the text section, then the compilation
11907              unit base address defaults to DW_AT_low_pc, which is the
11908              base of the text section.  */
11909           if (!have_multiple_function_sections)
11910             {
11911               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11912                                     text_section_label,
11913                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11914               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11915                                     text_section_label, NULL);
11916             }
11917
11918           /* Otherwise, the compilation unit base address is zero,
11919              which allows us to use absolute addresses, and not worry
11920              about whether the target supports cross-section
11921              arithmetic.  */
11922           else
11923             {
11924               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11925                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11926               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11927             }
11928
11929           fmt = NULL;
11930         }
11931
11932       /* Negative block_num stands for an index into ranges_by_label.  */
11933       else if (block_num < 0)
11934         {
11935           int lab_idx = - block_num - 1;
11936
11937           if (!have_multiple_function_sections)
11938             {
11939               gcc_unreachable ();
11940 #if 0
11941               /* If we ever use add_ranges_by_labels () for a single
11942                  function section, all we have to do is to take out
11943                  the #if 0 above.  */
11944               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11945                                     ranges_by_label[lab_idx].begin,
11946                                     text_section_label,
11947                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11948               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11949                                     ranges_by_label[lab_idx].end,
11950                                     text_section_label, NULL);
11951 #endif
11952             }
11953           else
11954             {
11955               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11956                                    ranges_by_label[lab_idx].begin,
11957                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11958               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11959                                    ranges_by_label[lab_idx].end,
11960                                    NULL);
11961             }
11962         }
11963       else
11964         {
11965           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11966           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11967           fmt = start_fmt;
11968         }
11969     }
11970 }
11971
11972 /* Data structure containing information about input files.  */
11973 struct file_info
11974 {
11975   const char *path;     /* Complete file name.  */
11976   const char *fname;    /* File name part.  */
11977   int length;           /* Length of entire string.  */
11978   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11979   int dir_idx;          /* Index in directory table.  */
11980 };
11981
11982 /* Data structure containing information about directories with source
11983    files.  */
11984 struct dir_info
11985 {
11986   const char *path;     /* Path including directory name.  */
11987   int length;           /* Path length.  */
11988   int prefix;           /* Index of directory entry which is a prefix.  */
11989   int count;            /* Number of files in this directory.  */
11990   int dir_idx;          /* Index of directory used as base.  */
11991 };
11992
11993 /* Callback function for file_info comparison.  We sort by looking at
11994    the directories in the path.  */
11995
11996 static int
11997 file_info_cmp (const void *p1, const void *p2)
11998 {
11999   const struct file_info *const s1 = (const struct file_info *) p1;
12000   const struct file_info *const s2 = (const struct file_info *) p2;
12001   const unsigned char *cp1;
12002   const unsigned char *cp2;
12003
12004   /* Take care of file names without directories.  We need to make sure that
12005      we return consistent values to qsort since some will get confused if
12006      we return the same value when identical operands are passed in opposite
12007      orders.  So if neither has a directory, return 0 and otherwise return
12008      1 or -1 depending on which one has the directory.  */
12009   if ((s1->path == s1->fname || s2->path == s2->fname))
12010     return (s2->path == s2->fname) - (s1->path == s1->fname);
12011
12012   cp1 = (const unsigned char *) s1->path;
12013   cp2 = (const unsigned char *) s2->path;
12014
12015   while (1)
12016     {
12017       ++cp1;
12018       ++cp2;
12019       /* Reached the end of the first path?  If so, handle like above.  */
12020       if ((cp1 == (const unsigned char *) s1->fname)
12021           || (cp2 == (const unsigned char *) s2->fname))
12022         return ((cp2 == (const unsigned char *) s2->fname)
12023                 - (cp1 == (const unsigned char *) s1->fname));
12024
12025       /* Character of current path component the same?  */
12026       else if (*cp1 != *cp2)
12027         return *cp1 - *cp2;
12028     }
12029 }
12030
12031 struct file_name_acquire_data
12032 {
12033   struct file_info *files;
12034   int used_files;
12035   int max_files;
12036 };
12037
12038 /* Traversal function for the hash table.  */
12039
12040 static int
12041 file_name_acquire (void ** slot, void *data)
12042 {
12043   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12044   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12045   struct file_info *fi;
12046   const char *f;
12047
12048   gcc_assert (fnad->max_files >= d->emitted_number);
12049
12050   if (! d->emitted_number)
12051     return 1;
12052
12053   gcc_assert (fnad->max_files != fnad->used_files);
12054
12055   fi = fnad->files + fnad->used_files++;
12056
12057   /* Skip all leading "./".  */
12058   f = d->filename;
12059   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12060     f += 2;
12061
12062   /* Create a new array entry.  */
12063   fi->path = f;
12064   fi->length = strlen (f);
12065   fi->file_idx = d;
12066
12067   /* Search for the file name part.  */
12068   f = strrchr (f, DIR_SEPARATOR);
12069 #if defined (DIR_SEPARATOR_2)
12070   {
12071     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12072
12073     if (g != NULL)
12074       {
12075         if (f == NULL || f < g)
12076           f = g;
12077       }
12078   }
12079 #endif
12080
12081   fi->fname = f == NULL ? fi->path : f + 1;
12082   return 1;
12083 }
12084
12085 /* Output the directory table and the file name table.  We try to minimize
12086    the total amount of memory needed.  A heuristic is used to avoid large
12087    slowdowns with many input files.  */
12088
12089 static void
12090 output_file_names (void)
12091 {
12092   struct file_name_acquire_data fnad;
12093   int numfiles;
12094   struct file_info *files;
12095   struct dir_info *dirs;
12096   int *saved;
12097   int *savehere;
12098   int *backmap;
12099   int ndirs;
12100   int idx_offset;
12101   int i;
12102
12103   if (!last_emitted_file)
12104     {
12105       dw2_asm_output_data (1, 0, "End directory table");
12106       dw2_asm_output_data (1, 0, "End file name table");
12107       return;
12108     }
12109
12110   numfiles = last_emitted_file->emitted_number;
12111
12112   /* Allocate the various arrays we need.  */
12113   files = XALLOCAVEC (struct file_info, numfiles);
12114   dirs = XALLOCAVEC (struct dir_info, numfiles);
12115
12116   fnad.files = files;
12117   fnad.used_files = 0;
12118   fnad.max_files = numfiles;
12119   htab_traverse (file_table, file_name_acquire, &fnad);
12120   gcc_assert (fnad.used_files == fnad.max_files);
12121
12122   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12123
12124   /* Find all the different directories used.  */
12125   dirs[0].path = files[0].path;
12126   dirs[0].length = files[0].fname - files[0].path;
12127   dirs[0].prefix = -1;
12128   dirs[0].count = 1;
12129   dirs[0].dir_idx = 0;
12130   files[0].dir_idx = 0;
12131   ndirs = 1;
12132
12133   for (i = 1; i < numfiles; i++)
12134     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12135         && memcmp (dirs[ndirs - 1].path, files[i].path,
12136                    dirs[ndirs - 1].length) == 0)
12137       {
12138         /* Same directory as last entry.  */
12139         files[i].dir_idx = ndirs - 1;
12140         ++dirs[ndirs - 1].count;
12141       }
12142     else
12143       {
12144         int j;
12145
12146         /* This is a new directory.  */
12147         dirs[ndirs].path = files[i].path;
12148         dirs[ndirs].length = files[i].fname - files[i].path;
12149         dirs[ndirs].count = 1;
12150         dirs[ndirs].dir_idx = ndirs;
12151         files[i].dir_idx = ndirs;
12152
12153         /* Search for a prefix.  */
12154         dirs[ndirs].prefix = -1;
12155         for (j = 0; j < ndirs; j++)
12156           if (dirs[j].length < dirs[ndirs].length
12157               && dirs[j].length > 1
12158               && (dirs[ndirs].prefix == -1
12159                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12160               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12161             dirs[ndirs].prefix = j;
12162
12163         ++ndirs;
12164       }
12165
12166   /* Now to the actual work.  We have to find a subset of the directories which
12167      allow expressing the file name using references to the directory table
12168      with the least amount of characters.  We do not do an exhaustive search
12169      where we would have to check out every combination of every single
12170      possible prefix.  Instead we use a heuristic which provides nearly optimal
12171      results in most cases and never is much off.  */
12172   saved = XALLOCAVEC (int, ndirs);
12173   savehere = XALLOCAVEC (int, ndirs);
12174
12175   memset (saved, '\0', ndirs * sizeof (saved[0]));
12176   for (i = 0; i < ndirs; i++)
12177     {
12178       int j;
12179       int total;
12180
12181       /* We can always save some space for the current directory.  But this
12182          does not mean it will be enough to justify adding the directory.  */
12183       savehere[i] = dirs[i].length;
12184       total = (savehere[i] - saved[i]) * dirs[i].count;
12185
12186       for (j = i + 1; j < ndirs; j++)
12187         {
12188           savehere[j] = 0;
12189           if (saved[j] < dirs[i].length)
12190             {
12191               /* Determine whether the dirs[i] path is a prefix of the
12192                  dirs[j] path.  */
12193               int k;
12194
12195               k = dirs[j].prefix;
12196               while (k != -1 && k != (int) i)
12197                 k = dirs[k].prefix;
12198
12199               if (k == (int) i)
12200                 {
12201                   /* Yes it is.  We can possibly save some memory by
12202                      writing the filenames in dirs[j] relative to
12203                      dirs[i].  */
12204                   savehere[j] = dirs[i].length;
12205                   total += (savehere[j] - saved[j]) * dirs[j].count;
12206                 }
12207             }
12208         }
12209
12210       /* Check whether we can save enough to justify adding the dirs[i]
12211          directory.  */
12212       if (total > dirs[i].length + 1)
12213         {
12214           /* It's worthwhile adding.  */
12215           for (j = i; j < ndirs; j++)
12216             if (savehere[j] > 0)
12217               {
12218                 /* Remember how much we saved for this directory so far.  */
12219                 saved[j] = savehere[j];
12220
12221                 /* Remember the prefix directory.  */
12222                 dirs[j].dir_idx = i;
12223               }
12224         }
12225     }
12226
12227   /* Emit the directory name table.  */
12228   idx_offset = dirs[0].length > 0 ? 1 : 0;
12229   for (i = 1 - idx_offset; i < ndirs; i++)
12230     dw2_asm_output_nstring (dirs[i].path,
12231                             dirs[i].length
12232                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12233                             "Directory Entry: %#x", i + idx_offset);
12234
12235   dw2_asm_output_data (1, 0, "End directory table");
12236
12237   /* We have to emit them in the order of emitted_number since that's
12238      used in the debug info generation.  To do this efficiently we
12239      generate a back-mapping of the indices first.  */
12240   backmap = XALLOCAVEC (int, numfiles);
12241   for (i = 0; i < numfiles; i++)
12242     backmap[files[i].file_idx->emitted_number - 1] = i;
12243
12244   /* Now write all the file names.  */
12245   for (i = 0; i < numfiles; i++)
12246     {
12247       int file_idx = backmap[i];
12248       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12249
12250 #ifdef VMS_DEBUGGING_INFO
12251 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12252
12253       /* Setting these fields can lead to debugger miscomparisons,
12254          but VMS Debug requires them to be set correctly.  */
12255
12256       int ver;
12257       long long cdt;
12258       long siz;
12259       int maxfilelen = strlen (files[file_idx].path)
12260                                + dirs[dir_idx].length
12261                                + MAX_VMS_VERSION_LEN + 1;
12262       char *filebuf = XALLOCAVEC (char, maxfilelen);
12263
12264       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12265       snprintf (filebuf, maxfilelen, "%s;%d",
12266                 files[file_idx].path + dirs[dir_idx].length, ver);
12267
12268       dw2_asm_output_nstring
12269         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12270
12271       /* Include directory index.  */
12272       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12273
12274       /* Modification time.  */
12275       dw2_asm_output_data_uleb128
12276         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12277           ? cdt : 0,
12278          NULL);
12279
12280       /* File length in bytes.  */
12281       dw2_asm_output_data_uleb128
12282         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12283           ? siz : 0,
12284          NULL);
12285 #else
12286       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12287                               "File Entry: %#x", (unsigned) i + 1);
12288
12289       /* Include directory index.  */
12290       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12291
12292       /* Modification time.  */
12293       dw2_asm_output_data_uleb128 (0, NULL);
12294
12295       /* File length in bytes.  */
12296       dw2_asm_output_data_uleb128 (0, NULL);
12297 #endif /* VMS_DEBUGGING_INFO */
12298     }
12299
12300   dw2_asm_output_data (1, 0, "End file name table");
12301 }
12302
12303
12304 /* Output one line number table into the .debug_line section.  */
12305
12306 static void
12307 output_one_line_info_table (dw_line_info_table *table)
12308 {
12309   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12310   unsigned int current_line = 1;
12311   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12312   dw_line_info_entry *ent;
12313   size_t i;
12314
12315   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12316     {
12317       switch (ent->opcode)
12318         {
12319         case LI_set_address:
12320           /* ??? Unfortunately, we have little choice here currently, and
12321              must always use the most general form.  GCC does not know the
12322              address delta itself, so we can't use DW_LNS_advance_pc.  Many
12323              ports do have length attributes which will give an upper bound
12324              on the address range.  We could perhaps use length attributes
12325              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12326           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12327
12328           /* This can handle any delta.  This takes
12329              4+DWARF2_ADDR_SIZE bytes.  */
12330           dw2_asm_output_data (1, 0, "set address %s", line_label);
12331           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12332           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12333           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12334           break;
12335
12336         case LI_set_line:
12337           if (ent->val == current_line)
12338             {
12339               /* We still need to start a new row, so output a copy insn.  */
12340               dw2_asm_output_data (1, DW_LNS_copy,
12341                                    "copy line %u", current_line);
12342             }
12343           else
12344             {
12345               int line_offset = ent->val - current_line;
12346               int line_delta = line_offset - DWARF_LINE_BASE;
12347
12348               current_line = ent->val;
12349               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12350                 {
12351                   /* This can handle deltas from -10 to 234, using the current
12352                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12353                      This takes 1 byte.  */
12354                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12355                                        "line %u", current_line);
12356                 }
12357               else
12358                 {
12359                   /* This can handle any delta.  This takes at least 4 bytes,
12360                      depending on the value being encoded.  */
12361                   dw2_asm_output_data (1, DW_LNS_advance_line,
12362                                        "advance to line %u", current_line);
12363                   dw2_asm_output_data_sleb128 (line_offset, NULL);
12364                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
12365                 }
12366             }
12367           break;
12368
12369         case LI_set_file:
12370           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12371           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12372           break;
12373
12374         case LI_set_column:
12375           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12376           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12377           break;
12378
12379         case LI_negate_stmt:
12380           current_is_stmt = !current_is_stmt;
12381           dw2_asm_output_data (1, DW_LNS_negate_stmt,
12382                                "is_stmt %d", current_is_stmt);
12383           break;
12384
12385         case LI_set_prologue_end:
12386           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12387                                "set prologue end");
12388           break;
12389           
12390         case LI_set_epilogue_begin:
12391           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12392                                "set epilogue begin");
12393           break;
12394
12395         case LI_set_discriminator:
12396           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12397           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12398           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12399           dw2_asm_output_data_uleb128 (ent->val, NULL);
12400           break;
12401         }
12402     }
12403
12404   /* Emit debug info for the address of the end of the table.  */
12405   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12406   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12407   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12408   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12409
12410   dw2_asm_output_data (1, 0, "end sequence");
12411   dw2_asm_output_data_uleb128 (1, NULL);
12412   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12413 }
12414
12415 /* Output the source line number correspondence information.  This
12416    information goes into the .debug_line section.  */
12417
12418 static void
12419 output_line_info (void)
12420 {
12421   char l1[20], l2[20], p1[20], p2[20];
12422   int ver = dwarf_version;
12423   bool saw_one = false;
12424   int opc;
12425
12426   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12427   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12428   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12429   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12430
12431   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12432     dw2_asm_output_data (4, 0xffffffff,
12433       "Initial length escape value indicating 64-bit DWARF extension");
12434   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12435                         "Length of Source Line Info");
12436   ASM_OUTPUT_LABEL (asm_out_file, l1);
12437
12438   dw2_asm_output_data (2, ver, "DWARF Version");
12439   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12440   ASM_OUTPUT_LABEL (asm_out_file, p1);
12441
12442   /* Define the architecture-dependent minimum instruction length (in bytes).
12443      In this implementation of DWARF, this field is used for information
12444      purposes only.  Since GCC generates assembly language, we have no
12445      a priori knowledge of how many instruction bytes are generated for each
12446      source line, and therefore can use only the DW_LNE_set_address and
12447      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12448      this as '1', which is "correct enough" for all architectures,
12449      and don't let the target override.  */
12450   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12451
12452   if (ver >= 4)
12453     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12454                          "Maximum Operations Per Instruction");
12455   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12456                        "Default is_stmt_start flag");
12457   dw2_asm_output_data (1, DWARF_LINE_BASE,
12458                        "Line Base Value (Special Opcodes)");
12459   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12460                        "Line Range Value (Special Opcodes)");
12461   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12462                        "Special Opcode Base");
12463
12464   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12465     {
12466       int n_op_args;
12467       switch (opc)
12468         {
12469         case DW_LNS_advance_pc:
12470         case DW_LNS_advance_line:
12471         case DW_LNS_set_file:
12472         case DW_LNS_set_column:
12473         case DW_LNS_fixed_advance_pc:
12474         case DW_LNS_set_isa:
12475           n_op_args = 1;
12476           break;
12477         default:
12478           n_op_args = 0;
12479           break;
12480         }
12481
12482       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12483                            opc, n_op_args);
12484     }
12485
12486   /* Write out the information about the files we use.  */
12487   output_file_names ();
12488   ASM_OUTPUT_LABEL (asm_out_file, p2);
12489
12490   if (separate_line_info)
12491     {
12492       dw_line_info_table *table;
12493       size_t i;
12494
12495       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12496         if (table->in_use)
12497           {
12498             output_one_line_info_table (table);
12499             saw_one = true;
12500           }
12501     }
12502   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12503     {
12504       output_one_line_info_table (cold_text_section_line_info);
12505       saw_one = true;
12506     }
12507
12508   /* ??? Some Darwin linkers crash on a .debug_line section with no
12509      sequences.  Further, merely a DW_LNE_end_sequence entry is not
12510      sufficient -- the address column must also be initialized.
12511      Make sure to output at least one set_address/end_sequence pair,
12512      choosing .text since that section is always present.  */
12513   if (text_section_line_info->in_use || !saw_one)
12514     output_one_line_info_table (text_section_line_info);
12515
12516   /* Output the marker for the end of the line number info.  */
12517   ASM_OUTPUT_LABEL (asm_out_file, l2);
12518 }
12519 \f
12520 /* Given a pointer to a tree node for some base type, return a pointer to
12521    a DIE that describes the given type.
12522
12523    This routine must only be called for GCC type nodes that correspond to
12524    Dwarf base (fundamental) types.  */
12525
12526 static dw_die_ref
12527 base_type_die (tree type)
12528 {
12529   dw_die_ref base_type_result;
12530   enum dwarf_type encoding;
12531
12532   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12533     return 0;
12534
12535   /* If this is a subtype that should not be emitted as a subrange type,
12536      use the base type.  See subrange_type_for_debug_p.  */
12537   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12538     type = TREE_TYPE (type);
12539
12540   switch (TREE_CODE (type))
12541     {
12542     case INTEGER_TYPE:
12543       if ((dwarf_version >= 4 || !dwarf_strict)
12544           && TYPE_NAME (type)
12545           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12546           && DECL_IS_BUILTIN (TYPE_NAME (type))
12547           && DECL_NAME (TYPE_NAME (type)))
12548         {
12549           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12550           if (strcmp (name, "char16_t") == 0
12551               || strcmp (name, "char32_t") == 0)
12552             {
12553               encoding = DW_ATE_UTF;
12554               break;
12555             }
12556         }
12557       if (TYPE_STRING_FLAG (type))
12558         {
12559           if (TYPE_UNSIGNED (type))
12560             encoding = DW_ATE_unsigned_char;
12561           else
12562             encoding = DW_ATE_signed_char;
12563         }
12564       else if (TYPE_UNSIGNED (type))
12565         encoding = DW_ATE_unsigned;
12566       else
12567         encoding = DW_ATE_signed;
12568       break;
12569
12570     case REAL_TYPE:
12571       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12572         {
12573           if (dwarf_version >= 3 || !dwarf_strict)
12574             encoding = DW_ATE_decimal_float;
12575           else
12576             encoding = DW_ATE_lo_user;
12577         }
12578       else
12579         encoding = DW_ATE_float;
12580       break;
12581
12582     case FIXED_POINT_TYPE:
12583       if (!(dwarf_version >= 3 || !dwarf_strict))
12584         encoding = DW_ATE_lo_user;
12585       else if (TYPE_UNSIGNED (type))
12586         encoding = DW_ATE_unsigned_fixed;
12587       else
12588         encoding = DW_ATE_signed_fixed;
12589       break;
12590
12591       /* Dwarf2 doesn't know anything about complex ints, so use
12592          a user defined type for it.  */
12593     case COMPLEX_TYPE:
12594       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12595         encoding = DW_ATE_complex_float;
12596       else
12597         encoding = DW_ATE_lo_user;
12598       break;
12599
12600     case BOOLEAN_TYPE:
12601       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12602       encoding = DW_ATE_boolean;
12603       break;
12604
12605     default:
12606       /* No other TREE_CODEs are Dwarf fundamental types.  */
12607       gcc_unreachable ();
12608     }
12609
12610   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12611
12612   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12613                    int_size_in_bytes (type));
12614   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12615
12616   return base_type_result;
12617 }
12618
12619 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12620    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12621
12622 static inline int
12623 is_base_type (tree type)
12624 {
12625   switch (TREE_CODE (type))
12626     {
12627     case ERROR_MARK:
12628     case VOID_TYPE:
12629     case INTEGER_TYPE:
12630     case REAL_TYPE:
12631     case FIXED_POINT_TYPE:
12632     case COMPLEX_TYPE:
12633     case BOOLEAN_TYPE:
12634       return 1;
12635
12636     case ARRAY_TYPE:
12637     case RECORD_TYPE:
12638     case UNION_TYPE:
12639     case QUAL_UNION_TYPE:
12640     case ENUMERAL_TYPE:
12641     case FUNCTION_TYPE:
12642     case METHOD_TYPE:
12643     case POINTER_TYPE:
12644     case REFERENCE_TYPE:
12645     case NULLPTR_TYPE:
12646     case OFFSET_TYPE:
12647     case LANG_TYPE:
12648     case VECTOR_TYPE:
12649       return 0;
12650
12651     default:
12652       gcc_unreachable ();
12653     }
12654
12655   return 0;
12656 }
12657
12658 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12659    node, return the size in bits for the type if it is a constant, or else
12660    return the alignment for the type if the type's size is not constant, or
12661    else return BITS_PER_WORD if the type actually turns out to be an
12662    ERROR_MARK node.  */
12663
12664 static inline unsigned HOST_WIDE_INT
12665 simple_type_size_in_bits (const_tree type)
12666 {
12667   if (TREE_CODE (type) == ERROR_MARK)
12668     return BITS_PER_WORD;
12669   else if (TYPE_SIZE (type) == NULL_TREE)
12670     return 0;
12671   else if (host_integerp (TYPE_SIZE (type), 1))
12672     return tree_low_cst (TYPE_SIZE (type), 1);
12673   else
12674     return TYPE_ALIGN (type);
12675 }
12676
12677 /* Similarly, but return a double_int instead of UHWI.  */
12678
12679 static inline double_int
12680 double_int_type_size_in_bits (const_tree type)
12681 {
12682   if (TREE_CODE (type) == ERROR_MARK)
12683     return uhwi_to_double_int (BITS_PER_WORD);
12684   else if (TYPE_SIZE (type) == NULL_TREE)
12685     return double_int_zero;
12686   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12687     return tree_to_double_int (TYPE_SIZE (type));
12688   else
12689     return uhwi_to_double_int (TYPE_ALIGN (type));
12690 }
12691
12692 /*  Given a pointer to a tree node for a subrange type, return a pointer
12693     to a DIE that describes the given type.  */
12694
12695 static dw_die_ref
12696 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12697 {
12698   dw_die_ref subrange_die;
12699   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12700
12701   if (context_die == NULL)
12702     context_die = comp_unit_die ();
12703
12704   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12705
12706   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12707     {
12708       /* The size of the subrange type and its base type do not match,
12709          so we need to generate a size attribute for the subrange type.  */
12710       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12711     }
12712
12713   if (low)
12714     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12715   if (high)
12716     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12717
12718   return subrange_die;
12719 }
12720
12721 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12722    entry that chains various modifiers in front of the given type.  */
12723
12724 static dw_die_ref
12725 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12726                    dw_die_ref context_die)
12727 {
12728   enum tree_code code = TREE_CODE (type);
12729   dw_die_ref mod_type_die;
12730   dw_die_ref sub_die = NULL;
12731   tree item_type = NULL;
12732   tree qualified_type;
12733   tree name, low, high;
12734
12735   if (code == ERROR_MARK)
12736     return NULL;
12737
12738   /* See if we already have the appropriately qualified variant of
12739      this type.  */
12740   qualified_type
12741     = get_qualified_type (type,
12742                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12743                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12744
12745   if (qualified_type == sizetype
12746       && TYPE_NAME (qualified_type)
12747       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12748     {
12749       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12750
12751       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12752                            && TYPE_PRECISION (t)
12753                            == TYPE_PRECISION (qualified_type)
12754                            && TYPE_UNSIGNED (t)
12755                            == TYPE_UNSIGNED (qualified_type));
12756       qualified_type = t;
12757     }
12758
12759   /* If we do, then we can just use its DIE, if it exists.  */
12760   if (qualified_type)
12761     {
12762       mod_type_die = lookup_type_die (qualified_type);
12763       if (mod_type_die)
12764         return mod_type_die;
12765     }
12766
12767   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12768
12769   /* Handle C typedef types.  */
12770   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12771       && !DECL_ARTIFICIAL (name))
12772     {
12773       tree dtype = TREE_TYPE (name);
12774
12775       if (qualified_type == dtype)
12776         {
12777           /* For a named type, use the typedef.  */
12778           gen_type_die (qualified_type, context_die);
12779           return lookup_type_die (qualified_type);
12780         }
12781       else if (is_const_type < TYPE_READONLY (dtype)
12782                || is_volatile_type < TYPE_VOLATILE (dtype)
12783                || (is_const_type <= TYPE_READONLY (dtype)
12784                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12785                    && DECL_ORIGINAL_TYPE (name) != type))
12786         /* cv-unqualified version of named type.  Just use the unnamed
12787            type to which it refers.  */
12788         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12789                                   is_const_type, is_volatile_type,
12790                                   context_die);
12791       /* Else cv-qualified version of named type; fall through.  */
12792     }
12793
12794   if (is_const_type
12795       /* If both is_const_type and is_volatile_type, prefer the path
12796          which leads to a qualified type.  */
12797       && (!is_volatile_type
12798           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12799           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12800     {
12801       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12802       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12803     }
12804   else if (is_volatile_type)
12805     {
12806       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12807       sub_die = modified_type_die (type, is_const_type, 0, context_die);
12808     }
12809   else if (code == POINTER_TYPE)
12810     {
12811       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12812       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12813                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12814       item_type = TREE_TYPE (type);
12815       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12816         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12817                          TYPE_ADDR_SPACE (item_type));
12818     }
12819   else if (code == REFERENCE_TYPE)
12820     {
12821       if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
12822         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12823                                 type);
12824       else
12825         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12826       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12827                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12828       item_type = TREE_TYPE (type);
12829       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12830         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12831                          TYPE_ADDR_SPACE (item_type));
12832     }
12833   else if (code == INTEGER_TYPE
12834            && TREE_TYPE (type) != NULL_TREE
12835            && subrange_type_for_debug_p (type, &low, &high))
12836     {
12837       mod_type_die = subrange_type_die (type, low, high, context_die);
12838       item_type = TREE_TYPE (type);
12839     }
12840   else if (is_base_type (type))
12841     mod_type_die = base_type_die (type);
12842   else
12843     {
12844       gen_type_die (type, context_die);
12845
12846       /* We have to get the type_main_variant here (and pass that to the
12847          `lookup_type_die' routine) because the ..._TYPE node we have
12848          might simply be a *copy* of some original type node (where the
12849          copy was created to help us keep track of typedef names) and
12850          that copy might have a different TYPE_UID from the original
12851          ..._TYPE node.  */
12852       if (TREE_CODE (type) != VECTOR_TYPE)
12853         return lookup_type_die (type_main_variant (type));
12854       else
12855         /* Vectors have the debugging information in the type,
12856            not the main variant.  */
12857         return lookup_type_die (type);
12858     }
12859
12860   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12861      don't output a DW_TAG_typedef, since there isn't one in the
12862      user's program; just attach a DW_AT_name to the type.
12863      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12864      if the base type already has the same name.  */
12865   if (name
12866       && ((TREE_CODE (name) != TYPE_DECL
12867            && (qualified_type == TYPE_MAIN_VARIANT (type)
12868                || (!is_const_type && !is_volatile_type)))
12869           || (TREE_CODE (name) == TYPE_DECL
12870               && TREE_TYPE (name) == qualified_type
12871               && DECL_NAME (name))))
12872     {
12873       if (TREE_CODE (name) == TYPE_DECL)
12874         /* Could just call add_name_and_src_coords_attributes here,
12875            but since this is a builtin type it doesn't have any
12876            useful source coordinates anyway.  */
12877         name = DECL_NAME (name);
12878       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12879       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12880     }
12881   /* This probably indicates a bug.  */
12882   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12883     add_name_attribute (mod_type_die, "__unknown__");
12884
12885   if (qualified_type)
12886     equate_type_number_to_die (qualified_type, mod_type_die);
12887
12888   if (item_type)
12889     /* We must do this after the equate_type_number_to_die call, in case
12890        this is a recursive type.  This ensures that the modified_type_die
12891        recursion will terminate even if the type is recursive.  Recursive
12892        types are possible in Ada.  */
12893     sub_die = modified_type_die (item_type,
12894                                  TYPE_READONLY (item_type),
12895                                  TYPE_VOLATILE (item_type),
12896                                  context_die);
12897
12898   if (sub_die != NULL)
12899     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12900
12901   return mod_type_die;
12902 }
12903
12904 /* Generate DIEs for the generic parameters of T.
12905    T must be either a generic type or a generic function.
12906    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12907
12908 static void
12909 gen_generic_params_dies (tree t)
12910 {
12911   tree parms, args;
12912   int parms_num, i;
12913   dw_die_ref die = NULL;
12914
12915   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12916     return;
12917
12918   if (TYPE_P (t))
12919     die = lookup_type_die (t);
12920   else if (DECL_P (t))
12921     die = lookup_decl_die (t);
12922
12923   gcc_assert (die);
12924
12925   parms = lang_hooks.get_innermost_generic_parms (t);
12926   if (!parms)
12927     /* T has no generic parameter. It means T is neither a generic type
12928        or function. End of story.  */
12929     return;
12930
12931   parms_num = TREE_VEC_LENGTH (parms);
12932   args = lang_hooks.get_innermost_generic_args (t);
12933   for (i = 0; i < parms_num; i++)
12934     {
12935       tree parm, arg, arg_pack_elems;
12936
12937       parm = TREE_VEC_ELT (parms, i);
12938       arg = TREE_VEC_ELT (args, i);
12939       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12940       gcc_assert (parm && TREE_VALUE (parm) && arg);
12941
12942       if (parm && TREE_VALUE (parm) && arg)
12943         {
12944           /* If PARM represents a template parameter pack,
12945              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12946              by DW_TAG_template_*_parameter DIEs for the argument
12947              pack elements of ARG. Note that ARG would then be
12948              an argument pack.  */
12949           if (arg_pack_elems)
12950             template_parameter_pack_die (TREE_VALUE (parm),
12951                                          arg_pack_elems,
12952                                          die);
12953           else
12954             generic_parameter_die (TREE_VALUE (parm), arg,
12955                                    true /* Emit DW_AT_name */, die);
12956         }
12957     }
12958 }
12959
12960 /* Create and return a DIE for PARM which should be
12961    the representation of a generic type parameter.
12962    For instance, in the C++ front end, PARM would be a template parameter.
12963    ARG is the argument to PARM.
12964    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12965    name of the PARM.
12966    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12967    as a child node.  */
12968
12969 static dw_die_ref
12970 generic_parameter_die (tree parm, tree arg,
12971                        bool emit_name_p,
12972                        dw_die_ref parent_die)
12973 {
12974   dw_die_ref tmpl_die = NULL;
12975   const char *name = NULL;
12976
12977   if (!parm || !DECL_NAME (parm) || !arg)
12978     return NULL;
12979
12980   /* We support non-type generic parameters and arguments,
12981      type generic parameters and arguments, as well as
12982      generic generic parameters (a.k.a. template template parameters in C++)
12983      and arguments.  */
12984   if (TREE_CODE (parm) == PARM_DECL)
12985     /* PARM is a nontype generic parameter  */
12986     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12987   else if (TREE_CODE (parm) == TYPE_DECL)
12988     /* PARM is a type generic parameter.  */
12989     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12990   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12991     /* PARM is a generic generic parameter.
12992        Its DIE is a GNU extension. It shall have a
12993        DW_AT_name attribute to represent the name of the template template
12994        parameter, and a DW_AT_GNU_template_name attribute to represent the
12995        name of the template template argument.  */
12996     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12997                         parent_die, parm);
12998   else
12999     gcc_unreachable ();
13000
13001   if (tmpl_die)
13002     {
13003       tree tmpl_type;
13004
13005       /* If PARM is a generic parameter pack, it means we are
13006          emitting debug info for a template argument pack element.
13007          In other terms, ARG is a template argument pack element.
13008          In that case, we don't emit any DW_AT_name attribute for
13009          the die.  */
13010       if (emit_name_p)
13011         {
13012           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13013           gcc_assert (name);
13014           add_AT_string (tmpl_die, DW_AT_name, name);
13015         }
13016
13017       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13018         {
13019           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13020              TMPL_DIE should have a child DW_AT_type attribute that is set
13021              to the type of the argument to PARM, which is ARG.
13022              If PARM is a type generic parameter, TMPL_DIE should have a
13023              child DW_AT_type that is set to ARG.  */
13024           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13025           add_type_attribute (tmpl_die, tmpl_type, 0,
13026                               TREE_THIS_VOLATILE (tmpl_type),
13027                               parent_die);
13028         }
13029       else
13030         {
13031           /* So TMPL_DIE is a DIE representing a
13032              a generic generic template parameter, a.k.a template template
13033              parameter in C++ and arg is a template.  */
13034
13035           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13036              to the name of the argument.  */
13037           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13038           if (name)
13039             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13040         }
13041
13042       if (TREE_CODE (parm) == PARM_DECL)
13043         /* So PARM is a non-type generic parameter.
13044            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13045            attribute of TMPL_DIE which value represents the value
13046            of ARG.
13047            We must be careful here:
13048            The value of ARG might reference some function decls.
13049            We might currently be emitting debug info for a generic
13050            type and types are emitted before function decls, we don't
13051            know if the function decls referenced by ARG will actually be
13052            emitted after cgraph computations.
13053            So must defer the generation of the DW_AT_const_value to
13054            after cgraph is ready.  */
13055         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13056     }
13057
13058   return tmpl_die;
13059 }
13060
13061 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13062    PARM_PACK must be a template parameter pack. The returned DIE
13063    will be child DIE of PARENT_DIE.  */
13064
13065 static dw_die_ref
13066 template_parameter_pack_die (tree parm_pack,
13067                              tree parm_pack_args,
13068                              dw_die_ref parent_die)
13069 {
13070   dw_die_ref die;
13071   int j;
13072
13073   gcc_assert (parent_die && parm_pack);
13074
13075   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13076   add_name_and_src_coords_attributes (die, parm_pack);
13077   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13078     generic_parameter_die (parm_pack,
13079                            TREE_VEC_ELT (parm_pack_args, j),
13080                            false /* Don't emit DW_AT_name */,
13081                            die);
13082   return die;
13083 }
13084
13085 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13086    an enumerated type.  */
13087
13088 static inline int
13089 type_is_enum (const_tree type)
13090 {
13091   return TREE_CODE (type) == ENUMERAL_TYPE;
13092 }
13093
13094 /* Return the DBX register number described by a given RTL node.  */
13095
13096 static unsigned int
13097 dbx_reg_number (const_rtx rtl)
13098 {
13099   unsigned regno = REGNO (rtl);
13100
13101   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13102
13103 #ifdef LEAF_REG_REMAP
13104   if (current_function_uses_only_leaf_regs)
13105     {
13106       int leaf_reg = LEAF_REG_REMAP (regno);
13107       if (leaf_reg != -1)
13108         regno = (unsigned) leaf_reg;
13109     }
13110 #endif
13111
13112   return DBX_REGISTER_NUMBER (regno);
13113 }
13114
13115 /* Optionally add a DW_OP_piece term to a location description expression.
13116    DW_OP_piece is only added if the location description expression already
13117    doesn't end with DW_OP_piece.  */
13118
13119 static void
13120 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13121 {
13122   dw_loc_descr_ref loc;
13123
13124   if (*list_head != NULL)
13125     {
13126       /* Find the end of the chain.  */
13127       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13128         ;
13129
13130       if (loc->dw_loc_opc != DW_OP_piece)
13131         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13132     }
13133 }
13134
13135 /* Return a location descriptor that designates a machine register or
13136    zero if there is none.  */
13137
13138 static dw_loc_descr_ref
13139 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13140 {
13141   rtx regs;
13142
13143   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13144     return 0;
13145
13146   /* We only use "frame base" when we're sure we're talking about the
13147      post-prologue local stack frame.  We do this by *not* running
13148      register elimination until this point, and recognizing the special
13149      argument pointer and soft frame pointer rtx's.
13150      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13151   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13152       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13153     {
13154       dw_loc_descr_ref result = NULL;
13155
13156       if (dwarf_version >= 4 || !dwarf_strict)
13157         {
13158           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13159           if (result)
13160             add_loc_descr (&result,
13161                            new_loc_descr (DW_OP_stack_value, 0, 0));
13162         }
13163       return result;
13164     }
13165
13166   regs = targetm.dwarf_register_span (rtl);
13167
13168   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13169     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13170   else
13171     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13172 }
13173
13174 /* Return a location descriptor that designates a machine register for
13175    a given hard register number.  */
13176
13177 static dw_loc_descr_ref
13178 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13179 {
13180   dw_loc_descr_ref reg_loc_descr;
13181
13182   if (regno <= 31)
13183     reg_loc_descr
13184       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13185   else
13186     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13187
13188   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13189     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13190
13191   return reg_loc_descr;
13192 }
13193
13194 /* Given an RTL of a register, return a location descriptor that
13195    designates a value that spans more than one register.  */
13196
13197 static dw_loc_descr_ref
13198 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13199                              enum var_init_status initialized)
13200 {
13201   int nregs, size, i;
13202   unsigned reg;
13203   dw_loc_descr_ref loc_result = NULL;
13204
13205   reg = REGNO (rtl);
13206 #ifdef LEAF_REG_REMAP
13207   if (current_function_uses_only_leaf_regs)
13208     {
13209       int leaf_reg = LEAF_REG_REMAP (reg);
13210       if (leaf_reg != -1)
13211         reg = (unsigned) leaf_reg;
13212     }
13213 #endif
13214   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13215   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13216
13217   /* Simple, contiguous registers.  */
13218   if (regs == NULL_RTX)
13219     {
13220       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13221
13222       loc_result = NULL;
13223       while (nregs--)
13224         {
13225           dw_loc_descr_ref t;
13226
13227           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13228                                       VAR_INIT_STATUS_INITIALIZED);
13229           add_loc_descr (&loc_result, t);
13230           add_loc_descr_op_piece (&loc_result, size);
13231           ++reg;
13232         }
13233       return loc_result;
13234     }
13235
13236   /* Now onto stupid register sets in non contiguous locations.  */
13237
13238   gcc_assert (GET_CODE (regs) == PARALLEL);
13239
13240   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13241   loc_result = NULL;
13242
13243   for (i = 0; i < XVECLEN (regs, 0); ++i)
13244     {
13245       dw_loc_descr_ref t;
13246
13247       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13248                                   VAR_INIT_STATUS_INITIALIZED);
13249       add_loc_descr (&loc_result, t);
13250       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13251       add_loc_descr_op_piece (&loc_result, size);
13252     }
13253
13254   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13255     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13256   return loc_result;
13257 }
13258
13259 /* Return a location descriptor that designates a constant.  */
13260
13261 static dw_loc_descr_ref
13262 int_loc_descriptor (HOST_WIDE_INT i)
13263 {
13264   enum dwarf_location_atom op;
13265
13266   /* Pick the smallest representation of a constant, rather than just
13267      defaulting to the LEB encoding.  */
13268   if (i >= 0)
13269     {
13270       if (i <= 31)
13271         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13272       else if (i <= 0xff)
13273         op = DW_OP_const1u;
13274       else if (i <= 0xffff)
13275         op = DW_OP_const2u;
13276       else if (HOST_BITS_PER_WIDE_INT == 32
13277                || i <= 0xffffffff)
13278         op = DW_OP_const4u;
13279       else
13280         op = DW_OP_constu;
13281     }
13282   else
13283     {
13284       if (i >= -0x80)
13285         op = DW_OP_const1s;
13286       else if (i >= -0x8000)
13287         op = DW_OP_const2s;
13288       else if (HOST_BITS_PER_WIDE_INT == 32
13289                || i >= -0x80000000)
13290         op = DW_OP_const4s;
13291       else
13292         op = DW_OP_consts;
13293     }
13294
13295   return new_loc_descr (op, i, 0);
13296 }
13297
13298 /* Return loc description representing "address" of integer value.
13299    This can appear only as toplevel expression.  */
13300
13301 static dw_loc_descr_ref
13302 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13303 {
13304   int litsize;
13305   dw_loc_descr_ref loc_result = NULL;
13306
13307   if (!(dwarf_version >= 4 || !dwarf_strict))
13308     return NULL;
13309
13310   if (i >= 0)
13311     {
13312       if (i <= 31)
13313         litsize = 1;
13314       else if (i <= 0xff)
13315         litsize = 2;
13316       else if (i <= 0xffff)
13317         litsize = 3;
13318       else if (HOST_BITS_PER_WIDE_INT == 32
13319                || i <= 0xffffffff)
13320         litsize = 5;
13321       else
13322         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13323     }
13324   else
13325     {
13326       if (i >= -0x80)
13327         litsize = 2;
13328       else if (i >= -0x8000)
13329         litsize = 3;
13330       else if (HOST_BITS_PER_WIDE_INT == 32
13331                || i >= -0x80000000)
13332         litsize = 5;
13333       else
13334         litsize = 1 + size_of_sleb128 (i);
13335     }
13336   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13337      is more compact.  For DW_OP_stack_value we need:
13338      litsize + 1 (DW_OP_stack_value)
13339      and for DW_OP_implicit_value:
13340      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13341   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13342     {
13343       loc_result = int_loc_descriptor (i);
13344       add_loc_descr (&loc_result,
13345                      new_loc_descr (DW_OP_stack_value, 0, 0));
13346       return loc_result;
13347     }
13348
13349   loc_result = new_loc_descr (DW_OP_implicit_value,
13350                               size, 0);
13351   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13352   loc_result->dw_loc_oprnd2.v.val_int = i;
13353   return loc_result;
13354 }
13355
13356 /* Return a location descriptor that designates a base+offset location.  */
13357
13358 static dw_loc_descr_ref
13359 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13360                  enum var_init_status initialized)
13361 {
13362   unsigned int regno;
13363   dw_loc_descr_ref result;
13364   dw_fde_ref fde = current_fde ();
13365
13366   /* We only use "frame base" when we're sure we're talking about the
13367      post-prologue local stack frame.  We do this by *not* running
13368      register elimination until this point, and recognizing the special
13369      argument pointer and soft frame pointer rtx's.  */
13370   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13371     {
13372       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13373
13374       if (elim != reg)
13375         {
13376           if (GET_CODE (elim) == PLUS)
13377             {
13378               offset += INTVAL (XEXP (elim, 1));
13379               elim = XEXP (elim, 0);
13380             }
13381           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13382                        && (elim == hard_frame_pointer_rtx
13383                            || elim == stack_pointer_rtx))
13384                       || elim == (frame_pointer_needed
13385                                   ? hard_frame_pointer_rtx
13386                                   : stack_pointer_rtx));
13387
13388           /* If drap register is used to align stack, use frame
13389              pointer + offset to access stack variables.  If stack
13390              is aligned without drap, use stack pointer + offset to
13391              access stack variables.  */
13392           if (crtl->stack_realign_tried
13393               && reg == frame_pointer_rtx)
13394             {
13395               int base_reg
13396                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13397                                       ? HARD_FRAME_POINTER_REGNUM
13398                                       : REGNO (elim));
13399               return new_reg_loc_descr (base_reg, offset);
13400             }
13401
13402           offset += frame_pointer_fb_offset;
13403           return new_loc_descr (DW_OP_fbreg, offset, 0);
13404         }
13405     }
13406   else if (!optimize
13407            && fde
13408            && (fde->drap_reg == REGNO (reg)
13409                || fde->vdrap_reg == REGNO (reg)))
13410     {
13411       /* Use cfa+offset to represent the location of arguments passed
13412          on the stack when drap is used to align stack.
13413          Only do this when not optimizing, for optimized code var-tracking
13414          is supposed to track where the arguments live and the register
13415          used as vdrap or drap in some spot might be used for something
13416          else in other part of the routine.  */
13417       return new_loc_descr (DW_OP_fbreg, offset, 0);
13418     }
13419
13420   regno = dbx_reg_number (reg);
13421   if (regno <= 31)
13422     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13423                             offset, 0);
13424   else
13425     result = new_loc_descr (DW_OP_bregx, regno, offset);
13426
13427   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13428     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13429
13430   return result;
13431 }
13432
13433 /* Return true if this RTL expression describes a base+offset calculation.  */
13434
13435 static inline int
13436 is_based_loc (const_rtx rtl)
13437 {
13438   return (GET_CODE (rtl) == PLUS
13439           && ((REG_P (XEXP (rtl, 0))
13440                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13441                && CONST_INT_P (XEXP (rtl, 1)))));
13442 }
13443
13444 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13445    failed.  */
13446
13447 static dw_loc_descr_ref
13448 tls_mem_loc_descriptor (rtx mem)
13449 {
13450   tree base;
13451   dw_loc_descr_ref loc_result;
13452
13453   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13454     return NULL;
13455
13456   base = get_base_address (MEM_EXPR (mem));
13457   if (base == NULL
13458       || TREE_CODE (base) != VAR_DECL
13459       || !DECL_THREAD_LOCAL_P (base))
13460     return NULL;
13461
13462   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13463   if (loc_result == NULL)
13464     return NULL;
13465
13466   if (INTVAL (MEM_OFFSET (mem)))
13467     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13468
13469   return loc_result;
13470 }
13471
13472 /* Output debug info about reason why we failed to expand expression as dwarf
13473    expression.  */
13474
13475 static void
13476 expansion_failed (tree expr, rtx rtl, char const *reason)
13477 {
13478   if (dump_file && (dump_flags & TDF_DETAILS))
13479     {
13480       fprintf (dump_file, "Failed to expand as dwarf: ");
13481       if (expr)
13482         print_generic_expr (dump_file, expr, dump_flags);
13483       if (rtl)
13484         {
13485           fprintf (dump_file, "\n");
13486           print_rtl (dump_file, rtl);
13487         }
13488       fprintf (dump_file, "\nReason: %s\n", reason);
13489     }
13490 }
13491
13492 /* Helper function for const_ok_for_output, called either directly
13493    or via for_each_rtx.  */
13494
13495 static int
13496 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13497 {
13498   rtx rtl = *rtlp;
13499
13500   if (GET_CODE (rtl) == UNSPEC)
13501     {
13502       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13503          we can't express it in the debug info.  */
13504 #ifdef ENABLE_CHECKING
13505       /* Don't complain about TLS UNSPECs, those are just too hard to
13506          delegitimize.  */
13507       if (XVECLEN (rtl, 0) != 1
13508           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13509           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13510           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13511           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13512         inform (current_function_decl
13513                 ? DECL_SOURCE_LOCATION (current_function_decl)
13514                 : UNKNOWN_LOCATION,
13515 #if NUM_UNSPEC_VALUES > 0
13516                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13517                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13518                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13519                 XINT (rtl, 1));
13520 #else
13521                 "non-delegitimized UNSPEC %d found in variable location",
13522                 XINT (rtl, 1));
13523 #endif
13524 #endif
13525       expansion_failed (NULL_TREE, rtl,
13526                         "UNSPEC hasn't been delegitimized.\n");
13527       return 1;
13528     }
13529
13530   if (GET_CODE (rtl) != SYMBOL_REF)
13531     return 0;
13532
13533   if (CONSTANT_POOL_ADDRESS_P (rtl))
13534     {
13535       bool marked;
13536       get_pool_constant_mark (rtl, &marked);
13537       /* If all references to this pool constant were optimized away,
13538          it was not output and thus we can't represent it.  */
13539       if (!marked)
13540         {
13541           expansion_failed (NULL_TREE, rtl,
13542                             "Constant was removed from constant pool.\n");
13543           return 1;
13544         }
13545     }
13546
13547   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13548     return 1;
13549
13550   /* Avoid references to external symbols in debug info, on several targets
13551      the linker might even refuse to link when linking a shared library,
13552      and in many other cases the relocations for .debug_info/.debug_loc are
13553      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13554      to be defined within the same shared library or executable are fine.  */
13555   if (SYMBOL_REF_EXTERNAL_P (rtl))
13556     {
13557       tree decl = SYMBOL_REF_DECL (rtl);
13558
13559       if (decl == NULL || !targetm.binds_local_p (decl))
13560         {
13561           expansion_failed (NULL_TREE, rtl,
13562                             "Symbol not defined in current TU.\n");
13563           return 1;
13564         }
13565     }
13566
13567   return 0;
13568 }
13569
13570 /* Return true if constant RTL can be emitted in DW_OP_addr or
13571    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13572    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13573
13574 static bool
13575 const_ok_for_output (rtx rtl)
13576 {
13577   if (GET_CODE (rtl) == SYMBOL_REF)
13578     return const_ok_for_output_1 (&rtl, NULL) == 0;
13579
13580   if (GET_CODE (rtl) == CONST)
13581     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13582
13583   return true;
13584 }
13585
13586 /* The following routine converts the RTL for a variable or parameter
13587    (resident in memory) into an equivalent Dwarf representation of a
13588    mechanism for getting the address of that same variable onto the top of a
13589    hypothetical "address evaluation" stack.
13590
13591    When creating memory location descriptors, we are effectively transforming
13592    the RTL for a memory-resident object into its Dwarf postfix expression
13593    equivalent.  This routine recursively descends an RTL tree, turning
13594    it into Dwarf postfix code as it goes.
13595
13596    MODE is the mode of the memory reference, needed to handle some
13597    autoincrement addressing modes.
13598
13599    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13600    location list for RTL.
13601
13602    Return 0 if we can't represent the location.  */
13603
13604 static dw_loc_descr_ref
13605 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13606                     enum var_init_status initialized)
13607 {
13608   dw_loc_descr_ref mem_loc_result = NULL;
13609   enum dwarf_location_atom op;
13610   dw_loc_descr_ref op0, op1;
13611
13612   /* Note that for a dynamically sized array, the location we will generate a
13613      description of here will be the lowest numbered location which is
13614      actually within the array.  That's *not* necessarily the same as the
13615      zeroth element of the array.  */
13616
13617   rtl = targetm.delegitimize_address (rtl);
13618
13619   switch (GET_CODE (rtl))
13620     {
13621     case POST_INC:
13622     case POST_DEC:
13623     case POST_MODIFY:
13624       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13625
13626     case SUBREG:
13627       /* The case of a subreg may arise when we have a local (register)
13628          variable or a formal (register) parameter which doesn't quite fill
13629          up an entire register.  For now, just assume that it is
13630          legitimate to make the Dwarf info refer to the whole register which
13631          contains the given subreg.  */
13632       if (!subreg_lowpart_p (rtl))
13633         break;
13634       rtl = SUBREG_REG (rtl);
13635       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13636         break;
13637       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13638         break;
13639       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13640       break;
13641
13642     case REG:
13643       /* Whenever a register number forms a part of the description of the
13644          method for calculating the (dynamic) address of a memory resident
13645          object, DWARF rules require the register number be referred to as
13646          a "base register".  This distinction is not based in any way upon
13647          what category of register the hardware believes the given register
13648          belongs to.  This is strictly DWARF terminology we're dealing with
13649          here. Note that in cases where the location of a memory-resident
13650          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13651          OP_CONST (0)) the actual DWARF location descriptor that we generate
13652          may just be OP_BASEREG (basereg).  This may look deceptively like
13653          the object in question was allocated to a register (rather than in
13654          memory) so DWARF consumers need to be aware of the subtle
13655          distinction between OP_REG and OP_BASEREG.  */
13656       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13657         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13658       else if (stack_realign_drap
13659                && crtl->drap_reg
13660                && crtl->args.internal_arg_pointer == rtl
13661                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13662         {
13663           /* If RTL is internal_arg_pointer, which has been optimized
13664              out, use DRAP instead.  */
13665           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13666                                             VAR_INIT_STATUS_INITIALIZED);
13667         }
13668       break;
13669
13670     case SIGN_EXTEND:
13671     case ZERO_EXTEND:
13672       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13673                                 VAR_INIT_STATUS_INITIALIZED);
13674       if (op0 == 0)
13675         break;
13676       else
13677         {
13678           int shift = DWARF2_ADDR_SIZE
13679                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13680           shift *= BITS_PER_UNIT;
13681           if (GET_CODE (rtl) == SIGN_EXTEND)
13682             op = DW_OP_shra;
13683           else
13684             op = DW_OP_shr;
13685           mem_loc_result = op0;
13686           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13687           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13688           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13689           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13690         }
13691       break;
13692
13693     case MEM:
13694       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13695                                            VAR_INIT_STATUS_INITIALIZED);
13696       if (mem_loc_result == NULL)
13697         mem_loc_result = tls_mem_loc_descriptor (rtl);
13698       if (mem_loc_result != 0)
13699         {
13700           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13701             {
13702               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13703               return 0;
13704             }
13705           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13706             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13707           else
13708             add_loc_descr (&mem_loc_result,
13709                            new_loc_descr (DW_OP_deref_size,
13710                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13711         }
13712       else
13713         {
13714           rtx new_rtl = avoid_constant_pool_reference (rtl);
13715           if (new_rtl != rtl)
13716             return mem_loc_descriptor (new_rtl, mode, initialized);
13717         }
13718       break;
13719
13720     case LO_SUM:
13721       return mem_loc_descriptor (XEXP (rtl, 1), mode, initialized);
13722
13723     case LABEL_REF:
13724       /* Some ports can transform a symbol ref into a label ref, because
13725          the symbol ref is too far away and has to be dumped into a constant
13726          pool.  */
13727     case CONST:
13728     case SYMBOL_REF:
13729       if (GET_CODE (rtl) == SYMBOL_REF
13730           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13731         {
13732           dw_loc_descr_ref temp;
13733
13734           /* If this is not defined, we have no way to emit the data.  */
13735           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13736             break;
13737
13738           /* We used to emit DW_OP_addr here, but that's wrong, since
13739              DW_OP_addr should be relocated by the debug info consumer,
13740              while DW_OP_GNU_push_tls_address operand should not.  */
13741           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13742                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13743           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13744           temp->dw_loc_oprnd1.v.val_addr = rtl;
13745           temp->dtprel = true;
13746
13747           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13748           add_loc_descr (&mem_loc_result, temp);
13749
13750           break;
13751         }
13752
13753       if (!const_ok_for_output (rtl))
13754         break;
13755
13756     symref:
13757       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13758       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13759       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13760       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13761       break;
13762
13763     case CONCAT:
13764     case CONCATN:
13765     case VAR_LOCATION:
13766     case DEBUG_IMPLICIT_PTR:
13767       expansion_failed (NULL_TREE, rtl,
13768                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13769       return 0;
13770
13771     case ENTRY_VALUE:
13772       if (dwarf_strict)
13773         return NULL;
13774       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13775       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13776       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13777         mem_loc_result->dw_loc_oprnd1.v.val_loc
13778           = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13779                                     VAR_INIT_STATUS_INITIALIZED);
13780       else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13781         {
13782           dw_loc_descr_ref ref
13783             = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13784                                   VAR_INIT_STATUS_INITIALIZED);
13785           if (ref == NULL || ref->dw_loc_opc == DW_OP_fbreg)
13786             return NULL;
13787           mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13788         }
13789       else
13790         gcc_unreachable ();
13791       return mem_loc_result;
13792
13793     case PRE_MODIFY:
13794       /* Extract the PLUS expression nested inside and fall into
13795          PLUS code below.  */
13796       rtl = XEXP (rtl, 1);
13797       goto plus;
13798
13799     case PRE_INC:
13800     case PRE_DEC:
13801       /* Turn these into a PLUS expression and fall into the PLUS code
13802          below.  */
13803       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13804                           GEN_INT (GET_CODE (rtl) == PRE_INC
13805                                    ? GET_MODE_UNIT_SIZE (mode)
13806                                    : -GET_MODE_UNIT_SIZE (mode)));
13807
13808       /* ... fall through ...  */
13809
13810     case PLUS:
13811     plus:
13812       if (is_based_loc (rtl))
13813         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13814                                           INTVAL (XEXP (rtl, 1)),
13815                                           VAR_INIT_STATUS_INITIALIZED);
13816       else
13817         {
13818           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13819                                                VAR_INIT_STATUS_INITIALIZED);
13820           if (mem_loc_result == 0)
13821             break;
13822
13823           if (CONST_INT_P (XEXP (rtl, 1)))
13824             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13825           else
13826             {
13827               dw_loc_descr_ref mem_loc_result2
13828                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13829                                       VAR_INIT_STATUS_INITIALIZED);
13830               if (mem_loc_result2 == 0)
13831                 break;
13832               add_loc_descr (&mem_loc_result, mem_loc_result2);
13833               add_loc_descr (&mem_loc_result,
13834                              new_loc_descr (DW_OP_plus, 0, 0));
13835             }
13836         }
13837       break;
13838
13839     /* If a pseudo-reg is optimized away, it is possible for it to
13840        be replaced with a MEM containing a multiply or shift.  */
13841     case MINUS:
13842       op = DW_OP_minus;
13843       goto do_binop;
13844
13845     case MULT:
13846       op = DW_OP_mul;
13847       goto do_binop;
13848
13849     case DIV:
13850       op = DW_OP_div;
13851       goto do_binop;
13852
13853     case UMOD:
13854       op = DW_OP_mod;
13855       goto do_binop;
13856
13857     case ASHIFT:
13858       op = DW_OP_shl;
13859       goto do_binop;
13860
13861     case ASHIFTRT:
13862       op = DW_OP_shra;
13863       goto do_binop;
13864
13865     case LSHIFTRT:
13866       op = DW_OP_shr;
13867       goto do_binop;
13868
13869     case AND:
13870       op = DW_OP_and;
13871       goto do_binop;
13872
13873     case IOR:
13874       op = DW_OP_or;
13875       goto do_binop;
13876
13877     case XOR:
13878       op = DW_OP_xor;
13879       goto do_binop;
13880
13881     do_binop:
13882       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13883                                 VAR_INIT_STATUS_INITIALIZED);
13884       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13885                                 VAR_INIT_STATUS_INITIALIZED);
13886
13887       if (op0 == 0 || op1 == 0)
13888         break;
13889
13890       mem_loc_result = op0;
13891       add_loc_descr (&mem_loc_result, op1);
13892       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13893       break;
13894
13895     case MOD:
13896       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13897                                 VAR_INIT_STATUS_INITIALIZED);
13898       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13899                                 VAR_INIT_STATUS_INITIALIZED);
13900
13901       if (op0 == 0 || op1 == 0)
13902         break;
13903
13904       mem_loc_result = op0;
13905       add_loc_descr (&mem_loc_result, op1);
13906       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13907       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13908       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13909       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13910       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13911       break;
13912
13913     case NOT:
13914       op = DW_OP_not;
13915       goto do_unop;
13916
13917     case ABS:
13918       op = DW_OP_abs;
13919       goto do_unop;
13920
13921     case NEG:
13922       op = DW_OP_neg;
13923       goto do_unop;
13924
13925     do_unop:
13926       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13927                                 VAR_INIT_STATUS_INITIALIZED);
13928
13929       if (op0 == 0)
13930         break;
13931
13932       mem_loc_result = op0;
13933       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13934       break;
13935
13936     case CONST_INT:
13937       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13938       break;
13939
13940     case EQ:
13941       op = DW_OP_eq;
13942       goto do_scompare;
13943
13944     case GE:
13945       op = DW_OP_ge;
13946       goto do_scompare;
13947
13948     case GT:
13949       op = DW_OP_gt;
13950       goto do_scompare;
13951
13952     case LE:
13953       op = DW_OP_le;
13954       goto do_scompare;
13955
13956     case LT:
13957       op = DW_OP_lt;
13958       goto do_scompare;
13959
13960     case NE:
13961       op = DW_OP_ne;
13962       goto do_scompare;
13963
13964     do_scompare:
13965       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13966           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13967         break;
13968       else
13969         {
13970           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13971
13972           if (op_mode == VOIDmode)
13973             op_mode = GET_MODE (XEXP (rtl, 1));
13974           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13975             break;
13976
13977           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13978                                     VAR_INIT_STATUS_INITIALIZED);
13979           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13980                                     VAR_INIT_STATUS_INITIALIZED);
13981
13982           if (op0 == 0 || op1 == 0)
13983             break;
13984
13985           if (op_mode != VOIDmode
13986               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13987             {
13988               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13989               shift *= BITS_PER_UNIT;
13990               /* For eq/ne, if the operands are known to be zero-extended,
13991                  there is no need to do the fancy shifting up.  */
13992               if (op == DW_OP_eq || op == DW_OP_ne)
13993                 {
13994                   dw_loc_descr_ref last0, last1;
13995                   for (last0 = op0;
13996                        last0->dw_loc_next != NULL;
13997                        last0 = last0->dw_loc_next)
13998                     ;
13999                   for (last1 = op1;
14000                        last1->dw_loc_next != NULL;
14001                        last1 = last1->dw_loc_next)
14002                     ;
14003                   /* deref_size zero extends, and for constants we can check
14004                      whether they are zero extended or not.  */
14005                   if (((last0->dw_loc_opc == DW_OP_deref_size
14006                         && last0->dw_loc_oprnd1.v.val_int
14007                            <= GET_MODE_SIZE (op_mode))
14008                        || (CONST_INT_P (XEXP (rtl, 0))
14009                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14010                                == (INTVAL (XEXP (rtl, 0))
14011                                    & GET_MODE_MASK (op_mode))))
14012                       && ((last1->dw_loc_opc == DW_OP_deref_size
14013                            && last1->dw_loc_oprnd1.v.val_int
14014                               <= GET_MODE_SIZE (op_mode))
14015                           || (CONST_INT_P (XEXP (rtl, 1))
14016                               && (unsigned HOST_WIDE_INT)
14017                                  INTVAL (XEXP (rtl, 1))
14018                                  == (INTVAL (XEXP (rtl, 1))
14019                                      & GET_MODE_MASK (op_mode)))))
14020                     goto do_compare;
14021                 }
14022               add_loc_descr (&op0, int_loc_descriptor (shift));
14023               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14024               if (CONST_INT_P (XEXP (rtl, 1)))
14025                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14026               else
14027                 {
14028                   add_loc_descr (&op1, int_loc_descriptor (shift));
14029                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14030                 }
14031             }
14032         }
14033
14034     do_compare:
14035       mem_loc_result = op0;
14036       add_loc_descr (&mem_loc_result, op1);
14037       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14038       if (STORE_FLAG_VALUE != 1)
14039         {
14040           add_loc_descr (&mem_loc_result,
14041                          int_loc_descriptor (STORE_FLAG_VALUE));
14042           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14043         }
14044       break;
14045
14046     case GEU:
14047       op = DW_OP_ge;
14048       goto do_ucompare;
14049
14050     case GTU:
14051       op = DW_OP_gt;
14052       goto do_ucompare;
14053
14054     case LEU:
14055       op = DW_OP_le;
14056       goto do_ucompare;
14057
14058     case LTU:
14059       op = DW_OP_lt;
14060       goto do_ucompare;
14061
14062     do_ucompare:
14063       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14064           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14065         break;
14066       else
14067         {
14068           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14069
14070           if (op_mode == VOIDmode)
14071             op_mode = GET_MODE (XEXP (rtl, 1));
14072           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14073             break;
14074
14075           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14076                                     VAR_INIT_STATUS_INITIALIZED);
14077           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14078                                     VAR_INIT_STATUS_INITIALIZED);
14079
14080           if (op0 == 0 || op1 == 0)
14081             break;
14082
14083           if (op_mode != VOIDmode
14084               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14085             {
14086               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14087               dw_loc_descr_ref last0, last1;
14088               for (last0 = op0;
14089                    last0->dw_loc_next != NULL;
14090                    last0 = last0->dw_loc_next)
14091                 ;
14092               for (last1 = op1;
14093                    last1->dw_loc_next != NULL;
14094                    last1 = last1->dw_loc_next)
14095                 ;
14096               if (CONST_INT_P (XEXP (rtl, 0)))
14097                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14098               /* deref_size zero extends, so no need to mask it again.  */
14099               else if (last0->dw_loc_opc != DW_OP_deref_size
14100                        || last0->dw_loc_oprnd1.v.val_int
14101                           > GET_MODE_SIZE (op_mode))
14102                 {
14103                   add_loc_descr (&op0, int_loc_descriptor (mask));
14104                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14105                 }
14106               if (CONST_INT_P (XEXP (rtl, 1)))
14107                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14108               /* deref_size zero extends, so no need to mask it again.  */
14109               else if (last1->dw_loc_opc != DW_OP_deref_size
14110                        || last1->dw_loc_oprnd1.v.val_int
14111                           > GET_MODE_SIZE (op_mode))
14112                 {
14113                   add_loc_descr (&op1, int_loc_descriptor (mask));
14114                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14115                 }
14116             }
14117           else
14118             {
14119               HOST_WIDE_INT bias = 1;
14120               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14121               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14122               if (CONST_INT_P (XEXP (rtl, 1)))
14123                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14124                                           + INTVAL (XEXP (rtl, 1)));
14125               else
14126                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14127                                                     bias, 0));
14128             }
14129         }
14130       goto do_compare;
14131
14132     case SMIN:
14133     case SMAX:
14134     case UMIN:
14135     case UMAX:
14136       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14137           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14138           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14139         break;
14140
14141       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14142                                 VAR_INIT_STATUS_INITIALIZED);
14143       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14144                                 VAR_INIT_STATUS_INITIALIZED);
14145
14146       if (op0 == 0 || op1 == 0)
14147         break;
14148
14149       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14150       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14151       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14152       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14153         {
14154           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14155             {
14156               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14157               add_loc_descr (&op0, int_loc_descriptor (mask));
14158               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14159               add_loc_descr (&op1, int_loc_descriptor (mask));
14160               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14161             }
14162           else
14163             {
14164               HOST_WIDE_INT bias = 1;
14165               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14166               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14167               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14168             }
14169         }
14170       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14171         {
14172           int shift = DWARF2_ADDR_SIZE
14173                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14174           shift *= BITS_PER_UNIT;
14175           add_loc_descr (&op0, int_loc_descriptor (shift));
14176           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14177           add_loc_descr (&op1, int_loc_descriptor (shift));
14178           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14179         }
14180
14181       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14182         op = DW_OP_lt;
14183       else
14184         op = DW_OP_gt;
14185       mem_loc_result = op0;
14186       add_loc_descr (&mem_loc_result, op1);
14187       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14188       {
14189         dw_loc_descr_ref bra_node, drop_node;
14190
14191         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14192         add_loc_descr (&mem_loc_result, bra_node);
14193         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14194         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14195         add_loc_descr (&mem_loc_result, drop_node);
14196         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14197         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14198       }
14199       break;
14200
14201     case ZERO_EXTRACT:
14202     case SIGN_EXTRACT:
14203       if (CONST_INT_P (XEXP (rtl, 1))
14204           && CONST_INT_P (XEXP (rtl, 2))
14205           && ((unsigned) INTVAL (XEXP (rtl, 1))
14206               + (unsigned) INTVAL (XEXP (rtl, 2))
14207               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14208           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14209           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14210         {
14211           int shift, size;
14212           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14213                                     VAR_INIT_STATUS_INITIALIZED);
14214           if (op0 == 0)
14215             break;
14216           if (GET_CODE (rtl) == SIGN_EXTRACT)
14217             op = DW_OP_shra;
14218           else
14219             op = DW_OP_shr;
14220           mem_loc_result = op0;
14221           size = INTVAL (XEXP (rtl, 1));
14222           shift = INTVAL (XEXP (rtl, 2));
14223           if (BITS_BIG_ENDIAN)
14224             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14225                     - shift - size;
14226           if (shift + size != (int) DWARF2_ADDR_SIZE)
14227             {
14228               add_loc_descr (&mem_loc_result,
14229                              int_loc_descriptor (DWARF2_ADDR_SIZE
14230                                                  - shift - size));
14231               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14232             }
14233           if (size != (int) DWARF2_ADDR_SIZE)
14234             {
14235               add_loc_descr (&mem_loc_result,
14236                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14237               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14238             }
14239         }
14240       break;
14241
14242     case IF_THEN_ELSE:
14243       {
14244         dw_loc_descr_ref op2, bra_node, drop_node;
14245         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14246                                   VAR_INIT_STATUS_INITIALIZED);
14247         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14248                                   VAR_INIT_STATUS_INITIALIZED);
14249         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14250                                   VAR_INIT_STATUS_INITIALIZED);
14251         if (op0 == NULL || op1 == NULL || op2 == NULL)
14252           break;
14253
14254         mem_loc_result = op1;
14255         add_loc_descr (&mem_loc_result, op2);
14256         add_loc_descr (&mem_loc_result, op0);
14257         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14258         add_loc_descr (&mem_loc_result, bra_node);
14259         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14260         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14261         add_loc_descr (&mem_loc_result, drop_node);
14262         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14263         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14264       }
14265       break;
14266
14267     case COMPARE:
14268     case ROTATE:
14269     case ROTATERT:
14270     case TRUNCATE:
14271       /* In theory, we could implement the above.  */
14272       /* DWARF cannot represent the unsigned compare operations
14273          natively.  */
14274     case SS_MULT:
14275     case US_MULT:
14276     case SS_DIV:
14277     case US_DIV:
14278     case SS_PLUS:
14279     case US_PLUS:
14280     case SS_MINUS:
14281     case US_MINUS:
14282     case SS_NEG:
14283     case US_NEG:
14284     case SS_ABS:
14285     case SS_ASHIFT:
14286     case US_ASHIFT:
14287     case SS_TRUNCATE:
14288     case US_TRUNCATE:
14289     case UDIV:
14290     case UNORDERED:
14291     case ORDERED:
14292     case UNEQ:
14293     case UNGE:
14294     case UNGT:
14295     case UNLE:
14296     case UNLT:
14297     case LTGT:
14298     case FLOAT_EXTEND:
14299     case FLOAT_TRUNCATE:
14300     case FLOAT:
14301     case UNSIGNED_FLOAT:
14302     case FIX:
14303     case UNSIGNED_FIX:
14304     case FRACT_CONVERT:
14305     case UNSIGNED_FRACT_CONVERT:
14306     case SAT_FRACT:
14307     case UNSIGNED_SAT_FRACT:
14308     case SQRT:
14309     case BSWAP:
14310     case FFS:
14311     case CLZ:
14312     case CTZ:
14313     case POPCOUNT:
14314     case PARITY:
14315     case ASM_OPERANDS:
14316     case VEC_MERGE:
14317     case VEC_SELECT:
14318     case VEC_CONCAT:
14319     case VEC_DUPLICATE:
14320     case UNSPEC:
14321     case HIGH:
14322       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14323          can't express it in the debug info.  This can happen e.g. with some
14324          TLS UNSPECs.  */
14325       break;
14326
14327     case CONST_STRING:
14328       resolve_one_addr (&rtl, NULL);
14329       goto symref;
14330
14331     default:
14332 #ifdef ENABLE_CHECKING
14333       print_rtl (stderr, rtl);
14334       gcc_unreachable ();
14335 #else
14336       break;
14337 #endif
14338     }
14339
14340   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14341     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14342
14343   return mem_loc_result;
14344 }
14345
14346 /* Return a descriptor that describes the concatenation of two locations.
14347    This is typically a complex variable.  */
14348
14349 static dw_loc_descr_ref
14350 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14351 {
14352   dw_loc_descr_ref cc_loc_result = NULL;
14353   dw_loc_descr_ref x0_ref
14354     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14355   dw_loc_descr_ref x1_ref
14356     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14357
14358   if (x0_ref == 0 || x1_ref == 0)
14359     return 0;
14360
14361   cc_loc_result = x0_ref;
14362   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14363
14364   add_loc_descr (&cc_loc_result, x1_ref);
14365   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14366
14367   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14368     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14369
14370   return cc_loc_result;
14371 }
14372
14373 /* Return a descriptor that describes the concatenation of N
14374    locations.  */
14375
14376 static dw_loc_descr_ref
14377 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14378 {
14379   unsigned int i;
14380   dw_loc_descr_ref cc_loc_result = NULL;
14381   unsigned int n = XVECLEN (concatn, 0);
14382
14383   for (i = 0; i < n; ++i)
14384     {
14385       dw_loc_descr_ref ref;
14386       rtx x = XVECEXP (concatn, 0, i);
14387
14388       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14389       if (ref == NULL)
14390         return NULL;
14391
14392       add_loc_descr (&cc_loc_result, ref);
14393       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14394     }
14395
14396   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14397     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14398
14399   return cc_loc_result;
14400 }
14401
14402 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14403    for DEBUG_IMPLICIT_PTR RTL.  */
14404
14405 static dw_loc_descr_ref
14406 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14407 {
14408   dw_loc_descr_ref ret;
14409   dw_die_ref ref;
14410
14411   if (dwarf_strict)
14412     return NULL;
14413   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14414               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14415               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14416   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14417   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14418   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14419   if (ref)
14420     {
14421       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14422       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14423       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14424     }
14425   else
14426     {
14427       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14428       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14429     }
14430   return ret;
14431 }
14432
14433 /* Output a proper Dwarf location descriptor for a variable or parameter
14434    which is either allocated in a register or in a memory location.  For a
14435    register, we just generate an OP_REG and the register number.  For a
14436    memory location we provide a Dwarf postfix expression describing how to
14437    generate the (dynamic) address of the object onto the address stack.
14438
14439    MODE is mode of the decl if this loc_descriptor is going to be used in
14440    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14441    allowed, VOIDmode otherwise.
14442
14443    If we don't know how to describe it, return 0.  */
14444
14445 static dw_loc_descr_ref
14446 loc_descriptor (rtx rtl, enum machine_mode mode,
14447                 enum var_init_status initialized)
14448 {
14449   dw_loc_descr_ref loc_result = NULL;
14450
14451   switch (GET_CODE (rtl))
14452     {
14453     case SUBREG:
14454       /* The case of a subreg may arise when we have a local (register)
14455          variable or a formal (register) parameter which doesn't quite fill
14456          up an entire register.  For now, just assume that it is
14457          legitimate to make the Dwarf info refer to the whole register which
14458          contains the given subreg.  */
14459       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14460       break;
14461
14462     case REG:
14463       loc_result = reg_loc_descriptor (rtl, initialized);
14464       break;
14465
14466     case MEM:
14467       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14468                                        initialized);
14469       if (loc_result == NULL)
14470         loc_result = tls_mem_loc_descriptor (rtl);
14471       if (loc_result == NULL)
14472         {
14473           rtx new_rtl = avoid_constant_pool_reference (rtl);
14474           if (new_rtl != rtl)
14475             loc_result = loc_descriptor (new_rtl, mode, initialized);
14476         }
14477       break;
14478
14479     case CONCAT:
14480       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14481                                           initialized);
14482       break;
14483
14484     case CONCATN:
14485       loc_result = concatn_loc_descriptor (rtl, initialized);
14486       break;
14487
14488     case VAR_LOCATION:
14489       /* Single part.  */
14490       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14491         {
14492           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14493           if (GET_CODE (loc) == EXPR_LIST)
14494             loc = XEXP (loc, 0);
14495           loc_result = loc_descriptor (loc, mode, initialized);
14496           break;
14497         }
14498
14499       rtl = XEXP (rtl, 1);
14500       /* FALLTHRU */
14501
14502     case PARALLEL:
14503       {
14504         rtvec par_elems = XVEC (rtl, 0);
14505         int num_elem = GET_NUM_ELEM (par_elems);
14506         enum machine_mode mode;
14507         int i;
14508
14509         /* Create the first one, so we have something to add to.  */
14510         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14511                                      VOIDmode, initialized);
14512         if (loc_result == NULL)
14513           return NULL;
14514         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14515         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14516         for (i = 1; i < num_elem; i++)
14517           {
14518             dw_loc_descr_ref temp;
14519
14520             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14521                                    VOIDmode, initialized);
14522             if (temp == NULL)
14523               return NULL;
14524             add_loc_descr (&loc_result, temp);
14525             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14526             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14527           }
14528       }
14529       break;
14530
14531     case CONST_INT:
14532       if (mode != VOIDmode && mode != BLKmode)
14533         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14534                                                     INTVAL (rtl));
14535       break;
14536
14537     case CONST_DOUBLE:
14538       if (mode == VOIDmode)
14539         mode = GET_MODE (rtl);
14540
14541       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14542         {
14543           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14544
14545           /* Note that a CONST_DOUBLE rtx could represent either an integer
14546              or a floating-point constant.  A CONST_DOUBLE is used whenever
14547              the constant requires more than one word in order to be
14548              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14549           loc_result = new_loc_descr (DW_OP_implicit_value,
14550                                       GET_MODE_SIZE (mode), 0);
14551           if (SCALAR_FLOAT_MODE_P (mode))
14552             {
14553               unsigned int length = GET_MODE_SIZE (mode);
14554               unsigned char *array
14555                   = (unsigned char*) ggc_alloc_atomic (length);
14556
14557               insert_float (rtl, array);
14558               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14559               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14560               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14561               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14562             }
14563           else
14564             {
14565               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14566               loc_result->dw_loc_oprnd2.v.val_double
14567                 = rtx_to_double_int (rtl);
14568             }
14569         }
14570       break;
14571
14572     case CONST_VECTOR:
14573       if (mode == VOIDmode)
14574         mode = GET_MODE (rtl);
14575
14576       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14577         {
14578           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14579           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14580           unsigned char *array = (unsigned char *)
14581             ggc_alloc_atomic (length * elt_size);
14582           unsigned int i;
14583           unsigned char *p;
14584
14585           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14586           switch (GET_MODE_CLASS (mode))
14587             {
14588             case MODE_VECTOR_INT:
14589               for (i = 0, p = array; i < length; i++, p += elt_size)
14590                 {
14591                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14592                   double_int val = rtx_to_double_int (elt);
14593
14594                   if (elt_size <= sizeof (HOST_WIDE_INT))
14595                     insert_int (double_int_to_shwi (val), elt_size, p);
14596                   else
14597                     {
14598                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14599                       insert_double (val, p);
14600                     }
14601                 }
14602               break;
14603
14604             case MODE_VECTOR_FLOAT:
14605               for (i = 0, p = array; i < length; i++, p += elt_size)
14606                 {
14607                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14608                   insert_float (elt, p);
14609                 }
14610               break;
14611
14612             default:
14613               gcc_unreachable ();
14614             }
14615
14616           loc_result = new_loc_descr (DW_OP_implicit_value,
14617                                       length * elt_size, 0);
14618           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14619           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14620           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14621           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14622         }
14623       break;
14624
14625     case CONST:
14626       if (mode == VOIDmode
14627           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14628           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14629           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14630         {
14631           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14632           break;
14633         }
14634       /* FALLTHROUGH */
14635     case SYMBOL_REF:
14636       if (!const_ok_for_output (rtl))
14637         break;
14638     case LABEL_REF:
14639       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14640           && (dwarf_version >= 4 || !dwarf_strict))
14641         {
14642           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14643           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14644           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14645           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14646           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14647         }
14648       break;
14649
14650     case DEBUG_IMPLICIT_PTR:
14651       loc_result = implicit_ptr_descriptor (rtl, 0);
14652       break;
14653
14654     case PLUS:
14655       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14656           && CONST_INT_P (XEXP (rtl, 1)))
14657         {
14658           loc_result
14659             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14660           break;
14661         }
14662       /* FALLTHRU */
14663     default:
14664       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14665           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14666           && (dwarf_version >= 4 || !dwarf_strict))
14667         {
14668           /* Value expression.  */
14669           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14670           if (loc_result)
14671             add_loc_descr (&loc_result,
14672                            new_loc_descr (DW_OP_stack_value, 0, 0));
14673         }
14674       break;
14675     }
14676
14677   return loc_result;
14678 }
14679
14680 /* We need to figure out what section we should use as the base for the
14681    address ranges where a given location is valid.
14682    1. If this particular DECL has a section associated with it, use that.
14683    2. If this function has a section associated with it, use that.
14684    3. Otherwise, use the text section.
14685    XXX: If you split a variable across multiple sections, we won't notice.  */
14686
14687 static const char *
14688 secname_for_decl (const_tree decl)
14689 {
14690   const char *secname;
14691
14692   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14693     {
14694       tree sectree = DECL_SECTION_NAME (decl);
14695       secname = TREE_STRING_POINTER (sectree);
14696     }
14697   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14698     {
14699       tree sectree = DECL_SECTION_NAME (current_function_decl);
14700       secname = TREE_STRING_POINTER (sectree);
14701     }
14702   else if (cfun && in_cold_section_p)
14703     secname = crtl->subsections.cold_section_label;
14704   else
14705     secname = text_section_label;
14706
14707   return secname;
14708 }
14709
14710 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14711
14712 static bool
14713 decl_by_reference_p (tree decl)
14714 {
14715   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14716            || TREE_CODE (decl) == VAR_DECL)
14717           && DECL_BY_REFERENCE (decl));
14718 }
14719
14720 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14721    for VARLOC.  */
14722
14723 static dw_loc_descr_ref
14724 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14725                enum var_init_status initialized)
14726 {
14727   int have_address = 0;
14728   dw_loc_descr_ref descr;
14729   enum machine_mode mode;
14730
14731   if (want_address != 2)
14732     {
14733       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14734       /* Single part.  */
14735       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14736         {
14737           varloc = PAT_VAR_LOCATION_LOC (varloc);
14738           if (GET_CODE (varloc) == EXPR_LIST)
14739             varloc = XEXP (varloc, 0);
14740           mode = GET_MODE (varloc);
14741           if (MEM_P (varloc))
14742             {
14743               rtx addr = XEXP (varloc, 0);
14744               descr = mem_loc_descriptor (addr, mode, initialized);
14745               if (descr)
14746                 have_address = 1;
14747               else
14748                 {
14749                   rtx x = avoid_constant_pool_reference (varloc);
14750                   if (x != varloc)
14751                     descr = mem_loc_descriptor (x, mode, initialized);
14752                 }
14753             }
14754           else
14755             descr = mem_loc_descriptor (varloc, mode, initialized);
14756         }
14757       else
14758         return 0;
14759     }
14760   else
14761     {
14762       if (GET_CODE (varloc) == VAR_LOCATION)
14763         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14764       else
14765         mode = DECL_MODE (loc);
14766       descr = loc_descriptor (varloc, mode, initialized);
14767       have_address = 1;
14768     }
14769
14770   if (!descr)
14771     return 0;
14772
14773   if (want_address == 2 && !have_address
14774       && (dwarf_version >= 4 || !dwarf_strict))
14775     {
14776       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14777         {
14778           expansion_failed (loc, NULL_RTX,
14779                             "DWARF address size mismatch");
14780           return 0;
14781         }
14782       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14783       have_address = 1;
14784     }
14785   /* Show if we can't fill the request for an address.  */
14786   if (want_address && !have_address)
14787     {
14788       expansion_failed (loc, NULL_RTX,
14789                         "Want address and only have value");
14790       return 0;
14791     }
14792
14793   /* If we've got an address and don't want one, dereference.  */
14794   if (!want_address && have_address)
14795     {
14796       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14797       enum dwarf_location_atom op;
14798
14799       if (size > DWARF2_ADDR_SIZE || size == -1)
14800         {
14801           expansion_failed (loc, NULL_RTX,
14802                             "DWARF address size mismatch");
14803           return 0;
14804         }
14805       else if (size == DWARF2_ADDR_SIZE)
14806         op = DW_OP_deref;
14807       else
14808         op = DW_OP_deref_size;
14809
14810       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14811     }
14812
14813   return descr;
14814 }
14815
14816 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14817    if it is not possible.  */
14818
14819 static dw_loc_descr_ref
14820 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14821 {
14822   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14823     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14824   else if (dwarf_version >= 3 || !dwarf_strict)
14825     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14826   else
14827     return NULL;
14828 }
14829
14830 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14831    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14832
14833 static dw_loc_descr_ref
14834 dw_sra_loc_expr (tree decl, rtx loc)
14835 {
14836   rtx p;
14837   unsigned int padsize = 0;
14838   dw_loc_descr_ref descr, *descr_tail;
14839   unsigned HOST_WIDE_INT decl_size;
14840   rtx varloc;
14841   enum var_init_status initialized;
14842
14843   if (DECL_SIZE (decl) == NULL
14844       || !host_integerp (DECL_SIZE (decl), 1))
14845     return NULL;
14846
14847   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14848   descr = NULL;
14849   descr_tail = &descr;
14850
14851   for (p = loc; p; p = XEXP (p, 1))
14852     {
14853       unsigned int bitsize = decl_piece_bitsize (p);
14854       rtx loc_note = *decl_piece_varloc_ptr (p);
14855       dw_loc_descr_ref cur_descr;
14856       dw_loc_descr_ref *tail, last = NULL;
14857       unsigned int opsize = 0;
14858
14859       if (loc_note == NULL_RTX
14860           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14861         {
14862           padsize += bitsize;
14863           continue;
14864         }
14865       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14866       varloc = NOTE_VAR_LOCATION (loc_note);
14867       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14868       if (cur_descr == NULL)
14869         {
14870           padsize += bitsize;
14871           continue;
14872         }
14873
14874       /* Check that cur_descr either doesn't use
14875          DW_OP_*piece operations, or their sum is equal
14876          to bitsize.  Otherwise we can't embed it.  */
14877       for (tail = &cur_descr; *tail != NULL;
14878            tail = &(*tail)->dw_loc_next)
14879         if ((*tail)->dw_loc_opc == DW_OP_piece)
14880           {
14881             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14882                       * BITS_PER_UNIT;
14883             last = *tail;
14884           }
14885         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14886           {
14887             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14888             last = *tail;
14889           }
14890
14891       if (last != NULL && opsize != bitsize)
14892         {
14893           padsize += bitsize;
14894           continue;
14895         }
14896
14897       /* If there is a hole, add DW_OP_*piece after empty DWARF
14898          expression, which means that those bits are optimized out.  */
14899       if (padsize)
14900         {
14901           if (padsize > decl_size)
14902             return NULL;
14903           decl_size -= padsize;
14904           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14905           if (*descr_tail == NULL)
14906             return NULL;
14907           descr_tail = &(*descr_tail)->dw_loc_next;
14908           padsize = 0;
14909         }
14910       *descr_tail = cur_descr;
14911       descr_tail = tail;
14912       if (bitsize > decl_size)
14913         return NULL;
14914       decl_size -= bitsize;
14915       if (last == NULL)
14916         {
14917           HOST_WIDE_INT offset = 0;
14918           if (GET_CODE (varloc) == VAR_LOCATION
14919               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14920             {
14921               varloc = PAT_VAR_LOCATION_LOC (varloc);
14922               if (GET_CODE (varloc) == EXPR_LIST)
14923                 varloc = XEXP (varloc, 0);
14924             }
14925           do 
14926             {
14927               if (GET_CODE (varloc) == CONST
14928                   || GET_CODE (varloc) == SIGN_EXTEND
14929                   || GET_CODE (varloc) == ZERO_EXTEND)
14930                 varloc = XEXP (varloc, 0);
14931               else if (GET_CODE (varloc) == SUBREG)
14932                 varloc = SUBREG_REG (varloc);
14933               else
14934                 break;
14935             }
14936           while (1);
14937           /* DW_OP_bit_size offset should be zero for register
14938              or implicit location descriptions and empty location
14939              descriptions, but for memory addresses needs big endian
14940              adjustment.  */
14941           if (MEM_P (varloc))
14942             {
14943               unsigned HOST_WIDE_INT memsize
14944                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14945               if (memsize != bitsize)
14946                 {
14947                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14948                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14949                     return NULL;
14950                   if (memsize < bitsize)
14951                     return NULL;
14952                   if (BITS_BIG_ENDIAN)
14953                     offset = memsize - bitsize;
14954                 }
14955             }
14956
14957           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14958           if (*descr_tail == NULL)
14959             return NULL;
14960           descr_tail = &(*descr_tail)->dw_loc_next;
14961         }
14962     }
14963
14964   /* If there were any non-empty expressions, add padding till the end of
14965      the decl.  */
14966   if (descr != NULL && decl_size != 0)
14967     {
14968       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14969       if (*descr_tail == NULL)
14970         return NULL;
14971     }
14972   return descr;
14973 }
14974
14975 /* Return the dwarf representation of the location list LOC_LIST of
14976    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14977    function.  */
14978
14979 static dw_loc_list_ref
14980 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14981 {
14982   const char *endname, *secname;
14983   rtx varloc;
14984   enum var_init_status initialized;
14985   struct var_loc_node *node;
14986   dw_loc_descr_ref descr;
14987   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14988   dw_loc_list_ref list = NULL;
14989   dw_loc_list_ref *listp = &list;
14990
14991   /* Now that we know what section we are using for a base,
14992      actually construct the list of locations.
14993      The first location information is what is passed to the
14994      function that creates the location list, and the remaining
14995      locations just get added on to that list.
14996      Note that we only know the start address for a location
14997      (IE location changes), so to build the range, we use
14998      the range [current location start, next location start].
14999      This means we have to special case the last node, and generate
15000      a range of [last location start, end of function label].  */
15001
15002   secname = secname_for_decl (decl);
15003
15004   for (node = loc_list->first; node; node = node->next)
15005     if (GET_CODE (node->loc) == EXPR_LIST
15006         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15007       {
15008         if (GET_CODE (node->loc) == EXPR_LIST)
15009           {
15010             /* This requires DW_OP_{,bit_}piece, which is not usable
15011                inside DWARF expressions.  */
15012             if (want_address != 2)
15013               continue;
15014             descr = dw_sra_loc_expr (decl, node->loc);
15015             if (descr == NULL)
15016               continue;
15017           }
15018         else
15019           {
15020             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15021             varloc = NOTE_VAR_LOCATION (node->loc);
15022             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15023           }
15024         if (descr)
15025           {
15026             bool range_across_switch = false;
15027             /* If section switch happens in between node->label
15028                and node->next->label (or end of function) and
15029                we can't emit it as a single entry list,
15030                emit two ranges, first one ending at the end
15031                of first partition and second one starting at the
15032                beginning of second partition.  */
15033             if (node == loc_list->last_before_switch
15034                 && (node != loc_list->first || loc_list->first->next)
15035                 && current_function_decl)
15036               {
15037                 endname = current_fde ()->dw_fde_end;
15038                 range_across_switch = true;
15039               }
15040             /* The variable has a location between NODE->LABEL and
15041                NODE->NEXT->LABEL.  */
15042             else if (node->next)
15043               endname = node->next->label;
15044             /* If the variable has a location at the last label
15045                it keeps its location until the end of function.  */
15046             else if (!current_function_decl)
15047               endname = text_end_label;
15048             else
15049               {
15050                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15051                                              current_function_funcdef_no);
15052                 endname = ggc_strdup (label_id);
15053               }
15054
15055             *listp = new_loc_list (descr, node->label, endname, secname);
15056             listp = &(*listp)->dw_loc_next;
15057
15058             if (range_across_switch)
15059               {
15060                 if (GET_CODE (node->loc) == EXPR_LIST)
15061                   descr = dw_sra_loc_expr (decl, node->loc);
15062                 else
15063                   {
15064                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15065                     varloc = NOTE_VAR_LOCATION (node->loc);
15066                     descr = dw_loc_list_1 (decl, varloc, want_address,
15067                                            initialized);
15068                   }
15069                 gcc_assert (descr);
15070                 /* The variable has a location between NODE->LABEL and
15071                    NODE->NEXT->LABEL.  */
15072                 if (node->next)
15073                   endname = node->next->label;
15074                 else
15075                   endname = current_fde ()->dw_fde_second_end;
15076                 *listp = new_loc_list (descr,
15077                                        current_fde ()->dw_fde_second_begin,
15078                                        endname, secname);
15079                 listp = &(*listp)->dw_loc_next;
15080               }
15081           }
15082       }
15083
15084   /* Try to avoid the overhead of a location list emitting a location
15085      expression instead, but only if we didn't have more than one
15086      location entry in the first place.  If some entries were not
15087      representable, we don't want to pretend a single entry that was
15088      applies to the entire scope in which the variable is
15089      available.  */
15090   if (list && loc_list->first->next)
15091     gen_llsym (list);
15092
15093   return list;
15094 }
15095
15096 /* Return if the loc_list has only single element and thus can be represented
15097    as location description.   */
15098
15099 static bool
15100 single_element_loc_list_p (dw_loc_list_ref list)
15101 {
15102   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15103   return !list->ll_symbol;
15104 }
15105
15106 /* To each location in list LIST add loc descr REF.  */
15107
15108 static void
15109 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15110 {
15111   dw_loc_descr_ref copy;
15112   add_loc_descr (&list->expr, ref);
15113   list = list->dw_loc_next;
15114   while (list)
15115     {
15116       copy = ggc_alloc_dw_loc_descr_node ();
15117       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15118       add_loc_descr (&list->expr, copy);
15119       while (copy->dw_loc_next)
15120         {
15121           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15122           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15123           copy->dw_loc_next = new_copy;
15124           copy = new_copy;
15125         }
15126       list = list->dw_loc_next;
15127     }
15128 }
15129
15130 /* Given two lists RET and LIST
15131    produce location list that is result of adding expression in LIST
15132    to expression in RET on each possition in program.
15133    Might be destructive on both RET and LIST.
15134
15135    TODO: We handle only simple cases of RET or LIST having at most one
15136    element. General case would inolve sorting the lists in program order
15137    and merging them that will need some additional work.
15138    Adding that will improve quality of debug info especially for SRA-ed
15139    structures.  */
15140
15141 static void
15142 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15143 {
15144   if (!list)
15145     return;
15146   if (!*ret)
15147     {
15148       *ret = list;
15149       return;
15150     }
15151   if (!list->dw_loc_next)
15152     {
15153       add_loc_descr_to_each (*ret, list->expr);
15154       return;
15155     }
15156   if (!(*ret)->dw_loc_next)
15157     {
15158       add_loc_descr_to_each (list, (*ret)->expr);
15159       *ret = list;
15160       return;
15161     }
15162   expansion_failed (NULL_TREE, NULL_RTX,
15163                     "Don't know how to merge two non-trivial"
15164                     " location lists.\n");
15165   *ret = NULL;
15166   return;
15167 }
15168
15169 /* LOC is constant expression.  Try a luck, look it up in constant
15170    pool and return its loc_descr of its address.  */
15171
15172 static dw_loc_descr_ref
15173 cst_pool_loc_descr (tree loc)
15174 {
15175   /* Get an RTL for this, if something has been emitted.  */
15176   rtx rtl = lookup_constant_def (loc);
15177   enum machine_mode mode;
15178
15179   if (!rtl || !MEM_P (rtl))
15180     {
15181       gcc_assert (!rtl);
15182       return 0;
15183     }
15184   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15185
15186   /* TODO: We might get more coverage if we was actually delaying expansion
15187      of all expressions till end of compilation when constant pools are fully
15188      populated.  */
15189   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15190     {
15191       expansion_failed (loc, NULL_RTX,
15192                         "CST value in contant pool but not marked.");
15193       return 0;
15194     }
15195   mode = GET_MODE (rtl);
15196   rtl = XEXP (rtl, 0);
15197   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15198 }
15199
15200 /* Return dw_loc_list representing address of addr_expr LOC
15201    by looking for innder INDIRECT_REF expression and turing it
15202    into simple arithmetics.  */
15203
15204 static dw_loc_list_ref
15205 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15206 {
15207   tree obj, offset;
15208   HOST_WIDE_INT bitsize, bitpos, bytepos;
15209   enum machine_mode mode;
15210   int volatilep;
15211   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15212   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15213
15214   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15215                              &bitsize, &bitpos, &offset, &mode,
15216                              &unsignedp, &volatilep, false);
15217   STRIP_NOPS (obj);
15218   if (bitpos % BITS_PER_UNIT)
15219     {
15220       expansion_failed (loc, NULL_RTX, "bitfield access");
15221       return 0;
15222     }
15223   if (!INDIRECT_REF_P (obj))
15224     {
15225       expansion_failed (obj,
15226                         NULL_RTX, "no indirect ref in inner refrence");
15227       return 0;
15228     }
15229   if (!offset && !bitpos)
15230     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15231   else if (toplev
15232            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15233            && (dwarf_version >= 4 || !dwarf_strict))
15234     {
15235       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15236       if (!list_ret)
15237         return 0;
15238       if (offset)
15239         {
15240           /* Variable offset.  */
15241           list_ret1 = loc_list_from_tree (offset, 0);
15242           if (list_ret1 == 0)
15243             return 0;
15244           add_loc_list (&list_ret, list_ret1);
15245           if (!list_ret)
15246             return 0;
15247           add_loc_descr_to_each (list_ret,
15248                                  new_loc_descr (DW_OP_plus, 0, 0));
15249         }
15250       bytepos = bitpos / BITS_PER_UNIT;
15251       if (bytepos > 0)
15252         add_loc_descr_to_each (list_ret,
15253                                new_loc_descr (DW_OP_plus_uconst,
15254                                               bytepos, 0));
15255       else if (bytepos < 0)
15256         loc_list_plus_const (list_ret, bytepos);
15257       add_loc_descr_to_each (list_ret,
15258                              new_loc_descr (DW_OP_stack_value, 0, 0));
15259     }
15260   return list_ret;
15261 }
15262
15263
15264 /* Generate Dwarf location list representing LOC.
15265    If WANT_ADDRESS is false, expression computing LOC will be computed
15266    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15267    if WANT_ADDRESS is 2, expression computing address useable in location
15268      will be returned (i.e. DW_OP_reg can be used
15269      to refer to register values).  */
15270
15271 static dw_loc_list_ref
15272 loc_list_from_tree (tree loc, int want_address)
15273 {
15274   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15275   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15276   int have_address = 0;
15277   enum dwarf_location_atom op;
15278
15279   /* ??? Most of the time we do not take proper care for sign/zero
15280      extending the values properly.  Hopefully this won't be a real
15281      problem...  */
15282
15283   switch (TREE_CODE (loc))
15284     {
15285     case ERROR_MARK:
15286       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15287       return 0;
15288
15289     case PLACEHOLDER_EXPR:
15290       /* This case involves extracting fields from an object to determine the
15291          position of other fields.  We don't try to encode this here.  The
15292          only user of this is Ada, which encodes the needed information using
15293          the names of types.  */
15294       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15295       return 0;
15296
15297     case CALL_EXPR:
15298       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15299       /* There are no opcodes for these operations.  */
15300       return 0;
15301
15302     case PREINCREMENT_EXPR:
15303     case PREDECREMENT_EXPR:
15304     case POSTINCREMENT_EXPR:
15305     case POSTDECREMENT_EXPR:
15306       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15307       /* There are no opcodes for these operations.  */
15308       return 0;
15309
15310     case ADDR_EXPR:
15311       /* If we already want an address, see if there is INDIRECT_REF inside
15312          e.g. for &this->field.  */
15313       if (want_address)
15314         {
15315           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15316                        (loc, want_address == 2);
15317           if (list_ret)
15318             have_address = 1;
15319           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15320                    && (ret = cst_pool_loc_descr (loc)))
15321             have_address = 1;
15322         }
15323         /* Otherwise, process the argument and look for the address.  */
15324       if (!list_ret && !ret)
15325         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15326       else
15327         {
15328           if (want_address)
15329             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15330           return NULL;
15331         }
15332       break;
15333
15334     case VAR_DECL:
15335       if (DECL_THREAD_LOCAL_P (loc))
15336         {
15337           rtx rtl;
15338           enum dwarf_location_atom first_op;
15339           enum dwarf_location_atom second_op;
15340           bool dtprel = false;
15341
15342           if (targetm.have_tls)
15343             {
15344               /* If this is not defined, we have no way to emit the
15345                  data.  */
15346               if (!targetm.asm_out.output_dwarf_dtprel)
15347                 return 0;
15348
15349                /* The way DW_OP_GNU_push_tls_address is specified, we
15350                   can only look up addresses of objects in the current
15351                   module.  We used DW_OP_addr as first op, but that's
15352                   wrong, because DW_OP_addr is relocated by the debug
15353                   info consumer, while DW_OP_GNU_push_tls_address
15354                   operand shouldn't be.  */
15355               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15356                 return 0;
15357               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15358               dtprel = true;
15359               second_op = DW_OP_GNU_push_tls_address;
15360             }
15361           else
15362             {
15363               if (!targetm.emutls.debug_form_tls_address
15364                   || !(dwarf_version >= 3 || !dwarf_strict))
15365                 return 0;
15366               /* We stuffed the control variable into the DECL_VALUE_EXPR
15367                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15368                  no longer appear in gimple code.  We used the control
15369                  variable in specific so that we could pick it up here.  */
15370               loc = DECL_VALUE_EXPR (loc);
15371               first_op = DW_OP_addr;
15372               second_op = DW_OP_form_tls_address;
15373             }
15374
15375           rtl = rtl_for_decl_location (loc);
15376           if (rtl == NULL_RTX)
15377             return 0;
15378
15379           if (!MEM_P (rtl))
15380             return 0;
15381           rtl = XEXP (rtl, 0);
15382           if (! CONSTANT_P (rtl))
15383             return 0;
15384
15385           ret = new_loc_descr (first_op, 0, 0);
15386           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15387           ret->dw_loc_oprnd1.v.val_addr = rtl;
15388           ret->dtprel = dtprel;
15389
15390           ret1 = new_loc_descr (second_op, 0, 0);
15391           add_loc_descr (&ret, ret1);
15392
15393           have_address = 1;
15394           break;
15395         }
15396       /* FALLTHRU */
15397
15398     case PARM_DECL:
15399     case RESULT_DECL:
15400       if (DECL_HAS_VALUE_EXPR_P (loc))
15401         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15402                                    want_address);
15403       /* FALLTHRU */
15404
15405     case FUNCTION_DECL:
15406       {
15407         rtx rtl;
15408         var_loc_list *loc_list = lookup_decl_loc (loc);
15409
15410         if (loc_list && loc_list->first)
15411           {
15412             list_ret = dw_loc_list (loc_list, loc, want_address);
15413             have_address = want_address != 0;
15414             break;
15415           }
15416         rtl = rtl_for_decl_location (loc);
15417         if (rtl == NULL_RTX)
15418           {
15419             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15420             return 0;
15421           }
15422         else if (CONST_INT_P (rtl))
15423           {
15424             HOST_WIDE_INT val = INTVAL (rtl);
15425             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15426               val &= GET_MODE_MASK (DECL_MODE (loc));
15427             ret = int_loc_descriptor (val);
15428           }
15429         else if (GET_CODE (rtl) == CONST_STRING)
15430           {
15431             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15432             return 0;
15433           }
15434         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15435           {
15436             ret = new_loc_descr (DW_OP_addr, 0, 0);
15437             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15438             ret->dw_loc_oprnd1.v.val_addr = rtl;
15439           }
15440         else
15441           {
15442             enum machine_mode mode;
15443
15444             /* Certain constructs can only be represented at top-level.  */
15445             if (want_address == 2)
15446               {
15447                 ret = loc_descriptor (rtl, VOIDmode,
15448                                       VAR_INIT_STATUS_INITIALIZED);
15449                 have_address = 1;
15450               }
15451             else
15452               {
15453                 mode = GET_MODE (rtl);
15454                 if (MEM_P (rtl))
15455                   {
15456                     rtl = XEXP (rtl, 0);
15457                     have_address = 1;
15458                   }
15459                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15460               }
15461             if (!ret)
15462               expansion_failed (loc, rtl,
15463                                 "failed to produce loc descriptor for rtl");
15464           }
15465       }
15466       break;
15467
15468     case MEM_REF:
15469       /* ??? FIXME.  */
15470       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15471         return 0;
15472       /* Fallthru.  */
15473     case INDIRECT_REF:
15474       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15475       have_address = 1;
15476       break;
15477
15478     case COMPOUND_EXPR:
15479       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15480
15481     CASE_CONVERT:
15482     case VIEW_CONVERT_EXPR:
15483     case SAVE_EXPR:
15484     case MODIFY_EXPR:
15485       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15486
15487     case COMPONENT_REF:
15488     case BIT_FIELD_REF:
15489     case ARRAY_REF:
15490     case ARRAY_RANGE_REF:
15491     case REALPART_EXPR:
15492     case IMAGPART_EXPR:
15493       {
15494         tree obj, offset;
15495         HOST_WIDE_INT bitsize, bitpos, bytepos;
15496         enum machine_mode mode;
15497         int volatilep;
15498         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15499
15500         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15501                                    &unsignedp, &volatilep, false);
15502
15503         gcc_assert (obj != loc);
15504
15505         list_ret = loc_list_from_tree (obj,
15506                                        want_address == 2
15507                                        && !bitpos && !offset ? 2 : 1);
15508         /* TODO: We can extract value of the small expression via shifting even
15509            for nonzero bitpos.  */
15510         if (list_ret == 0)
15511           return 0;
15512         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15513           {
15514             expansion_failed (loc, NULL_RTX,
15515                               "bitfield access");
15516             return 0;
15517           }
15518
15519         if (offset != NULL_TREE)
15520           {
15521             /* Variable offset.  */
15522             list_ret1 = loc_list_from_tree (offset, 0);
15523             if (list_ret1 == 0)
15524               return 0;
15525             add_loc_list (&list_ret, list_ret1);
15526             if (!list_ret)
15527               return 0;
15528             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15529           }
15530
15531         bytepos = bitpos / BITS_PER_UNIT;
15532         if (bytepos > 0)
15533           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15534         else if (bytepos < 0)
15535           loc_list_plus_const (list_ret, bytepos);
15536
15537         have_address = 1;
15538         break;
15539       }
15540
15541     case INTEGER_CST:
15542       if ((want_address || !host_integerp (loc, 0))
15543           && (ret = cst_pool_loc_descr (loc)))
15544         have_address = 1;
15545       else if (want_address == 2
15546                && host_integerp (loc, 0)
15547                && (ret = address_of_int_loc_descriptor
15548                            (int_size_in_bytes (TREE_TYPE (loc)),
15549                             tree_low_cst (loc, 0))))
15550         have_address = 1;
15551       else if (host_integerp (loc, 0))
15552         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15553       else
15554         {
15555           expansion_failed (loc, NULL_RTX,
15556                             "Integer operand is not host integer");
15557           return 0;
15558         }
15559       break;
15560
15561     case CONSTRUCTOR:
15562     case REAL_CST:
15563     case STRING_CST:
15564     case COMPLEX_CST:
15565       if ((ret = cst_pool_loc_descr (loc)))
15566         have_address = 1;
15567       else
15568       /* We can construct small constants here using int_loc_descriptor.  */
15569         expansion_failed (loc, NULL_RTX,
15570                           "constructor or constant not in constant pool");
15571       break;
15572
15573     case TRUTH_AND_EXPR:
15574     case TRUTH_ANDIF_EXPR:
15575     case BIT_AND_EXPR:
15576       op = DW_OP_and;
15577       goto do_binop;
15578
15579     case TRUTH_XOR_EXPR:
15580     case BIT_XOR_EXPR:
15581       op = DW_OP_xor;
15582       goto do_binop;
15583
15584     case TRUTH_OR_EXPR:
15585     case TRUTH_ORIF_EXPR:
15586     case BIT_IOR_EXPR:
15587       op = DW_OP_or;
15588       goto do_binop;
15589
15590     case FLOOR_DIV_EXPR:
15591     case CEIL_DIV_EXPR:
15592     case ROUND_DIV_EXPR:
15593     case TRUNC_DIV_EXPR:
15594       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15595         return 0;
15596       op = DW_OP_div;
15597       goto do_binop;
15598
15599     case MINUS_EXPR:
15600       op = DW_OP_minus;
15601       goto do_binop;
15602
15603     case FLOOR_MOD_EXPR:
15604     case CEIL_MOD_EXPR:
15605     case ROUND_MOD_EXPR:
15606     case TRUNC_MOD_EXPR:
15607       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15608         {
15609           op = DW_OP_mod;
15610           goto do_binop;
15611         }
15612       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15613       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15614       if (list_ret == 0 || list_ret1 == 0)
15615         return 0;
15616
15617       add_loc_list (&list_ret, list_ret1);
15618       if (list_ret == 0)
15619         return 0;
15620       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15621       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15622       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15623       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15624       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15625       break;
15626
15627     case MULT_EXPR:
15628       op = DW_OP_mul;
15629       goto do_binop;
15630
15631     case LSHIFT_EXPR:
15632       op = DW_OP_shl;
15633       goto do_binop;
15634
15635     case RSHIFT_EXPR:
15636       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15637       goto do_binop;
15638
15639     case POINTER_PLUS_EXPR:
15640     case PLUS_EXPR:
15641       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15642         {
15643           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15644           if (list_ret == 0)
15645             return 0;
15646
15647           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15648           break;
15649         }
15650
15651       op = DW_OP_plus;
15652       goto do_binop;
15653
15654     case LE_EXPR:
15655       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15656         return 0;
15657
15658       op = DW_OP_le;
15659       goto do_binop;
15660
15661     case GE_EXPR:
15662       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15663         return 0;
15664
15665       op = DW_OP_ge;
15666       goto do_binop;
15667
15668     case LT_EXPR:
15669       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15670         return 0;
15671
15672       op = DW_OP_lt;
15673       goto do_binop;
15674
15675     case GT_EXPR:
15676       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15677         return 0;
15678
15679       op = DW_OP_gt;
15680       goto do_binop;
15681
15682     case EQ_EXPR:
15683       op = DW_OP_eq;
15684       goto do_binop;
15685
15686     case NE_EXPR:
15687       op = DW_OP_ne;
15688       goto do_binop;
15689
15690     do_binop:
15691       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15692       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15693       if (list_ret == 0 || list_ret1 == 0)
15694         return 0;
15695
15696       add_loc_list (&list_ret, list_ret1);
15697       if (list_ret == 0)
15698         return 0;
15699       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15700       break;
15701
15702     case TRUTH_NOT_EXPR:
15703     case BIT_NOT_EXPR:
15704       op = DW_OP_not;
15705       goto do_unop;
15706
15707     case ABS_EXPR:
15708       op = DW_OP_abs;
15709       goto do_unop;
15710
15711     case NEGATE_EXPR:
15712       op = DW_OP_neg;
15713       goto do_unop;
15714
15715     do_unop:
15716       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15717       if (list_ret == 0)
15718         return 0;
15719
15720       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15721       break;
15722
15723     case MIN_EXPR:
15724     case MAX_EXPR:
15725       {
15726         const enum tree_code code =
15727           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15728
15729         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15730                       build2 (code, integer_type_node,
15731                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15732                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15733       }
15734
15735       /* ... fall through ...  */
15736
15737     case COND_EXPR:
15738       {
15739         dw_loc_descr_ref lhs
15740           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15741         dw_loc_list_ref rhs
15742           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15743         dw_loc_descr_ref bra_node, jump_node, tmp;
15744
15745         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15746         if (list_ret == 0 || lhs == 0 || rhs == 0)
15747           return 0;
15748
15749         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15750         add_loc_descr_to_each (list_ret, bra_node);
15751
15752         add_loc_list (&list_ret, rhs);
15753         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15754         add_loc_descr_to_each (list_ret, jump_node);
15755
15756         add_loc_descr_to_each (list_ret, lhs);
15757         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15758         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15759
15760         /* ??? Need a node to point the skip at.  Use a nop.  */
15761         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15762         add_loc_descr_to_each (list_ret, tmp);
15763         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15764         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15765       }
15766       break;
15767
15768     case FIX_TRUNC_EXPR:
15769       return 0;
15770
15771     default:
15772       /* Leave front-end specific codes as simply unknown.  This comes
15773          up, for instance, with the C STMT_EXPR.  */
15774       if ((unsigned int) TREE_CODE (loc)
15775           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15776         {
15777           expansion_failed (loc, NULL_RTX,
15778                             "language specific tree node");
15779           return 0;
15780         }
15781
15782 #ifdef ENABLE_CHECKING
15783       /* Otherwise this is a generic code; we should just lists all of
15784          these explicitly.  We forgot one.  */
15785       gcc_unreachable ();
15786 #else
15787       /* In a release build, we want to degrade gracefully: better to
15788          generate incomplete debugging information than to crash.  */
15789       return NULL;
15790 #endif
15791     }
15792
15793   if (!ret && !list_ret)
15794     return 0;
15795
15796   if (want_address == 2 && !have_address
15797       && (dwarf_version >= 4 || !dwarf_strict))
15798     {
15799       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15800         {
15801           expansion_failed (loc, NULL_RTX,
15802                             "DWARF address size mismatch");
15803           return 0;
15804         }
15805       if (ret)
15806         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15807       else
15808         add_loc_descr_to_each (list_ret,
15809                                new_loc_descr (DW_OP_stack_value, 0, 0));
15810       have_address = 1;
15811     }
15812   /* Show if we can't fill the request for an address.  */
15813   if (want_address && !have_address)
15814     {
15815       expansion_failed (loc, NULL_RTX,
15816                         "Want address and only have value");
15817       return 0;
15818     }
15819
15820   gcc_assert (!ret || !list_ret);
15821
15822   /* If we've got an address and don't want one, dereference.  */
15823   if (!want_address && have_address)
15824     {
15825       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15826
15827       if (size > DWARF2_ADDR_SIZE || size == -1)
15828         {
15829           expansion_failed (loc, NULL_RTX,
15830                             "DWARF address size mismatch");
15831           return 0;
15832         }
15833       else if (size == DWARF2_ADDR_SIZE)
15834         op = DW_OP_deref;
15835       else
15836         op = DW_OP_deref_size;
15837
15838       if (ret)
15839         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15840       else
15841         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15842     }
15843   if (ret)
15844     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15845
15846   return list_ret;
15847 }
15848
15849 /* Same as above but return only single location expression.  */
15850 static dw_loc_descr_ref
15851 loc_descriptor_from_tree (tree loc, int want_address)
15852 {
15853   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15854   if (!ret)
15855     return NULL;
15856   if (ret->dw_loc_next)
15857     {
15858       expansion_failed (loc, NULL_RTX,
15859                         "Location list where only loc descriptor needed");
15860       return NULL;
15861     }
15862   return ret->expr;
15863 }
15864
15865 /* Given a value, round it up to the lowest multiple of `boundary'
15866    which is not less than the value itself.  */
15867
15868 static inline HOST_WIDE_INT
15869 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15870 {
15871   return (((value + boundary - 1) / boundary) * boundary);
15872 }
15873
15874 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15875    pointer to the declared type for the relevant field variable, or return
15876    `integer_type_node' if the given node turns out to be an
15877    ERROR_MARK node.  */
15878
15879 static inline tree
15880 field_type (const_tree decl)
15881 {
15882   tree type;
15883
15884   if (TREE_CODE (decl) == ERROR_MARK)
15885     return integer_type_node;
15886
15887   type = DECL_BIT_FIELD_TYPE (decl);
15888   if (type == NULL_TREE)
15889     type = TREE_TYPE (decl);
15890
15891   return type;
15892 }
15893
15894 /* Given a pointer to a tree node, return the alignment in bits for
15895    it, or else return BITS_PER_WORD if the node actually turns out to
15896    be an ERROR_MARK node.  */
15897
15898 static inline unsigned
15899 simple_type_align_in_bits (const_tree type)
15900 {
15901   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15902 }
15903
15904 static inline unsigned
15905 simple_decl_align_in_bits (const_tree decl)
15906 {
15907   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15908 }
15909
15910 /* Return the result of rounding T up to ALIGN.  */
15911
15912 static inline double_int
15913 round_up_to_align (double_int t, unsigned int align)
15914 {
15915   double_int alignd = uhwi_to_double_int (align);
15916   t = double_int_add (t, alignd);
15917   t = double_int_add (t, double_int_minus_one);
15918   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15919   t = double_int_mul (t, alignd);
15920   return t;
15921 }
15922
15923 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15924    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15925    or return 0 if we are unable to determine what that offset is, either
15926    because the argument turns out to be a pointer to an ERROR_MARK node, or
15927    because the offset is actually variable.  (We can't handle the latter case
15928    just yet).  */
15929
15930 static HOST_WIDE_INT
15931 field_byte_offset (const_tree decl)
15932 {
15933   double_int object_offset_in_bits;
15934   double_int object_offset_in_bytes;
15935   double_int bitpos_int;
15936
15937   if (TREE_CODE (decl) == ERROR_MARK)
15938     return 0;
15939
15940   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15941
15942   /* We cannot yet cope with fields whose positions are variable, so
15943      for now, when we see such things, we simply return 0.  Someday, we may
15944      be able to handle such cases, but it will be damn difficult.  */
15945   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15946     return 0;
15947
15948   bitpos_int = tree_to_double_int (bit_position (decl));
15949
15950 #ifdef PCC_BITFIELD_TYPE_MATTERS
15951   if (PCC_BITFIELD_TYPE_MATTERS)
15952     {
15953       tree type;
15954       tree field_size_tree;
15955       double_int deepest_bitpos;
15956       double_int field_size_in_bits;
15957       unsigned int type_align_in_bits;
15958       unsigned int decl_align_in_bits;
15959       double_int type_size_in_bits;
15960
15961       type = field_type (decl);
15962       type_size_in_bits = double_int_type_size_in_bits (type);
15963       type_align_in_bits = simple_type_align_in_bits (type);
15964
15965       field_size_tree = DECL_SIZE (decl);
15966
15967       /* The size could be unspecified if there was an error, or for
15968          a flexible array member.  */
15969       if (!field_size_tree)
15970         field_size_tree = bitsize_zero_node;
15971
15972       /* If the size of the field is not constant, use the type size.  */
15973       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15974         field_size_in_bits = tree_to_double_int (field_size_tree);
15975       else
15976         field_size_in_bits = type_size_in_bits;
15977
15978       decl_align_in_bits = simple_decl_align_in_bits (decl);
15979
15980       /* The GCC front-end doesn't make any attempt to keep track of the
15981          starting bit offset (relative to the start of the containing
15982          structure type) of the hypothetical "containing object" for a
15983          bit-field.  Thus, when computing the byte offset value for the
15984          start of the "containing object" of a bit-field, we must deduce
15985          this information on our own. This can be rather tricky to do in
15986          some cases.  For example, handling the following structure type
15987          definition when compiling for an i386/i486 target (which only
15988          aligns long long's to 32-bit boundaries) can be very tricky:
15989
15990          struct S { int field1; long long field2:31; };
15991
15992          Fortunately, there is a simple rule-of-thumb which can be used
15993          in such cases.  When compiling for an i386/i486, GCC will
15994          allocate 8 bytes for the structure shown above.  It decides to
15995          do this based upon one simple rule for bit-field allocation.
15996          GCC allocates each "containing object" for each bit-field at
15997          the first (i.e. lowest addressed) legitimate alignment boundary
15998          (based upon the required minimum alignment for the declared
15999          type of the field) which it can possibly use, subject to the
16000          condition that there is still enough available space remaining
16001          in the containing object (when allocated at the selected point)
16002          to fully accommodate all of the bits of the bit-field itself.
16003
16004          This simple rule makes it obvious why GCC allocates 8 bytes for
16005          each object of the structure type shown above.  When looking
16006          for a place to allocate the "containing object" for `field2',
16007          the compiler simply tries to allocate a 64-bit "containing
16008          object" at each successive 32-bit boundary (starting at zero)
16009          until it finds a place to allocate that 64- bit field such that
16010          at least 31 contiguous (and previously unallocated) bits remain
16011          within that selected 64 bit field.  (As it turns out, for the
16012          example above, the compiler finds it is OK to allocate the
16013          "containing object" 64-bit field at bit-offset zero within the
16014          structure type.)
16015
16016          Here we attempt to work backwards from the limited set of facts
16017          we're given, and we try to deduce from those facts, where GCC
16018          must have believed that the containing object started (within
16019          the structure type). The value we deduce is then used (by the
16020          callers of this routine) to generate DW_AT_location and
16021          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16022          the case of DW_AT_location, regular fields as well).  */
16023
16024       /* Figure out the bit-distance from the start of the structure to
16025          the "deepest" bit of the bit-field.  */
16026       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16027
16028       /* This is the tricky part.  Use some fancy footwork to deduce
16029          where the lowest addressed bit of the containing object must
16030          be.  */
16031       object_offset_in_bits
16032         = double_int_sub (deepest_bitpos, type_size_in_bits);
16033
16034       /* Round up to type_align by default.  This works best for
16035          bitfields.  */
16036       object_offset_in_bits
16037         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16038
16039       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16040         {
16041           object_offset_in_bits
16042             = double_int_sub (deepest_bitpos, type_size_in_bits);
16043
16044           /* Round up to decl_align instead.  */
16045           object_offset_in_bits
16046             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16047         }
16048     }
16049   else
16050 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16051     object_offset_in_bits = bitpos_int;
16052
16053   object_offset_in_bytes
16054     = double_int_div (object_offset_in_bits,
16055                       uhwi_to_double_int (BITS_PER_UNIT), true,
16056                       TRUNC_DIV_EXPR);
16057   return double_int_to_shwi (object_offset_in_bytes);
16058 }
16059 \f
16060 /* The following routines define various Dwarf attributes and any data
16061    associated with them.  */
16062
16063 /* Add a location description attribute value to a DIE.
16064
16065    This emits location attributes suitable for whole variables and
16066    whole parameters.  Note that the location attributes for struct fields are
16067    generated by the routine `data_member_location_attribute' below.  */
16068
16069 static inline void
16070 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16071                              dw_loc_list_ref descr)
16072 {
16073   if (descr == 0)
16074     return;
16075   if (single_element_loc_list_p (descr))
16076     add_AT_loc (die, attr_kind, descr->expr);
16077   else
16078     add_AT_loc_list (die, attr_kind, descr);
16079 }
16080
16081 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16082
16083 static void
16084 add_accessibility_attribute (dw_die_ref die, tree decl)
16085 {
16086   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16087      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16088      the default has always been DW_ACCESS_public.  */
16089   if (TREE_PROTECTED (decl))
16090     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16091   else if (TREE_PRIVATE (decl))
16092     {
16093       if (dwarf_version == 2
16094           || die->die_parent == NULL
16095           || die->die_parent->die_tag != DW_TAG_class_type)
16096         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16097     }
16098   else if (dwarf_version > 2
16099            && die->die_parent
16100            && die->die_parent->die_tag == DW_TAG_class_type)
16101     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16102 }
16103
16104 /* Attach the specialized form of location attribute used for data members of
16105    struct and union types.  In the special case of a FIELD_DECL node which
16106    represents a bit-field, the "offset" part of this special location
16107    descriptor must indicate the distance in bytes from the lowest-addressed
16108    byte of the containing struct or union type to the lowest-addressed byte of
16109    the "containing object" for the bit-field.  (See the `field_byte_offset'
16110    function above).
16111
16112    For any given bit-field, the "containing object" is a hypothetical object
16113    (of some integral or enum type) within which the given bit-field lives.  The
16114    type of this hypothetical "containing object" is always the same as the
16115    declared type of the individual bit-field itself (for GCC anyway... the
16116    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16117    bytes) of the hypothetical "containing object" which will be given in the
16118    DW_AT_byte_size attribute for this bit-field.  (See the
16119    `byte_size_attribute' function below.)  It is also used when calculating the
16120    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16121    function below.)  */
16122
16123 static void
16124 add_data_member_location_attribute (dw_die_ref die, tree decl)
16125 {
16126   HOST_WIDE_INT offset;
16127   dw_loc_descr_ref loc_descr = 0;
16128
16129   if (TREE_CODE (decl) == TREE_BINFO)
16130     {
16131       /* We're working on the TAG_inheritance for a base class.  */
16132       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16133         {
16134           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16135              aren't at a fixed offset from all (sub)objects of the same
16136              type.  We need to extract the appropriate offset from our
16137              vtable.  The following dwarf expression means
16138
16139                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16140
16141              This is specific to the V3 ABI, of course.  */
16142
16143           dw_loc_descr_ref tmp;
16144
16145           /* Make a copy of the object address.  */
16146           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16147           add_loc_descr (&loc_descr, tmp);
16148
16149           /* Extract the vtable address.  */
16150           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16151           add_loc_descr (&loc_descr, tmp);
16152
16153           /* Calculate the address of the offset.  */
16154           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16155           gcc_assert (offset < 0);
16156
16157           tmp = int_loc_descriptor (-offset);
16158           add_loc_descr (&loc_descr, tmp);
16159           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16160           add_loc_descr (&loc_descr, tmp);
16161
16162           /* Extract the offset.  */
16163           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16164           add_loc_descr (&loc_descr, tmp);
16165
16166           /* Add it to the object address.  */
16167           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16168           add_loc_descr (&loc_descr, tmp);
16169         }
16170       else
16171         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16172     }
16173   else
16174     offset = field_byte_offset (decl);
16175
16176   if (! loc_descr)
16177     {
16178       if (dwarf_version > 2)
16179         {
16180           /* Don't need to output a location expression, just the constant. */
16181           if (offset < 0)
16182             add_AT_int (die, DW_AT_data_member_location, offset);
16183           else
16184             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16185           return;
16186         }
16187       else
16188         {
16189           enum dwarf_location_atom op;
16190
16191           /* The DWARF2 standard says that we should assume that the structure
16192              address is already on the stack, so we can specify a structure
16193              field address by using DW_OP_plus_uconst.  */
16194
16195 #ifdef MIPS_DEBUGGING_INFO
16196           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16197              operator correctly.  It works only if we leave the offset on the
16198              stack.  */
16199           op = DW_OP_constu;
16200 #else
16201           op = DW_OP_plus_uconst;
16202 #endif
16203
16204           loc_descr = new_loc_descr (op, offset, 0);
16205         }
16206     }
16207
16208   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16209 }
16210
16211 /* Writes integer values to dw_vec_const array.  */
16212
16213 static void
16214 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16215 {
16216   while (size != 0)
16217     {
16218       *dest++ = val & 0xff;
16219       val >>= 8;
16220       --size;
16221     }
16222 }
16223
16224 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16225
16226 static HOST_WIDE_INT
16227 extract_int (const unsigned char *src, unsigned int size)
16228 {
16229   HOST_WIDE_INT val = 0;
16230
16231   src += size;
16232   while (size != 0)
16233     {
16234       val <<= 8;
16235       val |= *--src & 0xff;
16236       --size;
16237     }
16238   return val;
16239 }
16240
16241 /* Writes double_int values to dw_vec_const array.  */
16242
16243 static void
16244 insert_double (double_int val, unsigned char *dest)
16245 {
16246   unsigned char *p0 = dest;
16247   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16248
16249   if (WORDS_BIG_ENDIAN)
16250     {
16251       p0 = p1;
16252       p1 = dest;
16253     }
16254
16255   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16256   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16257 }
16258
16259 /* Writes floating point values to dw_vec_const array.  */
16260
16261 static void
16262 insert_float (const_rtx rtl, unsigned char *array)
16263 {
16264   REAL_VALUE_TYPE rv;
16265   long val[4];
16266   int i;
16267
16268   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16269   real_to_target (val, &rv, GET_MODE (rtl));
16270
16271   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16272   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16273     {
16274       insert_int (val[i], 4, array);
16275       array += 4;
16276     }
16277 }
16278
16279 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16280    does not have a "location" either in memory or in a register.  These
16281    things can arise in GNU C when a constant is passed as an actual parameter
16282    to an inlined function.  They can also arise in C++ where declared
16283    constants do not necessarily get memory "homes".  */
16284
16285 static bool
16286 add_const_value_attribute (dw_die_ref die, rtx rtl)
16287 {
16288   switch (GET_CODE (rtl))
16289     {
16290     case CONST_INT:
16291       {
16292         HOST_WIDE_INT val = INTVAL (rtl);
16293
16294         if (val < 0)
16295           add_AT_int (die, DW_AT_const_value, val);
16296         else
16297           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16298       }
16299       return true;
16300
16301     case CONST_DOUBLE:
16302       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16303          floating-point constant.  A CONST_DOUBLE is used whenever the
16304          constant requires more than one word in order to be adequately
16305          represented.  */
16306       {
16307         enum machine_mode mode = GET_MODE (rtl);
16308
16309         if (SCALAR_FLOAT_MODE_P (mode))
16310           {
16311             unsigned int length = GET_MODE_SIZE (mode);
16312             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16313
16314             insert_float (rtl, array);
16315             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16316           }
16317         else
16318           add_AT_double (die, DW_AT_const_value,
16319                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16320       }
16321       return true;
16322
16323     case CONST_VECTOR:
16324       {
16325         enum machine_mode mode = GET_MODE (rtl);
16326         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16327         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16328         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16329           (length * elt_size);
16330         unsigned int i;
16331         unsigned char *p;
16332
16333         switch (GET_MODE_CLASS (mode))
16334           {
16335           case MODE_VECTOR_INT:
16336             for (i = 0, p = array; i < length; i++, p += elt_size)
16337               {
16338                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16339                 double_int val = rtx_to_double_int (elt);
16340
16341                 if (elt_size <= sizeof (HOST_WIDE_INT))
16342                   insert_int (double_int_to_shwi (val), elt_size, p);
16343                 else
16344                   {
16345                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16346                     insert_double (val, p);
16347                   }
16348               }
16349             break;
16350
16351           case MODE_VECTOR_FLOAT:
16352             for (i = 0, p = array; i < length; i++, p += elt_size)
16353               {
16354                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16355                 insert_float (elt, p);
16356               }
16357             break;
16358
16359           default:
16360             gcc_unreachable ();
16361           }
16362
16363         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16364       }
16365       return true;
16366
16367     case CONST_STRING:
16368       if (dwarf_version >= 4 || !dwarf_strict)
16369         {
16370           dw_loc_descr_ref loc_result;
16371           resolve_one_addr (&rtl, NULL);
16372         rtl_addr:
16373           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16374           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16375           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16376           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16377           add_AT_loc (die, DW_AT_location, loc_result);
16378           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16379           return true;
16380         }
16381       return false;
16382
16383     case CONST:
16384       if (CONSTANT_P (XEXP (rtl, 0)))
16385         return add_const_value_attribute (die, XEXP (rtl, 0));
16386       /* FALLTHROUGH */
16387     case SYMBOL_REF:
16388       if (!const_ok_for_output (rtl))
16389         return false;
16390     case LABEL_REF:
16391       if (dwarf_version >= 4 || !dwarf_strict)
16392         goto rtl_addr;
16393       return false;
16394
16395     case PLUS:
16396       /* In cases where an inlined instance of an inline function is passed
16397          the address of an `auto' variable (which is local to the caller) we
16398          can get a situation where the DECL_RTL of the artificial local
16399          variable (for the inlining) which acts as a stand-in for the
16400          corresponding formal parameter (of the inline function) will look
16401          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16402          exactly a compile-time constant expression, but it isn't the address
16403          of the (artificial) local variable either.  Rather, it represents the
16404          *value* which the artificial local variable always has during its
16405          lifetime.  We currently have no way to represent such quasi-constant
16406          values in Dwarf, so for now we just punt and generate nothing.  */
16407       return false;
16408
16409     case HIGH:
16410     case CONST_FIXED:
16411       return false;
16412
16413     case MEM:
16414       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16415           && MEM_READONLY_P (rtl)
16416           && GET_MODE (rtl) == BLKmode)
16417         {
16418           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16419           return true;
16420         }
16421       return false;
16422
16423     default:
16424       /* No other kinds of rtx should be possible here.  */
16425       gcc_unreachable ();
16426     }
16427   return false;
16428 }
16429
16430 /* Determine whether the evaluation of EXPR references any variables
16431    or functions which aren't otherwise used (and therefore may not be
16432    output).  */
16433 static tree
16434 reference_to_unused (tree * tp, int * walk_subtrees,
16435                      void * data ATTRIBUTE_UNUSED)
16436 {
16437   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16438     *walk_subtrees = 0;
16439
16440   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16441       && ! TREE_ASM_WRITTEN (*tp))
16442     return *tp;
16443   /* ???  The C++ FE emits debug information for using decls, so
16444      putting gcc_unreachable here falls over.  See PR31899.  For now
16445      be conservative.  */
16446   else if (!cgraph_global_info_ready
16447            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16448     return *tp;
16449   else if (TREE_CODE (*tp) == VAR_DECL)
16450     {
16451       struct varpool_node *node = varpool_get_node (*tp);
16452       if (!node || !node->needed)
16453         return *tp;
16454     }
16455   else if (TREE_CODE (*tp) == FUNCTION_DECL
16456            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16457     {
16458       /* The call graph machinery must have finished analyzing,
16459          optimizing and gimplifying the CU by now.
16460          So if *TP has no call graph node associated
16461          to it, it means *TP will not be emitted.  */
16462       if (!cgraph_get_node (*tp))
16463         return *tp;
16464     }
16465   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16466     return *tp;
16467
16468   return NULL_TREE;
16469 }
16470
16471 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16472    for use in a later add_const_value_attribute call.  */
16473
16474 static rtx
16475 rtl_for_decl_init (tree init, tree type)
16476 {
16477   rtx rtl = NULL_RTX;
16478
16479   STRIP_NOPS (init);
16480
16481   /* If a variable is initialized with a string constant without embedded
16482      zeros, build CONST_STRING.  */
16483   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16484     {
16485       tree enttype = TREE_TYPE (type);
16486       tree domain = TYPE_DOMAIN (type);
16487       enum machine_mode mode = TYPE_MODE (enttype);
16488
16489       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16490           && domain
16491           && integer_zerop (TYPE_MIN_VALUE (domain))
16492           && compare_tree_int (TYPE_MAX_VALUE (domain),
16493                                TREE_STRING_LENGTH (init) - 1) == 0
16494           && ((size_t) TREE_STRING_LENGTH (init)
16495               == strlen (TREE_STRING_POINTER (init)) + 1))
16496         {
16497           rtl = gen_rtx_CONST_STRING (VOIDmode,
16498                                       ggc_strdup (TREE_STRING_POINTER (init)));
16499           rtl = gen_rtx_MEM (BLKmode, rtl);
16500           MEM_READONLY_P (rtl) = 1;
16501         }
16502     }
16503   /* Other aggregates, and complex values, could be represented using
16504      CONCAT: FIXME!  */
16505   else if (AGGREGATE_TYPE_P (type)
16506            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16507                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16508            || TREE_CODE (type) == COMPLEX_TYPE)
16509     ;
16510   /* Vectors only work if their mode is supported by the target.
16511      FIXME: generic vectors ought to work too.  */
16512   else if (TREE_CODE (type) == VECTOR_TYPE
16513            && !VECTOR_MODE_P (TYPE_MODE (type)))
16514     ;
16515   /* If the initializer is something that we know will expand into an
16516      immediate RTL constant, expand it now.  We must be careful not to
16517      reference variables which won't be output.  */
16518   else if (initializer_constant_valid_p (init, type)
16519            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16520     {
16521       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16522          possible.  */
16523       if (TREE_CODE (type) == VECTOR_TYPE)
16524         switch (TREE_CODE (init))
16525           {
16526           case VECTOR_CST:
16527             break;
16528           case CONSTRUCTOR:
16529             if (TREE_CONSTANT (init))
16530               {
16531                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16532                 bool constant_p = true;
16533                 tree value;
16534                 unsigned HOST_WIDE_INT ix;
16535
16536                 /* Even when ctor is constant, it might contain non-*_CST
16537                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16538                    belong into VECTOR_CST nodes.  */
16539                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16540                   if (!CONSTANT_CLASS_P (value))
16541                     {
16542                       constant_p = false;
16543                       break;
16544                     }
16545
16546                 if (constant_p)
16547                   {
16548                     init = build_vector_from_ctor (type, elts);
16549                     break;
16550                   }
16551               }
16552             /* FALLTHRU */
16553
16554           default:
16555             return NULL;
16556           }
16557
16558       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16559
16560       /* If expand_expr returns a MEM, it wasn't immediate.  */
16561       gcc_assert (!rtl || !MEM_P (rtl));
16562     }
16563
16564   return rtl;
16565 }
16566
16567 /* Generate RTL for the variable DECL to represent its location.  */
16568
16569 static rtx
16570 rtl_for_decl_location (tree decl)
16571 {
16572   rtx rtl;
16573
16574   /* Here we have to decide where we are going to say the parameter "lives"
16575      (as far as the debugger is concerned).  We only have a couple of
16576      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16577
16578      DECL_RTL normally indicates where the parameter lives during most of the
16579      activation of the function.  If optimization is enabled however, this
16580      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16581      that the parameter doesn't really live anywhere (as far as the code
16582      generation parts of GCC are concerned) during most of the function's
16583      activation.  That will happen (for example) if the parameter is never
16584      referenced within the function.
16585
16586      We could just generate a location descriptor here for all non-NULL
16587      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16588      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16589      where DECL_RTL is NULL or is a pseudo-reg.
16590
16591      Note however that we can only get away with using DECL_INCOMING_RTL as
16592      a backup substitute for DECL_RTL in certain limited cases.  In cases
16593      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16594      we can be sure that the parameter was passed using the same type as it is
16595      declared to have within the function, and that its DECL_INCOMING_RTL
16596      points us to a place where a value of that type is passed.
16597
16598      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16599      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16600      because in these cases DECL_INCOMING_RTL points us to a value of some
16601      type which is *different* from the type of the parameter itself.  Thus,
16602      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16603      such cases, the debugger would end up (for example) trying to fetch a
16604      `float' from a place which actually contains the first part of a
16605      `double'.  That would lead to really incorrect and confusing
16606      output at debug-time.
16607
16608      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16609      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16610      are a couple of exceptions however.  On little-endian machines we can
16611      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16612      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16613      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16614      when (on a little-endian machine) a non-prototyped function has a
16615      parameter declared to be of type `short' or `char'.  In such cases,
16616      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16617      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16618      passed `int' value.  If the debugger then uses that address to fetch
16619      a `short' or a `char' (on a little-endian machine) the result will be
16620      the correct data, so we allow for such exceptional cases below.
16621
16622      Note that our goal here is to describe the place where the given formal
16623      parameter lives during most of the function's activation (i.e. between the
16624      end of the prologue and the start of the epilogue).  We'll do that as best
16625      as we can. Note however that if the given formal parameter is modified
16626      sometime during the execution of the function, then a stack backtrace (at
16627      debug-time) will show the function as having been called with the *new*
16628      value rather than the value which was originally passed in.  This happens
16629      rarely enough that it is not a major problem, but it *is* a problem, and
16630      I'd like to fix it.
16631
16632      A future version of dwarf2out.c may generate two additional attributes for
16633      any given DW_TAG_formal_parameter DIE which will describe the "passed
16634      type" and the "passed location" for the given formal parameter in addition
16635      to the attributes we now generate to indicate the "declared type" and the
16636      "active location" for each parameter.  This additional set of attributes
16637      could be used by debuggers for stack backtraces. Separately, note that
16638      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16639      This happens (for example) for inlined-instances of inline function formal
16640      parameters which are never referenced.  This really shouldn't be
16641      happening.  All PARM_DECL nodes should get valid non-NULL
16642      DECL_INCOMING_RTL values.  FIXME.  */
16643
16644   /* Use DECL_RTL as the "location" unless we find something better.  */
16645   rtl = DECL_RTL_IF_SET (decl);
16646
16647   /* When generating abstract instances, ignore everything except
16648      constants, symbols living in memory, and symbols living in
16649      fixed registers.  */
16650   if (! reload_completed)
16651     {
16652       if (rtl
16653           && (CONSTANT_P (rtl)
16654               || (MEM_P (rtl)
16655                   && CONSTANT_P (XEXP (rtl, 0)))
16656               || (REG_P (rtl)
16657                   && TREE_CODE (decl) == VAR_DECL
16658                   && TREE_STATIC (decl))))
16659         {
16660           rtl = targetm.delegitimize_address (rtl);
16661           return rtl;
16662         }
16663       rtl = NULL_RTX;
16664     }
16665   else if (TREE_CODE (decl) == PARM_DECL)
16666     {
16667       if (rtl == NULL_RTX
16668           || is_pseudo_reg (rtl)
16669           || (MEM_P (rtl)
16670               && is_pseudo_reg (XEXP (rtl, 0))
16671               && DECL_INCOMING_RTL (decl)
16672               && MEM_P (DECL_INCOMING_RTL (decl))
16673               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16674         {
16675           tree declared_type = TREE_TYPE (decl);
16676           tree passed_type = DECL_ARG_TYPE (decl);
16677           enum machine_mode dmode = TYPE_MODE (declared_type);
16678           enum machine_mode pmode = TYPE_MODE (passed_type);
16679
16680           /* This decl represents a formal parameter which was optimized out.
16681              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16682              all cases where (rtl == NULL_RTX) just below.  */
16683           if (dmode == pmode)
16684             rtl = DECL_INCOMING_RTL (decl);
16685           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16686                    && SCALAR_INT_MODE_P (dmode)
16687                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16688                    && DECL_INCOMING_RTL (decl))
16689             {
16690               rtx inc = DECL_INCOMING_RTL (decl);
16691               if (REG_P (inc))
16692                 rtl = inc;
16693               else if (MEM_P (inc))
16694                 {
16695                   if (BYTES_BIG_ENDIAN)
16696                     rtl = adjust_address_nv (inc, dmode,
16697                                              GET_MODE_SIZE (pmode)
16698                                              - GET_MODE_SIZE (dmode));
16699                   else
16700                     rtl = inc;
16701                 }
16702             }
16703         }
16704
16705       /* If the parm was passed in registers, but lives on the stack, then
16706          make a big endian correction if the mode of the type of the
16707          parameter is not the same as the mode of the rtl.  */
16708       /* ??? This is the same series of checks that are made in dbxout.c before
16709          we reach the big endian correction code there.  It isn't clear if all
16710          of these checks are necessary here, but keeping them all is the safe
16711          thing to do.  */
16712       else if (MEM_P (rtl)
16713                && XEXP (rtl, 0) != const0_rtx
16714                && ! CONSTANT_P (XEXP (rtl, 0))
16715                /* Not passed in memory.  */
16716                && !MEM_P (DECL_INCOMING_RTL (decl))
16717                /* Not passed by invisible reference.  */
16718                && (!REG_P (XEXP (rtl, 0))
16719                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16720                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16721 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16722                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16723 #endif
16724                      )
16725                /* Big endian correction check.  */
16726                && BYTES_BIG_ENDIAN
16727                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16728                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16729                    < UNITS_PER_WORD))
16730         {
16731           int offset = (UNITS_PER_WORD
16732                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16733
16734           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16735                              plus_constant (XEXP (rtl, 0), offset));
16736         }
16737     }
16738   else if (TREE_CODE (decl) == VAR_DECL
16739            && rtl
16740            && MEM_P (rtl)
16741            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16742            && BYTES_BIG_ENDIAN)
16743     {
16744       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16745       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16746
16747       /* If a variable is declared "register" yet is smaller than
16748          a register, then if we store the variable to memory, it
16749          looks like we're storing a register-sized value, when in
16750          fact we are not.  We need to adjust the offset of the
16751          storage location to reflect the actual value's bytes,
16752          else gdb will not be able to display it.  */
16753       if (rsize > dsize)
16754         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16755                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16756     }
16757
16758   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16759      and will have been substituted directly into all expressions that use it.
16760      C does not have such a concept, but C++ and other languages do.  */
16761   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16762     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16763
16764   if (rtl)
16765     rtl = targetm.delegitimize_address (rtl);
16766
16767   /* If we don't look past the constant pool, we risk emitting a
16768      reference to a constant pool entry that isn't referenced from
16769      code, and thus is not emitted.  */
16770   if (rtl)
16771     rtl = avoid_constant_pool_reference (rtl);
16772
16773   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16774      in the current CU, resolve_addr will remove the expression referencing
16775      it.  */
16776   if (rtl == NULL_RTX
16777       && TREE_CODE (decl) == VAR_DECL
16778       && !DECL_EXTERNAL (decl)
16779       && TREE_STATIC (decl)
16780       && DECL_NAME (decl)
16781       && !DECL_HARD_REGISTER (decl)
16782       && DECL_MODE (decl) != VOIDmode)
16783     {
16784       rtl = make_decl_rtl_for_debug (decl);
16785       if (!MEM_P (rtl)
16786           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16787           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16788         rtl = NULL_RTX;
16789     }
16790
16791   return rtl;
16792 }
16793
16794 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16795    returned.  If so, the decl for the COMMON block is returned, and the
16796    value is the offset into the common block for the symbol.  */
16797
16798 static tree
16799 fortran_common (tree decl, HOST_WIDE_INT *value)
16800 {
16801   tree val_expr, cvar;
16802   enum machine_mode mode;
16803   HOST_WIDE_INT bitsize, bitpos;
16804   tree offset;
16805   int volatilep = 0, unsignedp = 0;
16806
16807   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16808      it does not have a value (the offset into the common area), or if it
16809      is thread local (as opposed to global) then it isn't common, and shouldn't
16810      be handled as such.  */
16811   if (TREE_CODE (decl) != VAR_DECL
16812       || !TREE_STATIC (decl)
16813       || !DECL_HAS_VALUE_EXPR_P (decl)
16814       || !is_fortran ())
16815     return NULL_TREE;
16816
16817   val_expr = DECL_VALUE_EXPR (decl);
16818   if (TREE_CODE (val_expr) != COMPONENT_REF)
16819     return NULL_TREE;
16820
16821   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16822                               &mode, &unsignedp, &volatilep, true);
16823
16824   if (cvar == NULL_TREE
16825       || TREE_CODE (cvar) != VAR_DECL
16826       || DECL_ARTIFICIAL (cvar)
16827       || !TREE_PUBLIC (cvar))
16828     return NULL_TREE;
16829
16830   *value = 0;
16831   if (offset != NULL)
16832     {
16833       if (!host_integerp (offset, 0))
16834         return NULL_TREE;
16835       *value = tree_low_cst (offset, 0);
16836     }
16837   if (bitpos != 0)
16838     *value += bitpos / BITS_PER_UNIT;
16839
16840   return cvar;
16841 }
16842
16843 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16844    data attribute for a variable or a parameter.  We generate the
16845    DW_AT_const_value attribute only in those cases where the given variable
16846    or parameter does not have a true "location" either in memory or in a
16847    register.  This can happen (for example) when a constant is passed as an
16848    actual argument in a call to an inline function.  (It's possible that
16849    these things can crop up in other ways also.)  Note that one type of
16850    constant value which can be passed into an inlined function is a constant
16851    pointer.  This can happen for example if an actual argument in an inlined
16852    function call evaluates to a compile-time constant address.
16853
16854    CACHE_P is true if it is worth caching the location list for DECL,
16855    so that future calls can reuse it rather than regenerate it from scratch.
16856    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16857    since we will need to refer to them each time the function is inlined.  */
16858
16859 static bool
16860 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16861                                        enum dwarf_attribute attr)
16862 {
16863   rtx rtl;
16864   dw_loc_list_ref list;
16865   var_loc_list *loc_list;
16866   cached_dw_loc_list *cache;
16867   void **slot;
16868
16869   if (TREE_CODE (decl) == ERROR_MARK)
16870     return false;
16871
16872   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16873               || TREE_CODE (decl) == RESULT_DECL);
16874
16875   /* Try to get some constant RTL for this decl, and use that as the value of
16876      the location.  */
16877
16878   rtl = rtl_for_decl_location (decl);
16879   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16880       && add_const_value_attribute (die, rtl))
16881     return true;
16882
16883   /* See if we have single element location list that is equivalent to
16884      a constant value.  That way we are better to use add_const_value_attribute
16885      rather than expanding constant value equivalent.  */
16886   loc_list = lookup_decl_loc (decl);
16887   if (loc_list
16888       && loc_list->first
16889       && loc_list->first->next == NULL
16890       && NOTE_P (loc_list->first->loc)
16891       && NOTE_VAR_LOCATION (loc_list->first->loc)
16892       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16893     {
16894       struct var_loc_node *node;
16895
16896       node = loc_list->first;
16897       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16898       if (GET_CODE (rtl) == EXPR_LIST)
16899         rtl = XEXP (rtl, 0);
16900       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16901           && add_const_value_attribute (die, rtl))
16902          return true;
16903     }
16904   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16905      list several times.  See if we've already cached the contents.  */
16906   list = NULL;
16907   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16908     cache_p = false;
16909   if (cache_p)
16910     {
16911       cache = (cached_dw_loc_list *)
16912         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
16913       if (cache)
16914         list = cache->loc_list;
16915     }
16916   if (list == NULL)
16917     {
16918       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16919       /* It is usually worth caching this result if the decl is from
16920          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16921       if (cache_p && list && list->dw_loc_next)
16922         {
16923           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
16924                                            DECL_UID (decl), INSERT);
16925           cache = ggc_alloc_cleared_cached_dw_loc_list ();
16926           cache->decl_id = DECL_UID (decl);
16927           cache->loc_list = list;
16928           *slot = cache;
16929         }
16930     }
16931   if (list)
16932     {
16933       add_AT_location_description (die, attr, list);
16934       return true;
16935     }
16936   /* None of that worked, so it must not really have a location;
16937      try adding a constant value attribute from the DECL_INITIAL.  */
16938   return tree_add_const_value_attribute_for_decl (die, decl);
16939 }
16940
16941 /* Add VARIABLE and DIE into deferred locations list.  */
16942
16943 static void
16944 defer_location (tree variable, dw_die_ref die)
16945 {
16946   deferred_locations entry;
16947   entry.variable = variable;
16948   entry.die = die;
16949   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16950 }
16951
16952 /* Helper function for tree_add_const_value_attribute.  Natively encode
16953    initializer INIT into an array.  Return true if successful.  */
16954
16955 static bool
16956 native_encode_initializer (tree init, unsigned char *array, int size)
16957 {
16958   tree type;
16959
16960   if (init == NULL_TREE)
16961     return false;
16962
16963   STRIP_NOPS (init);
16964   switch (TREE_CODE (init))
16965     {
16966     case STRING_CST:
16967       type = TREE_TYPE (init);
16968       if (TREE_CODE (type) == ARRAY_TYPE)
16969         {
16970           tree enttype = TREE_TYPE (type);
16971           enum machine_mode mode = TYPE_MODE (enttype);
16972
16973           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16974             return false;
16975           if (int_size_in_bytes (type) != size)
16976             return false;
16977           if (size > TREE_STRING_LENGTH (init))
16978             {
16979               memcpy (array, TREE_STRING_POINTER (init),
16980                       TREE_STRING_LENGTH (init));
16981               memset (array + TREE_STRING_LENGTH (init),
16982                       '\0', size - TREE_STRING_LENGTH (init));
16983             }
16984           else
16985             memcpy (array, TREE_STRING_POINTER (init), size);
16986           return true;
16987         }
16988       return false;
16989     case CONSTRUCTOR:
16990       type = TREE_TYPE (init);
16991       if (int_size_in_bytes (type) != size)
16992         return false;
16993       if (TREE_CODE (type) == ARRAY_TYPE)
16994         {
16995           HOST_WIDE_INT min_index;
16996           unsigned HOST_WIDE_INT cnt;
16997           int curpos = 0, fieldsize;
16998           constructor_elt *ce;
16999
17000           if (TYPE_DOMAIN (type) == NULL_TREE
17001               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17002             return false;
17003
17004           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17005           if (fieldsize <= 0)
17006             return false;
17007
17008           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17009           memset (array, '\0', size);
17010           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17011             {
17012               tree val = ce->value;
17013               tree index = ce->index;
17014               int pos = curpos;
17015               if (index && TREE_CODE (index) == RANGE_EXPR)
17016                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17017                       * fieldsize;
17018               else if (index)
17019                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17020
17021               if (val)
17022                 {
17023                   STRIP_NOPS (val);
17024                   if (!native_encode_initializer (val, array + pos, fieldsize))
17025                     return false;
17026                 }
17027               curpos = pos + fieldsize;
17028               if (index && TREE_CODE (index) == RANGE_EXPR)
17029                 {
17030                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17031                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17032                   while (count > 0)
17033                     {
17034                       if (val)
17035                         memcpy (array + curpos, array + pos, fieldsize);
17036                       curpos += fieldsize;
17037                     }
17038                 }
17039               gcc_assert (curpos <= size);
17040             }
17041           return true;
17042         }
17043       else if (TREE_CODE (type) == RECORD_TYPE
17044                || TREE_CODE (type) == UNION_TYPE)
17045         {
17046           tree field = NULL_TREE;
17047           unsigned HOST_WIDE_INT cnt;
17048           constructor_elt *ce;
17049
17050           if (int_size_in_bytes (type) != size)
17051             return false;
17052
17053           if (TREE_CODE (type) == RECORD_TYPE)
17054             field = TYPE_FIELDS (type);
17055
17056           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17057             {
17058               tree val = ce->value;
17059               int pos, fieldsize;
17060
17061               if (ce->index != 0)
17062                 field = ce->index;
17063
17064               if (val)
17065                 STRIP_NOPS (val);
17066
17067               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17068                 return false;
17069
17070               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17071                   && TYPE_DOMAIN (TREE_TYPE (field))
17072                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17073                 return false;
17074               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17075                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17076                 return false;
17077               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17078               pos = int_byte_position (field);
17079               gcc_assert (pos + fieldsize <= size);
17080               if (val
17081                   && !native_encode_initializer (val, array + pos, fieldsize))
17082                 return false;
17083             }
17084           return true;
17085         }
17086       return false;
17087     case VIEW_CONVERT_EXPR:
17088     case NON_LVALUE_EXPR:
17089       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17090     default:
17091       return native_encode_expr (init, array, size) == size;
17092     }
17093 }
17094
17095 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17096    attribute is the const value T.  */
17097
17098 static bool
17099 tree_add_const_value_attribute (dw_die_ref die, tree t)
17100 {
17101   tree init;
17102   tree type = TREE_TYPE (t);
17103   rtx rtl;
17104
17105   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17106     return false;
17107
17108   init = t;
17109   gcc_assert (!DECL_P (init));
17110
17111   rtl = rtl_for_decl_init (init, type);
17112   if (rtl)
17113     return add_const_value_attribute (die, rtl);
17114   /* If the host and target are sane, try harder.  */
17115   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17116            && initializer_constant_valid_p (init, type))
17117     {
17118       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17119       if (size > 0 && (int) size == size)
17120         {
17121           unsigned char *array = (unsigned char *)
17122             ggc_alloc_cleared_atomic (size);
17123
17124           if (native_encode_initializer (init, array, size))
17125             {
17126               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17127               return true;
17128             }
17129         }
17130     }
17131   return false;
17132 }
17133
17134 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17135    attribute is the const value of T, where T is an integral constant
17136    variable with static storage duration
17137    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17138
17139 static bool
17140 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17141 {
17142
17143   if (!decl
17144       || (TREE_CODE (decl) != VAR_DECL
17145           && TREE_CODE (decl) != CONST_DECL))
17146     return false;
17147
17148     if (TREE_READONLY (decl)
17149         && ! TREE_THIS_VOLATILE (decl)
17150         && DECL_INITIAL (decl))
17151       /* OK */;
17152     else
17153       return false;
17154
17155   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17156   if (get_AT (var_die, DW_AT_const_value))
17157     return false;
17158
17159   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17160 }
17161
17162 /* Convert the CFI instructions for the current function into a
17163    location list.  This is used for DW_AT_frame_base when we targeting
17164    a dwarf2 consumer that does not support the dwarf3
17165    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17166    expressions.  */
17167
17168 static dw_loc_list_ref
17169 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17170 {
17171   dw_fde_ref fde;
17172   dw_loc_list_ref list, *list_tail;
17173   dw_cfi_ref cfi;
17174   dw_cfa_location last_cfa, next_cfa;
17175   const char *start_label, *last_label, *section;
17176   dw_cfa_location remember;
17177
17178   fde = current_fde ();
17179   gcc_assert (fde != NULL);
17180
17181   section = secname_for_decl (current_function_decl);
17182   list_tail = &list;
17183   list = NULL;
17184
17185   memset (&next_cfa, 0, sizeof (next_cfa));
17186   next_cfa.reg = INVALID_REGNUM;
17187   remember = next_cfa;
17188
17189   start_label = fde->dw_fde_begin;
17190
17191   /* ??? Bald assumption that the CIE opcode list does not contain
17192      advance opcodes.  */
17193   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17194     lookup_cfa_1 (cfi, &next_cfa, &remember);
17195
17196   last_cfa = next_cfa;
17197   last_label = start_label;
17198
17199   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi == NULL)
17200     {
17201       /* If the first partition contained no CFI adjustments, the
17202          CIE opcodes apply to the whole first partition.  */
17203       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17204                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17205       list_tail =&(*list_tail)->dw_loc_next;
17206       start_label = last_label = fde->dw_fde_second_begin;
17207     }
17208
17209   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17210     {
17211       switch (cfi->dw_cfi_opc)
17212         {
17213         case DW_CFA_set_loc:
17214         case DW_CFA_advance_loc1:
17215         case DW_CFA_advance_loc2:
17216         case DW_CFA_advance_loc4:
17217           if (!cfa_equal_p (&last_cfa, &next_cfa))
17218             {
17219               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17220                                          start_label, last_label, section);
17221
17222               list_tail = &(*list_tail)->dw_loc_next;
17223               last_cfa = next_cfa;
17224               start_label = last_label;
17225             }
17226           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17227           break;
17228
17229         case DW_CFA_advance_loc:
17230           /* The encoding is complex enough that we should never emit this.  */
17231           gcc_unreachable ();
17232
17233         default:
17234           lookup_cfa_1 (cfi, &next_cfa, &remember);
17235           break;
17236         }
17237       if (cfi == fde->dw_fde_switch_cfi)
17238         {
17239           if (!cfa_equal_p (&last_cfa, &next_cfa))
17240             {
17241               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17242                                          start_label, last_label, section);
17243
17244               list_tail = &(*list_tail)->dw_loc_next;
17245               last_cfa = next_cfa;
17246               start_label = last_label;
17247             }
17248           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17249                                      start_label, fde->dw_fde_end, section);
17250           list_tail = &(*list_tail)->dw_loc_next;
17251           start_label = last_label = fde->dw_fde_second_begin;
17252         }
17253     }
17254
17255   if (!cfa_equal_p (&last_cfa, &next_cfa))
17256     {
17257       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17258                                  start_label, last_label, section);
17259       list_tail = &(*list_tail)->dw_loc_next;
17260       start_label = last_label;
17261     }
17262
17263   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17264                              start_label,
17265                              fde->dw_fde_second_begin
17266                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17267                              section);
17268
17269   if (list && list->dw_loc_next)
17270     gen_llsym (list);
17271
17272   return list;
17273 }
17274
17275 /* Compute a displacement from the "steady-state frame pointer" to the
17276    frame base (often the same as the CFA), and store it in
17277    frame_pointer_fb_offset.  OFFSET is added to the displacement
17278    before the latter is negated.  */
17279
17280 static void
17281 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17282 {
17283   rtx reg, elim;
17284
17285 #ifdef FRAME_POINTER_CFA_OFFSET
17286   reg = frame_pointer_rtx;
17287   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17288 #else
17289   reg = arg_pointer_rtx;
17290   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17291 #endif
17292
17293   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17294   if (GET_CODE (elim) == PLUS)
17295     {
17296       offset += INTVAL (XEXP (elim, 1));
17297       elim = XEXP (elim, 0);
17298     }
17299
17300   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17301                && (elim == hard_frame_pointer_rtx
17302                    || elim == stack_pointer_rtx))
17303               || elim == (frame_pointer_needed
17304                           ? hard_frame_pointer_rtx
17305                           : stack_pointer_rtx));
17306
17307   frame_pointer_fb_offset = -offset;
17308 }
17309
17310 /* Generate a DW_AT_name attribute given some string value to be included as
17311    the value of the attribute.  */
17312
17313 static void
17314 add_name_attribute (dw_die_ref die, const char *name_string)
17315 {
17316   if (name_string != NULL && *name_string != 0)
17317     {
17318       if (demangle_name_func)
17319         name_string = (*demangle_name_func) (name_string);
17320
17321       add_AT_string (die, DW_AT_name, name_string);
17322     }
17323 }
17324
17325 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
17326    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
17327    of TYPE accordingly.
17328
17329    ??? This is a temporary measure until after we're able to generate
17330    regular DWARF for the complex Ada type system.  */
17331
17332 static void 
17333 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
17334                                      dw_die_ref context_die)
17335 {
17336   tree dtype;
17337   dw_die_ref dtype_die;
17338
17339   if (!lang_hooks.types.descriptive_type)
17340     return;
17341
17342   dtype = lang_hooks.types.descriptive_type (type);
17343   if (!dtype)
17344     return;
17345
17346   dtype_die = lookup_type_die (dtype);
17347   if (!dtype_die)
17348     {
17349       gen_type_die (dtype, context_die);
17350       dtype_die = lookup_type_die (dtype);
17351       gcc_assert (dtype_die);
17352     }
17353
17354   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
17355 }
17356
17357 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17358
17359 static void
17360 add_comp_dir_attribute (dw_die_ref die)
17361 {
17362   const char *wd = get_src_pwd ();
17363   char *wd1;
17364
17365   if (wd == NULL)
17366     return;
17367
17368   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17369     {
17370       int wdlen;
17371
17372       wdlen = strlen (wd);
17373       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17374       strcpy (wd1, wd);
17375       wd1 [wdlen] = DIR_SEPARATOR;
17376       wd1 [wdlen + 1] = 0;
17377       wd = wd1;
17378     }
17379
17380     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17381 }
17382
17383 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17384    default.  */
17385
17386 static int
17387 lower_bound_default (void)
17388 {
17389   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17390     {
17391     case DW_LANG_C:
17392     case DW_LANG_C89:
17393     case DW_LANG_C99:
17394     case DW_LANG_C_plus_plus:
17395     case DW_LANG_ObjC:
17396     case DW_LANG_ObjC_plus_plus:
17397     case DW_LANG_Java:
17398       return 0;
17399     case DW_LANG_Fortran77:
17400     case DW_LANG_Fortran90:
17401     case DW_LANG_Fortran95:
17402       return 1;
17403     case DW_LANG_UPC:
17404     case DW_LANG_D:
17405     case DW_LANG_Python:
17406       return dwarf_version >= 4 ? 0 : -1;
17407     case DW_LANG_Ada95:
17408     case DW_LANG_Ada83:
17409     case DW_LANG_Cobol74:
17410     case DW_LANG_Cobol85:
17411     case DW_LANG_Pascal83:
17412     case DW_LANG_Modula2:
17413     case DW_LANG_PLI:
17414       return dwarf_version >= 4 ? 1 : -1;
17415     default:
17416       return -1;
17417     }
17418 }
17419
17420 /* Given a tree node describing an array bound (either lower or upper) output
17421    a representation for that bound.  */
17422
17423 static void
17424 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17425 {
17426   switch (TREE_CODE (bound))
17427     {
17428     case ERROR_MARK:
17429       return;
17430
17431     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17432     case INTEGER_CST:
17433       {
17434         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17435         int dflt;
17436
17437         /* Use the default if possible.  */
17438         if (bound_attr == DW_AT_lower_bound
17439             && host_integerp (bound, 0)
17440             && (dflt = lower_bound_default ()) != -1
17441             && tree_low_cst (bound, 0) == dflt)
17442           ;
17443
17444         /* Otherwise represent the bound as an unsigned value with the
17445            precision of its type.  The precision and signedness of the
17446            type will be necessary to re-interpret it unambiguously.  */
17447         else if (prec < HOST_BITS_PER_WIDE_INT)
17448           {
17449             unsigned HOST_WIDE_INT mask
17450               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17451             add_AT_unsigned (subrange_die, bound_attr,
17452                              TREE_INT_CST_LOW (bound) & mask);
17453           }
17454         else if (prec == HOST_BITS_PER_WIDE_INT
17455                  || TREE_INT_CST_HIGH (bound) == 0)
17456           add_AT_unsigned (subrange_die, bound_attr,
17457                            TREE_INT_CST_LOW (bound));
17458         else
17459           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17460                          TREE_INT_CST_LOW (bound));
17461       }
17462       break;
17463
17464     CASE_CONVERT:
17465     case VIEW_CONVERT_EXPR:
17466       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17467       break;
17468
17469     case SAVE_EXPR:
17470       break;
17471
17472     case VAR_DECL:
17473     case PARM_DECL:
17474     case RESULT_DECL:
17475       {
17476         dw_die_ref decl_die = lookup_decl_die (bound);
17477
17478         /* ??? Can this happen, or should the variable have been bound
17479            first?  Probably it can, since I imagine that we try to create
17480            the types of parameters in the order in which they exist in
17481            the list, and won't have created a forward reference to a
17482            later parameter.  */
17483         if (decl_die != NULL)
17484           {
17485             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17486             break;
17487           }
17488       }
17489       /* FALLTHRU */
17490
17491     default:
17492       {
17493         /* Otherwise try to create a stack operation procedure to
17494            evaluate the value of the array bound.  */
17495
17496         dw_die_ref ctx, decl_die;
17497         dw_loc_list_ref list;
17498
17499         list = loc_list_from_tree (bound, 2);
17500         if (list == NULL || single_element_loc_list_p (list))
17501           {
17502             /* If DW_AT_*bound is not a reference nor constant, it is
17503                a DWARF expression rather than location description.
17504                For that loc_list_from_tree (bound, 0) is needed.
17505                If that fails to give a single element list,
17506                fall back to outputting this as a reference anyway.  */
17507             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17508             if (list2 && single_element_loc_list_p (list2))
17509               {
17510                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17511                 break;
17512               }
17513           }
17514         if (list == NULL)
17515           break;
17516
17517         if (current_function_decl == 0)
17518           ctx = comp_unit_die ();
17519         else
17520           ctx = lookup_decl_die (current_function_decl);
17521
17522         decl_die = new_die (DW_TAG_variable, ctx, bound);
17523         add_AT_flag (decl_die, DW_AT_artificial, 1);
17524         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17525         add_AT_location_description (decl_die, DW_AT_location, list);
17526         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17527         break;
17528       }
17529     }
17530 }
17531
17532 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17533    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17534    Note that the block of subscript information for an array type also
17535    includes information about the element type of the given array type.  */
17536
17537 static void
17538 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17539 {
17540   unsigned dimension_number;
17541   tree lower, upper;
17542   dw_die_ref subrange_die;
17543
17544   for (dimension_number = 0;
17545        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17546        type = TREE_TYPE (type), dimension_number++)
17547     {
17548       tree domain = TYPE_DOMAIN (type);
17549
17550       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17551         break;
17552
17553       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17554          and (in GNU C only) variable bounds.  Handle all three forms
17555          here.  */
17556       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17557       if (domain)
17558         {
17559           /* We have an array type with specified bounds.  */
17560           lower = TYPE_MIN_VALUE (domain);
17561           upper = TYPE_MAX_VALUE (domain);
17562
17563           /* Define the index type.  */
17564           if (TREE_TYPE (domain))
17565             {
17566               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17567                  TREE_TYPE field.  We can't emit debug info for this
17568                  because it is an unnamed integral type.  */
17569               if (TREE_CODE (domain) == INTEGER_TYPE
17570                   && TYPE_NAME (domain) == NULL_TREE
17571                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17572                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17573                 ;
17574               else
17575                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17576                                     type_die);
17577             }
17578
17579           /* ??? If upper is NULL, the array has unspecified length,
17580              but it does have a lower bound.  This happens with Fortran
17581                dimension arr(N:*)
17582              Since the debugger is definitely going to need to know N
17583              to produce useful results, go ahead and output the lower
17584              bound solo, and hope the debugger can cope.  */
17585
17586           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17587           if (upper)
17588             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17589         }
17590
17591       /* Otherwise we have an array type with an unspecified length.  The
17592          DWARF-2 spec does not say how to handle this; let's just leave out the
17593          bounds.  */
17594     }
17595 }
17596
17597 static void
17598 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17599 {
17600   unsigned size;
17601
17602   switch (TREE_CODE (tree_node))
17603     {
17604     case ERROR_MARK:
17605       size = 0;
17606       break;
17607     case ENUMERAL_TYPE:
17608     case RECORD_TYPE:
17609     case UNION_TYPE:
17610     case QUAL_UNION_TYPE:
17611       size = int_size_in_bytes (tree_node);
17612       break;
17613     case FIELD_DECL:
17614       /* For a data member of a struct or union, the DW_AT_byte_size is
17615          generally given as the number of bytes normally allocated for an
17616          object of the *declared* type of the member itself.  This is true
17617          even for bit-fields.  */
17618       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17619       break;
17620     default:
17621       gcc_unreachable ();
17622     }
17623
17624   /* Note that `size' might be -1 when we get to this point.  If it is, that
17625      indicates that the byte size of the entity in question is variable.  We
17626      have no good way of expressing this fact in Dwarf at the present time,
17627      so just let the -1 pass on through.  */
17628   add_AT_unsigned (die, DW_AT_byte_size, size);
17629 }
17630
17631 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17632    which specifies the distance in bits from the highest order bit of the
17633    "containing object" for the bit-field to the highest order bit of the
17634    bit-field itself.
17635
17636    For any given bit-field, the "containing object" is a hypothetical object
17637    (of some integral or enum type) within which the given bit-field lives.  The
17638    type of this hypothetical "containing object" is always the same as the
17639    declared type of the individual bit-field itself.  The determination of the
17640    exact location of the "containing object" for a bit-field is rather
17641    complicated.  It's handled by the `field_byte_offset' function (above).
17642
17643    Note that it is the size (in bytes) of the hypothetical "containing object"
17644    which will be given in the DW_AT_byte_size attribute for this bit-field.
17645    (See `byte_size_attribute' above).  */
17646
17647 static inline void
17648 add_bit_offset_attribute (dw_die_ref die, tree decl)
17649 {
17650   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17651   tree type = DECL_BIT_FIELD_TYPE (decl);
17652   HOST_WIDE_INT bitpos_int;
17653   HOST_WIDE_INT highest_order_object_bit_offset;
17654   HOST_WIDE_INT highest_order_field_bit_offset;
17655   HOST_WIDE_INT bit_offset;
17656
17657   /* Must be a field and a bit field.  */
17658   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17659
17660   /* We can't yet handle bit-fields whose offsets are variable, so if we
17661      encounter such things, just return without generating any attribute
17662      whatsoever.  Likewise for variable or too large size.  */
17663   if (! host_integerp (bit_position (decl), 0)
17664       || ! host_integerp (DECL_SIZE (decl), 1))
17665     return;
17666
17667   bitpos_int = int_bit_position (decl);
17668
17669   /* Note that the bit offset is always the distance (in bits) from the
17670      highest-order bit of the "containing object" to the highest-order bit of
17671      the bit-field itself.  Since the "high-order end" of any object or field
17672      is different on big-endian and little-endian machines, the computation
17673      below must take account of these differences.  */
17674   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17675   highest_order_field_bit_offset = bitpos_int;
17676
17677   if (! BYTES_BIG_ENDIAN)
17678     {
17679       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17680       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17681     }
17682
17683   bit_offset
17684     = (! BYTES_BIG_ENDIAN
17685        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17686        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17687
17688   if (bit_offset < 0)
17689     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17690   else
17691     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17692 }
17693
17694 /* For a FIELD_DECL node which represents a bit field, output an attribute
17695    which specifies the length in bits of the given field.  */
17696
17697 static inline void
17698 add_bit_size_attribute (dw_die_ref die, tree decl)
17699 {
17700   /* Must be a field and a bit field.  */
17701   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17702               && DECL_BIT_FIELD_TYPE (decl));
17703
17704   if (host_integerp (DECL_SIZE (decl), 1))
17705     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17706 }
17707
17708 /* If the compiled language is ANSI C, then add a 'prototyped'
17709    attribute, if arg types are given for the parameters of a function.  */
17710
17711 static inline void
17712 add_prototyped_attribute (dw_die_ref die, tree func_type)
17713 {
17714   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17715       && prototype_p (func_type))
17716     add_AT_flag (die, DW_AT_prototyped, 1);
17717 }
17718
17719 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17720    by looking in either the type declaration or object declaration
17721    equate table.  */
17722
17723 static inline dw_die_ref
17724 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17725 {
17726   dw_die_ref origin_die = NULL;
17727
17728   if (TREE_CODE (origin) != FUNCTION_DECL)
17729     {
17730       /* We may have gotten separated from the block for the inlined
17731          function, if we're in an exception handler or some such; make
17732          sure that the abstract function has been written out.
17733
17734          Doing this for nested functions is wrong, however; functions are
17735          distinct units, and our context might not even be inline.  */
17736       tree fn = origin;
17737
17738       if (TYPE_P (fn))
17739         fn = TYPE_STUB_DECL (fn);
17740
17741       fn = decl_function_context (fn);
17742       if (fn)
17743         dwarf2out_abstract_function (fn);
17744     }
17745
17746   if (DECL_P (origin))
17747     origin_die = lookup_decl_die (origin);
17748   else if (TYPE_P (origin))
17749     origin_die = lookup_type_die (origin);
17750
17751   /* XXX: Functions that are never lowered don't always have correct block
17752      trees (in the case of java, they simply have no block tree, in some other
17753      languages).  For these functions, there is nothing we can really do to
17754      output correct debug info for inlined functions in all cases.  Rather
17755      than die, we'll just produce deficient debug info now, in that we will
17756      have variables without a proper abstract origin.  In the future, when all
17757      functions are lowered, we should re-add a gcc_assert (origin_die)
17758      here.  */
17759
17760   if (origin_die)
17761     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17762   return origin_die;
17763 }
17764
17765 /* We do not currently support the pure_virtual attribute.  */
17766
17767 static inline void
17768 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17769 {
17770   if (DECL_VINDEX (func_decl))
17771     {
17772       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17773
17774       if (host_integerp (DECL_VINDEX (func_decl), 0))
17775         add_AT_loc (die, DW_AT_vtable_elem_location,
17776                     new_loc_descr (DW_OP_constu,
17777                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17778                                    0));
17779
17780       /* GNU extension: Record what type this method came from originally.  */
17781       if (debug_info_level > DINFO_LEVEL_TERSE
17782           && DECL_CONTEXT (func_decl))
17783         add_AT_die_ref (die, DW_AT_containing_type,
17784                         lookup_type_die (DECL_CONTEXT (func_decl)));
17785     }
17786 }
17787 \f
17788 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17789    given decl.  This used to be a vendor extension until after DWARF 4
17790    standardized it.  */
17791
17792 static void
17793 add_linkage_attr (dw_die_ref die, tree decl)
17794 {
17795   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17796
17797   /* Mimic what assemble_name_raw does with a leading '*'.  */
17798   if (name[0] == '*')
17799     name = &name[1];
17800
17801   if (dwarf_version >= 4)
17802     add_AT_string (die, DW_AT_linkage_name, name);
17803   else
17804     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17805 }
17806
17807 /* Add source coordinate attributes for the given decl.  */
17808
17809 static void
17810 add_src_coords_attributes (dw_die_ref die, tree decl)
17811 {
17812   expanded_location s;
17813
17814   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17815     return;
17816   s = expand_location (DECL_SOURCE_LOCATION (decl));
17817   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17818   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17819 }
17820
17821 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17822
17823 static void
17824 add_linkage_name (dw_die_ref die, tree decl)
17825 {
17826   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17827        && TREE_PUBLIC (decl)
17828        && !DECL_ABSTRACT (decl)
17829        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17830        && die->die_tag != DW_TAG_member)
17831     {
17832       /* Defer until we have an assembler name set.  */
17833       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17834         {
17835           limbo_die_node *asm_name;
17836
17837           asm_name = ggc_alloc_cleared_limbo_die_node ();
17838           asm_name->die = die;
17839           asm_name->created_for = decl;
17840           asm_name->next = deferred_asm_name;
17841           deferred_asm_name = asm_name;
17842         }
17843       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17844         add_linkage_attr (die, decl);
17845     }
17846 }
17847
17848 /* Add a DW_AT_name attribute and source coordinate attribute for the
17849    given decl, but only if it actually has a name.  */
17850
17851 static void
17852 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17853 {
17854   tree decl_name;
17855
17856   decl_name = DECL_NAME (decl);
17857   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17858     {
17859       const char *name = dwarf2_name (decl, 0);
17860       if (name)
17861         add_name_attribute (die, name);
17862       if (! DECL_ARTIFICIAL (decl))
17863         add_src_coords_attributes (die, decl);
17864
17865       add_linkage_name (die, decl);
17866     }
17867
17868 #ifdef VMS_DEBUGGING_INFO
17869   /* Get the function's name, as described by its RTL.  This may be different
17870      from the DECL_NAME name used in the source file.  */
17871   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17872     {
17873       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17874                    XEXP (DECL_RTL (decl), 0));
17875       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17876     }
17877 #endif /* VMS_DEBUGGING_INFO */
17878 }
17879
17880 #ifdef VMS_DEBUGGING_INFO
17881 /* Output the debug main pointer die for VMS */
17882
17883 void
17884 dwarf2out_vms_debug_main_pointer (void)
17885 {
17886   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17887   dw_die_ref die;
17888
17889   /* Allocate the VMS debug main subprogram die.  */
17890   die = ggc_alloc_cleared_die_node ();
17891   die->die_tag = DW_TAG_subprogram;
17892   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17893   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17894                                current_function_funcdef_no);
17895   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17896
17897   /* Make it the first child of comp_unit_die ().  */
17898   die->die_parent = comp_unit_die ();
17899   if (comp_unit_die ()->die_child)
17900     {
17901       die->die_sib = comp_unit_die ()->die_child->die_sib;
17902       comp_unit_die ()->die_child->die_sib = die;
17903     }
17904   else
17905     {
17906       die->die_sib = die;
17907       comp_unit_die ()->die_child = die;
17908     }
17909 }
17910 #endif /* VMS_DEBUGGING_INFO */
17911
17912 /* Push a new declaration scope.  */
17913
17914 static void
17915 push_decl_scope (tree scope)
17916 {
17917   VEC_safe_push (tree, gc, decl_scope_table, scope);
17918 }
17919
17920 /* Pop a declaration scope.  */
17921
17922 static inline void
17923 pop_decl_scope (void)
17924 {
17925   VEC_pop (tree, decl_scope_table);
17926 }
17927
17928 /* Return the DIE for the scope that immediately contains this type.
17929    Non-named types get global scope.  Named types nested in other
17930    types get their containing scope if it's open, or global scope
17931    otherwise.  All other types (i.e. function-local named types) get
17932    the current active scope.  */
17933
17934 static dw_die_ref
17935 scope_die_for (tree t, dw_die_ref context_die)
17936 {
17937   dw_die_ref scope_die = NULL;
17938   tree containing_scope;
17939   int i;
17940
17941   /* Non-types always go in the current scope.  */
17942   gcc_assert (TYPE_P (t));
17943
17944   containing_scope = TYPE_CONTEXT (t);
17945
17946   /* Use the containing namespace if it was passed in (for a declaration).  */
17947   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17948     {
17949       if (context_die == lookup_decl_die (containing_scope))
17950         /* OK */;
17951       else
17952         containing_scope = NULL_TREE;
17953     }
17954
17955   /* Ignore function type "scopes" from the C frontend.  They mean that
17956      a tagged type is local to a parmlist of a function declarator, but
17957      that isn't useful to DWARF.  */
17958   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17959     containing_scope = NULL_TREE;
17960
17961   if (SCOPE_FILE_SCOPE_P (containing_scope))
17962     scope_die = comp_unit_die ();
17963   else if (TYPE_P (containing_scope))
17964     {
17965       /* For types, we can just look up the appropriate DIE.  But
17966          first we check to see if we're in the middle of emitting it
17967          so we know where the new DIE should go.  */
17968       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17969         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17970           break;
17971
17972       if (i < 0)
17973         {
17974           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17975                       || TREE_ASM_WRITTEN (containing_scope));
17976           /*We are not in the middle of emitting the type
17977             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17978           scope_die = lookup_type_die (containing_scope);
17979
17980           /* If none of the current dies are suitable, we get file scope.  */
17981           if (scope_die == NULL)
17982             scope_die = comp_unit_die ();
17983         }
17984       else
17985         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17986     }
17987   else
17988     scope_die = context_die;
17989
17990   return scope_die;
17991 }
17992
17993 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17994
17995 static inline int
17996 local_scope_p (dw_die_ref context_die)
17997 {
17998   for (; context_die; context_die = context_die->die_parent)
17999     if (context_die->die_tag == DW_TAG_inlined_subroutine
18000         || context_die->die_tag == DW_TAG_subprogram)
18001       return 1;
18002
18003   return 0;
18004 }
18005
18006 /* Returns nonzero if CONTEXT_DIE is a class.  */
18007
18008 static inline int
18009 class_scope_p (dw_die_ref context_die)
18010 {
18011   return (context_die
18012           && (context_die->die_tag == DW_TAG_structure_type
18013               || context_die->die_tag == DW_TAG_class_type
18014               || context_die->die_tag == DW_TAG_interface_type
18015               || context_die->die_tag == DW_TAG_union_type));
18016 }
18017
18018 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18019    whether or not to treat a DIE in this context as a declaration.  */
18020
18021 static inline int
18022 class_or_namespace_scope_p (dw_die_ref context_die)
18023 {
18024   return (class_scope_p (context_die)
18025           || (context_die && context_die->die_tag == DW_TAG_namespace));
18026 }
18027
18028 /* Many forms of DIEs require a "type description" attribute.  This
18029    routine locates the proper "type descriptor" die for the type given
18030    by 'type', and adds a DW_AT_type attribute below the given die.  */
18031
18032 static void
18033 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18034                     int decl_volatile, dw_die_ref context_die)
18035 {
18036   enum tree_code code  = TREE_CODE (type);
18037   dw_die_ref type_die  = NULL;
18038
18039   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18040      or fixed-point type, use the inner type.  This is because we have no
18041      support for unnamed types in base_type_die.  This can happen if this is
18042      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18043   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18044       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18045     type = TREE_TYPE (type), code = TREE_CODE (type);
18046
18047   if (code == ERROR_MARK
18048       /* Handle a special case.  For functions whose return type is void, we
18049          generate *no* type attribute.  (Note that no object may have type
18050          `void', so this only applies to function return types).  */
18051       || code == VOID_TYPE)
18052     return;
18053
18054   type_die = modified_type_die (type,
18055                                 decl_const || TYPE_READONLY (type),
18056                                 decl_volatile || TYPE_VOLATILE (type),
18057                                 context_die);
18058
18059   if (type_die != NULL)
18060     add_AT_die_ref (object_die, DW_AT_type, type_die);
18061 }
18062
18063 /* Given an object die, add the calling convention attribute for the
18064    function call type.  */
18065 static void
18066 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18067 {
18068   enum dwarf_calling_convention value = DW_CC_normal;
18069
18070   value = ((enum dwarf_calling_convention)
18071            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18072
18073   if (is_fortran ()
18074       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18075     {
18076       /* DWARF 2 doesn't provide a way to identify a program's source-level
18077         entry point.  DW_AT_calling_convention attributes are only meant
18078         to describe functions' calling conventions.  However, lacking a
18079         better way to signal the Fortran main program, we used this for 
18080         a long time, following existing custom.  Now, DWARF 4 has 
18081         DW_AT_main_subprogram, which we add below, but some tools still
18082         rely on the old way, which we thus keep.  */
18083       value = DW_CC_program;
18084
18085       if (dwarf_version >= 4 || !dwarf_strict)
18086         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18087     }
18088
18089   /* Only add the attribute if the backend requests it, and
18090      is not DW_CC_normal.  */
18091   if (value && (value != DW_CC_normal))
18092     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18093 }
18094
18095 /* Given a tree pointer to a struct, class, union, or enum type node, return
18096    a pointer to the (string) tag name for the given type, or zero if the type
18097    was declared without a tag.  */
18098
18099 static const char *
18100 type_tag (const_tree type)
18101 {
18102   const char *name = 0;
18103
18104   if (TYPE_NAME (type) != 0)
18105     {
18106       tree t = 0;
18107
18108       /* Find the IDENTIFIER_NODE for the type name.  */
18109       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18110           && !TYPE_NAMELESS (type))
18111         t = TYPE_NAME (type);
18112
18113       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18114          a TYPE_DECL node, regardless of whether or not a `typedef' was
18115          involved.  */
18116       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18117                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18118         {
18119           /* We want to be extra verbose.  Don't call dwarf_name if
18120              DECL_NAME isn't set.  The default hook for decl_printable_name
18121              doesn't like that, and in this context it's correct to return
18122              0, instead of "<anonymous>" or the like.  */
18123           if (DECL_NAME (TYPE_NAME (type))
18124               && !DECL_NAMELESS (TYPE_NAME (type)))
18125             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18126         }
18127
18128       /* Now get the name as a string, or invent one.  */
18129       if (!name && t != 0)
18130         name = IDENTIFIER_POINTER (t);
18131     }
18132
18133   return (name == 0 || *name == '\0') ? 0 : name;
18134 }
18135
18136 /* Return the type associated with a data member, make a special check
18137    for bit field types.  */
18138
18139 static inline tree
18140 member_declared_type (const_tree member)
18141 {
18142   return (DECL_BIT_FIELD_TYPE (member)
18143           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18144 }
18145
18146 /* Get the decl's label, as described by its RTL. This may be different
18147    from the DECL_NAME name used in the source file.  */
18148
18149 #if 0
18150 static const char *
18151 decl_start_label (tree decl)
18152 {
18153   rtx x;
18154   const char *fnname;
18155
18156   x = DECL_RTL (decl);
18157   gcc_assert (MEM_P (x));
18158
18159   x = XEXP (x, 0);
18160   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18161
18162   fnname = XSTR (x, 0);
18163   return fnname;
18164 }
18165 #endif
18166 \f
18167 /* These routines generate the internal representation of the DIE's for
18168    the compilation unit.  Debugging information is collected by walking
18169    the declaration trees passed in from dwarf2out_decl().  */
18170
18171 static void
18172 gen_array_type_die (tree type, dw_die_ref context_die)
18173 {
18174   dw_die_ref scope_die = scope_die_for (type, context_die);
18175   dw_die_ref array_die;
18176
18177   /* GNU compilers represent multidimensional array types as sequences of one
18178      dimensional array types whose element types are themselves array types.
18179      We sometimes squish that down to a single array_type DIE with multiple
18180      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18181      say that we are allowed to do this kind of compression in C, because
18182      there is no difference between an array of arrays and a multidimensional
18183      array.  We don't do this for Ada to remain as close as possible to the
18184      actual representation, which is especially important against the language
18185      flexibilty wrt arrays of variable size.  */
18186
18187   bool collapse_nested_arrays = !is_ada ();
18188   tree element_type;
18189
18190   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18191      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18192   if (TYPE_STRING_FLAG (type)
18193       && TREE_CODE (type) == ARRAY_TYPE
18194       && is_fortran ()
18195       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18196     {
18197       HOST_WIDE_INT size;
18198
18199       array_die = new_die (DW_TAG_string_type, scope_die, type);
18200       add_name_attribute (array_die, type_tag (type));
18201       equate_type_number_to_die (type, array_die);
18202       size = int_size_in_bytes (type);
18203       if (size >= 0)
18204         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18205       else if (TYPE_DOMAIN (type) != NULL_TREE
18206                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18207                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18208         {
18209           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18210           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18211
18212           size = int_size_in_bytes (TREE_TYPE (szdecl));
18213           if (loc && size > 0)
18214             {
18215               add_AT_location_description (array_die, DW_AT_string_length, loc);
18216               if (size != DWARF2_ADDR_SIZE)
18217                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18218             }
18219         }
18220       return;
18221     }
18222
18223   /* ??? The SGI dwarf reader fails for array of array of enum types
18224      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18225      array type comes before the outer array type.  We thus call gen_type_die
18226      before we new_die and must prevent nested array types collapsing for this
18227      target.  */
18228
18229 #ifdef MIPS_DEBUGGING_INFO
18230   gen_type_die (TREE_TYPE (type), context_die);
18231   collapse_nested_arrays = false;
18232 #endif
18233
18234   array_die = new_die (DW_TAG_array_type, scope_die, type);
18235   add_name_attribute (array_die, type_tag (type));
18236   add_gnat_descriptive_type_attribute (array_die, type, context_die);
18237   equate_type_number_to_die (type, array_die);
18238
18239   if (TREE_CODE (type) == VECTOR_TYPE)
18240     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18241
18242   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18243   if (is_fortran ()
18244       && TREE_CODE (type) == ARRAY_TYPE
18245       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18246       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18247     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18248
18249 #if 0
18250   /* We default the array ordering.  SDB will probably do
18251      the right things even if DW_AT_ordering is not present.  It's not even
18252      an issue until we start to get into multidimensional arrays anyway.  If
18253      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18254      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18255      and when we find out that we need to put these in, we will only do so
18256      for multidimensional arrays.  */
18257   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18258 #endif
18259
18260 #ifdef MIPS_DEBUGGING_INFO
18261   /* The SGI compilers handle arrays of unknown bound by setting
18262      AT_declaration and not emitting any subrange DIEs.  */
18263   if (TREE_CODE (type) == ARRAY_TYPE
18264       && ! TYPE_DOMAIN (type))
18265     add_AT_flag (array_die, DW_AT_declaration, 1);
18266   else
18267 #endif
18268   if (TREE_CODE (type) == VECTOR_TYPE)
18269     {
18270       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18271       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18272       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18273       add_bound_info (subrange_die, DW_AT_upper_bound,
18274                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18275     }
18276   else
18277     add_subscript_info (array_die, type, collapse_nested_arrays);
18278
18279   /* Add representation of the type of the elements of this array type and
18280      emit the corresponding DIE if we haven't done it already.  */
18281   element_type = TREE_TYPE (type);
18282   if (collapse_nested_arrays)
18283     while (TREE_CODE (element_type) == ARRAY_TYPE)
18284       {
18285         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18286           break;
18287         element_type = TREE_TYPE (element_type);
18288       }
18289
18290 #ifndef MIPS_DEBUGGING_INFO
18291   gen_type_die (element_type, context_die);
18292 #endif
18293
18294   add_type_attribute (array_die, element_type, 0, 0, context_die);
18295
18296   if (get_AT (array_die, DW_AT_name))
18297     add_pubtype (type, array_die);
18298 }
18299
18300 static dw_loc_descr_ref
18301 descr_info_loc (tree val, tree base_decl)
18302 {
18303   HOST_WIDE_INT size;
18304   dw_loc_descr_ref loc, loc2;
18305   enum dwarf_location_atom op;
18306
18307   if (val == base_decl)
18308     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18309
18310   switch (TREE_CODE (val))
18311     {
18312     CASE_CONVERT:
18313       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18314     case VAR_DECL:
18315       return loc_descriptor_from_tree (val, 0);
18316     case INTEGER_CST:
18317       if (host_integerp (val, 0))
18318         return int_loc_descriptor (tree_low_cst (val, 0));
18319       break;
18320     case INDIRECT_REF:
18321       size = int_size_in_bytes (TREE_TYPE (val));
18322       if (size < 0)
18323         break;
18324       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18325       if (!loc)
18326         break;
18327       if (size == DWARF2_ADDR_SIZE)
18328         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18329       else
18330         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18331       return loc;
18332     case POINTER_PLUS_EXPR:
18333     case PLUS_EXPR:
18334       if (host_integerp (TREE_OPERAND (val, 1), 1)
18335           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18336              < 16384)
18337         {
18338           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18339           if (!loc)
18340             break;
18341           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18342         }
18343       else
18344         {
18345           op = DW_OP_plus;
18346         do_binop:
18347           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18348           if (!loc)
18349             break;
18350           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18351           if (!loc2)
18352             break;
18353           add_loc_descr (&loc, loc2);
18354           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18355         }
18356       return loc;
18357     case MINUS_EXPR:
18358       op = DW_OP_minus;
18359       goto do_binop;
18360     case MULT_EXPR:
18361       op = DW_OP_mul;
18362       goto do_binop;
18363     case EQ_EXPR:
18364       op = DW_OP_eq;
18365       goto do_binop;
18366     case NE_EXPR:
18367       op = DW_OP_ne;
18368       goto do_binop;
18369     default:
18370       break;
18371     }
18372   return NULL;
18373 }
18374
18375 static void
18376 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18377                       tree val, tree base_decl)
18378 {
18379   dw_loc_descr_ref loc;
18380
18381   if (host_integerp (val, 0))
18382     {
18383       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18384       return;
18385     }
18386
18387   loc = descr_info_loc (val, base_decl);
18388   if (!loc)
18389     return;
18390
18391   add_AT_loc (die, attr, loc);
18392 }
18393
18394 /* This routine generates DIE for array with hidden descriptor, details
18395    are filled into *info by a langhook.  */
18396
18397 static void
18398 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18399                           dw_die_ref context_die)
18400 {
18401   dw_die_ref scope_die = scope_die_for (type, context_die);
18402   dw_die_ref array_die;
18403   int dim;
18404
18405   array_die = new_die (DW_TAG_array_type, scope_die, type);
18406   add_name_attribute (array_die, type_tag (type));
18407   equate_type_number_to_die (type, array_die);
18408
18409   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18410   if (is_fortran ()
18411       && info->ndimensions >= 2)
18412     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18413
18414   if (info->data_location)
18415     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18416                           info->base_decl);
18417   if (info->associated)
18418     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18419                           info->base_decl);
18420   if (info->allocated)
18421     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18422                           info->base_decl);
18423
18424   for (dim = 0; dim < info->ndimensions; dim++)
18425     {
18426       dw_die_ref subrange_die
18427         = new_die (DW_TAG_subrange_type, array_die, NULL);
18428
18429       if (info->dimen[dim].lower_bound)
18430         {
18431           /* If it is the default value, omit it.  */
18432           int dflt;
18433
18434           if (host_integerp (info->dimen[dim].lower_bound, 0)
18435               && (dflt = lower_bound_default ()) != -1
18436               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18437             ;
18438           else
18439             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18440                                   info->dimen[dim].lower_bound,
18441                                   info->base_decl);
18442         }
18443       if (info->dimen[dim].upper_bound)
18444         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18445                               info->dimen[dim].upper_bound,
18446                               info->base_decl);
18447       if (info->dimen[dim].stride)
18448         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18449                               info->dimen[dim].stride,
18450                               info->base_decl);
18451     }
18452
18453   gen_type_die (info->element_type, context_die);
18454   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18455
18456   if (get_AT (array_die, DW_AT_name))
18457     add_pubtype (type, array_die);
18458 }
18459
18460 #if 0
18461 static void
18462 gen_entry_point_die (tree decl, dw_die_ref context_die)
18463 {
18464   tree origin = decl_ultimate_origin (decl);
18465   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18466
18467   if (origin != NULL)
18468     add_abstract_origin_attribute (decl_die, origin);
18469   else
18470     {
18471       add_name_and_src_coords_attributes (decl_die, decl);
18472       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18473                           0, 0, context_die);
18474     }
18475
18476   if (DECL_ABSTRACT (decl))
18477     equate_decl_number_to_die (decl, decl_die);
18478   else
18479     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18480 }
18481 #endif
18482
18483 /* Walk through the list of incomplete types again, trying once more to
18484    emit full debugging info for them.  */
18485
18486 static void
18487 retry_incomplete_types (void)
18488 {
18489   int i;
18490
18491   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18492     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18493                                   DINFO_USAGE_DIR_USE))
18494       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18495 }
18496
18497 /* Determine what tag to use for a record type.  */
18498
18499 static enum dwarf_tag
18500 record_type_tag (tree type)
18501 {
18502   if (! lang_hooks.types.classify_record)
18503     return DW_TAG_structure_type;
18504
18505   switch (lang_hooks.types.classify_record (type))
18506     {
18507     case RECORD_IS_STRUCT:
18508       return DW_TAG_structure_type;
18509
18510     case RECORD_IS_CLASS:
18511       return DW_TAG_class_type;
18512
18513     case RECORD_IS_INTERFACE:
18514       if (dwarf_version >= 3 || !dwarf_strict)
18515         return DW_TAG_interface_type;
18516       return DW_TAG_structure_type;
18517
18518     default:
18519       gcc_unreachable ();
18520     }
18521 }
18522
18523 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18524    include all of the information about the enumeration values also. Each
18525    enumerated type name/value is listed as a child of the enumerated type
18526    DIE.  */
18527
18528 static dw_die_ref
18529 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18530 {
18531   dw_die_ref type_die = lookup_type_die (type);
18532
18533   if (type_die == NULL)
18534     {
18535       type_die = new_die (DW_TAG_enumeration_type,
18536                           scope_die_for (type, context_die), type);
18537       equate_type_number_to_die (type, type_die);
18538       add_name_attribute (type_die, type_tag (type));
18539       add_gnat_descriptive_type_attribute (type_die, type, context_die);
18540       if (dwarf_version >= 4 || !dwarf_strict)
18541         {
18542           if (ENUM_IS_SCOPED (type))
18543             add_AT_flag (type_die, DW_AT_enum_class, 1);
18544           if (ENUM_IS_OPAQUE (type))
18545             add_AT_flag (type_die, DW_AT_declaration, 1);
18546         }
18547     }
18548   else if (! TYPE_SIZE (type))
18549     return type_die;
18550   else
18551     remove_AT (type_die, DW_AT_declaration);
18552
18553   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18554      given enum type is incomplete, do not generate the DW_AT_byte_size
18555      attribute or the DW_AT_element_list attribute.  */
18556   if (TYPE_SIZE (type))
18557     {
18558       tree link;
18559
18560       TREE_ASM_WRITTEN (type) = 1;
18561       add_byte_size_attribute (type_die, type);
18562       if (TYPE_STUB_DECL (type) != NULL_TREE)
18563         {
18564           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18565           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18566         }
18567
18568       /* If the first reference to this type was as the return type of an
18569          inline function, then it may not have a parent.  Fix this now.  */
18570       if (type_die->die_parent == NULL)
18571         add_child_die (scope_die_for (type, context_die), type_die);
18572
18573       for (link = TYPE_VALUES (type);
18574            link != NULL; link = TREE_CHAIN (link))
18575         {
18576           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18577           tree value = TREE_VALUE (link);
18578
18579           add_name_attribute (enum_die,
18580                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18581
18582           if (TREE_CODE (value) == CONST_DECL)
18583             value = DECL_INITIAL (value);
18584
18585           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18586             /* DWARF2 does not provide a way of indicating whether or
18587                not enumeration constants are signed or unsigned.  GDB
18588                always assumes the values are signed, so we output all
18589                values as if they were signed.  That means that
18590                enumeration constants with very large unsigned values
18591                will appear to have negative values in the debugger.  */
18592             add_AT_int (enum_die, DW_AT_const_value,
18593                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18594         }
18595     }
18596   else
18597     add_AT_flag (type_die, DW_AT_declaration, 1);
18598
18599   if (get_AT (type_die, DW_AT_name))
18600     add_pubtype (type, type_die);
18601
18602   return type_die;
18603 }
18604
18605 /* Generate a DIE to represent either a real live formal parameter decl or to
18606    represent just the type of some formal parameter position in some function
18607    type.
18608
18609    Note that this routine is a bit unusual because its argument may be a
18610    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18611    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18612    node.  If it's the former then this function is being called to output a
18613    DIE to represent a formal parameter object (or some inlining thereof).  If
18614    it's the latter, then this function is only being called to output a
18615    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18616    argument type of some subprogram type.
18617    If EMIT_NAME_P is true, name and source coordinate attributes
18618    are emitted.  */
18619
18620 static dw_die_ref
18621 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18622                           dw_die_ref context_die)
18623 {
18624   tree node_or_origin = node ? node : origin;
18625   tree ultimate_origin;
18626   dw_die_ref parm_die
18627     = new_die (DW_TAG_formal_parameter, context_die, node);
18628
18629   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18630     {
18631     case tcc_declaration:
18632       ultimate_origin = decl_ultimate_origin (node_or_origin);
18633       if (node || ultimate_origin)
18634         origin = ultimate_origin;
18635       if (origin != NULL)
18636         add_abstract_origin_attribute (parm_die, origin);
18637       else if (emit_name_p)
18638         add_name_and_src_coords_attributes (parm_die, node);
18639       if (origin == NULL
18640           || (! DECL_ABSTRACT (node_or_origin)
18641               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18642                                            decl_function_context
18643                                                             (node_or_origin))))
18644         {
18645           tree type = TREE_TYPE (node_or_origin);
18646           if (decl_by_reference_p (node_or_origin))
18647             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18648                                 context_die);
18649           else
18650             add_type_attribute (parm_die, type,
18651                                 TREE_READONLY (node_or_origin),
18652                                 TREE_THIS_VOLATILE (node_or_origin),
18653                                 context_die);
18654         }
18655       if (origin == NULL && DECL_ARTIFICIAL (node))
18656         add_AT_flag (parm_die, DW_AT_artificial, 1);
18657
18658       if (node && node != origin)
18659         equate_decl_number_to_die (node, parm_die);
18660       if (! DECL_ABSTRACT (node_or_origin))
18661         add_location_or_const_value_attribute (parm_die, node_or_origin,
18662                                                node == NULL, DW_AT_location);
18663
18664       break;
18665
18666     case tcc_type:
18667       /* We were called with some kind of a ..._TYPE node.  */
18668       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18669       break;
18670
18671     default:
18672       gcc_unreachable ();
18673     }
18674
18675   return parm_die;
18676 }
18677
18678 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18679    children DW_TAG_formal_parameter DIEs representing the arguments of the
18680    parameter pack.
18681
18682    PARM_PACK must be a function parameter pack.
18683    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18684    must point to the subsequent arguments of the function PACK_ARG belongs to.
18685    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18686    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18687    following the last one for which a DIE was generated.  */
18688
18689 static dw_die_ref
18690 gen_formal_parameter_pack_die  (tree parm_pack,
18691                                 tree pack_arg,
18692                                 dw_die_ref subr_die,
18693                                 tree *next_arg)
18694 {
18695   tree arg;
18696   dw_die_ref parm_pack_die;
18697
18698   gcc_assert (parm_pack
18699               && lang_hooks.function_parameter_pack_p (parm_pack)
18700               && subr_die);
18701
18702   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18703   add_src_coords_attributes (parm_pack_die, parm_pack);
18704
18705   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18706     {
18707       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18708                                                                  parm_pack))
18709         break;
18710       gen_formal_parameter_die (arg, NULL,
18711                                 false /* Don't emit name attribute.  */,
18712                                 parm_pack_die);
18713     }
18714   if (next_arg)
18715     *next_arg = arg;
18716   return parm_pack_die;
18717 }
18718
18719 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18720    at the end of an (ANSI prototyped) formal parameters list.  */
18721
18722 static void
18723 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18724 {
18725   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18726 }
18727
18728 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18729    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18730    parameters as specified in some function type specification (except for
18731    those which appear as part of a function *definition*).  */
18732
18733 static void
18734 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18735 {
18736   tree link;
18737   tree formal_type = NULL;
18738   tree first_parm_type;
18739   tree arg;
18740
18741   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18742     {
18743       arg = DECL_ARGUMENTS (function_or_method_type);
18744       function_or_method_type = TREE_TYPE (function_or_method_type);
18745     }
18746   else
18747     arg = NULL_TREE;
18748
18749   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18750
18751   /* Make our first pass over the list of formal parameter types and output a
18752      DW_TAG_formal_parameter DIE for each one.  */
18753   for (link = first_parm_type; link; )
18754     {
18755       dw_die_ref parm_die;
18756
18757       formal_type = TREE_VALUE (link);
18758       if (formal_type == void_type_node)
18759         break;
18760
18761       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18762       parm_die = gen_formal_parameter_die (formal_type, NULL,
18763                                            true /* Emit name attribute.  */,
18764                                            context_die);
18765       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18766           && link == first_parm_type)
18767         {
18768           add_AT_flag (parm_die, DW_AT_artificial, 1);
18769           if (dwarf_version >= 3 || !dwarf_strict)
18770             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18771         }
18772       else if (arg && DECL_ARTIFICIAL (arg))
18773         add_AT_flag (parm_die, DW_AT_artificial, 1);
18774
18775       link = TREE_CHAIN (link);
18776       if (arg)
18777         arg = DECL_CHAIN (arg);
18778     }
18779
18780   /* If this function type has an ellipsis, add a
18781      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18782   if (formal_type != void_type_node)
18783     gen_unspecified_parameters_die (function_or_method_type, context_die);
18784
18785   /* Make our second (and final) pass over the list of formal parameter types
18786      and output DIEs to represent those types (as necessary).  */
18787   for (link = TYPE_ARG_TYPES (function_or_method_type);
18788        link && TREE_VALUE (link);
18789        link = TREE_CHAIN (link))
18790     gen_type_die (TREE_VALUE (link), context_die);
18791 }
18792
18793 /* We want to generate the DIE for TYPE so that we can generate the
18794    die for MEMBER, which has been defined; we will need to refer back
18795    to the member declaration nested within TYPE.  If we're trying to
18796    generate minimal debug info for TYPE, processing TYPE won't do the
18797    trick; we need to attach the member declaration by hand.  */
18798
18799 static void
18800 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18801 {
18802   gen_type_die (type, context_die);
18803
18804   /* If we're trying to avoid duplicate debug info, we may not have
18805      emitted the member decl for this function.  Emit it now.  */
18806   if (TYPE_STUB_DECL (type)
18807       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18808       && ! lookup_decl_die (member))
18809     {
18810       dw_die_ref type_die;
18811       gcc_assert (!decl_ultimate_origin (member));
18812
18813       push_decl_scope (type);
18814       type_die = lookup_type_die_strip_naming_typedef (type);
18815       if (TREE_CODE (member) == FUNCTION_DECL)
18816         gen_subprogram_die (member, type_die);
18817       else if (TREE_CODE (member) == FIELD_DECL)
18818         {
18819           /* Ignore the nameless fields that are used to skip bits but handle
18820              C++ anonymous unions and structs.  */
18821           if (DECL_NAME (member) != NULL_TREE
18822               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18823               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18824             {
18825               gen_type_die (member_declared_type (member), type_die);
18826               gen_field_die (member, type_die);
18827             }
18828         }
18829       else
18830         gen_variable_die (member, NULL_TREE, type_die);
18831
18832       pop_decl_scope ();
18833     }
18834 }
18835
18836 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18837    may later generate inlined and/or out-of-line instances of.  */
18838
18839 static void
18840 dwarf2out_abstract_function (tree decl)
18841 {
18842   dw_die_ref old_die;
18843   tree save_fn;
18844   tree context;
18845   int was_abstract;
18846   htab_t old_decl_loc_table;
18847   htab_t old_cached_dw_loc_list_table;
18848   int old_call_site_count, old_tail_call_site_count;
18849   struct call_arg_loc_node *old_call_arg_locations;
18850
18851   /* Make sure we have the actual abstract inline, not a clone.  */
18852   decl = DECL_ORIGIN (decl);
18853
18854   old_die = lookup_decl_die (decl);
18855   if (old_die && get_AT (old_die, DW_AT_inline))
18856     /* We've already generated the abstract instance.  */
18857     return;
18858
18859   /* We can be called while recursively when seeing block defining inlined subroutine
18860      DIE.  Be sure to not clobber the outer location table nor use it or we would
18861      get locations in abstract instantces.  */
18862   old_decl_loc_table = decl_loc_table;
18863   decl_loc_table = NULL;
18864   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18865   cached_dw_loc_list_table = NULL;
18866   old_call_arg_locations = call_arg_locations;
18867   call_arg_locations = NULL;
18868   old_call_site_count = call_site_count;
18869   call_site_count = -1;
18870   old_tail_call_site_count = tail_call_site_count;
18871   tail_call_site_count = -1;
18872
18873   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18874      we don't get confused by DECL_ABSTRACT.  */
18875   if (debug_info_level > DINFO_LEVEL_TERSE)
18876     {
18877       context = decl_class_context (decl);
18878       if (context)
18879         gen_type_die_for_member
18880           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18881     }
18882
18883   /* Pretend we've just finished compiling this function.  */
18884   save_fn = current_function_decl;
18885   current_function_decl = decl;
18886   push_cfun (DECL_STRUCT_FUNCTION (decl));
18887
18888   was_abstract = DECL_ABSTRACT (decl);
18889   set_decl_abstract_flags (decl, 1);
18890   dwarf2out_decl (decl);
18891   if (! was_abstract)
18892     set_decl_abstract_flags (decl, 0);
18893
18894   current_function_decl = save_fn;
18895   decl_loc_table = old_decl_loc_table;
18896   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18897   call_arg_locations = old_call_arg_locations;
18898   call_site_count = old_call_site_count;
18899   tail_call_site_count = old_tail_call_site_count;
18900   pop_cfun ();
18901 }
18902
18903 /* Helper function of premark_used_types() which gets called through
18904    htab_traverse.
18905
18906    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18907    marked as unused by prune_unused_types.  */
18908
18909 static int
18910 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18911 {
18912   tree type;
18913   dw_die_ref die;
18914
18915   type = (tree) *slot;
18916   die = lookup_type_die (type);
18917   if (die != NULL)
18918     die->die_perennial_p = 1;
18919   return 1;
18920 }
18921
18922 /* Helper function of premark_types_used_by_global_vars which gets called
18923    through htab_traverse.
18924
18925    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18926    marked as unused by prune_unused_types. The DIE of the type is marked
18927    only if the global variable using the type will actually be emitted.  */
18928
18929 static int
18930 premark_types_used_by_global_vars_helper (void **slot,
18931                                           void *data ATTRIBUTE_UNUSED)
18932 {
18933   struct types_used_by_vars_entry *entry;
18934   dw_die_ref die;
18935
18936   entry = (struct types_used_by_vars_entry *) *slot;
18937   gcc_assert (entry->type != NULL
18938               && entry->var_decl != NULL);
18939   die = lookup_type_die (entry->type);
18940   if (die)
18941     {
18942       /* Ask cgraph if the global variable really is to be emitted.
18943          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18944       struct varpool_node *node = varpool_get_node (entry->var_decl);
18945       if (node && node->needed)
18946         {
18947           die->die_perennial_p = 1;
18948           /* Keep the parent DIEs as well.  */
18949           while ((die = die->die_parent) && die->die_perennial_p == 0)
18950             die->die_perennial_p = 1;
18951         }
18952     }
18953   return 1;
18954 }
18955
18956 /* Mark all members of used_types_hash as perennial.  */
18957
18958 static void
18959 premark_used_types (void)
18960 {
18961   if (cfun && cfun->used_types_hash)
18962     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18963 }
18964
18965 /* Mark all members of types_used_by_vars_entry as perennial.  */
18966
18967 static void
18968 premark_types_used_by_global_vars (void)
18969 {
18970   if (types_used_by_vars_hash)
18971     htab_traverse (types_used_by_vars_hash,
18972                    premark_types_used_by_global_vars_helper, NULL);
18973 }
18974
18975 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18976    for CA_LOC call arg loc node.  */
18977
18978 static dw_die_ref
18979 gen_call_site_die (tree decl, dw_die_ref subr_die,
18980                    struct call_arg_loc_node *ca_loc)
18981 {
18982   dw_die_ref stmt_die = NULL, die;
18983   tree block = ca_loc->block;
18984
18985   while (block
18986          && block != DECL_INITIAL (decl)
18987          && TREE_CODE (block) == BLOCK)
18988     {
18989       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
18990         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
18991       if (stmt_die)
18992         break;
18993       block = BLOCK_SUPERCONTEXT (block);
18994     }
18995   if (stmt_die == NULL)
18996     stmt_die = subr_die;
18997   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18998   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18999   if (ca_loc->tail_call_p)
19000     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19001   if (ca_loc->symbol_ref)
19002     {
19003       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19004       if (tdie)
19005         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19006       else
19007         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
19008     }
19009   return die;
19010 }
19011
19012 /* Generate a DIE to represent a declared function (either file-scope or
19013    block-local).  */
19014
19015 static void
19016 gen_subprogram_die (tree decl, dw_die_ref context_die)
19017 {
19018   tree origin = decl_ultimate_origin (decl);
19019   dw_die_ref subr_die;
19020   tree outer_scope;
19021   dw_die_ref old_die = lookup_decl_die (decl);
19022   int declaration = (current_function_decl != decl
19023                      || class_or_namespace_scope_p (context_die));
19024
19025   premark_used_types ();
19026
19027   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19028      started to generate the abstract instance of an inline, decided to output
19029      its containing class, and proceeded to emit the declaration of the inline
19030      from the member list for the class.  If so, DECLARATION takes priority;
19031      we'll get back to the abstract instance when done with the class.  */
19032
19033   /* The class-scope declaration DIE must be the primary DIE.  */
19034   if (origin && declaration && class_or_namespace_scope_p (context_die))
19035     {
19036       origin = NULL;
19037       gcc_assert (!old_die);
19038     }
19039
19040   /* Now that the C++ front end lazily declares artificial member fns, we
19041      might need to retrofit the declaration into its class.  */
19042   if (!declaration && !origin && !old_die
19043       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19044       && !class_or_namespace_scope_p (context_die)
19045       && debug_info_level > DINFO_LEVEL_TERSE)
19046     old_die = force_decl_die (decl);
19047
19048   if (origin != NULL)
19049     {
19050       gcc_assert (!declaration || local_scope_p (context_die));
19051
19052       /* Fixup die_parent for the abstract instance of a nested
19053          inline function.  */
19054       if (old_die && old_die->die_parent == NULL)
19055         add_child_die (context_die, old_die);
19056
19057       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19058       add_abstract_origin_attribute (subr_die, origin);
19059     }
19060   else if (old_die)
19061     {
19062       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19063       struct dwarf_file_data * file_index = lookup_filename (s.file);
19064
19065       if (!get_AT_flag (old_die, DW_AT_declaration)
19066           /* We can have a normal definition following an inline one in the
19067              case of redefinition of GNU C extern inlines.
19068              It seems reasonable to use AT_specification in this case.  */
19069           && !get_AT (old_die, DW_AT_inline))
19070         {
19071           /* Detect and ignore this case, where we are trying to output
19072              something we have already output.  */
19073           return;
19074         }
19075
19076       /* If the definition comes from the same place as the declaration,
19077          maybe use the old DIE.  We always want the DIE for this function
19078          that has the *_pc attributes to be under comp_unit_die so the
19079          debugger can find it.  We also need to do this for abstract
19080          instances of inlines, since the spec requires the out-of-line copy
19081          to have the same parent.  For local class methods, this doesn't
19082          apply; we just use the old DIE.  */
19083       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19084           && (DECL_ARTIFICIAL (decl)
19085               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19086                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19087                       == (unsigned) s.line))))
19088         {
19089           subr_die = old_die;
19090
19091           /* Clear out the declaration attribute and the formal parameters.
19092              Do not remove all children, because it is possible that this
19093              declaration die was forced using force_decl_die(). In such
19094              cases die that forced declaration die (e.g. TAG_imported_module)
19095              is one of the children that we do not want to remove.  */
19096           remove_AT (subr_die, DW_AT_declaration);
19097           remove_AT (subr_die, DW_AT_object_pointer);
19098           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19099         }
19100       else
19101         {
19102           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19103           add_AT_specification (subr_die, old_die);
19104           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19105             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19106           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19107             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19108         }
19109     }
19110   else
19111     {
19112       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19113
19114       if (TREE_PUBLIC (decl))
19115         add_AT_flag (subr_die, DW_AT_external, 1);
19116
19117       add_name_and_src_coords_attributes (subr_die, decl);
19118       if (debug_info_level > DINFO_LEVEL_TERSE)
19119         {
19120           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19121           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19122                               0, 0, context_die);
19123         }
19124
19125       add_pure_or_virtual_attribute (subr_die, decl);
19126       if (DECL_ARTIFICIAL (decl))
19127         add_AT_flag (subr_die, DW_AT_artificial, 1);
19128
19129       add_accessibility_attribute (subr_die, decl);
19130     }
19131
19132   if (declaration)
19133     {
19134       if (!old_die || !get_AT (old_die, DW_AT_inline))
19135         {
19136           add_AT_flag (subr_die, DW_AT_declaration, 1);
19137
19138           /* If this is an explicit function declaration then generate
19139              a DW_AT_explicit attribute.  */
19140           if (lang_hooks.decls.function_decl_explicit_p (decl)
19141               && (dwarf_version >= 3 || !dwarf_strict))
19142             add_AT_flag (subr_die, DW_AT_explicit, 1);
19143
19144           /* The first time we see a member function, it is in the context of
19145              the class to which it belongs.  We make sure of this by emitting
19146              the class first.  The next time is the definition, which is
19147              handled above.  The two may come from the same source text.
19148
19149              Note that force_decl_die() forces function declaration die. It is
19150              later reused to represent definition.  */
19151           equate_decl_number_to_die (decl, subr_die);
19152         }
19153     }
19154   else if (DECL_ABSTRACT (decl))
19155     {
19156       if (DECL_DECLARED_INLINE_P (decl))
19157         {
19158           if (cgraph_function_possibly_inlined_p (decl))
19159             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19160           else
19161             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19162         }
19163       else
19164         {
19165           if (cgraph_function_possibly_inlined_p (decl))
19166             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19167           else
19168             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19169         }
19170
19171       if (DECL_DECLARED_INLINE_P (decl)
19172           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19173         add_AT_flag (subr_die, DW_AT_artificial, 1);
19174
19175       equate_decl_number_to_die (decl, subr_die);
19176     }
19177   else if (!DECL_EXTERNAL (decl))
19178     {
19179       HOST_WIDE_INT cfa_fb_offset;
19180
19181       if (!old_die || !get_AT (old_die, DW_AT_inline))
19182         equate_decl_number_to_die (decl, subr_die);
19183
19184       if (!flag_reorder_blocks_and_partition)
19185         {
19186           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19187           if (fde->dw_fde_begin)
19188             {
19189               /* We have already generated the labels.  */
19190               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19191               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19192             }
19193           else
19194             {
19195               /* Create start/end labels and add the range.  */
19196               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19197               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19198                                            current_function_funcdef_no);
19199               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19200               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19201                                            current_function_funcdef_no);
19202               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19203             }
19204
19205 #if VMS_DEBUGGING_INFO
19206       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19207          Section 2.3 Prologue and Epilogue Attributes:
19208          When a breakpoint is set on entry to a function, it is generally
19209          desirable for execution to be suspended, not on the very first
19210          instruction of the function, but rather at a point after the
19211          function's frame has been set up, after any language defined local
19212          declaration processing has been completed, and before execution of
19213          the first statement of the function begins. Debuggers generally
19214          cannot properly determine where this point is.  Similarly for a
19215          breakpoint set on exit from a function. The prologue and epilogue
19216          attributes allow a compiler to communicate the location(s) to use.  */
19217
19218       {
19219         if (fde->dw_fde_vms_end_prologue)
19220           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19221             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19222
19223         if (fde->dw_fde_vms_begin_epilogue)
19224           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19225             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19226       }
19227 #endif
19228
19229           add_pubname (decl, subr_die);
19230         }
19231       else
19232         {  /* Generate pubnames entries for the split function code
19233               ranges.  */
19234           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19235
19236           if (fde->dw_fde_second_begin)
19237             {
19238               if (dwarf_version >= 3 || !dwarf_strict)
19239                 {
19240                   /* We should use ranges for non-contiguous code section 
19241                      addresses.  Use the actual code range for the initial
19242                      section, since the HOT/COLD labels might precede an 
19243                      alignment offset.  */
19244                   bool range_list_added = false;
19245                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19246                                         fde->dw_fde_end, &range_list_added);
19247                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19248                                         fde->dw_fde_second_end,
19249                                         &range_list_added);
19250                   add_pubname (decl, subr_die);
19251                   if (range_list_added)
19252                     add_ranges (NULL);
19253                 }
19254               else
19255                 {
19256                   /* There is no real support in DW2 for this .. so we make
19257                      a work-around.  First, emit the pub name for the segment
19258                      containing the function label.  Then make and emit a
19259                      simplified subprogram DIE for the second segment with the
19260                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19261                      linkage name for the second die so that gdb will find both
19262                      sections when given "b foo".  */
19263                   const char *name = NULL;
19264                   tree decl_name = DECL_NAME (decl);
19265                   dw_die_ref seg_die;
19266
19267                   /* Do the 'primary' section.   */
19268                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19269                                  fde->dw_fde_begin);
19270                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19271                                  fde->dw_fde_end);
19272                   /* Add it.   */
19273                   add_pubname (decl, subr_die);
19274
19275                   /* Build a minimal DIE for the secondary section.  */
19276                   seg_die = new_die (DW_TAG_subprogram,
19277                                      subr_die->die_parent, decl);
19278
19279                   if (TREE_PUBLIC (decl))
19280                     add_AT_flag (seg_die, DW_AT_external, 1);
19281
19282                   if (decl_name != NULL 
19283                       && IDENTIFIER_POINTER (decl_name) != NULL)
19284                     {
19285                       name = dwarf2_name (decl, 1);
19286                       if (! DECL_ARTIFICIAL (decl))
19287                         add_src_coords_attributes (seg_die, decl);
19288
19289                       add_linkage_name (seg_die, decl);
19290                     }
19291                   gcc_assert (name != NULL);
19292                   add_pure_or_virtual_attribute (seg_die, decl);
19293                   if (DECL_ARTIFICIAL (decl))
19294                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19295
19296                   name = concat ("__second_sect_of_", name, NULL); 
19297                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
19298                                  fde->dw_fde_second_begin);
19299                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
19300                                  fde->dw_fde_second_end);
19301                   add_name_attribute (seg_die, name);
19302                   add_pubname_string (name, seg_die);
19303                 }
19304             }
19305           else
19306             {
19307               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19308               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19309               add_pubname (decl, subr_die);
19310             }
19311         }
19312
19313 #ifdef MIPS_DEBUGGING_INFO
19314       /* Add a reference to the FDE for this routine.  */
19315       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19316 #endif
19317
19318       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19319
19320       /* We define the "frame base" as the function's CFA.  This is more
19321          convenient for several reasons: (1) It's stable across the prologue
19322          and epilogue, which makes it better than just a frame pointer,
19323          (2) With dwarf3, there exists a one-byte encoding that allows us
19324          to reference the .debug_frame data by proxy, but failing that,
19325          (3) We can at least reuse the code inspection and interpretation
19326          code that determines the CFA position at various points in the
19327          function.  */
19328       if (dwarf_version >= 3)
19329         {
19330           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19331           add_AT_loc (subr_die, DW_AT_frame_base, op);
19332         }
19333       else
19334         {
19335           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19336           if (list->dw_loc_next)
19337             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19338           else
19339             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19340         }
19341
19342       /* Compute a displacement from the "steady-state frame pointer" to
19343          the CFA.  The former is what all stack slots and argument slots
19344          will reference in the rtl; the later is what we've told the
19345          debugger about.  We'll need to adjust all frame_base references
19346          by this displacement.  */
19347       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19348
19349       if (cfun->static_chain_decl)
19350         add_AT_location_description (subr_die, DW_AT_static_link,
19351                  loc_list_from_tree (cfun->static_chain_decl, 2));
19352     }
19353
19354   /* Generate child dies for template paramaters.  */
19355   if (debug_info_level > DINFO_LEVEL_TERSE)
19356     gen_generic_params_dies (decl);
19357
19358   /* Now output descriptions of the arguments for this function. This gets
19359      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19360      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19361      `...' at the end of the formal parameter list.  In order to find out if
19362      there was a trailing ellipsis or not, we must instead look at the type
19363      associated with the FUNCTION_DECL.  This will be a node of type
19364      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19365      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19366      an ellipsis at the end.  */
19367
19368   /* In the case where we are describing a mere function declaration, all we
19369      need to do here (and all we *can* do here) is to describe the *types* of
19370      its formal parameters.  */
19371   if (debug_info_level <= DINFO_LEVEL_TERSE)
19372     ;
19373   else if (declaration)
19374     gen_formal_types_die (decl, subr_die);
19375   else
19376     {
19377       /* Generate DIEs to represent all known formal parameters.  */
19378       tree parm = DECL_ARGUMENTS (decl);
19379       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19380       tree generic_decl_parm = generic_decl
19381                                 ? DECL_ARGUMENTS (generic_decl)
19382                                 : NULL;
19383
19384       /* Now we want to walk the list of parameters of the function and
19385          emit their relevant DIEs.
19386
19387          We consider the case of DECL being an instance of a generic function
19388          as well as it being a normal function.
19389
19390          If DECL is an instance of a generic function we walk the
19391          parameters of the generic function declaration _and_ the parameters of
19392          DECL itself. This is useful because we want to emit specific DIEs for
19393          function parameter packs and those are declared as part of the
19394          generic function declaration. In that particular case,
19395          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19396          That DIE has children DIEs representing the set of arguments
19397          of the pack. Note that the set of pack arguments can be empty.
19398          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19399          children DIE.
19400
19401          Otherwise, we just consider the parameters of DECL.  */
19402       while (generic_decl_parm || parm)
19403         {
19404           if (generic_decl_parm
19405               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19406             gen_formal_parameter_pack_die (generic_decl_parm,
19407                                            parm, subr_die,
19408                                            &parm);
19409           else if (parm)
19410             {
19411               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19412
19413               if (parm == DECL_ARGUMENTS (decl)
19414                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19415                   && parm_die
19416                   && (dwarf_version >= 3 || !dwarf_strict))
19417                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19418
19419               parm = DECL_CHAIN (parm);
19420             }
19421
19422           if (generic_decl_parm)
19423             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19424         }
19425
19426       /* Decide whether we need an unspecified_parameters DIE at the end.
19427          There are 2 more cases to do this for: 1) the ansi ... declaration -
19428          this is detectable when the end of the arg list is not a
19429          void_type_node 2) an unprototyped function declaration (not a
19430          definition).  This just means that we have no info about the
19431          parameters at all.  */
19432       if (prototype_p (TREE_TYPE (decl)))
19433         {
19434           /* This is the prototyped case, check for....  */
19435           if (stdarg_p (TREE_TYPE (decl)))
19436             gen_unspecified_parameters_die (decl, subr_die);
19437         }
19438       else if (DECL_INITIAL (decl) == NULL_TREE)
19439         gen_unspecified_parameters_die (decl, subr_die);
19440     }
19441
19442   /* Output Dwarf info for all of the stuff within the body of the function
19443      (if it has one - it may be just a declaration).  */
19444   outer_scope = DECL_INITIAL (decl);
19445
19446   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19447      a function.  This BLOCK actually represents the outermost binding contour
19448      for the function, i.e. the contour in which the function's formal
19449      parameters and labels get declared. Curiously, it appears that the front
19450      end doesn't actually put the PARM_DECL nodes for the current function onto
19451      the BLOCK_VARS list for this outer scope, but are strung off of the
19452      DECL_ARGUMENTS list for the function instead.
19453
19454      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19455      the LABEL_DECL nodes for the function however, and we output DWARF info
19456      for those in decls_for_scope.  Just within the `outer_scope' there will be
19457      a BLOCK node representing the function's outermost pair of curly braces,
19458      and any blocks used for the base and member initializers of a C++
19459      constructor function.  */
19460   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19461     {
19462       int call_site_note_count = 0;
19463       int tail_call_site_note_count = 0;
19464
19465       /* Emit a DW_TAG_variable DIE for a named return value.  */
19466       if (DECL_NAME (DECL_RESULT (decl)))
19467         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19468
19469       current_function_has_inlines = 0;
19470       decls_for_scope (outer_scope, subr_die, 0);
19471
19472       if (call_arg_locations && !dwarf_strict)
19473         {
19474           struct call_arg_loc_node *ca_loc;
19475           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19476             {
19477               dw_die_ref die = NULL;
19478               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19479               rtx arg, next_arg;
19480
19481               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19482                    arg; arg = next_arg)
19483                 {
19484                   dw_loc_descr_ref reg, val;
19485                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19486                   dw_die_ref cdie;
19487
19488                   next_arg = XEXP (arg, 1);
19489                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19490                       && next_arg
19491                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19492                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19493                       && REGNO (XEXP (XEXP (arg, 0), 0))
19494                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19495                     next_arg = XEXP (next_arg, 1);
19496                   if (mode == VOIDmode)
19497                     {
19498                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19499                       if (mode == VOIDmode)
19500                         mode = GET_MODE (XEXP (arg, 0));
19501                     }
19502                   if (GET_MODE_CLASS (mode) != MODE_INT
19503                       || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19504                     continue;
19505                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19506                     {
19507                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19508                       tloc = XEXP (XEXP (arg, 0), 1);
19509                       continue;
19510                     }
19511                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19512                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19513                     {
19514                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19515                       tlocc = XEXP (XEXP (arg, 0), 1);
19516                       continue;
19517                     }
19518                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19519                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19520                                               VAR_INIT_STATUS_INITIALIZED);
19521                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19522                     reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19523                                                           0), 0), mode,
19524                                               VAR_INIT_STATUS_INITIALIZED);
19525                   else
19526                     continue;
19527                   if (reg == NULL)
19528                     continue;
19529                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19530                                             VAR_INIT_STATUS_INITIALIZED);
19531                   if (val == NULL)
19532                     continue;
19533                   if (die == NULL)
19534                     die = gen_call_site_die (decl, subr_die, ca_loc);
19535                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19536                                   NULL_TREE);           
19537                   add_AT_loc (cdie, DW_AT_location, reg);
19538                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19539                   if (next_arg != XEXP (arg, 1))
19540                     {
19541                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19542                                                             0), 1), VOIDmode,
19543                                                 VAR_INIT_STATUS_INITIALIZED);
19544                       if (val != NULL)
19545                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19546                     }
19547                 }
19548               if (die == NULL
19549                   && (ca_loc->symbol_ref || tloc))
19550                 die = gen_call_site_die (decl, subr_die, ca_loc);
19551               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19552                 {
19553                   dw_loc_descr_ref tval = NULL;
19554
19555                   if (tloc != NULL_RTX)
19556                     tval = mem_loc_descriptor (tloc, VOIDmode,
19557                                                VAR_INIT_STATUS_INITIALIZED);
19558                   if (tval)
19559                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19560                   else if (tlocc != NULL_RTX)
19561                     {
19562                       tval = mem_loc_descriptor (tlocc, VOIDmode,
19563                                                  VAR_INIT_STATUS_INITIALIZED);
19564                       if (tval)
19565                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19566                                     tval);
19567                     }
19568                 }
19569               if (die != NULL)
19570                 {
19571                   call_site_note_count++;
19572                   if (ca_loc->tail_call_p)
19573                     tail_call_site_note_count++;
19574                 }
19575             }
19576         }
19577       call_arg_locations = NULL;
19578       call_arg_loc_last = NULL;
19579       if (tail_call_site_count >= 0
19580           && tail_call_site_count == tail_call_site_note_count
19581           && !dwarf_strict)
19582         {
19583           if (call_site_count >= 0
19584               && call_site_count == call_site_note_count)
19585             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19586           else
19587             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19588         }
19589       call_site_count = -1;
19590       tail_call_site_count = -1;
19591     }
19592   /* Add the calling convention attribute if requested.  */
19593   add_calling_convention_attribute (subr_die, decl);
19594
19595 }
19596
19597 /* Returns a hash value for X (which really is a die_struct).  */
19598
19599 static hashval_t
19600 common_block_die_table_hash (const void *x)
19601 {
19602   const_dw_die_ref d = (const_dw_die_ref) x;
19603   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19604 }
19605
19606 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19607    as decl_id and die_parent of die_struct Y.  */
19608
19609 static int
19610 common_block_die_table_eq (const void *x, const void *y)
19611 {
19612   const_dw_die_ref d = (const_dw_die_ref) x;
19613   const_dw_die_ref e = (const_dw_die_ref) y;
19614   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19615 }
19616
19617 /* Generate a DIE to represent a declared data object.
19618    Either DECL or ORIGIN must be non-null.  */
19619
19620 static void
19621 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19622 {
19623   HOST_WIDE_INT off;
19624   tree com_decl;
19625   tree decl_or_origin = decl ? decl : origin;
19626   tree ultimate_origin;
19627   dw_die_ref var_die;
19628   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19629   dw_die_ref origin_die;
19630   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19631                       || class_or_namespace_scope_p (context_die));
19632   bool specialization_p = false;
19633
19634   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19635   if (decl || ultimate_origin)
19636     origin = ultimate_origin;
19637   com_decl = fortran_common (decl_or_origin, &off);
19638
19639   /* Symbol in common gets emitted as a child of the common block, in the form
19640      of a data member.  */
19641   if (com_decl)
19642     {
19643       dw_die_ref com_die;
19644       dw_loc_list_ref loc;
19645       die_node com_die_arg;
19646
19647       var_die = lookup_decl_die (decl_or_origin);
19648       if (var_die)
19649         {
19650           if (get_AT (var_die, DW_AT_location) == NULL)
19651             {
19652               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19653               if (loc)
19654                 {
19655                   if (off)
19656                     {
19657                       /* Optimize the common case.  */
19658                       if (single_element_loc_list_p (loc)
19659                           && loc->expr->dw_loc_opc == DW_OP_addr
19660                           && loc->expr->dw_loc_next == NULL
19661                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19662                              == SYMBOL_REF)
19663                         loc->expr->dw_loc_oprnd1.v.val_addr
19664                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19665                         else
19666                           loc_list_plus_const (loc, off);
19667                     }
19668                   add_AT_location_description (var_die, DW_AT_location, loc);
19669                   remove_AT (var_die, DW_AT_declaration);
19670                 }
19671             }
19672           return;
19673         }
19674
19675       if (common_block_die_table == NULL)
19676         common_block_die_table
19677           = htab_create_ggc (10, common_block_die_table_hash,
19678                              common_block_die_table_eq, NULL);
19679
19680       com_die_arg.decl_id = DECL_UID (com_decl);
19681       com_die_arg.die_parent = context_die;
19682       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19683       loc = loc_list_from_tree (com_decl, 2);
19684       if (com_die == NULL)
19685         {
19686           const char *cnam
19687             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19688           void **slot;
19689
19690           com_die = new_die (DW_TAG_common_block, context_die, decl);
19691           add_name_and_src_coords_attributes (com_die, com_decl);
19692           if (loc)
19693             {
19694               add_AT_location_description (com_die, DW_AT_location, loc);
19695               /* Avoid sharing the same loc descriptor between
19696                  DW_TAG_common_block and DW_TAG_variable.  */
19697               loc = loc_list_from_tree (com_decl, 2);
19698             }
19699           else if (DECL_EXTERNAL (decl))
19700             add_AT_flag (com_die, DW_AT_declaration, 1);
19701           add_pubname_string (cnam, com_die); /* ??? needed? */
19702           com_die->decl_id = DECL_UID (com_decl);
19703           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19704           *slot = (void *) com_die;
19705         }
19706       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19707         {
19708           add_AT_location_description (com_die, DW_AT_location, loc);
19709           loc = loc_list_from_tree (com_decl, 2);
19710           remove_AT (com_die, DW_AT_declaration);
19711         }
19712       var_die = new_die (DW_TAG_variable, com_die, decl);
19713       add_name_and_src_coords_attributes (var_die, decl);
19714       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19715                           TREE_THIS_VOLATILE (decl), context_die);
19716       add_AT_flag (var_die, DW_AT_external, 1);
19717       if (loc)
19718         {
19719           if (off)
19720             {
19721               /* Optimize the common case.  */
19722               if (single_element_loc_list_p (loc)
19723                   && loc->expr->dw_loc_opc == DW_OP_addr
19724                   && loc->expr->dw_loc_next == NULL
19725                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19726                 loc->expr->dw_loc_oprnd1.v.val_addr
19727                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19728               else
19729                 loc_list_plus_const (loc, off);
19730             }
19731           add_AT_location_description (var_die, DW_AT_location, loc);
19732         }
19733       else if (DECL_EXTERNAL (decl))
19734         add_AT_flag (var_die, DW_AT_declaration, 1);
19735       equate_decl_number_to_die (decl, var_die);
19736       return;
19737     }
19738
19739   /* If the compiler emitted a definition for the DECL declaration
19740      and if we already emitted a DIE for it, don't emit a second
19741      DIE for it again. Allow re-declarations of DECLs that are
19742      inside functions, though.  */
19743   if (old_die && declaration && !local_scope_p (context_die))
19744     return;
19745
19746   /* For static data members, the declaration in the class is supposed
19747      to have DW_TAG_member tag; the specification should still be
19748      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19749   if (declaration && class_scope_p (context_die))
19750     var_die = new_die (DW_TAG_member, context_die, decl);
19751   else
19752     var_die = new_die (DW_TAG_variable, context_die, decl);
19753
19754   origin_die = NULL;
19755   if (origin != NULL)
19756     origin_die = add_abstract_origin_attribute (var_die, origin);
19757
19758   /* Loop unrolling can create multiple blocks that refer to the same
19759      static variable, so we must test for the DW_AT_declaration flag.
19760
19761      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19762      copy decls and set the DECL_ABSTRACT flag on them instead of
19763      sharing them.
19764
19765      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19766
19767      ??? The declare_in_namespace support causes us to get two DIEs for one
19768      variable, both of which are declarations.  We want to avoid considering
19769      one to be a specification, so we must test that this DIE is not a
19770      declaration.  */
19771   else if (old_die && TREE_STATIC (decl) && ! declaration
19772            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19773     {
19774       /* This is a definition of a C++ class level static.  */
19775       add_AT_specification (var_die, old_die);
19776       specialization_p = true;
19777       if (DECL_NAME (decl))
19778         {
19779           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19780           struct dwarf_file_data * file_index = lookup_filename (s.file);
19781
19782           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19783             add_AT_file (var_die, DW_AT_decl_file, file_index);
19784
19785           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19786             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19787
19788           if (old_die->die_tag == DW_TAG_member)
19789             add_linkage_name (var_die, decl);
19790         }
19791     }
19792   else
19793     add_name_and_src_coords_attributes (var_die, decl);
19794
19795   if ((origin == NULL && !specialization_p)
19796       || (origin != NULL
19797           && !DECL_ABSTRACT (decl_or_origin)
19798           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19799                                        decl_function_context
19800                                                         (decl_or_origin))))
19801     {
19802       tree type = TREE_TYPE (decl_or_origin);
19803
19804       if (decl_by_reference_p (decl_or_origin))
19805         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19806       else
19807         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19808                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19809     }
19810
19811   if (origin == NULL && !specialization_p)
19812     {
19813       if (TREE_PUBLIC (decl))
19814         add_AT_flag (var_die, DW_AT_external, 1);
19815
19816       if (DECL_ARTIFICIAL (decl))
19817         add_AT_flag (var_die, DW_AT_artificial, 1);
19818
19819       add_accessibility_attribute (var_die, decl);
19820     }
19821
19822   if (declaration)
19823     add_AT_flag (var_die, DW_AT_declaration, 1);
19824
19825   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19826     equate_decl_number_to_die (decl, var_die);
19827
19828   if (! declaration
19829       && (! DECL_ABSTRACT (decl_or_origin)
19830           /* Local static vars are shared between all clones/inlines,
19831              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19832              already set.  */
19833           || (TREE_CODE (decl_or_origin) == VAR_DECL
19834               && TREE_STATIC (decl_or_origin)
19835               && DECL_RTL_SET_P (decl_or_origin)))
19836       /* When abstract origin already has DW_AT_location attribute, no need
19837          to add it again.  */
19838       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19839     {
19840       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19841           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19842         defer_location (decl_or_origin, var_die);
19843       else
19844         add_location_or_const_value_attribute (var_die, decl_or_origin,
19845                                                decl == NULL, DW_AT_location);
19846       add_pubname (decl_or_origin, var_die);
19847     }
19848   else
19849     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19850 }
19851
19852 /* Generate a DIE to represent a named constant.  */
19853
19854 static void
19855 gen_const_die (tree decl, dw_die_ref context_die)
19856 {
19857   dw_die_ref const_die;
19858   tree type = TREE_TYPE (decl);
19859
19860   const_die = new_die (DW_TAG_constant, context_die, decl);
19861   add_name_and_src_coords_attributes (const_die, decl);
19862   add_type_attribute (const_die, type, 1, 0, context_die);
19863   if (TREE_PUBLIC (decl))
19864     add_AT_flag (const_die, DW_AT_external, 1);
19865   if (DECL_ARTIFICIAL (decl))
19866     add_AT_flag (const_die, DW_AT_artificial, 1);
19867   tree_add_const_value_attribute_for_decl (const_die, decl);
19868 }
19869
19870 /* Generate a DIE to represent a label identifier.  */
19871
19872 static void
19873 gen_label_die (tree decl, dw_die_ref context_die)
19874 {
19875   tree origin = decl_ultimate_origin (decl);
19876   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19877   rtx insn;
19878   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19879
19880   if (origin != NULL)
19881     add_abstract_origin_attribute (lbl_die, origin);
19882   else
19883     add_name_and_src_coords_attributes (lbl_die, decl);
19884
19885   if (DECL_ABSTRACT (decl))
19886     equate_decl_number_to_die (decl, lbl_die);
19887   else
19888     {
19889       insn = DECL_RTL_IF_SET (decl);
19890
19891       /* Deleted labels are programmer specified labels which have been
19892          eliminated because of various optimizations.  We still emit them
19893          here so that it is possible to put breakpoints on them.  */
19894       if (insn
19895           && (LABEL_P (insn)
19896               || ((NOTE_P (insn)
19897                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19898         {
19899           /* When optimization is enabled (via -O) some parts of the compiler
19900              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19901              represent source-level labels which were explicitly declared by
19902              the user.  This really shouldn't be happening though, so catch
19903              it if it ever does happen.  */
19904           gcc_assert (!INSN_DELETED_P (insn));
19905
19906           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19907           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19908         }
19909     }
19910 }
19911
19912 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19913    attributes to the DIE for a block STMT, to describe where the inlined
19914    function was called from.  This is similar to add_src_coords_attributes.  */
19915
19916 static inline void
19917 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19918 {
19919   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19920
19921   if (dwarf_version >= 3 || !dwarf_strict)
19922     {
19923       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19924       add_AT_unsigned (die, DW_AT_call_line, s.line);
19925     }
19926 }
19927
19928
19929 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19930    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19931
19932 static inline void
19933 add_high_low_attributes (tree stmt, dw_die_ref die)
19934 {
19935   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19936
19937   if (BLOCK_FRAGMENT_CHAIN (stmt)
19938       && (dwarf_version >= 3 || !dwarf_strict))
19939     {
19940       tree chain;
19941
19942       if (inlined_function_outer_scope_p (stmt))
19943         {
19944           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19945                                        BLOCK_NUMBER (stmt));
19946           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19947         }
19948
19949       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19950
19951       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19952       do
19953         {
19954           add_ranges (chain);
19955           chain = BLOCK_FRAGMENT_CHAIN (chain);
19956         }
19957       while (chain);
19958       add_ranges (NULL);
19959     }
19960   else
19961     {
19962       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19963                                    BLOCK_NUMBER (stmt));
19964       add_AT_lbl_id (die, DW_AT_low_pc, label);
19965       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19966                                    BLOCK_NUMBER (stmt));
19967       add_AT_lbl_id (die, DW_AT_high_pc, label);
19968     }
19969 }
19970
19971 /* Generate a DIE for a lexical block.  */
19972
19973 static void
19974 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19975 {
19976   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19977
19978   if (call_arg_locations)
19979     {
19980       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
19981         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
19982                                BLOCK_NUMBER (stmt) + 1);
19983       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
19984     }
19985
19986   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19987     add_high_low_attributes (stmt, stmt_die);
19988
19989   decls_for_scope (stmt, stmt_die, depth);
19990 }
19991
19992 /* Generate a DIE for an inlined subprogram.  */
19993
19994 static void
19995 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19996 {
19997   tree decl;
19998
19999   /* The instance of function that is effectively being inlined shall not
20000      be abstract.  */
20001   gcc_assert (! BLOCK_ABSTRACT (stmt));
20002
20003   decl = block_ultimate_origin (stmt);
20004
20005   /* Emit info for the abstract instance first, if we haven't yet.  We
20006      must emit this even if the block is abstract, otherwise when we
20007      emit the block below (or elsewhere), we may end up trying to emit
20008      a die whose origin die hasn't been emitted, and crashing.  */
20009   dwarf2out_abstract_function (decl);
20010
20011   if (! BLOCK_ABSTRACT (stmt))
20012     {
20013       dw_die_ref subr_die
20014         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20015
20016       if (call_arg_locations)
20017         {
20018           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20019             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20020                                    BLOCK_NUMBER (stmt) + 1);
20021           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20022         }
20023       add_abstract_origin_attribute (subr_die, decl);
20024       if (TREE_ASM_WRITTEN (stmt))
20025         add_high_low_attributes (stmt, subr_die);
20026       add_call_src_coords_attributes (stmt, subr_die);
20027
20028       decls_for_scope (stmt, subr_die, depth);
20029       current_function_has_inlines = 1;
20030     }
20031 }
20032
20033 /* Generate a DIE for a field in a record, or structure.  */
20034
20035 static void
20036 gen_field_die (tree decl, dw_die_ref context_die)
20037 {
20038   dw_die_ref decl_die;
20039
20040   if (TREE_TYPE (decl) == error_mark_node)
20041     return;
20042
20043   decl_die = new_die (DW_TAG_member, context_die, decl);
20044   add_name_and_src_coords_attributes (decl_die, decl);
20045   add_type_attribute (decl_die, member_declared_type (decl),
20046                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20047                       context_die);
20048
20049   if (DECL_BIT_FIELD_TYPE (decl))
20050     {
20051       add_byte_size_attribute (decl_die, decl);
20052       add_bit_size_attribute (decl_die, decl);
20053       add_bit_offset_attribute (decl_die, decl);
20054     }
20055
20056   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20057     add_data_member_location_attribute (decl_die, decl);
20058
20059   if (DECL_ARTIFICIAL (decl))
20060     add_AT_flag (decl_die, DW_AT_artificial, 1);
20061
20062   add_accessibility_attribute (decl_die, decl);
20063
20064   /* Equate decl number to die, so that we can look up this decl later on.  */
20065   equate_decl_number_to_die (decl, decl_die);
20066 }
20067
20068 #if 0
20069 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20070    Use modified_type_die instead.
20071    We keep this code here just in case these types of DIEs may be needed to
20072    represent certain things in other languages (e.g. Pascal) someday.  */
20073
20074 static void
20075 gen_pointer_type_die (tree type, dw_die_ref context_die)
20076 {
20077   dw_die_ref ptr_die
20078     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20079
20080   equate_type_number_to_die (type, ptr_die);
20081   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20082   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20083 }
20084
20085 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20086    Use modified_type_die instead.
20087    We keep this code here just in case these types of DIEs may be needed to
20088    represent certain things in other languages (e.g. Pascal) someday.  */
20089
20090 static void
20091 gen_reference_type_die (tree type, dw_die_ref context_die)
20092 {
20093   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20094
20095   if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
20096     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20097   else
20098     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20099
20100   equate_type_number_to_die (type, ref_die);
20101   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20102   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20103 }
20104 #endif
20105
20106 /* Generate a DIE for a pointer to a member type.  */
20107
20108 static void
20109 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20110 {
20111   dw_die_ref ptr_die
20112     = new_die (DW_TAG_ptr_to_member_type,
20113                scope_die_for (type, context_die), type);
20114
20115   equate_type_number_to_die (type, ptr_die);
20116   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20117                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20118   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20119 }
20120
20121 /* Generate the DIE for the compilation unit.  */
20122
20123 static dw_die_ref
20124 gen_compile_unit_die (const char *filename)
20125 {
20126   dw_die_ref die;
20127   char producer[250];
20128   const char *language_string = lang_hooks.name;
20129   int language;
20130
20131   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20132
20133   if (filename)
20134     {
20135       add_name_attribute (die, filename);
20136       /* Don't add cwd for <built-in>.  */
20137       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20138         add_comp_dir_attribute (die);
20139     }
20140
20141   sprintf (producer, "%s %s", language_string, version_string);
20142
20143 #ifdef MIPS_DEBUGGING_INFO
20144   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20145      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20146      not appear in the producer string, the debugger reaches the conclusion
20147      that the object file is stripped and has no debugging information.
20148      To get the MIPS/SGI debugger to believe that there is debugging
20149      information in the object file, we add a -g to the producer string.  */
20150   if (debug_info_level > DINFO_LEVEL_TERSE)
20151     strcat (producer, " -g");
20152 #endif
20153
20154   add_AT_string (die, DW_AT_producer, producer);
20155
20156   /* If our producer is LTO try to figure out a common language to use
20157      from the global list of translation units.  */
20158   if (strcmp (language_string, "GNU GIMPLE") == 0)
20159     {
20160       unsigned i;
20161       tree t;
20162       const char *common_lang = NULL;
20163
20164       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20165         {
20166           if (!TRANSLATION_UNIT_LANGUAGE (t))
20167             continue;
20168           if (!common_lang)
20169             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20170           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20171             ;
20172           else if (strncmp (common_lang, "GNU C", 5) == 0
20173                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20174             /* Mixing C and C++ is ok, use C++ in that case.  */
20175             common_lang = "GNU C++";
20176           else
20177             {
20178               /* Fall back to C.  */
20179               common_lang = NULL;
20180               break;
20181             }
20182         }
20183
20184       if (common_lang)
20185         language_string = common_lang;
20186     }
20187
20188   language = DW_LANG_C89;
20189   if (strcmp (language_string, "GNU C++") == 0)
20190     language = DW_LANG_C_plus_plus;
20191   else if (strcmp (language_string, "GNU F77") == 0)
20192     language = DW_LANG_Fortran77;
20193   else if (strcmp (language_string, "GNU Pascal") == 0)
20194     language = DW_LANG_Pascal83;
20195   else if (dwarf_version >= 3 || !dwarf_strict)
20196     {
20197       if (strcmp (language_string, "GNU Ada") == 0)
20198         language = DW_LANG_Ada95;
20199       else if (strcmp (language_string, "GNU Fortran") == 0)
20200         language = DW_LANG_Fortran95;
20201       else if (strcmp (language_string, "GNU Java") == 0)
20202         language = DW_LANG_Java;
20203       else if (strcmp (language_string, "GNU Objective-C") == 0)
20204         language = DW_LANG_ObjC;
20205       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20206         language = DW_LANG_ObjC_plus_plus;
20207     }
20208
20209   add_AT_unsigned (die, DW_AT_language, language);
20210
20211   switch (language)
20212     {
20213     case DW_LANG_Fortran77:
20214     case DW_LANG_Fortran90:
20215     case DW_LANG_Fortran95:
20216       /* Fortran has case insensitive identifiers and the front-end
20217          lowercases everything.  */
20218       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20219       break;
20220     default:
20221       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20222       break;
20223     }
20224   return die;
20225 }
20226
20227 /* Generate the DIE for a base class.  */
20228
20229 static void
20230 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20231 {
20232   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20233
20234   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20235   add_data_member_location_attribute (die, binfo);
20236
20237   if (BINFO_VIRTUAL_P (binfo))
20238     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20239
20240   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20241      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20242      the default has always been DW_ACCESS_private.  */
20243   if (access == access_public_node)
20244     {
20245       if (dwarf_version == 2
20246           || context_die->die_tag == DW_TAG_class_type)
20247       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20248     }
20249   else if (access == access_protected_node)
20250     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20251   else if (dwarf_version > 2
20252            && context_die->die_tag != DW_TAG_class_type)
20253     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20254 }
20255
20256 /* Generate a DIE for a class member.  */
20257
20258 static void
20259 gen_member_die (tree type, dw_die_ref context_die)
20260 {
20261   tree member;
20262   tree binfo = TYPE_BINFO (type);
20263   dw_die_ref child;
20264
20265   /* If this is not an incomplete type, output descriptions of each of its
20266      members. Note that as we output the DIEs necessary to represent the
20267      members of this record or union type, we will also be trying to output
20268      DIEs to represent the *types* of those members. However the `type'
20269      function (above) will specifically avoid generating type DIEs for member
20270      types *within* the list of member DIEs for this (containing) type except
20271      for those types (of members) which are explicitly marked as also being
20272      members of this (containing) type themselves.  The g++ front- end can
20273      force any given type to be treated as a member of some other (containing)
20274      type by setting the TYPE_CONTEXT of the given (member) type to point to
20275      the TREE node representing the appropriate (containing) type.  */
20276
20277   /* First output info about the base classes.  */
20278   if (binfo)
20279     {
20280       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20281       int i;
20282       tree base;
20283
20284       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20285         gen_inheritance_die (base,
20286                              (accesses ? VEC_index (tree, accesses, i)
20287                               : access_public_node), context_die);
20288     }
20289
20290   /* Now output info about the data members and type members.  */
20291   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20292     {
20293       /* If we thought we were generating minimal debug info for TYPE
20294          and then changed our minds, some of the member declarations
20295          may have already been defined.  Don't define them again, but
20296          do put them in the right order.  */
20297
20298       child = lookup_decl_die (member);
20299       if (child)
20300         splice_child_die (context_die, child);
20301       else
20302         gen_decl_die (member, NULL, context_die);
20303     }
20304
20305   /* Now output info about the function members (if any).  */
20306   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20307     {
20308       /* Don't include clones in the member list.  */
20309       if (DECL_ABSTRACT_ORIGIN (member))
20310         continue;
20311
20312       child = lookup_decl_die (member);
20313       if (child)
20314         splice_child_die (context_die, child);
20315       else
20316         gen_decl_die (member, NULL, context_die);
20317     }
20318 }
20319
20320 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20321    is set, we pretend that the type was never defined, so we only get the
20322    member DIEs needed by later specification DIEs.  */
20323
20324 static void
20325 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20326                                 enum debug_info_usage usage)
20327 {
20328   dw_die_ref type_die = lookup_type_die (type);
20329   dw_die_ref scope_die = 0;
20330   int nested = 0;
20331   int complete = (TYPE_SIZE (type)
20332                   && (! TYPE_STUB_DECL (type)
20333                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20334   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20335   complete = complete && should_emit_struct_debug (type, usage);
20336
20337   if (type_die && ! complete)
20338     return;
20339
20340   if (TYPE_CONTEXT (type) != NULL_TREE
20341       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20342           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20343     nested = 1;
20344
20345   scope_die = scope_die_for (type, context_die);
20346
20347   if (! type_die || (nested && is_cu_die (scope_die)))
20348     /* First occurrence of type or toplevel definition of nested class.  */
20349     {
20350       dw_die_ref old_die = type_die;
20351
20352       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20353                           ? record_type_tag (type) : DW_TAG_union_type,
20354                           scope_die, type);
20355       equate_type_number_to_die (type, type_die);
20356       if (old_die)
20357         add_AT_specification (type_die, old_die);
20358       else
20359         {
20360           add_name_attribute (type_die, type_tag (type));
20361           add_gnat_descriptive_type_attribute (type_die, type, context_die);
20362         }
20363     }
20364   else
20365     remove_AT (type_die, DW_AT_declaration);
20366
20367   /* Generate child dies for template paramaters.  */
20368   if (debug_info_level > DINFO_LEVEL_TERSE
20369       && COMPLETE_TYPE_P (type))
20370     schedule_generic_params_dies_gen (type);
20371
20372   /* If this type has been completed, then give it a byte_size attribute and
20373      then give a list of members.  */
20374   if (complete && !ns_decl)
20375     {
20376       /* Prevent infinite recursion in cases where the type of some member of
20377          this type is expressed in terms of this type itself.  */
20378       TREE_ASM_WRITTEN (type) = 1;
20379       add_byte_size_attribute (type_die, type);
20380       if (TYPE_STUB_DECL (type) != NULL_TREE)
20381         {
20382           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20383           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20384         }
20385
20386       /* If the first reference to this type was as the return type of an
20387          inline function, then it may not have a parent.  Fix this now.  */
20388       if (type_die->die_parent == NULL)
20389         add_child_die (scope_die, type_die);
20390
20391       push_decl_scope (type);
20392       gen_member_die (type, type_die);
20393       pop_decl_scope ();
20394
20395       /* GNU extension: Record what type our vtable lives in.  */
20396       if (TYPE_VFIELD (type))
20397         {
20398           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20399
20400           gen_type_die (vtype, context_die);
20401           add_AT_die_ref (type_die, DW_AT_containing_type,
20402                           lookup_type_die (vtype));
20403         }
20404     }
20405   else
20406     {
20407       add_AT_flag (type_die, DW_AT_declaration, 1);
20408
20409       /* We don't need to do this for function-local types.  */
20410       if (TYPE_STUB_DECL (type)
20411           && ! decl_function_context (TYPE_STUB_DECL (type)))
20412         VEC_safe_push (tree, gc, incomplete_types, type);
20413     }
20414
20415   if (get_AT (type_die, DW_AT_name))
20416     add_pubtype (type, type_die);
20417 }
20418
20419 /* Generate a DIE for a subroutine _type_.  */
20420
20421 static void
20422 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20423 {
20424   tree return_type = TREE_TYPE (type);
20425   dw_die_ref subr_die
20426     = new_die (DW_TAG_subroutine_type,
20427                scope_die_for (type, context_die), type);
20428
20429   equate_type_number_to_die (type, subr_die);
20430   add_prototyped_attribute (subr_die, type);
20431   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20432   gen_formal_types_die (type, subr_die);
20433
20434   if (get_AT (subr_die, DW_AT_name))
20435     add_pubtype (type, subr_die);
20436 }
20437
20438 /* Generate a DIE for a type definition.  */
20439
20440 static void
20441 gen_typedef_die (tree decl, dw_die_ref context_die)
20442 {
20443   dw_die_ref type_die;
20444   tree origin;
20445
20446   if (TREE_ASM_WRITTEN (decl))
20447     return;
20448
20449   TREE_ASM_WRITTEN (decl) = 1;
20450   type_die = new_die (DW_TAG_typedef, context_die, decl);
20451   origin = decl_ultimate_origin (decl);
20452   if (origin != NULL)
20453     add_abstract_origin_attribute (type_die, origin);
20454   else
20455     {
20456       tree type;
20457
20458       add_name_and_src_coords_attributes (type_die, decl);
20459       if (DECL_ORIGINAL_TYPE (decl))
20460         {
20461           type = DECL_ORIGINAL_TYPE (decl);
20462
20463           gcc_assert (type != TREE_TYPE (decl));
20464           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20465         }
20466       else
20467         {
20468           type = TREE_TYPE (decl);
20469
20470           if (is_naming_typedef_decl (TYPE_NAME (type)))
20471             {
20472               /* Here, we are in the case of decl being a typedef naming
20473                  an anonymous type, e.g:
20474                      typedef struct {...} foo;
20475                  In that case TREE_TYPE (decl) is not a typedef variant
20476                  type and TYPE_NAME of the anonymous type is set to the
20477                  TYPE_DECL of the typedef. This construct is emitted by
20478                  the C++ FE.
20479
20480                  TYPE is the anonymous struct named by the typedef
20481                  DECL. As we need the DW_AT_type attribute of the
20482                  DW_TAG_typedef to point to the DIE of TYPE, let's
20483                  generate that DIE right away. add_type_attribute
20484                  called below will then pick (via lookup_type_die) that
20485                  anonymous struct DIE.  */
20486               if (!TREE_ASM_WRITTEN (type))
20487                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20488
20489               /* This is a GNU Extension.  We are adding a
20490                  DW_AT_linkage_name attribute to the DIE of the
20491                  anonymous struct TYPE.  The value of that attribute
20492                  is the name of the typedef decl naming the anonymous
20493                  struct.  This greatly eases the work of consumers of
20494                  this debug info.  */
20495               add_linkage_attr (lookup_type_die (type), decl);
20496             }
20497         }
20498
20499       add_type_attribute (type_die, type, TREE_READONLY (decl),
20500                           TREE_THIS_VOLATILE (decl), context_die);
20501
20502       if (is_naming_typedef_decl (decl))
20503         /* We want that all subsequent calls to lookup_type_die with
20504            TYPE in argument yield the DW_TAG_typedef we have just
20505            created.  */
20506         equate_type_number_to_die (type, type_die);
20507
20508       add_accessibility_attribute (type_die, decl);
20509     }
20510
20511   if (DECL_ABSTRACT (decl))
20512     equate_decl_number_to_die (decl, type_die);
20513
20514   if (get_AT (type_die, DW_AT_name))
20515     add_pubtype (decl, type_die);
20516 }
20517
20518 /* Generate a DIE for a struct, class, enum or union type.  */
20519
20520 static void
20521 gen_tagged_type_die (tree type,
20522                      dw_die_ref context_die,
20523                      enum debug_info_usage usage)
20524 {
20525   int need_pop;
20526
20527   if (type == NULL_TREE
20528       || !is_tagged_type (type))
20529     return;
20530
20531   /* If this is a nested type whose containing class hasn't been written
20532      out yet, writing it out will cover this one, too.  This does not apply
20533      to instantiations of member class templates; they need to be added to
20534      the containing class as they are generated.  FIXME: This hurts the
20535      idea of combining type decls from multiple TUs, since we can't predict
20536      what set of template instantiations we'll get.  */
20537   if (TYPE_CONTEXT (type)
20538       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20539       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20540     {
20541       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20542
20543       if (TREE_ASM_WRITTEN (type))
20544         return;
20545
20546       /* If that failed, attach ourselves to the stub.  */
20547       push_decl_scope (TYPE_CONTEXT (type));
20548       context_die = lookup_type_die (TYPE_CONTEXT (type));
20549       need_pop = 1;
20550     }
20551   else if (TYPE_CONTEXT (type) != NULL_TREE
20552            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20553     {
20554       /* If this type is local to a function that hasn't been written
20555          out yet, use a NULL context for now; it will be fixed up in
20556          decls_for_scope.  */
20557       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20558       /* A declaration DIE doesn't count; nested types need to go in the
20559          specification.  */
20560       if (context_die && is_declaration_die (context_die))
20561         context_die = NULL;
20562       need_pop = 0;
20563     }
20564   else
20565     {
20566       context_die = declare_in_namespace (type, context_die);
20567       need_pop = 0;
20568     }
20569
20570   if (TREE_CODE (type) == ENUMERAL_TYPE)
20571     {
20572       /* This might have been written out by the call to
20573          declare_in_namespace.  */
20574       if (!TREE_ASM_WRITTEN (type))
20575         gen_enumeration_type_die (type, context_die);
20576     }
20577   else
20578     gen_struct_or_union_type_die (type, context_die, usage);
20579
20580   if (need_pop)
20581     pop_decl_scope ();
20582
20583   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20584      it up if it is ever completed.  gen_*_type_die will set it for us
20585      when appropriate.  */
20586 }
20587
20588 /* Generate a type description DIE.  */
20589
20590 static void
20591 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20592                          enum debug_info_usage usage)
20593 {
20594   struct array_descr_info info;
20595
20596   if (type == NULL_TREE || type == error_mark_node)
20597     return;
20598
20599   if (TYPE_NAME (type) != NULL_TREE
20600       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20601       && is_redundant_typedef (TYPE_NAME (type))
20602       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20603     /* The DECL of this type is a typedef we don't want to emit debug
20604        info for but we want debug info for its underlying typedef.
20605        This can happen for e.g, the injected-class-name of a C++
20606        type.  */
20607     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20608
20609   /* If TYPE is a typedef type variant, let's generate debug info
20610      for the parent typedef which TYPE is a type of.  */
20611   if (typedef_variant_p (type))
20612     {
20613       if (TREE_ASM_WRITTEN (type))
20614         return;
20615
20616       /* Prevent broken recursion; we can't hand off to the same type.  */
20617       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20618
20619       /* Use the DIE of the containing namespace as the parent DIE of
20620          the type description DIE we want to generate.  */
20621       if (DECL_CONTEXT (TYPE_NAME (type))
20622           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20623         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20624
20625       TREE_ASM_WRITTEN (type) = 1;
20626
20627       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20628       return;
20629     }
20630
20631   /* If type is an anonymous tagged type named by a typedef, let's
20632      generate debug info for the typedef.  */
20633   if (is_naming_typedef_decl (TYPE_NAME (type)))
20634     {
20635       /* Use the DIE of the containing namespace as the parent DIE of
20636          the type description DIE we want to generate.  */
20637       if (DECL_CONTEXT (TYPE_NAME (type))
20638           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20639         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20640       
20641       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20642       return;
20643     }
20644
20645   /* If this is an array type with hidden descriptor, handle it first.  */
20646   if (!TREE_ASM_WRITTEN (type)
20647       && lang_hooks.types.get_array_descr_info
20648       && lang_hooks.types.get_array_descr_info (type, &info)
20649       && (dwarf_version >= 3 || !dwarf_strict))
20650     {
20651       gen_descr_array_type_die (type, &info, context_die);
20652       TREE_ASM_WRITTEN (type) = 1;
20653       return;
20654     }
20655
20656   /* We are going to output a DIE to represent the unqualified version
20657      of this type (i.e. without any const or volatile qualifiers) so
20658      get the main variant (i.e. the unqualified version) of this type
20659      now.  (Vectors are special because the debugging info is in the
20660      cloned type itself).  */
20661   if (TREE_CODE (type) != VECTOR_TYPE)
20662     type = type_main_variant (type);
20663
20664   if (TREE_ASM_WRITTEN (type))
20665     return;
20666
20667   switch (TREE_CODE (type))
20668     {
20669     case ERROR_MARK:
20670       break;
20671
20672     case POINTER_TYPE:
20673     case REFERENCE_TYPE:
20674       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20675          ensures that the gen_type_die recursion will terminate even if the
20676          type is recursive.  Recursive types are possible in Ada.  */
20677       /* ??? We could perhaps do this for all types before the switch
20678          statement.  */
20679       TREE_ASM_WRITTEN (type) = 1;
20680
20681       /* For these types, all that is required is that we output a DIE (or a
20682          set of DIEs) to represent the "basis" type.  */
20683       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20684                                 DINFO_USAGE_IND_USE);
20685       break;
20686
20687     case OFFSET_TYPE:
20688       /* This code is used for C++ pointer-to-data-member types.
20689          Output a description of the relevant class type.  */
20690       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20691                                         DINFO_USAGE_IND_USE);
20692
20693       /* Output a description of the type of the object pointed to.  */
20694       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20695                                         DINFO_USAGE_IND_USE);
20696
20697       /* Now output a DIE to represent this pointer-to-data-member type
20698          itself.  */
20699       gen_ptr_to_mbr_type_die (type, context_die);
20700       break;
20701
20702     case FUNCTION_TYPE:
20703       /* Force out return type (in case it wasn't forced out already).  */
20704       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20705                                         DINFO_USAGE_DIR_USE);
20706       gen_subroutine_type_die (type, context_die);
20707       break;
20708
20709     case METHOD_TYPE:
20710       /* Force out return type (in case it wasn't forced out already).  */
20711       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20712                                         DINFO_USAGE_DIR_USE);
20713       gen_subroutine_type_die (type, context_die);
20714       break;
20715
20716     case ARRAY_TYPE:
20717       gen_array_type_die (type, context_die);
20718       break;
20719
20720     case VECTOR_TYPE:
20721       gen_array_type_die (type, context_die);
20722       break;
20723
20724     case ENUMERAL_TYPE:
20725     case RECORD_TYPE:
20726     case UNION_TYPE:
20727     case QUAL_UNION_TYPE:
20728       gen_tagged_type_die (type, context_die, usage);
20729       return;
20730
20731     case VOID_TYPE:
20732     case INTEGER_TYPE:
20733     case REAL_TYPE:
20734     case FIXED_POINT_TYPE:
20735     case COMPLEX_TYPE:
20736     case BOOLEAN_TYPE:
20737       /* No DIEs needed for fundamental types.  */
20738       break;
20739
20740     case NULLPTR_TYPE:
20741     case LANG_TYPE:
20742       /* Just use DW_TAG_unspecified_type.  */
20743       {
20744         dw_die_ref type_die = lookup_type_die (type);
20745         if (type_die == NULL)
20746           {
20747             tree name = TYPE_NAME (type);
20748             if (TREE_CODE (name) == TYPE_DECL)
20749               name = DECL_NAME (name);
20750             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20751             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20752             equate_type_number_to_die (type, type_die);
20753           }
20754       }
20755       break;
20756
20757     default:
20758       gcc_unreachable ();
20759     }
20760
20761   TREE_ASM_WRITTEN (type) = 1;
20762 }
20763
20764 static void
20765 gen_type_die (tree type, dw_die_ref context_die)
20766 {
20767   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20768 }
20769
20770 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20771    things which are local to the given block.  */
20772
20773 static void
20774 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20775 {
20776   int must_output_die = 0;
20777   bool inlined_func;
20778
20779   /* Ignore blocks that are NULL.  */
20780   if (stmt == NULL_TREE)
20781     return;
20782
20783   inlined_func = inlined_function_outer_scope_p (stmt);
20784
20785   /* If the block is one fragment of a non-contiguous block, do not
20786      process the variables, since they will have been done by the
20787      origin block.  Do process subblocks.  */
20788   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20789     {
20790       tree sub;
20791
20792       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20793         gen_block_die (sub, context_die, depth + 1);
20794
20795       return;
20796     }
20797
20798   /* Determine if we need to output any Dwarf DIEs at all to represent this
20799      block.  */
20800   if (inlined_func)
20801     /* The outer scopes for inlinings *must* always be represented.  We
20802        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20803     must_output_die = 1;
20804   else
20805     {
20806       /* Determine if this block directly contains any "significant"
20807          local declarations which we will need to output DIEs for.  */
20808       if (debug_info_level > DINFO_LEVEL_TERSE)
20809         /* We are not in terse mode so *any* local declaration counts
20810            as being a "significant" one.  */
20811         must_output_die = ((BLOCK_VARS (stmt) != NULL
20812                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20813                            && (TREE_USED (stmt)
20814                                || TREE_ASM_WRITTEN (stmt)
20815                                || BLOCK_ABSTRACT (stmt)));
20816       else if ((TREE_USED (stmt)
20817                 || TREE_ASM_WRITTEN (stmt)
20818                 || BLOCK_ABSTRACT (stmt))
20819                && !dwarf2out_ignore_block (stmt))
20820         must_output_die = 1;
20821     }
20822
20823   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20824      DIE for any block which contains no significant local declarations at
20825      all.  Rather, in such cases we just call `decls_for_scope' so that any
20826      needed Dwarf info for any sub-blocks will get properly generated. Note
20827      that in terse mode, our definition of what constitutes a "significant"
20828      local declaration gets restricted to include only inlined function
20829      instances and local (nested) function definitions.  */
20830   if (must_output_die)
20831     {
20832       if (inlined_func)
20833         {
20834           /* If STMT block is abstract, that means we have been called
20835              indirectly from dwarf2out_abstract_function.
20836              That function rightfully marks the descendent blocks (of
20837              the abstract function it is dealing with) as being abstract,
20838              precisely to prevent us from emitting any
20839              DW_TAG_inlined_subroutine DIE as a descendent
20840              of an abstract function instance. So in that case, we should
20841              not call gen_inlined_subroutine_die.
20842
20843              Later though, when cgraph asks dwarf2out to emit info
20844              for the concrete instance of the function decl into which
20845              the concrete instance of STMT got inlined, the later will lead
20846              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20847           if (! BLOCK_ABSTRACT (stmt))
20848             gen_inlined_subroutine_die (stmt, context_die, depth);
20849         }
20850       else
20851         gen_lexical_block_die (stmt, context_die, depth);
20852     }
20853   else
20854     decls_for_scope (stmt, context_die, depth);
20855 }
20856
20857 /* Process variable DECL (or variable with origin ORIGIN) within
20858    block STMT and add it to CONTEXT_DIE.  */
20859 static void
20860 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20861 {
20862   dw_die_ref die;
20863   tree decl_or_origin = decl ? decl : origin;
20864
20865   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20866     die = lookup_decl_die (decl_or_origin);
20867   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20868            && TYPE_DECL_IS_STUB (decl_or_origin))
20869     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20870   else
20871     die = NULL;
20872
20873   if (die != NULL && die->die_parent == NULL)
20874     add_child_die (context_die, die);
20875   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20876     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20877                                          stmt, context_die);
20878   else
20879     gen_decl_die (decl, origin, context_die);
20880 }
20881
20882 /* Generate all of the decls declared within a given scope and (recursively)
20883    all of its sub-blocks.  */
20884
20885 static void
20886 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20887 {
20888   tree decl;
20889   unsigned int i;
20890   tree subblocks;
20891
20892   /* Ignore NULL blocks.  */
20893   if (stmt == NULL_TREE)
20894     return;
20895
20896   /* Output the DIEs to represent all of the data objects and typedefs
20897      declared directly within this block but not within any nested
20898      sub-blocks.  Also, nested function and tag DIEs have been
20899      generated with a parent of NULL; fix that up now.  */
20900   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20901     process_scope_var (stmt, decl, NULL_TREE, context_die);
20902   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20903     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20904                        context_die);
20905
20906   /* If we're at -g1, we're not interested in subblocks.  */
20907   if (debug_info_level <= DINFO_LEVEL_TERSE)
20908     return;
20909
20910   /* Output the DIEs to represent all sub-blocks (and the items declared
20911      therein) of this block.  */
20912   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20913        subblocks != NULL;
20914        subblocks = BLOCK_CHAIN (subblocks))
20915     gen_block_die (subblocks, context_die, depth + 1);
20916 }
20917
20918 /* Is this a typedef we can avoid emitting?  */
20919
20920 static inline int
20921 is_redundant_typedef (const_tree decl)
20922 {
20923   if (TYPE_DECL_IS_STUB (decl))
20924     return 1;
20925
20926   if (DECL_ARTIFICIAL (decl)
20927       && DECL_CONTEXT (decl)
20928       && is_tagged_type (DECL_CONTEXT (decl))
20929       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20930       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20931     /* Also ignore the artificial member typedef for the class name.  */
20932     return 1;
20933
20934   return 0;
20935 }
20936
20937 /* Return TRUE if TYPE is a typedef that names a type for linkage
20938    purposes. This kind of typedefs is produced by the C++ FE for
20939    constructs like:
20940
20941    typedef struct {...} foo;
20942
20943    In that case, there is no typedef variant type produced for foo.
20944    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20945    struct type.  */
20946
20947 static bool
20948 is_naming_typedef_decl (const_tree decl)
20949 {
20950   if (decl == NULL_TREE
20951       || TREE_CODE (decl) != TYPE_DECL
20952       || !is_tagged_type (TREE_TYPE (decl))
20953       || DECL_IS_BUILTIN (decl)
20954       || is_redundant_typedef (decl)
20955       /* It looks like Ada produces TYPE_DECLs that are very similar
20956          to C++ naming typedefs but that have different
20957          semantics. Let's be specific to c++ for now.  */
20958       || !is_cxx ())
20959     return FALSE;
20960
20961   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20962           && TYPE_NAME (TREE_TYPE (decl)) == decl
20963           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20964               != TYPE_NAME (TREE_TYPE (decl))));
20965 }
20966
20967 /* Returns the DIE for a context.  */
20968
20969 static inline dw_die_ref
20970 get_context_die (tree context)
20971 {
20972   if (context)
20973     {
20974       /* Find die that represents this context.  */
20975       if (TYPE_P (context))
20976         {
20977           context = TYPE_MAIN_VARIANT (context);
20978           return strip_naming_typedef (context, force_type_die (context));
20979         }
20980       else
20981         return force_decl_die (context);
20982     }
20983   return comp_unit_die ();
20984 }
20985
20986 /* Returns the DIE for decl.  A DIE will always be returned.  */
20987
20988 static dw_die_ref
20989 force_decl_die (tree decl)
20990 {
20991   dw_die_ref decl_die;
20992   unsigned saved_external_flag;
20993   tree save_fn = NULL_TREE;
20994   decl_die = lookup_decl_die (decl);
20995   if (!decl_die)
20996     {
20997       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20998
20999       decl_die = lookup_decl_die (decl);
21000       if (decl_die)
21001         return decl_die;
21002
21003       switch (TREE_CODE (decl))
21004         {
21005         case FUNCTION_DECL:
21006           /* Clear current_function_decl, so that gen_subprogram_die thinks
21007              that this is a declaration. At this point, we just want to force
21008              declaration die.  */
21009           save_fn = current_function_decl;
21010           current_function_decl = NULL_TREE;
21011           gen_subprogram_die (decl, context_die);
21012           current_function_decl = save_fn;
21013           break;
21014
21015         case VAR_DECL:
21016           /* Set external flag to force declaration die. Restore it after
21017            gen_decl_die() call.  */
21018           saved_external_flag = DECL_EXTERNAL (decl);
21019           DECL_EXTERNAL (decl) = 1;
21020           gen_decl_die (decl, NULL, context_die);
21021           DECL_EXTERNAL (decl) = saved_external_flag;
21022           break;
21023
21024         case NAMESPACE_DECL:
21025           if (dwarf_version >= 3 || !dwarf_strict)
21026             dwarf2out_decl (decl);
21027           else
21028             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21029             decl_die = comp_unit_die ();
21030           break;
21031
21032         case TRANSLATION_UNIT_DECL:
21033           decl_die = comp_unit_die ();
21034           break;
21035
21036         default:
21037           gcc_unreachable ();
21038         }
21039
21040       /* We should be able to find the DIE now.  */
21041       if (!decl_die)
21042         decl_die = lookup_decl_die (decl);
21043       gcc_assert (decl_die);
21044     }
21045
21046   return decl_die;
21047 }
21048
21049 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21050    always returned.  */
21051
21052 static dw_die_ref
21053 force_type_die (tree type)
21054 {
21055   dw_die_ref type_die;
21056
21057   type_die = lookup_type_die (type);
21058   if (!type_die)
21059     {
21060       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21061
21062       type_die = modified_type_die (type, TYPE_READONLY (type),
21063                                     TYPE_VOLATILE (type), context_die);
21064       gcc_assert (type_die);
21065     }
21066   return type_die;
21067 }
21068
21069 /* Force out any required namespaces to be able to output DECL,
21070    and return the new context_die for it, if it's changed.  */
21071
21072 static dw_die_ref
21073 setup_namespace_context (tree thing, dw_die_ref context_die)
21074 {
21075   tree context = (DECL_P (thing)
21076                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21077   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21078     /* Force out the namespace.  */
21079     context_die = force_decl_die (context);
21080
21081   return context_die;
21082 }
21083
21084 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21085    type) within its namespace, if appropriate.
21086
21087    For compatibility with older debuggers, namespace DIEs only contain
21088    declarations; all definitions are emitted at CU scope.  */
21089
21090 static dw_die_ref
21091 declare_in_namespace (tree thing, dw_die_ref context_die)
21092 {
21093   dw_die_ref ns_context;
21094
21095   if (debug_info_level <= DINFO_LEVEL_TERSE)
21096     return context_die;
21097
21098   /* If this decl is from an inlined function, then don't try to emit it in its
21099      namespace, as we will get confused.  It would have already been emitted
21100      when the abstract instance of the inline function was emitted anyways.  */
21101   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21102     return context_die;
21103
21104   ns_context = setup_namespace_context (thing, context_die);
21105
21106   if (ns_context != context_die)
21107     {
21108       if (is_fortran ())
21109         return ns_context;
21110       if (DECL_P (thing))
21111         gen_decl_die (thing, NULL, ns_context);
21112       else
21113         gen_type_die (thing, ns_context);
21114     }
21115   return context_die;
21116 }
21117
21118 /* Generate a DIE for a namespace or namespace alias.  */
21119
21120 static void
21121 gen_namespace_die (tree decl, dw_die_ref context_die)
21122 {
21123   dw_die_ref namespace_die;
21124
21125   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21126      they are an alias of.  */
21127   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21128     {
21129       /* Output a real namespace or module.  */
21130       context_die = setup_namespace_context (decl, comp_unit_die ());
21131       namespace_die = new_die (is_fortran ()
21132                                ? DW_TAG_module : DW_TAG_namespace,
21133                                context_die, decl);
21134       /* For Fortran modules defined in different CU don't add src coords.  */
21135       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21136         {
21137           const char *name = dwarf2_name (decl, 0);
21138           if (name)
21139             add_name_attribute (namespace_die, name);
21140         }
21141       else
21142         add_name_and_src_coords_attributes (namespace_die, decl);
21143       if (DECL_EXTERNAL (decl))
21144         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21145       equate_decl_number_to_die (decl, namespace_die);
21146     }
21147   else
21148     {
21149       /* Output a namespace alias.  */
21150
21151       /* Force out the namespace we are an alias of, if necessary.  */
21152       dw_die_ref origin_die
21153         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21154
21155       if (DECL_FILE_SCOPE_P (decl)
21156           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21157         context_die = setup_namespace_context (decl, comp_unit_die ());
21158       /* Now create the namespace alias DIE.  */
21159       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21160       add_name_and_src_coords_attributes (namespace_die, decl);
21161       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21162       equate_decl_number_to_die (decl, namespace_die);
21163     }
21164 }
21165
21166 /* Generate Dwarf debug information for a decl described by DECL.
21167    The return value is currently only meaningful for PARM_DECLs,
21168    for all other decls it returns NULL.  */
21169
21170 static dw_die_ref
21171 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21172 {
21173   tree decl_or_origin = decl ? decl : origin;
21174   tree class_origin = NULL, ultimate_origin;
21175
21176   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21177     return NULL;
21178
21179   switch (TREE_CODE (decl_or_origin))
21180     {
21181     case ERROR_MARK:
21182       break;
21183
21184     case CONST_DECL:
21185       if (!is_fortran () && !is_ada ())
21186         {
21187           /* The individual enumerators of an enum type get output when we output
21188              the Dwarf representation of the relevant enum type itself.  */
21189           break;
21190         }
21191
21192       /* Emit its type.  */
21193       gen_type_die (TREE_TYPE (decl), context_die);
21194
21195       /* And its containing namespace.  */
21196       context_die = declare_in_namespace (decl, context_die);
21197
21198       gen_const_die (decl, context_die);
21199       break;
21200
21201     case FUNCTION_DECL:
21202       /* Don't output any DIEs to represent mere function declarations,
21203          unless they are class members or explicit block externs.  */
21204       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21205           && DECL_FILE_SCOPE_P (decl_or_origin)
21206           && (current_function_decl == NULL_TREE
21207               || DECL_ARTIFICIAL (decl_or_origin)))
21208         break;
21209
21210 #if 0
21211       /* FIXME */
21212       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21213          on local redeclarations of global functions.  That seems broken.  */
21214       if (current_function_decl != decl)
21215         /* This is only a declaration.  */;
21216 #endif
21217
21218       /* If we're emitting a clone, emit info for the abstract instance.  */
21219       if (origin || DECL_ORIGIN (decl) != decl)
21220         dwarf2out_abstract_function (origin
21221                                      ? DECL_ORIGIN (origin)
21222                                      : DECL_ABSTRACT_ORIGIN (decl));
21223
21224       /* If we're emitting an out-of-line copy of an inline function,
21225          emit info for the abstract instance and set up to refer to it.  */
21226       else if (cgraph_function_possibly_inlined_p (decl)
21227                && ! DECL_ABSTRACT (decl)
21228                && ! class_or_namespace_scope_p (context_die)
21229                /* dwarf2out_abstract_function won't emit a die if this is just
21230                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21231                   that case, because that works only if we have a die.  */
21232                && DECL_INITIAL (decl) != NULL_TREE)
21233         {
21234           dwarf2out_abstract_function (decl);
21235           set_decl_origin_self (decl);
21236         }
21237
21238       /* Otherwise we're emitting the primary DIE for this decl.  */
21239       else if (debug_info_level > DINFO_LEVEL_TERSE)
21240         {
21241           /* Before we describe the FUNCTION_DECL itself, make sure that we
21242              have its containing type.  */
21243           if (!origin)
21244             origin = decl_class_context (decl);
21245           if (origin != NULL_TREE)
21246             gen_type_die (origin, context_die);
21247
21248           /* And its return type.  */
21249           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21250
21251           /* And its virtual context.  */
21252           if (DECL_VINDEX (decl) != NULL_TREE)
21253             gen_type_die (DECL_CONTEXT (decl), context_die);
21254
21255           /* Make sure we have a member DIE for decl.  */
21256           if (origin != NULL_TREE)
21257             gen_type_die_for_member (origin, decl, context_die);
21258
21259           /* And its containing namespace.  */
21260           context_die = declare_in_namespace (decl, context_die);
21261         }
21262
21263       /* Now output a DIE to represent the function itself.  */
21264       if (decl)
21265         gen_subprogram_die (decl, context_die);
21266       break;
21267
21268     case TYPE_DECL:
21269       /* If we are in terse mode, don't generate any DIEs to represent any
21270          actual typedefs.  */
21271       if (debug_info_level <= DINFO_LEVEL_TERSE)
21272         break;
21273
21274       /* In the special case of a TYPE_DECL node representing the declaration
21275          of some type tag, if the given TYPE_DECL is marked as having been
21276          instantiated from some other (original) TYPE_DECL node (e.g. one which
21277          was generated within the original definition of an inline function) we
21278          used to generate a special (abbreviated) DW_TAG_structure_type,
21279          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21280          should be actually referencing those DIEs, as variable DIEs with that
21281          type would be emitted already in the abstract origin, so it was always
21282          removed during unused type prunning.  Don't add anything in this
21283          case.  */
21284       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21285         break;
21286
21287       if (is_redundant_typedef (decl))
21288         gen_type_die (TREE_TYPE (decl), context_die);
21289       else
21290         /* Output a DIE to represent the typedef itself.  */
21291         gen_typedef_die (decl, context_die);
21292       break;
21293
21294     case LABEL_DECL:
21295       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21296         gen_label_die (decl, context_die);
21297       break;
21298
21299     case VAR_DECL:
21300     case RESULT_DECL:
21301       /* If we are in terse mode, don't generate any DIEs to represent any
21302          variable declarations or definitions.  */
21303       if (debug_info_level <= DINFO_LEVEL_TERSE)
21304         break;
21305
21306       /* Output any DIEs that are needed to specify the type of this data
21307          object.  */
21308       if (decl_by_reference_p (decl_or_origin))
21309         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21310       else
21311         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21312
21313       /* And its containing type.  */
21314       class_origin = decl_class_context (decl_or_origin);
21315       if (class_origin != NULL_TREE)
21316         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21317
21318       /* And its containing namespace.  */
21319       context_die = declare_in_namespace (decl_or_origin, context_die);
21320
21321       /* Now output the DIE to represent the data object itself.  This gets
21322          complicated because of the possibility that the VAR_DECL really
21323          represents an inlined instance of a formal parameter for an inline
21324          function.  */
21325       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21326       if (ultimate_origin != NULL_TREE
21327           && TREE_CODE (ultimate_origin) == PARM_DECL)
21328         gen_formal_parameter_die (decl, origin,
21329                                   true /* Emit name attribute.  */,
21330                                   context_die);
21331       else
21332         gen_variable_die (decl, origin, context_die);
21333       break;
21334
21335     case FIELD_DECL:
21336       /* Ignore the nameless fields that are used to skip bits but handle C++
21337          anonymous unions and structs.  */
21338       if (DECL_NAME (decl) != NULL_TREE
21339           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21340           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21341         {
21342           gen_type_die (member_declared_type (decl), context_die);
21343           gen_field_die (decl, context_die);
21344         }
21345       break;
21346
21347     case PARM_DECL:
21348       if (DECL_BY_REFERENCE (decl_or_origin))
21349         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21350       else
21351         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21352       return gen_formal_parameter_die (decl, origin,
21353                                        true /* Emit name attribute.  */,
21354                                        context_die);
21355
21356     case NAMESPACE_DECL:
21357     case IMPORTED_DECL:
21358       if (dwarf_version >= 3 || !dwarf_strict)
21359         gen_namespace_die (decl, context_die);
21360       break;
21361
21362     default:
21363       /* Probably some frontend-internal decl.  Assume we don't care.  */
21364       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21365       break;
21366     }
21367
21368   return NULL;
21369 }
21370 \f
21371 /* Output debug information for global decl DECL.  Called from toplev.c after
21372    compilation proper has finished.  */
21373
21374 static void
21375 dwarf2out_global_decl (tree decl)
21376 {
21377   /* Output DWARF2 information for file-scope tentative data object
21378      declarations, file-scope (extern) function declarations (which
21379      had no corresponding body) and file-scope tagged type declarations
21380      and definitions which have not yet been forced out.  */
21381   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21382     dwarf2out_decl (decl);
21383 }
21384
21385 /* Output debug information for type decl DECL.  Called from toplev.c
21386    and from language front ends (to record built-in types).  */
21387 static void
21388 dwarf2out_type_decl (tree decl, int local)
21389 {
21390   if (!local)
21391     dwarf2out_decl (decl);
21392 }
21393
21394 /* Output debug information for imported module or decl DECL.
21395    NAME is non-NULL name in the lexical block if the decl has been renamed.
21396    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21397    that DECL belongs to.
21398    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21399 static void
21400 dwarf2out_imported_module_or_decl_1 (tree decl,
21401                                      tree name,
21402                                      tree lexical_block,
21403                                      dw_die_ref lexical_block_die)
21404 {
21405   expanded_location xloc;
21406   dw_die_ref imported_die = NULL;
21407   dw_die_ref at_import_die;
21408
21409   if (TREE_CODE (decl) == IMPORTED_DECL)
21410     {
21411       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21412       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21413       gcc_assert (decl);
21414     }
21415   else
21416     xloc = expand_location (input_location);
21417
21418   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21419     {
21420       at_import_die = force_type_die (TREE_TYPE (decl));
21421       /* For namespace N { typedef void T; } using N::T; base_type_die
21422          returns NULL, but DW_TAG_imported_declaration requires
21423          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21424       if (!at_import_die)
21425         {
21426           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21427           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21428           at_import_die = lookup_type_die (TREE_TYPE (decl));
21429           gcc_assert (at_import_die);
21430         }
21431     }
21432   else
21433     {
21434       at_import_die = lookup_decl_die (decl);
21435       if (!at_import_die)
21436         {
21437           /* If we're trying to avoid duplicate debug info, we may not have
21438              emitted the member decl for this field.  Emit it now.  */
21439           if (TREE_CODE (decl) == FIELD_DECL)
21440             {
21441               tree type = DECL_CONTEXT (decl);
21442
21443               if (TYPE_CONTEXT (type)
21444                   && TYPE_P (TYPE_CONTEXT (type))
21445                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21446                                                 DINFO_USAGE_DIR_USE))
21447                 return;
21448               gen_type_die_for_member (type, decl,
21449                                        get_context_die (TYPE_CONTEXT (type)));
21450             }
21451           at_import_die = force_decl_die (decl);
21452         }
21453     }
21454
21455   if (TREE_CODE (decl) == NAMESPACE_DECL)
21456     {
21457       if (dwarf_version >= 3 || !dwarf_strict)
21458         imported_die = new_die (DW_TAG_imported_module,
21459                                 lexical_block_die,
21460                                 lexical_block);
21461       else
21462         return;
21463     }
21464   else
21465     imported_die = new_die (DW_TAG_imported_declaration,
21466                             lexical_block_die,
21467                             lexical_block);
21468
21469   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21470   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21471   if (name)
21472     add_AT_string (imported_die, DW_AT_name,
21473                    IDENTIFIER_POINTER (name));
21474   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21475 }
21476
21477 /* Output debug information for imported module or decl DECL.
21478    NAME is non-NULL name in context if the decl has been renamed.
21479    CHILD is true if decl is one of the renamed decls as part of
21480    importing whole module.  */
21481
21482 static void
21483 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21484                                    bool child)
21485 {
21486   /* dw_die_ref at_import_die;  */
21487   dw_die_ref scope_die;
21488
21489   if (debug_info_level <= DINFO_LEVEL_TERSE)
21490     return;
21491
21492   gcc_assert (decl);
21493
21494   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21495      We need decl DIE for reference and scope die. First, get DIE for the decl
21496      itself.  */
21497
21498   /* Get the scope die for decl context. Use comp_unit_die for global module
21499      or decl. If die is not found for non globals, force new die.  */
21500   if (context
21501       && TYPE_P (context)
21502       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21503     return;
21504
21505   if (!(dwarf_version >= 3 || !dwarf_strict))
21506     return;
21507
21508   scope_die = get_context_die (context);
21509
21510   if (child)
21511     {
21512       gcc_assert (scope_die->die_child);
21513       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21514       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21515       scope_die = scope_die->die_child;
21516     }
21517
21518   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21519   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21520
21521 }
21522
21523 /* Write the debugging output for DECL.  */
21524
21525 void
21526 dwarf2out_decl (tree decl)
21527 {
21528   dw_die_ref context_die = comp_unit_die ();
21529
21530   switch (TREE_CODE (decl))
21531     {
21532     case ERROR_MARK:
21533       return;
21534
21535     case FUNCTION_DECL:
21536       /* What we would really like to do here is to filter out all mere
21537          file-scope declarations of file-scope functions which are never
21538          referenced later within this translation unit (and keep all of ones
21539          that *are* referenced later on) but we aren't clairvoyant, so we have
21540          no idea which functions will be referenced in the future (i.e. later
21541          on within the current translation unit). So here we just ignore all
21542          file-scope function declarations which are not also definitions.  If
21543          and when the debugger needs to know something about these functions,
21544          it will have to hunt around and find the DWARF information associated
21545          with the definition of the function.
21546
21547          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21548          nodes represent definitions and which ones represent mere
21549          declarations.  We have to check DECL_INITIAL instead. That's because
21550          the C front-end supports some weird semantics for "extern inline"
21551          function definitions.  These can get inlined within the current
21552          translation unit (and thus, we need to generate Dwarf info for their
21553          abstract instances so that the Dwarf info for the concrete inlined
21554          instances can have something to refer to) but the compiler never
21555          generates any out-of-lines instances of such things (despite the fact
21556          that they *are* definitions).
21557
21558          The important point is that the C front-end marks these "extern
21559          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21560          them anyway. Note that the C++ front-end also plays some similar games
21561          for inline function definitions appearing within include files which
21562          also contain `#pragma interface' pragmas.  */
21563       if (DECL_INITIAL (decl) == NULL_TREE)
21564         return;
21565
21566       /* If we're a nested function, initially use a parent of NULL; if we're
21567          a plain function, this will be fixed up in decls_for_scope.  If
21568          we're a method, it will be ignored, since we already have a DIE.  */
21569       if (decl_function_context (decl)
21570           /* But if we're in terse mode, we don't care about scope.  */
21571           && debug_info_level > DINFO_LEVEL_TERSE)
21572         context_die = NULL;
21573       break;
21574
21575     case VAR_DECL:
21576       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21577          declaration and if the declaration was never even referenced from
21578          within this entire compilation unit.  We suppress these DIEs in
21579          order to save space in the .debug section (by eliminating entries
21580          which are probably useless).  Note that we must not suppress
21581          block-local extern declarations (whether used or not) because that
21582          would screw-up the debugger's name lookup mechanism and cause it to
21583          miss things which really ought to be in scope at a given point.  */
21584       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21585         return;
21586
21587       /* For local statics lookup proper context die.  */
21588       if (TREE_STATIC (decl) && decl_function_context (decl))
21589         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21590
21591       /* If we are in terse mode, don't generate any DIEs to represent any
21592          variable declarations or definitions.  */
21593       if (debug_info_level <= DINFO_LEVEL_TERSE)
21594         return;
21595       break;
21596
21597     case CONST_DECL:
21598       if (debug_info_level <= DINFO_LEVEL_TERSE)
21599         return;
21600       if (!is_fortran () && !is_ada ())
21601         return;
21602       if (TREE_STATIC (decl) && decl_function_context (decl))
21603         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21604       break;
21605
21606     case NAMESPACE_DECL:
21607     case IMPORTED_DECL:
21608       if (debug_info_level <= DINFO_LEVEL_TERSE)
21609         return;
21610       if (lookup_decl_die (decl) != NULL)
21611         return;
21612       break;
21613
21614     case TYPE_DECL:
21615       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21616       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21617         return;
21618
21619       /* Don't bother trying to generate any DIEs to represent any of the
21620          normal built-in types for the language we are compiling.  */
21621       if (DECL_IS_BUILTIN (decl))
21622         return;
21623
21624       /* If we are in terse mode, don't generate any DIEs for types.  */
21625       if (debug_info_level <= DINFO_LEVEL_TERSE)
21626         return;
21627
21628       /* If we're a function-scope tag, initially use a parent of NULL;
21629          this will be fixed up in decls_for_scope.  */
21630       if (decl_function_context (decl))
21631         context_die = NULL;
21632
21633       break;
21634
21635     default:
21636       return;
21637     }
21638
21639   gen_decl_die (decl, NULL, context_die);
21640 }
21641
21642 /* Write the debugging output for DECL.  */
21643
21644 static void
21645 dwarf2out_function_decl (tree decl)
21646 {
21647   dwarf2out_decl (decl);
21648   call_arg_locations = NULL;
21649   call_arg_loc_last = NULL;
21650   call_site_count = -1;
21651   tail_call_site_count = -1;
21652   VEC_free (dw_die_ref, heap, block_map);
21653   htab_empty (decl_loc_table);
21654   htab_empty (cached_dw_loc_list_table);
21655 }
21656
21657 /* Output a marker (i.e. a label) for the beginning of the generated code for
21658    a lexical block.  */
21659
21660 static void
21661 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21662                        unsigned int blocknum)
21663 {
21664   switch_to_section (current_function_section ());
21665   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21666 }
21667
21668 /* Output a marker (i.e. a label) for the end of the generated code for a
21669    lexical block.  */
21670
21671 static void
21672 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21673 {
21674   switch_to_section (current_function_section ());
21675   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21676 }
21677
21678 /* Returns nonzero if it is appropriate not to emit any debugging
21679    information for BLOCK, because it doesn't contain any instructions.
21680
21681    Don't allow this for blocks with nested functions or local classes
21682    as we would end up with orphans, and in the presence of scheduling
21683    we may end up calling them anyway.  */
21684
21685 static bool
21686 dwarf2out_ignore_block (const_tree block)
21687 {
21688   tree decl;
21689   unsigned int i;
21690
21691   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21692     if (TREE_CODE (decl) == FUNCTION_DECL
21693         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21694       return 0;
21695   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21696     {
21697       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21698       if (TREE_CODE (decl) == FUNCTION_DECL
21699           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21700       return 0;
21701     }
21702
21703   return 1;
21704 }
21705
21706 /* Hash table routines for file_hash.  */
21707
21708 static int
21709 file_table_eq (const void *p1_p, const void *p2_p)
21710 {
21711   const struct dwarf_file_data *const p1 =
21712     (const struct dwarf_file_data *) p1_p;
21713   const char *const p2 = (const char *) p2_p;
21714   return filename_cmp (p1->filename, p2) == 0;
21715 }
21716
21717 static hashval_t
21718 file_table_hash (const void *p_p)
21719 {
21720   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21721   return htab_hash_string (p->filename);
21722 }
21723
21724 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21725    dwarf2out.c) and return its "index".  The index of each (known) filename is
21726    just a unique number which is associated with only that one filename.  We
21727    need such numbers for the sake of generating labels (in the .debug_sfnames
21728    section) and references to those files numbers (in the .debug_srcinfo
21729    and.debug_macinfo sections).  If the filename given as an argument is not
21730    found in our current list, add it to the list and assign it the next
21731    available unique index number.  In order to speed up searches, we remember
21732    the index of the filename was looked up last.  This handles the majority of
21733    all searches.  */
21734
21735 static struct dwarf_file_data *
21736 lookup_filename (const char *file_name)
21737 {
21738   void ** slot;
21739   struct dwarf_file_data * created;
21740
21741   /* Check to see if the file name that was searched on the previous
21742      call matches this file name.  If so, return the index.  */
21743   if (file_table_last_lookup
21744       && (file_name == file_table_last_lookup->filename
21745           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21746     return file_table_last_lookup;
21747
21748   /* Didn't match the previous lookup, search the table.  */
21749   slot = htab_find_slot_with_hash (file_table, file_name,
21750                                    htab_hash_string (file_name), INSERT);
21751   if (*slot)
21752     return (struct dwarf_file_data *) *slot;
21753
21754   created = ggc_alloc_dwarf_file_data ();
21755   created->filename = file_name;
21756   created->emitted_number = 0;
21757   *slot = created;
21758   return created;
21759 }
21760
21761 /* If the assembler will construct the file table, then translate the compiler
21762    internal file table number into the assembler file table number, and emit
21763    a .file directive if we haven't already emitted one yet.  The file table
21764    numbers are different because we prune debug info for unused variables and
21765    types, which may include filenames.  */
21766
21767 static int
21768 maybe_emit_file (struct dwarf_file_data * fd)
21769 {
21770   if (! fd->emitted_number)
21771     {
21772       if (last_emitted_file)
21773         fd->emitted_number = last_emitted_file->emitted_number + 1;
21774       else
21775         fd->emitted_number = 1;
21776       last_emitted_file = fd;
21777
21778       if (DWARF2_ASM_LINE_DEBUG_INFO)
21779         {
21780           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21781           output_quoted_string (asm_out_file,
21782                                 remap_debug_filename (fd->filename));
21783           fputc ('\n', asm_out_file);
21784         }
21785     }
21786
21787   return fd->emitted_number;
21788 }
21789
21790 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21791    That generation should happen after function debug info has been
21792    generated. The value of the attribute is the constant value of ARG.  */
21793
21794 static void
21795 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21796 {
21797   die_arg_entry entry;
21798
21799   if (!die || !arg)
21800     return;
21801
21802   if (!tmpl_value_parm_die_table)
21803     tmpl_value_parm_die_table
21804       = VEC_alloc (die_arg_entry, gc, 32);
21805
21806   entry.die = die;
21807   entry.arg = arg;
21808   VEC_safe_push (die_arg_entry, gc,
21809                  tmpl_value_parm_die_table,
21810                  &entry);
21811 }
21812
21813 /* Return TRUE if T is an instance of generic type, FALSE
21814    otherwise.  */
21815
21816 static bool
21817 generic_type_p (tree t)
21818 {
21819   if (t == NULL_TREE || !TYPE_P (t))
21820     return false;
21821   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21822 }
21823
21824 /* Schedule the generation of the generic parameter dies for the
21825   instance of generic type T. The proper generation itself is later
21826   done by gen_scheduled_generic_parms_dies. */
21827
21828 static void
21829 schedule_generic_params_dies_gen (tree t)
21830 {
21831   if (!generic_type_p (t))
21832     return;
21833
21834   if (generic_type_instances == NULL)
21835     generic_type_instances = VEC_alloc (tree, gc, 256);
21836
21837   VEC_safe_push (tree, gc, generic_type_instances, t);
21838 }
21839
21840 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21841    by append_entry_to_tmpl_value_parm_die_table. This function must
21842    be called after function DIEs have been generated.  */
21843
21844 static void
21845 gen_remaining_tmpl_value_param_die_attribute (void)
21846 {
21847   if (tmpl_value_parm_die_table)
21848     {
21849       unsigned i;
21850       die_arg_entry *e;
21851
21852       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21853         tree_add_const_value_attribute (e->die, e->arg);
21854     }
21855 }
21856
21857 /* Generate generic parameters DIEs for instances of generic types
21858    that have been previously scheduled by
21859    schedule_generic_params_dies_gen. This function must be called
21860    after all the types of the CU have been laid out.  */
21861
21862 static void
21863 gen_scheduled_generic_parms_dies (void)
21864 {
21865   unsigned i;
21866   tree t;
21867
21868   if (generic_type_instances == NULL)
21869     return;
21870   
21871   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21872     gen_generic_params_dies (t);
21873 }
21874
21875
21876 /* Replace DW_AT_name for the decl with name.  */
21877
21878 static void
21879 dwarf2out_set_name (tree decl, tree name)
21880 {
21881   dw_die_ref die;
21882   dw_attr_ref attr;
21883   const char *dname;
21884
21885   die = TYPE_SYMTAB_DIE (decl);
21886   if (!die)
21887     return;
21888
21889   dname = dwarf2_name (name, 0);
21890   if (!dname)
21891     return;
21892
21893   attr = get_AT (die, DW_AT_name);
21894   if (attr)
21895     {
21896       struct indirect_string_node *node;
21897
21898       node = find_AT_string (dname);
21899       /* replace the string.  */
21900       attr->dw_attr_val.v.val_str = node;
21901     }
21902
21903   else
21904     add_name_attribute (die, dname);
21905 }
21906
21907 /* Called by the final INSN scan whenever we see a var location.  We
21908    use it to drop labels in the right places, and throw the location in
21909    our lookup table.  */
21910
21911 static void
21912 dwarf2out_var_location (rtx loc_note)
21913 {
21914   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21915   struct var_loc_node *newloc;
21916   rtx next_real;
21917   static const char *last_label;
21918   static const char *last_postcall_label;
21919   static bool last_in_cold_section_p;
21920   tree decl;
21921   bool var_loc_p;
21922
21923   if (!NOTE_P (loc_note))
21924     {
21925       if (CALL_P (loc_note))
21926         {
21927           call_site_count++;
21928           if (SIBLING_CALL_P (loc_note))
21929             tail_call_site_count++;
21930         }
21931       return;
21932     }
21933
21934   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21935   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21936     return;
21937
21938   next_real = next_real_insn (loc_note);
21939
21940   /* If there are no instructions which would be affected by this note,
21941      don't do anything.  */
21942   if (var_loc_p
21943       && next_real == NULL_RTX
21944       && !NOTE_DURING_CALL_P (loc_note))
21945     return;
21946
21947   if (next_real == NULL_RTX)
21948     next_real = get_last_insn ();
21949
21950   /* If there were any real insns between note we processed last time
21951      and this note (or if it is the first note), clear
21952      last_{,postcall_}label so that they are not reused this time.  */
21953   if (last_var_location_insn == NULL_RTX
21954       || last_var_location_insn != next_real
21955       || last_in_cold_section_p != in_cold_section_p)
21956     {
21957       last_label = NULL;
21958       last_postcall_label = NULL;
21959     }
21960
21961   if (var_loc_p)
21962     {
21963       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21964       newloc = add_var_loc_to_decl (decl, loc_note,
21965                                     NOTE_DURING_CALL_P (loc_note)
21966                                     ? last_postcall_label : last_label);
21967       if (newloc == NULL)
21968         return;
21969     }
21970   else
21971     {
21972       decl = NULL_TREE;
21973       newloc = NULL;
21974     }
21975
21976   /* If there were no real insns between note we processed last time
21977      and this note, use the label we emitted last time.  Otherwise
21978      create a new label and emit it.  */
21979   if (last_label == NULL)
21980     {
21981       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21982       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21983       loclabel_num++;
21984       last_label = ggc_strdup (loclabel);
21985     }
21986
21987   if (!var_loc_p)
21988     {
21989       struct call_arg_loc_node *ca_loc
21990         = ggc_alloc_cleared_call_arg_loc_node ();
21991       rtx prev = prev_real_insn (loc_note), x;
21992       ca_loc->call_arg_loc_note = loc_note;
21993       ca_loc->next = NULL;
21994       ca_loc->label = last_label;
21995       gcc_assert (prev
21996                   && (CALL_P (prev)
21997                       || (NONJUMP_INSN_P (prev)
21998                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21999                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22000       if (!CALL_P (prev))
22001         prev = XVECEXP (PATTERN (prev), 0, 0);
22002       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22003       x = PATTERN (prev);
22004       if (GET_CODE (x) == PARALLEL)
22005         x = XVECEXP (x, 0, 0);
22006       if (GET_CODE (x) == SET)
22007         x = SET_SRC (x);
22008       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22009         {
22010           x = XEXP (XEXP (x, 0), 0);
22011           if (GET_CODE (x) == SYMBOL_REF
22012               && SYMBOL_REF_DECL (x)
22013               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22014             ca_loc->symbol_ref = x;
22015         }
22016       ca_loc->block = insn_scope (prev);
22017       if (call_arg_locations)
22018         call_arg_loc_last->next = ca_loc;
22019       else
22020         call_arg_locations = ca_loc;
22021       call_arg_loc_last = ca_loc;
22022     }
22023   else if (!NOTE_DURING_CALL_P (loc_note))
22024     newloc->label = last_label;
22025   else
22026     {
22027       if (!last_postcall_label)
22028         {
22029           sprintf (loclabel, "%s-1", last_label);
22030           last_postcall_label = ggc_strdup (loclabel);
22031         }
22032       newloc->label = last_postcall_label;
22033     }
22034
22035   last_var_location_insn = next_real;
22036   last_in_cold_section_p = in_cold_section_p;
22037 }
22038
22039 /* Note in one location list that text section has changed.  */
22040
22041 static int
22042 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
22043 {
22044   var_loc_list *list = (var_loc_list *) *slot;
22045   if (list->first)
22046     list->last_before_switch
22047       = list->last->next ? list->last->next : list->last;
22048   return 1;
22049 }
22050
22051 /* Note in all location lists that text section has changed.  */
22052
22053 static void
22054 var_location_switch_text_section (void)
22055 {
22056   if (decl_loc_table == NULL)
22057     return;
22058
22059   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22060 }
22061
22062 /* Create a new line number table.  */
22063
22064 static dw_line_info_table *
22065 new_line_info_table (void)
22066 {
22067   dw_line_info_table *table;
22068
22069   table = ggc_alloc_cleared_dw_line_info_table_struct ();
22070   table->file_num = 1;
22071   table->line_num = 1;
22072   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22073
22074   return table;
22075 }
22076
22077 /* Lookup the "current" table into which we emit line info, so
22078    that we don't have to do it for every source line.  */
22079
22080 static void
22081 set_cur_line_info_table (section *sec)
22082 {
22083   dw_line_info_table *table;
22084
22085   if (sec == text_section)
22086     table = text_section_line_info;
22087   else if (sec == cold_text_section)
22088     {
22089       table = cold_text_section_line_info;
22090       if (!table)
22091         {
22092           cold_text_section_line_info = table = new_line_info_table ();
22093           table->end_label = cold_end_label;
22094         }
22095     }
22096   else
22097     {
22098       const char *end_label;
22099
22100       if (flag_reorder_blocks_and_partition)
22101         {
22102           if (in_cold_section_p)
22103             end_label = crtl->subsections.cold_section_end_label;
22104           else
22105             end_label = crtl->subsections.hot_section_end_label;
22106         }
22107       else
22108         {
22109           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22110           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22111                                        current_function_funcdef_no);
22112           end_label = ggc_strdup (label);
22113         }
22114
22115       table = new_line_info_table ();
22116       table->end_label = end_label;
22117
22118       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
22119     }
22120
22121   cur_line_info_table = table;
22122 }
22123
22124
22125 /* We need to reset the locations at the beginning of each
22126    function. We can't do this in the end_function hook, because the
22127    declarations that use the locations won't have been output when
22128    that hook is called.  Also compute have_multiple_function_sections here.  */
22129
22130 static void
22131 dwarf2out_begin_function (tree fun)
22132 {
22133   section *sec = function_section (fun);
22134
22135   if (sec != text_section)
22136     have_multiple_function_sections = true;
22137
22138   if (flag_reorder_blocks_and_partition && !cold_text_section)
22139     {
22140       gcc_assert (current_function_decl == fun);
22141       cold_text_section = unlikely_text_section ();
22142       switch_to_section (cold_text_section);
22143       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22144       switch_to_section (sec);
22145     }
22146
22147   dwarf2out_note_section_used ();
22148   call_site_count = 0;
22149   tail_call_site_count = 0;
22150
22151   set_cur_line_info_table (sec);
22152 }
22153
22154 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22155
22156 static void
22157 push_dw_line_info_entry (dw_line_info_table *table,
22158                          enum dw_line_info_opcode opcode, unsigned int val)
22159 {
22160   dw_line_info_entry e;
22161   e.opcode = opcode;
22162   e.val = val;
22163   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
22164 }
22165
22166 /* Output a label to mark the beginning of a source code line entry
22167    and record information relating to this source line, in
22168    'line_info_table' for later output of the .debug_line section.  */
22169 /* ??? The discriminator parameter ought to be unsigned.  */
22170
22171 static void
22172 dwarf2out_source_line (unsigned int line, const char *filename,
22173                        int discriminator, bool is_stmt)
22174 {
22175   unsigned int file_num;
22176   dw_line_info_table *table;
22177
22178   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
22179     return;
22180
22181   /* The discriminator column was added in dwarf4.  Simplify the below
22182      by simply removing it if we're not supposed to output it.  */
22183   if (dwarf_version < 4 && dwarf_strict)
22184     discriminator = 0;
22185
22186   table = cur_line_info_table;
22187   file_num = maybe_emit_file (lookup_filename (filename));
22188
22189   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22190      the debugger has used the second (possibly duplicate) line number
22191      at the beginning of the function to mark the end of the prologue.
22192      We could eliminate any other duplicates within the function.  For
22193      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22194      that second line number entry.  */
22195   /* Recall that this end-of-prologue indication is *not* the same thing
22196      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22197      to which the hook corresponds, follows the last insn that was 
22198      emitted by gen_prologue.  What we need is to preceed the first insn
22199      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22200      insn that corresponds to something the user wrote.  These may be
22201      very different locations once scheduling is enabled.  */
22202
22203   if (0 && file_num == table->file_num
22204       && line == table->line_num
22205       && discriminator == table->discrim_num
22206       && is_stmt == table->is_stmt)
22207     return;
22208
22209   switch_to_section (current_function_section ());
22210
22211   /* If requested, emit something human-readable.  */
22212   if (flag_debug_asm)
22213     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22214
22215   if (DWARF2_ASM_LINE_DEBUG_INFO)
22216     {
22217       /* Emit the .loc directive understood by GNU as.  */
22218       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22219       if (is_stmt != table->is_stmt)
22220         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22221       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22222         fprintf (asm_out_file, " discriminator %d", discriminator);
22223       fputc ('\n', asm_out_file);
22224     }
22225   else
22226     {
22227       unsigned int label_num = ++line_info_label_num;
22228
22229       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22230
22231       push_dw_line_info_entry (table, LI_set_address, label_num);
22232       if (file_num != table->file_num)
22233         push_dw_line_info_entry (table, LI_set_file, file_num);
22234       if (discriminator != table->discrim_num)
22235         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22236       if (is_stmt != table->is_stmt)
22237         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22238       push_dw_line_info_entry (table, LI_set_line, line);
22239     }
22240
22241   table->file_num = file_num;
22242   table->line_num = line;
22243   table->discrim_num = discriminator;
22244   table->is_stmt = is_stmt;
22245   table->in_use = true;
22246 }
22247
22248 /* Record the beginning of a new source file.  */
22249
22250 static void
22251 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22252 {
22253   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22254     {
22255       /* Record the beginning of the file for break_out_includes.  */
22256       dw_die_ref bincl_die;
22257
22258       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22259       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22260     }
22261
22262   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22263     {
22264       macinfo_entry e;
22265       e.code = DW_MACINFO_start_file;
22266       e.lineno = lineno;
22267       e.info = xstrdup (filename);
22268       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22269     }
22270 }
22271
22272 /* Record the end of a source file.  */
22273
22274 static void
22275 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22276 {
22277   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22278     /* Record the end of the file for break_out_includes.  */
22279     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22280
22281   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22282     {
22283       macinfo_entry e;
22284       e.code = DW_MACINFO_end_file;
22285       e.lineno = lineno;
22286       e.info = NULL;
22287       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22288     }
22289 }
22290
22291 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22292    the tail part of the directive line, i.e. the part which is past the
22293    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22294
22295 static void
22296 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22297                   const char *buffer ATTRIBUTE_UNUSED)
22298 {
22299   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22300     {
22301       macinfo_entry e;
22302       e.code = DW_MACINFO_define;
22303       e.lineno = lineno;
22304       e.info = xstrdup (buffer);;
22305       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22306     }
22307 }
22308
22309 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22310    the tail part of the directive line, i.e. the part which is past the
22311    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22312
22313 static void
22314 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22315                  const char *buffer ATTRIBUTE_UNUSED)
22316 {
22317   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22318     {
22319       macinfo_entry e;
22320       e.code = DW_MACINFO_undef;
22321       e.lineno = lineno;
22322       e.info = xstrdup (buffer);;
22323       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22324     }
22325 }
22326
22327 static void
22328 output_macinfo (void)
22329 {
22330   unsigned i;
22331   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22332   macinfo_entry *ref;
22333
22334   if (! length)
22335     return;
22336
22337   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22338     {
22339       switch (ref->code)
22340         {
22341           case DW_MACINFO_start_file:
22342             {
22343               int file_num = maybe_emit_file (lookup_filename (ref->info));
22344               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22345               dw2_asm_output_data_uleb128 
22346                         (ref->lineno, "Included from line number %lu", 
22347                                                 (unsigned long)ref->lineno);
22348               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22349             }
22350             break;
22351           case DW_MACINFO_end_file:
22352             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22353             break;
22354           case DW_MACINFO_define:
22355             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22356             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22357                                                 (unsigned long)ref->lineno);
22358             dw2_asm_output_nstring (ref->info, -1, "The macro");
22359             break;
22360           case DW_MACINFO_undef:
22361             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22362             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22363                                                 (unsigned long)ref->lineno);
22364             dw2_asm_output_nstring (ref->info, -1, "The macro");
22365             break;
22366           default:
22367            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22368              ASM_COMMENT_START, (unsigned long)ref->code);
22369           break;
22370         }
22371     }
22372 }
22373
22374 /* Set up for Dwarf output at the start of compilation.  */
22375
22376 static void
22377 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22378 {
22379   /* Allocate the file_table.  */
22380   file_table = htab_create_ggc (50, file_table_hash,
22381                                 file_table_eq, NULL);
22382
22383   /* Allocate the decl_die_table.  */
22384   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22385                                     decl_die_table_eq, NULL);
22386
22387   /* Allocate the decl_loc_table.  */
22388   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22389                                     decl_loc_table_eq, NULL);
22390
22391   /* Allocate the cached_dw_loc_list_table.  */
22392   cached_dw_loc_list_table
22393     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22394                        cached_dw_loc_list_table_eq, NULL);
22395
22396   /* Allocate the initial hunk of the decl_scope_table.  */
22397   decl_scope_table = VEC_alloc (tree, gc, 256);
22398
22399   /* Allocate the initial hunk of the abbrev_die_table.  */
22400   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22401     (ABBREV_DIE_TABLE_INCREMENT);
22402   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22403   /* Zero-th entry is allocated, but unused.  */
22404   abbrev_die_table_in_use = 1;
22405
22406   /* Allocate the pubtypes and pubnames vectors.  */
22407   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22408   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22409
22410   incomplete_types = VEC_alloc (tree, gc, 64);
22411
22412   used_rtx_array = VEC_alloc (rtx, gc, 32);
22413
22414   debug_info_section = get_section (DEBUG_INFO_SECTION,
22415                                     SECTION_DEBUG, NULL);
22416   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22417                                       SECTION_DEBUG, NULL);
22418   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22419                                        SECTION_DEBUG, NULL);
22420   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22421                                        SECTION_DEBUG, NULL);
22422   debug_line_section = get_section (DEBUG_LINE_SECTION,
22423                                     SECTION_DEBUG, NULL);
22424   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22425                                    SECTION_DEBUG, NULL);
22426   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22427                                         SECTION_DEBUG, NULL);
22428   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22429                                         SECTION_DEBUG, NULL);
22430   debug_str_section = get_section (DEBUG_STR_SECTION,
22431                                    DEBUG_STR_SECTION_FLAGS, NULL);
22432   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22433                                       SECTION_DEBUG, NULL);
22434   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22435                                      SECTION_DEBUG, NULL);
22436
22437   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22438   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22439                                DEBUG_ABBREV_SECTION_LABEL, 0);
22440   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22441   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22442                                COLD_TEXT_SECTION_LABEL, 0);
22443   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22444
22445   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22446                                DEBUG_INFO_SECTION_LABEL, 0);
22447   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22448                                DEBUG_LINE_SECTION_LABEL, 0);
22449   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22450                                DEBUG_RANGES_SECTION_LABEL, 0);
22451   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22452                                DEBUG_MACINFO_SECTION_LABEL, 0);
22453
22454   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22455     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22456
22457   switch_to_section (text_section);
22458   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22459
22460   /* Make sure the line number table for .text always exists.  */
22461   text_section_line_info = new_line_info_table ();
22462   text_section_line_info->end_label = text_end_label;
22463 }
22464
22465 /* Called before cgraph_optimize starts outputtting functions, variables
22466    and toplevel asms into assembly.  */
22467
22468 static void
22469 dwarf2out_assembly_start (void)
22470 {
22471   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22472       && dwarf2out_do_cfi_asm ()
22473       && (!(flag_unwind_tables || flag_exceptions)
22474           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22475     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22476 }
22477
22478 /* A helper function for dwarf2out_finish called through
22479    htab_traverse.  Emit one queued .debug_str string.  */
22480
22481 static int
22482 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22483 {
22484   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22485
22486   if (node->label && node->refcount)
22487     {
22488       switch_to_section (debug_str_section);
22489       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22490       assemble_string (node->str, strlen (node->str) + 1);
22491     }
22492
22493   return 1;
22494 }
22495
22496 #if ENABLE_ASSERT_CHECKING
22497 /* Verify that all marks are clear.  */
22498
22499 static void
22500 verify_marks_clear (dw_die_ref die)
22501 {
22502   dw_die_ref c;
22503
22504   gcc_assert (! die->die_mark);
22505   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22506 }
22507 #endif /* ENABLE_ASSERT_CHECKING */
22508
22509 /* Clear the marks for a die and its children.
22510    Be cool if the mark isn't set.  */
22511
22512 static void
22513 prune_unmark_dies (dw_die_ref die)
22514 {
22515   dw_die_ref c;
22516
22517   if (die->die_mark)
22518     die->die_mark = 0;
22519   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22520 }
22521
22522 /* Given DIE that we're marking as used, find any other dies
22523    it references as attributes and mark them as used.  */
22524
22525 static void
22526 prune_unused_types_walk_attribs (dw_die_ref die)
22527 {
22528   dw_attr_ref a;
22529   unsigned ix;
22530
22531   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22532     {
22533       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22534         {
22535           /* A reference to another DIE.
22536              Make sure that it will get emitted.
22537              If it was broken out into a comdat group, don't follow it.  */
22538           if (! use_debug_types
22539               || a->dw_attr == DW_AT_specification
22540               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22541             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22542         }
22543       /* Set the string's refcount to 0 so that prune_unused_types_mark
22544          accounts properly for it.  */
22545       if (AT_class (a) == dw_val_class_str)
22546         a->dw_attr_val.v.val_str->refcount = 0;
22547     }
22548 }
22549
22550 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22551
22552 static void
22553 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22554 {
22555   dw_die_ref c;
22556
22557   if (die == NULL || die->die_child == NULL)
22558     return;
22559   c = die->die_child;
22560   do
22561     {
22562       switch (c->die_tag)
22563         {
22564         case DW_TAG_template_type_param:
22565         case DW_TAG_template_value_param:
22566         case DW_TAG_GNU_template_template_param:
22567         case DW_TAG_GNU_template_parameter_pack:
22568           prune_unused_types_mark (c, 1);
22569           break;
22570         default:
22571           break;
22572         }
22573       c = c->die_sib;
22574     } while (c && c != die->die_child);
22575 }
22576
22577 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22578    to DIE's children.  */
22579
22580 static void
22581 prune_unused_types_mark (dw_die_ref die, int dokids)
22582 {
22583   dw_die_ref c;
22584
22585   if (die->die_mark == 0)
22586     {
22587       /* We haven't done this node yet.  Mark it as used.  */
22588       die->die_mark = 1;
22589       /* If this is the DIE of a generic type instantiation,
22590          mark the children DIEs that describe its generic parms and
22591          args.  */
22592       prune_unused_types_mark_generic_parms_dies (die);
22593
22594       /* We also have to mark its parents as used.
22595          (But we don't want to mark our parents' kids due to this.)  */
22596       if (die->die_parent)
22597         prune_unused_types_mark (die->die_parent, 0);
22598
22599       /* Mark any referenced nodes.  */
22600       prune_unused_types_walk_attribs (die);
22601
22602       /* If this node is a specification,
22603          also mark the definition, if it exists.  */
22604       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22605         prune_unused_types_mark (die->die_definition, 1);
22606     }
22607
22608   if (dokids && die->die_mark != 2)
22609     {
22610       /* We need to walk the children, but haven't done so yet.
22611          Remember that we've walked the kids.  */
22612       die->die_mark = 2;
22613
22614       /* If this is an array type, we need to make sure our
22615          kids get marked, even if they're types.  If we're
22616          breaking out types into comdat sections, do this
22617          for all type definitions.  */
22618       if (die->die_tag == DW_TAG_array_type
22619           || (use_debug_types
22620               && is_type_die (die) && ! is_declaration_die (die)))
22621         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22622       else
22623         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22624     }
22625 }
22626
22627 /* For local classes, look if any static member functions were emitted
22628    and if so, mark them.  */
22629
22630 static void
22631 prune_unused_types_walk_local_classes (dw_die_ref die)
22632 {
22633   dw_die_ref c;
22634
22635   if (die->die_mark == 2)
22636     return;
22637
22638   switch (die->die_tag)
22639     {
22640     case DW_TAG_structure_type:
22641     case DW_TAG_union_type:
22642     case DW_TAG_class_type:
22643       break;
22644
22645     case DW_TAG_subprogram:
22646       if (!get_AT_flag (die, DW_AT_declaration)
22647           || die->die_definition != NULL)
22648         prune_unused_types_mark (die, 1);
22649       return;
22650
22651     default:
22652       return;
22653     }
22654
22655   /* Mark children.  */
22656   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22657 }
22658
22659 /* Walk the tree DIE and mark types that we actually use.  */
22660
22661 static void
22662 prune_unused_types_walk (dw_die_ref die)
22663 {
22664   dw_die_ref c;
22665
22666   /* Don't do anything if this node is already marked and
22667      children have been marked as well.  */
22668   if (die->die_mark == 2)
22669     return;
22670
22671   switch (die->die_tag)
22672     {
22673     case DW_TAG_structure_type:
22674     case DW_TAG_union_type:
22675     case DW_TAG_class_type:
22676       if (die->die_perennial_p)
22677         break;
22678
22679       for (c = die->die_parent; c; c = c->die_parent)
22680         if (c->die_tag == DW_TAG_subprogram)
22681           break;
22682
22683       /* Finding used static member functions inside of classes
22684          is needed just for local classes, because for other classes
22685          static member function DIEs with DW_AT_specification
22686          are emitted outside of the DW_TAG_*_type.  If we ever change
22687          it, we'd need to call this even for non-local classes.  */
22688       if (c)
22689         prune_unused_types_walk_local_classes (die);
22690
22691       /* It's a type node --- don't mark it.  */
22692       return;
22693
22694     case DW_TAG_const_type:
22695     case DW_TAG_packed_type:
22696     case DW_TAG_pointer_type:
22697     case DW_TAG_reference_type:
22698     case DW_TAG_rvalue_reference_type:
22699     case DW_TAG_volatile_type:
22700     case DW_TAG_typedef:
22701     case DW_TAG_array_type:
22702     case DW_TAG_interface_type:
22703     case DW_TAG_friend:
22704     case DW_TAG_variant_part:
22705     case DW_TAG_enumeration_type:
22706     case DW_TAG_subroutine_type:
22707     case DW_TAG_string_type:
22708     case DW_TAG_set_type:
22709     case DW_TAG_subrange_type:
22710     case DW_TAG_ptr_to_member_type:
22711     case DW_TAG_file_type:
22712       if (die->die_perennial_p)
22713         break;
22714
22715       /* It's a type node --- don't mark it.  */
22716       return;
22717
22718     default:
22719       /* Mark everything else.  */
22720       break;
22721   }
22722
22723   if (die->die_mark == 0)
22724     {
22725       die->die_mark = 1;
22726
22727       /* Now, mark any dies referenced from here.  */
22728       prune_unused_types_walk_attribs (die);
22729     }
22730
22731   die->die_mark = 2;
22732
22733   /* Mark children.  */
22734   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22735 }
22736
22737 /* Increment the string counts on strings referred to from DIE's
22738    attributes.  */
22739
22740 static void
22741 prune_unused_types_update_strings (dw_die_ref die)
22742 {
22743   dw_attr_ref a;
22744   unsigned ix;
22745
22746   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22747     if (AT_class (a) == dw_val_class_str)
22748       {
22749         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22750         s->refcount++;
22751         /* Avoid unnecessarily putting strings that are used less than
22752            twice in the hash table.  */
22753         if (s->refcount
22754             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22755           {
22756             void ** slot;
22757             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22758                                              htab_hash_string (s->str),
22759                                              INSERT);
22760             gcc_assert (*slot == NULL);
22761             *slot = s;
22762           }
22763       }
22764 }
22765
22766 /* Remove from the tree DIE any dies that aren't marked.  */
22767
22768 static void
22769 prune_unused_types_prune (dw_die_ref die)
22770 {
22771   dw_die_ref c;
22772
22773   gcc_assert (die->die_mark);
22774   prune_unused_types_update_strings (die);
22775
22776   if (! die->die_child)
22777     return;
22778
22779   c = die->die_child;
22780   do {
22781     dw_die_ref prev = c;
22782     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22783       if (c == die->die_child)
22784         {
22785           /* No marked children between 'prev' and the end of the list.  */
22786           if (prev == c)
22787             /* No marked children at all.  */
22788             die->die_child = NULL;
22789           else
22790             {
22791               prev->die_sib = c->die_sib;
22792               die->die_child = prev;
22793             }
22794           return;
22795         }
22796
22797     if (c != prev->die_sib)
22798       prev->die_sib = c;
22799     prune_unused_types_prune (c);
22800   } while (c != die->die_child);
22801 }
22802
22803 /* A helper function for dwarf2out_finish called through
22804    htab_traverse.  Clear .debug_str strings that we haven't already
22805    decided to emit.  */
22806
22807 static int
22808 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22809 {
22810   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22811
22812   if (!node->label || !node->refcount)
22813     htab_clear_slot (debug_str_hash, h);
22814
22815   return 1;
22816 }
22817
22818 /* Remove dies representing declarations that we never use.  */
22819
22820 static void
22821 prune_unused_types (void)
22822 {
22823   unsigned int i;
22824   limbo_die_node *node;
22825   comdat_type_node *ctnode;
22826   pubname_ref pub;
22827
22828 #if ENABLE_ASSERT_CHECKING
22829   /* All the marks should already be clear.  */
22830   verify_marks_clear (comp_unit_die ());
22831   for (node = limbo_die_list; node; node = node->next)
22832     verify_marks_clear (node->die);
22833   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22834     verify_marks_clear (ctnode->root_die);
22835 #endif /* ENABLE_ASSERT_CHECKING */
22836
22837   /* Mark types that are used in global variables.  */
22838   premark_types_used_by_global_vars ();
22839
22840   /* Set the mark on nodes that are actually used.  */
22841   prune_unused_types_walk (comp_unit_die ());
22842   for (node = limbo_die_list; node; node = node->next)
22843     prune_unused_types_walk (node->die);
22844   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22845     {
22846       prune_unused_types_walk (ctnode->root_die);
22847       prune_unused_types_mark (ctnode->type_die, 1);
22848     }
22849
22850   /* Also set the mark on nodes referenced from the
22851      pubname_table.  */
22852   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22853     prune_unused_types_mark (pub->die, 1);
22854
22855   /* Get rid of nodes that aren't marked; and update the string counts.  */
22856   if (debug_str_hash && debug_str_hash_forced)
22857     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22858   else if (debug_str_hash)
22859     htab_empty (debug_str_hash);
22860   prune_unused_types_prune (comp_unit_die ());
22861   for (node = limbo_die_list; node; node = node->next)
22862     prune_unused_types_prune (node->die);
22863   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22864     prune_unused_types_prune (ctnode->root_die);
22865
22866   /* Leave the marks clear.  */
22867   prune_unmark_dies (comp_unit_die ());
22868   for (node = limbo_die_list; node; node = node->next)
22869     prune_unmark_dies (node->die);
22870   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22871     prune_unmark_dies (ctnode->root_die);
22872 }
22873
22874 /* Set the parameter to true if there are any relative pathnames in
22875    the file table.  */
22876 static int
22877 file_table_relative_p (void ** slot, void *param)
22878 {
22879   bool *p = (bool *) param;
22880   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22881   if (!IS_ABSOLUTE_PATH (d->filename))
22882     {
22883       *p = true;
22884       return 0;
22885     }
22886   return 1;
22887 }
22888
22889 /* Routines to manipulate hash table of comdat type units.  */
22890
22891 static hashval_t
22892 htab_ct_hash (const void *of)
22893 {
22894   hashval_t h;
22895   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22896
22897   memcpy (&h, type_node->signature, sizeof (h));
22898   return h;
22899 }
22900
22901 static int
22902 htab_ct_eq (const void *of1, const void *of2)
22903 {
22904   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22905   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22906
22907   return (! memcmp (type_node_1->signature, type_node_2->signature,
22908                     DWARF_TYPE_SIGNATURE_SIZE));
22909 }
22910
22911 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22912    to the location it would have been added, should we know its
22913    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22914    probably improve compactness of debug info, removing equivalent
22915    abbrevs, and hide any differences caused by deferring the
22916    computation of the assembler name, triggered by e.g. PCH.  */
22917
22918 static inline void
22919 move_linkage_attr (dw_die_ref die)
22920 {
22921   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22922   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22923
22924   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22925               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22926
22927   while (--ix > 0)
22928     {
22929       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22930
22931       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22932         break;
22933     }
22934
22935   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22936     {
22937       VEC_pop (dw_attr_node, die->die_attr);
22938       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22939     }
22940 }
22941
22942 /* Helper function for resolve_addr, attempt to resolve
22943    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22944    SYMBOL_REFs refer to variables emitted in the current CU.  */
22945
22946 static int
22947 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22948 {
22949   rtx rtl = *addr;
22950
22951   if (GET_CODE (rtl) == CONST_STRING)
22952     {
22953       size_t len = strlen (XSTR (rtl, 0)) + 1;
22954       tree t = build_string (len, XSTR (rtl, 0));
22955       tree tlen = build_int_cst (NULL_TREE, len - 1);
22956       TREE_TYPE (t)
22957         = build_array_type (char_type_node, build_index_type (tlen));
22958       rtl = lookup_constant_def (t);
22959       if (!rtl || !MEM_P (rtl))
22960         return 1;
22961       rtl = XEXP (rtl, 0);
22962       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22963       *addr = rtl;
22964       return 0;
22965     }
22966
22967   if (GET_CODE (rtl) == SYMBOL_REF
22968       && SYMBOL_REF_DECL (rtl))
22969     {
22970       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22971         {
22972           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22973             return 1;
22974         }
22975       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22976         return 1;
22977     }
22978
22979   if (GET_CODE (rtl) == CONST
22980       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22981     return 1;
22982
22983   return 0;
22984 }
22985
22986 /* Helper function for resolve_addr, handle one location
22987    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22988    the location list couldn't be resolved.  */
22989
22990 static bool
22991 resolve_addr_in_expr (dw_loc_descr_ref loc)
22992 {
22993   for (; loc; loc = loc->dw_loc_next)
22994     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22995          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22996         || (loc->dw_loc_opc == DW_OP_implicit_value
22997             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22998             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22999       return false;
23000     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
23001              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23002       {
23003         dw_die_ref ref
23004           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23005         if (ref == NULL)
23006           return false;
23007         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23008         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23009         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23010       }
23011   return true;
23012 }
23013
23014 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23015    an address in .rodata section if the string literal is emitted there,
23016    or remove the containing location list or replace DW_AT_const_value
23017    with DW_AT_location and empty location expression, if it isn't found
23018    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23019    to something that has been emitted in the current CU.  */
23020
23021 static void
23022 resolve_addr (dw_die_ref die)
23023 {
23024   dw_die_ref c;
23025   dw_attr_ref a;
23026   dw_loc_list_ref *curr, *start, loc;
23027   unsigned ix;
23028
23029   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23030     switch (AT_class (a))
23031       {
23032       case dw_val_class_loc_list:
23033         start = curr = AT_loc_list_ptr (a);
23034         loc = *curr;
23035         gcc_assert (loc);
23036         /* The same list can be referenced more than once.  See if we have
23037            already recorded the result from a previous pass.  */
23038         if (loc->replaced)
23039           *curr = loc->dw_loc_next;
23040         else if (!loc->resolved_addr)
23041           {
23042             /* As things stand, we do not expect or allow one die to
23043                reference a suffix of another die's location list chain.
23044                References must be identical or completely separate.
23045                There is therefore no need to cache the result of this
23046                pass on any list other than the first; doing so
23047                would lead to unnecessary writes.  */
23048             while (*curr)
23049               {
23050                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23051                 if (!resolve_addr_in_expr ((*curr)->expr))
23052                   {
23053                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23054                     if (next && (*curr)->ll_symbol)
23055                       {
23056                         gcc_assert (!next->ll_symbol);
23057                         next->ll_symbol = (*curr)->ll_symbol;
23058                       }
23059                     *curr = next;
23060                   }
23061                 else
23062                   curr = &(*curr)->dw_loc_next;
23063               }
23064             if (loc == *start)
23065               loc->resolved_addr = 1;
23066             else
23067               {
23068                 loc->replaced = 1;
23069                 loc->dw_loc_next = *start;
23070               }
23071           }
23072         if (!*start)
23073           {
23074             remove_AT (die, a->dw_attr);
23075             ix--;
23076           }
23077         break;
23078       case dw_val_class_loc:
23079         if (!resolve_addr_in_expr (AT_loc (a)))
23080           {
23081             remove_AT (die, a->dw_attr);
23082             ix--;
23083           }
23084         break;
23085       case dw_val_class_addr:
23086         if (a->dw_attr == DW_AT_const_value
23087             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23088           {
23089             remove_AT (die, a->dw_attr);
23090             ix--;
23091           }
23092         if (die->die_tag == DW_TAG_GNU_call_site
23093             && a->dw_attr == DW_AT_abstract_origin)
23094           {
23095             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23096             dw_die_ref tdie = lookup_decl_die (tdecl);
23097             if (tdie == NULL
23098                 && DECL_EXTERNAL (tdecl)
23099                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23100               {
23101                 force_decl_die (tdecl);
23102                 tdie = lookup_decl_die (tdecl);
23103               }
23104             if (tdie)
23105               {
23106                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23107                 a->dw_attr_val.v.val_die_ref.die = tdie;
23108                 a->dw_attr_val.v.val_die_ref.external = 0;
23109               }
23110             else
23111               {
23112                 remove_AT (die, a->dw_attr);
23113                 ix--;
23114               }
23115           }
23116         break;
23117       default:
23118         break;
23119       }
23120
23121   FOR_EACH_CHILD (die, c, resolve_addr (c));
23122 }
23123 \f
23124 /* Helper routines for optimize_location_lists.
23125    This pass tries to share identical local lists in .debug_loc
23126    section.  */
23127
23128 /* Iteratively hash operands of LOC opcode.  */
23129
23130 static inline hashval_t
23131 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23132 {
23133   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23134   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23135
23136   switch (loc->dw_loc_opc)
23137     {
23138     case DW_OP_const4u:
23139     case DW_OP_const8u:
23140       if (loc->dtprel)
23141         goto hash_addr;
23142       /* FALLTHRU */
23143     case DW_OP_const1u:
23144     case DW_OP_const1s:
23145     case DW_OP_const2u:
23146     case DW_OP_const2s:
23147     case DW_OP_const4s:
23148     case DW_OP_const8s:
23149     case DW_OP_constu:
23150     case DW_OP_consts:
23151     case DW_OP_pick:
23152     case DW_OP_plus_uconst:
23153     case DW_OP_breg0:
23154     case DW_OP_breg1:
23155     case DW_OP_breg2:
23156     case DW_OP_breg3:
23157     case DW_OP_breg4:
23158     case DW_OP_breg5:
23159     case DW_OP_breg6:
23160     case DW_OP_breg7:
23161     case DW_OP_breg8:
23162     case DW_OP_breg9:
23163     case DW_OP_breg10:
23164     case DW_OP_breg11:
23165     case DW_OP_breg12:
23166     case DW_OP_breg13:
23167     case DW_OP_breg14:
23168     case DW_OP_breg15:
23169     case DW_OP_breg16:
23170     case DW_OP_breg17:
23171     case DW_OP_breg18:
23172     case DW_OP_breg19:
23173     case DW_OP_breg20:
23174     case DW_OP_breg21:
23175     case DW_OP_breg22:
23176     case DW_OP_breg23:
23177     case DW_OP_breg24:
23178     case DW_OP_breg25:
23179     case DW_OP_breg26:
23180     case DW_OP_breg27:
23181     case DW_OP_breg28:
23182     case DW_OP_breg29:
23183     case DW_OP_breg30:
23184     case DW_OP_breg31:
23185     case DW_OP_regx:
23186     case DW_OP_fbreg:
23187     case DW_OP_piece:
23188     case DW_OP_deref_size:
23189     case DW_OP_xderef_size:
23190       hash = iterative_hash_object (val1->v.val_int, hash);
23191       break;
23192     case DW_OP_skip:
23193     case DW_OP_bra:
23194       {
23195         int offset;
23196
23197         gcc_assert (val1->val_class == dw_val_class_loc);
23198         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23199         hash = iterative_hash_object (offset, hash);
23200       }
23201       break;
23202     case DW_OP_implicit_value:
23203       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23204       switch (val2->val_class)
23205         {
23206         case dw_val_class_const:
23207           hash = iterative_hash_object (val2->v.val_int, hash);
23208           break;
23209         case dw_val_class_vec:
23210           {
23211             unsigned int elt_size = val2->v.val_vec.elt_size;
23212             unsigned int len = val2->v.val_vec.length;
23213
23214             hash = iterative_hash_object (elt_size, hash);
23215             hash = iterative_hash_object (len, hash);
23216             hash = iterative_hash (val2->v.val_vec.array,
23217                                    len * elt_size, hash);
23218           }
23219           break;
23220         case dw_val_class_const_double:
23221           hash = iterative_hash_object (val2->v.val_double.low, hash);
23222           hash = iterative_hash_object (val2->v.val_double.high, hash);
23223           break;
23224         case dw_val_class_addr:
23225           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23226           break;
23227         default:
23228           gcc_unreachable ();
23229         }
23230       break;
23231     case DW_OP_bregx:
23232     case DW_OP_bit_piece:
23233       hash = iterative_hash_object (val1->v.val_int, hash);
23234       hash = iterative_hash_object (val2->v.val_int, hash);
23235       break;
23236     case DW_OP_addr:
23237     hash_addr:
23238       if (loc->dtprel)
23239         {
23240           unsigned char dtprel = 0xd1;
23241           hash = iterative_hash_object (dtprel, hash);
23242         }
23243       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23244       break;
23245     case DW_OP_GNU_implicit_pointer:
23246       hash = iterative_hash_object (val2->v.val_int, hash);
23247       break;
23248     case DW_OP_GNU_entry_value:
23249       hash = hash_loc_operands (val1->v.val_loc, hash);
23250       break;
23251
23252     default:
23253       /* Other codes have no operands.  */
23254       break;
23255     }
23256   return hash;
23257 }
23258
23259 /* Iteratively hash the whole DWARF location expression LOC.  */
23260
23261 static inline hashval_t
23262 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23263 {
23264   dw_loc_descr_ref l;
23265   bool sizes_computed = false;
23266   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23267   size_of_locs (loc);
23268
23269   for (l = loc; l != NULL; l = l->dw_loc_next)
23270     {
23271       enum dwarf_location_atom opc = l->dw_loc_opc;
23272       hash = iterative_hash_object (opc, hash);
23273       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23274         {
23275           size_of_locs (loc);
23276           sizes_computed = true;
23277         }
23278       hash = hash_loc_operands (l, hash);
23279     }
23280   return hash;
23281 }
23282
23283 /* Compute hash of the whole location list LIST_HEAD.  */
23284
23285 static inline void
23286 hash_loc_list (dw_loc_list_ref list_head)
23287 {
23288   dw_loc_list_ref curr = list_head;
23289   hashval_t hash = 0;
23290
23291   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23292     {
23293       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23294       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23295       if (curr->section)
23296         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23297                                hash);
23298       hash = hash_locs (curr->expr, hash);
23299     }
23300   list_head->hash = hash;
23301 }
23302
23303 /* Return true if X and Y opcodes have the same operands.  */
23304
23305 static inline bool
23306 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23307 {
23308   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23309   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23310   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23311   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23312
23313   switch (x->dw_loc_opc)
23314     {
23315     case DW_OP_const4u:
23316     case DW_OP_const8u:
23317       if (x->dtprel)
23318         goto hash_addr;
23319       /* FALLTHRU */
23320     case DW_OP_const1u:
23321     case DW_OP_const1s:
23322     case DW_OP_const2u:
23323     case DW_OP_const2s:
23324     case DW_OP_const4s:
23325     case DW_OP_const8s:
23326     case DW_OP_constu:
23327     case DW_OP_consts:
23328     case DW_OP_pick:
23329     case DW_OP_plus_uconst:
23330     case DW_OP_breg0:
23331     case DW_OP_breg1:
23332     case DW_OP_breg2:
23333     case DW_OP_breg3:
23334     case DW_OP_breg4:
23335     case DW_OP_breg5:
23336     case DW_OP_breg6:
23337     case DW_OP_breg7:
23338     case DW_OP_breg8:
23339     case DW_OP_breg9:
23340     case DW_OP_breg10:
23341     case DW_OP_breg11:
23342     case DW_OP_breg12:
23343     case DW_OP_breg13:
23344     case DW_OP_breg14:
23345     case DW_OP_breg15:
23346     case DW_OP_breg16:
23347     case DW_OP_breg17:
23348     case DW_OP_breg18:
23349     case DW_OP_breg19:
23350     case DW_OP_breg20:
23351     case DW_OP_breg21:
23352     case DW_OP_breg22:
23353     case DW_OP_breg23:
23354     case DW_OP_breg24:
23355     case DW_OP_breg25:
23356     case DW_OP_breg26:
23357     case DW_OP_breg27:
23358     case DW_OP_breg28:
23359     case DW_OP_breg29:
23360     case DW_OP_breg30:
23361     case DW_OP_breg31:
23362     case DW_OP_regx:
23363     case DW_OP_fbreg:
23364     case DW_OP_piece:
23365     case DW_OP_deref_size:
23366     case DW_OP_xderef_size:
23367       return valx1->v.val_int == valy1->v.val_int;
23368     case DW_OP_skip:
23369     case DW_OP_bra:
23370       gcc_assert (valx1->val_class == dw_val_class_loc
23371                   && valy1->val_class == dw_val_class_loc
23372                   && x->dw_loc_addr == y->dw_loc_addr);
23373       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23374     case DW_OP_implicit_value:
23375       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23376           || valx2->val_class != valy2->val_class)
23377         return false;
23378       switch (valx2->val_class)
23379         {
23380         case dw_val_class_const:
23381           return valx2->v.val_int == valy2->v.val_int;
23382         case dw_val_class_vec:
23383           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23384                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23385                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23386                             valx2->v.val_vec.elt_size
23387                             * valx2->v.val_vec.length) == 0;
23388         case dw_val_class_const_double:
23389           return valx2->v.val_double.low == valy2->v.val_double.low
23390                  && valx2->v.val_double.high == valy2->v.val_double.high;
23391         case dw_val_class_addr:
23392           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23393         default:
23394           gcc_unreachable ();
23395         }
23396     case DW_OP_bregx:
23397     case DW_OP_bit_piece:
23398       return valx1->v.val_int == valy1->v.val_int
23399              && valx2->v.val_int == valy2->v.val_int;
23400     case DW_OP_addr:
23401     hash_addr:
23402       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23403     case DW_OP_GNU_implicit_pointer:
23404       return valx1->val_class == dw_val_class_die_ref
23405              && valx1->val_class == valy1->val_class
23406              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23407              && valx2->v.val_int == valy2->v.val_int;
23408     case DW_OP_GNU_entry_value:
23409       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23410     default:
23411       /* Other codes have no operands.  */
23412       return true;
23413     }
23414 }
23415
23416 /* Return true if DWARF location expressions X and Y are the same.  */
23417
23418 static inline bool
23419 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23420 {
23421   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23422     if (x->dw_loc_opc != y->dw_loc_opc
23423         || x->dtprel != y->dtprel
23424         || !compare_loc_operands (x, y))
23425       break;
23426   return x == NULL && y == NULL;
23427 }
23428
23429 /* Return precomputed hash of location list X.  */
23430
23431 static hashval_t
23432 loc_list_hash (const void *x)
23433 {
23434   return ((const struct dw_loc_list_struct *) x)->hash;
23435 }
23436
23437 /* Return 1 if location lists X and Y are the same.  */
23438
23439 static int
23440 loc_list_eq (const void *x, const void *y)
23441 {
23442   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23443   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23444   if (a == b)
23445     return 1;
23446   if (a->hash != b->hash)
23447     return 0;
23448   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23449     if (strcmp (a->begin, b->begin) != 0
23450         || strcmp (a->end, b->end) != 0
23451         || (a->section == NULL) != (b->section == NULL)
23452         || (a->section && strcmp (a->section, b->section) != 0)
23453         || !compare_locs (a->expr, b->expr))
23454       break;
23455   return a == NULL && b == NULL;
23456 }
23457
23458 /* Recursively optimize location lists referenced from DIE
23459    children and share them whenever possible.  */
23460
23461 static void
23462 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23463 {
23464   dw_die_ref c;
23465   dw_attr_ref a;
23466   unsigned ix;
23467   void **slot;
23468
23469   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23470     if (AT_class (a) == dw_val_class_loc_list)
23471       {
23472         dw_loc_list_ref list = AT_loc_list (a);
23473         /* TODO: perform some optimizations here, before hashing
23474            it and storing into the hash table.  */
23475         hash_loc_list (list);
23476         slot = htab_find_slot_with_hash (htab, list, list->hash,
23477                                          INSERT);
23478         if (*slot == NULL)
23479           *slot = (void *) list;
23480         else
23481           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23482       }
23483
23484   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23485 }
23486
23487 /* Optimize location lists referenced from DIE
23488    children and share them whenever possible.  */
23489
23490 static void
23491 optimize_location_lists (dw_die_ref die)
23492 {
23493   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23494   optimize_location_lists_1 (die, htab);
23495   htab_delete (htab);
23496 }
23497 \f
23498 /* Output stuff that dwarf requires at the end of every file,
23499    and generate the DWARF-2 debugging info.  */
23500
23501 static void
23502 dwarf2out_finish (const char *filename)
23503 {
23504   limbo_die_node *node, *next_node;
23505   comdat_type_node *ctnode;
23506   htab_t comdat_type_table;
23507   unsigned int i;
23508
23509   gen_scheduled_generic_parms_dies ();
23510   gen_remaining_tmpl_value_param_die_attribute ();
23511
23512   /* Add the name for the main input file now.  We delayed this from
23513      dwarf2out_init to avoid complications with PCH.  */
23514   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23515   if (!IS_ABSOLUTE_PATH (filename))
23516     add_comp_dir_attribute (comp_unit_die ());
23517   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23518     {
23519       bool p = false;
23520       htab_traverse (file_table, file_table_relative_p, &p);
23521       if (p)
23522         add_comp_dir_attribute (comp_unit_die ());
23523     }
23524
23525   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23526     {
23527       add_location_or_const_value_attribute (
23528         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23529         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23530         false,
23531         DW_AT_location);
23532     }
23533
23534   /* Traverse the limbo die list, and add parent/child links.  The only
23535      dies without parents that should be here are concrete instances of
23536      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23537      For concrete instances, we can get the parent die from the abstract
23538      instance.  */
23539   for (node = limbo_die_list; node; node = next_node)
23540     {
23541       dw_die_ref die = node->die;
23542       next_node = node->next;
23543
23544       if (die->die_parent == NULL)
23545         {
23546           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23547
23548           if (origin)
23549             add_child_die (origin->die_parent, die);
23550           else if (is_cu_die (die))
23551             ;
23552           else if (seen_error ())
23553             /* It's OK to be confused by errors in the input.  */
23554             add_child_die (comp_unit_die (), die);
23555           else
23556             {
23557               /* In certain situations, the lexical block containing a
23558                  nested function can be optimized away, which results
23559                  in the nested function die being orphaned.  Likewise
23560                  with the return type of that nested function.  Force
23561                  this to be a child of the containing function.
23562
23563                  It may happen that even the containing function got fully
23564                  inlined and optimized out.  In that case we are lost and
23565                  assign the empty child.  This should not be big issue as
23566                  the function is likely unreachable too.  */
23567               tree context = NULL_TREE;
23568
23569               gcc_assert (node->created_for);
23570
23571               if (DECL_P (node->created_for))
23572                 context = DECL_CONTEXT (node->created_for);
23573               else if (TYPE_P (node->created_for))
23574                 context = TYPE_CONTEXT (node->created_for);
23575
23576               gcc_assert (context
23577                           && (TREE_CODE (context) == FUNCTION_DECL
23578                               || TREE_CODE (context) == NAMESPACE_DECL));
23579
23580               origin = lookup_decl_die (context);
23581               if (origin)
23582                 add_child_die (origin, die);
23583               else
23584                 add_child_die (comp_unit_die (), die);
23585             }
23586         }
23587     }
23588
23589   limbo_die_list = NULL;
23590
23591   resolve_addr (comp_unit_die ());
23592
23593   for (node = deferred_asm_name; node; node = node->next)
23594     {
23595       tree decl = node->created_for;
23596       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23597         {
23598           add_linkage_attr (node->die, decl);
23599           move_linkage_attr (node->die);
23600         }
23601     }
23602
23603   deferred_asm_name = NULL;
23604
23605   /* Walk through the list of incomplete types again, trying once more to
23606      emit full debugging info for them.  */
23607   retry_incomplete_types ();
23608
23609   if (flag_eliminate_unused_debug_types)
23610     prune_unused_types ();
23611
23612   /* Generate separate CUs for each of the include files we've seen.
23613      They will go into limbo_die_list.  */
23614   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23615     break_out_includes (comp_unit_die ());
23616
23617   /* Generate separate COMDAT sections for type DIEs. */
23618   if (use_debug_types)
23619     {
23620       break_out_comdat_types (comp_unit_die ());
23621
23622       /* Each new type_unit DIE was added to the limbo die list when created.
23623          Since these have all been added to comdat_type_list, clear the
23624          limbo die list.  */
23625       limbo_die_list = NULL;
23626
23627       /* For each new comdat type unit, copy declarations for incomplete
23628          types to make the new unit self-contained (i.e., no direct
23629          references to the main compile unit).  */
23630       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23631         copy_decls_for_unworthy_types (ctnode->root_die);
23632       copy_decls_for_unworthy_types (comp_unit_die ());
23633
23634       /* In the process of copying declarations from one unit to another,
23635          we may have left some declarations behind that are no longer
23636          referenced.  Prune them.  */
23637       prune_unused_types ();
23638     }
23639
23640   /* Traverse the DIE's and add add sibling attributes to those DIE's
23641      that have children.  */
23642   add_sibling_attributes (comp_unit_die ());
23643   for (node = limbo_die_list; node; node = node->next)
23644     add_sibling_attributes (node->die);
23645   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23646     add_sibling_attributes (ctnode->root_die);
23647
23648   /* Output a terminator label for the .text section.  */
23649   switch_to_section (text_section);
23650   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23651   if (cold_text_section)
23652     {
23653       switch_to_section (cold_text_section);
23654       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23655     }
23656
23657   /* We can only use the low/high_pc attributes if all of the code was
23658      in .text.  */
23659   if (!have_multiple_function_sections 
23660       || (dwarf_version < 3 && dwarf_strict))
23661     {
23662       /* Don't add if the CU has no associated code.  */
23663       if (text_section_used)
23664         {
23665           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23666           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23667         }
23668     }
23669   else
23670     {
23671       unsigned fde_idx = 0;
23672       bool range_list_added = false;
23673
23674       if (text_section_used)
23675         add_ranges_by_labels (comp_unit_die (), text_section_label,
23676                               text_end_label, &range_list_added);
23677       if (cold_text_section_used)
23678         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23679                               cold_end_label, &range_list_added);
23680
23681       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23682         {
23683           dw_fde_ref fde = &fde_table[fde_idx];
23684
23685           if (!fde->in_std_section)
23686             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23687                                   fde->dw_fde_end, &range_list_added);
23688           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23689             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
23690                                   fde->dw_fde_second_end, &range_list_added);
23691         }
23692
23693       if (range_list_added)
23694         {
23695           /* We need to give .debug_loc and .debug_ranges an appropriate
23696              "base address".  Use zero so that these addresses become
23697              absolute.  Historically, we've emitted the unexpected
23698              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23699              Emit both to give time for other tools to adapt.  */
23700           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23701           if (! dwarf_strict && dwarf_version < 4)
23702             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23703
23704           add_ranges (NULL);
23705         }
23706     }
23707
23708   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23709     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23710                     debug_line_section_label);
23711
23712   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23713     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23714
23715   if (have_location_lists)
23716     optimize_location_lists (comp_unit_die ());
23717
23718   /* Output all of the compilation units.  We put the main one last so that
23719      the offsets are available to output_pubnames.  */
23720   for (node = limbo_die_list; node; node = node->next)
23721     output_comp_unit (node->die, 0);
23722
23723   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23724   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23725     {
23726       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23727
23728       /* Don't output duplicate types.  */
23729       if (*slot != HTAB_EMPTY_ENTRY)
23730         continue;
23731
23732       /* Add a pointer to the line table for the main compilation unit
23733          so that the debugger can make sense of DW_AT_decl_file
23734          attributes.  */
23735       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23736         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23737                         debug_line_section_label);
23738
23739       output_comdat_type_unit (ctnode);
23740       *slot = ctnode;
23741     }
23742   htab_delete (comdat_type_table);
23743
23744   /* Output the main compilation unit if non-empty or if .debug_macinfo
23745      will be emitted.  */
23746   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23747
23748   /* Output the abbreviation table.  */
23749   switch_to_section (debug_abbrev_section);
23750   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23751   output_abbrev_section ();
23752
23753   /* Output location list section if necessary.  */
23754   if (have_location_lists)
23755     {
23756       /* Output the location lists info.  */
23757       switch_to_section (debug_loc_section);
23758       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23759                                    DEBUG_LOC_SECTION_LABEL, 0);
23760       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23761       output_location_lists (comp_unit_die ());
23762     }
23763
23764   /* Output public names table if necessary.  */
23765   if (!VEC_empty (pubname_entry, pubname_table))
23766     {
23767       gcc_assert (info_section_emitted);
23768       switch_to_section (debug_pubnames_section);
23769       output_pubnames (pubname_table);
23770     }
23771
23772   /* Output public types table if necessary.  */
23773   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23774      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23775      simply won't look for the section.  */
23776   if (!VEC_empty (pubname_entry, pubtype_table))
23777     {
23778       bool empty = false;
23779       
23780       if (flag_eliminate_unused_debug_types)
23781         {
23782           /* The pubtypes table might be emptied by pruning unused items.  */
23783           unsigned i;
23784           pubname_ref p;
23785           empty = true;
23786           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23787             if (p->die->die_offset != 0)
23788               {
23789                 empty = false;
23790                 break;
23791               }
23792         }
23793       if (!empty)
23794         {
23795           gcc_assert (info_section_emitted);
23796           switch_to_section (debug_pubtypes_section);
23797           output_pubnames (pubtype_table);
23798         }
23799     }
23800
23801   /* Output the address range information.  We only put functions in the
23802      arange table, so don't write it out if we don't have any.  */
23803   if (info_section_emitted)
23804     {
23805       unsigned long aranges_length = size_of_aranges ();
23806
23807       /* Empty .debug_aranges would contain just header and
23808          terminating 0,0.  */
23809       if (aranges_length
23810           != (unsigned long) (DWARF_ARANGES_HEADER_SIZE
23811                               + 2 * DWARF2_ADDR_SIZE))
23812         {
23813           switch_to_section (debug_aranges_section);
23814           output_aranges (aranges_length);
23815         }
23816     }
23817
23818   /* Output ranges section if necessary.  */
23819   if (ranges_table_in_use)
23820     {
23821       switch_to_section (debug_ranges_section);
23822       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23823       output_ranges ();
23824     }
23825
23826   /* Output the source line correspondence table.  We must do this
23827      even if there is no line information.  Otherwise, on an empty
23828      translation unit, we will generate a present, but empty,
23829      .debug_info section.  IRIX 6.5 `nm' will then complain when
23830      examining the file.  This is done late so that any filenames
23831      used by the debug_info section are marked as 'used'.  */
23832   switch_to_section (debug_line_section);
23833   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23834   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23835     output_line_info ();
23836
23837   /* Have to end the macro section.  */
23838   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23839     {
23840       switch_to_section (debug_macinfo_section);
23841       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23842       if (!VEC_empty (macinfo_entry, macinfo_table))
23843         output_macinfo ();
23844       dw2_asm_output_data (1, 0, "End compilation unit");
23845     }
23846
23847   /* If we emitted any DW_FORM_strp form attribute, output the string
23848      table too.  */
23849   if (debug_str_hash)
23850     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23851 }
23852
23853 #include "gt-dwarf2out.h"